[44] | 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-2008 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 12037 2008-10-20 18:54:44Z shahar $
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * @see Zend_Loader
|
---|
| 24 | */
|
---|
| 25 | require_once 'Zend/Loader.php';
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Abstract class for all Zend_Uri handlers
|
---|
| 29 | *
|
---|
| 30 | * @category Zend
|
---|
| 31 | * @package Zend_Uri
|
---|
| 32 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
| 33 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
| 34 | */
|
---|
| 35 | abstract class Zend_Uri
|
---|
| 36 | {
|
---|
| 37 | /**
|
---|
| 38 | * Scheme of this URI (http, ftp, etc.)
|
---|
| 39 | *
|
---|
| 40 | * @var string
|
---|
| 41 | */
|
---|
| 42 | protected $_scheme = '';
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Global configuration array
|
---|
| 46 | *
|
---|
| 47 | * @var array
|
---|
| 48 | */
|
---|
| 49 | static protected $_config = array(
|
---|
| 50 | 'allow_unwise' => false
|
---|
| 51 | );
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * Return a string representation of this URI.
|
---|
| 55 | *
|
---|
| 56 | * @see getUri()
|
---|
| 57 | * @return string
|
---|
| 58 | */
|
---|
| 59 | public function __toString()
|
---|
| 60 | {
|
---|
| 61 | return $this->getUri();
|
---|
| 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 | * @throws Zend_Uri_Exception When an empty string was supplied for the scheme
|
---|
| 89 | * @throws Zend_Uri_Exception When an illegal scheme is supplied
|
---|
| 90 | * @throws Zend_Uri_Exception When the scheme is not supported
|
---|
| 91 | * @return Zend_Uri
|
---|
| 92 | * @link http://www.faqs.org/rfcs/rfc2396.html
|
---|
| 93 | */
|
---|
| 94 | public static function factory($uri = 'http')
|
---|
| 95 | {
|
---|
| 96 | // Separate the scheme from the scheme-specific parts
|
---|
| 97 | $uri = explode(':', $uri, 2);
|
---|
| 98 | $scheme = strtolower($uri[0]);
|
---|
| 99 | $schemeSpecific = isset($uri[1]) === true ? $uri[1] : '';
|
---|
| 100 |
|
---|
| 101 | if (strlen($scheme) === 0) {
|
---|
| 102 | require_once 'Zend/Uri/Exception.php';
|
---|
| 103 | throw new Zend_Uri_Exception('An empty string was supplied for the scheme');
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | // Security check: $scheme is used to load a class file, so only alphanumerics are allowed.
|
---|
| 107 | if (ctype_alnum($scheme) === false) {
|
---|
| 108 | require_once 'Zend/Uri/Exception.php';
|
---|
| 109 | throw new Zend_Uri_Exception('Illegal scheme supplied, only alphanumeric characters are permitted');
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /**
|
---|
| 113 | * Create a new Zend_Uri object for the $uri. If a subclass of Zend_Uri exists for the
|
---|
| 114 | * scheme, return an instance of that class. Otherwise, a Zend_Uri_Exception is thrown.
|
---|
| 115 | */
|
---|
| 116 | switch ($scheme) {
|
---|
| 117 | case 'http':
|
---|
| 118 | // Break intentionally omitted
|
---|
| 119 | case 'https':
|
---|
| 120 | $className = 'Zend_Uri_Http';
|
---|
| 121 | break;
|
---|
| 122 |
|
---|
| 123 | case 'mailto':
|
---|
| 124 | // TODO
|
---|
| 125 | default:
|
---|
| 126 | require_once 'Zend/Uri/Exception.php';
|
---|
| 127 | throw new Zend_Uri_Exception("Scheme \"$scheme\" is not supported");
|
---|
| 128 | break;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | Zend_Loader::loadClass($className);
|
---|
| 132 | $schemeHandler = new $className($scheme, $schemeSpecific);
|
---|
| 133 |
|
---|
| 134 | return $schemeHandler;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * Get the URI's scheme
|
---|
| 139 | *
|
---|
| 140 | * @return string|false Scheme or false if no scheme is set.
|
---|
| 141 | */
|
---|
| 142 | public function getScheme()
|
---|
| 143 | {
|
---|
| 144 | if (empty($this->_scheme) === false) {
|
---|
| 145 | return $this->_scheme;
|
---|
| 146 | } else {
|
---|
| 147 | return false;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /**
|
---|
| 152 | * Set global configuration options
|
---|
| 153 | *
|
---|
| 154 | * @param array $config
|
---|
| 155 | */
|
---|
| 156 | static public function setConfig(array $config)
|
---|
| 157 | {
|
---|
| 158 | foreach ($config as $k => $v) {
|
---|
| 159 | self::$_config[$k] = $v;
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Zend_Uri and its subclasses cannot be instantiated directly.
|
---|
| 165 | * Use Zend_Uri::factory() to return a new Zend_Uri object.
|
---|
| 166 | *
|
---|
| 167 | * @param string $scheme The scheme of the URI
|
---|
| 168 | * @param string $schemeSpecific The scheme-specific part of the URI
|
---|
| 169 | */
|
---|
| 170 | abstract protected function __construct($scheme, $schemeSpecific = '');
|
---|
| 171 |
|
---|
| 172 | /**
|
---|
| 173 | * Return a string representation of this URI.
|
---|
| 174 | *
|
---|
| 175 | * @return string
|
---|
| 176 | */
|
---|
| 177 | abstract public function getUri();
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
| 180 | * Returns TRUE if this URI is valid, or FALSE otherwise.
|
---|
| 181 | *
|
---|
| 182 | * @return boolean
|
---|
| 183 | */
|
---|
| 184 | abstract public function valid();
|
---|
| 185 | }
|
---|