2 // +-----------------------------------------------------------------------+
\r
3 // | Copyright (c) 2002-2004, Richard Heyes |
\r
4 // | All rights reserved. |
\r
6 // | Redistribution and use in source and binary forms, with or without |
\r
7 // | modification, are permitted provided that the following conditions |
\r
10 // | o Redistributions of source code must retain the above copyright |
\r
11 // | notice, this list of conditions and the following disclaimer. |
\r
12 // | o Redistributions in binary form must reproduce the above copyright |
\r
13 // | notice, this list of conditions and the following disclaimer in the |
\r
14 // | documentation and/or other materials provided with the distribution.|
\r
15 // | o The names of the authors may not be used to endorse or promote |
\r
16 // | products derived from this software without specific prior written |
\r
19 // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
\r
20 // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
\r
21 // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
\r
22 // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
\r
23 // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
\r
24 // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
\r
25 // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
\r
26 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
\r
27 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
\r
28 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
\r
29 // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
\r
31 // +-----------------------------------------------------------------------+
\r
32 // | Author: Richard Heyes <richard at php net> |
\r
33 // +-----------------------------------------------------------------------+
\r
35 // $Id: URL.php,v 1.1 2006/11/27 17:14:39 hsur Exp $
\r
104 * @see __construct()
\r
106 function Net_URL($url = null, $useBrackets = true)
\r
108 $this->__construct($url, $useBrackets);
\r
114 * Parses the given url and stores the various parts
\r
115 * Defaults are used in certain cases
\r
117 * @param string $url Optional URL
\r
118 * @param bool $useBrackets Whether to use square brackets when
\r
119 * multiple querystrings with the same name
\r
122 function __construct($url = null, $useBrackets = true)
\r
124 $HTTP_SERVER_VARS = !empty($_SERVER) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
\r
126 $this->useBrackets = $useBrackets;
\r
133 $this->querystring = array();
\r
134 $this->anchor = '';
\r
136 // Only use defaults if not an absolute URL given
\r
137 if (!preg_match('/^[a-z0-9]+:\/\//i', $url)) {
\r
139 $this->protocol = (@$HTTP_SERVER_VARS['HTTPS'] == 'on' ? 'https' : 'http');
\r
142 * Figure out host/port
\r
144 if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) AND preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches)) {
\r
145 $host = $matches[1];
\r
146 if (!empty($matches[3])) {
\r
147 $port = $matches[3];
\r
149 $port = $this->getStandardPort($this->protocol);
\r
155 $this->host = !empty($host) ? $host : (isset($HTTP_SERVER_VARS['SERVER_NAME']) ? $HTTP_SERVER_VARS['SERVER_NAME'] : 'localhost');
\r
156 $this->port = !empty($port) ? $port : (isset($HTTP_SERVER_VARS['SERVER_PORT']) ? $HTTP_SERVER_VARS['SERVER_PORT'] : $this->getStandardPort($this->protocol));
\r
157 $this->path = !empty($HTTP_SERVER_VARS['PHP_SELF']) ? $HTTP_SERVER_VARS['PHP_SELF'] : '/';
\r
158 $this->querystring = isset($HTTP_SERVER_VARS['QUERY_STRING']) ? $this->_parseRawQuerystring($HTTP_SERVER_VARS['QUERY_STRING']) : null;
\r
159 $this->anchor = '';
\r
162 // Parse the url and store the various parts
\r
163 if (!empty($url)) {
\r
164 $urlinfo = parse_url($url);
\r
166 // Default querystring
\r
167 $this->querystring = array();
\r
169 foreach ($urlinfo as $key => $value) {
\r
172 $this->protocol = $value;
\r
173 $this->port = $this->getStandardPort($value);
\r
180 $this->$key = $value;
\r
184 if ($value{0} == '/') {
\r
185 $this->path = $value;
\r
187 $path = dirname($this->path) == DIRECTORY_SEPARATOR ? '' : dirname($this->path);
\r
188 $this->path = sprintf('%s/%s', $path, $value);
\r
193 $this->querystring = $this->_parseRawQueryString($value);
\r
197 $this->anchor = $value;
\r
207 * @return string Full url
\r
212 $querystring = $this->getQueryString();
\r
214 $this->url = $this->protocol . '://'
\r
215 . $this->user . (!empty($this->pass) ? ':' : '')
\r
216 . $this->pass . (!empty($this->user) ? '@' : '')
\r
217 . $this->host . ($this->port == $this->getStandardPort($this->protocol) ? '' : ':' . $this->port)
\r
219 . (!empty($querystring) ? '?' . $querystring : '')
\r
220 . (!empty($this->anchor) ? '#' . $this->anchor : '');
\r
226 * Adds a querystring item
\r
228 * @param string $name Name of item
\r
229 * @param string $value Value of item
\r
230 * @param bool $preencoded Whether value is urlencoded or not, default = not
\r
233 function addQueryString($name, $value, $preencoded = false)
\r
236 $this->querystring[$name] = $value;
\r
238 $this->querystring[$name] = is_array($value) ? array_map('rawurlencode', $value): rawurlencode($value);
\r
243 * Removes a querystring item
\r
245 * @param string $name Name of item
\r
248 function removeQueryString($name)
\r
250 if (isset($this->querystring[$name])) {
\r
251 unset($this->querystring[$name]);
\r
256 * Sets the querystring to literally what you supply
\r
258 * @param string $querystring The querystring data. Should be of the format foo=bar&x=y etc
\r
261 function addRawQueryString($querystring)
\r
263 $this->querystring = $this->_parseRawQueryString($querystring);
\r
267 * Returns flat querystring
\r
269 * @return string Querystring
\r
272 function getQueryString()
\r
274 if (!empty($this->querystring)) {
\r
275 foreach ($this->querystring as $name => $value) {
\r
276 if (is_array($value)) {
\r
277 foreach ($value as $k => $v) {
\r
278 $querystring[] = $this->useBrackets ? sprintf('%s[%s]=%s', $name, $k, $v) : ($name . '=' . $v);
\r
280 } elseif (!is_null($value)) {
\r
281 $querystring[] = $name . '=' . $value;
\r
283 $querystring[] = $name;
\r
286 $querystring = implode(ini_get('arg_separator.output'), $querystring);
\r
291 return $querystring;
\r
295 * Parses raw querystring and returns an array of it
\r
297 * @param string $querystring The querystring to parse
\r
298 * @return array An array of the querystring data
\r
301 function _parseRawQuerystring($querystring)
\r
303 $parts = preg_split('/[' . preg_quote(ini_get('arg_separator.input'), '/') . ']/', $querystring, -1, PREG_SPLIT_NO_EMPTY);
\r
306 foreach ($parts as $part) {
\r
307 if (strpos($part, '=') !== false) {
\r
308 $value = substr($part, strpos($part, '=') + 1);
\r
309 $key = substr($part, 0, strpos($part, '='));
\r
314 if (substr($key, -2) == '[]') {
\r
315 $key = substr($key, 0, -2);
\r
316 if (@!is_array($return[$key])) {
\r
317 $return[$key] = array();
\r
318 $return[$key][] = $value;
\r
320 $return[$key][] = $value;
\r
322 } elseif (!$this->useBrackets AND !empty($return[$key])) {
\r
323 $return[$key] = (array)$return[$key];
\r
324 $return[$key][] = $value;
\r
326 $return[$key] = $value;
\r
334 * Resolves //, ../ and ./ from a path and returns
\r
337 * /foo/bar/../boo.php => /foo/boo.php
\r
338 * /foo/bar/../../boo.php => /boo.php
\r
339 * /foo/bar/.././/boo.php => /foo/boo.php
\r
341 * This method can also be called statically.
\r
343 * @param string $url URL path to resolve
\r
344 * @return string The result
\r
346 function resolvePath($path)
\r
348 $path = explode('/', str_replace('//', '/', $path));
\r
350 for ($i=0; $i<count($path); $i++) {
\r
351 if ($path[$i] == '.') {
\r
353 $path = array_values($path);
\r
356 } elseif ($path[$i] == '..' AND ($i > 1 OR ($i == 1 AND $path[0] != '') ) ) {
\r
358 unset($path[$i-1]);
\r
359 $path = array_values($path);
\r
362 } elseif ($path[$i] == '..' AND $i == 1 AND $path[0] == '') {
\r
364 $path = array_values($path);
\r
372 return implode('/', $path);
\r
376 * Returns the standard port number for a protocol
\r
378 * @param string $scheme The protocol to lookup
\r
379 * @return integer Port number or NULL if no scheme matches
\r
381 * @author Philippe Jausions <Philippe.Jausions@11abacus.com>
\r
383 function getStandardPort($scheme)
\r
385 switch (strtolower($scheme)) {
\r
386 case 'http': return 80;
\r
387 case 'https': return 443;
\r
388 case 'ftp': return 21;
\r
389 case 'imap': return 143;
\r
390 case 'imaps': return 993;
\r
391 case 'pop3': return 110;
\r
392 case 'pop3s': return 995;
\r
393 default: return null;
\r
398 * Forces the URL to a particular protocol
\r
400 * @param string $protocol Protocol to force the URL to
\r
401 * @param integer $port Optional port (standard port is used by default)
\r
403 function setProtocol($protocol, $port = null)
\r
405 $this->protocol = $protocol;
\r
406 $this->port = is_null($port) ? $this->getStandardPort() : $port;
\r