[ 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      $json_flags  = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT;
 112      yourls_do_action( 'bookmarklet' );
 113  
 114      // No sanitization needed here: everything happens in yourls_add_new_link()
 115      if( isset( $_GET['u'] ) ) {
 116          // Old school bookmarklet: ?u=<url>
 117          $url = $_GET['u'];
 118      } else {
 119          // New style bookmarklet: ?up=<url protocol>&us=<url slashes>&ur=<url rest>
 120          $url = $_GET['up'] . $_GET['us'] . $_GET['ur'];
 121      }
 122      $keyword = ( isset( $_GET['k'] ) ? ( $_GET['k'] ) : '' );
 123      $title   = ( isset( $_GET['t'] ) ? ( $_GET['t'] ) : '' );
 124      $return  = yourls_add_new_link( $url, $keyword, $title );
 125  
 126      // If fails because keyword already exist, retry with no keyword
 127      if ( isset( $return['status'] ) && $return['status'] == 'fail' && isset( $return['code'] ) && $return['code'] == 'error:keyword' ) {
 128          $msg = $return['message'];
 129          $return = yourls_add_new_link( $url, '' );
 130          $return['message'] .= ' ('.$msg.')';
 131      }
 132  
 133      // Stop here if bookmarklet with a JSON callback function
 134      if( isset( $_GET['jsonp'] ) && $_GET['jsonp'] == 'yourls' ) {
 135          $short   = $return['shorturl'] ? $return['shorturl'] : '';
 136          $message = $return['message'];
 137          $json    = json_encode( array( 'short_url' => $short, 'message' => $message ), $json_flags );
 138          yourls_content_type_header( 'application/javascript' );
 139          echo yourls_apply_filter( 'bookmarklet_jsonp', "yourls_callback($json);" );
 140  
 141          die();
 142      }
 143  
 144      // Now use the URL that has been sanitized and returned by yourls_add_new_link()
 145      $url = $return['url']['url'];
 146      $where['sql'] .= ' AND `url` LIKE :url ';
 147      $where['binds']['url'] = $url;
 148  
 149      $page   = $total_pages = $perpage = 1;
 150      $offset = 0;
 151  
 152      $text   = ( isset( $_GET['s'] ) ? stripslashes( $_GET['s'] ) : '' );
 153  
 154      // Sharing with social bookmarklets
 155      if( !empty($_GET['share']) ) {
 156          yourls_do_action( 'pre_share_redirect' );
 157          switch ( $_GET['share'] ) {
 158              case 'twitter':
 159                  // share with Twitter
 160                  $destination = sprintf( "https://twitter.com/intent/tweet?url=%s&text=%s", urlencode( $return['shorturl'] ), urlencode( $title ) );
 161                  yourls_redirect( $destination, 303 );
 162  
 163                  // Deal with the case when redirection failed:
 164                  $return['status']    = 'error';
 165                  $return['errorCode'] = '400';
 166                  $return['message']   = yourls_s( 'Short URL created, but could not redirect to %s !', 'Twitter' );
 167                  break;
 168  
 169              case 'facebook':
 170                  // share with Facebook
 171                  $destination = sprintf( "https://www.facebook.com/sharer/sharer.php?u=%s&t=%s", urlencode( $return['shorturl'] ), urlencode( $title ) );
 172                  yourls_redirect( $destination, 303 );
 173  
 174                  // Deal with the case when redirection failed:
 175                  $return['status']    = 'error';
 176                  $return['errorCode'] = '400';
 177                  $return['message']   = yourls_s( 'Short URL created, but could not redirect to %s !', 'Facebook' );
 178                  break;
 179  
 180              case 'tumblr':
 181                  // share with Tumblr
 182                  $destination = sprintf( "https://www.tumblr.com/share?v=3&u=%s&t=%s&s=%s", urlencode( $return['shorturl'] ), urlencode( $title ), urlencode( $text ) );
 183                  yourls_redirect( $destination, 303 );
 184  
 185                  // Deal with the case when redirection failed:
 186                  $return['status']    = 'error';
 187                  $return['errorCode'] = '400';
 188                  $return['message']   = yourls_s( 'Short URL created, but could not redirect to %s !', 'Tumblr' );
 189                  break;
 190  
 191              default:
 192                  // Is there a custom registered social bookmark?
 193                  yourls_do_action( 'share_redirect_' . $_GET['share'], $return );
 194  
 195                  // Still here? That was an unknown 'share' method, then.
 196                  $return['status']    = 'error';
 197                  $return['errorCode'] = '400';
 198                  $return['message']   = yourls__( 'Unknown "Share" bookmarklet' );
 199                  break;
 200          }
 201      }
 202  
 203  // This is not a bookmarklet
 204  } else {
 205      $is_bookmark = false;
 206  
 207      // Checking $page, $offset, $perpage
 208      if( empty($page) || $page == 0 ) {
 209          $page = 1;
 210      }
 211      if( empty($offset) ) {
 212          $offset = 0;
 213      }
 214      if( empty($perpage) || $perpage == 0) {
 215          $perpage = 50;
 216      }
 217  
 218      // Determine $offset
 219      $offset = ( $page-1 ) * $perpage;
 220  
 221      // Determine Max Number Of Items To Display On Page
 222      if( ( $offset + $perpage ) > $total_items ) {
 223          $max_on_page = $total_items;
 224      } else {
 225          $max_on_page = ( $offset + $perpage );
 226      }
 227  
 228      // Determine Number Of Items To Display On Page
 229      if ( ( $offset + 1 ) > $total_items ) {
 230          $display_on_page = $total_items;
 231      } else {
 232          $display_on_page = ( $offset + 1 );
 233      }
 234  
 235      // Determine Total Amount Of Pages
 236      $total_pages = ceil( $total_items / $perpage );
 237  }
 238  
 239  
 240  // Begin output of the page
 241  $context = ( $is_bookmark ? 'bookmark' : 'index' );
 242  yourls_html_head( $context );
 243  yourls_html_logo();
 244  yourls_html_menu() ;
 245  
 246  yourls_do_action( 'admin_page_before_content' );
 247  
 248  if ( !$is_bookmark ) { ?>
 249      <p><?php echo $search_sentence; ?></p>
 250      <p><?php
 251          if ( $total_items === 0 ) {
 252              printf( yourls__( 'No URLs.' ) );
 253              if ( ! empty( $search ) )
 254                  printf( ' ' . yourls__( 'Try being less specific' ) );
 255          } else {
 256              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 );
 257              if( $total_items_clicks !== false )
 258                  echo ", " . sprintf( yourls_n( 'counting <strong>1</strong> click', 'counting <strong>%s</strong> clicks', $total_items_clicks ), yourls_number_format_i18n( $total_items_clicks ) );
 259          }
 260      ?>.</p>
 261  <?php } ?>
 262  <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>
 263  <?php
 264  
 265  yourls_do_action( 'admin_page_before_form' );
 266  
 267  yourls_html_addnew();
 268  
 269  // If bookmarklet, add message. Otherwise, hide hidden share box.
 270  if ( !$is_bookmark ) {
 271      yourls_share_box( '', '', '', '', '', '', true );
 272  } else {
 273      echo '<script type="text/javascript">$(document).ready(function(){
 274          feedback( ' . json_encode( $return['message'], $json_flags ) . ', ' . json_encode( $return['status'], $json_flags ) . ' );
 275          init_clipboard();
 276      });</script>';
 277  }
 278  
 279  yourls_do_action( 'admin_page_before_table' );
 280  
 281  yourls_table_head();
 282  
 283  if ( !$is_bookmark ) {
 284      $params = array(
 285          'search'       => $search,
 286          'search_text'  => $search_text,
 287          'search_in'    => $search_in,
 288          'sort_by'      => $sort_by,
 289          'sort_order'   => $sort_order,
 290          'page'         => $page,
 291          'perpage'      => $perpage,
 292          'click_filter' => $click_filter,
 293          'click_limit'  => $click_limit,
 294          'total_pages'  => $total_pages,
 295          'date_filter'  => $date_filter,
 296          'date_first'   => $date_first,
 297          'date_second'  => $date_second,
 298      );
 299      yourls_html_tfooter( $params );
 300  }
 301  
 302  yourls_table_tbody_start();
 303  
 304  // Main Query
 305  $where = yourls_apply_filter( 'admin_list_where', $where );
 306  $url_results = yourls_get_db('read-admin_index')->fetchObjects( "SELECT * FROM `$table_url` WHERE 1=1 {$where['sql']} ORDER BY `$sort_by` $sort_order LIMIT $offset, $perpage;", $where['binds'] );
 307  $found_rows = false;
 308  if( $url_results ) {
 309      $found_rows = true;
 310      foreach( $url_results as $url_result ) {
 311          $keyword = yourls_sanitize_keyword($url_result->keyword);
 312          $timestamp = strtotime( $url_result->timestamp );
 313          $url = stripslashes( $url_result->url );
 314          $ip = $url_result->ip;
 315          $title = $url_result->title ? $url_result->title : '';
 316          $clicks = $url_result->clicks;
 317  
 318          echo yourls_table_add_row( $keyword, $url, $title, $ip, $clicks, $timestamp );
 319      }
 320  }
 321  
 322  $display = $found_rows ? 'display:none' : '';
 323  echo '<tr id="nourl_found" style="'.$display.'"><td colspan="6">' . yourls__('No URL') . '</td></tr>';
 324  
 325  yourls_table_tbody_end();
 326  
 327  yourls_table_end();
 328  
 329  yourls_do_action( 'admin_page_after_table' );
 330  
 331  yourls_delete_link_modal();
 332  
 333  if ( $is_bookmark )
 334      yourls_share_box( $url, $return['shorturl'], $title, $text );
 335  ?>
 336  
 337  <?php yourls_html_footer( ); ?>


Generated: Mon Nov 9 05:10:31 2026 Cross-referenced by PHPXref 0.7.1