123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- abstract class sfWebController extends sfController
- {
-
- public function genUrl($parameters = array(), $absolute = false)
- {
-
- if (is_string($parameters) && preg_match('#^[a-z][a-z0-9\+.\-]*\://#i', $parameters))
- {
- return $parameters;
- }
-
- if (is_string($parameters) && '/' == $parameters[0])
- {
- return $parameters;
- }
- if (is_string($parameters) && $parameters == '#')
- {
- return $parameters;
- }
- $route = '';
- $fragment = '';
- if (is_string($parameters))
- {
-
- if (false !== ($pos = strpos($parameters, '#')))
- {
- $fragment = substr($parameters, $pos + 1);
- $parameters = substr($parameters, 0, $pos);
- }
- list($route, $parameters) = $this->convertUrlStringToParameters($parameters);
- }
- else if (is_array($parameters))
- {
- if (isset($parameters['sf_route']))
- {
- $route = $parameters['sf_route'];
- unset($parameters['sf_route']);
- }
- }
-
- $url = $this->context->getRouting()->generate($route, $parameters, $absolute);
- if ($fragment)
- {
- $url .= '#'.$fragment;
- }
- return $url;
- }
-
- public function convertUrlStringToParameters($url)
- {
- $givenUrl = $url;
- $params = array();
- $queryString = '';
- $route = '';
-
- if (!$url)
- {
- $url = '/';
- }
-
- if ($pos = strpos($url, '?'))
- {
- $queryString = substr($url, $pos + 1);
- $url = substr($url, 0, $pos);
- }
-
-
-
-
- if ($url[0] == '/')
- {
- $url = substr($url, 1);
- }
-
- if ($url[0] == '@')
- {
- $route = substr($url, 1);
- }
- else if (false !== strpos($url, '/'))
- {
- list($params['module'], $params['action']) = explode('/', $url);
- }
- else if (!$queryString)
- {
- $route = $givenUrl;
- }
- else
- {
- throw new InvalidArgumentException(sprintf('An internal URI must contain a module and an action (module/action) ("%s" given).', $givenUrl));
- }
-
- if ($queryString)
- {
- $matched = preg_match_all('/
- ([^&=]+) # key
- = # =
- (.*?) # value
- (?:
- (?=&[^&=]+=) | $ # followed by another key= or the end of the string
- )
- /x', $queryString, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
- foreach ($matches as $match)
- {
- $params[urldecode($match[1][0])] = urldecode($match[2][0]);
- }
-
- if (!$matched)
- {
- throw new sfParseException(sprintf('Unable to parse query string "%s".', $queryString));
- }
- }
- return array($route, $params);
- }
-
- public function redirect($url, $delay = 0, $statusCode = 302)
- {
- $url = $this->genUrl($url, true);
- if (sfConfig::get('sf_logging_enabled'))
- {
- $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Redirect to "%s"', $url))));
- }
-
- $response = $this->context->getResponse();
- $response->clearHttpHeaders();
- $response->setStatusCode($statusCode);
- $response->setHttpHeader('Location', $url);
- $response->setContent(sprintf('<html><head><meta http-equiv="refresh" content="%d;url=%s"/></head></html>', $delay, htmlspecialchars($url, ENT_QUOTES, sfConfig::get('sf_charset'))));
- $response->send();
- }
- }
|