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