[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/environment/ -> uri.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Environment
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  /**
  13   * JURI Class
  14   *
  15   * This class serves two purposes. First it parses a URI and provides a common interface
  16   * for the Joomla Platform to access and manipulate a URI.  Second it obtains the URI of
  17   * the current executing script from the server regardless of server.
  18   *
  19   * @package     Joomla.Platform
  20   * @subpackage  Environment
  21   * @since       11.1
  22   */
  23  class JURI extends JObject
  24  {
  25      /**
  26       * @var    string Original URI
  27       * @since  11.1
  28       */
  29      protected $_uri = null;
  30  
  31      /**
  32       * @var    string  Protocol
  33       * @since  11.1
  34       */
  35      protected $_scheme = null;
  36  
  37      /**
  38       * @var    string  Host
  39       * @since  11.1
  40       */
  41      protected $_host = null;
  42  
  43      /**
  44       * @var    integer  Port
  45       * @since  11.1
  46       */
  47      protected $_port = null;
  48  
  49      /**
  50       * @var    string  Username
  51       * @since  11.1
  52       */
  53      protected $_user = null;
  54  
  55      /**
  56       * @var    string  Password
  57       * @since  11.1
  58       */
  59      protected $_pass = null;
  60  
  61      /**
  62       * @var    string  Path
  63       * @since  11.1
  64       */
  65      protected $_path = null;
  66  
  67      /**
  68       * @var    string  Query
  69       * @since  11.1
  70       */
  71      protected $_query = null;
  72  
  73      /**
  74       * @var    string  Anchor
  75       * @since  11.1
  76       */
  77      protected $_fragment = null;
  78  
  79      /**
  80       * @var    array  Query variable hash
  81       * @since  11.1
  82       */
  83      protected $_vars = array();
  84  
  85      /**
  86       * @var    array  An array of JURI instances.
  87       * @since  11.1
  88       */
  89      protected static $instances = array();
  90  
  91      /**
  92       * @var    array  The current calculated base url segments.
  93       * @since  11.1
  94       */
  95      protected static $base = array();
  96  
  97      /**
  98       * @var    array  The current calculated root url segments.
  99       * @since  11.1
 100       */
 101      protected static $root = array();
 102  
 103      /**
 104       * @var    string  The current url.
 105       * @since  11.1
 106       */
 107      protected static $current;
 108  
 109      /**
 110       * Constructor.
 111       * You can pass a URI string to the constructor to initialise a specific URI.
 112       *
 113       * @param   string  $uri  The optional URI string
 114       *
 115       * @since   11.1
 116       */
 117  	public function __construct($uri = null)
 118      {
 119          if (!is_null($uri))
 120          {
 121              $this->parse($uri);
 122          }
 123      }
 124  
 125      /**
 126       * Magic method to get the string representation of the URI object.
 127       *
 128       * @return  string
 129       *
 130       * @since   11.1
 131       */
 132  	public function __toString()
 133      {
 134          return $this->toString();
 135      }
 136  
 137      /**
 138       * Returns the global JURI object, only creating it
 139       * if it doesn't already exist.
 140       *
 141       * @param   string  $uri  The URI to parse.  [optional: if null uses script URI]
 142       *
 143       * @return  JURI  The URI object.
 144       *
 145       * @since   11.1
 146       */
 147  	public static function getInstance($uri = 'SERVER')
 148      {
 149  
 150          if (empty(self::$instances[$uri]))
 151          {
 152              // Are we obtaining the URI from the server?
 153              if ($uri == 'SERVER')
 154              {
 155                  // Determine if the request was over SSL (HTTPS).
 156                  if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off'))
 157                  {
 158                      $https = 's://';
 159                  }
 160                  else
 161                  {
 162                      $https = '://';
 163                  }
 164  
 165                  // Since we are assigning the URI from the server variables, we first need
 166                  // to determine if we are running on apache or IIS.  If PHP_SELF and REQUEST_URI
 167                  // are present, we will assume we are running on apache.
 168  
 169                  if (!empty($_SERVER['PHP_SELF']) && !empty($_SERVER['REQUEST_URI']))
 170                  {
 171                      // To build the entire URI we need to prepend the protocol, and the http host
 172                      // to the URI string.
 173                      $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
 174                  }
 175                  else
 176                  {
 177                      // Since we do not have REQUEST_URI to work with, we will assume we are
 178                      // running on IIS and will therefore need to work some magic with the SCRIPT_NAME and
 179                      // QUERY_STRING environment variables.
 180  
 181                      // IIS uses the SCRIPT_NAME variable instead of a REQUEST_URI variable... thanks, MS
 182                      $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
 183  
 184                      // If the query string exists append it to the URI string
 185                      if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING']))
 186                      {
 187                          $theURI .= '?' . $_SERVER['QUERY_STRING'];
 188                      }
 189                  }
 190              }
 191              else
 192              {
 193                  // We were given a URI
 194                  $theURI = $uri;
 195              }
 196  
 197              // Create the new JURI instance
 198              self::$instances[$uri] = new JURI($theURI);
 199          }
 200          return self::$instances[$uri];
 201      }
 202  
 203      /**
 204       * Returns the base URI for the request.
 205       *
 206       * @param   boolean  $pathonly  If false, prepend the scheme, host and port information. Default is false.
 207       *
 208       * @return  string  The base URI string
 209       *
 210       * @since   11.1
 211       */
 212  	public static function base($pathonly = false)
 213      {
 214          // Get the base request path.
 215          if (empty(self::$base))
 216          {
 217              $config = JFactory::getConfig();
 218              $live_site = $config->get('live_site');
 219              if (trim($live_site) != '')
 220              {
 221                  $uri = self::getInstance($live_site);
 222                  self::$base['prefix'] = $uri->toString(array('scheme', 'host', 'port'));
 223                  self::$base['path'] = rtrim($uri->toString(array('path')), '/\\');
 224  
 225                  if (JPATH_BASE == JPATH_ADMINISTRATOR)
 226                  {
 227                      self::$base['path'] .= '/administrator';
 228                  }
 229              }
 230              else
 231              {
 232                  $uri = self::getInstance();
 233                  self::$base['prefix'] = $uri->toString(array('scheme', 'host', 'port'));
 234  
 235                  if (strpos(php_sapi_name(), 'cgi') !== false && !ini_get('cgi.fix_pathinfo') && !empty($_SERVER['REQUEST_URI']))
 236                  {
 237                      // PHP-CGI on Apache with "cgi.fix_pathinfo = 0"
 238  
 239                      // We shouldn't have user-supplied PATH_INFO in PHP_SELF in this case
 240                      // because PHP will not work with PATH_INFO at all.
 241                      $script_name = $_SERVER['PHP_SELF'];
 242                  }
 243                  else
 244                  {
 245                      // Others
 246                      $script_name = $_SERVER['SCRIPT_NAME'];
 247                  }
 248  
 249                  self::$base['path'] = rtrim(dirname($script_name), '/\\');
 250              }
 251          }
 252  
 253          return $pathonly === false ? self::$base['prefix'] . self::$base['path'] . '/' : self::$base['path'];
 254      }
 255  
 256      /**
 257       * Returns the root URI for the request.
 258       *
 259       * @param   boolean  $pathonly  If false, prepend the scheme, host and port information. Default is false.
 260       * @param   string   $path      The path
 261       *
 262       * @return  string  The root URI string.
 263       *
 264       * @since   11.1
 265       */
 266  	public static function root($pathonly = false, $path = null)
 267      {
 268          // Get the scheme
 269          if (empty(self::$root))
 270          {
 271              $uri = self::getInstance(self::base());
 272              self::$root['prefix'] = $uri->toString(array('scheme', 'host', 'port'));
 273              self::$root['path'] = rtrim($uri->toString(array('path')), '/\\');
 274          }
 275  
 276          // Get the scheme
 277          if (isset($path))
 278          {
 279              self::$root['path'] = $path;
 280          }
 281  
 282          return $pathonly === false ? self::$root['prefix'] . self::$root['path'] . '/' : self::$root['path'];
 283      }
 284  
 285      /**
 286       * Returns the URL for the request, minus the query.
 287       *
 288       * @return  string
 289       *
 290       * @since   11.1
 291       */
 292  	public static function current()
 293      {
 294          // Get the current URL.
 295          if (empty(self::$current))
 296          {
 297              $uri = self::getInstance();
 298              self::$current = $uri->toString(array('scheme', 'host', 'port', 'path'));
 299          }
 300  
 301          return self::$current;
 302      }
 303  
 304      /**
 305       * Method to reset class static members for testing and other various issues.
 306       *
 307       * @return  void
 308       *
 309       * @since   11.1
 310       */
 311  	public static function reset()
 312      {
 313          self::$instances = array();
 314          self::$base = array();
 315          self::$root = array();
 316          self::$current = '';
 317      }
 318  
 319      /**
 320       * Parse a given URI and populate the class fields.
 321       *
 322       * @param   string  $uri  The URI string to parse.
 323       *
 324       * @return  boolean  True on success.
 325       *
 326       * @since   11.1
 327       */
 328  	public function parse($uri)
 329      {
 330          // Initialise variables
 331          $retval = false;
 332  
 333          // Set the original URI to fall back on
 334          $this->_uri = $uri;
 335  
 336          // Parse the URI and populate the object fields.  If URI is parsed properly,
 337          // set method return value to true.
 338  
 339          if ($_parts = JString::parse_url($uri))
 340          {
 341              $retval = true;
 342          }
 343  
 344          // We need to replace &amp; with & for parse_str to work right...
 345          if (isset($_parts['query']) && strpos($_parts['query'], '&amp;'))
 346          {
 347              $_parts['query'] = str_replace('&amp;', '&', $_parts['query']);
 348          }
 349  
 350          $this->_scheme = isset($_parts['scheme']) ? $_parts['scheme'] : null;
 351          $this->_user = isset($_parts['user']) ? $_parts['user'] : null;
 352          $this->_pass = isset($_parts['pass']) ? $_parts['pass'] : null;
 353          $this->_host = isset($_parts['host']) ? $_parts['host'] : null;
 354          $this->_port = isset($_parts['port']) ? $_parts['port'] : null;
 355          $this->_path = isset($_parts['path']) ? $_parts['path'] : null;
 356          $this->_query = isset($_parts['query']) ? $_parts['query'] : null;
 357          $this->_fragment = isset($_parts['fragment']) ? $_parts['fragment'] : null;
 358  
 359          // Parse the query
 360  
 361          if (isset($_parts['query']))
 362          {
 363              parse_str($_parts['query'], $this->_vars);
 364          }
 365          return $retval;
 366      }
 367  
 368      /**
 369       * Returns full uri string.
 370       *
 371       * @param   array  $parts  An array specifying the parts to render.
 372       *
 373       * @return  string  The rendered URI string.
 374       *
 375       * @since   11.1
 376       */
 377  	public function toString($parts = array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment'))
 378      {
 379          // Make sure the query is created
 380          $query = $this->getQuery();
 381  
 382          $uri = '';
 383          $uri .= in_array('scheme', $parts) ? (!empty($this->_scheme) ? $this->_scheme . '://' : '') : '';
 384          $uri .= in_array('user', $parts) ? $this->_user : '';
 385          $uri .= in_array('pass', $parts) ? (!empty($this->_pass) ? ':' : '') . $this->_pass . (!empty($this->_user) ? '@' : '') : '';
 386          $uri .= in_array('host', $parts) ? $this->_host : '';
 387          $uri .= in_array('port', $parts) ? (!empty($this->_port) ? ':' : '') . $this->_port : '';
 388          $uri .= in_array('path', $parts) ? $this->_path : '';
 389          $uri .= in_array('query', $parts) ? (!empty($query) ? '?' . $query : '') : '';
 390          $uri .= in_array('fragment', $parts) ? (!empty($this->_fragment) ? '#' . $this->_fragment : '') : '';
 391  
 392          return $uri;
 393      }
 394  
 395      /**
 396       * Adds a query variable and value, replacing the value if it
 397       * already exists and returning the old value.
 398       *
 399       * @param   string  $name   Name of the query variable to set.
 400       * @param   string  $value  Value of the query variable.
 401       *
 402       * @return  string  Previous value for the query variable.
 403       *
 404       * @since   11.1
 405       */
 406  	public function setVar($name, $value)
 407      {
 408          $tmp = @$this->_vars[$name];
 409          $this->_vars[$name] = $value;
 410  
 411          // Empty the query
 412          $this->_query = null;
 413  
 414          return $tmp;
 415      }
 416  
 417      /**
 418       * Checks if variable exists.
 419       *
 420       * @param   string  $name  Name of the query variable to check.
 421       *
 422       * @return  boolean  True if the variable exists.
 423       *
 424       * @since   11.1
 425       */
 426  	public function hasVar($name)
 427      {
 428          return array_key_exists($name, $this->_vars);
 429      }
 430  
 431      /**
 432       * Returns a query variable by name.
 433       *
 434       * @param   string  $name     Name of the query variable to get.
 435       * @param   string  $default  Default value to return if the variable is not set.
 436       *
 437       * @return  array   Query variables.
 438       *
 439       * @since   11.1
 440       */
 441  	public function getVar($name, $default = null)
 442      {
 443          if (array_key_exists($name, $this->_vars))
 444          {
 445              return $this->_vars[$name];
 446          }
 447          return $default;
 448      }
 449  
 450      /**
 451       * Removes an item from the query string variables if it exists.
 452       *
 453       * @param   string  $name  Name of variable to remove.
 454       *
 455       * @return  void
 456       *
 457       * @since   11.1
 458       */
 459  	public function delVar($name)
 460      {
 461          if (array_key_exists($name, $this->_vars))
 462          {
 463              unset($this->_vars[$name]);
 464  
 465              //empty the query
 466              $this->_query = null;
 467          }
 468      }
 469  
 470      /**
 471       * Sets the query to a supplied string in format:
 472       * foo=bar&x=y
 473       *
 474       * @param   mixed  $query  The query string or array.
 475       *
 476       * @return  void
 477       *
 478       * @since   11.1
 479       */
 480  	public function setQuery($query)
 481      {
 482          if (is_array($query))
 483          {
 484              $this->_vars = $query;
 485          }
 486          else
 487          {
 488              if (strpos($query, '&amp;') !== false)
 489              {
 490                  $query = str_replace('&amp;', '&', $query);
 491              }
 492              parse_str($query, $this->_vars);
 493          }
 494  
 495          // Empty the query
 496          $this->_query = null;
 497      }
 498  
 499      /**
 500       * Returns flat query string.
 501       *
 502       * @param   boolean  $toArray  True to return the query as a key => value pair array.
 503       *
 504       * @return  string   Query string.
 505       *
 506       * @since   11.1
 507       */
 508  	public function getQuery($toArray = false)
 509      {
 510          if ($toArray)
 511          {
 512              return $this->_vars;
 513          }
 514  
 515          // If the query is empty build it first
 516          if (is_null($this->_query))
 517          {
 518              $this->_query = self::buildQuery($this->_vars);
 519          }
 520  
 521          return $this->_query;
 522      }
 523  
 524      /**
 525       * Build a query from a array (reverse of the PHP parse_str()).
 526       *
 527       * @param   array  $params  The array of key => value pairs to return as a query string.
 528       *
 529       * @return  string  The resulting query string.
 530       *
 531       * @see     parse_str()
 532       * @since   11.1
 533       */
 534  	public static function buildQuery($params)
 535      {
 536          if (!is_array($params) || count($params) == 0)
 537          {
 538              return false;
 539          }
 540  
 541          return urldecode(http_build_query($params, '', '&'));
 542      }
 543  
 544      /**
 545       * Get URI scheme (protocol)
 546       * ie. http, https, ftp, etc...
 547       *
 548       * @return  string  The URI scheme.
 549       *
 550       * @since   11.1
 551       */
 552  	public function getScheme()
 553      {
 554          return $this->_scheme;
 555      }
 556  
 557      /**
 558       * Set URI scheme (protocol)
 559       * ie. http, https, ftp, etc...
 560       *
 561       * @param   string  $scheme  The URI scheme.
 562       *
 563       * @return  void
 564       *
 565       * @since   11.1
 566       */
 567  	public function setScheme($scheme)
 568      {
 569          $this->_scheme = $scheme;
 570      }
 571  
 572      /**
 573       * Get URI username
 574       * Returns the username, or null if no username was specified.
 575       *
 576       * @return  string  The URI username.
 577       *
 578       * @since   11.1
 579       */
 580  	public function getUser()
 581      {
 582          return $this->_user;
 583      }
 584  
 585      /**
 586       * Set URI username.
 587       *
 588       * @param   string  $user  The URI username.
 589       *
 590       * @return  void
 591       *
 592       * @since   11.1
 593       */
 594  	public function setUser($user)
 595      {
 596          $this->_user = $user;
 597      }
 598  
 599      /**
 600       * Get URI password
 601       * Returns the password, or null if no password was specified.
 602       *
 603       * @return  string  The URI password.
 604       *
 605       * @since   11.1
 606       */
 607  	public function getPass()
 608      {
 609          return $this->_pass;
 610      }
 611  
 612      /**
 613       * Set URI password.
 614       *
 615       * @param   string  $pass  The URI password.
 616       *
 617       * @return  void
 618       *
 619       * @since   11.1
 620       */
 621  	public function setPass($pass)
 622      {
 623          $this->_pass = $pass;
 624      }
 625  
 626      /**
 627       * Get URI host
 628       * Returns the hostname/ip or null if no hostname/ip was specified.
 629       *
 630       * @return  string  The URI host.
 631       *
 632       * @since   11.1
 633       */
 634  	public function getHost()
 635      {
 636          return $this->_host;
 637      }
 638  
 639      /**
 640       * Set URI host.
 641       *
 642       * @param   string  $host  The URI host.
 643       *
 644       * @return  void
 645       *
 646       * @since   11.1
 647       */
 648  	public function setHost($host)
 649      {
 650          $this->_host = $host;
 651      }
 652  
 653      /**
 654       * Get URI port
 655       * Returns the port number, or null if no port was specified.
 656       *
 657       * @return  integer  The URI port number.
 658       *
 659       * @since   11.1
 660       */
 661  	public function getPort()
 662      {
 663          return (isset($this->_port)) ? $this->_port : null;
 664      }
 665  
 666      /**
 667       * Set URI port.
 668       *
 669       * @param   integer  $port  The URI port number.
 670       *
 671       * @return  void
 672       *
 673       * @since   11.1
 674       */
 675  	public function setPort($port)
 676      {
 677          $this->_port = $port;
 678      }
 679  
 680      /**
 681       * Gets the URI path string.
 682       *
 683       * @return  string  The URI path string.
 684       *
 685       * @since   11.1
 686       */
 687  	public function getPath()
 688      {
 689          return $this->_path;
 690      }
 691  
 692      /**
 693       * Set the URI path string.
 694       *
 695       * @param   string  $path  The URI path string.
 696       *
 697       * @return  void
 698       *
 699       * @since   11.1
 700       */
 701  	public function setPath($path)
 702      {
 703          $this->_path = $this->_cleanPath($path);
 704      }
 705  
 706      /**
 707       * Get the URI archor string
 708       * Everything after the "#".
 709       *
 710       * @return  string  The URI anchor string.
 711       *
 712       * @since   11.1
 713       */
 714  	public function getFragment()
 715      {
 716          return $this->_fragment;
 717      }
 718  
 719      /**
 720       * Set the URI anchor string
 721       * everything after the "#".
 722       *
 723       * @param   string  $anchor  The URI anchor string.
 724       *
 725       * @return  void
 726       *
 727       * @since   11.1
 728       */
 729  	public function setFragment($anchor)
 730      {
 731          $this->_fragment = $anchor;
 732      }
 733  
 734      /**
 735       * Checks whether the current URI is using HTTPS.
 736       *
 737       * @return  boolean  True if using SSL via HTTPS.
 738       *
 739       * @since   11.1
 740       */
 741  	public function isSSL()
 742      {
 743          return $this->getScheme() == 'https' ? true : false;
 744      }
 745  
 746      /**
 747       * Checks if the supplied URL is internal
 748       *
 749       * @param   string  $url  The URL to check.
 750       *
 751       * @return  boolean  True if Internal.
 752       *
 753       * @since   11.1
 754       */
 755  	public static function isInternal($url)
 756      {
 757          $uri = self::getInstance($url);
 758          $base = $uri->toString(array('scheme', 'host', 'port', 'path'));
 759          $host = $uri->toString(array('scheme', 'host', 'port'));
 760          if (stripos($base, self::base()) !== 0 && !empty($host))
 761          {
 762              return false;
 763          }
 764          return true;
 765      }
 766  
 767      /**
 768       * Resolves //, ../ and ./ from a path and returns
 769       * the result. Eg:
 770       *
 771       * /foo/bar/../boo.php    => /foo/boo.php
 772       * /foo/bar/../../boo.php => /boo.php
 773       * /foo/bar/.././/boo.php => /foo/boo.php
 774       *
 775       * @param   string  $path  The URI path to clean.
 776       *
 777       * @return  string  Cleaned and resolved URI path.
 778       *
 779       * @since   11.1
 780       */
 781  	protected function _cleanPath($path)
 782      {
 783          $path = explode('/', preg_replace('#(/+)#', '/', $path));
 784  
 785          for ($i = 0, $n = count($path); $i < $n; $i++)
 786          {
 787              if ($path[$i] == '.' or $path[$i] == '..')
 788              {
 789                  if (($path[$i] == '.') or ($path[$i] == '..' and $i == 1 and $path[0] == ''))
 790                  {
 791                      unset($path[$i]);
 792                      $path = array_values($path);
 793                      $i--;
 794                      $n--;
 795                  }
 796                  elseif ($path[$i] == '..' and ($i > 1 or ($i == 1 and $path[0] != '')))
 797                  {
 798                      unset($path[$i]);
 799                      unset($path[$i - 1]);
 800                      $path = array_values($path);
 801                      $i -= 2;
 802                      $n -= 2;
 803                  }
 804              }
 805          }
 806  
 807          return implode('/', $path);
 808      }
 809  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1