PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins_repo/openXWorkflow/www/admin/plugins/openXWorkflow/library/Zend/Controller/Router/Route/Hostname.php

https://github.com/orchestra-io/sample-openx
PHP | 341 lines | 164 code | 47 blank | 130 comment | 42 complexity | 57c2a500922c91c3a35298a72a67002a 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. * @package Zend_Controller
  16. * @subpackage Router
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Route.php 9581 2008-06-01 14:08:03Z martel $
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Controller_Router_Route_Abstract */
  22. require_once 'Zend/Controller/Router/Route/Abstract.php';
  23. /**
  24. * Hostname Route
  25. *
  26. * @package Zend_Controller
  27. * @subpackage Router
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @see http://manuals.rubyonrails.com/read/chapter/65
  31. */
  32. class Zend_Controller_Router_Route_Hostname extends Zend_Controller_Router_Route_Abstract
  33. {
  34. protected $_hostVariable = ':';
  35. protected $_regexDelimiter = '#';
  36. protected $_defaultRegex = null;
  37. /**
  38. * Holds names of all route's pattern variable names. Array index holds a position in host.
  39. * @var array
  40. */
  41. protected $_variables = array();
  42. /**
  43. * Holds Route patterns for all host parts. In case of a variable it stores it's regex
  44. * requirement or null. In case of a static part, it holds only it's direct value.
  45. * @var array
  46. */
  47. protected $_parts = array();
  48. /**
  49. * Holds user submitted default values for route's variables. Name and value pairs.
  50. * @var array
  51. */
  52. protected $_defaults = array();
  53. /**
  54. * Holds user submitted regular expression patterns for route's variables' values.
  55. * Name and value pairs.
  56. * @var array
  57. */
  58. protected $_requirements = array();
  59. /**
  60. * Default scheme
  61. * @var string
  62. */
  63. protected $_scheme = null;
  64. /**
  65. * Associative array filled on match() that holds matched path values
  66. * for given variable names.
  67. * @var array
  68. */
  69. protected $_values = array();
  70. /**
  71. * Current request object
  72. *
  73. * @var Zend_Controller_Request_Abstract
  74. */
  75. protected $_request;
  76. /**
  77. * Helper var that holds a count of route pattern's static parts
  78. * for validation
  79. * @var int
  80. */
  81. private $_staticCount = 0;
  82. /**
  83. * Set the request object
  84. *
  85. * @param Zend_Controller_Request_Abstract|null $request
  86. * @return void
  87. */
  88. public function setRequest(Zend_Controller_Request_Abstract $request = null)
  89. {
  90. $this->_request = $request;
  91. }
  92. /**
  93. * Get the request object
  94. *
  95. * @return Zend_Controller_Request_Abstract $request
  96. */
  97. public function getRequest()
  98. {
  99. if ($this->_request === null) {
  100. require_once 'Zend/Controller/Front.php';
  101. $this->_request = Zend_Controller_Front::getInstance()->getRequest();
  102. }
  103. return $this->_request;
  104. }
  105. /**
  106. * Instantiates route based on passed Zend_Config structure
  107. *
  108. * @param Zend_Config $config Configuration object
  109. */
  110. public static function getInstance(Zend_Config $config)
  111. {
  112. $reqs = ($config->reqs instanceof Zend_Config) ? $config->reqs->toArray() : array();
  113. $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
  114. $scheme = (isset($config->scheme)) ? $config->scheme : null;
  115. return new self($config->route, $defs, $reqs, $scheme);
  116. }
  117. /**
  118. * Prepares the route for mapping by splitting (exploding) it
  119. * to a corresponding atomic parts. These parts are assigned
  120. * a position which is later used for matching and preparing values.
  121. *
  122. * @param string $route Map used to match with later submitted hostname
  123. * @param array $defaults Defaults for map variables with keys as variable names
  124. * @param array $reqs Regular expression requirements for variables (keys as variable names)
  125. * @param string $scheme
  126. */
  127. public function __construct($route, $defaults = array(), $reqs = array(), $scheme = null)
  128. {
  129. $route = trim($route, '.');
  130. $this->_defaults = (array) $defaults;
  131. $this->_requirements = (array) $reqs;
  132. $this->_scheme = $scheme;
  133. if ($route != '') {
  134. foreach (explode('.', $route) as $pos => $part) {
  135. if (substr($part, 0, 1) == $this->_hostVariable) {
  136. $name = substr($part, 1);
  137. $this->_parts[$pos] = (isset($reqs[$name]) ? $reqs[$name] : $this->_defaultRegex);
  138. $this->_variables[$pos] = $name;
  139. } else {
  140. $this->_parts[$pos] = $part;
  141. $this->_staticCount++;
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Matches a user submitted path with parts defined by a map. Assigns and
  148. * returns an array of variables on a successful match.
  149. *
  150. * @param Zend_Controller_Request_Http $request Request to get the host from
  151. * @return array|false An array of assigned values or a false on a mismatch
  152. */
  153. public function match($request)
  154. {
  155. // Check the scheme if required
  156. if ($this->_scheme !== null) {
  157. $scheme = $request->getScheme();
  158. if ($scheme !== $this->_scheme) {
  159. return false;
  160. }
  161. }
  162. // Get the host and remove unnecessary port information
  163. $host = $request->getHttpHost();
  164. if (preg_match('#:\d+$#', $host, $result) === 1) {
  165. $host = substr($host, 0, -strlen($result[0]));
  166. }
  167. $hostStaticCount = 0;
  168. $values = array();
  169. $host = trim($host, '.');
  170. if ($host != '') {
  171. $host = explode('.', $host);
  172. foreach ($host as $pos => $hostPart) {
  173. // Host is longer than a route, it's not a match
  174. if (!array_key_exists($pos, $this->_parts)) {
  175. return false;
  176. }
  177. $name = isset($this->_variables[$pos]) ? $this->_variables[$pos] : null;
  178. $hostPart = urldecode($hostPart);
  179. // If it's a static part, match directly
  180. if ($name === null && $this->_parts[$pos] != $hostPart) {
  181. return false;
  182. }
  183. // If it's a variable with requirement, match a regex. If not - everything matches
  184. if ($this->_parts[$pos] !== null && !preg_match($this->_regexDelimiter . '^' . $this->_parts[$pos] . '$' . $this->_regexDelimiter . 'iu', $hostPart)) {
  185. return false;
  186. }
  187. // If it's a variable store it's value for later
  188. if ($name !== null) {
  189. $values[$name] = $hostPart;
  190. } else {
  191. $hostStaticCount++;
  192. }
  193. }
  194. }
  195. // Check if all static mappings have been matched
  196. if ($this->_staticCount != $hostStaticCount) {
  197. return false;
  198. }
  199. $return = $values + $this->_defaults;
  200. // Check if all map variables have been initialized
  201. foreach ($this->_variables as $var) {
  202. if (!array_key_exists($var, $return)) {
  203. return false;
  204. }
  205. }
  206. $this->_values = $values;
  207. return $return;
  208. }
  209. /**
  210. * Assembles user submitted parameters forming a hostname defined by this route
  211. *
  212. * @param array $data An array of variable and value pairs used as parameters
  213. * @param boolean $reset Whether or not to set route defaults with those provided in $data
  214. * @return string Route path with user submitted parameters
  215. */
  216. public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
  217. {
  218. $host = array();
  219. $flag = false;
  220. foreach ($this->_parts as $key => $part) {
  221. $name = isset($this->_variables[$key]) ? $this->_variables[$key] : null;
  222. $useDefault = false;
  223. if (isset($name) && array_key_exists($name, $data) && $data[$name] === null) {
  224. $useDefault = true;
  225. }
  226. if (isset($name)) {
  227. if (isset($data[$name]) && !$useDefault) {
  228. $host[$key] = $data[$name];
  229. unset($data[$name]);
  230. } elseif (!$reset && !$useDefault && isset($this->_values[$name])) {
  231. $host[$key] = $this->_values[$name];
  232. } elseif (isset($this->_defaults[$name])) {
  233. $host[$key] = $this->_defaults[$name];
  234. } else {
  235. require_once 'Zend/Controller/Router/Exception.php';
  236. throw new Zend_Controller_Router_Exception($name . ' is not specified');
  237. }
  238. } else {
  239. $host[$key] = $part;
  240. }
  241. }
  242. $return = '';
  243. foreach (array_reverse($host, true) as $key => $value) {
  244. if ($flag || !isset($this->_variables[$key]) || $value !== $this->getDefault($this->_variables[$key]) || $partial) {
  245. if ($encode) $value = urlencode($value);
  246. $return = '.' . $value . $return;
  247. $flag = true;
  248. }
  249. }
  250. $url = trim($return, '.');
  251. if ($this->_scheme !== null) {
  252. $scheme = $this->_scheme;
  253. } else {
  254. $request = $this->getRequest();
  255. if ($request instanceof Zend_Controller_Request_Http) {
  256. $scheme = $request->getScheme();
  257. } else {
  258. $scheme = 'http';
  259. }
  260. }
  261. $hostname = implode('.', $host);
  262. $url = $scheme . '://' . $url;
  263. return $url;
  264. }
  265. /**
  266. * Return a single parameter of route's defaults
  267. *
  268. * @param string $name Array key of the parameter
  269. * @return string Previously set default
  270. */
  271. public function getDefault($name) {
  272. if (isset($this->_defaults[$name])) {
  273. return $this->_defaults[$name];
  274. }
  275. return null;
  276. }
  277. /**
  278. * Return an array of defaults
  279. *
  280. * @return array Route defaults
  281. */
  282. public function getDefaults() {
  283. return $this->_defaults;
  284. }
  285. /**
  286. * Get all variables which are used by the route
  287. *
  288. * @return array
  289. */
  290. public function getVariables()
  291. {
  292. return $this->_variables;
  293. }
  294. }