[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/net/src/ -> lmbUri.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/core/src/lmbArrayHelper.class.php');
  10  
  11  /**

  12   * class lmbUri.

  13   *

  14   * @package net

  15   * @version $Id: lmbUri.class.php 5945 2007-06-06 08:31:43Z pachanga $

  16   */
  17  class lmbUri
  18  {
  19    protected $_protocol = '';
  20    protected $_user = '';
  21    protected $_password = '';
  22    protected $_host = '';
  23    protected $_port = '';
  24    protected $_path = '';
  25    protected $_anchor = '';
  26    protected $_query_items = array();
  27    protected $_path_elements = array();
  28  
  29    function __construct($str='')
  30    {
  31      if($str)
  32        $this->reset($str);
  33    }
  34  
  35    static function addQueryItems($url, $items=array())
  36    {
  37      $str_params = '';
  38  
  39      if(strpos($url, '?') === false)
  40        $url .= '?';
  41      else
  42        $url .= '&';
  43  
  44      $str_params_arr = array();
  45      foreach($items as $key => $val)
  46      {
  47        $url = preg_replace("/&*{$key}=[^&]*/", '', $url);
  48        $str_params_arr[] = "$key=$val";
  49      }
  50  
  51      $items = explode('#', $url);
  52  
  53      $url = $items[0];
  54      $fragment = isset($items[1]) ? '#' . $items[1] : '';
  55  
  56      return $url . implode('&', $str_params_arr) . $fragment;
  57    }
  58  
  59    /**

  60     * @deprecated

  61     */
  62    function parse($uri)
  63    {
  64      $this->reset($uri);
  65    }
  66  
  67    function reset($str = null)
  68    {
  69      $this->_user        = '';
  70      $this->_password    = '';
  71      $this->_host        = '';
  72      $this->_port        = '';
  73      $this->_path        = '';
  74      $this->_query_items = array();
  75      $this->_anchor      = '';
  76      $this->_path_elements = array();
  77  
  78      if(!$str)
  79        return;
  80  
  81      if(!$urlinfo = @parse_url($str))
  82        throw new lmbException("URI '$str' is not valid");
  83  
  84      foreach($urlinfo as $key => $value)
  85      {
  86        switch($key)
  87        {
  88          case 'scheme':
  89            $this->setProtocol($value);
  90          break;
  91  
  92          case 'user':
  93            $this->setUser($value);
  94          break;
  95  
  96          case 'host':
  97            $this->setHost($value);
  98          break;
  99  
 100          case 'port':
 101            $this->setPort($value);
 102          break;
 103  
 104          case 'pass':
 105            $this->setPassword($value);
 106          break;
 107  
 108          case 'path':
 109            $this->setPath($value);
 110          break;
 111  
 112          case 'query':
 113            $this->setQueryString($value);
 114          break;
 115  
 116          case 'fragment':
 117            $this->setAnchor($value);
 118          break;
 119        }
 120      }
 121    }
 122  
 123    function getProtocol()
 124    {
 125      return $this->_protocol;
 126    }
 127  
 128    function getUser()
 129    {
 130      return $this->_user;
 131    }
 132  
 133    function getPassword()
 134    {
 135      return $this->_password;
 136    }
 137  
 138    function getHost()
 139    {
 140      return $this->_host;
 141    }
 142  
 143    function getPort()
 144    {
 145      return $this->_port;
 146    }
 147  
 148    function getPath()
 149    {
 150      return $this->_path;
 151    }
 152  
 153    function getAnchor()
 154    {
 155      return $this->_anchor;
 156    }
 157  
 158    function setProtocol($protocol)
 159    {
 160      $this->_protocol = $protocol;
 161    }
 162  
 163    function setUser($user)
 164    {
 165      $this->_user = $user;
 166    }
 167  
 168    function setPassword($password)
 169    {
 170      $this->_password = $password;
 171    }
 172  
 173    function setHost($host)
 174    {
 175      $this->_host = $host;
 176    }
 177  
 178    function setPort($port)
 179    {
 180      $this->_port = $port;
 181    }
 182  
 183    function setPath($path)
 184    {
 185      $this->_path = $path;
 186      $this->_path_elements = explode('/',$this->_path);
 187    }
 188  
 189    function setAnchor($anchor)
 190    {
 191      $this->_anchor = $anchor;
 192    }
 193  
 194    function isAbsolute()
 195    {
 196      if(!strlen($this->_path))
 197        return true;
 198  
 199      return ('/' == $this->_path{0});
 200    }
 201  
 202    function isRelative()
 203    {
 204      return !$this->isAbsolute();
 205    }
 206  
 207    function countPath()
 208    {
 209      return sizeof($this->_path_elements);
 210    }
 211  
 212    function countQueryItems()
 213    {
 214      return sizeof($this->_query_items);
 215    }
 216  
 217    function compare($uri)
 218    {
 219      return (
 220            $this->_protocol == $uri->getProtocol() &&
 221            $this->_host == $uri->getHost() &&
 222            $this->_port == $uri->getPort() &&
 223            $this->_user === $uri->getUser() &&
 224            $this->_password === $uri->getPassword() &&
 225            $this->compareQuery($uri) &&
 226            $this->comparePath($uri) === 0
 227          );
 228    }
 229  
 230    function compareQuery($uri)
 231    {
 232      if ($this->countQueryItems() != $uri->countQueryItems())
 233        return false;
 234  
 235      foreach($this->_query_items as $name => $value)
 236      {
 237        if( (($item = $uri->getQueryItem($name)) === false) ||
 238            $item != $value)
 239          return false;
 240      }
 241      return true;
 242    }
 243  
 244    function comparePath($uri)
 245    {
 246      $count1 = $this->countPath();
 247      $count2 = $uri->countPath();
 248      $iterCount = min($count1, $count2);
 249  
 250      for($i=0; $i < $iterCount; $i++)
 251      {
 252        if( $this->getPathElement($i) != $uri->getPathElement($i) )
 253          return false;
 254      }
 255  
 256      return ($count1 - $count2);
 257    }
 258  
 259    function toString($parts = array('protocol', 'user', 'password', 'host', 'port', 'path', 'query', 'anchor'))
 260    {
 261      $string = '';
 262  
 263      if(in_array('protocol', $parts))
 264        $string .= !empty($this->_protocol) ? $this->_protocol . '://' : '';
 265  
 266      if(in_array('user', $parts))
 267      {
 268        $string .=  $this->_user;
 269  
 270        if(in_array('password', $parts))
 271          $string .= (!empty($this->_password) ? ':' : '') . $this->_password;
 272  
 273        $string .= (!empty($this->_user) ? '@' : '');
 274      }
 275  
 276      if(in_array('host', $parts))
 277      {
 278        $string .= $this->_host;
 279  
 280        if(in_array('port', $parts))
 281          $string .= (empty($this->_port) ||  ($this->_port == '80') ? '' : ':' . $this->_port);
 282      }
 283      else
 284        $string = '';
 285  
 286      if(in_array('path', $parts))
 287        $string .= $this->_path;
 288  
 289      if(in_array('query', $parts))
 290      {
 291        $query_string = $this->getQueryString();
 292        $string .= !empty($query_string) ? '?' . $query_string : '';
 293      }
 294  
 295      if(in_array('anchor', $parts))
 296        $string .= !empty($this->_anchor) ? '#' . $this->_anchor : '';
 297  
 298       return $string;
 299    }
 300  
 301    function getPathElement($level)
 302    {
 303      return isset($this->_path_elements[$level]) ? $this->_path_elements[$level] : '';
 304    }
 305  
 306    function getPathElements()
 307    {
 308      return $this->_path_elements;
 309    }
 310  
 311    function getPathToLevel($level)
 312    {
 313      if(!$this->_path_elements || $level >= sizeof($this->_path_elements))
 314        return '';
 315  
 316      $items = array();
 317      for($i = 0; $i <= $level; $i++)
 318        $items[] = $this->_path_elements[$i];
 319  
 320      return implode('/', $items);
 321    }
 322  
 323    function getPathFromLevel($level)
 324    {
 325      if($level <= 0)
 326        return $this->_path;
 327  
 328      if(!$this->_path_elements || $level >= sizeof($this->_path_elements))
 329        return '/';
 330  
 331      $items[] = '';
 332  
 333      for($i = $level; $i < sizeof($this->_path_elements); $i++)
 334        $items[] = $this->_path_elements[$i];
 335  
 336      return implode('/', $items);
 337    }
 338  
 339  
 340    function addEncodedQueryItem($name, $value)
 341    {
 342      $this->_query_items[$name] = $value;
 343    }
 344  
 345    function addQueryItem($name, $value)
 346    {
 347      $this->_query_items[$name] = is_array($value) ?
 348        lmbArrayHelper :: arrayMapRecursive('urlencode', $value) :
 349        urlencode($value);
 350    }
 351  
 352    function getQueryItem($name)
 353    {
 354      if (isset($this->_query_items[$name]))
 355        return $this->_query_items[$name];
 356  
 357      return false;
 358    }
 359  
 360    function getQueryItems()
 361    {
 362      return $this->_query_items;
 363    }
 364  
 365    function setQueryItems($items)
 366    {
 367      $this->_query_items = $items;
 368    }
 369  
 370    /**

 371    * Removes a query_string item

 372    *

 373    */
 374    function removeQueryItem($name)
 375    {
 376      if (isset($this->_query_items[$name]))
 377        unset($this->_query_items[$name]);
 378    }
 379  
 380    /**

 381    * Sets the query_string to literally what you supply

 382    */
 383    function setQueryString($query_string)
 384    {
 385      $this->_query_items = $this->_parseQueryString($query_string);
 386    }
 387  
 388    /**

 389    * Removes query items

 390    */
 391    function removeQueryItems()
 392    {
 393      $this->_query_items = array();
 394    }
 395  
 396    /**

 397    * Returns flat query_string

 398    *

 399    */
 400    function getQueryString()
 401    {
 402      $query_string = '';
 403      $query_items = array();
 404      $flat_array = array();
 405  
 406      lmbArrayHelper :: toFlatArray($this->_query_items, $flat_array);
 407      ksort($flat_array);
 408      foreach($flat_array as $key => $value)
 409      {
 410        if ($value != '' ||  is_null($value))
 411          $query_items[] = $key . '=' . $value;
 412        else
 413          $query_items[] = $key;
 414      }
 415  
 416      if($query_items)
 417        $query_string = implode('&', $query_items);
 418  
 419      return $query_string;
 420    }
 421  
 422    /**

 423    * Parses raw query_string and returns an array of it

 424    */
 425    protected function _parseQueryString($query_string)
 426    {
 427      parse_str($query_string, $arr);
 428  
 429      foreach($arr as $key => $item)
 430      {
 431        if(!is_array($item))
 432          $arr[$key] = rawurldecode($item);
 433      }
 434  
 435      return $arr;
 436    }
 437  
 438    /**

 439    * Resolves //, ../ and ./ from a path and returns

 440    * the result. Eg:

 441    *

 442    * /foo/bar/../boo.php    => /foo/boo.php

 443    * /foo/bar/../../boo.php => /boo.php

 444    * /foo/bar/.././/boo.php => /foo/boo.php

 445    *

 446    */
 447    function normalizePath()
 448    {
 449      $path = $this->_path;
 450      $path = explode('/', preg_replace('~[\/]+~', '/', $path));
 451  
 452      for ($i=0; $i < sizeof($path); $i++)
 453      {
 454        if ($path[$i] == '.')
 455        {
 456          unset($path[$i]);
 457          $path = array_values($path);
 458          $i--;
 459        }
 460        elseif ($path[$i] == '..' &&  ($i > 1 ||  ($i == 1 &&  $path[0] != '') ) )
 461        {
 462          unset($path[$i]);
 463          unset($path[$i-1]);
 464          $path = array_values($path);
 465          $i -= 2;
 466        }
 467        elseif ($path[$i] == '..' &&  $i == 1 &&  $path[0] == '')
 468        {
 469          unset($path[$i]);
 470          $path = array_values($path);
 471          $i--;
 472        }
 473        else
 474          continue;
 475      }
 476  
 477      $this->_path = implode('/', $path);
 478      $this->_path_elements = explode('/',$this->_path);
 479    }
 480  }
 481  ?>