home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / URL.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  11.4 KB  |  362 lines

  1. <?php
  2. // +-----------------------------------------------------------------------+
  3. // | Copyright (c) 2002-2004, Richard Heyes                                |
  4. // | All rights reserved.                                                  |
  5. // |                                                                       |
  6. // | Redistribution and use in source and binary forms, with or without    |
  7. // | modification, are permitted provided that the following conditions    |
  8. // | are met:                                                              |
  9. // |                                                                       |
  10. // | o Redistributions of source code must retain the above copyright      |
  11. // |   notice, this list of conditions and the following disclaimer.       |
  12. // | o Redistributions in binary form must reproduce the above copyright   |
  13. // |   notice, this list of conditions and the following disclaimer in the |
  14. // |   documentation and/or other materials provided with the distribution.| 
  15. // | o The names of the authors may not be used to endorse or promote      |
  16. // |   products derived from this software without specific prior written  |
  17. // |   permission.                                                         |
  18. // |                                                                       |
  19. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
  20. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
  21. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
  22. // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
  23. // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
  24. // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
  25. // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  26. // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  27. // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
  28. // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
  29. // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
  30. // |                                                                       |
  31. // +-----------------------------------------------------------------------+
  32. // | Author: Richard Heyes <richard@php.net>                               |
  33. // +-----------------------------------------------------------------------+
  34. // $Id: URL.php,v 1.31 2004/01/17 15:52:50 pajoye Exp $
  35. // 
  36. // Net_URL Class
  37.  
  38. class Net_URL
  39. {
  40.     /**
  41.     * Full url
  42.     * @var string
  43.     */
  44.     var $url;
  45.     
  46.     /**
  47.     * Protocol
  48.     * @var string
  49.     */
  50.     var $protocol;
  51.  
  52.     /**
  53.     * Username
  54.     * @var string
  55.     */
  56.     var $username;
  57.  
  58.     /**
  59.     * Password
  60.     * @var string
  61.     */
  62.     var $password;
  63.  
  64.     /**
  65.     * Host
  66.     * @var string
  67.     */
  68.     var $host;
  69.     
  70.     /**
  71.     * Port
  72.     * @var integer
  73.     */
  74.     var $port;
  75.     
  76.     /**
  77.     * Path
  78.     * @var string
  79.     */
  80.     var $path;
  81.     
  82.     /**
  83.     * Query string
  84.     * @var array
  85.     */
  86.     var $querystring;
  87.  
  88.     /**
  89.     * Anchor
  90.     * @var string
  91.     */
  92.     var $anchor;
  93.  
  94.     /**
  95.     * Whether to use []
  96.     * @var bool
  97.     */
  98.     var $useBrackets;
  99.  
  100.     /**
  101.     * Constructor
  102.     *
  103.     * Parses the given url and stores the various parts
  104.     * Defaults are used in certain cases
  105.     *
  106.     * @param string $url         Optional URL
  107.     * @param bool   $useBrackets Whether to use square brackets when
  108.     *                            multiple querystrings with the same name
  109.     *                            exist
  110.     */
  111.     function Net_URL($url = null, $useBrackets = true)
  112.     {
  113.         global $HTTP_SERVER_VARS;
  114.  
  115.         $this->useBrackets = $useBrackets;
  116.         $this->url         = $url;
  117.         $this->user        = '';
  118.         $this->pass        = '';
  119.         $this->host        = '';
  120.         $this->port        = 80;
  121.         $this->path        = '';
  122.         $this->querystring = array();
  123.         $this->anchor      = '';
  124.  
  125.         // Only use defaults if not an absolute URL given
  126.         if (!preg_match('/^[a-z0-9]+:\/\//i', $url)) {
  127.             /**
  128.             * Figure out host/port
  129.             */
  130.             if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) AND preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches)) {
  131.                 $host = $matches[1];
  132.                 if (!empty($matches[3])) {
  133.                     $port = $matches[3];
  134.                 } else {
  135.                     $port = '80';
  136.                 }
  137.             }
  138.     
  139.             $this->protocol    = 'http' . (@$HTTP_SERVER_VARS['HTTPS'] == 'on' ? 's' : '');
  140.             $this->user        = '';
  141.             $this->pass        = '';
  142.             $this->host        = !empty($host) ? $host : (isset($HTTP_SERVER_VARS['SERVER_NAME']) ? $HTTP_SERVER_VARS['SERVER_NAME'] : 'localhost');
  143.             $this->port        = !empty($port) ? $port : (isset($HTTP_SERVER_VARS['SERVER_PORT']) ? $HTTP_SERVER_VARS['SERVER_PORT'] : 80);
  144.             $this->path        = !empty($HTTP_SERVER_VARS['PHP_SELF']) ? $HTTP_SERVER_VARS['PHP_SELF'] : '/';
  145.             $this->querystring = isset($HTTP_SERVER_VARS['QUERY_STRING']) ? $this->_parseRawQuerystring($HTTP_SERVER_VARS['QUERY_STRING']) : null;
  146.             $this->anchor      = '';
  147.         }
  148.  
  149.         // Parse the url and store the various parts
  150.         if (!empty($url)) {
  151.             $urlinfo = parse_url($url);
  152.  
  153.             // Default querystring
  154.             $this->querystring = array();
  155.     
  156.             foreach ($urlinfo as $key => $value) {
  157.                 switch ($key) {
  158.                     case 'scheme':
  159.                         $this->protocol = $value;
  160.                         break;
  161.                     
  162.                     case 'user':
  163.                     case 'pass':
  164.                     case 'host':
  165.                     case 'port':
  166.                         $this->$key = $value;
  167.                         break;
  168.  
  169.                     case 'path':
  170.                         if ($value{0} == '/') {
  171.                             $this->path = $value;
  172.                         } else {
  173.                             $path = dirname($this->path) == DIRECTORY_SEPARATOR ? '' : dirname($this->path);
  174.                             $this->path = sprintf('%s/%s', $path, $value);
  175.                         }
  176.                         break;
  177.                     
  178.                     case 'query':
  179.                         $this->querystring = $this->_parseRawQueryString($value);
  180.                         break;
  181.  
  182.                     case 'fragment':
  183.                         $this->anchor = $value;
  184.                         break;
  185.                 }
  186.             }
  187.         }
  188.     }
  189.  
  190.     /**
  191.     * Returns full url
  192.     *
  193.     * @return string Full url
  194.     * @access public
  195.     */
  196.     function getURL()
  197.     {
  198.         $querystring = $this->getQueryString();
  199.  
  200.         $this->url = $this->protocol . '://'
  201.                    . $this->user . (!empty($this->pass) ? ':' : '')
  202.                    . $this->pass . (!empty($this->user) ? '@' : '')
  203.                    . $this->host . ($this->port == '80' ? '' : ':' . $this->port)
  204.                    . $this->path
  205.                    . (!empty($querystring) ? '?' . $querystring : '')
  206.                    . (!empty($this->anchor) ? '#' . $this->anchor : '');
  207.  
  208.         return $this->url;
  209.     }
  210.  
  211.     /**
  212.     * Adds a querystring item
  213.     *
  214.     * @param  string $name       Name of item
  215.     * @param  string $value      Value of item
  216.     * @param  bool   $preencoded Whether value is urlencoded or not, default = not
  217.     * @access public
  218.     */
  219.     function addQueryString($name, $value, $preencoded = false)
  220.     {
  221.         if ($preencoded) {
  222.             $this->querystring[$name] = $value;
  223.         } else {
  224.             $this->querystring[$name] = is_array($value)? array_map('rawurlencode', $value): rawurlencode($value);
  225.         }
  226.     }    
  227.  
  228.     /**
  229.     * Removes a querystring item
  230.     *
  231.     * @param  string $name Name of item
  232.     * @access public
  233.     */
  234.     function removeQueryString($name)
  235.     {
  236.         if (isset($this->querystring[$name])) {
  237.             unset($this->querystring[$name]);
  238.         }
  239.     }    
  240.     
  241.     /**
  242.     * Sets the querystring to literally what you supply
  243.     *
  244.     * @param  string $querystring The querystring data. Should be of the format foo=bar&x=y etc
  245.     * @access public
  246.     */
  247.     function addRawQueryString($querystring)
  248.     {
  249.         $this->querystring = $this->_parseRawQueryString($querystring);
  250.     }
  251.     
  252.     /**
  253.     * Returns flat querystring
  254.     *
  255.     * @return string Querystring
  256.     * @access public
  257.     */
  258.     function getQueryString()
  259.     {
  260.         if (!empty($this->querystring)) {
  261.             foreach ($this->querystring as $name => $value) {
  262.                 if (is_array($value)) {
  263.                     foreach ($value as $k => $v) {
  264.                         $querystring[] = $this->useBrackets ? sprintf('%s[%s]=%s', $name, $k, $v) : ($name . '=' . $v);
  265.                     }
  266.                 } elseif (!is_null($value)) {
  267.                     $querystring[] = $name . '=' . $value;
  268.                 } else {
  269.                     $querystring[] = $name;
  270.                 }
  271.             }
  272.             $querystring = implode('&', $querystring);
  273.         } else {
  274.             $querystring = '';
  275.         }
  276.  
  277.         return $querystring;
  278.     }
  279.  
  280.     /**
  281.     * Parses raw querystring and returns an array of it
  282.     *
  283.     * @param  string  $querystring The querystring to parse
  284.     * @return array                An array of the querystring data
  285.     * @access private
  286.     */
  287.     function _parseRawQuerystring($querystring)
  288.     {
  289.         $parts  = preg_split('/&/', $querystring, -1, PREG_SPLIT_NO_EMPTY);
  290.         $return = array();
  291.         
  292.         foreach ($parts as $part) {
  293.             if (strpos($part, '=') !== false) {
  294.                 $value = substr($part, strpos($part, '=') + 1);
  295.                 $key   = substr($part, 0, strpos($part, '='));
  296.             } else {
  297.                 $value = null;
  298.                 $key   = $part;
  299.             }
  300.             if (substr($key, -2) == '[]') {
  301.                 $key = substr($key, 0, -2);
  302.                 if (@!is_array($return[$key])) {
  303.                     $return[$key]   = array();
  304.                     $return[$key][] = $value;
  305.                 } else {
  306.                     $return[$key][] = $value;
  307.                 }
  308.             } elseif (!$this->useBrackets AND !empty($return[$key])) {
  309.                 $return[$key]   = (array)$return[$key];
  310.                 $return[$key][] = $value;
  311.             } else {
  312.                 $return[$key] = $value;
  313.             }
  314.         }
  315.  
  316.         return $return;
  317.     }
  318.     
  319.     /**
  320.     * Resolves //, ../ and ./ from a path and returns
  321.     * the result. Eg:
  322.     *
  323.     * /foo/bar/../boo.php    => /foo/boo.php
  324.     * /foo/bar/../../boo.php => /boo.php
  325.     * /foo/bar/.././/boo.php => /foo/boo.php
  326.     *
  327.     * This method can also be called statically.
  328.     *
  329.     * @param  string $url URL path to resolve
  330.     * @return string      The result
  331.     */
  332.     function resolvePath($path)
  333.     {
  334.         $path = explode('/', str_replace('//', '/', $path));
  335.         
  336.         for ($i=0; $i<count($path); $i++) {
  337.             if ($path[$i] == '.') {
  338.                 unset($path[$i]);
  339.                 $path = array_values($path);
  340.                 $i--;
  341.  
  342.             } elseif ($path[$i] == '..' AND ($i > 1 OR ($i == 1 AND $path[0] != '') ) ) {
  343.                 unset($path[$i]);
  344.                 unset($path[$i-1]);
  345.                 $path = array_values($path);
  346.                 $i -= 2;
  347.  
  348.             } elseif ($path[$i] == '..' AND $i == 1 AND $path[0] == '') {
  349.                 unset($path[$i]);
  350.                 $path = array_values($path);
  351.                 $i--;
  352.  
  353.             } else {
  354.                 continue;
  355.             }
  356.         }
  357.  
  358.         return implode('/', $path);
  359.     }
  360. }
  361. ?>
  362.