Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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: sfWebRequest.class.php 32729 2011-07-05 15:23:04Z www-data $
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
    foreach (array_filter(explode(',', $header)) as $value)
637
    {
638
      // Cut off any q-value that might come after a semi-colon
639
      if ($pos = strpos($value, ';'))
640
      {
641
        $q     = (float) trim(substr($value, strpos($value, '=') + 1));
642
        $value = substr($value, 0, $pos);
643
      }
644
      else
645
      {
646
        $q = 1;
647
      }
648
 
649
      if (0 < $q)
650
      {
651
        $values[trim($value)] = $q;
652
      }
653
    }
654
 
655
    arsort($values);
656
 
657
    return array_keys($values);
658
  }
659
 
660
  /**
661
   * Returns the array that contains all request information ($_SERVER or $_ENV).
662
   *
663
   * This information is stored in the path_info_array option.
664
   *
665
   * @return  array Path information
666
   */
667
  public function getPathInfoArray()
668
  {
669
    if (!$this->pathInfoArray)
670
    {
671
      // parse PATH_INFO
672
      switch ($this->options['path_info_array'])
673
      {
674
        case 'SERVER':
675
          $this->pathInfoArray =& $_SERVER;
676
          break;
677
 
678
        case 'ENV':
679
        default:
680
          $this->pathInfoArray =& $_ENV;
681
      }
682
    }
683
 
684
    return $this->pathInfoArray;
685
  }
686
 
687
  /**
688
   * Gets the mime type associated with the format.
689
   *
690
   * @param  string $format  The format
691
   *
692
   * @return string The associated mime type (null if not found)
693
   */
694
  public function getMimeType($format)
695
  {
696
    return isset($this->formats[$format]) ? $this->formats[$format][0] : null;
697
  }
698
 
699
  /**
700
   * Gets the format associated with the mime type.
701
   *
702
   * @param  string $mimeType  The associated mime type
703
   *
704
   * @return string The format (null if not found)
705
   */
706
  public function getFormat($mimeType)
707
  {
708
    foreach ($this->formats as $format => $mimeTypes)
709
    {
710
      if (in_array($mimeType, $mimeTypes))
711
      {
712
        return $format;
713
      }
714
    }
715
 
716
    return null;
717
  }
718
 
719
  /**
720
   * Associates a format with mime types.
721
   *
722
   * @param string       $format     The format
723
   * @param string|array $mimeTypes  The associated mime types (the preferred one must be the first as it will be used as the content type)
724
   */
725
  public function setFormat($format, $mimeTypes)
726
  {
727
    $this->formats[$format] = is_array($mimeTypes) ? $mimeTypes : array($mimeTypes);
728
  }
729
 
730
  /**
731
   * Sets the request format.
732
   *
733
   * @param string $format  The request format
734
   */
735
  public function setRequestFormat($format)
736
  {
737
    $this->format = $format;
738
  }
739
 
740
  /**
741
   * Gets the request format.
742
   *
743
   * Here is the process to determine the format:
744
   *
745
   *  * format defined by the user (with setRequestFormat())
746
   *  * sf_format request parameter
747
   *  * default format from factories
748
   *
749
   * @return string The request format
750
   */
751
  public function getRequestFormat()
752
  {
753
    if (null === $this->format)
754
    {
755
      $this->setRequestFormat($this->getParameter('sf_format', $this->options['default_format']));
756
    }
757
 
758
    return $this->format;
759
  }
760
 
761
  /**
762
   * Retrieves an array of files.
763
   *
764
   * @param  string $key  A key
765
   * @return array  An associative array of files
766
   */
767
  public function getFiles($key = null)
768
  {
769
    if (false === $this->fixedFileArray)
770
    {
771
      $this->fixedFileArray = self::convertFileInformation($_FILES);
772
    }
773
 
774
    return null === $key ? $this->fixedFileArray : (isset($this->fixedFileArray[$key]) ? $this->fixedFileArray[$key] : array());
775
  }
776
 
777
  /**
778
   * Converts uploaded file array to a format following the $_GET and $POST naming convention.
779
   *
780
   * It's safe to pass an already converted array, in which case this method just returns the original array unmodified.
781
   *
782
   * @param  array $taintedFiles An array representing uploaded file information
783
   *
784
   * @return array An array of re-ordered uploaded file information
785
   */
786
  static public function convertFileInformation(array $taintedFiles)
787
  {
788
    $files = array();
789
    foreach ($taintedFiles as $key => $data)
790
    {
791
      $files[$key] = self::fixPhpFilesArray($data);
792
    }
793
 
794
    return $files;
795
  }
796
 
797
  static protected function fixPhpFilesArray($data)
798
  {
799
    $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
800
    $keys = array_keys($data);
801
    sort($keys);
802
 
803
    if ($fileKeys != $keys || !isset($data['name']) || !is_array($data['name']))
804
    {
805
      return $data;
806
    }
807
 
808
    $files = $data;
809
    foreach ($fileKeys as $k)
810
    {
811
      unset($files[$k]);
812
    }
813
    foreach (array_keys($data['name']) as $key)
814
    {
815
      $files[$key] = self::fixPhpFilesArray(array(
816
        'error'    => $data['error'][$key],
817
        'name'     => $data['name'][$key],
818
        'type'     => $data['type'][$key],
819
        'tmp_name' => $data['tmp_name'][$key],
820
        'size'     => $data['size'][$key],
821
      ));
822
    }
823
 
824
    return $files;
825
  }
826
 
827
  /**
828
   * Returns the value of a GET parameter.
829
   *
830
   * @param  string $name     The GET parameter name
831
   * @param  string $default  The default value
832
   *
833
   * @return string The GET parameter value
834
   */
835
  public function getGetParameter($name, $default = null)
836
  {
837
    if (isset($this->getParameters[$name]))
838
    {
839
      return $this->getParameters[$name];
840
    }
841
    else
842
    {
843
      return sfToolkit::getArrayValueForPath($this->getParameters, $name, $default);
844
    }
845
  }
846
 
847
  /**
848
   * Returns the value of a POST parameter.
849
   *
850
   * @param  string $name     The POST parameter name
851
   * @param  string $default  The default value
852
   *
853
   * @return string The POST parameter value
854
   */
855
  public function getPostParameter($name, $default = null)
856
  {
857
    if (isset($this->postParameters[$name]))
858
    {
859
      return $this->postParameters[$name];
860
    }
861
    else
862
    {
863
      return sfToolkit::getArrayValueForPath($this->postParameters, $name, $default);
864
    }
865
  }
866
 
867
  /**
868
   * Returns the value of a parameter passed as a URL segment.
869
   *
870
   * @param  string $name     The parameter name
871
   * @param  string $default  The default value
872
   *
873
   * @return string The parameter value
874
   */
875
  public function getUrlParameter($name, $default = null)
876
  {
877
    if (isset($this->requestParameters[$name]))
878
    {
879
      return $this->requestParameters[$name];
880
    }
881
    else
882
    {
883
      return sfToolkit::getArrayValueForPath($this->requestParameters, $name, $default);
884
    }
885
  }
886
 
887
  /**
888
   * Returns the remote IP address that made the request.
889
   *
890
   * @return string The remote IP address
891
   */
892
  public function getRemoteAddress()
893
  {
894
    $pathInfo = $this->getPathInfoArray();
895
 
896
    return $pathInfo['REMOTE_ADDR'];
897
  }
898
 
899
  /**
900
   * Returns an array containing a list of IPs, the first being the client address
901
   * and the others the addresses of each proxy that passed the request. The address
902
   * for the last proxy can be retrieved via getRemoteAddress().
903
   *
904
   * This method returns null if no proxy passed this request. Note that some proxies
905
   * do not use this header, and act as if they were the client.
906
   *
907
   * @return string|null An array of IP from the client and the proxies that passed
908
   * the request, or null if no proxy was used.
909
   */
910
  public function getForwardedFor()
911
  {
912
    $pathInfo = $this->getPathInfoArray();
913
 
914
    if (empty($pathInfo['HTTP_X_FORWARDED_FOR']))
915
    {
916
      return null;
917
    }
918
 
919
    return explode(', ', $pathInfo['HTTP_X_FORWARDED_FOR']);
920
  }
921
 
922
  public function checkCSRFProtection()
923
  {
924
    $form = new BaseForm();
925
    $form->bind($form->isCSRFProtected() ? array($form->getCSRFFieldName() => $this->getParameter($form->getCSRFFieldName())) : array());
926
 
927
    if (!$form->isValid())
928
    {
929
      throw $form->getErrorSchema();
930
    }
931
  }
932
 
933
  /**
934
   * Parses the request parameters.
935
   *
936
   * This method notifies the request.filter_parameters event.
937
   *
938
   * @return array An array of request parameters.
939
   */
940
  protected function parseRequestParameters()
941
  {
942
    return $this->dispatcher->filter(new sfEvent($this, 'request.filter_parameters', $this->getRequestContext()), array())->getReturnValue();
943
  }
944
 
945
  /**
946
   * Returns the request context used.
947
   *
948
   * @return array An array of values representing the current request
949
   */
950
  public function getRequestContext()
951
  {
952
    return array(
953
      'path_info'   => $this->getPathInfo(),
954
      'prefix'      => $this->getPathInfoPrefix(),
955
      'method'      => $this->getMethod(),
956
      'format'      => $this->getRequestFormat(),
957
      'host'        => $this->getHost(),
958
      'is_secure'   => $this->isSecure(),
959
      'request_uri' => $this->getUri(),
960
    );
961
  }
962
 
963
  protected function fixParameters()
964
  {
965
    // move symfony parameters to attributes (parameters prefixed with _sf_)
966
    foreach ($this->parameterHolder->getAll() as $key => $value)
967
    {
968
      if (0 === stripos($key, '_sf_'))
969
      {
970
        $this->parameterHolder->remove($key);
971
        $this->setAttribute(substr($key, 1), $value);
972
      }
973
    }
974
  }
975
}