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.
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * Abstract class for all Zend_Uri handlers
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
28 * @license http://framework.zend.com/license/new-bsd New BSD License
30 abstract class Zend_Uri
33 * Scheme of this URI (http, ftp, etc.)
37 protected $_scheme = '';
40 * Global configuration array
44 static protected $_config = array(
45 'allow_unwise' => false
49 * Return a string representation of this URI.
54 public function __toString()
56 return $this->getUri();
60 * Convenience function, checks that a $uri string is well-formed
61 * by validating it but not returning an object. Returns TRUE if
62 * $uri is a well-formed URI, or FALSE otherwise.
64 * @param string $uri The URI to check
67 public static function check($uri)
70 $uri = self::factory($uri);
71 } catch (Exception $e) {
79 * Create a new Zend_Uri object for a URI. If building a new URI, then $uri should contain
80 * only the scheme (http, ftp, etc). Otherwise, supply $uri with the complete URI.
82 * @param string $uri The URI form which a Zend_Uri instance is created
83 * @param string $className The name of the class to use in order to manipulate URI
84 * @throws Zend_Uri_Exception When an empty string was supplied for the scheme
85 * @throws Zend_Uri_Exception When an illegal scheme is supplied
86 * @throws Zend_Uri_Exception When the scheme is not supported
87 * @throws Zend_Uri_Exception When $className doesn't exist or doesn't implements Zend_Uri
89 * @link http://www.faqs.org/rfcs/rfc2396.html
91 public static function factory($uri = 'http', $className = null)
93 // Separate the scheme from the scheme-specific parts
94 $uri = explode(':', $uri, 2);
95 $scheme = strtolower($uri[0]);
96 $schemeSpecific = isset($uri[1]) === true ? $uri[1] : '';
98 if (strlen($scheme) === 0) {
99 require_once 'Zend/Uri/Exception.php';
100 throw new Zend_Uri_Exception('An empty string was supplied for the scheme');
103 // Security check: $scheme is used to load a class file, so only alphanumerics are allowed.
104 if (ctype_alnum($scheme) === false) {
105 require_once 'Zend/Uri/Exception.php';
106 throw new Zend_Uri_Exception('Illegal scheme supplied, only alphanumeric characters are permitted');
109 if ($className === null) {
111 * Create a new Zend_Uri object for the $uri. If a subclass of Zend_Uri exists for the
112 * scheme, return an instance of that class. Otherwise, a Zend_Uri_Exception is thrown.
116 // Break intentionally omitted
118 $className = 'Zend_Uri_Http';
124 require_once 'Zend/Uri/Exception.php';
125 throw new Zend_Uri_Exception("Scheme \"$scheme\" is not supported");
130 require_once 'Zend/Loader.php';
132 Zend_Loader::loadClass($className);
133 } catch (Exception $e) {
134 require_once 'Zend/Uri/Exception.php';
135 throw new Zend_Uri_Exception("\"$className\" not found");
138 $schemeHandler = new $className($scheme, $schemeSpecific);
140 if (! $schemeHandler instanceof Zend_Uri) {
141 require_once 'Zend/Uri/Exception.php';
142 throw new Zend_Uri_Exception("\"$className\" is not an instance of Zend_Uri");
145 return $schemeHandler;
149 * Get the URI's scheme
151 * @return string|false Scheme or false if no scheme is set.
153 public function getScheme()
155 if (empty($this->_scheme) === false) {
156 return $this->_scheme;
163 * Set global configuration options
165 * @param Zend_Config|array $config
167 static public function setConfig($config)
169 if ($config instanceof Zend_Config) {
170 $config = $config->toArray();
171 } elseif (!is_array($config)) {
172 throw new Zend_Uri_Exception("Config must be an array or an instance of Zend_Config.");
175 foreach ($config as $k => $v) {
176 self::$_config[$k] = $v;
181 * Zend_Uri and its subclasses cannot be instantiated directly.
182 * Use Zend_Uri::factory() to return a new Zend_Uri object.
184 * @param string $scheme The scheme of the URI
185 * @param string $schemeSpecific The scheme-specific part of the URI
187 abstract protected function __construct($scheme, $schemeSpecific = '');
190 * Return a string representation of this URI.
194 abstract public function getUri();
197 * Returns TRUE if this URI is valid, or FALSE otherwise.
201 abstract public function valid();