1 | <?php |
---|
2 | /** |
---|
3 | * Zend Framework |
---|
4 | * |
---|
5 | * LICENSE |
---|
6 | * |
---|
7 | * This source file is subject to the new BSD license that is bundled |
---|
8 | * with this package in the file LICENSE.txt. |
---|
9 | * It is also available through the world-wide-web at this URL: |
---|
10 | * http://framework.zend.com/license/new-bsd |
---|
11 | * If you did not receive a copy of the license and are unable to |
---|
12 | * obtain it through the world-wide-web, please send an email |
---|
13 | * to license@zend.com so we can send you a copy immediately. |
---|
14 | * |
---|
15 | * @category Zend |
---|
16 | * @package Zend_Uri |
---|
17 | * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
---|
18 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
19 | * @version $Id: Uri.php 24594 2012-01-05 21:27:01Z matthew $ |
---|
20 | */ |
---|
21 | |
---|
22 | /** |
---|
23 | * Abstract class for all Zend_Uri handlers |
---|
24 | * |
---|
25 | * @category Zend |
---|
26 | * @package Zend_Uri |
---|
27 | * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
---|
28 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
29 | */ |
---|
30 | abstract class Zend_Uri |
---|
31 | { |
---|
32 | /** |
---|
33 | * Scheme of this URI (http, ftp, etc.) |
---|
34 | * |
---|
35 | * @var string |
---|
36 | */ |
---|
37 | protected $_scheme = ''; |
---|
38 | |
---|
39 | /** |
---|
40 | * Global configuration array |
---|
41 | * |
---|
42 | * @var array |
---|
43 | */ |
---|
44 | static protected $_config = array( |
---|
45 | 'allow_unwise' => false |
---|
46 | ); |
---|
47 | |
---|
48 | /** |
---|
49 | * Return a string representation of this URI. |
---|
50 | * |
---|
51 | * @see getUri() |
---|
52 | * @return string |
---|
53 | */ |
---|
54 | public function __toString() |
---|
55 | { |
---|
56 | try { |
---|
57 | return $this->getUri(); |
---|
58 | } catch (Exception $e) { |
---|
59 | trigger_error($e->getMessage(), E_USER_WARNING); |
---|
60 | return ''; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Convenience function, checks that a $uri string is well-formed |
---|
66 | * by validating it but not returning an object. Returns TRUE if |
---|
67 | * $uri is a well-formed URI, or FALSE otherwise. |
---|
68 | * |
---|
69 | * @param string $uri The URI to check |
---|
70 | * @return boolean |
---|
71 | */ |
---|
72 | public static function check($uri) |
---|
73 | { |
---|
74 | try { |
---|
75 | $uri = self::factory($uri); |
---|
76 | } catch (Exception $e) { |
---|
77 | return false; |
---|
78 | } |
---|
79 | |
---|
80 | return $uri->valid(); |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Create a new Zend_Uri object for a URI. If building a new URI, then $uri should contain |
---|
85 | * only the scheme (http, ftp, etc). Otherwise, supply $uri with the complete URI. |
---|
86 | * |
---|
87 | * @param string $uri The URI form which a Zend_Uri instance is created |
---|
88 | * @param string $className The name of the class to use in order to manipulate URI |
---|
89 | * @throws Zend_Uri_Exception When an empty string was supplied for the scheme |
---|
90 | * @throws Zend_Uri_Exception When an illegal scheme is supplied |
---|
91 | * @throws Zend_Uri_Exception When the scheme is not supported |
---|
92 | * @throws Zend_Uri_Exception When $className doesn't exist or doesn't implements Zend_Uri |
---|
93 | * @return Zend_Uri |
---|
94 | * @link http://www.faqs.org/rfcs/rfc2396.html |
---|
95 | */ |
---|
96 | public static function factory($uri = 'http', $className = null) |
---|
97 | { |
---|
98 | // Separate the scheme from the scheme-specific parts |
---|
99 | $uri = explode(':', $uri, 2); |
---|
100 | $scheme = strtolower($uri[0]); |
---|
101 | $schemeSpecific = isset($uri[1]) === true ? $uri[1] : ''; |
---|
102 | |
---|
103 | if (strlen($scheme) === 0) { |
---|
104 | require_once 'Zend/Uri/Exception.php'; |
---|
105 | throw new Zend_Uri_Exception('An empty string was supplied for the scheme'); |
---|
106 | } |
---|
107 | |
---|
108 | // Security check: $scheme is used to load a class file, so only alphanumerics are allowed. |
---|
109 | if (ctype_alnum($scheme) === false) { |
---|
110 | require_once 'Zend/Uri/Exception.php'; |
---|
111 | throw new Zend_Uri_Exception('Illegal scheme supplied, only alphanumeric characters are permitted'); |
---|
112 | } |
---|
113 | |
---|
114 | if ($className === null) { |
---|
115 | /** |
---|
116 | * Create a new Zend_Uri object for the $uri. If a subclass of Zend_Uri exists for the |
---|
117 | * scheme, return an instance of that class. Otherwise, a Zend_Uri_Exception is thrown. |
---|
118 | */ |
---|
119 | switch ($scheme) { |
---|
120 | case 'http': |
---|
121 | // Break intentionally omitted |
---|
122 | case 'https': |
---|
123 | $className = 'Zend_Uri_Http'; |
---|
124 | break; |
---|
125 | |
---|
126 | case 'mailto': |
---|
127 | // TODO |
---|
128 | default: |
---|
129 | require_once 'Zend/Uri/Exception.php'; |
---|
130 | throw new Zend_Uri_Exception("Scheme \"$scheme\" is not supported"); |
---|
131 | break; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | require_once 'Zend/Loader.php'; |
---|
136 | try { |
---|
137 | Zend_Loader::loadClass($className); |
---|
138 | } catch (Exception $e) { |
---|
139 | require_once 'Zend/Uri/Exception.php'; |
---|
140 | throw new Zend_Uri_Exception("\"$className\" not found"); |
---|
141 | } |
---|
142 | |
---|
143 | $schemeHandler = new $className($scheme, $schemeSpecific); |
---|
144 | |
---|
145 | if (! $schemeHandler instanceof Zend_Uri) { |
---|
146 | require_once 'Zend/Uri/Exception.php'; |
---|
147 | throw new Zend_Uri_Exception("\"$className\" is not an instance of Zend_Uri"); |
---|
148 | } |
---|
149 | |
---|
150 | return $schemeHandler; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Get the URI's scheme |
---|
155 | * |
---|
156 | * @return string|false Scheme or false if no scheme is set. |
---|
157 | */ |
---|
158 | public function getScheme() |
---|
159 | { |
---|
160 | if (empty($this->_scheme) === false) { |
---|
161 | return $this->_scheme; |
---|
162 | } else { |
---|
163 | return false; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * Set global configuration options |
---|
169 | * |
---|
170 | * @param Zend_Config|array $config |
---|
171 | */ |
---|
172 | static public function setConfig($config) |
---|
173 | { |
---|
174 | if ($config instanceof Zend_Config) { |
---|
175 | $config = $config->toArray(); |
---|
176 | } elseif (!is_array($config)) { |
---|
177 | throw new Zend_Uri_Exception("Config must be an array or an instance of Zend_Config."); |
---|
178 | } |
---|
179 | |
---|
180 | foreach ($config as $k => $v) { |
---|
181 | self::$_config[$k] = $v; |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * Zend_Uri and its subclasses cannot be instantiated directly. |
---|
187 | * Use Zend_Uri::factory() to return a new Zend_Uri object. |
---|
188 | * |
---|
189 | * @param string $scheme The scheme of the URI |
---|
190 | * @param string $schemeSpecific The scheme-specific part of the URI |
---|
191 | */ |
---|
192 | abstract protected function __construct($scheme, $schemeSpecific = ''); |
---|
193 | |
---|
194 | /** |
---|
195 | * Return a string representation of this URI. |
---|
196 | * |
---|
197 | * @return string |
---|
198 | */ |
---|
199 | abstract public function getUri(); |
---|
200 | |
---|
201 | /** |
---|
202 | * Returns TRUE if this URI is valid, or FALSE otherwise. |
---|
203 | * |
---|
204 | * @return boolean |
---|
205 | */ |
---|
206 | abstract public function valid(); |
---|
207 | } |
---|