[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/controller/ -> sfWebController.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * (c) 2004-2006 Sean Kerr.
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  /**
  13   * sfWebController provides web specific methods to sfController such as, url redirection.
  14   *
  15   * @package    symfony
  16   * @subpackage controller
  17   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  18   * @author     Sean Kerr <skerr@mojavi.org>
  19   * @version    SVN: $Id: sfWebController.class.php 3204 2007-01-09 18:50:08Z fabien $
  20   */
  21  abstract class sfWebController extends sfController
  22  {
  23    /**
  24     * Generates an URL from an array of parameters.
  25     *
  26     * @param mixed   An associative array of URL parameters or an internal URI as a string.
  27     * @param boolean Whether to generate an absolute URL
  28     *
  29     * @return string A URL to a symfony resource
  30     */
  31    public function genUrl($parameters = array(), $absolute = false)
  32    {
  33      // absolute URL or symfony URL?
  34      if (!is_array($parameters) && preg_match('#^[a-z]+\://#', $parameters))
  35      {
  36        return $parameters;
  37      }
  38  
  39      if (!is_array($parameters) && $parameters == '#')
  40      {
  41        return $parameters;
  42      }
  43  
  44      $url = '';
  45      if (!($sf_no_script_name = sfConfig::get('sf_no_script_name')))
  46      {
  47        $url = $this->getContext()->getRequest()->getScriptName();
  48      }
  49      else if (($sf_relative_url_root = $this->getContext()->getRequest()->getRelativeUrlRoot()) && $sf_no_script_name)
  50      {
  51        $url = $sf_relative_url_root;
  52      }
  53  
  54      $route_name = '';
  55      $fragment = '';
  56  
  57      if (!is_array($parameters))
  58      {
  59        // strip fragment
  60        if (false !== ($pos = strpos($parameters, '#')))
  61        {
  62          $fragment = substr($parameters, $pos + 1);
  63          $parameters = substr($parameters, 0, $pos);
  64        }
  65  
  66        list($route_name, $parameters) = $this->convertUrlStringToParameters($parameters);
  67      }
  68  
  69      if (sfConfig::get('sf_url_format') == 'PATH')
  70      {
  71        // use PATH format
  72        $divider = '/';
  73        $equals  = '/';
  74        $querydiv = '/';
  75      }
  76      else
  77      {
  78        // use GET format
  79        $divider = ini_get('arg_separator.output');
  80        $equals  = '=';
  81        $querydiv = '?';
  82      }
  83  
  84      // default module
  85      if (!isset($parameters['module']))
  86      {
  87        $parameters['module'] = sfConfig::get('sf_default_module');
  88      }
  89  
  90      // default action
  91      if (!isset($parameters['action']))
  92      {
  93        $parameters['action'] = sfConfig::get('sf_default_action');
  94      }
  95  
  96      $r = sfRouting::getInstance();
  97      if ($r->hasRoutes() && $generated_url = $r->generate($route_name, $parameters, $querydiv, $divider, $equals))
  98      {
  99        $url .= $generated_url;
 100      }
 101      else
 102      {
 103        $query = http_build_query($parameters);
 104  
 105        if (sfConfig::get('sf_url_format') == 'PATH')
 106        {
 107          $query = strtr($query, ini_get('arg_separator.output').'=', '/');
 108        }
 109  
 110        $url .= $query;
 111      }
 112  
 113      if ($absolute)
 114      {
 115        $request = $this->getContext()->getRequest();
 116        $url = 'http'.($request->isSecure() ? 's' : '').'://'.$request->getHost().$url;
 117      }
 118  
 119      if ($fragment)
 120      {
 121        $url .= '#'.$fragment;
 122      }
 123  
 124      return $url;
 125    }
 126  
 127    /**
 128     * Converts an internal URI string to an array of parameters.
 129     *
 130     * @param string An internal URI
 131     *
 132     * @return array An array of parameters
 133     */
 134    public function convertUrlStringToParameters($url)
 135    {
 136      $params       = array();
 137      $query_string = '';
 138      $route_name   = '';
 139  
 140      // empty url?
 141      if (!$url)
 142      {
 143        $url = '/';
 144      }
 145  
 146      // we get the query string out of the url
 147      if ($pos = strpos($url, '?'))
 148      {
 149        $query_string = substr($url, $pos + 1);
 150        $url = substr($url, 0, $pos);
 151      }
 152  
 153      // 2 url forms
 154      // @route_name?key1=value1&key2=value2...
 155      // module/action?key1=value1&key2=value2...
 156  
 157      // first slash optional
 158      if ($url[0] == '/')
 159      {
 160        $url = substr($url, 1);
 161      }
 162  
 163  
 164      // route_name?
 165      if ($url[0] == '@')
 166      {
 167        $route_name = substr($url, 1);
 168      }
 169      else
 170      {
 171        $tmp = explode('/', $url);
 172  
 173        $params['module'] = $tmp[0];
 174        $params['action'] = isset($tmp[1]) ? $tmp[1] : sfConfig::get('sf_default_action');
 175      }
 176  
 177      // split the query string
 178      if ($query_string)
 179      {
 180        $matched = preg_match_all('/
 181          ([^&=]+)            # key
 182          =                   # =
 183          (.*?)               # value
 184          (?:
 185            (?=&[^&=]+=) | $   # followed by another key= or the end of the string
 186          )
 187        /x', $query_string, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
 188        foreach ($matches as $match)
 189        {
 190          $params[$match[1][0]] = $match[2][0];
 191        }
 192  
 193        // check that all string is matched
 194        if (!$matched)
 195        {
 196          throw new sfParseException(sprintf('Unable to parse query string "%s".', $query_string));
 197        }
 198      }
 199  
 200      return array($route_name, $params);
 201    }
 202  
 203    /**
 204     * Redirects the request to another URL.
 205     *
 206     * @param string An existing URL
 207     * @param int    A delay in seconds before redirecting. This is only needed on
 208     *               browsers that do not support HTTP headers
 209     * @param int    The status code
 210     */
 211    public function redirect($url, $delay = 0, $statusCode = 302)
 212    {
 213      $response = $this->getContext()->getResponse();
 214  
 215      // redirect
 216      $response->clearHttpHeaders();
 217      $response->setStatusCode($statusCode);
 218      $response->setHttpHeader('Location', $url);
 219      $response->setContent(sprintf('<html><head><meta http-equiv="refresh" content="%d;url=%s"/></head></html>', $delay, htmlentities($url, ENT_QUOTES, sfConfig::get('sf_charset'))));
 220  
 221      if (!sfConfig::get('sf_test'))
 222      {
 223        $response->sendHttpHeaders();
 224      }
 225      $response->sendContent();
 226    }
 227  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7