PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Controller/Action/Helper/Redirector.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 534 lines | 222 code | 51 blank | 261 comment | 48 complexity | 715082fe095611c24fb024e1313102a2 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_Controller
  17. * @subpackage Zend_Controller_Action_Helper
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Redirector.php 23940 2011-05-02 20:20:40Z matthew $
  21. */
  22. /**
  23. * @see Zend_Controller_Action_Helper_Abstract
  24. */
  25. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Controller
  29. * @subpackage Zend_Controller_Action_Helper
  30. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Controller_Action_Helper_Redirector extends Zend_Controller_Action_Helper_Abstract
  34. {
  35. /**
  36. * HTTP status code for redirects
  37. * @var int
  38. */
  39. protected $_code = 302;
  40. /**
  41. * Whether or not calls to _redirect() should exit script execution
  42. * @var boolean
  43. */
  44. protected $_exit = true;
  45. /**
  46. * Whether or not _redirect() should attempt to prepend the base URL to the
  47. * passed URL (if it's a relative URL)
  48. * @var boolean
  49. */
  50. protected $_prependBase = true;
  51. /**
  52. * Url to which to redirect
  53. * @var string
  54. */
  55. protected $_redirectUrl = null;
  56. /**
  57. * Whether or not to use an absolute URI when redirecting
  58. * @var boolean
  59. */
  60. protected $_useAbsoluteUri = false;
  61. /**
  62. * Whether or not to close the session before exiting
  63. * @var boolean
  64. */
  65. protected $_closeSessionOnExit = true;
  66. /**
  67. * Retrieve HTTP status code to emit on {@link _redirect()} call
  68. *
  69. * @return int
  70. */
  71. public function getCode()
  72. {
  73. return $this->_code;
  74. }
  75. /**
  76. * Validate HTTP status redirect code
  77. *
  78. * @param int $code
  79. * @throws Zend_Controller_Action_Exception on invalid HTTP status code
  80. * @return true
  81. */
  82. protected function _checkCode($code)
  83. {
  84. $code = (int)$code;
  85. if ((300 > $code) || (307 < $code) || (304 == $code) || (306 == $code)) {
  86. require_once 'Zend/Controller/Action/Exception.php';
  87. throw new Zend_Controller_Action_Exception('Invalid redirect HTTP status code (' . $code . ')');
  88. }
  89. return true;
  90. }
  91. /**
  92. * Retrieve HTTP status code for {@link _redirect()} behaviour
  93. *
  94. * @param int $code
  95. * @return Zend_Controller_Action_Helper_Redirector Provides a fluent interface
  96. */
  97. public function setCode($code)
  98. {
  99. $this->_checkCode($code);
  100. $this->_code = $code;
  101. return $this;
  102. }
  103. /**
  104. * Retrieve flag for whether or not {@link _redirect()} will exit when finished.
  105. *
  106. * @return boolean
  107. */
  108. public function getExit()
  109. {
  110. return $this->_exit;
  111. }
  112. /**
  113. * Retrieve exit flag for {@link _redirect()} behaviour
  114. *
  115. * @param boolean $flag
  116. * @return Zend_Controller_Action_Helper_Redirector Provides a fluent interface
  117. */
  118. public function setExit($flag)
  119. {
  120. $this->_exit = ($flag) ? true : false;
  121. return $this;
  122. }
  123. /**
  124. * Retrieve flag for whether or not {@link _redirect()} will prepend the
  125. * base URL on relative URLs
  126. *
  127. * @return boolean
  128. */
  129. public function getPrependBase()
  130. {
  131. return $this->_prependBase;
  132. }
  133. /**
  134. * Retrieve 'prepend base' flag for {@link _redirect()} behaviour
  135. *
  136. * @param boolean $flag
  137. * @return Zend_Controller_Action_Helper_Redirector Provides a fluent interface
  138. */
  139. public function setPrependBase($flag)
  140. {
  141. $this->_prependBase = ($flag) ? true : false;
  142. return $this;
  143. }
  144. /**
  145. * Retrieve flag for whether or not {@link redirectAndExit()} shall close the session before
  146. * exiting.
  147. *
  148. * @return boolean
  149. */
  150. public function getCloseSessionOnExit()
  151. {
  152. return $this->_closeSessionOnExit;
  153. }
  154. /**
  155. * Set flag for whether or not {@link redirectAndExit()} shall close the session before exiting.
  156. *
  157. * @param boolean $flag
  158. * @return Zend_Controller_Action_Helper_Redirector Provides a fluent interface
  159. */
  160. public function setCloseSessionOnExit($flag)
  161. {
  162. $this->_closeSessionOnExit = ($flag) ? true : false;
  163. return $this;
  164. }
  165. /**
  166. * Return use absolute URI flag
  167. *
  168. * @return boolean
  169. */
  170. public function getUseAbsoluteUri()
  171. {
  172. return $this->_useAbsoluteUri;
  173. }
  174. /**
  175. * Set use absolute URI flag
  176. *
  177. * @param boolean $flag
  178. * @return Zend_Controller_Action_Helper_Redirector Provides a fluent interface
  179. */
  180. public function setUseAbsoluteUri($flag = true)
  181. {
  182. $this->_useAbsoluteUri = ($flag) ? true : false;
  183. return $this;
  184. }
  185. /**
  186. * Set redirect in response object
  187. *
  188. * @return void
  189. */
  190. protected function _redirect($url)
  191. {
  192. if ($this->getUseAbsoluteUri() && !preg_match('#^(https?|ftp)://#', $url)) {
  193. $host = (isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:'');
  194. $proto = (isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']!=="off") ? 'https' : 'http';
  195. $port = (isset($_SERVER['SERVER_PORT'])?$_SERVER['SERVER_PORT']:80);
  196. $uri = $proto . '://' . $host;
  197. if ((('http' == $proto) && (80 != $port)) || (('https' == $proto) && (443 != $port))) {
  198. // do not append if HTTP_HOST already contains port
  199. if (strrchr($host, ':') === false) {
  200. $uri .= ':' . $port;
  201. }
  202. }
  203. $url = $uri . '/' . ltrim($url, '/');
  204. }
  205. $this->_redirectUrl = $url;
  206. $this->getResponse()->setRedirect($url, $this->getCode());
  207. }
  208. /**
  209. * Retrieve currently set URL for redirect
  210. *
  211. * @return string
  212. */
  213. public function getRedirectUrl()
  214. {
  215. return $this->_redirectUrl;
  216. }
  217. /**
  218. * Determine if the baseUrl should be prepended, and prepend if necessary
  219. *
  220. * @param string $url
  221. * @return string
  222. */
  223. protected function _prependBase($url)
  224. {
  225. if ($this->getPrependBase()) {
  226. $request = $this->getRequest();
  227. if ($request instanceof Zend_Controller_Request_Http) {
  228. $base = rtrim($request->getBaseUrl(), '/');
  229. if (!empty($base) && ('/' != $base)) {
  230. $url = $base . '/' . ltrim($url, '/');
  231. } else {
  232. $url = '/' . ltrim($url, '/');
  233. }
  234. }
  235. }
  236. return $url;
  237. }
  238. /**
  239. * Set a redirect URL of the form /module/controller/action/params
  240. *
  241. * @param string $action
  242. * @param string $controller
  243. * @param string $module
  244. * @param array $params
  245. * @return void
  246. */
  247. public function setGotoSimple($action, $controller = null, $module = null, array $params = array())
  248. {
  249. $dispatcher = $this->getFrontController()->getDispatcher();
  250. $request = $this->getRequest();
  251. $curModule = $request->getModuleName();
  252. $useDefaultController = false;
  253. if (null === $controller && null !== $module) {
  254. $useDefaultController = true;
  255. }
  256. if (null === $module) {
  257. $module = $curModule;
  258. }
  259. if ($module == $dispatcher->getDefaultModule()) {
  260. $module = '';
  261. }
  262. if (null === $controller && !$useDefaultController) {
  263. $controller = $request->getControllerName();
  264. if (empty($controller)) {
  265. $controller = $dispatcher->getDefaultControllerName();
  266. }
  267. }
  268. $params[$request->getModuleKey()] = $module;
  269. $params[$request->getControllerKey()] = $controller;
  270. $params[$request->getActionKey()] = $action;
  271. $router = $this->getFrontController()->getRouter();
  272. $url = $router->assemble($params, 'default', true);
  273. $this->_redirect($url);
  274. }
  275. /**
  276. * Build a URL based on a route
  277. *
  278. * @param array $urlOptions
  279. * @param string $name Route name
  280. * @param boolean $reset
  281. * @param boolean $encode
  282. * @return void
  283. */
  284. public function setGotoRoute(array $urlOptions = array(), $name = null, $reset = false, $encode = true)
  285. {
  286. $router = $this->getFrontController()->getRouter();
  287. $url = $router->assemble($urlOptions, $name, $reset, $encode);
  288. $this->_redirect($url);
  289. }
  290. /**
  291. * Set a redirect URL string
  292. *
  293. * By default, emits a 302 HTTP status header, prepends base URL as defined
  294. * in request object if url is relative, and halts script execution by
  295. * calling exit().
  296. *
  297. * $options is an optional associative array that can be used to control
  298. * redirect behaviour. The available option keys are:
  299. * - exit: boolean flag indicating whether or not to halt script execution when done
  300. * - prependBase: boolean flag indicating whether or not to prepend the base URL when a relative URL is provided
  301. * - code: integer HTTP status code to use with redirect. Should be between 300 and 307.
  302. *
  303. * _redirect() sets the Location header in the response object. If you set
  304. * the exit flag to false, you can override this header later in code
  305. * execution.
  306. *
  307. * If the exit flag is true (true by default), _redirect() will write and
  308. * close the current session, if any.
  309. *
  310. * @param string $url
  311. * @param array $options
  312. * @return void
  313. */
  314. public function setGotoUrl($url, array $options = array())
  315. {
  316. // prevent header injections
  317. $url = str_replace(array("\n", "\r"), '', $url);
  318. if (null !== $options) {
  319. if (isset($options['exit'])) {
  320. $this->setExit(($options['exit']) ? true : false);
  321. }
  322. if (isset($options['prependBase'])) {
  323. $this->setPrependBase(($options['prependBase']) ? true : false);
  324. }
  325. if (isset($options['code'])) {
  326. $this->setCode($options['code']);
  327. }
  328. }
  329. // If relative URL, decide if we should prepend base URL
  330. if (!preg_match('|^[a-z]+://|', $url)) {
  331. $url = $this->_prependBase($url);
  332. }
  333. $this->_redirect($url);
  334. }
  335. /**
  336. * Perform a redirect to an action/controller/module with params
  337. *
  338. * @param string $action
  339. * @param string $controller
  340. * @param string $module
  341. * @param array $params
  342. * @return void
  343. */
  344. public function gotoSimple($action, $controller = null, $module = null, array $params = array())
  345. {
  346. $this->setGotoSimple($action, $controller, $module, $params);
  347. if ($this->getExit()) {
  348. $this->redirectAndExit();
  349. }
  350. }
  351. /**
  352. * Perform a redirect to an action/controller/module with params, forcing an immdiate exit
  353. *
  354. * @param mixed $action
  355. * @param mixed $controller
  356. * @param mixed $module
  357. * @param array $params
  358. * @return void
  359. */
  360. public function gotoSimpleAndExit($action, $controller = null, $module = null, array $params = array())
  361. {
  362. $this->setGotoSimple($action, $controller, $module, $params);
  363. $this->redirectAndExit();
  364. }
  365. /**
  366. * Redirect to a route-based URL
  367. *
  368. * Uses route's assemble method tobuild the URL; route is specified by $name;
  369. * default route is used if none provided.
  370. *
  371. * @param array $urlOptions Array of key/value pairs used to assemble URL
  372. * @param string $name
  373. * @param boolean $reset
  374. * @param boolean $encode
  375. * @return void
  376. */
  377. public function gotoRoute(array $urlOptions = array(), $name = null, $reset = false, $encode = true)
  378. {
  379. $this->setGotoRoute($urlOptions, $name, $reset, $encode);
  380. if ($this->getExit()) {
  381. $this->redirectAndExit();
  382. }
  383. }
  384. /**
  385. * Redirect to a route-based URL, and immediately exit
  386. *
  387. * Uses route's assemble method tobuild the URL; route is specified by $name;
  388. * default route is used if none provided.
  389. *
  390. * @param array $urlOptions Array of key/value pairs used to assemble URL
  391. * @param string $name
  392. * @param boolean $reset
  393. * @return void
  394. */
  395. public function gotoRouteAndExit(array $urlOptions = array(), $name = null, $reset = false)
  396. {
  397. $this->setGotoRoute($urlOptions, $name, $reset);
  398. $this->redirectAndExit();
  399. }
  400. /**
  401. * Perform a redirect to a url
  402. *
  403. * @param string $url
  404. * @param array $options
  405. * @return void
  406. */
  407. public function gotoUrl($url, array $options = array())
  408. {
  409. $this->setGotoUrl($url, $options);
  410. if ($this->getExit()) {
  411. $this->redirectAndExit();
  412. }
  413. }
  414. /**
  415. * Set a URL string for a redirect, perform redirect, and immediately exit
  416. *
  417. * @param string $url
  418. * @param array $options
  419. * @return void
  420. */
  421. public function gotoUrlAndExit($url, array $options = array())
  422. {
  423. $this->setGotoUrl($url, $options);
  424. $this->redirectAndExit();
  425. }
  426. /**
  427. * exit(): Perform exit for redirector
  428. *
  429. * @return void
  430. */
  431. public function redirectAndExit()
  432. {
  433. if ($this->getCloseSessionOnExit()) {
  434. // Close session, if started
  435. if (class_exists('Zend_Session', false) && Zend_Session::isStarted()) {
  436. Zend_Session::writeClose();
  437. } elseif (isset($_SESSION)) {
  438. session_write_close();
  439. }
  440. }
  441. $this->getResponse()->sendHeaders();
  442. exit();
  443. }
  444. /**
  445. * direct(): Perform helper when called as
  446. * $this->_helper->redirector($action, $controller, $module, $params)
  447. *
  448. * @param string $action
  449. * @param string $controller
  450. * @param string $module
  451. * @param array $params
  452. * @return void
  453. */
  454. public function direct($action, $controller = null, $module = null, array $params = array())
  455. {
  456. $this->gotoSimple($action, $controller, $module, $params);
  457. }
  458. /**
  459. * Overloading
  460. *
  461. * Overloading for old 'goto', 'setGoto', and 'gotoAndExit' methods
  462. *
  463. * @param string $method
  464. * @param array $args
  465. * @return mixed
  466. * @throws Zend_Controller_Action_Exception for invalid methods
  467. */
  468. public function __call($method, $args)
  469. {
  470. $method = strtolower($method);
  471. if ('goto' == $method) {
  472. return call_user_func_array(array($this, 'gotoSimple'), $args);
  473. }
  474. if ('setgoto' == $method) {
  475. return call_user_func_array(array($this, 'setGotoSimple'), $args);
  476. }
  477. if ('gotoandexit' == $method) {
  478. return call_user_func_array(array($this, 'gotoSimpleAndExit'), $args);
  479. }
  480. require_once 'Zend/Controller/Action/Exception.php';
  481. throw new Zend_Controller_Action_Exception(sprintf('Invalid method "%s" called on redirector', $method));
  482. }
  483. }