Development

/branches/1.4/lib/request/sfWebRequest.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/request/sfWebRequest.class.php

Revision 33544, 25.1 kB (checked in by fabien, 2 years ago)

[1.4] fixed sfWebRequest::splitHttpAcceptHeader incorrect result order (closes #10069, patch by Keri Henare)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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 <sean@code-box.org>
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  * sfWebRequest class.
14  *
15  * This class manages web requests. It parses input from the request and store them as parameters.
16  *
17  * @package    symfony
18  * @subpackage request
19  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20  * @author     Sean Kerr <sean@code-box.org>
21  * @version    SVN: $Id$
22  */
23 class sfWebRequest extends sfRequest
24 {
25   const
26     PORT_HTTP  = 80,
27     PORT_HTTPS = 443;
28  
29   protected
30     $languages              = null,
31     $charsets               = null,
32     $acceptableContentTypes = null,
33     $pathInfoArray          = null,
34     $relativeUrlRoot        = null,
35     $getParameters          = null,
36     $postParameters         = null,
37     $requestParameters      = null,
38     $formats                = array(),
39     $format                 = null,
40     $fixedFileArray         = false;
41
42   /**
43    * Initializes this sfRequest.
44    *
45    * Available options:
46    *
47    *  * formats:           The list of supported format and their associated mime-types
48    *  * path_info_key:     The path info key (default to PATH_INFO)
49    *  * path_info_array:   The path info array (default to SERVER)
50    *  * relative_url_root: The relative URL root
51    *  * http_port:         The port to use for HTTP requests
52    *  * https_port:        The port to use for HTTPS requests
53    *
54    * @param  sfEventDispatcher $dispatcher  An sfEventDispatcher instance
55    * @param  array             $parameters  An associative array of initialization parameters
56    * @param  array             $attributes  An associative array of initialization attributes
57    * @param  array             $options     An associative array of options
58    *
59    * @return bool true, if initialization completes successfully, otherwise false
60    *
61    * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfRequest
62    *
63    * @see sfRequest
64    */
65   public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
66   {
67     $options = array_merge(array(
68       'path_info_key'   => 'PATH_INFO',
69       'path_info_array' => 'SERVER',
70       'http_port'       => null,
71       'https_port'      => null,
72       'default_format'  => null, // to maintain bc
73     ), $options);
74     parent::initialize($dispatcher, $parameters, $attributes, $options);
75
76     // GET parameters
77     $this->getParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_GET) : $_GET;
78     $this->parameterHolder->add($this->getParameters);
79
80     $postParameters = $_POST;
81
82     if (isset($_SERVER['REQUEST_METHOD']))
83     {
84       switch ($_SERVER['REQUEST_METHOD'])
85       {
86         case 'GET':
87           $this->setMethod(self::GET);
88           break;
89
90         case 'POST':
91           if (isset($_POST['sf_method']))
92           {
93             $this->setMethod(strtoupper($_POST['sf_method']));
94             unset($postParameters['sf_method']);
95           }
96           elseif (isset($_GET['sf_method']))
97           {
98             $this->setMethod(strtoupper($_GET['sf_method']));
99             unset($_GET['sf_method']);
100           }
101           else
102           {
103             $this->setMethod(self::POST);
104           }
105           $this->parameterHolder->remove('sf_method');
106           break;
107
108         case 'PUT':
109           $this->setMethod(self::PUT);
110           if ('application/x-www-form-urlencoded' === $this->getContentType())
111           {
112             parse_str($this->getContent(), $postParameters);
113           }
114           break;
115
116         case 'DELETE':
117           $this->setMethod(self::DELETE);
118           if ('application/x-www-form-urlencoded' === $this->getContentType())
119           {
120             parse_str($this->getContent(), $postParameters);
121           }
122           break;
123
124         case 'HEAD':
125           $this->setMethod(self::HEAD);
126           break;
127
128         default:
129           $this->setMethod(self::GET);
130       }
131     }
132     else
133     {
134       // set the default method
135       $this->setMethod(self::GET);
136     }
137
138     $this->postParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($postParameters) : $postParameters;
139     $this->parameterHolder->add($this->postParameters);
140
141     if (isset($this->options['formats']))
142     {
143       foreach ($this->options['formats'] as $format => $mimeTypes)
144       {
145         $this->setFormat($format, $mimeTypes);
146       }
147     }
148
149     // additional parameters
150     $this->requestParameters = $this->parseRequestParameters();
151     $this->parameterHolder->add($this->requestParameters);
152
153     $this->fixParameters();
154   }
155
156   /**
157    * Returns the content type of the current request.
158    *
159    * @param  Boolean $trimmed If false the full Content-Type header will be returned
160    *
161    * @return string
162    */
163   public function getContentType($trim = true)
164   {
165     $contentType = $this->getHttpHeader('Content-Type', null);
166
167     if ($trim && false !== $pos = strpos($contentType, ';'))
168     {
169       $contentType = substr($contentType, 0, $pos);
170     }
171
172     return $contentType;
173   }
174
175   /**
176    * Retrieves the uniform resource identifier for the current web request.
177    *
178    * @return string Unified resource identifier
179    */
180   public function getUri()
181   {
182     $pathArray = $this->getPathInfoArray();
183
184     // for IIS with rewrite module (IIFR, ISAPI Rewrite, ...)
185     if ('HTTP_X_REWRITE_URL' == $this->options['path_info_key'])
186     {
187       $uri = isset($pathArray['HTTP_X_REWRITE_URL']) ? $pathArray['HTTP_X_REWRITE_URL'] : '';
188     }
189     else
190     {
191       $uri = isset($pathArray['REQUEST_URI']) ? $pathArray['REQUEST_URI'] : '';
192     }
193
194     return $this->isAbsUri() ? $uri : $this->getUriPrefix().$uri;
195   }
196
197   /**
198    * See if the client is using absolute uri
199    *
200    * @return boolean true, if is absolute uri otherwise false
201    */
202   public function isAbsUri()
203   {
204     $pathArray = $this->getPathInfoArray();
205
206     return isset($pathArray['REQUEST_URI']) ? preg_match('/^http/', $pathArray['REQUEST_URI']) : false;
207   }
208
209   /**
210    * Returns Uri prefix, including protocol, hostname and server port.
211    *
212    * @return string Uniform resource identifier prefix
213    */
214   public function getUriPrefix()
215   {
216     $pathArray = $this->getPathInfoArray();
217     $secure = $this->isSecure();
218
219     $protocol = $secure ? 'https' : 'http';
220     $host = $this->getHost();
221     $port = null;
222
223     // extract port from host or environment variable
224     if (false !== strpos($host, ':'))
225     {
226       list($host, $port) = explode(':', $host, 2);
227     }
228     else if (isset($this->options[$protocol.'_port']))
229     {
230       $port = $this->options[$protocol.'_port'];
231     }
232     else if (isset($pathArray['SERVER_PORT']))
233     {
234       $port = $pathArray['SERVER_PORT'];
235     }
236
237     // cleanup the port based on whether the current request is forwarded from
238     // a secure one and whether the introspected port matches the standard one
239     if ($this->isForwardedSecure())
240     {
241       $port = isset($this->options['https_port']) && self::PORT_HTTPS != $this->options['https_port'] ? $this->options['https_port'] : null;
242     }
243     elseif (($secure && self::PORT_HTTPS == $port) || (!$secure && self::PORT_HTTP == $port))
244     {
245       $port = null;
246     }
247
248     return sprintf('%s://%s%s', $protocol, $host, $port ? ':'.$port : '');
249   }
250
251   /**
252    * Retrieves the path info for the current web request.
253    *
254    * @return string Path info
255    */
256   public function getPathInfo()
257   {
258     $pathInfo = '';
259
260     $pathArray = $this->getPathInfoArray();
261
262     // simulate PATH_INFO if needed
263     $sf_path_info_key = $this->options['path_info_key'];
264     if (!isset($pathArray[$sf_path_info_key]) || !$pathArray[$sf_path_info_key])
265     {
266       if (isset($pathArray['REQUEST_URI']))
267       {
268         $qs = isset($pathArray['QUERY_STRING']) ? $pathArray['QUERY_STRING'] : '';
269         $script_name = $this->getScriptName();
270         $uri_prefix = $this->isAbsUri() ? $this->getUriPrefix() : '';
271         $pathInfo = preg_replace('/^'.preg_quote($uri_prefix, '/').'/','',$pathArray['REQUEST_URI']);
272         $pathInfo = preg_replace('/^'.preg_quote($script_name, '/').'/', '', $pathInfo);
273         $prefix_name = preg_replace('#/[^/]+$#', '', $script_name);
274         $pathInfo = preg_replace('/^'.preg_quote($prefix_name, '/').'/', '', $pathInfo);
275         $pathInfo = preg_replace('/\??'.preg_quote($qs, '/').'$/', '', $pathInfo);
276       }
277     }
278     else
279     {
280       $pathInfo = $pathArray[$sf_path_info_key];
281       if ($relativeUrlRoot = $this->getRelativeUrlRoot())
282       {
283         $pathInfo = preg_replace('/^'.str_replace('/', '\\/', $relativeUrlRoot).'\//', '', $pathInfo);
284       }
285     }
286
287     // for IIS
288     if (isset($_SERVER['SERVER_SOFTWARE']) && false !== stripos($_SERVER['SERVER_SOFTWARE'], 'iis') && $pos = stripos($pathInfo, '.php'))
289     {
290       $pathInfo = substr($pathInfo, $pos + 4);
291     }
292
293     if (!$pathInfo)
294     {
295       $pathInfo = '/';
296     }
297
298     return $pathInfo;
299   }
300
301   public function getPathInfoPrefix()
302   {
303     $prefix = $this->getRelativeUrlRoot();
304
305     if (!isset($this->options['no_script_name']) || !$this->options['no_script_name'])
306     {
307       $scriptName = $this->getScriptName();
308       $prefix = null === $prefix ? $scriptName : $prefix.'/'.basename($scriptName);
309     }
310
311     return $prefix;
312   }
313
314   public function getGetParameters()
315   {
316     return $this->getParameters;
317   }
318
319   public function getPostParameters()
320   {
321     return $this->postParameters;
322   }
323
324   public function getRequestParameters()
325   {
326     return $this->requestParameters;
327   }
328
329   public function addRequestParameters($parameters)
330   {
331     $this->requestParameters = array_merge($this->requestParameters, $parameters);
332     $this->getParameterHolder()->add($parameters);
333
334     $this->fixParameters();
335   }
336
337   /**
338    * Returns referer.
339    *
340    * @return string
341    */
342   public function getReferer()
343   {
344     $pathArray = $this->getPathInfoArray();
345
346     return isset($pathArray['HTTP_REFERER']) ? $pathArray['HTTP_REFERER'] : '';
347   }
348
349   /**
350    * Returns current host name.
351    *
352    * @return string
353    */
354   public function getHost()
355   {
356     $pathArray = $this->getPathInfoArray();
357
358     if (isset($pathArray['HTTP_X_FORWARDED_HOST']))
359     {
360       $elements = explode(',', $pathArray['HTTP_X_FORWARDED_HOST']);
361
362       return trim($elements[count($elements) - 1]);
363     }
364     else
365     {
366       return isset($pathArray['HTTP_HOST']) ? $pathArray['HTTP_HOST'] : '';
367     }
368   }
369
370   /**
371    * Returns current script name.
372    *
373    * @return string
374    */
375   public function getScriptName()
376   {
377     $pathArray = $this->getPathInfoArray();
378
379     return isset($pathArray['SCRIPT_NAME']) ? $pathArray['SCRIPT_NAME'] : (isset($pathArray['ORIG_SCRIPT_NAME']) ? $pathArray['ORIG_SCRIPT_NAME'] : '');
380   }
381
382   /**
383    * Checks if the request method is the given one.
384    *
385    * @param  string $method  The method name
386    *
387    * @return bool true if the current method is the given one, false otherwise
388    */
389   public function isMethod($method)
390   {
391     return strtoupper($method) == $this->getMethod();
392   }
393
394   /**
395    * Returns the preferred culture for the current request.
396    *
397    * @param  array  $cultures  An array of ordered cultures available
398    *
399    * @return string The preferred culture
400    */
401   public function getPreferredCulture(array $cultures = null)
402   {
403     $preferredCultures = $this->getLanguages();
404
405     if (null === $cultures)
406     {
407       return isset($preferredCultures[0]) ? $preferredCultures[0] : null;
408     }
409
410     if (!$preferredCultures)
411     {
412       return $cultures[0];
413     }
414
415     $preferredCultures = array_values(array_intersect($preferredCultures, $cultures));
416
417     return isset($preferredCultures[0]) ? $preferredCultures[0] : $cultures[0];
418   }
419
420   /**
421    * Gets a list of languages acceptable by the client browser
422    *
423    * @return array Languages ordered in the user browser preferences
424    */
425   public function getLanguages()
426   {
427     if ($this->languages)
428     {
429       return $this->languages;
430     }
431
432     if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
433     {
434       return array();
435     }
436
437     $languages = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT_LANGUAGE']);
438     foreach ($languages as $lang)
439     {
440       if (strstr($lang, '-'))
441       {
442         $codes = explode('-', $lang);
443         if ($codes[0] == 'i')
444         {
445           // Language not listed in ISO 639 that are not variants
446           // of any listed language, which can be registerd with the
447           // i-prefix, such as i-cherokee
448           if (count($codes) > 1)
449           {
450             $lang = $codes[1];
451           }
452         }
453         else
454         {
455           for ($i = 0, $max = count($codes); $i < $max; $i++)
456           {
457             if ($i == 0)
458             {
459               $lang = strtolower($codes[0]);
460             }
461             else
462             {
463               $lang .= '_'.strtoupper($codes[$i]);
464             }
465           }
466         }
467       }
468
469       $this->languages[] = $lang;
470     }
471
472     return $this->languages;
473   }
474
475   /**
476    * Gets a list of charsets acceptable by the client browser.
477    *
478    * @return array List of charsets in preferable order
479    */
480   public function getCharsets()
481   {
482     if ($this->charsets)
483     {
484       return $this->charsets;
485     }
486
487     if (!isset($_SERVER['HTTP_ACCEPT_CHARSET']))
488     {
489       return array();
490     }
491
492     $this->charsets = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT_CHARSET']);
493
494     return $this->charsets;
495   }
496
497   /**
498    * Gets a list of content types acceptable by the client browser
499    *
500    * @return array Languages ordered in the user browser preferences
501    */
502   public function getAcceptableContentTypes()
503   {
504     if ($this->acceptableContentTypes)
505     {
506       return $this->acceptableContentTypes;
507     }
508
509     if (!isset($_SERVER['HTTP_ACCEPT']))
510     {
511       return array();
512     }
513
514     $this->acceptableContentTypes = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT']);
515
516     return $this->acceptableContentTypes;
517   }
518
519   /**
520    * Returns true if the request is a XMLHttpRequest.
521    *
522    * It works if your JavaScript library set an X-Requested-With HTTP header.
523    * Works with Prototype, Mootools, jQuery, and perhaps others.
524    *
525    * @return bool true if the request is an XMLHttpRequest, false otherwise
526    */
527   public function isXmlHttpRequest()
528   {
529     return ($this->getHttpHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
530   }
531
532   public function getHttpHeader($name, $prefix = 'http')
533   {
534     if ($prefix)
535     {
536       $prefix = strtoupper($prefix).'_';
537     }
538
539     $name = $prefix.strtoupper(strtr($name, '-', '_'));
540
541     $pathArray = $this->getPathInfoArray();
542
543     return isset($pathArray[$name]) ? sfToolkit::stripslashesDeep($pathArray[$name]) : null;
544   }
545
546   /**
547    * Gets a cookie value.
548    *
549    * @param  string $name          Cookie name
550    * @param  string $defaultValue  Default value returned when no cookie with given name is found
551    *
552    * @return mixed
553    */
554   public function getCookie($name, $defaultValue = null)
555   {
556     $retval = $defaultValue;
557
558     if (isset($_COOKIE[$name]))
559     {
560       $retval = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_COOKIE[$name]) : $_COOKIE[$name];
561     }
562
563     return $retval;
564   }
565
566   /**
567    * Returns true if the current or forwarded request is secure (HTTPS protocol).
568    *
569    * @return boolean
570    */
571   public function isSecure()
572   {
573     $pathArray = $this->getPathInfoArray();
574
575     return
576       (isset($pathArray['HTTPS']) && ('on' == strtolower($pathArray['HTTPS']) || 1 == $pathArray['HTTPS']))
577       ||
578       (isset($pathArray['HTTP_SSL_HTTPS']) && ('on' == strtolower($pathArray['HTTP_SSL_HTTPS']) || 1 == $pathArray['HTTP_SSL_HTTPS']))
579       ||
580       $this->isForwardedSecure()
581     ;
582   }
583
584   /**
585    * Returns true if the current request is forwarded from a request that is secure.
586    *
587    * @return boolean
588    */
589   protected function isForwardedSecure()
590   {
591     $pathArray = $this->getPathInfoArray();
592
593     return isset($pathArray['HTTP_X_FORWARDED_PROTO']) && 'https' == strtolower($pathArray['HTTP_X_FORWARDED_PROTO']);
594   }
595
596   /**
597    * Retrieves relative root url.
598    *
599    * @return string URL
600    */
601   public function getRelativeUrlRoot()
602   {
603     if (null === $this->relativeUrlRoot)
604     {
605       if (!isset($this->options['relative_url_root']))
606       {
607         $this->relativeUrlRoot = preg_replace('#/[^/]+\.php5?$#', '', $this->getScriptName());
608       }
609       else
610       {
611         $this->relativeUrlRoot = $this->options['relative_url_root'];
612       }
613     }
614
615     return $this->relativeUrlRoot;
616   }
617
618   /**
619    * Sets the relative root url for the current web request.
620    *
621    * @param string $value  Value for the url
622    */
623   public function setRelativeUrlRoot($value)
624   {
625     $this->relativeUrlRoot = $value;
626   }
627
628   /**
629    * Splits an HTTP header for the current web request.
630    *
631    * @param string $header  Header to split
632    */
633   public function splitHttpAcceptHeader($header)
634   {
635     $values = array();
636     $groups = array();
637     foreach (array_filter(explode(',', $header)) as $value)
638     {
639       // Cut off any q-value that might come after a semi-colon
640       if ($pos = strpos($value, ';'))
641       {
642         $q     = trim(substr($value, strpos($value, '=') + 1));
643         $value = substr($value, 0, $pos);
644       }
645       else
646       {
647         $q = 1;
648       }
649
650       $groups[$q][] = $value;
651     }
652
653     krsort($groups);
654
655     foreach ($groups as $q => $items) {
656       if (0 < $q) {
657         foreach ($items as $value) {
658           $values[] = trim($value);
659         }
660       }
661     }
662
663     return $values;
664   }
665
666   /**
667    * Returns the array that contains all request information ($_SERVER or $_ENV).
668    *
669    * This information is stored in the path_info_array option.
670    *
671    * @return  array Path information
672    */
673   public function getPathInfoArray()
674   {
675     if (!$this->pathInfoArray)
676     {
677       // parse PATH_INFO
678       switch ($this->options['path_info_array'])
679       {
680         case 'SERVER':
681           $this->pathInfoArray =& $_SERVER;
682           break;
683
684         case 'ENV':
685         default:
686           $this->pathInfoArray =& $_ENV;
687       }
688     }
689
690     return $this->pathInfoArray;
691   }
692
693   /**
694    * Gets the mime type associated with the format.
695    *
696    * @param  string $format  The format
697    *
698    * @return string The associated mime type (null if not found)
699    */
700   public function getMimeType($format)
701   {
702     return isset($this->formats[$format]) ? $this->formats[$format][0] : null;
703   }
704
705   /**
706    * Gets the format associated with the mime type.
707    *
708    * @param  string $mimeType  The associated mime type
709    *
710    * @return string The format (null if not found)
711    */
712   public function getFormat($mimeType)
713   {
714     foreach ($this->formats as $format => $mimeTypes)
715     {
716       if (in_array($mimeType, $mimeTypes))
717       {
718         return $format;
719       }
720     }
721
722     return null;
723   }
724
725   /**
726    * Associates a format with mime types.
727    *
728    * @param string       $format     The format
729    * @param string|array $mimeTypes  The associated mime types (the preferred one must be the first as it will be used as the content type)
730    */
731   public function setFormat($format, $mimeTypes)
732   {
733     $this->formats[$format] = is_array($mimeTypes) ? $mimeTypes : array($mimeTypes);
734   }
735
736   /**
737    * Sets the request format.
738    *
739    * @param string $format  The request format
740    */
741   public function setRequestFormat($format)
742   {
743     $this->format = $format;
744   }
745
746   /**
747    * Gets the request format.
748    *
749    * Here is the process to determine the format:
750    *
751    *  * format defined by the user (with setRequestFormat())
752    *  * sf_format request parameter
753    *  * default format from factories
754    *
755    * @return string The request format
756    */
757   public function getRequestFormat()
758   {
759     if (null === $this->format)
760     {
761       $this->setRequestFormat($this->getParameter('sf_format', $this->options['default_format']));
762     }
763
764     return $this->format;
765   }
766
767   /**
768    * Retrieves an array of files.
769    *
770    * @param  string $key  A key
771    * @return array  An associative array of files
772    */
773   public function getFiles($key = null)
774   {
775     if (false === $this->fixedFileArray)
776     {
777       $this->fixedFileArray = self::convertFileInformation($_FILES);
778     }
779
780     return null === $key ? $this->fixedFileArray : (isset($this->fixedFileArray[$key]) ? $this->fixedFileArray[$key] : array());
781   }
782
783   /**
784    * Converts uploaded file array to a format following the $_GET and $POST naming convention.
785    *
786    * It's safe to pass an already converted array, in which case this method just returns the original array unmodified.
787    *
788    * @param  array $taintedFiles An array representing uploaded file information
789    *
790    * @return array An array of re-ordered uploaded file information
791    */
792   static public function convertFileInformation(array $taintedFiles)
793   {
794     $files = array();
795     foreach ($taintedFiles as $key => $data)
796     {
797       $files[$key] = self::fixPhpFilesArray($data);
798     }
799
800     return $files;
801   }
802
803   static protected function fixPhpFilesArray($data)
804   {
805     $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
806     $keys = array_keys($data);
807     sort($keys);
808
809     if ($fileKeys != $keys || !isset($data['name']) || !is_array($data['name']))
810     {
811       return $data;
812     }
813
814     $files = $data;
815     foreach ($fileKeys as $k)
816     {
817       unset($files[$k]);
818     }
819     foreach (array_keys($data['name']) as $key)
820     {
821       $files[$key] = self::fixPhpFilesArray(array(
822         'error'    => $data['error'][$key],
823         'name'     => $data['name'][$key],
824         'type'     => $data['type'][$key],
825         'tmp_name' => $data['tmp_name'][$key],
826         'size'     => $data['size'][$key],
827       ));
828     }
829
830     return $files;
831   }
832
833   /**
834    * Returns the value of a GET parameter.
835    *
836    * @param  string $name     The GET parameter name
837    * @param  string $default  The default value
838    *
839    * @return string The GET parameter value
840    */
841   public function getGetParameter($name, $default = null)
842   {
843     if (isset($this->getParameters[$name]))
844     {
845       return $this->getParameters[$name];
846     }
847     else
848     {
849       return sfToolkit::getArrayValueForPath($this->getParameters, $name, $default);
850     }
851   }
852
853   /**
854    * Returns the value of a POST parameter.
855    *
856    * @param  string $name     The POST parameter name
857    * @param  string $default  The default value
858    *
859    * @return string The POST parameter value
860    */
861   public function getPostParameter($name, $default = null)
862   {
863     if (isset($this->postParameters[$name]))
864     {
865       return $this->postParameters[$name];
866     }
867     else
868     {
869       return sfToolkit::getArrayValueForPath($this->postParameters, $name, $default);
870     }
871   }
872
873   /**
874    * Returns the value of a parameter passed as a URL segment.
875    *
876    * @param  string $name     The parameter name
877    * @param  string $default  The default value
878    *
879    * @return string The parameter value
880    */
881   public function getUrlParameter($name, $default = null)
882   {
883     if (isset($this->requestParameters[$name]))
884     {
885       return $this->requestParameters[$name];
886     }
887     else
888     {
889       return sfToolkit::getArrayValueForPath($this->requestParameters, $name, $default);
890     }
891   }
892
893   /**
894    * Returns the remote IP address that made the request.
895    *
896    * @return string The remote IP address
897    */
898   public function getRemoteAddress()
899   {
900     $pathInfo = $this->getPathInfoArray();
901
902     return $pathInfo['REMOTE_ADDR'];
903   }
904
905   /**
906    * Returns an array containing a list of IPs, the first being the client address
907    * and the others the addresses of each proxy that passed the request. The address
908    * for the last proxy can be retrieved via getRemoteAddress().
909    *
910    * This method returns null if no proxy passed this request. Note that some proxies
911    * do not use this header, and act as if they were the client.
912    *
913    * @return string|null An array of IP from the client and the proxies that passed
914    * the request, or null if no proxy was used.
915    */
916   public function getForwardedFor()
917   {
918     $pathInfo = $this->getPathInfoArray();
919
920     if (empty($pathInfo['HTTP_X_FORWARDED_FOR']))
921     {
922       return null;
923     }
924
925     return explode(', ', $pathInfo['HTTP_X_FORWARDED_FOR']);
926   }
927
928   public function checkCSRFProtection()
929   {
930     $form = new BaseForm();
931     $form->bind($form->isCSRFProtected() ? array($form->getCSRFFieldName() => $this->getParameter($form->getCSRFFieldName())) : array());
932
933     if (!$form->isValid())
934     {
935       throw $form->getErrorSchema();
936     }
937   }
938
939   /**
940    * Parses the request parameters.
941    *
942    * This method notifies the request.filter_parameters event.
943    *
944    * @return array An array of request parameters.
945    */
946   protected function parseRequestParameters()
947   {
948     return $this->dispatcher->filter(new sfEvent($this, 'request.filter_parameters', $this->getRequestContext()), array())->getReturnValue();
949   }
950
951   /**
952    * Returns the request context used.
953    *
954    * @return array An array of values representing the current request
955    */
956   public function getRequestContext()
957   {
958     return array(
959       'path_info'   => $this->getPathInfo(),
960       'prefix'      => $this->getPathInfoPrefix(),
961       'method'      => $this->getMethod(),
962       'format'      => $this->getRequestFormat(),
963       'host'        => $this->getHost(),
964       'is_secure'   => $this->isSecure(),
965       'request_uri' => $this->getUri(),
966     );
967   }
968
969   protected function fixParameters()
970   {
971     // move symfony parameters to attributes (parameters prefixed with _sf_)
972     foreach ($this->parameterHolder->getAll() as $key => $value)
973     {
974       if (0 === stripos($key, '_sf_'))
975       {
976         $this->parameterHolder->remove($key);
977         $this->setAttribute(substr($key, 1), $value);
978       }
979     }
980   }
981 }
982
Note: See TracBrowser for help on using the browser.