sfWebController.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfWebController provides web specific methods to sfController such as, url redirection.
  12. *
  13. * @package symfony
  14. * @subpackage controller
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @author Sean Kerr <sean@code-box.org>
  17. * @version SVN: $Id: sfWebController.class.php 13026 2008-11-16 16:50:51Z fabien $
  18. */
  19. abstract class sfWebController extends sfController
  20. {
  21. /**
  22. * Generates an URL from an array of parameters.
  23. *
  24. * @param mixed $parameters An associative array of URL parameters or an internal URI as a string.
  25. * @param boolean $absolute Whether to generate an absolute URL
  26. *
  27. * @return string A URL to a symfony resource
  28. */
  29. public function genUrl($parameters = array(), $absolute = false)
  30. {
  31. // absolute URL or symfony URL?
  32. if (is_string($parameters) && preg_match('#^[a-z][a-z0-9\+.\-]*\://#i', $parameters))
  33. {
  34. return $parameters;
  35. }
  36. // relative URL?
  37. if (is_string($parameters) && '/' == $parameters[0])
  38. {
  39. return $parameters;
  40. }
  41. if (is_string($parameters) && $parameters == '#')
  42. {
  43. return $parameters;
  44. }
  45. $route = '';
  46. $fragment = '';
  47. if (is_string($parameters))
  48. {
  49. // strip fragment
  50. if (false !== ($pos = strpos($parameters, '#')))
  51. {
  52. $fragment = substr($parameters, $pos + 1);
  53. $parameters = substr($parameters, 0, $pos);
  54. }
  55. list($route, $parameters) = $this->convertUrlStringToParameters($parameters);
  56. }
  57. else if (is_array($parameters))
  58. {
  59. if (isset($parameters['sf_route']))
  60. {
  61. $route = $parameters['sf_route'];
  62. unset($parameters['sf_route']);
  63. }
  64. }
  65. // routing to generate path
  66. $url = $this->context->getRouting()->generate($route, $parameters, $absolute);
  67. if ($fragment)
  68. {
  69. $url .= '#'.$fragment;
  70. }
  71. return $url;
  72. }
  73. /**
  74. * Converts an internal URI string to an array of parameters.
  75. *
  76. * @param string $url An internal URI
  77. *
  78. * @return array An array of parameters
  79. */
  80. public function convertUrlStringToParameters($url)
  81. {
  82. $givenUrl = $url;
  83. $params = array();
  84. $queryString = '';
  85. $route = '';
  86. // empty url?
  87. if (!$url)
  88. {
  89. $url = '/';
  90. }
  91. // we get the query string out of the url
  92. if ($pos = strpos($url, '?'))
  93. {
  94. $queryString = substr($url, $pos + 1);
  95. $url = substr($url, 0, $pos);
  96. }
  97. // 2 url forms
  98. // @routeName?key1=value1&key2=value2...
  99. // module/action?key1=value1&key2=value2...
  100. // first slash optional
  101. if ($url[0] == '/')
  102. {
  103. $url = substr($url, 1);
  104. }
  105. // routeName?
  106. if ($url[0] == '@')
  107. {
  108. $route = substr($url, 1);
  109. }
  110. else if (false !== strpos($url, '/'))
  111. {
  112. list($params['module'], $params['action']) = explode('/', $url);
  113. }
  114. else if (!$queryString)
  115. {
  116. $route = $givenUrl;
  117. }
  118. else
  119. {
  120. throw new InvalidArgumentException(sprintf('An internal URI must contain a module and an action (module/action) ("%s" given).', $givenUrl));
  121. }
  122. // split the query string
  123. if ($queryString)
  124. {
  125. $matched = preg_match_all('/
  126. ([^&=]+) # key
  127. = # =
  128. (.*?) # value
  129. (?:
  130. (?=&[^&=]+=) | $ # followed by another key= or the end of the string
  131. )
  132. /x', $queryString, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
  133. foreach ($matches as $match)
  134. {
  135. $params[urldecode($match[1][0])] = urldecode($match[2][0]);
  136. }
  137. // check that all string is matched
  138. if (!$matched)
  139. {
  140. throw new sfParseException(sprintf('Unable to parse query string "%s".', $queryString));
  141. }
  142. }
  143. return array($route, $params);
  144. }
  145. /**
  146. * Redirects the request to another URL.
  147. *
  148. * @param string $url An existing URL
  149. * @param int $delay A delay in seconds before redirecting. This is only needed on
  150. * browsers that do not support HTTP headers
  151. * @param int $statusCode The status code
  152. */
  153. public function redirect($url, $delay = 0, $statusCode = 302)
  154. {
  155. $url = $this->genUrl($url, true);
  156. if (sfConfig::get('sf_logging_enabled'))
  157. {
  158. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Redirect to "%s"', $url))));
  159. }
  160. // redirect
  161. $response = $this->context->getResponse();
  162. $response->clearHttpHeaders();
  163. $response->setStatusCode($statusCode);
  164. $response->setHttpHeader('Location', $url);
  165. $response->setContent(sprintf('<html><head><meta http-equiv="refresh" content="%d;url=%s"/></head></html>', $delay, htmlspecialchars($url, ENT_QUOTES, sfConfig::get('sf_charset'))));
  166. $response->send();
  167. }
  168. }