Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/environment/uri.php

Documentation is available at uri.php

  1. <?php
  2. /**
  3.  * @version        $Id: uri.php 6674 2007-02-19 05:52:03Z Jinx $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Environment
  6.  * @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19.  * JURI Class
  20.  *
  21.  * This class serves two purposes.  First to parse a URI and provide a common interface
  22.  * for the Joomla Framework to access and manipulate a URI.  Second to attain the URI of
  23.  * the current executing script from the server regardless of server.
  24.  *
  25.  * @author        Louis Landry <[email protected]>
  26.  * @package        Joomla.Framework
  27.  * @subpackage    Environment
  28.  * @since        1.5
  29.  */
  30. class JURI extends JObject
  31. {
  32.     /**
  33.      * Original URI
  34.      *
  35.      * @var string 
  36.      */
  37.     var $_uri = null;
  38.  
  39.     /**
  40.      * Protocol
  41.      *
  42.      * @var string 
  43.      */
  44.     var $_scheme = null;
  45.  
  46.     /**
  47.      * Host
  48.      *
  49.      * @var string 
  50.      */
  51.     var $_host = null;
  52.  
  53.     /**
  54.      * Port
  55.      *
  56.      * @var integer 
  57.      */
  58.     var $_port = null;
  59.  
  60.     /**
  61.      * Username
  62.      *
  63.      * @var string 
  64.      */
  65.     var $_user = null;
  66.  
  67.     /**
  68.      * Password
  69.      *
  70.      * @var string 
  71.      */
  72.     var $_pass = null;
  73.  
  74.     /**
  75.      * Path
  76.      *
  77.      * @var string 
  78.      */
  79.     var $_path = null;
  80.  
  81.     /**
  82.      * Query
  83.      *
  84.      * @var string 
  85.      */
  86.     var $_query = null;
  87.  
  88.     /**
  89.      * Anchor
  90.      *
  91.      * @var string 
  92.      */
  93.     var $_fragment = null;
  94.  
  95.     /**
  96.      * Query variable hash
  97.      *
  98.      * @var array 
  99.      */
  100.     var $_vars = array ();
  101.  
  102.     /**
  103.      * Constructor.
  104.      * You can pass a URI string to the constructor to initialize a specific URI.
  105.      *
  106.      * @param string $uri The optional URI string
  107.      */
  108.     function __construct($uri null)
  109.     {
  110.         if ($uri !== null{
  111.             $this->parse($uri);
  112.         }
  113.     }
  114.  
  115.     /**
  116.      * Returns a reference to a global JURI object, only creating it
  117.      * if it doesn't already exist.
  118.      *
  119.      * This method must be invoked as:
  120.      *         <pre>  $uri =& JURI::getInstance([$uri]);</pre>
  121.      *
  122.      * @static
  123.      * @param string $uri The URI to parse.  [optional: if null uses script URI]
  124.      * @return JURI  The URI object.
  125.      * @since 1.5
  126.      */
  127.     function &getInstance($uri 'SERVER')
  128.     {
  129.         static $instances array();
  130.  
  131.         if (!isset ($instances[$uri]))
  132.         {
  133.             // Are we obtaining the URI from the server?
  134.                         if ($uri == 'SERVER')
  135.             {
  136.                 // Determine if the request was over SSL (HTTPS)
  137.                                 if (isset($_SERVER['HTTPS']&& !empty($_SERVER['HTTPS']&& (strtolower($_SERVER['HTTPS']!= 'off')) {
  138.                     $https 's://';
  139.                 else {
  140.                     $https '://';
  141.                 }
  142.  
  143.                 /*
  144.                  * Since we are assigning the URI from the server variables, we first need
  145.                  * to determine if we are running on apache or IIS.  If PHP_SELF and REQUEST_URI
  146.                  * are present, we will assume we are running on apache.
  147.                  */
  148.                 if (!empty ($_SERVER['PHP_SELF']&& !empty ($_SERVER['REQUEST_URI'])) {
  149.  
  150.                     /*
  151.                      * To build the entire URI we need to prepend the protocol, and the http host
  152.                      * to the URI string.
  153.                      */
  154.                     $theURI 'http' $https $_SERVER['HTTP_HOST'$_SERVER['REQUEST_URI'];
  155.  
  156.                 /*
  157.                  * Since we do not have REQUEST_URI to work with, we will assume we are
  158.                  * running on IIS and will therefore need to work some magic with the SCRIPT_NAME and
  159.                  * QUERY_STRING environment variables.
  160.                  */
  161.                 }
  162.                  else
  163.                  {
  164.                     // IIS uses the SCRIPT_NAME variable instead of a REQUEST_URI variable... thanks, MS
  165.                                         $theURI 'http' $https $_SERVER['HTTP_HOST'$_SERVER['SCRIPT_NAME'];
  166.  
  167.                     // If the query string exists append it to the URI string
  168.                                         if (isset($_SERVER['QUERY_STRING']&& !empty($_SERVER['QUERY_STRING'])) {
  169.                         $theURI .= '?' $_SERVER['QUERY_STRING'];
  170.                     }
  171.                 }
  172.             }
  173.             else
  174.             {
  175.                 // We were given a URI
  176.                                 $theURI $uri;
  177.             }
  178.  
  179.             // Create the new JURI instance
  180.                         $instances[$urinew JURI($theURI);
  181.         }
  182.         return $instances[$uri];
  183.     }
  184.  
  185.     /**
  186.      * Returns the base URI for the request.
  187.      *
  188.      * @access    public
  189.      * @return    string    The base URI string
  190.      * @since    1.5
  191.      */
  192.     function base()
  193.     {
  194.         static $base;
  195.  
  196.         // Get the base request URL if not set
  197.         if (!isset($base))
  198.         {
  199.             $uri    =JURI::getInstance();
  200.  
  201.             $base $uri->getScheme().'://';
  202.             $base .= $uri->getHost();
  203.  
  204.             if ($port $uri->getPort()) {
  205.                 $base .= ":$port";
  206.             }
  207.  
  208.             if (strpos(php_sapi_name()'cgi'!== false && !empty($_SERVER['REQUEST_URI'])) {
  209.                 //Apache CGI
  210.                 $base .=  rtrim(dirname($_SERVER['PHP_SELF'])'/\\').'/';
  211.             else {
  212.                 //Others
  213.                 $base .=  rtrim(dirname($_SERVER['SCRIPT_NAME'])'/\\').'/';
  214.             }
  215.         }
  216.         return $base;
  217.     }
  218.  
  219.     /**
  220.      * Parse a given URI and populate the class fields
  221.      *
  222.      * @access public
  223.      * @param string $uri The URI string to parse
  224.      * @return boolean True on success
  225.      * @since 1.5
  226.      */
  227.     function parse($uri)
  228.     {
  229.         //Initialize variables
  230.         $retval false;
  231.  
  232.         // Set the original URI to fall back on
  233.         $this->_uri $uri;
  234.  
  235.         // Decode the passed in uri
  236.         $uri urldecode($uri);
  237.  
  238.         /*
  239.          * Parse the URI and populate the object fields.  If URI is parsed properly,
  240.          * set method return value to true.
  241.          */
  242.         if ($_parts @parse_url($uri)) {
  243.             $retval true;
  244.         }
  245.  
  246.         $this->_scheme = isset ($_parts['scheme']$_parts['scheme'null;
  247.         $this->_user = isset ($_parts['user']$_parts['user'null;
  248.         $this->_pass = isset ($_parts['pass']$_parts['pass'null;
  249.         $this->_host = isset ($_parts['host']$_parts['host'null;
  250.         $this->_port = isset ($_parts['port']$_parts['port'null;
  251.         $this->_path = isset ($_parts['path']$_parts['path'null;
  252.         $this->_query = isset ($_parts['query'])$_parts['query'null;
  253.         $this->_fragment = isset ($_parts['fragment']$_parts['fragment'null;
  254.  
  255.         //parse the query
  256.         if(isset ($_parts['query'])) parse_str($_parts['query']$this->_vars);
  257.  
  258.         return $retval;
  259.     }
  260.  
  261.     /**
  262.      * Returns full uri string
  263.      *
  264.      * @access public
  265.      * @param array $parts An array specifying the parts to render
  266.      * @return string The rendered URI string
  267.      * @since 1.5
  268.      */
  269.     function toString($parts array('scheme''user''pass''host''port''path''query''fragment'))
  270.     {
  271.         $uri '';
  272.         $uri .= in_array('scheme'$parts)  (!empty($this->_scheme$this->_scheme.'://' '''';
  273.         $uri .= in_array('user'$parts)    $this->_user '';
  274.         $uri .= in_array('pass'$parts)    (!empty ($this->_pass':' ''.$this->_pass(!empty ($this->_user'@' '''';
  275.         $uri .= in_array('host'$parts)    $this->_host '';
  276.         $uri .= in_array('port'$parts)    (!empty ($this->_port':' '').$this->_port '';
  277.         $uri .= in_array('path'$parts)    $this->_path '';
  278.         $uri .= in_array('query'$parts)    (!empty ($this->_query'?'.$this->_query '''';
  279.         $uri .= in_array('fragment'$parts)(!empty ($this->_fragment'#'.$this->_fragment '''';
  280.  
  281.         return $uri;
  282.     }
  283.  
  284.     /**
  285.      * Adds a query variable and value, replacing the value if it
  286.      * already exists and returning the old value.
  287.      *
  288.      * @access public
  289.      * @param string $name Name of the query variable to set
  290.      * @param string $value Value of the query variable
  291.      * @return string Previous value for the query variable
  292.      * @since 1.5
  293.      */
  294.     function setVar($name$value)
  295.     {
  296.         $tmp @$this->_vars[$name];
  297.         $this->_vars[$name$value;
  298.         $this->_query JURI::_buildQuery($this->_vars);
  299.         return $tmp;
  300.     }
  301.  
  302.     /**
  303.      * Returns a query variable by name
  304.      *
  305.      * @access public
  306.      * @param string $name Name of the query variable to get
  307.      * @return array Query variables
  308.      * @since 1.5
  309.      */
  310.     function getVar($name null$default=null)
  311.     {
  312.         if(isset($this->_vars[$name])) {
  313.             return $this->_vars[$name];
  314.         }
  315.         return $default;
  316.     }
  317.  
  318.     /**
  319.      * Removes an item from the query string variables if it exists
  320.      *
  321.      * @access public
  322.      * @param string $name Name of variable to remove
  323.      * @since 1.5
  324.      */
  325.     function delVar($name)
  326.     {
  327.         if (in_array($namearray_keys($this->_vars))) {
  328.             unset ($this->_vars[$name]);
  329.             $this->_query JURI::_buildQuery($this->_vars);
  330.         }
  331.     }
  332.  
  333.     /**
  334.      * Sets the query to a supplied string in format:
  335.      *         foo=bar&x=y
  336.      *
  337.      * @access public
  338.      * @param mixed (array|string) $query The query string
  339.      * @since 1.5
  340.      */
  341.     function setQuery($query)
  342.     {
  343.         if(!is_array($query)) {
  344.             $this->_query $query;
  345.             parse_str($query$this->_vars);
  346.         }
  347.  
  348.         if(is_array($query)) {
  349.             $this->_query JURI::_buildQuery($query);
  350.             $this->_vars $query;
  351.         }
  352.     }
  353.  
  354.     /**
  355.      * Returns flat query string
  356.      *
  357.      * @access public
  358.      * @return string Query string
  359.      * @since 1.5
  360.      */
  361.     function getQuery($toArray false)
  362.     {
  363.         if($toArray{
  364.             return $this->_vars;
  365.         }
  366.         return $this->_query;
  367.     }
  368.  
  369.     /**
  370.      * Get URI scheme (protocol)
  371.      *         ie. http, https, ftp, etc...
  372.      *
  373.      * @access public
  374.      * @return string The URI scheme
  375.      * @since 1.5
  376.      */
  377.     function getScheme({
  378.         return $this->_scheme;
  379.     }
  380.  
  381.     /**
  382.      * Set URI scheme (protocol)
  383.      *         ie. http, https, ftp, etc...
  384.      *
  385.      * @access public
  386.      * @param string $scheme The URI scheme
  387.      * @since 1.5
  388.      */
  389.     function setScheme($scheme{
  390.         $this->_scheme $scheme;
  391.     }
  392.  
  393.     /**
  394.      * Get URI username
  395.      *         returns the username, or null if no username was specified
  396.      *
  397.      * @access public
  398.      * @return string The URI username
  399.      * @since 1.5
  400.      */
  401.     function getUser({
  402.         return $this->_user;
  403.     }
  404.  
  405.     /**
  406.      * Set URI username
  407.      *
  408.      * @access public
  409.      * @param string $user The URI username
  410.      * @since 1.5
  411.      */
  412.     function setUser($user{
  413.         $this->_user $user;
  414.     }
  415.  
  416.     /**
  417.      * Get URI password
  418.      *         returns the password, or null if no password was specified
  419.      *
  420.      * @access public
  421.      * @return string The URI password
  422.      * @since 1.5
  423.      */
  424.     function getPass({
  425.         return $this->_pass;
  426.     }
  427.  
  428.     /**
  429.      * Set URI password
  430.      *
  431.      * @access public
  432.      * @param string $pass The URI password
  433.      * @since 1.5
  434.      */
  435.     function setPass($pass{
  436.         $this->_pass $pass;
  437.     }
  438.  
  439.     /**
  440.      * Get URI host
  441.      *         returns the hostname/ip, or null if no hostname/ip was specified
  442.      *
  443.      * @access public
  444.      * @return string The URI host
  445.      * @since 1.5
  446.      */
  447.     function getHost({
  448.         return $this->_host;
  449.     }
  450.  
  451.     /**
  452.      * Set URI host
  453.      *
  454.      * @access public
  455.      * @param string $host The URI host
  456.      * @since 1.5
  457.      */
  458.     function setHost($host{
  459.         $this->_host $host;
  460.     }
  461.  
  462.     /**
  463.      * Get URI port
  464.      *         returns the port number, or null if no port was specified
  465.      *
  466.      * @access public
  467.      * @return int The URI port number
  468.      */
  469.     function getPort({
  470.         return (isset ($this->_port)) $this->_port null;
  471.     }
  472.  
  473.     /**
  474.      * Set URI port
  475.      *
  476.      * @access public
  477.      * @param int $port The URI port number
  478.      * @since 1.5
  479.      */
  480.     function setPort($port{
  481.         $this->_port $port;
  482.     }
  483.  
  484.     /**
  485.      * Gets the URI path string
  486.      *
  487.      * @access public
  488.      * @return string The URI path string
  489.      * @since 1.5
  490.      */
  491.     function getPath({
  492.         return $this->_path;
  493.     }
  494.  
  495.     /**
  496.      * Set the URI path string
  497.      *
  498.      * @access public
  499.      * @param string $path The URI path string
  500.      * @since 1.5
  501.      */
  502.     function setPath($path{
  503.         $this->_path $this->_cleanPath($path);
  504.     }
  505.  
  506.     /**
  507.      * Get the URI archor string
  508.      *         everything after the "#"
  509.      *
  510.      * @access public
  511.      * @return string The URI anchor string
  512.      * @since 1.5
  513.      */
  514.     function getFragment({
  515.         return $this->_fragment;
  516.     }
  517.  
  518.     /**
  519.      * Set the URI anchor string
  520.      *         everything after the "#"
  521.      *
  522.      * @access public
  523.      * @param string $anchor The URI anchor string
  524.      * @since 1.5
  525.      */
  526.     function setFragment($anchor{
  527.         $this->_fragment $anchor;
  528.     }
  529.  
  530.     /**
  531.      * Checks whether the current URI is using HTTPS
  532.      *
  533.      * @access public
  534.      * @return boolean True if using SSL via HTTPS
  535.      * @since 1.5
  536.      */
  537.     function isSSL({
  538.         return $this->getScheme(== 'https' true false;
  539.     }
  540.  
  541.     /**
  542.      * Resolves //, ../ and ./ from a path and returns
  543.      * the result. Eg:
  544.      *
  545.      * /foo/bar/../boo.php    => /foo/boo.php
  546.      * /foo/bar/../../boo.php => /boo.php
  547.      * /foo/bar/.././/boo.php => /foo/boo.php
  548.      *
  549.      * @access private
  550.      * @param string $uri The URI path to clean
  551.      * @return string Cleaned and resolved URI path
  552.      * @since 1.5
  553.      */
  554.     function _cleanPath($path)
  555.     {
  556.         $path explode('/'str_replace('//''/'$path));
  557.  
  558.         for ($i 0$i count($path)$i ++{
  559.             if ($path[$i== '.'{
  560.                 unset ($path[$i]);
  561.                 $path array_values($path);
  562.                 $i --;
  563.  
  564.             }
  565.             elseif ($path[$i== '..' AND ($i OR ($i == AND $path[0!= ''))) {
  566.                 unset ($path[$i]);
  567.                 unset ($path[$i -1]);
  568.                 $path array_values($path);
  569.                 $i -= 2;
  570.  
  571.             }
  572.             elseif ($path[$i== '..' AND $i == AND $path[0== ''{
  573.                 unset ($path[$i]);
  574.                 $path array_values($path);
  575.                 $i --;
  576.  
  577.             else {
  578.                 continue;
  579.             }
  580.         }
  581.  
  582.         return implode('/'$path);
  583.     }
  584.  
  585.     /**
  586.      * Build a query from a array (reverse of the PHP parse_str())
  587.      *
  588.      * @access private
  589.      * @return string The resulting query string
  590.      * @since 1.5
  591.      * @see parse_str()
  592.      */
  593.     function _buildQuery ($params$akey null)
  594.     {
  595.         if !is_array($params|| count($params== {
  596.             return false;
  597.         }
  598.  
  599.         static $out array();
  600.  
  601.         //reset in case we are looping
  602.                 if!isset($akey&& !count($out) )  {
  603.             unset($out);
  604.             $out array();
  605.         }
  606.  
  607.         foreach $params as $key => $val )
  608.         {
  609.             if is_array($val) ) {
  610.                 $out[JURI::_buildQuery($val,$key);
  611.                 continue;
  612.             }
  613.  
  614.             $thekey !$akey $key $akey.'[]';
  615.             $out[$thekey."=".urlencode($val);
  616.         }
  617.  
  618.         return implode("&",$out);
  619.     }
  620. }
  621. ?>

Documentation generated on Mon, 05 Mar 2007 21:30:07 +0000 by phpDocumentor 1.3.1