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 
00018     class Url extends GenericUri
00019     {
00020         protected $knownSubSchemes  = array(
00021             'http'      => 'HttpUrl',
00022             'https'     => 'HttpUrl',
00023             'ftp'       => 'Url',
00024             'nntp'      => 'Url',
00025             'telnet'    => 'Url',
00026             'gopher'    => 'Url',
00027             'wais'      => 'Url',
00028             'file'      => 'Url',
00029             'prospero'  => 'Url'
00030         );
00031         
00035         public static function create()
00036         {
00037             return new self;
00038         }
00039         
00040         public function getKnownSubSchemes()
00041         {
00042             return $this->knownSubSchemes;
00043         }
00044         
00045         public function isValid()
00046         {
00047             if (!parent::isValid())
00048                 return false;
00049             
00050             return
00051                 ($this->isAbsolute() && $this->getAuthority() !== null)
00052                 || ($this->isRelative() && $this->getAuthority() === null);
00053         }
00054         
00060         public function fixAuthorityFromPath()
00061         {
00062             if ($this->scheme && !$this->getAuthority()) {
00063                 $segments = explode('/', $this->path);
00064                 
00065                 while ($segments && empty($segments[0]))
00066                     array_shift($segments);
00067                 
00068                 if ($segments) {
00069                     $this->setAuthority(array_shift($segments));
00070                     $this->setPath('/'.implode('/', $segments));
00071                 }
00072             }
00073             
00074             return $this;
00075         }
00076         
00080         public function fixMistakenPath()
00081         {
00082             if ($this->scheme || $this->getAuthority())
00083                 return $this;
00084             
00085             $urlSubSchemes = Url::create()->getKnownSubSchemes();
00086             
00087             $matches = array();
00088             
00089             if (
00090                 !preg_match('/^([a-z][a-z0-9.+-]*):(.*)/i', $this->path, $matches)
00091                 || !isset($urlSubSchemes[strtolower($matches[1])])
00092             ) {
00093                 // localhost:80 not a scheme+authority
00094                 return $this;
00095             }
00096             
00097             // but http:anything:80/... and http:/anything:80/.. becomes
00098             // http://anything:80/...
00099             
00100             $this->setScheme($matches[1]);
00101             $this->setPath($matches[2]);
00102             
00103             $this->fixAuthorityFromPath();
00104             
00105             return $this;
00106         }
00107         
00108         public function toSmallString()
00109         {
00110             $result = null;
00111             
00112             $authority = $this->getAuthority();
00113             
00114             if ($authority !== null)
00115                 $result .= $authority;
00116             
00117             $result .= $this->path;
00118             
00119             if ($this->query !== null)
00120                 $result .= '?'.$this->query;
00121             
00122             if ($this->fragment !== null)
00123                 $result .= '#'.$this->fragment;
00124             
00125             return $result;
00126         }
00127         
00128         public function normalize()
00129         {
00130             parent::normalize();
00131             
00132             if ($this->getPort() === '')
00133                 $this->setPort(null);
00134             
00135             return $this;
00136         }
00137     }
00138 ?>