Url.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Ivan Y. Khvostishkov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: Url.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00019     class Url extends GenericUri
00020     {
00021         protected $knownSubSchemes  = array(
00022             'http'      => 'HttpUrl',
00023             'https'     => 'HttpUrl',
00024             'ftp'       => 'Url',
00025             'nntp'      => 'Url',
00026             'telnet'    => 'Url',
00027             'gopher'    => 'Url',
00028             'wais'      => 'Url',
00029             'file'      => 'Url',
00030             'prospero'  => 'Url'
00031         );
00032         
00036         public static function create()
00037         {
00038             return new self;
00039         }
00040         
00041         public function getKnownSubSchemes()
00042         {
00043             return $this->knownSubSchemes;
00044         }
00045         
00046         public function isValid()
00047         {
00048             if (!parent::isValid())
00049                 return false;
00050             
00051             return
00052                 ($this->isAbsolute() && $this->getAuthority() !== null)
00053                 || ($this->isRelative() && $this->getAuthority() === null);
00054         }
00055         
00061         public function fixAuthorityFromPath()
00062         {
00063             if ($this->scheme && !$this->getAuthority()) {
00064                 $segments = explode('/', $this->path);
00065                 
00066                 while ($segments && empty($segments[0]))
00067                     array_shift($segments);
00068                 
00069                 if ($segments) {
00070                     $this->setAuthority(array_shift($segments));
00071                     $this->setPath('/'.implode('/', $segments));
00072                 }
00073             }
00074             
00075             return $this;
00076         }
00077         
00081         public function fixMistakenPath()
00082         {
00083             if ($this->scheme || $this->getAuthority())
00084                 return $this;
00085             
00086             $urlSubSchemes = Url::create()->getKnownSubSchemes();
00087             
00088             $matches = array();
00089             
00090             if (
00091                 !preg_match('/^([a-z][a-z0-9.+-]*):(.*)/i', $this->path, $matches)
00092                 || !isset($urlSubSchemes[strtolower($matches[1])])
00093             ) {
00094                 // localhost:80 not a scheme+authority
00095                 return $this;
00096             }
00097             
00098             // but http:anything:80/... and http:/anything:80/.. becomes
00099             // http://anything:80/...
00100             
00101             $this->setScheme($matches[1]);
00102             $this->setPath($matches[2]);
00103             
00104             $this->fixAuthorityFromPath();
00105             
00106             return $this;
00107         }
00108         
00109         public function toSmallString()
00110         {
00111             $result = null;
00112             
00113             $authority = $this->getAuthority();
00114             
00115             if ($authority !== null)
00116                 $result .= $authority;
00117             
00118             $result .= $this->path;
00119             
00120             if ($this->query !== null)
00121                 $result .= '?'.$this->query;
00122             
00123             if ($this->fragment !== null)
00124                 $result .= '#'.$this->fragment;
00125             
00126             return $result;
00127         }
00128         
00129         public function normalize()
00130         {
00131             parent::normalize();
00132             
00133             if ($this->getPort() === '')
00134                 $this->setPort(null);
00135             
00136             return $this;
00137         }
00138     }
00139 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4