[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Uri/ -> Uri.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2005 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Uri;
  11  
  12  use Joomla\CMS\Factory;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Uri Class
  20   *
  21   * This class serves two purposes. First it parses a URI and provides a common interface
  22   * for the Joomla Platform to access and manipulate a URI.  Second it obtains the URI of
  23   * the current executing script from the server regardless of server.
  24   *
  25   * @since  1.7.0
  26   */
  27  class Uri extends \Joomla\Uri\Uri
  28  {
  29      /**
  30       * @var    Uri[]  An array of Uri instances.
  31       * @since  1.7.0
  32       */
  33      protected static $instances = array();
  34  
  35      /**
  36       * @var    array  The current calculated base url segments.
  37       * @since  1.7.0
  38       */
  39      protected static $base = array();
  40  
  41      /**
  42       * @var    array  The current calculated root url segments.
  43       * @since  1.7.0
  44       */
  45      protected static $root = array();
  46  
  47      /**
  48       * @var    string  The current url.
  49       * @since  1.7.0
  50       */
  51      protected static $current;
  52  
  53      /**
  54       * Returns the global Uri object, only creating it if it doesn't already exist.
  55       *
  56       * @param   string  $uri  The URI to parse.  [optional: if null uses script URI]
  57       *
  58       * @return  Uri  The URI object.
  59       *
  60       * @since   1.7.0
  61       */
  62      public static function getInstance($uri = 'SERVER')
  63      {
  64          if (empty(static::$instances[$uri])) {
  65              // Are we obtaining the URI from the server?
  66              if ($uri === 'SERVER') {
  67                  // Determine if the request was over SSL (HTTPS).
  68                  if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) !== 'off')) {
  69                      $https = 's://';
  70                  } elseif (
  71                      (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
  72                      && !empty($_SERVER['HTTP_X_FORWARDED_PROTO'])
  73                      && (strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) !== 'http'))
  74                  ) {
  75                      $https = 's://';
  76                  } else {
  77                      $https = '://';
  78                  }
  79  
  80                  /*
  81                   * Since we are assigning the URI from the server variables, we first need
  82                   * to determine if we are running on apache or IIS.  If PHP_SELF and REQUEST_URI
  83                   * are present, we will assume we are running on apache.
  84                   */
  85  
  86                  if (!empty($_SERVER['PHP_SELF']) && !empty($_SERVER['REQUEST_URI'])) {
  87                      // To build the entire URI we need to prepend the protocol, and the http host
  88                      // to the URI string.
  89                      $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  90                  } else {
  91                      /*
  92                       * Since we do not have REQUEST_URI to work with, we will assume we are
  93                       * running on IIS and will therefore need to work some magic with the SCRIPT_NAME and
  94                       * QUERY_STRING environment variables.
  95                       *
  96                       * IIS uses the SCRIPT_NAME variable instead of a REQUEST_URI variable... thanks, MS
  97                       */
  98                      $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
  99  
 100                      // If the query string exists append it to the URI string
 101                      if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
 102                          $theURI .= '?' . $_SERVER['QUERY_STRING'];
 103                      }
 104                  }
 105  
 106                  // Extra cleanup to remove invalid chars in the URL to prevent injections through the Host header
 107                  $theURI = str_replace(array("'", '"', '<', '>'), array('%27', '%22', '%3C', '%3E'), $theURI);
 108              } else {
 109                  // We were given a URI
 110                  $theURI = $uri;
 111              }
 112  
 113              static::$instances[$uri] = new static($theURI);
 114          }
 115  
 116          return static::$instances[$uri];
 117      }
 118  
 119      /**
 120       * Returns the base URI for the request.
 121       *
 122       * @param   boolean  $pathonly  If false, prepend the scheme, host and port information. Default is false.
 123       *
 124       * @return  string  The base URI string
 125       *
 126       * @since   1.7.0
 127       */
 128      public static function base($pathonly = false)
 129      {
 130          // Get the base request path.
 131          if (empty(static::$base)) {
 132              $config = Factory::getContainer()->get('config');
 133              $uri = static::getInstance();
 134              $live_site = ($uri->isSsl()) ? str_replace('http://', 'https://', $config->get('live_site', '')) : $config->get('live_site', '');
 135  
 136              if (trim($live_site) != '') {
 137                  $uri = static::getInstance($live_site);
 138                  static::$base['prefix'] = $uri->toString(array('scheme', 'host', 'port'));
 139                  static::$base['path'] = rtrim($uri->toString(array('path')), '/\\');
 140  
 141                  if (\defined('JPATH_BASE') && \defined('JPATH_ADMINISTRATOR') && JPATH_BASE == JPATH_ADMINISTRATOR) {
 142                      static::$base['path'] .= '/administrator';
 143                  }
 144  
 145                  if (\defined('JPATH_BASE') && \defined('JPATH_API') && JPATH_BASE == JPATH_API) {
 146                      static::$base['path'] .= '/api';
 147                  }
 148              } else {
 149                  static::$base['prefix'] = $uri->toString(array('scheme', 'host', 'port'));
 150  
 151                  if (strpos(PHP_SAPI, 'cgi') !== false && !ini_get('cgi.fix_pathinfo') && !empty($_SERVER['REQUEST_URI'])) {
 152                      // PHP-CGI on Apache with "cgi.fix_pathinfo = 0"
 153  
 154                      // We shouldn't have user-supplied PATH_INFO in PHP_SELF in this case
 155                      // because PHP will not work with PATH_INFO at all.
 156                      $script_name = $_SERVER['PHP_SELF'];
 157                  } else {
 158                      // Others
 159                      $script_name = $_SERVER['SCRIPT_NAME'];
 160                  }
 161  
 162                  // Extra cleanup to remove invalid chars in the URL to prevent injections through broken server implementation
 163                  $script_name = str_replace(array("'", '"', '<', '>'), array('%27', '%22', '%3C', '%3E'), $script_name);
 164  
 165                  static::$base['path'] = rtrim(\dirname($script_name), '/\\');
 166              }
 167          }
 168  
 169          return $pathonly === false ? static::$base['prefix'] . static::$base['path'] . '/' : static::$base['path'];
 170      }
 171  
 172      /**
 173       * Returns the root URI for the request.
 174       *
 175       * @param   boolean  $pathonly  If false, prepend the scheme, host and port information. Default is false.
 176       * @param   string   $path      The path
 177       *
 178       * @return  string  The root URI string.
 179       *
 180       * @since   1.7.0
 181       */
 182      public static function root($pathonly = false, $path = null)
 183      {
 184          // Get the scheme
 185          if (empty(static::$root)) {
 186              $uri = static::getInstance(static::base());
 187              static::$root['prefix'] = $uri->toString(array('scheme', 'host', 'port'));
 188              static::$root['path'] = rtrim($uri->toString(array('path')), '/\\');
 189          }
 190  
 191          // Get the scheme
 192          if (isset($path)) {
 193              static::$root['path'] = $path;
 194          }
 195  
 196          return $pathonly === false ? static::$root['prefix'] . static::$root['path'] . '/' : static::$root['path'];
 197      }
 198  
 199      /**
 200       * Returns the URL for the request, minus the query.
 201       *
 202       * @return  string
 203       *
 204       * @since   1.7.0
 205       */
 206      public static function current()
 207      {
 208          // Get the current URL.
 209          if (empty(static::$current)) {
 210              $uri = static::getInstance();
 211              static::$current = $uri->toString(array('scheme', 'host', 'port', 'path'));
 212          }
 213  
 214          return static::$current;
 215      }
 216  
 217      /**
 218       * Method to reset class static members for testing and other various issues.
 219       *
 220       * @return  void
 221       *
 222       * @since   1.7.0
 223       */
 224      public static function reset()
 225      {
 226          static::$instances = array();
 227          static::$base = array();
 228          static::$root = array();
 229          static::$current = '';
 230      }
 231  
 232      /**
 233       * Checks if the supplied URL is internal
 234       *
 235       * @param   string  $url  The URL to check.
 236       *
 237       * @return  boolean  True if Internal.
 238       *
 239       * @since   1.7.0
 240       */
 241      public static function isInternal($url)
 242      {
 243          $url = str_replace('\\', '/', $url);
 244  
 245          $uri = static::getInstance($url);
 246          $base = $uri->toString(array('scheme', 'host', 'port', 'path'));
 247          $host = $uri->toString(array('scheme', 'host', 'port'));
 248  
 249          // @see UriTest
 250          if (
 251              empty($host) && strpos($uri->path, 'index.php') === 0
 252              || !empty($host) && preg_match('#' . preg_quote(static::base(), '#') . '#', $base)
 253              || !empty($host) && $host === static::getInstance(static::base())->host && strpos($uri->path, 'index.php') !== false
 254              || !empty($host) && $base === $host && preg_match('#' . preg_quote($base, '#') . '#', static::base())
 255          ) {
 256              return true;
 257          }
 258  
 259          return false;
 260      }
 261  
 262      /**
 263       * Build a query from an array (reverse of the PHP parse_str()).
 264       *
 265       * @param   array  $params  The array of key => value pairs to return as a query string.
 266       *
 267       * @return  string  The resulting query string.
 268       *
 269       * @see     parse_str()
 270       * @since   1.7.0
 271       * @note    The parent method is protected, this exposes it as public for B/C
 272       */
 273      public static function buildQuery(array $params)
 274      {
 275          return parent::buildQuery($params);
 276      }
 277  
 278      /**
 279       * Parse a given URI and populate the class fields.
 280       *
 281       * @param   string  $uri  The URI string to parse.
 282       *
 283       * @return  boolean  True on success.
 284       *
 285       * @since   1.7.0
 286       * @note    The parent method is protected, this exposes it as public for B/C
 287       */
 288      public function parse($uri)
 289      {
 290          return parent::parse($uri);
 291      }
 292  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer