[ 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   * Get the list of reserved keywords for URLs.
 212   *
 213   * @return array             Array of reserved keywords
 214   */
 215  function yourls_get_reserved_URL() {
 216      global $yourls_reserved_URL;
 217      if ( ! isset( $yourls_reserved_URL ) || ! is_array( $yourls_reserved_URL ) ) {
 218          return array();
 219      }
 220  
 221      return $yourls_reserved_URL;
 222  }
 223  
 224  /**
 225   * Check to see if a given keyword is reserved (ie reserved URL or an existing page). Returns bool
 226   *
 227   * @param  string $keyword   Short URL keyword
 228   * @return bool              True if keyword reserved, false if free to be used
 229   */
 230  function yourls_keyword_is_reserved( $keyword ) {
 231      $keyword = yourls_sanitize_keyword( $keyword );
 232      $reserved = false;
 233  
 234      if ( in_array( $keyword, yourls_get_reserved_URL() )
 235          or yourls_is_page($keyword)
 236          or is_dir( YOURLS_ABSPATH ."/$keyword" )
 237      )
 238          $reserved = true;
 239  
 240      return yourls_apply_filter( 'keyword_is_reserved', $reserved, $keyword );
 241  }
 242  
 243  /**
 244   * Delete a link in the DB
 245   *
 246   * @param  string $keyword   Short URL keyword
 247   * @return int               Number of links deleted
 248   */
 249  function yourls_delete_link_by_keyword( $keyword ) {
 250      // Allow plugins to short-circuit the whole function
 251      $pre = yourls_apply_filter( 'shunt_delete_link_by_keyword', null, $keyword );
 252      if ( null !== $pre ) {
 253          return $pre;
 254      }
 255  
 256      $table = YOURLS_DB_TABLE_URL;
 257      $keyword = yourls_sanitize_keyword($keyword);
 258      $delete = yourls_get_db()->fetchAffected("DELETE FROM `$table` WHERE `keyword` = :keyword", array('keyword' => $keyword));
 259      yourls_do_action( 'delete_link', $keyword, $delete );
 260      return $delete;
 261  }
 262  
 263  /**
 264   * SQL query to insert a new link in the DB. Returns boolean for success or failure of the inserting
 265   *
 266   * @param string $url
 267   * @param string $keyword
 268   * @param string $title
 269   * @return bool true if insert succeeded, false if failed
 270   */
 271  function yourls_insert_link_in_db($url, $keyword, $title = '' ) {
 272      $url       = yourls_sanitize_url($url);
 273      $keyword   = yourls_sanitize_keyword($keyword);
 274      $title     = yourls_sanitize_title($title);
 275      $timestamp = date('Y-m-d H:i:s');
 276      $ip        = yourls_get_IP();
 277  
 278      $table = YOURLS_DB_TABLE_URL;
 279      $binds = array(
 280          'keyword'   => $keyword,
 281          'url'       => $url,
 282          'title'     => $title,
 283          'timestamp' => $timestamp,
 284          'ip'        => $ip,
 285      );
 286      $insert = yourls_get_db()->fetchAffected("INSERT INTO `$table` (`keyword`, `url`, `title`, `timestamp`, `ip`, `clicks`) VALUES(:keyword, :url, :title, :timestamp, :ip, 0);", $binds);
 287  
 288      yourls_do_action( 'insert_link', (bool)$insert, $url, $keyword, $title, $timestamp, $ip );
 289  
 290      return (bool)$insert;
 291  }
 292  
 293  /**
 294   * Check if a long URL already exists in the DB. Return NULL (doesn't exist) or an object with URL informations.
 295   *
 296   * This function supersedes function yourls_url_exists(), deprecated in 1.7.10, with a better naming.
 297   *
 298   * @since 1.7.10
 299   * @param  string $url  URL to check if already shortened
 300   * @return mixed        NULL if does not already exist in DB, or object with URL information as properties (eg keyword, url, title, ...)
 301   */
 302  function yourls_long_url_exists( $url ) {
 303      // Allow plugins to short-circuit the whole function
 304      $pre = yourls_apply_filter( 'shunt_url_exists', false, $url );
 305      if ( false !== $pre ) {
 306          return $pre;
 307      }
 308  
 309      $table = YOURLS_DB_TABLE_URL;
 310      $url   = yourls_sanitize_url($url);
 311      $url_exists = yourls_get_db()->fetchObject("SELECT * FROM `$table` WHERE `url` = :url", array('url'=>$url));
 312  
 313      if ($url_exists === false) {
 314          $url_exists = NULL;
 315      }
 316  
 317      return yourls_apply_filter( 'url_exists', $url_exists, $url );
 318  }
 319  
 320  /**
 321   * Edit a link
 322   *
 323   * @param string $url
 324   * @param string $keyword
 325   * @param string $newkeyword
 326   * @param string $title
 327   * @return array Result of the edit and link information if successful
 328   */
 329  function yourls_edit_link($url, $keyword, $newkeyword='', $title='' ) {
 330      // Allow plugins to short-circuit the whole function
 331      $pre = yourls_apply_filter( 'shunt_edit_link', null, $keyword, $url, $keyword, $newkeyword, $title );
 332      if ( null !== $pre )
 333          return $pre;
 334  
 335      $ydb = yourls_get_db();
 336  
 337      $table = YOURLS_DB_TABLE_URL;
 338      $url = yourls_sanitize_url($url);
 339      $keyword = yourls_sanitize_keyword($keyword);
 340      $title = yourls_sanitize_title($title);
 341      $newkeyword = yourls_sanitize_keyword($newkeyword, true);
 342  
 343      if(!$url OR !$newkeyword) {
 344          $return['status']  = 'fail';
 345          $return['message'] = yourls__( 'Long URL or Short URL cannot be blank' );
 346          return yourls_apply_filter( 'edit_link', $return, $url, $keyword, $newkeyword, $title );
 347      }
 348  
 349      $old_url = $ydb->fetchValue("SELECT `url` FROM `$table` WHERE `keyword` = :keyword", array('keyword' => $keyword));
 350  
 351      // Check if new URL is not here already
 352      if ( $old_url != $url && !yourls_allow_duplicate_longurls() ) {
 353          $new_url_already_there = intval($ydb->fetchValue("SELECT COUNT(keyword) FROM `$table` WHERE `url` = :url;", array('url' => $url)));
 354      } else {
 355          $new_url_already_there = false;
 356      }
 357  
 358      // Check if the new keyword is not here already
 359      if ( $newkeyword != $keyword ) {
 360          $keyword_is_ok = yourls_keyword_is_free( $newkeyword );
 361      } else {
 362          $keyword_is_ok = true;
 363      }
 364  
 365      yourls_do_action( 'pre_edit_link', $url, $keyword, $newkeyword, $new_url_already_there, $keyword_is_ok );
 366  
 367      // All clear, update
 368      if ( ( !$new_url_already_there || yourls_allow_duplicate_longurls() ) && $keyword_is_ok ) {
 369              $sql   = "UPDATE `$table` SET `url` = :url, `keyword` = :newkeyword, `title` = :title WHERE `keyword` = :keyword";
 370              $binds = array('url' => $url, 'newkeyword' => $newkeyword, 'title' => $title, 'keyword' => $keyword);
 371              $update_url = $ydb->fetchAffected($sql, $binds);
 372          if( $update_url ) {
 373              $return['url']     = array( 'keyword'       => $newkeyword,
 374                                          'shorturl'      => yourls_link($newkeyword),
 375                                          'url'           => yourls_esc_url($url),
 376                                          'display_url'   => yourls_esc_html(yourls_trim_long_string($url)),
 377                                          'title'         => yourls_esc_attr($title),
 378                                          'display_title' => yourls_esc_html(yourls_trim_long_string( $title ))
 379                                  );
 380              $return['status']  = 'success';
 381              $return['message'] = yourls__( 'Link updated in database' );
 382          } else {
 383              $return['status']  = 'fail';
 384              $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 ) ;
 385          }
 386  
 387      // Nope
 388      } else {
 389          $return['status']  = 'fail';
 390          $return['message'] = yourls__( 'URL or keyword already exists in database' );
 391      }
 392  
 393      return yourls_apply_filter( 'edit_link', $return, $url, $keyword, $newkeyword, $title, $new_url_already_there, $keyword_is_ok );
 394  }
 395  
 396  /**
 397   * Update a title link (no checks for duplicates etc..)
 398   *
 399   * @param string $keyword
 400   * @param string $title
 401   * @return int number of rows updated
 402   */
 403  function yourls_edit_link_title( $keyword, $title ) {
 404      // Allow plugins to short-circuit the whole function
 405      $pre = yourls_apply_filter( 'shunt_edit_link_title', null, $keyword, $title );
 406      if ( null !== $pre ) {
 407          return $pre;
 408      }
 409  
 410      $keyword = yourls_sanitize_keyword( $keyword );
 411      $title = yourls_sanitize_title( $title );
 412  
 413      $table = YOURLS_DB_TABLE_URL;
 414      $update = yourls_get_db()->fetchAffected("UPDATE `$table` SET `title` = :title WHERE `keyword` = :keyword;", array('title' => $title, 'keyword' => $keyword));
 415  
 416      return $update;
 417  }
 418  
 419  /**
 420   * Check if keyword id is free (ie not already taken, and not reserved). Return bool.
 421   *
 422   * @param  string $keyword    short URL keyword
 423   * @return bool               true if keyword is taken (ie there is a short URL for it), false otherwise
 424   */
 425  function yourls_keyword_is_free( $keyword  ) {
 426      $free = true;
 427      if ( yourls_keyword_is_reserved( $keyword ) or yourls_keyword_is_taken( $keyword, false ) ) {
 428          $free = false;
 429      }
 430  
 431      return yourls_apply_filter( 'keyword_is_free', $free, $keyword );
 432  }
 433  
 434  /**
 435   * Check if a keyword matches a "page"
 436   *
 437   * @see https://docs.yourls.org/guide/extend/pages.html
 438   * @since 1.7.10
 439   * @param  string $keyword  Short URL $keyword
 440   * @return bool             true if is page, false otherwise
 441   */
 442  function yourls_is_page($keyword) {
 443      return yourls_apply_filter( 'is_page', file_exists( YOURLS_PAGEDIR . "/$keyword.php" ) );
 444  }
 445  
 446  /**
 447   * Check if a keyword is taken (ie there is already a short URL with this id). Return bool.
 448   *
 449   */
 450  /**
 451   * Check if a keyword is taken (ie there is already a short URL with this id). Return bool.
 452   *
 453   * @param  string $keyword    short URL keyword
 454   * @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
 455   * @return bool               true if keyword is taken (ie there is a short URL for it), false otherwise
 456   */
 457  function yourls_keyword_is_taken( $keyword, $use_cache = true ) {
 458      // Allow plugins to short-circuit the whole function
 459      $pre = yourls_apply_filter( 'shunt_keyword_is_taken', false, $keyword );
 460      if ( false !== $pre ) {
 461          return $pre;
 462      }
 463  
 464      $taken = false;
 465      // To check if a keyword is already associated with a short URL, we fetch all info matching that keyword. This
 466      // will save a query in case of a redirection in yourls-go.php because info will be cached
 467      if ( yourls_get_keyword_infos($keyword, $use_cache) ) {
 468          $taken = true;
 469      }
 470  
 471      return yourls_apply_filter( 'keyword_is_taken', $taken, $keyword );
 472  }
 473  
 474  /**
 475   * 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
 476   *
 477   * Sincere apologies to native English speakers, we are aware that the plural of 'info' is actually 'info', not 'infos'.
 478   * This function yourls_get_keyword_infos() returns all information, while function yourls_get_keyword_info() (no 's') return only
 479   * one information. Blame YOURLS contributors whose mother tongue is not English :)
 480   *
 481   * @since 1.4
 482   * @param  string $keyword    Short URL keyword
 483   * @param  bool   $use_cache  Default true, set to false to force fetching from DB
 484   * @return false|object       false if not found, object with URL properties if found
 485   */
 486  function yourls_get_keyword_infos( $keyword, $use_cache = true ) {
 487      $ydb = yourls_get_db();
 488      $keyword = yourls_sanitize_keyword( $keyword );
 489  
 490      yourls_do_action( 'pre_get_keyword', $keyword, $use_cache );
 491  
 492      if( $ydb->has_infos($keyword) && $use_cache === true ) {
 493          return yourls_apply_filter( 'get_keyword_infos', $ydb->get_infos($keyword), $keyword );
 494      }
 495  
 496      yourls_do_action( 'get_keyword_not_cached', $keyword );
 497  
 498      $table = YOURLS_DB_TABLE_URL;
 499      $infos = $ydb->fetchObject("SELECT * FROM `$table` WHERE `keyword` = :keyword", array('keyword' => $keyword));
 500  
 501      if( $infos ) {
 502          $infos = (array)$infos;
 503          $ydb->set_infos($keyword, $infos);
 504      } else {
 505          // is NULL if not found
 506          $infos = false;
 507          $ydb->set_infos($keyword, false);
 508      }
 509  
 510      return yourls_apply_filter( 'get_keyword_infos', $infos, $keyword );
 511  }
 512  
 513  /**
 514   * Return information associated with a keyword (eg clicks, URL, title...). Optional $notfound = string default message if nothing found
 515   *
 516   * @param string $keyword          Short URL keyword
 517   * @param string $field            Field to return (eg 'url', 'title', 'ip', 'clicks', 'timestamp', 'keyword')
 518   * @param false|string $notfound   Optional string to return if keyword not found
 519   * @return mixed|string
 520   */
 521  function yourls_get_keyword_info($keyword, $field, $notfound = false ) {
 522  
 523      // Allow plugins to short-circuit the whole function
 524      $pre = yourls_apply_filter( 'shunt_get_keyword_info', false, $keyword, $field, $notfound );
 525      if ( false !== $pre )
 526          return $pre;
 527  
 528      $keyword = yourls_sanitize_keyword( $keyword );
 529      $infos = yourls_get_keyword_infos( $keyword );
 530  
 531      $return = $notfound;
 532      if ( isset( $infos[ $field ] ) && $infos[ $field ] !== false )
 533          $return = $infos[ $field ];
 534  
 535      return yourls_apply_filter( 'get_keyword_info', $return, $keyword, $field, $notfound );
 536  }
 537  
 538  /**
 539   * Return title associated with keyword. Optional $notfound = string default message if nothing found
 540   *
 541   * @param string $keyword          Short URL keyword
 542   * @param false|string $notfound   Optional string to return if keyword not found
 543   * @return mixed|string
 544   */
 545  function yourls_get_keyword_title( $keyword, $notfound = false ) {
 546      return yourls_get_keyword_info( $keyword, 'title', $notfound );
 547  }
 548  
 549  /**
 550   * Return long URL associated with keyword. Optional $notfound = string default message if nothing found
 551   *
 552   * @param string $keyword          Short URL keyword
 553   * @param false|string $notfound   Optional string to return if keyword not found
 554   * @return mixed|string
 555   */
 556  function yourls_get_keyword_longurl( $keyword, $notfound = false ) {
 557      return yourls_get_keyword_info( $keyword, 'url', $notfound );
 558  }
 559  
 560  /**
 561   * Return number of clicks on a keyword. Optional $notfound = string default message if nothing found
 562   *
 563   * @param string $keyword          Short URL keyword
 564   * @param false|string $notfound   Optional string to return if keyword not found
 565   * @return mixed|string
 566   */
 567  function yourls_get_keyword_clicks( $keyword, $notfound = false ) {
 568      return yourls_get_keyword_info( $keyword, 'clicks', $notfound );
 569  }
 570  
 571  /**
 572   * Return IP that added a keyword. Optional $notfound = string default message if nothing found
 573   *
 574   * @param string $keyword          Short URL keyword
 575   * @param false|string $notfound   Optional string to return if keyword not found
 576   * @return mixed|string
 577   */
 578  function yourls_get_keyword_IP( $keyword, $notfound = false ) {
 579      return yourls_get_keyword_info( $keyword, 'ip', $notfound );
 580  }
 581  
 582  /**
 583   * Return timestamp associated with a keyword. Optional $notfound = string default message if nothing found
 584   *
 585   * @param string $keyword          Short URL keyword
 586   * @param false|string $notfound   Optional string to return if keyword not found
 587   * @return mixed|string
 588   */
 589  function yourls_get_keyword_timestamp( $keyword, $notfound = false ) {
 590      return yourls_get_keyword_info( $keyword, 'timestamp', $notfound );
 591  }
 592  
 593  /**
 594   * Return array of stats for a given keyword
 595   *
 596   * This function supersedes function yourls_get_link_stats(), deprecated in 1.7.10, with a better naming.
 597   *
 598   * @since 1.7.10
 599   * @param  string $shorturl short URL keyword
 600   * @return array            stats
 601   */
 602  function yourls_get_keyword_stats( $shorturl ) {
 603      $table_url = YOURLS_DB_TABLE_URL;
 604      $shorturl  = yourls_sanitize_keyword( $shorturl );
 605  
 606      $res = yourls_get_db()->fetchObject("SELECT * FROM `$table_url` WHERE `keyword` = :keyword", array('keyword' => $shorturl));
 607  
 608      if( !$res ) {
 609          // non existent link
 610          $return = array(
 611              'statusCode' => '404',
 612              'message'    => 'Error: short URL not found',
 613          );
 614      } else {
 615          $return = array(
 616              'statusCode' => '200',
 617              'message'    => 'success',
 618              'link'       => array(
 619                  'shorturl' => yourls_link($res->keyword),
 620                  'url'      => $res->url,
 621                  'title'    => $res->title,
 622                  'timestamp'=> $res->timestamp,
 623                  'ip'       => $res->ip,
 624                  'clicks'   => $res->clicks,
 625              )
 626          );
 627      }
 628  
 629      return yourls_apply_filter( 'get_link_stats', $return, $shorturl );
 630  }
 631  
 632  /**
 633   * Return array of keywords that redirect to the submitted long URL
 634   *
 635   * @since 1.7
 636   * @param string $longurl long url
 637   * @param string $order Optional SORT order (can be 'ASC' or 'DESC')
 638   * @return array array of keywords
 639   */
 640  function yourls_get_longurl_keywords( $longurl, $order = 'ASC' ) {
 641      $longurl = yourls_sanitize_url($longurl);
 642      $table   = YOURLS_DB_TABLE_URL;
 643      $sql     = "SELECT `keyword` FROM `$table` WHERE `url` = :url";
 644  
 645      if (in_array($order, array('ASC','DESC'))) {
 646          $sql .= " ORDER BY `keyword` ".$order;
 647      }
 648  
 649      return yourls_apply_filter( 'get_longurl_keywords', yourls_get_db()->fetchCol($sql, array('url'=>$longurl)), $longurl );
 650  }


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