HttpUrl.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 
00015     final class HttpUrl extends Url
00016     {
00017         protected $knownSubSchemes  = array();
00018         
00022         public static function create()
00023         {
00024             return new self;
00025         }
00026         
00034         public function setHttpHost($host)
00035         {
00036             $parts = explode(':', $host, 2);
00037             
00038             $this->setHost($parts[0]);
00039             
00040             if (isset($parts[1]))
00041                 $this->setPort($parts[1]);
00042             
00043             return $this;
00044         }
00045         
00046         public function ensureAbsolute()
00047         {
00048             $this->fixMistakenPath();
00049             
00050             if (!$this->scheme && !$this->getAuthority()) {
00051                 $this->scheme = 'http';
00052                 
00053                 $segments = explode('/', $this->path);
00054                 
00055                 if (!empty($segments[0])) {
00056                     // localhost/anything becomes http://localhost/anything
00057                     
00058                     $this->setAuthority(array_shift($segments));
00059                     
00060                     $this->setPath('/'.implode('/', $segments));
00061                 }
00062             }
00063             
00064             $this->fixAuthorityFromPath();
00065             
00066             return $this;
00067         }
00068         
00069         public function isValidScheme()
00070         {
00071             if (!parent::isValidScheme())
00072                 return false;
00073             
00074             if (
00075                 $this->scheme
00076                 && !in_array(strtolower($this->scheme), array('http', 'https'))
00077             )
00078                 return false;
00079             
00080             return true;
00081         }
00082         
00083         public function isValidPort()
00084         {
00085             if (!parent::isValidPort())
00086                 return false;
00087             
00088             if (
00089                 $this->port
00090                 && !in_array($this->port, array(80, 443))
00091                 && $this->port < 1024
00092             )
00093                 return false;
00094             
00095             return true;
00096         }
00097         
00098         protected function isValidHostName()
00099         {
00100             if (!parent::isValidHostName())
00101                 return false;
00102             
00103             $charPattern = $this->charPattern(null);
00104             
00105             // using rfc 2396, in order to detect bad ip address ranges like
00106             // 666.666.666.666 which are valid hostnames in generic uri syntax
00107             
00108             $topLabelPattern = '(([a-z])|([a-z]([a-z0-9-])*[a-z0-9]))\.?';
00109             
00110             return (
00111                 preg_match(
00112                     "/^($charPattern*\.)?{$topLabelPattern}$/i",
00113                     $this->host
00114                 ) == 1
00115             );
00116         }
00117         
00118         public function normalize()
00119         {
00120             parent::normalize();
00121             
00122             $port = $this->getPort();
00123             $scheme = $this->getScheme();
00124             
00125             if (
00126                 ($scheme == 'http' && $port == '80')
00127                 || ($scheme == 'https' && $port == '443')
00128             )
00129                 $this->setPort(null);
00130             
00131             if ($this->getPath() === null || $this->getPath() === '')
00132                 $this->setPath('/');
00133             
00134             return $this;
00135         }
00136         
00137         public function makeComparable()
00138         {
00139             return $this->ensureAbsolute()->normalize()->setFragment(null);
00140         }
00141     }
00142 ?>