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


Generated: Fri Mar 28 05:10:25 2025 Cross-referenced by PHPXref 0.7.1