Hostname.php 11 KB

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