[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

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

   1  <?php
   2  /*
   3   * Functions relative to how YOURLS handle some links
   4   *
   5   */
   6  
   7  /**
   8   * Add a query var to a URL and return URL. Completely stolen from WP.
   9   *
  10   * Works with one of these parameter patterns:
  11   *     array( 'var' => 'value' )
  12   *     array( 'var' => 'value' ), $url
  13   *     'var', 'value'
  14   *     'var', 'value', $url
  15   * If $url omitted, uses $_SERVER['REQUEST_URI']
  16   *
  17   * The result of this function call is a URL : it should be escaped before being printed as HTML
  18   *
  19   * @since 1.5
  20   * @param string|array $param1 Either newkey or an associative_array.
  21   * @param string       $param2 Either newvalue or oldquery or URI.
  22   * @param string       $param3 Optional. Old query or URI.
  23   * @return string New URL query string.
  24   */
  25  function yourls_add_query_arg() {
  26      $ret = '';
  27      if ( is_array( func_get_arg(0) ) ) {
  28          if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) )
  29              $uri = $_SERVER['REQUEST_URI'];
  30          else
  31              $uri = @func_get_arg( 1 );
  32      } else {
  33          if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) )
  34              $uri = $_SERVER['REQUEST_URI'];
  35          else
  36              $uri = @func_get_arg( 2 );
  37      }
  38  
  39      $uri = str_replace( '&amp;', '&', $uri );
  40  
  41  
  42      if ( $frag = strstr( $uri, '#' ) )
  43          $uri = substr( $uri, 0, -strlen( $frag ) );
  44      else
  45          $frag = '';
  46  
  47      if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
  48          $protocol = $matches[0];
  49          $uri = substr( $uri, strlen( $protocol ) );
  50      } else {
  51          $protocol = '';
  52      }
  53  
  54      if ( strpos( $uri, '?' ) !== false ) {
  55          $parts = explode( '?', $uri, 2 );
  56          if ( 1 == count( $parts ) ) {
  57              $base = '?';
  58              $query = $parts[0];
  59          } else {
  60              $base = $parts[0] . '?';
  61              $query = $parts[1];
  62          }
  63      } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) {
  64          $base = $uri . '?';
  65          $query = '';
  66      } else {
  67          $base = '';
  68          $query = $uri;
  69      }
  70  
  71      parse_str( $query, $qs );
  72      $qs = yourls_urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
  73      if ( is_array( func_get_arg( 0 ) ) ) {
  74          $kayvees = func_get_arg( 0 );
  75          $qs = array_merge( $qs, $kayvees );
  76      } else {
  77          $qs[func_get_arg( 0 )] = func_get_arg( 1 );
  78      }
  79  
  80      foreach ( (array) $qs as $k => $v ) {
  81          if ( $v === false )
  82              unset( $qs[$k] );
  83      }
  84  
  85      $ret = http_build_query( $qs );
  86      $ret = trim( $ret, '?' );
  87      $ret = preg_replace( '#=(&|$)#', '$1', $ret );
  88      $ret = $protocol . $base . $ret . $frag;
  89      $ret = rtrim( $ret, '?' );
  90      return $ret;
  91  }
  92  
  93  /**
  94   * Navigates through an array and encodes the values to be used in a URL. Stolen from WP, used in yourls_add_query_arg()
  95   *
  96   * @param array|string $value The array or string to be encoded.
  97   * @return array|string
  98   */
  99  function yourls_urlencode_deep( $value ) {
 100      $value = is_array( $value ) ? array_map( 'yourls_urlencode_deep', $value ) : urlencode( $value );
 101      return $value;
 102  }
 103  
 104  /**
 105   * Remove arg from query. Opposite of yourls_add_query_arg. Stolen from WP.
 106   *
 107   * The result of this function call is a URL : it should be escaped before being printed as HTML
 108   *
 109   * @since 1.5
 110   * @param string|array $key   Query key or keys to remove.
 111   * @param bool|string  $query Optional. When false uses the $_SERVER value. Default false.
 112   * @return string New URL query string.
 113   */
 114  function yourls_remove_query_arg( $key, $query = false ) {
 115      if ( is_array( $key ) ) { // removing multiple keys
 116          foreach ( $key as $k )
 117              $query = yourls_add_query_arg( $k, false, $query );
 118          return $query;
 119      }
 120      return yourls_add_query_arg( $key, false, $query );
 121  }
 122  
 123  /**
 124   * Converts keyword into short link (prepend with YOURLS base URL) or stat link (sho.rt/abc+)
 125   *
 126   * This function does not check for a valid keyword.
 127   * The resulting link is normalized to allow for IDN translation to UTF8
 128   *
 129   * @param  string $keyword  Short URL keyword
 130   * @param  bool   $stats    Optional, true to return a stat link (eg sho.rt/abc+)
 131   * @return string           Short URL, or keyword stat URL
 132   */
 133  function yourls_link( $keyword = '', $stats = false ) {
 134      $keyword = yourls_sanitize_keyword($keyword);
 135      if( $stats  === true ) {
 136          $keyword = $keyword . '+';
 137      }
 138      $link    = yourls_normalize_uri( yourls_get_yourls_site() . '/' . $keyword );
 139  
 140      if( yourls_is_ssl() ) {
 141          $link = yourls_set_url_scheme( $link, 'https' );
 142      }
 143  
 144      return yourls_apply_filter( 'yourls_link', $link, $keyword );
 145  }
 146  
 147  /**
 148   * Converts keyword into stat link (prepend with YOURLS base URL, append +)
 149   *
 150   * This function does not make sure the keyword matches an actual short URL
 151   *
 152   * @param  string $keyword  Short URL keyword
 153   * @return string           Short URL stat link
 154   */
 155  function yourls_statlink( $keyword = '' ) {
 156      $link = yourls_link( $keyword, true );
 157      return yourls_apply_filter( 'yourls_statlink', $link, $keyword );
 158  }
 159  
 160  /**
 161   * Return admin link, with SSL preference if applicable.
 162   *
 163   * @param string $page  Page name, eg "index.php"
 164   * @return string
 165   */
 166  function yourls_admin_url( $page = '' ) {
 167      $admin = yourls_get_yourls_site() . '/admin/' . $page;
 168      if( yourls_is_ssl() or yourls_needs_ssl() ) {
 169          $admin = yourls_set_url_scheme( $admin, 'https' );
 170      }
 171      return yourls_apply_filter( 'admin_url', $admin, $page );
 172  }
 173  
 174  /**
 175   * Return YOURLS_SITE or URL under YOURLS setup, with SSL preference
 176   *
 177   * @param bool $echo   Echo if true, or return if false
 178   * @param string $url
 179   * @return string
 180   */
 181  function yourls_site_url($echo = true, $url = '' ) {
 182      $url = yourls_get_relative_url( $url );
 183      $url = trim( yourls_get_yourls_site() . '/' . $url, '/' );
 184  
 185      // Do not enforce (checking yourls_need_ssl() ) but check current usage so it won't force SSL on non-admin pages
 186      if( yourls_is_ssl() ) {
 187          $url = yourls_set_url_scheme( $url, 'https' );
 188      }
 189      $url = yourls_apply_filter( 'site_url', $url );
 190      if( $echo ) {
 191          echo $url;
 192      }
 193      return $url;
 194  }
 195  
 196  /**
 197   *  Get YOURLS_SITE value, trimmed and filtered
 198   *
 199   *  In addition of being filtered for plugins to hack this, this function is mostly here
 200   *  to help people entering "sho.rt/" instead of "sho.rt" in their config
 201   *
 202   *  @since 1.7.7
 203   *  @return string  YOURLS_SITE, trimmed and filtered
 204   */
 205  function yourls_get_yourls_site() {
 206      return yourls_apply_filter('get_yourls_site', trim(YOURLS_SITE, '/'));
 207  }
 208  
 209  /**
 210   * Change protocol of a URL to HTTPS if we are currently on HTTPS
 211   *
 212   * This function is used to avoid insert 'http://' images or scripts in a page when it's served through HTTPS,
 213   * to avoid "mixed content" errors.
 214   * So:
 215   *   - if you are on http://sho.rt/, 'http://something' and 'https://something' are left untouched.
 216   *   - if you are on https:/sho.rt/, 'http://something' is changed to 'https://something'
 217   *
 218   * So, arguably, this function is poorly named. It should be something like yourls_match_current_protocol_if_we_re_on_https
 219   *
 220   * @since 1.5.1
 221   * @param string $url        a URL
 222   * @param string $normal     Optional, the standard scheme (defaults to 'http://')
 223   * @param string $ssl        Optional, the SSL scheme (defaults to 'https://')
 224   * @return string            the modified URL, if applicable
 225   */
 226  function yourls_match_current_protocol( $url, $normal = 'http://', $ssl = 'https://' ) {
 227      // we're only doing something if we're currently serving through SSL and the input URL begins with 'http://' or 'https://'
 228      if( yourls_is_ssl() && in_array( yourls_get_protocol($url), array('http://', 'https://') ) ) {
 229          $url = str_replace( $normal, $ssl, $url );
 230      }
 231  
 232      return yourls_apply_filter( 'match_current_protocol', $url );
 233  }
 234  
 235  /**
 236   * Auto detect custom favicon in /user directory, fallback to YOURLS favicon, and echo/return its URL
 237   *
 238   * This function supersedes function yourls_favicon(), deprecated in 1.7.10, with a better naming.
 239   *
 240   * @since 1.7.10
 241   * @param  bool $echo   true to echo, false to silently return
 242   * @return string       favicon URL
 243   *
 244   */
 245  function yourls_get_yourls_favicon_url( $echo = true ) {
 246      static $favicon = null;
 247  
 248      if( $favicon !== null ) {
 249          if( $echo ) {
 250              echo $favicon;
 251          }
 252          return $favicon;
 253      }
 254  
 255      $custom = null;
 256      // search for favicon.(gif|ico|png|jpg|svg)
 257      foreach( array( 'gif', 'ico', 'png', 'jpg', 'svg' ) as $ext ) {
 258          if( file_exists( YOURLS_USERDIR. '/favicon.' . $ext ) ) {
 259              $custom = 'favicon.' . $ext;
 260              break;
 261          }
 262      }
 263  
 264      if( $custom ) {
 265          $favicon = yourls_site_url( false, YOURLS_USERURL . '/' . $custom );
 266      } else {
 267          $favicon = yourls_site_url( false ) . '/images/favicon.svg';
 268      }
 269  
 270      $favicon = yourls_apply_filter('get_favicon_url', $favicon);
 271  
 272      if( $echo ) {
 273          echo $favicon;
 274      }
 275      return $favicon;
 276  }


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