PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/src/application/libraries/Zend/Uri.php

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