Uri.php 6.2 KB

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