[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/net/ -> url.class.php (source)

   1  <?php
   2  
   3      
   4  
   5      /**
   6       * \ingroup Net
   7       *
   8       * Encapsulates a definition of an object representing a URL
   9       *
  10       * Provides getters and setters for all the parts of the url:
  11       * <ul>
  12       * <li>url (the complete url)</li>
  13       * <li>scheme (http, file, ftp)</li>
  14       * <li>host</li>
  15       * <li>user</li>
  16       * <li>password</li>
  17       * <li>path</li>
  18       * <li>query (anything after the question mark "?")</li>
  19       * <li>fragment (anything after the hash mark "#")</li>
  20       * </ul>
  21       * Every time a change is made in one of the fields the
  22       * url string is recalculated so that any call to getUrl
  23       * will return the right one.
  24       */
  25      class Url  
  26      {
  27  
  28          var $_url;
  29          var $_scheme;
  30          var $_host;
  31          var $_port;
  32          var $_user;
  33          var $_pass;
  34          var $_path;
  35          var $_query;
  36          var $_fragment;
  37  
  38          /**
  39           * given a string representing a valid URL, build the object. If the string is not a valid
  40           * URL, the constructor will not generate an error but the results of calling any of the getter
  41           * methods are undefined
  42           *
  43           * @param url A string with a valid URL
  44           */
  45  		function Url( $url )
  46          {
  47              $this->_url = $url;
  48  
  49              $this->_calculateFields();
  50          }
  51  
  52          /**
  53           * @private
  54           */
  55  		function _calculateFields()
  56          {
  57              $parts = parse_url( $this->_url );
  58  
  59              $keys = Array( "scheme", "host", "port", "user", "pass",
  60                            "path", "query", "fragment" );
  61  
  62              // this saves us time ;)
  63              foreach( $keys as $key ) {
  64                  if (isset($parts[$key])) {
  65                      $var = "_{$key}";
  66                      $this->$var = $parts["$key"];
  67                  }
  68              }
  69          }
  70  
  71          /**
  72           * @return returns the URL as it was given in the constructor
  73           */
  74  		function getUrl()
  75          {
  76              return $this->_url;
  77          }
  78  
  79          /**
  80           * sets a new URL string, which will overwrite the previous one.
  81           *
  82           * @param the new URL string
  83           */
  84  		function setUrl( $url )
  85          {
  86              $this->_url = $url;
  87  
  88              $this->_calculateFields();
  89          }
  90  
  91          /**
  92           * @return returns the scheme of the given url (http, file, ftp, ...)
  93           */
  94  		function getScheme()
  95          {
  96              return $this->_scheme;
  97          }
  98  
  99          /**
 100           * sets a new scheme
 101           *
 102           * @param Scheme The new scheme (http, file, ftp, ...)
 103           */
 104  		function setScheme( $scheme )
 105          {
 106              $this->_scheme = $scheme;
 107  
 108              $this->glueUrl();
 109          }
 110  
 111          /**
 112           * @return Returns the host specified in this URL
 113           */
 114  		function getHost()
 115          {
 116              return $this->_host;
 117          }
 118  
 119          /**
 120           * sets a new host
 121           *
 122           * @param Host the new host
 123           */
 124  		function setHost( $host )
 125          {
 126              $this->_host = $host;
 127  
 128              $this->glueUrl();
 129          }
 130  
 131          /**
 132           * @return Returns the port that was specified in the original URL, or 80 if there was nothing
 133           * specified
 134           */
 135  		function getPort()
 136          {
 137              return $this->_port;
 138          }
 139  
 140          /**
 141           * sets a new port
 142           *
 143           * @param port the new port
 144           */
 145  		function setPort( $port )
 146          {
 147              $this->_port = $port;
 148  
 149              $this->glueUrl();
 150          }
 151  
 152          /**
 153           * @return Returns the user that was specified in the URL, if any.
 154           */
 155  		function getUser()
 156          {
 157              return $this->_user;
 158          }
 159  
 160          /** 
 161           * sets a new user in the URL
 162           *
 163           * @param user The new username
 164           */
 165  		function setUser( $user )
 166          {
 167              $this->_user = $user;
 168  
 169              $this->glueUrl();
 170          }
 171  
 172          /**
 173           * @return Returns the password that was set in the URL
 174           */
 175  		function getPass()
 176          {
 177              return $this->_pass;
 178          }
 179  
 180          /**
 181           * sets a new password in the URL
 182           *
 183           * @param pass the new password
 184           */
 185  		function setPass( $pass )
 186          {
 187              $this->_pass = $pass;
 188  
 189              $this->glueUrl();
 190          }
 191  
 192          /**
 193           * @return Returns the path
 194           */
 195  		function getPath()
 196          {
 197              return $this->_path;
 198          }
 199  
 200          /**
 201           * sets the new path
 202           *
 203           * @param path The new path
 204           */
 205  		function setPath( $path )
 206          {
 207              $this->_path = $path;
 208  
 209              $this->glueUrl();
 210          }
 211  
 212          /**
 213           * @return Returns the query
 214           */
 215  		function getQuery()
 216          {
 217              return $this->_query;
 218          }
 219  
 220          /**
 221           * Returns the query as an array of items
 222           *
 223           * @return An associative array where the keys are the name
 224           * of the parameters and the value is the value assigned to
 225           * the parameter.
 226           */
 227          function getQueryArray()
 228          {
 229              // first, separate all the different parameters
 230              $reqParams = explode( "&", $this->_query );
 231  
 232              $results = Array();
 233              foreach( $reqParams as $param ) {
 234                  // now, for every parameter, get rid of the '='
 235                  $parts = explode( "=", $param );
 236                  $var = $parts[0];
 237                  $value = urldecode($parts[1]);
 238  
 239                  $results[$var] = $value;
 240              }
 241  
 242              return $results;
 243          }
 244  
 245          /** 
 246           * sets a new query
 247           *
 248           * @param query The new query
 249           */
 250  		function setQuery( $query )
 251          {
 252              $this->_query = $query;
 253  
 254              $this->glueUrl();
 255          }
 256  
 257          /**
 258           * @return Returns the fragment
 259           */
 260  		function getFragment()
 261          {
 262              return $this->_fragment;
 263          }
 264  
 265          /**
 266           * Sets a new fragment
 267           *
 268           * @param fragment The new fragment
 269           */
 270  		function setFragment( $fragment )
 271          {
 272              $this->_fragment = $fragment;
 273  
 274              $this->glueUrl();
 275          }
 276  
 277          /**
 278           * Puts all the pieces back in place, and returns the resulting
 279           * url. It is usually not necessary to call this method to obtain the new URL once we've called
 280           * any of the setter methods of this class, since it is done automatically. Doing
 281           *
 282           * <pre>
 283           *  $url->setScheme( "ftp" );
 284           *  print("new url = ".$url->getUrl());
 285           * </pre>
 286           *
 287           * is enough to obtain the updated URL string.
 288           *
 289           * Extracted from http://www.php.net/manual/en/function.parse-url.php
 290           *
 291           * @return a valid URL generated from the different parts of the object
 292           */
 293  		function glueUrl()
 294          {
 295               $uri = $this->_scheme ? $this->_scheme.':'.((strtolower($this->_scheme) == 'mailto') ? '':'//'): '';
 296               $uri .= $this->_user ? $this->_user.($this->_pass? ':'.$this->_pass:'').'@':'';
 297               $uri .= $this->_host ? $this->_host : '';
 298               $uri .= $this->_port ? ':'.$this->_port : '';
 299               $uri .= $this->_path ? $this->_path : '';
 300               $uri .= $this->_query ? '?'.$this->_query : '';
 301               $uri .= $this->_fragment ? '#'.$this->_fragment : '';
 302  
 303              $this->_url = $uri;
 304  
 305              return $uri;
 306          }
 307      }
 308  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics