PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/devblocks/libs/ZendFramework/Zend/Controller/Action/Helper/Redirector.php

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