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