Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Source code for file /joomla/environment/uri.php

Documentation is available at uri.php

  1. <?php
  2. /**
  3.  * @version        $Id: uri.php 9764 2007-12-30 07:48:11Z ircmaxell $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Environment
  6.  * @copyright    Copyright (C) 2005 - 2008 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 <louis.landry@joomla.org>
  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.                 
  138.                 if (isset($_SERVER['HTTPS']&& !empty($_SERVER['HTTPS']&& (strtolower($_SERVER['HTTPS']!= 'off')) {
  139.                     $https 's://';
  140.                 else {
  141.                     $https '://';
  142.                 }
  143.  
  144.                 /*
  145.                  * Since we are assigning the URI from the server variables, we first need
  146.                  * to determine if we are running on apache or IIS.  If PHP_SELF and REQUEST_URI
  147.                  * are present, we will assume we are running on apache.
  148.                  */
  149.                 if (!empty ($_SERVER['PHP_SELF']&& !empty ($_SERVER['REQUEST_URI'])) {
  150.  
  151.                     /*
  152.                      * To build the entire URI we need to prepend the protocol, and the http host
  153.                      * to the URI string.
  154.                      */
  155.                     $theURI 'http' $https $_SERVER['HTTP_HOST'$_SERVER['REQUEST_URI'];
  156.  
  157.                 /*
  158.                  * Since we do not have REQUEST_URI to work with, we will assume we are
  159.                  * running on IIS and will therefore need to work some magic with the SCRIPT_NAME and
  160.                  * QUERY_STRING environment variables.
  161.                  */
  162.                 }
  163.                  else
  164.                  {
  165.                     // IIS uses the SCRIPT_NAME variable instead of a REQUEST_URI variable... thanks, MS
  166.                     
  167.                     $theURI 'http' $https $_SERVER['HTTP_HOST'$_SERVER['SCRIPT_NAME'];
  168.  
  169.                     // If the query string exists append it to the URI string
  170.                     
  171.                     if (isset($_SERVER['QUERY_STRING']&& !empty($_SERVER['QUERY_STRING'])) {
  172.                         $theURI .= '?' $_SERVER['QUERY_STRING'];
  173.                     }
  174.                 }
  175.  
  176.                 // Now we need to clean what we got since we can't trust the server var
  177.                 $theURI urldecode($theURI);
  178.                 $theURI str_replace('"''&quot;',$theURI);
  179.                 $theURI str_replace('<''&lt;',$theURI);
  180.                 $theURI str_replace('>''&gt;',$theURI);
  181.                 $theURI preg_replace('/eval\((.*)\)/'''$theURI);
  182.                 $theURI preg_replace('/[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']/''""'$theURI);
  183.             }
  184.             else
  185.             {
  186.                 // We were given a URI
  187.                 $theURI $uri;
  188.             }
  189.  
  190.             // Create the new JURI instance
  191.             $instances[$urinew JURI($theURI);
  192.         }
  193.         return $instances[$uri];
  194.     }
  195.  
  196.     /**
  197.      * Returns the base URI for the request.
  198.      *
  199.      * @access    public
  200.      * @static
  201.      * @param    boolean $pathonly If true, prepend the scheme, host and port information. Default is false.
  202.      * @return    string    The base URI string
  203.      * @since    1.5
  204.      */
  205.     function base($pathonly false)
  206.     {
  207.         static $base;
  208.  
  209.         // Get the base request path
  210.         if (!isset($base))
  211.         {
  212.             $uri             =JURI::getInstance();
  213.             $base['prefix'$uri->toStringarray('scheme''host''port'));
  214.  
  215.             if (strpos(php_sapi_name()'cgi'!== false && !empty($_SERVER['REQUEST_URI'])) {
  216.                 //Apache CGI
  217.                 $base['path'=  rtrim(dirname($_SERVER['PHP_SELF'])'/\\');
  218.             else {
  219.                 //Others
  220.                 $base['path'=  rtrim(dirname($_SERVER['SCRIPT_NAME'])'/\\');
  221.             }
  222.         }
  223.  
  224.         return $pathonly === false $base['prefix'].$base['path'].'/' $base['path'];
  225.     }
  226.  
  227.     /**
  228.      * Returns the root URI for the request.
  229.      *
  230.      * @access    public
  231.      * @static
  232.      * @param    boolean $pathonly If true, prepend the scheme, host and port information. Default is false.
  233.      * @return    string    The root URI string
  234.      * @since    1.5
  235.      */
  236.     function root($pathonly false$path null)
  237.     {
  238.         static $root;
  239.  
  240.         // Get the scheme
  241.         if(!isset($root))
  242.         {
  243.             $uri            =JURI::getInstance();
  244.             $root['prefix'$uri->toStringarray('scheme''host''port'));
  245.             $root['path']    JURI::base(true);
  246.         }
  247.  
  248.         // Get the scheme
  249.         if(isset($path)) {
  250.             $root['path']    $path;
  251.         }
  252.  
  253.         return $pathonly === false $root['prefix'].$root['path'].'/' $root['path'];
  254.     }
  255.  
  256.     /**
  257.      * Returns the URL for the request, minus the query
  258.      *
  259.      * @access    public
  260.      * @return    string 
  261.      * @since    1.5
  262.      */
  263.     function current()
  264.     {
  265.         static $current;
  266.  
  267.         // Get the current URL
  268.         if (!isset($current))
  269.         {
  270.             $uri     JURI::getInstance();
  271.             $current $uri->toStringarray('scheme''host''port''path'));
  272.         }
  273.  
  274.         return $current;
  275.     }
  276.  
  277.     /**
  278.      * Parse a given URI and populate the class fields
  279.      *
  280.      * @access    public
  281.      * @param    string $uri The URI string to parse
  282.      * @return    boolean True on success
  283.      * @since    1.5
  284.      */
  285.     function parse($uri)
  286.     {
  287.         //Initialize variables
  288.         $retval false;
  289.  
  290.         // Set the original URI to fall back on
  291.         $this->_uri $uri;
  292.  
  293.         /*
  294.          * Parse the URI and populate the object fields.  If URI is parsed properly,
  295.          * set method return value to true.
  296.          */
  297.         if ($_parts $this->_parseURL($uri)) {
  298.             $retval true;
  299.         }
  300.  
  301.         $this->_scheme = isset ($_parts['scheme']$_parts['scheme'null;
  302.         $this->_user = isset ($_parts['user']$_parts['user'null;
  303.         $this->_pass = isset ($_parts['pass']$_parts['pass'null;
  304.         $this->_host = isset ($_parts['host']$_parts['host'null;
  305.         $this->_port = isset ($_parts['port']$_parts['port'null;
  306.         $this->_path = isset ($_parts['path']$_parts['path'null;
  307.         $this->_query = isset ($_parts['query'])$_parts['query'null;
  308.         $this->_fragment = isset ($_parts['fragment']$_parts['fragment'null;
  309.  
  310.         //parse the query
  311.         if(isset ($_parts['query'])) parse_str($_parts['query']$this->_vars);
  312.  
  313.         return $retval;
  314.     }
  315.  
  316.     /**
  317.      * Returns full uri string
  318.      *
  319.      * @access    public
  320.      * @param    array $parts An array specifying the parts to render
  321.      * @return    string The rendered URI string
  322.      * @since    1.5
  323.      */
  324.     function toString($parts array('scheme''user''pass''host''port''path''query''fragment'))
  325.     {
  326.         $query $this->getQuery()//make sure the query is created
  327.  
  328.         $uri '';
  329.         $uri .= in_array('scheme'$parts)  (!empty($this->_scheme$this->_scheme.'://' '''';
  330.         $uri .= in_array('user'$parts)    $this->_user '';
  331.         $uri .= in_array('pass'$parts)    (!empty ($this->_pass':' ''.$this->_pass(!empty ($this->_user'@' '''';
  332.         $uri .= in_array('host'$parts)    $this->_host '';
  333.         $uri .= in_array('port'$parts)    (!empty ($this->_port':' '').$this->_port '';
  334.         $uri .= in_array('path'$parts)    $this->_path '';
  335.         $uri .= in_array('query'$parts)    (!empty ($query'?'.$query '''';
  336.         $uri .= in_array('fragment'$parts)(!empty ($this->_fragment'#'.$this->_fragment '''';
  337.  
  338.         return $uri;
  339.     }
  340.  
  341.     /**
  342.      * Adds a query variable and value, replacing the value if it
  343.      * already exists and returning the old value.
  344.      *
  345.      * @access    public
  346.      * @param    string $name Name of the query variable to set
  347.      * @param    string $value Value of the query variable
  348.      * @return    string Previous value for the query variable
  349.      * @since    1.5
  350.      */
  351.     function setVar($name$value)
  352.     {
  353.         $tmp @$this->_vars[$name];
  354.         $this->_vars[$name$value;
  355.  
  356.         //empty the query
  357.         $this->_query null;
  358.  
  359.         return $tmp;
  360.     }
  361.  
  362.     /**
  363.      * Returns a query variable by name
  364.      *
  365.      * @access    public
  366.      * @param    string $name Name of the query variable to get
  367.      * @return    array Query variables
  368.      * @since    1.5
  369.      */
  370.     function getVar($name null$default=null)
  371.     {
  372.         if(isset($this->_vars[$name])) {
  373.             return $this->_vars[$name];
  374.         }
  375.         return $default;
  376.     }
  377.  
  378.     /**
  379.      * Removes an item from the query string variables if it exists
  380.      *
  381.      * @access    public
  382.      * @param    string $name Name of variable to remove
  383.      * @since    1.5
  384.      */
  385.     function delVar($name)
  386.     {
  387.         if (in_array($namearray_keys($this->_vars)))
  388.         {
  389.             unset ($this->_vars[$name]);
  390.  
  391.             //empty the query
  392.             $this->_query null;
  393.         }
  394.     }
  395.  
  396.     /**
  397.      * Sets the query to a supplied string in format:
  398.      *         foo=bar&x=y
  399.      *
  400.      * @access    public
  401.      * @param    mixed (array|string) $query The query string
  402.      * @since    1.5
  403.      */
  404.     function setQuery($query)
  405.     {
  406.         if(!is_array($query)) {
  407.             parse_str($query$this->_vars);
  408.         }
  409.  
  410.         if(is_array($query)) {
  411.             $this->_vars $query;
  412.         }
  413.  
  414.         //empty the query
  415.         $this->_query null;
  416.     }
  417.  
  418.     /**
  419.      * Returns flat query string
  420.      *
  421.      * @access    public
  422.      * @return    string Query string
  423.      * @since    1.5
  424.      */
  425.     function getQuery($toArray false)
  426.     {
  427.         if($toArray{
  428.             return $this->_vars;
  429.         }
  430.  
  431.         //If the query is empty build it first
  432.         if(is_null($this->_query)) {
  433.             $this->_query $this->buildQuery($this->_vars);
  434.         }
  435.  
  436.         return $this->_query;
  437.     }
  438.  
  439.     /**
  440.      * Build a query from a array (reverse of the PHP parse_str())
  441.      *
  442.      * @access    public
  443.      * @return    string The resulting query string
  444.      * @since    1.5
  445.      * @see    parse_str()
  446.      */
  447.     function buildQuery ($params$akey null)
  448.     {
  449.         if !is_array($params|| count($params== {
  450.             return false;
  451.         }
  452.  
  453.         $out array();
  454.  
  455.         //reset in case we are looping
  456.         if!isset($akey&& !count($out) )  {
  457.             unset($out);
  458.             $out array();
  459.         }
  460.  
  461.         foreach $params as $key => $val )
  462.         {
  463.             if is_array($val) ) {
  464.                 $out[JURI::buildQuery($val,$key);
  465.                 continue;
  466.             }
  467.  
  468.           &nb