Hostname.php 11 KB

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