Uri.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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-2009 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-2009 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. * @throws Zend_Uri_Exception When an empty string was supplied for the scheme
  78. * @throws Zend_Uri_Exception When an illegal scheme is supplied
  79. * @throws Zend_Uri_Exception When the scheme is not supported
  80. * @return Zend_Uri
  81. * @link http://www.faqs.org/rfcs/rfc2396.html
  82. */
  83. public static function factory($uri = 'http')
  84. {
  85. // Separate the scheme from the scheme-specific parts
  86. $uri = explode(':', $uri, 2);
  87. $scheme = strtolower($uri[0]);
  88. $schemeSpecific = isset($uri[1]) === true ? $uri[1] : '';
  89. if (strlen($scheme) === 0) {
  90. require_once 'Zend/Uri/Exception.php';
  91. throw new Zend_Uri_Exception('An empty string was supplied for the scheme');
  92. }
  93. // Security check: $scheme is used to load a class file, so only alphanumerics are allowed.
  94. if (ctype_alnum($scheme) === false) {
  95. require_once 'Zend/Uri/Exception.php';
  96. throw new Zend_Uri_Exception('Illegal scheme supplied, only alphanumeric characters are permitted');
  97. }
  98. /**
  99. * Create a new Zend_Uri object for the $uri. If a subclass of Zend_Uri exists for the
  100. * scheme, return an instance of that class. Otherwise, a Zend_Uri_Exception is thrown.
  101. */
  102. switch ($scheme) {
  103. case 'http':
  104. // Break intentionally omitted
  105. case 'https':
  106. $className = 'Zend_Uri_Http';
  107. break;
  108. case 'mailto':
  109. // TODO
  110. default:
  111. require_once 'Zend/Uri/Exception.php';
  112. throw new Zend_Uri_Exception("Scheme \"$scheme\" is not supported");
  113. break;
  114. }
  115. if (!class_exists($className)) {
  116. require_once 'Zend/Loader.php';
  117. Zend_Loader::loadClass($className);
  118. }
  119. $schemeHandler = new $className($scheme, $schemeSpecific);
  120. return $schemeHandler;
  121. }
  122. /**
  123. * Get the URI's scheme
  124. *
  125. * @return string|false Scheme or false if no scheme is set.
  126. */
  127. public function getScheme()
  128. {
  129. if (empty($this->_scheme) === false) {
  130. return $this->_scheme;
  131. } else {
  132. return false;
  133. }
  134. }
  135. /**
  136. * Set global configuration options
  137. *
  138. * @param Zend_Config|array $config
  139. */
  140. static public function setConfig($config)
  141. {
  142. if ($config instanceof Zend_Config) {
  143. $config = $config->toArray();
  144. } elseif (!is_array($config)) {
  145. throw new Zend_Uri_Exception("Config must be an array or an instance of Zend_Config.");
  146. }
  147. foreach ($config as $k => $v) {
  148. self::$_config[$k] = $v;
  149. }
  150. }
  151. /**
  152. * Zend_Uri and its subclasses cannot be instantiated directly.
  153. * Use Zend_Uri::factory() to return a new Zend_Uri object.
  154. *
  155. * @param string $scheme The scheme of the URI
  156. * @param string $schemeSpecific The scheme-specific part of the URI
  157. */
  158. abstract protected function __construct($scheme, $schemeSpecific = '');
  159. /**
  160. * Return a string representation of this URI.
  161. *
  162. * @return string
  163. */
  164. abstract public function getUri();
  165. /**
  166. * Returns TRUE if this URI is valid, or FALSE otherwise.
  167. *
  168. * @return boolean
  169. */
  170. abstract public function valid();
  171. }