PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/controller/sfWebController.class.php

https://github.com/theodo/symfony1.0-backports
PHP | 227 lines | 141 code | 30 blank | 56 comment | 29 complexity | ba56542a40f263c5e1c3fd1bf941443f MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, BSD-3-Clause
  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$
  18. */
  19. abstract class sfWebController extends sfController
  20. {
  21. /**
  22. * Generates an URL from an array of parameters.
  23. *
  24. * @param mixed An associative array of URL parameters or an internal URI as a string.
  25. * @param boolean 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_array($parameters) && preg_match('#^[a-z]+\://#', $parameters))
  33. {
  34. return $parameters;
  35. }
  36. if (!is_array($parameters) && $parameters == '#')
  37. {
  38. return $parameters;
  39. }
  40. $url = '';
  41. if (!sfConfig::get('sf_no_script_name'))
  42. {
  43. $url = $this->getContext()->getRequest()->getScriptName();
  44. }
  45. else if ($sf_relative_url_root = $this->getContext()->getRequest()->getRelativeUrlRoot())
  46. {
  47. $url = $sf_relative_url_root;
  48. }
  49. $route_name = '';
  50. $fragment = '';
  51. if (!is_array($parameters))
  52. {
  53. // strip fragment
  54. if (false !== ($pos = strpos($parameters, '#')))
  55. {
  56. $fragment = substr($parameters, $pos + 1);
  57. $parameters = substr($parameters, 0, $pos);
  58. }
  59. list($route_name, $parameters) = $this->convertUrlStringToParameters($parameters);
  60. }
  61. if (sfConfig::get('sf_url_format') == 'PATH')
  62. {
  63. // use PATH format
  64. $divider = '/';
  65. $equals = '/';
  66. $querydiv = '/';
  67. }
  68. else
  69. {
  70. // use GET format
  71. $divider = '&';
  72. $equals = '=';
  73. $querydiv = '?';
  74. }
  75. // default module
  76. if (!isset($parameters['module']))
  77. {
  78. $parameters['module'] = sfConfig::get('sf_default_module');
  79. }
  80. // default action
  81. if (!isset($parameters['action']))
  82. {
  83. $parameters['action'] = sfConfig::get('sf_default_action');
  84. }
  85. $r = sfRouting::getInstance();
  86. if ($r->hasRoutes() && $generated_url = $r->generate($route_name, $parameters, $querydiv, $divider, $equals))
  87. {
  88. $url .= $generated_url;
  89. }
  90. else
  91. {
  92. $query = http_build_query($parameters);
  93. if (sfConfig::get('sf_url_format') == 'PATH')
  94. {
  95. $query = strtr($query, ini_get('arg_separator.output').'=', '/');
  96. }
  97. $url .= $query;
  98. }
  99. if ($absolute)
  100. {
  101. $request = $this->getContext()->getRequest();
  102. $url = 'http'.($request->isSecure() ? 's' : '').'://'.$request->getHost().$url;
  103. }
  104. if ($fragment)
  105. {
  106. $url .= '#'.$fragment;
  107. }
  108. return $url;
  109. }
  110. /**
  111. * Converts an internal URI string to an array of parameters.
  112. *
  113. * @param string An internal URI
  114. *
  115. * @return array An array of parameters
  116. */
  117. public function convertUrlStringToParameters($url)
  118. {
  119. $params = array();
  120. $query_string = '';
  121. $route_name = '';
  122. // empty url?
  123. if (!$url)
  124. {
  125. $url = '/';
  126. }
  127. // we get the query string out of the url
  128. if ($pos = strpos($url, '?'))
  129. {
  130. $query_string = substr($url, $pos + 1);
  131. $url = substr($url, 0, $pos);
  132. }
  133. // 2 url forms
  134. // @route_name?key1=value1&key2=value2...
  135. // module/action?key1=value1&key2=value2...
  136. // first slash optional
  137. if ($url[0] == '/')
  138. {
  139. $url = substr($url, 1);
  140. }
  141. // route_name?
  142. if ($url[0] == '@')
  143. {
  144. $route_name = substr($url, 1);
  145. }
  146. else
  147. {
  148. $tmp = explode('/', $url);
  149. $params['module'] = $tmp[0];
  150. $params['action'] = isset($tmp[1]) ? $tmp[1] : sfConfig::get('sf_default_action');
  151. }
  152. // split the query string
  153. if ($query_string)
  154. {
  155. $matched = preg_match_all('/
  156. ([^&=]+) # key
  157. = # =
  158. (.*?) # value
  159. (?:
  160. (?=&[^&=]+=) | $ # followed by another key= or the end of the string
  161. )
  162. /x', $query_string, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
  163. foreach ($matches as $match)
  164. {
  165. $params[urldecode($match[1][0])] = urldecode($match[2][0]);
  166. }
  167. // check that all string is matched
  168. if (!$matched)
  169. {
  170. throw new sfParseException(sprintf('Unable to parse query string "%s".', $query_string));
  171. }
  172. }
  173. return array($route_name, $params);
  174. }
  175. /**
  176. * Redirects the request to another URL.
  177. *
  178. * @param string An existing URL
  179. * @param int A delay in seconds before redirecting. This is only needed on
  180. * browsers that do not support HTTP headers
  181. * @param int The status code
  182. */
  183. public function redirect($url, $delay = 0, $statusCode = 302)
  184. {
  185. $response = $this->getContext()->getResponse();
  186. // redirect
  187. $response->clearHttpHeaders();
  188. $response->setStatusCode($statusCode);
  189. $response->setHttpHeader('Location', $url);
  190. $response->setContent(sprintf('<html><head><meta http-equiv="refresh" content="%d;url=%s"/></head></html>', $delay, htmlentities($url, ENT_QUOTES, sfConfig::get('sf_charset'))));
  191. if (!sfConfig::get('sf_test'))
  192. {
  193. $response->sendHttpHeaders();
  194. }
  195. $response->sendContent();
  196. }
  197. }