[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/ -> functions-shorturls.php (source)

   1  <?php
   2  /*
   3   * Functions relative to short URLs: adding, editing, etc
   4   * (either proper short URLs ("http://sho.rt/abc") or "keywords" (the "abc" part)
   5   */
   6  
   7  
   8  /**
   9   * Add a new link in the DB, either with custom keyword, or find one
  10   *
  11   * The return array will contain at least the following keys:
  12   *    status: string, 'success' or 'fail'
  13   *    message: string, a descriptive localized message of what happened in any case
  14   *    code: string, a short descriptivish and untranslated message describing what happened
  15   *    errorCode: string, a HTTP status code
  16   *    statusCode: string, a HTTP status code
  17   * Depending on the operation, it will contain any of the following keys:
  18   *    url: array, the short URL creation information, with keys: 'keyword', 'url', 'title', 'date', 'ip', 'clicks'
  19   *    title: string, the URL title
  20   *    shorturl: string, the proper short URL in full (eg 'http://sho.rt/abc')
  21   *    html: string, the HTML part used by the ajax to update the page display if any
  22   *
  23   * For compatibility with early consumers and third parties, when people asked for various data and data formats
  24   * before the internal API was really structured, the return array now collects several redundant information.
  25   *
  26   * @param  string $url      URL to shorten
  27   * @param  string $keyword  optional "keyword"
  28   * @param  string $title    option title
  29   * @param  int    $row_id   used to form unique IDs in the generated HTML
  30   * @return array            array with error/success state and short URL information
  31   */
  32  function yourls_add_new_link( $url, $keyword = '', $title = '', $row_id = 1 ) {
  33      // Allow plugins to short-circuit the whole function
  34      $pre = yourls_apply_filter( 'shunt_add_new_link', false, $url, $keyword, $title );
  35      if ( false !== $pre ) {
  36          return $pre;
  37      }
  38  
  39      /**
  40       * The result array.
  41       */
  42      $return = [
  43          // Always present :
  44          'status' => '',
  45          'code'   => '',
  46          'message' => '',
  47          'errorCode' => '',
  48          'statusCode' => '',
  49      ];
  50  
  51      // Sanitize URL
  52      $url = yourls_sanitize_url( $url );
  53      if ( !$url || $url == 'http://' || $url == 'https://' ) {
  54          $return['status']    = 'fail';
  55          $return['code']      = 'error:nourl';
  56          $return['message']   = yourls__( 'Missing or malformed URL' );
  57          $return['errorCode'] = $return['statusCode'] = '400'; // 400 Bad Request
  58  
  59          return yourls_apply_filter( 'add_new_link_fail_nourl', $return, $url, $keyword, $title );
  60      }
  61  
  62      // Prevent DB flood
  63      $ip = yourls_get_IP();
  64      yourls_check_IP_flood( $ip );
  65  
  66      // Prevent internal redirection loops: cannot shorten a shortened URL
  67      if (yourls_is_shorturl($url)) {
  68          $return['status']    = 'fail';
  69          $return['code']      = 'error:noloop';
  70          $return['message']   = yourls__( 'URL is a short URL' );
  71          $return['errorCode'] = $return['statusCode'] = '400'; // 400 Bad Request
  72          return yourls_apply_filter( 'add_new_link_fail_noloop', $return, $url, $keyword, $title );
  73      }
  74  
  75      yourls_do_action( 'pre_add_new_link', $url, $keyword, $title );
  76  
  77      // Check if URL was already stored and we don't accept duplicates
  78      if ( !yourls_allow_duplicate_longurls() && ($url_exists = yourls_long_url_exists( $url )) ) {
  79          yourls_do_action( 'add_new_link_already_stored', $url, $keyword, $title );
  80  
  81          $return['status']   = 'fail';
  82          $return['code']     = 'error:url';
  83          $return['url']      = array( 'keyword' => $url_exists->keyword, 'url' => $url, 'title' => $url_exists->title, 'date' => $url_exists->timestamp, 'ip' => $url_exists->ip, 'clicks' => $url_exists->clicks );
  84          $return['message']  = /* //translators: eg "http://someurl/ already exists (short URL: sho.rt/abc)" */ yourls_s('%s already exists in database (short URL: %s)',
  85              yourls_trim_long_string($url), preg_replace('!https?://!', '',  yourls_get_yourls_site()) . '/'. $url_exists->keyword );
  86          $return['title']    = $url_exists->title;
  87          $return['shorturl'] = yourls_link($url_exists->keyword);
  88          $return['errorCode'] = $return['statusCode'] = '400'; // 400 Bad Request
  89  
  90          return yourls_apply_filter( 'add_new_link_already_stored_filter', $return, $url, $keyword, $title );
  91      }
  92  
  93      // Sanitize provided title, or fetch one
  94      if( isset( $title ) && !empty( $title ) ) {
  95          $title = yourls_sanitize_title( $title );
  96      } else {
  97          $title = yourls_get_remote_title( $url );
  98      }
  99      $title = yourls_apply_filter( 'add_new_title', $title, $url, $keyword );
 100  
 101      // Custom keyword provided : sanitize and make sure it's free
 102      if ($keyword) {
 103          yourls_do_action( 'add_new_link_custom_keyword', $url, $keyword, $title );
 104  
 105          $keyword = yourls_sanitize_keyword( $keyword, true );
 106          $keyword = yourls_apply_filter( 'custom_keyword', $keyword, $url, $title );
 107  
 108          if ( !yourls_keyword_is_free( $keyword ) ) {
 109              // This shorturl either reserved or taken already
 110              $return['status']  = 'fail';
 111              $return['code']    = 'error:keyword';
 112              $return['message'] = yourls_s( 'Short URL %s already exists in database or is reserved', $keyword );
 113              $return['errorCode'] = $return['statusCode'] = '400'; // 400 Bad Request
 114  
 115              return yourls_apply_filter( 'add_new_link_keyword_exists', $return, $url, $keyword, $title );
 116          }
 117  
 118          // Create random keyword
 119      } else {
 120          yourls_do_action( 'add_new_link_create_keyword', $url, $keyword, $title );
 121  
 122          $id = yourls_get_next_decimal();
 123  
 124          do {
 125              $keyword = yourls_int2string( $id );
 126              $keyword = yourls_apply_filter( 'random_keyword', $keyword, $url, $title );
 127              $id++;
 128          } while ( !yourls_keyword_is_free($keyword) );
 129  
 130          yourls_update_next_decimal($id);
 131      }
 132  
 133      // We should be all set now. Store the short URL !
 134  
 135      $timestamp = date( 'Y-m-d H:i:s' );
 136  
 137      try {
 138          if (yourls_insert_link_in_db( $url, $keyword, $title )){
 139              // everything ok, populate needed vars
 140              $return['url']      = array('keyword' => $keyword, 'url' => $url, 'title' => $title, 'date' => $timestamp, 'ip' => $ip );
 141              $return['status']   = 'success';
 142              $return['message']  = /* //translators: eg "http://someurl/ added to DB" */ yourls_s( '%s added to database', yourls_trim_long_string( $url ) );
 143              $return['title']    = $title;
 144              $return['html']     = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time(), $row_id );
 145              $return['shorturl'] = yourls_link($keyword);
 146              $return['statusCode'] = '200'; // 200 OK
 147          } else {
 148              // unknown database error, couldn't store result
 149              $return['status']   = 'fail';
 150              $return['code']     = 'error:db';
 151              $return['message']  = yourls_s( 'Error saving url to database' );
 152              $return['errorCode'] = $return['statusCode'] = '500'; // 500 Internal Server Error
 153          }
 154      } catch (Exception $e) {
 155          // Keyword supposed to be free but the INSERT caused an exception: most likely we're facing a
 156          // concurrency problem. See Issue 2538.
 157          $return['status']  = 'fail';
 158          $return['code']    = 'error:concurrency';
 159          $return['message'] = $e->getMessage();
 160          $return['errorCode'] = $return['statusCode'] = '503'; // 503 Service Unavailable
 161      }
 162  
 163      yourls_do_action( 'post_add_new_link', $url, $keyword, $title, $return );
 164  
 165      return yourls_apply_filter( 'add_new_link', $return, $url, $keyword, $title );
 166  }
 167  /**
 168   * Determine the allowed character set in short URLs
 169   *
 170   * @return string    Acceptable charset for short URLS keywords
 171   */
 172  function yourls_get_shorturl_charset() {
 173      if ( defined( 'YOURLS_URL_CONVERT' ) && in_array( YOURLS_URL_CONVERT, [ 62, 64 ] ) ) {
 174          $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 175      }
 176      else {
 177          // defined to 36, or wrongly defined
 178          $charset = '0123456789abcdefghijklmnopqrstuvwxyz';
 179      }
 180  
 181      return yourls_apply_filter( 'get_shorturl_charset', $charset );
 182  }
 183  
 184  /**
 185   * Is a URL a short URL? Accept either 'http://sho.rt/abc' or 'abc'
 186   *
 187   * @param  string $shorturl   short URL
 188   * @return bool               true if registered short URL, false otherwise
 189   */
 190  function yourls_is_shorturl( $shorturl ) {
 191      // TODO: make sure this function evolves with the feature set.
 192  
 193      $is_short = false;
 194  
 195      // Is $shorturl a URL (http://sho.rt/abc) or a keyword (abc) ?
 196      if( yourls_get_protocol( $shorturl ) ) {
 197          $keyword = yourls_get_relative_url( $shorturl );
 198      } else {
 199          $keyword = $shorturl;
 200      }
 201  
 202      // Check if it's a valid && used keyword
 203      if( $keyword && $keyword == yourls_sanitize_keyword( $keyword ) && yourls_keyword_is_taken( $keyword ) ) {
 204          $is_short = true;
 205      }
 206  
 207      return yourls_apply_filter( 'is_shorturl', $is_short, $shorturl );
 208  }
 209  
 210  /**
 211   * Check to see if a given keyword is reserved (ie reserved URL or an existing page). Returns bool
 212   *
 213   * @param  string $keyword   Short URL keyword
 214   * @return bool              True if keyword reserved, false if free to be used
 215   */
 216  function yourls_keyword_is_reserved( $keyword ) {
 217      global $yourls_reserved_URL;
 218      $keyword = yourls_sanitize_keyword( $keyword );
 219      $reserved = false;
 220  
 221      if ( in_array( $keyword, $yourls_reserved_URL)
 222          or yourls_is_page($keyword)
 223          or is_dir( YOURLS_ABSPATH ."/$keyword" )
 224      )
 225          $reserved = true;
 226  
 227      return yourls_apply_filter( 'keyword_is_reserved', $reserved, $keyword );
 228  }
 229  
 230  /**
 231   * Delete a link in the DB
 232   *
 233   * @param  string $keyword   Short URL keyword
 234   * @return int               Number of links deleted
 235   */
 236  function yourls_delete_link_by_keyword( $keyword ) {
 237      // Allow plugins to short-circuit the whole function
 238      $pre = yourls_apply_filter( 'shunt_delete_link_by_keyword', null, $keyword );
 239      if ( null !== $pre ) {
 240          return $pre;
 241      }
 242  
 243      $table = YOURLS_DB_TABLE_URL;
 244      $keyword = yourls_sanitize_keyword($keyword);
 245      $delete = yourls_get_db()->fetchAffected("DELETE FROM `$table` WHERE `keyword` = :keyword", array('keyword' => $keyword));
 246      yourls_do_action( 'delete_link', $keyword, $delete );
 247      return $delete;
 248  }
 249  
 250  /**
 251   * SQL query to insert a new link in the DB. Returns boolean for success or failure of the inserting
 252   *
 253   * @param string $url
 254   * @param string $keyword
 255   * @param string $title
 256   * @return bool true if insert succeeded, false if failed
 257   */
 258  function yourls_insert_link_in_db($url, $keyword, $title = '' ) {
 259      $url       = yourls_sanitize_url($url);
 260      $keyword   = yourls_sanitize_keyword($keyword);
 261      $title     = yourls_sanitize_title($title);
 262      $timestamp = date('Y-m-d H:i:s');
 263      $ip        = yourls_get_IP();
 264  
 265      $table = YOURLS_DB_TABLE_URL;
 266      $binds = array(
 267          'keyword'   => $keyword,
 268          'url'       => $url,
 269          'title'     => $title,
 270          'timestamp' => $timestamp,
 271          'ip'        => $ip,
 272      );
 273      $insert = yourls_get_db()->fetchAffected("INSERT INTO `$table` (`keyword`, `url`, `title`, `timestamp`, `ip`, `clicks`) VALUES(:keyword, :url, :title, :timestamp, :ip, 0);", $binds);
 274  
 275      yourls_do_action( 'insert_link', (bool)$insert, $url, $keyword, $title, $timestamp, $ip );
 276  
 277      return (bool)$insert;
 278  }
 279  
 280  /**
 281   * Check if a long URL already exists in the DB. Return NULL (doesn't exist) or an object with URL informations.
 282   *
 283   * This function supersedes function yourls_url_exists(), deprecated in 1.7.10, with a better naming.
 284   *
 285   * @since 1.7.10
 286   * @param  string $url  URL to check if already shortened
 287   * @return mixed        NULL if does not already exist in DB, or object with URL information as properties (eg keyword, url, title, ...)
 288   */
 289  function yourls_long_url_exists( $url ) {
 290      // Allow plugins to short-circuit the whole function
 291      $pre = yourls_apply_filter( 'shunt_url_exists', false, $url );
 292      if ( false !== $pre ) {
 293          return $pre;
 294      }
 295  
 296      $table = YOURLS_DB_TABLE_URL;
 297      $url   = yourls_sanitize_url($url);
 298      $url_exists = yourls_get_db()->fetchObject("SELECT * FROM `$table` WHERE `url` = :url", array('url'=>$url));
 299  
 300      if ($url_exists === false) {
 301          $url_exists = NULL;
 302      }
 303  
 304      return yourls_apply_filter( 'url_exists', $url_exists, $url );
 305  }
 306  
 307  /**
 308   * Edit a link
 309   *
 310   * @param string $url
 311   * @param string $keyword
 312   * @param string $newkeyword
 313   * @param string $title
 314   * @return array Result of the edit and link information if successful
 315   */
 316  function yourls_edit_link($url, $keyword, $newkeyword='', $title='' ) {
 317      // Allow plugins to short-circuit the whole function
 318      $pre = yourls_apply_filter( 'shunt_edit_link', null, $keyword, $url, $keyword, $newkeyword, $title );
 319      if ( null !== $pre )
 320          return $pre;
 321  
 322      $ydb = yourls_get_db();
 323  
 324      $table = YOURLS_DB_TABLE_URL;
 325      $url = yourls_sanitize_url($url);
 326      $keyword = yourls_sanitize_keyword($keyword);
 327      $title = yourls_sanitize_title($title);
 328      $newkeyword = yourls_sanitize_keyword($newkeyword, true);
 329  
 330      if(!$url OR !$newkeyword) {
 331          $return['status']  = 'fail';
 332          $return['message'] = yourls__( 'Long URL or Short URL cannot be blank' );
 333          return yourls_apply_filter( 'edit_link', $return, $url, $keyword, $newkeyword, $title );
 334      }
 335  
 336      $old_url = $ydb->fetchValue("SELECT `url` FROM `$table` WHERE `keyword` = :keyword", array('keyword' => $keyword));
 337  
 338      // Check if new URL is not here already
 339      if ( $old_url != $url && !yourls_allow_duplicate_longurls() ) {
 340          $new_url_already_there = intval($ydb->fetchValue("SELECT COUNT(keyword) FROM `$table` WHERE `url` = :url;", array('url' => $url)));
 341      } else {
 342          $new_url_already_there = false;
 343      }
 344  
 345      // Check if the new keyword is not here already
 346      if ( $newkeyword != $keyword ) {
 347          $keyword_is_ok = yourls_keyword_is_free( $newkeyword );
 348      } else {
 349          $keyword_is_ok = true;
 350      }
 351  
 352      yourls_do_action( 'pre_edit_link', $url, $keyword, $newkeyword, $new_url_already_there, $keyword_is_ok );
 353  
 354      // All clear, update
 355      if ( ( !$new_url_already_there || yourls_allow_duplicate_longurls() ) && $keyword_is_ok ) {
 356              $sql   = "UPDATE `$table` SET `url` = :url, `keyword` = :newkeyword, `title` = :title WHERE `keyword` = :keyword";
 357              $binds = array('url' => $url, 'newkeyword' => $newkeyword, 'title' => $title, 'keyword' => $keyword);
 358              $update_url = $ydb->fetchAffected($sql, $binds);
 359          if( $update_url ) {
 360              $return['url']     = array( 'keyword'       => $newkeyword,
 361                                          'shorturl'      => yourls_link($newkeyword),
 362                                          'url'           => yourls_esc_url($url),
 363                                          'display_url'   => yourls_esc_html(yourls_trim_long_string($url)),
 364                                          'title'         => yourls_esc_attr($title),
 365                                          'display_title' => yourls_esc_html(yourls_trim_long_string( $title ))
 366                                  );
 367              $return['status']  = 'success';
 368              $return['message'] = yourls__( 'Link updated in database' );
 369          } else {
 370              $return['status']  = 'fail';
 371              $return['message'] = /* //translators: "Error updating http://someurl/ (Shorturl: http://sho.rt/blah)" */ yourls_s( 'Error updating %s (Short URL: %s)', yourls_esc_html(yourls_trim_long_string($url)), $keyword ) ;
 372          }
 373  
 374      // Nope
 375      } else {
 376          $return['status']  = 'fail';
 377          $return['message'] = yourls__( 'URL or keyword already exists in database' );
 378      }
 379  
 380      return yourls_apply_filter( 'edit_link', $return, $url, $keyword, $newkeyword, $title, $new_url_already_there, $keyword_is_ok );
 381  }
 382  
 383  /**
 384   * Update a title link (no checks for duplicates etc..)
 385   *
 386   * @param string $keyword
 387   * @param string $title
 388   * @return int number of rows updated
 389   */
 390  function yourls_edit_link_title( $keyword, $title ) {
 391      // Allow plugins to short-circuit the whole function
 392      $pre = yourls_apply_filter( 'shunt_edit_link_title', null, $keyword, $title );
 393      if ( null !== $pre ) {
 394          return $pre;
 395      }
 396  
 397      $keyword = yourls_sanitize_keyword( $keyword );
 398      $title = yourls_sanitize_title( $title );
 399  
 400      $table = YOURLS_DB_TABLE_URL;
 401      $update = yourls_get_db()->fetchAffected("UPDATE `$table` SET `title` = :title WHERE `keyword` = :keyword;", array('title' => $title, 'keyword' => $keyword));
 402  
 403      return $update;
 404  }
 405  
 406  /**
 407   * Check if keyword id is free (ie not already taken, and not reserved). Return bool.
 408   *
 409   * @param  string $keyword    short URL keyword
 410   * @return bool               true if keyword is taken (ie there is a short URL for it), false otherwise
 411   */
 412  function yourls_keyword_is_free( $keyword  ) {
 413      $free = true;
 414      if ( yourls_keyword_is_reserved( $keyword ) or yourls_keyword_is_taken( $keyword, false ) ) {
 415          $free = false;
 416      }
 417  
 418      return yourls_apply_filter( 'keyword_is_free', $free, $keyword );
 419  }
 420  
 421  /**
 422   * Check if a keyword matches a "page"
 423   *
 424   * @see https://docs.yourls.org/guide/extend/pages.html
 425   * @since 1.7.10
 426   * @param  string $keyword  Short URL $keyword
 427   * @return bool             true if is page, false otherwise
 428   */
 429  function yourls_is_page($keyword) {
 430      return yourls_apply_filter( 'is_page', file_exists( YOURLS_PAGEDIR . "/$keyword.php" ) );
 431  }
 432  
 433  /**
 434   * Check if a keyword is taken (ie there is already a short URL with this id). Return bool.
 435   *
 436   */
 437  /**
 438   * Check if a keyword is taken (ie there is already a short URL with this id). Return bool.
 439   *
 440   * @param  string $keyword    short URL keyword
 441   * @param  bool   $use_cache  optional, default true: do we want to use what is cached in memory, if any, or force a new SQL query
 442   * @return bool               true if keyword is taken (ie there is a short URL for it), false otherwise
 443   */
 444  function yourls_keyword_is_taken( $keyword, $use_cache = true ) {
 445      // Allow plugins to short-circuit the whole function
 446      $pre = yourls_apply_filter( 'shunt_keyword_is_taken', false, $keyword );
 447      if ( false !== $pre ) {
 448          return $pre;
 449      }
 450  
 451      $taken = false;
 452      // To check if a keyword is already associated with a short URL, we fetch all info matching that keyword. This
 453      // will save a query in case of a redirection in yourls-go.php because info will be cached
 454      if ( yourls_get_keyword_infos($keyword, $use_cache) ) {
 455          $taken = true;
 456      }
 457  
 458      return yourls_apply_filter( 'keyword_is_taken', $taken, $keyword );
 459  }
 460  
 461  /**
 462   * Return array of all information associated with keyword. Returns false if keyword not found. Set optional $use_cache to false to force fetching from DB
 463   *
 464   * Sincere apologies to native English speakers, we are aware that the plural of 'info' is actually 'info', not 'infos'.
 465   * This function yourls_get_keyword_infos() returns all information, while function yourls_get_keyword_info() (no 's') return only
 466   * one information. Blame YOURLS contributors whose mother tongue is not English :)
 467   *
 468   * @since 1.4
 469   * @param  string $keyword    Short URL keyword
 470   * @param  bool   $use_cache  Default true, set to false to force fetching from DB
 471   * @return false|object       false if not found, object with URL properties if found
 472   */
 473  function yourls_get_keyword_infos( $keyword, $use_cache = true ) {
 474      $ydb = yourls_get_db();
 475      $keyword = yourls_sanitize_keyword( $keyword );
 476  
 477      yourls_do_action( 'pre_get_keyword', $keyword, $use_cache );
 478  
 479      if( $ydb->has_infos($keyword) && $use_cache === true ) {
 480          return yourls_apply_filter( 'get_keyword_infos', $ydb->get_infos($keyword), $keyword );
 481      }
 482  
 483      yourls_do_action( 'get_keyword_not_cached', $keyword );
 484  
 485      $table = YOURLS_DB_TABLE_URL;
 486      $infos = $ydb->fetchObject("SELECT * FROM `$table` WHERE `keyword` = :keyword", array('keyword' => $keyword));
 487  
 488      if( $infos ) {
 489          $infos = (array)$infos;
 490          $ydb->set_infos($keyword, $infos);
 491      } else {
 492          // is NULL if not found
 493          $infos = false;
 494          $ydb->set_infos($keyword, false);
 495      }
 496  
 497      return yourls_apply_filter( 'get_keyword_infos', $infos, $keyword );
 498  }
 499  
 500  /**
 501   * Return information associated with a keyword (eg clicks, URL, title...). Optional $notfound = string default message if nothing found
 502   *
 503   * @param string $keyword          Short URL keyword
 504   * @param string $field            Field to return (eg 'url', 'title', 'ip', 'clicks', 'timestamp', 'keyword')
 505   * @param false|string $notfound   Optional string to return if keyword not found
 506   * @return mixed|string
 507   */
 508  function yourls_get_keyword_info($keyword, $field, $notfound = false ) {
 509  
 510      // Allow plugins to short-circuit the whole function
 511      $pre = yourls_apply_filter( 'shunt_get_keyword_info', false, $keyword, $field, $notfound );
 512      if ( false !== $pre )
 513          return $pre;
 514  
 515      $keyword = yourls_sanitize_keyword( $keyword );
 516      $infos = yourls_get_keyword_infos( $keyword );
 517  
 518      $return = $notfound;
 519      if ( isset( $infos[ $field ] ) && $infos[ $field ] !== false )
 520          $return = $infos[ $field ];
 521  
 522      return yourls_apply_filter( 'get_keyword_info', $return, $keyword, $field, $notfound );
 523  }
 524  
 525  /**
 526   * Return title associated with keyword. Optional $notfound = string default message if nothing found
 527   *
 528   * @param string $keyword          Short URL keyword
 529   * @param false|string $notfound   Optional string to return if keyword not found
 530   * @return mixed|string
 531   */
 532  function yourls_get_keyword_title( $keyword, $notfound = false ) {
 533      return yourls_get_keyword_info( $keyword, 'title', $notfound );
 534  }
 535  
 536  /**
 537   * Return long URL associated with keyword. Optional $notfound = string default message if nothing found
 538   *
 539   * @param string $keyword          Short URL keyword
 540   * @param false|string $notfound   Optional string to return if keyword not found
 541   * @return mixed|string
 542   */
 543  function yourls_get_keyword_longurl( $keyword, $notfound = false ) {
 544      return yourls_get_keyword_info( $keyword, 'url', $notfound );
 545  }
 546  
 547  /**
 548   * Return number of clicks on a keyword. Optional $notfound = string default message if nothing found
 549   *
 550   * @param string $keyword          Short URL keyword
 551   * @param false|string $notfound   Optional string to return if keyword not found
 552   * @return mixed|string
 553   */
 554  function yourls_get_keyword_clicks( $keyword, $notfound = false ) {
 555      return yourls_get_keyword_info( $keyword, 'clicks', $notfound );
 556  }
 557  
 558  /**
 559   * Return IP that added a keyword. Optional $notfound = string default message if nothing found
 560   *
 561   * @param string $keyword          Short URL keyword
 562   * @param false|string $notfound   Optional string to return if keyword not found
 563   * @return mixed|string
 564   */
 565  function yourls_get_keyword_IP( $keyword, $notfound = false ) {
 566      return yourls_get_keyword_info( $keyword, 'ip', $notfound );
 567  }
 568  
 569  /**
 570   * Return timestamp associated with a keyword. Optional $notfound = string default message if nothing found
 571   *
 572   * @param string $keyword          Short URL keyword
 573   * @param false|string $notfound   Optional string to return if keyword not found
 574   * @return mixed|string
 575   */
 576  function yourls_get_keyword_timestamp( $keyword, $notfound = false ) {
 577      return yourls_get_keyword_info( $keyword, 'timestamp', $notfound );
 578  }
 579  
 580  /**
 581   * Return array of stats for a given keyword
 582   *
 583   * This function supersedes function yourls_get_link_stats(), deprecated in 1.7.10, with a better naming.
 584   *
 585   * @since 1.7.10
 586   * @param  string $shorturl short URL keyword
 587   * @return array            stats
 588   */
 589  function yourls_get_keyword_stats( $shorturl ) {
 590      $table_url = YOURLS_DB_TABLE_URL;
 591      $shorturl  = yourls_sanitize_keyword( $shorturl );
 592  
 593      $res = yourls_get_db()->fetchObject("SELECT * FROM `$table_url` WHERE `keyword` = :keyword", array('keyword' => $shorturl));
 594  
 595      if( !$res ) {
 596          // non existent link
 597          $return = array(
 598              'statusCode' => '404',
 599              'message'    => 'Error: short URL not found',
 600          );
 601      } else {
 602          $return = array(
 603              'statusCode' => '200',
 604              'message'    => 'success',
 605              'link'       => array(
 606                  'shorturl' => yourls_link($res->keyword),
 607                  'url'      => $res->url,
 608                  'title'    => $res->title,
 609                  'timestamp'=> $res->timestamp,
 610                  'ip'       => $res->ip,
 611                  'clicks'   => $res->clicks,
 612              )
 613          );
 614      }
 615  
 616      return yourls_apply_filter( 'get_link_stats', $return, $shorturl );
 617  }
 618  
 619  /**
 620   * Return array of keywords that redirect to the submitted long URL
 621   *
 622   * @since 1.7
 623   * @param string $longurl long url
 624   * @param string $order Optional SORT order (can be 'ASC' or 'DESC')
 625   * @return array array of keywords
 626   */
 627  function yourls_get_longurl_keywords( $longurl, $order = 'ASC' ) {
 628      $longurl = yourls_sanitize_url($longurl);
 629      $table   = YOURLS_DB_TABLE_URL;
 630      $sql     = "SELECT `keyword` FROM `$table` WHERE `url` = :url";
 631  
 632      if (in_array($order, array('ASC','DESC'))) {
 633          $sql .= " ORDER BY `keyword` ".$order;
 634      }
 635  
 636      return yourls_apply_filter( 'get_longurl_keywords', yourls_get_db()->fetchCol($sql, array('url'=>$longurl)), $longurl );
 637  }


Generated: Sat Feb 22 05:10:06 2025 Cross-referenced by PHPXref 0.7.1