PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/oiclient/data/symfony/controller/sfWebController.class.php

http://openirudi.googlecode.com/
PHP | 203 lines | 121 code | 27 blank | 55 comment | 23 complexity | 64b4e1c7c82cc1b417e4aa32568c489b MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  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 9956 2008-06-28 07:21:42Z 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_array($parameters) && preg_match('#^[a-z][a-z0-9\+.\-]*\://#i', $parameters))
  33. {
  34. return $parameters;
  35. }
  36. if (!is_array($parameters) && $parameters == '#')
  37. {
  38. return $parameters;
  39. }
  40. $url = $this->context->getRequest()->getRelativeUrlRoot();
  41. if (!sfConfig::get('sf_no_script_name'))
  42. {
  43. $scriptName = $this->context->getRequest()->getScriptName();
  44. $url = is_null($url) ? $scriptName : $url.'/'.basename($scriptName);
  45. }
  46. $route_name = '';
  47. $fragment = '';
  48. if (!is_array($parameters))
  49. {
  50. // strip fragment
  51. if (false !== ($pos = strpos($parameters, '#')))
  52. {
  53. $fragment = substr($parameters, $pos + 1);
  54. $parameters = substr($parameters, 0, $pos);
  55. }
  56. list($route_name, $parameters) = $this->convertUrlStringToParameters($parameters);
  57. }
  58. if (sfConfig::get('sf_url_format') == 'PATH')
  59. {
  60. // use PATH format
  61. $divider = '/';
  62. $equals = '/';
  63. $querydiv = '/';
  64. }
  65. else
  66. {
  67. // use GET format
  68. $divider = ini_get('arg_separator.output');
  69. $equals = '=';
  70. $querydiv = '?';
  71. }
  72. // routing to generate path
  73. $url .= $this->context->getRouting()->generate($route_name, $parameters, $querydiv, $divider, $equals);
  74. if ($absolute)
  75. {
  76. $request = $this->context->getRequest();
  77. $url = 'http'.($request->isSecure() ? 's' : '').'://'.$request->getHost().$url;
  78. }
  79. if ($fragment)
  80. {
  81. $url .= '#'.$fragment;
  82. }
  83. return $url;
  84. }
  85. /**
  86. * Converts an internal URI string to an array of parameters.
  87. *
  88. * @param string $url An internal URI
  89. *
  90. * @return array An array of parameters
  91. */
  92. public function convertUrlStringToParameters($url)
  93. {
  94. $givenUrl = $url;
  95. $params = array();
  96. $query_string = '';
  97. $route_name = '';
  98. // empty url?
  99. if (!$url)
  100. {
  101. $url = '/';
  102. }
  103. // we get the query string out of the url
  104. if ($pos = strpos($url, '?'))
  105. {
  106. $query_string = substr($url, $pos + 1);
  107. $url = substr($url, 0, $pos);
  108. }
  109. // 2 url forms
  110. // @route_name?key1=value1&key2=value2...
  111. // module/action?key1=value1&key2=value2...
  112. // first slash optional
  113. if ($url[0] == '/')
  114. {
  115. $url = substr($url, 1);
  116. }
  117. // route_name?
  118. if ($url[0] == '@')
  119. {
  120. $route_name = substr($url, 1);
  121. }
  122. else if (false !== strpos($url, '/'))
  123. {
  124. list($params['module'], $params['action']) = explode('/', $url);
  125. }
  126. else
  127. {
  128. throw new InvalidArgumentException(sprintf('An internal URI must contain a module and an action (module/action) ("%s" given).', $givenUrl));
  129. }
  130. // split the query string
  131. if ($query_string)
  132. {
  133. $matched = preg_match_all('/
  134. ([^&=]+) # key
  135. = # =
  136. (.*?) # value
  137. (?:
  138. (?=&[^&=]+=) | $ # followed by another key= or the end of the string
  139. )
  140. /x', $query_string, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
  141. foreach ($matches as $match)
  142. {
  143. $params[urldecode($match[1][0])] = urldecode($match[2][0]);
  144. }
  145. // check that all string is matched
  146. if (!$matched)
  147. {
  148. throw new sfParseException(sprintf('Unable to parse query string "%s".', $query_string));
  149. }
  150. }
  151. return array($route_name, $params);
  152. }
  153. /**
  154. * Redirects the request to another URL.
  155. *
  156. * @param string $url An existing URL
  157. * @param int $delay A delay in seconds before redirecting. This is only needed on
  158. * browsers that do not support HTTP headers
  159. * @param int $statusCode The status code
  160. */
  161. public function redirect($url, $delay = 0, $statusCode = 302)
  162. {
  163. $url = $this->genUrl($url, true);
  164. if (sfConfig::get('sf_logging_enabled'))
  165. {
  166. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Redirect to "%s"', $url))));
  167. }
  168. // redirect
  169. $response = $this->context->getResponse();
  170. $response->clearHttpHeaders();
  171. $response->setStatusCode($statusCode);
  172. $response->setHttpHeader('Location', $url);
  173. $response->setContent(sprintf('<html><head><meta http-equiv="refresh" content="%d;url=%s"/></head></html>', $delay, htmlspecialchars($url, ENT_QUOTES, sfConfig::get('sf_charset'))));
  174. $response->send();
  175. }
  176. }