]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - admin/index.php
Removed extra quotes in bookmarklets
[Github/YOURLS.git] / admin / index.php
1 <?php\r
2 define( 'YOURLS_ADMIN', true );\r
3 require_once( dirname( dirname( __FILE__ ) ).'/includes/load-yourls.php' );\r
4 yourls_maybe_require_auth();\r
5 \r
6 // Variables\r
7 $table_url = YOURLS_DB_TABLE_URL;\r
8 $where = $search_sentence = $search_text = $url = $keyword = '';\r
9 $date_filter = $date_first  = $date_second = '';\r
10 $base_page   = yourls_admin_url( 'index.php' );\r
11 \r
12 // Default SQL behavior\r
13 $search_in_text  = yourls__( 'URL' );\r
14 $search_in       = 'url';\r
15 $sort_by_text    = yourls__( 'Short URL' );\r
16 $sort_by         = 'timestamp';\r
17 $sort_order_text = yourls__( 'Descending Order' );\r
18 $sort_order      = 'desc';\r
19 $page            = ( isset( $_GET['page'] ) ? intval($_GET['page']) : 1 );\r
20 $search          = ( isset( $_GET['search'] ) ? htmlspecialchars( trim($_GET['search']) ) : '' );\r
21 $perpage         = ( isset( $_GET['perpage'] ) && intval( $_GET['perpage'] ) ? intval($_GET['perpage']) : 15 );\r
22 $click_limit     = ( isset( $_GET['click_limit'] ) && $_GET['click_limit'] !== '' ) ? intval( $_GET['click_limit'] ) : '' ;\r
23 if ( $click_limit !== '' ) {\r
24         $click_filter   = ( isset( $_GET['click_filter'] ) && $_GET['click_filter'] == 'more' ? 'more' : 'less' ) ;\r
25         $click_moreless = ( $click_filter == 'more' ? '>' : '<' );\r
26         $where          = " AND clicks $click_moreless $click_limit";\r
27 } else {\r
28         $click_filter   = '';\r
29 }\r
30 \r
31 // Searching\r
32 if( !empty( $search ) && !empty( $_GET['search_in'] ) ) {\r
33         switch( $_GET['search_in'] ) {\r
34                 case 'keyword':\r
35                         $search_in_text = yourls__( 'Short URL' );\r
36                         $search_in      = 'keyword';\r
37                         break;\r
38                 case 'url':\r
39                         $search_in_text = yourls__( 'URL' );\r
40                         $search_in      = 'url';\r
41                         break;\r
42                 case 'title':\r
43                         $search_in_text = yourls__( 'Title' );\r
44                         $search_in      = 'title';\r
45                         break;\r
46                 case 'ip':\r
47                         $search_in_text = yourls__( 'IP Address' );\r
48                         $search_in      = 'ip';\r
49                         break;\r
50         }\r
51         $search_sentence = yourls_s( 'Searching for <strong>%s</strong> in <strong>%s</strong>.', yourls_esc_html( $search ), yourls_esc_html( $search_in_text ) );\r
52         $search_url      = yourls_sanitize_url( "&amp;search=$search&amp;search_in=$search_in" );\r
53         $search_text     = $search;\r
54         $search          = str_replace( '*', '%', '*' . yourls_escape( $search ) . '*' );\r
55         $where .= " AND `$search_in` LIKE ('$search')";\r
56 }\r
57 \r
58 // Time span\r
59 if( !empty( $_GET['date_filter'] ) ) {\r
60         switch( $_GET['date_filter'] ) {\r
61                 case 'before':\r
62                         $date_filter = 'before';\r
63                         if( isset( $_GET['date_first'] ) && yourls_sanitize_date( $_GET['date_first'] ) ) {\r
64                                 $date_first     = yourls_sanitize_date( $_GET['date_first'] );\r
65                                 $date_first_sql = yourls_sanitize_date_for_sql( $_GET['date_first'] );\r
66                                 $where .= " AND `timestamp` < '$date_first_sql'";\r
67                         }\r
68                         break;\r
69                 case 'after':\r
70                         $date_filter = 'after';\r
71                         if( isset( $_GET['date_first'] ) && yourls_sanitize_date( $_GET['date_first'] ) ) {\r
72                                 $date_first_sql = yourls_sanitize_date_for_sql( $_GET['date_first'] );\r
73                                 $date_first     = yourls_sanitize_date( $_GET['date_first'] );\r
74                                 $where .= " AND `timestamp` > '$date_first_sql'";\r
75                         }\r
76                         break;\r
77                 case 'between':\r
78                         $date_filter = 'between';\r
79                         if( isset( $_GET['date_first'] ) && isset( $_GET['date_second'] ) && yourls_sanitize_date( $_GET['date_first'] ) && yourls_sanitize_date( $_GET['date_second'] ) ) {\r
80                                 $date_first_sql  = yourls_sanitize_date_for_sql( $_GET['date_first'] );\r
81                                 $date_second_sql = yourls_sanitize_date_for_sql( $_GET['date_second'] );\r
82                                 $date_first      = yourls_sanitize_date( $_GET['date_first'] );\r
83                                 $date_second     = yourls_sanitize_date( $_GET['date_second'] );\r
84                                 $where .= " AND `timestamp` BETWEEN '$date_first_sql' AND '$date_second_sql'";\r
85                         }\r
86                         break;\r
87         }\r
88 }\r
89 \r
90 // Sorting\r
91 if( !empty( $_GET['sort_by'] ) || !empty( $_GET['sort_order'] ) ) {\r
92         switch( $_GET['sort_by'] ) {\r
93                 case 'keyword':\r
94                         $sort_by_text = yourls__( 'Short URL' );\r
95                         $sort_by      = 'keyword';\r
96                         break;\r
97                 case 'url':\r
98                         $sort_by_text = yourls__( 'URL' );\r
99                         $sort_by      = 'url';\r
100                         break;\r
101                 case 'timestamp':\r
102                         $sort_by_text = yourls__( 'Date' );\r
103                         $sort_by      = 'timestamp';\r
104                         break;\r
105                 case 'ip':\r
106                         $sort_by_text = yourls__( 'IP Address' );\r
107                         $sort_by      = 'ip';\r
108                         break;\r
109                 case 'clicks':\r
110                         $sort_by_text = yourls__( 'Clicks' );\r
111                         $sort_by      = 'clicks';\r
112                         break;\r
113         }\r
114         switch( $_GET['sort_order'] ) {\r
115                 case 'asc':\r
116                         $sort_order_text = yourls__( 'Ascending Order' );\r
117                         $sort_order      = 'asc';\r
118                         break;\r
119                 case 'desc':\r
120                         $sort_order_text = yourls__( 'Descending Order' );\r
121                         $sort_order      = 'desc';\r
122                         break;\r
123         }\r
124 }\r
125 \r
126 // Get URLs Count for current filter, total links in DB & total clicks\r
127 list( $total_urls, $total_clicks ) = array_values( yourls_get_db_stats() );\r
128 if ( $where ) {\r
129         list( $total_items, $total_items_clicks ) = array_values( yourls_get_db_stats( $where ) );\r
130 } else {\r
131         $total_items        = $total_urls;\r
132         $total_items_clicks = false;\r
133 }\r
134 \r
135 // This is a bookmarklet\r
136 if ( isset( $_GET['u'] ) ) {\r
137         $is_bookmark = true;\r
138         yourls_do_action( 'bookmarklet' );\r
139 \r
140         // No sanitization needed here: everything happens in yourls_add_new_link()\r
141         $url     = ( $_GET['u'] );\r
142         $keyword = ( isset( $_GET['k'] ) ? ( $_GET['k'] ) : '' );\r
143         $title   = ( isset( $_GET['t'] ) ? ( $_GET['t'] ) : '' );\r
144         $return  = yourls_add_new_link( $url, $keyword, $title );\r
145         \r
146         // If fails because keyword already exist, retry with no keyword\r
147         if ( isset( $return['status'] ) && $return['status'] == 'fail' && isset( $return['code'] ) && $return['code'] == 'error:keyword' ) {\r
148                 $msg = $return['message'];\r
149                 $return = yourls_add_new_link( $url, '', $ydb );\r
150                 $return['message'] .= ' ('.$msg.')';\r
151         }\r
152         \r
153         // Stop here if bookmarklet with a JSON callback function\r
154         if( isset( $_GET['jsonp'] ) && $_GET['jsonp'] == 'yourls' ) {\r
155                 $short   = $return['shorturl'] ? $return['shorturl'] : '';\r
156                 $message = $return['message'];\r
157                 header( 'Content-type: application/json' );\r
158                 echo yourls_apply_filter( 'bookmarklet_jsonp', "yourls_callback({'short_url':'$short','message':'$message'});" );\r
159                 \r
160                 die();\r
161         }\r
162         \r
163         // Now use the URL that has been sanitized and returned by yourls_add_new_link()\r
164         $url = $return['url']['url'];\r
165         $where  = sprintf( " AND `url` LIKE '%s' ", yourls_escape( $url ) );\r
166         \r
167         $page   = $total_pages = $perpage = 1;\r
168         $offset = 0;\r
169         \r
170         $text   = ( isset( $_GET['s'] ) ? stripslashes( $_GET['s'] ) : '' );\r
171         \r
172 \r
173 // This is not a bookmarklet\r
174 } else {\r
175         $is_bookmark = false;\r
176         \r
177         // Checking $page, $offset, $perpage\r
178         if( empty($page) || $page == 0 ) {\r
179                 $page = 1;\r
180         }\r
181         if( empty($offset) ) {\r
182                 $offset = 0;\r
183         }\r
184         if( empty($perpage) || $perpage == 0) {\r
185                 $perpage = 50;\r
186         }\r
187 \r
188         // Determine $offset\r
189         $offset = ( $page-1 ) * $perpage;\r
190 \r
191         // Determine Max Number Of Items To Display On Page\r
192         if( ( $offset + $perpage ) > $total_items ) { \r
193                 $max_on_page = $total_items; \r
194         } else { \r
195                 $max_on_page = ( $offset + $perpage ); \r
196         }\r
197 \r
198         // Determine Number Of Items To Display On Page\r
199         if ( ( $offset + 1 ) > $total_items ) { \r
200                 $display_on_page = $total_items; \r
201         } else { \r
202                 $display_on_page = ( $offset + 1 ); \r
203         }\r
204 \r
205         // Determing Total Amount Of Pages\r
206         $total_pages = ceil( $total_items / $perpage );\r
207 }\r
208 \r
209 \r
210 // Begin output of the page\r
211 $context = ( $is_bookmark ? 'bookmark' : 'index' );\r
212 yourls_html_head( $context );\r
213 yourls_html_logo();\r
214 yourls_html_menu() ;\r
215 \r
216 yourls_do_action( 'admin_page_before_content' );\r
217 \r
218 if ( !$is_bookmark ) { ?>\r
219         <p><?php echo $search_sentence; ?></p>\r
220         <p><?php\r
221                 printf( yourls__( 'Display <strong>%s</strong> to <strong class="increment">%s</strong> of <strong class="increment">%s</strong> URLs' ), $display_on_page, $max_on_page, $total_items );\r
222                 if( $total_items_clicks !== false )\r
223                         echo ", " . sprintf( yourls__( 'counting <strong>1</strong> click', 'counting <strong>%s</strong> clicks', $total_items_clicks ), $total_items_clicks );\r
224         ?>.</p>\r
225 <?php } ?>\r
226 <p><?php printf( yourls__( 'Overall, tracking <strong class="increment">%s</strong> links, <strong>%s</strong> clicks, and counting!' ), yourls_number_format_i18n( $total_urls ), yourls_number_format_i18n( $total_clicks ) ); ?></p>\r
227 <?php yourls_do_action( 'admin_page_before_form' ); ?>\r
228 \r
229 <?php yourls_html_addnew(); ?>\r
230 \r
231 <?php\r
232 // If bookmarklet, add message. Otherwise, hide hidden share box.\r
233 if ( !$is_bookmark ) {\r
234         yourls_share_box( '', '', '', '', '', '', true );\r
235 } else {\r
236         echo '<script type="text/javascript">$(document).ready(function(){\r
237                 feedback( "' . $return['message'] . '", "'. $return['status'] .'");\r
238                 init_clipboard();\r
239         });</script>';\r
240 }\r
241 \r
242 yourls_do_action( 'admin_page_before_table' );\r
243 \r
244 yourls_table_head();\r
245 \r
246 if ( !$is_bookmark ) {\r
247         $params = array(\r
248                 'search'      => $search,\r
249                 'search_text' => $search_text,\r
250                 'search_in'   => $search_in,\r
251                 'sort_by'     => $sort_by,\r
252                 'sort_order'  => $sort_order,\r
253                 'page'        => $page,\r
254                 'perpage'     => $perpage,\r
255                 'click_filter' => $click_filter,\r
256                 'click_limit'  => $click_limit,\r
257                 'total_pages' => $total_pages,\r
258                 'date_filter' => $date_filter,\r
259                 'date_first'  => $date_first,\r
260                 'date_second' => $date_second,\r
261         );\r
262         yourls_html_tfooter( $params );\r
263 }\r
264 \r
265 yourls_table_tbody_start();\r
266 \r
267 // Main Query\r
268 $where = yourls_apply_filter( 'admin_list_where', $where );\r
269 $url_results = $ydb->get_results( "SELECT * FROM `$table_url` WHERE 1=1 $where ORDER BY `$sort_by` $sort_order LIMIT $offset, $perpage;" );\r
270 $found_rows = false;\r
271 if( $url_results ) {\r
272         $found_rows = true;\r
273         foreach( $url_results as $url_result ) {\r
274                 $keyword = yourls_sanitize_string( $url_result->keyword );\r
275                 $timestamp = strtotime( $url_result->timestamp );\r
276                 $url = stripslashes( $url_result->url );\r
277                 $ip = $url_result->ip;\r
278                 $title = $url_result->title ? $url_result->title : '';\r
279                 $clicks = $url_result->clicks;\r
280 \r
281                 echo yourls_table_add_row( $keyword, $url, $title, $ip, $clicks, $timestamp );\r
282         }\r
283 }\r
284 \r
285 $display = $found_rows ? 'display:none' : '';\r
286 echo '<tr id="nourl_found" style="'.$display.'"><td colspan="6">' . yourls__('No URL') . '</td></tr>';\r
287 \r
288 yourls_table_tbody_end();\r
289 \r
290 yourls_table_end();\r
291 \r
292 yourls_do_action( 'admin_page_after_table' );\r
293 \r
294 if ( $is_bookmark )\r
295         yourls_share_box( $url, $return['shorturl'], $title, $text );\r
296 ?>\r
297         \r
298 <?php yourls_html_footer( ); ?>