[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/admin/ -> index.php (source)

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


Generated: Wed Oct 15 05:10:31 2025 Cross-referenced by PHPXref 0.7.1