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


Generated: Tue Sep 27 05:10:01 2022 Cross-referenced by PHPXref 0.7.1