Uri.php 5.2 KB

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