2 define( 'YOURLS_ADMIN', true );
3 require_once( dirname( dirname( __FILE__ ) ).'/includes/load-yourls.php' );
4 yourls_maybe_require_auth();
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' );
12 // Default SQL behavior
13 $search_in_text = yourls__( 'URL' );
15 $sort_by_text = yourls__( 'Short URL' );
16 $sort_by = 'timestamp';
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";
31 if( !empty( $search ) && !empty( $_GET['search_in'] ) ) {
32 switch( $_GET['search_in'] ) {
34 $search_in_text = yourls__( 'All fields' );
38 $search_in_text = yourls__( 'Short URL' );
39 $search_in = 'keyword';
42 $search_in_text = yourls__( 'URL' );
46 $search_in_text = yourls__( 'Title' );
50 $search_in_text = yourls__( 'IP Address' );
54 $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 ) );
55 $search_url = yourls_sanitize_url( "&search=$search&search_in=$search_in" );
56 $search_text = $search;
57 $search = str_replace( '*', '%', '*' . yourls_escape( $search ) . '*' );
58 if( $search_in == 'all' ) {
59 $where .= " AND CONCAT_WS('',`keyword`,`url`,`title`,`ip`) LIKE ('$search')";
60 // Search across all fields. The resulting SQL will be something like:
61 // SELECT * FROM `yourls_url` WHERE CONCAT_WS('',`keyword`,`url`,`title`,`ip`) LIKE ("%ozh%")
62 // CONCAT_WS because CONCAT('foo', 'bar’, NULL) = NULL. NULL wins. Not sure if values can be NULL now or in the future, so better safe.
63 // TODO: pay attention to this bit when the DB schema changes
65 $where .= " AND `$search_in` LIKE ('$search')";
70 if( !empty( $_GET['date_filter'] ) ) {
71 switch( $_GET['date_filter'] ) {
73 $date_filter = 'before';
74 if( isset( $_GET['date_first'] ) && yourls_sanitize_date( $_GET['date_first'] ) ) {
75 $date_first = yourls_sanitize_date( $_GET['date_first'] );
76 $date_first_sql = yourls_sanitize_date_for_sql( $_GET['date_first'] );
77 $where .= " AND `timestamp` < '$date_first_sql'";
81 $date_filter = 'after';
82 if( isset( $_GET['date_first'] ) && yourls_sanitize_date( $_GET['date_first'] ) ) {
83 $date_first_sql = yourls_sanitize_date_for_sql( $_GET['date_first'] );
84 $date_first = yourls_sanitize_date( $_GET['date_first'] );
85 $where .= " AND `timestamp` > '$date_first_sql'";
89 $date_filter = 'between';
90 if( isset( $_GET['date_first'] ) && isset( $_GET['date_second'] ) && yourls_sanitize_date( $_GET['date_first'] ) && yourls_sanitize_date( $_GET['date_second'] ) ) {
91 $date_first_sql = yourls_sanitize_date_for_sql( $_GET['date_first'] );
92 $date_second_sql = yourls_sanitize_date_for_sql( $_GET['date_second'] );
93 $date_first = yourls_sanitize_date( $_GET['date_first'] );
94 $date_second = yourls_sanitize_date( $_GET['date_second'] );
95 $where .= " AND `timestamp` BETWEEN '$date_first_sql' AND '$date_second_sql'";
102 if( !empty( $_GET['sort_by'] ) || !empty( $_GET['sort_order'] ) ) {
103 switch( $_GET['sort_by'] ) {
105 $sort_by_text = yourls__( 'Short URL' );
106 $sort_by = 'keyword';
109 $sort_by_text = yourls__( 'URL' );
113 $sort_by_text = yourls__( 'Date' );
114 $sort_by = 'timestamp';
117 $sort_by_text = yourls__( 'IP Address' );
121 $sort_by_text = yourls__( 'Clicks' );
125 switch( $_GET['sort_order'] ) {
130 $sort_order = 'desc';
135 // Get URLs Count for current filter, total links in DB & total clicks
136 list( $total_urls, $total_clicks ) = array_values( yourls_get_db_stats() );
138 list( $total_items, $total_items_clicks ) = array_values( yourls_get_db_stats( $where ) );
140 $total_items = $total_urls;
141 $total_items_clicks = false;
144 // This is a bookmarklet
145 if ( isset( $_GET['u'] ) or isset( $_GET['up'] ) ) {
147 yourls_do_action( 'bookmarklet' );
149 // No sanitization needed here: everything happens in yourls_add_new_link()
150 if( isset( $_GET['u'] ) ) {
151 // Old school bookmarklet: ?u=<url>
152 $url = rawurldecode( $_GET['u'] );
154 // New style bookmarklet: ?up=<url protocol>&us=<url slashes>&ur=<url rest>
155 $url = rawurldecode( $_GET['up'] . $_GET['us'] . $_GET['ur'] );
157 $keyword = ( isset( $_GET['k'] ) ? ( $_GET['k'] ) : '' );
158 $title = ( isset( $_GET['t'] ) ? ( $_GET['t'] ) : '' );
159 $return = yourls_add_new_link( $url, $keyword, $title );
161 // If fails because keyword already exist, retry with no keyword
162 if ( isset( $return['status'] ) && $return['status'] == 'fail' && isset( $return['code'] ) && $return['code'] == 'error:keyword' ) {
163 $msg = $return['message'];
164 $return = yourls_add_new_link( $url, '', $ydb );
165 $return['message'] .= ' ('.$msg.')';
168 // Stop here if bookmarklet with a JSON callback function
169 if( isset( $_GET['jsonp'] ) && $_GET['jsonp'] == 'yourls' ) {
170 $short = $return['shorturl'] ? $return['shorturl'] : '';
171 $message = $return['message'];
172 yourls_content_type_header( 'application/javascript' );
173 echo yourls_apply_filter( 'bookmarklet_jsonp', "yourls_callback({'short_url':'$short','message':'$message'});" );
178 // Now use the URL that has been sanitized and returned by yourls_add_new_link()
179 $url = $return['url']['url'];
180 $where = sprintf( " AND `url` LIKE '%s' ", yourls_escape( $url ) );
182 $page = $total_pages = $perpage = 1;
185 $text = ( isset( $_GET['s'] ) ? stripslashes( $_GET['s'] ) : '' );
187 // Sharing with social bookmarklets
188 if( !empty($_GET['share']) ) {
189 yourls_do_action( 'pre_share_redirect' );
190 switch ( $_GET['share'] ) {
192 // share with Twitter
193 $destination = sprintf( "https://twitter.com/intent/tweet?url=%s&text=%s", urlencode( $return['shorturl'] ), urlencode( $title ) );
194 yourls_redirect( $destination, 303 );
196 // Deal with the case when redirection failed:
197 $return['status'] = 'error';
198 $return['errorCode'] = 400;
199 $return['message'] = yourls_s( 'Short URL created, but could not redirect to %s !', 'Twitter' );
203 // share with Facebook
204 $destination = sprintf( "https://www.facebook.com/sharer/sharer.php?u=%s&t=%s", urlencode( $return['shorturl'] ), urlencode( $title ) );
205 yourls_redirect( $destination, 303 );
207 // Deal with the case when redirection failed:
208 $return['status'] = 'error';
209 $return['errorCode'] = 400;
210 $return['message'] = yourls_s( 'Short URL created, but could not redirect to %s !', 'Facebook' );
215 $destination = sprintf( "https://www.tumblr.com/share?v=3&u=%s&t=%s&s=%s", urlencode( $return['shorturl'] ), urlencode( $title ), urlencode( $text ) );
216 yourls_redirect( $destination, 303 );
218 // Deal with the case when redirection failed:
219 $return['status'] = 'error';
220 $return['errorCode'] = 400;
221 $return['message'] = yourls_s( 'Short URL created, but could not redirect to %s !', 'Tumblr' );
225 // Is there a custom registered social bookmark?
226 yourls_do_action( 'share_redirect_' . $_GET['share'], $return );
228 // Still here? That was an unknown 'share' method, then.
229 $return['status'] = 'error';
230 $return['errorCode'] = 400;
231 $return['message'] = yourls__( 'Unknown "Share" bookmarklet' );
236 // This is not a bookmarklet
238 $is_bookmark = false;
240 // Checking $page, $offset, $perpage
241 if( empty($page) || $page == 0 ) {
244 if( empty($offset) ) {
247 if( empty($perpage) || $perpage == 0) {
252 $offset = ( $page-1 ) * $perpage;
254 // Determine Max Number Of Items To Display On Page
255 if( ( $offset + $perpage ) > $total_items ) {
256 $max_on_page = $total_items;
258 $max_on_page = ( $offset + $perpage );
261 // Determine Number Of Items To Display On Page
262 if ( ( $offset + 1 ) > $total_items ) {
263 $display_on_page = $total_items;
265 $display_on_page = ( $offset + 1 );
268 // Determing Total Amount Of Pages
269 $total_pages = ceil( $total_items / $perpage );
273 // Begin output of the page
274 $context = ( $is_bookmark ? 'bookmark' : 'index' );
275 yourls_html_head( $context );
279 yourls_do_action( 'admin_page_before_content' );
281 if ( !$is_bookmark ) { ?>
282 <p><?php echo $search_sentence; ?></p>
284 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 );
285 if( $total_items_clicks !== false )
286 echo ", " . sprintf( yourls_n( 'counting <strong>1</strong> click', 'counting <strong>%s</strong> clicks', $total_items_clicks ), yourls_number_format_i18n( $total_items_clicks ) );
289 <p><?php printf( yourls__( 'Overall, tracking <strong class="increment">%1$s</strong> links, <strong id="total_clicks">%2$s</strong> clicks, and counting!' ), yourls_number_format_i18n( $total_urls ), yourls_number_format_i18n( $total_clicks ) ); ?></p>
292 yourls_do_action( 'admin_page_before_form' );
294 yourls_html_addnew();
296 // If bookmarklet, add message. Otherwise, hide hidden share box.
297 if ( !$is_bookmark ) {
298 yourls_share_box( '', '', '', '', '', '', true );
300 echo '<script type="text/javascript">$(document).ready(function(){
301 feedback( "' . $return['message'] . '", "'. $return['status'] .'");
306 yourls_do_action( 'admin_page_before_table' );
310 if ( !$is_bookmark ) {
313 'search_text' => $search_text,
314 'search_in' => $search_in,
315 'sort_by' => $sort_by,
316 'sort_order' => $sort_order,
318 'perpage' => $perpage,
319 'click_filter' => $click_filter,
320 'click_limit' => $click_limit,
321 'total_pages' => $total_pages,
322 'date_filter' => $date_filter,
323 'date_first' => $date_first,
324 'date_second' => $date_second,
326 yourls_html_tfooter( $params );
329 yourls_table_tbody_start();
332 $where = yourls_apply_filter( 'admin_list_where', $where );
333 $url_results = $ydb->get_results( "SELECT * FROM `$table_url` WHERE 1=1 $where ORDER BY `$sort_by` $sort_order LIMIT $offset, $perpage;" );
337 foreach( $url_results as $url_result ) {
338 $keyword = yourls_sanitize_string( $url_result->keyword );
339 $timestamp = strtotime( $url_result->timestamp );
340 $url = stripslashes( $url_result->url );
341 $ip = $url_result->ip;
342 $title = $url_result->title ? $url_result->title : '';
343 $clicks = $url_result->clicks;
345 echo yourls_table_add_row( $keyword, $url, $title, $ip, $clicks, $timestamp );
349 $display = $found_rows ? 'display:none' : '';
350 echo '<tr id="nourl_found" style="'.$display.'"><td colspan="6">' . yourls__('No URL') . '</td></tr>';
352 yourls_table_tbody_end();
356 yourls_do_action( 'admin_page_after_table' );
359 yourls_share_box( $url, $return['shorturl'], $title, $text );
362 <?php yourls_html_footer( ); ?>