Hostname.php 11 KB

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