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


Generated: Wed Feb 25 05:10:41 2026 Cross-referenced by PHPXref 0.7.1