[ Index ] |
|
Code source de Symfony 1.0.0 |
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 * sfWebRequest class. 14 * 15 * This class manages web requests. It parses input from the request and store them as parameters. 16 * sfWebRequest is able to parse request with routing support enabled. 17 * 18 * @package symfony 19 * @subpackage request 20 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 21 * @author Sean Kerr <skerr@mojavi.org> 22 * @version SVN: $Id: sfWebRequest.class.php 3250 2007-01-12 20:09:11Z fabien $ 23 */ 24 class sfWebRequest extends sfRequest 25 { 26 /** 27 * A list of languages accepted by the browser. 28 * @var array 29 */ 30 protected $languages = null; 31 32 /** 33 * A list of charsets accepted by the browser 34 * @var array 35 */ 36 protected $charsets = null; 37 38 /** 39 * @var array List of content types accepted by the client. 40 */ 41 protected $acceptableContentTypes = null; 42 43 protected $pathInfoArray = null; 44 45 protected $relativeUrlRoot = null; 46 47 /** 48 * Retrieves an array of file information. 49 * 50 * @param string A file name 51 * 52 * @return array An associative array of file information, if the file exists, otherwise null 53 */ 54 public function getFile($name) 55 { 56 return ($this->hasFile($name) ? $this->getFileValues($name) : null); 57 } 58 59 /** 60 * Retrieves a file error. 61 * 62 * @param string A file name 63 * 64 * @return int One of the following error codes: 65 * 66 * - <b>UPLOAD_ERR_OK</b> (no error) 67 * - <b>UPLOAD_ERR_INI_SIZE</b> (the uploaded file exceeds the 68 * upload_max_filesize directive 69 * in php.ini) 70 * - <b>UPLOAD_ERR_FORM_SIZE</b> (the uploaded file exceeds the 71 * MAX_FILE_SIZE directive that 72 * was specified in the HTML form) 73 * - <b>UPLOAD_ERR_PARTIAL</b> (the uploaded file was only 74 * partially uploaded) 75 * - <b>UPLOAD_ERR_NO_FILE</b> (no file was uploaded) 76 */ 77 public function getFileError($name) 78 { 79 return ($this->hasFile($name) ? $this->getFileValue($name, 'error') : UPLOAD_ERR_NO_FILE); 80 } 81 82 /** 83 * Retrieves a file name. 84 * 85 * @param string A file nam. 86 * 87 * @return string A file name, if the file exists, otherwise null 88 */ 89 public function getFileName($name) 90 { 91 return ($this->hasFile($name) ? $this->getFileValue($name, 'name') : null); 92 } 93 94 /** 95 * Retrieves an array of file names. 96 * 97 * @return array An indexed array of file names 98 */ 99 public function getFileNames() 100 { 101 return array_keys($_FILES); 102 } 103 104 /** 105 * Retrieves an array of files. 106 * 107 * @return array An associative array of files 108 */ 109 public function getFiles() 110 { 111 return $_FILES; 112 } 113 114 /** 115 * Retrieves a file path. 116 * 117 * @param string A file name 118 * 119 * @return string A file path, if the file exists, otherwise null 120 */ 121 public function getFilePath($name) 122 { 123 return ($this->hasFile($name) ? $this->getFileValue($name, 'tmp_name') : null); 124 } 125 126 /** 127 * Retrieve a file size. 128 * 129 * @param string A file name 130 * 131 * @return int A file size, if the file exists, otherwise null 132 */ 133 public function getFileSize($name) 134 { 135 return ($this->hasFile($name) ? $this->getFileValue($name, 'size') : null); 136 } 137 138 /** 139 * Retrieves a file type. 140 * 141 * This may not be accurate. This is the mime-type sent by the browser 142 * during the upload. 143 * 144 * @param string A file name 145 * 146 * @return string A file type, if the file exists, otherwise null 147 */ 148 public function getFileType($name) 149 { 150 return ($this->hasFile($name) ? $this->getFileValue($name, 'type') : null); 151 } 152 153 /** 154 * Indicates whether or not a file exists. 155 * 156 * @param string A file name 157 * 158 * @return boolean true, if the file exists, otherwise false 159 */ 160 public function hasFile($name) 161 { 162 if (preg_match('/^(.+?)\[(.+?)\]$/', $name, $match)) 163 { 164 return isset($_FILES[$match[1]]['name'][$match[2]]); 165 } 166 else 167 { 168 return isset($_FILES[$name]); 169 } 170 } 171 172 /** 173 * Indicates whether or not a file error exists. 174 * 175 * @param string A file name 176 * 177 * @return boolean true, if the file error exists, otherwise false 178 */ 179 public function hasFileError($name) 180 { 181 return ($this->hasFile($name) ? ($this->getFileValue($name, 'error') != UPLOAD_ERR_OK) : false); 182 } 183 184 /** 185 * Indicates whether or not any file errors occured. 186 * 187 * @return boolean true, if any file errors occured, otherwise false 188 */ 189 public function hasFileErrors() 190 { 191 foreach ($this->getFileNames() as $name) 192 { 193 if ($this->hasFileError($name) === true) 194 { 195 return true; 196 } 197 } 198 199 return false; 200 } 201 202 /** 203 * Indicates whether or not any files exist. 204 * 205 * @return boolean true, if any files exist, otherwise false 206 */ 207 public function hasFiles() 208 { 209 return (count($_FILES) > 0); 210 } 211 212 /** 213 * Retrieves a file value. 214 * 215 * @param string A file name 216 * @param string Value to search in the file 217 * 218 * @return string File value 219 */ 220 public function getFileValue($name, $key) 221 { 222 if (preg_match('/^(.+?)\[(.+?)\]$/', $name, $match)) 223 { 224 return $_FILES[$match[1]][$key][$match[2]]; 225 } 226 else 227 { 228 return $_FILES[$name][$key]; 229 } 230 } 231 232 /** 233 * Retrieves all the values from a file. 234 * 235 * @param string A file name 236 * 237 * @return array Associative list of the file values 238 */ 239 public function getFileValues($name) 240 { 241 if (preg_match('/^(.+?)\[(.+?)\]$/', $name, $match)) 242 { 243 return array( 244 'name' => $_FILES[$match[1]]['name'][$match[2]], 245 'type' => $_FILES[$match[1]]['type'][$match[2]], 246 'tmp_name' => $_FILES[$match[1]]['tmp_name'][$match[2]], 247 'error' => $_FILES[$match[1]]['error'][$match[2]], 248 'size' => $_FILES[$match[1]]['size'][$match[2]], 249 ); 250 } 251 else 252 { 253 return $_FILES[$name]; 254 } 255 } 256 257 /** 258 * Retrieves an extension for a given file. 259 * 260 * @param string A file name 261 * 262 * @return string Extension for the file 263 */ 264 public function getFileExtension($name) 265 { 266 $fileType = $this->getFileType($name); 267 268 if (!$fileType) 269 { 270 return '.bin'; 271 } 272 273 $mimeTypes = unserialize(file_get_contents(sfConfig::get('sf_symfony_data_dir').'/data/mime_types.dat')); 274 275 return isset($mimeTypes[$fileType]) ? '.'.$mimeTypes[$fileType] : '.bin'; 276 } 277 278 /** 279 * Initializes this sfRequest. 280 * 281 * @param sfContext A sfContext instance 282 * @param array An associative array of initialization parameters 283 * @param array An associative array of initialization attributes 284 * 285 * @return boolean true, if initialization completes successfully, otherwise false 286 * 287 * @throws <b>sfInitializationException</b> If an error occurs while initializing this Request 288 */ 289 public function initialize($context, $parameters = array(), $attributes = array()) 290 { 291 parent::initialize($context, $parameters, $attributes); 292 293 if (isset($_SERVER['REQUEST_METHOD'])) 294 { 295 switch ($_SERVER['REQUEST_METHOD']) 296 { 297 case 'GET': 298 $this->setMethod(self::GET); 299 break; 300 301 case 'POST': 302 $this->setMethod(self::POST); 303 break; 304 305 case 'PUT': 306 $this->setMethod(self::PUT); 307 break; 308 309 case 'DELETE': 310 $this->setMethod(self::DELETE); 311 break; 312 313 case 'HEAD': 314 $this->setMethod(self::HEAD); 315 break; 316 317 default: 318 $this->setMethod(self::GET); 319 } 320 } 321 else 322 { 323 // set the default method 324 $this->setMethod(self::GET); 325 } 326 327 // load parameters from GET/PATH_INFO/POST 328 $this->loadParameters(); 329 } 330 331 /** 332 * Returns the array that contains all request information ($_SERVER or $_ENV). 333 * 334 * This information is stored in the [sf_path_info_array] constant. 335 * 336 * @return array Path information 337 */ 338 protected function getPathInfoArray() 339 { 340 if (!$this->pathInfoArray) 341 { 342 // parse PATH_INFO 343 switch (sfConfig::get('sf_path_info_array')) 344 { 345 case 'SERVER': 346 $this->pathInfoArray =& $_SERVER; 347 break; 348 349 case 'ENV': 350 default: 351 $this->pathInfoArray =& $_ENV; 352 } 353 } 354 355 return $this->pathInfoArray; 356 } 357 358 /** 359 * Retrieves the uniform resource identifier for the current web request. 360 * 361 * @return string Unified resource identifier 362 */ 363 public function getUri() 364 { 365 $pathArray = $this->getPathInfoArray(); 366 367 if ($this->isAbsUri()) 368 { 369 return $pathArray['REQUEST_URI']; 370 } 371 372 return $this->getUriPrefix().$pathArray['REQUEST_URI']; 373 } 374 375 /** 376 * See if the client is using absolute uri 377 * 378 * @return boolean true, if is absolute uri otherwise false 379 */ 380 public function isAbsUri() 381 { 382 $pathArray = $this->getPathInfoArray(); 383 384 return preg_match('/^http/', $pathArray['REQUEST_URI']); 385 } 386 387 /** 388 * Returns Uri prefix, including protocol, hostname and server port. 389 * 390 * @return string Uniform resource identifier prefix 391 */ 392 public function getUriPrefix() 393 { 394 $pathArray = $this->getPathInfoArray(); 395 if ($this->isSecure()) 396 { 397 $standardPort = '443'; 398 $proto = 'https'; 399 } 400 else 401 { 402 $standardPort = '80'; 403 $proto = 'http'; 404 } 405 406 $port = $pathArray['SERVER_PORT'] == $standardPort || !$pathArray['SERVER_PORT'] ? '' : ':'.$pathArray['SERVER_PORT']; 407 408 return $proto.'://'.$pathArray['SERVER_NAME'].$port; 409 } 410 411 /** 412 * Retrieves the path info for the current web request. 413 * 414 * @return string Path info 415 */ 416 public function getPathInfo() 417 { 418 $pathInfo = ''; 419 420 $pathArray = $this->getPathInfoArray(); 421 422 // simulate PATH_INFO if needed 423 $sf_path_info_key = sfConfig::get('sf_path_info_key'); 424 if (!isset($pathArray[$sf_path_info_key]) || !$pathArray[$sf_path_info_key]) 425 { 426 if (isset($pathArray['REQUEST_URI'])) 427 { 428 $script_name = $this->getScriptName(); 429 $uri_prefix = $this->isAbsUri() ? $this->getUriPrefix() : ''; 430 $pathInfo = preg_replace('/^'.preg_quote($uri_prefix, '/').'/','',$pathArray['REQUEST_URI']); 431 $pathInfo = preg_replace('/^'.preg_quote($script_name, '/').'/', '', $pathInfo); 432 $prefix_name = preg_replace('#/[^/]+$#', '', $script_name); 433 $pathInfo = preg_replace('/^'.preg_quote($prefix_name, '/').'/', '', $pathInfo); 434 $pathInfo = preg_replace('/'.preg_quote($pathArray['QUERY_STRING'], '/').'$/', '', $pathInfo); 435 } 436 } 437 else 438 { 439 $pathInfo = $pathArray[$sf_path_info_key]; 440 if ($sf_relative_url_root = $this->getRelativeUrlRoot()) 441 { 442 $pathInfo = preg_replace('/^'.str_replace('/', '\\/', $sf_relative_url_root).'\//', '', $pathInfo); 443 } 444 } 445 446 // for IIS 447 if (isset($_SERVER['SERVER_SOFTWARE']) && false !== stripos($_SERVER['SERVER_SOFTWARE'], 'iis') && $pos = stripos($pathInfo, '.php')) 448 { 449 $pathInfo = substr($pathInfo, $pos + 4); 450 } 451 452 if (!$pathInfo) 453 { 454 $pathInfo = '/'; 455 } 456 457 return $pathInfo; 458 } 459 460 /** 461 * Loads GET, PATH_INFO and POST data into the parameter list. 462 * 463 */ 464 protected function loadParameters() 465 { 466 // merge GET parameters 467 if (get_magic_quotes_gpc()) 468 { 469 $_GET = sfToolkit::stripslashesDeep($_GET); 470 } 471 $this->getParameterHolder()->addByRef($_GET); 472 473 $pathInfo = $this->getPathInfo(); 474 if ($pathInfo) 475 { 476 // routing map defined? 477 $r = sfRouting::getInstance(); 478 if ($r->hasRoutes()) 479 { 480 $results = $r->parse($pathInfo); 481 if ($results !== null) 482 { 483 $this->getParameterHolder()->addByRef($results); 484 } 485 else 486 { 487 $this->setParameter('module', sfConfig::get('sf_error_404_module')); 488 $this->setParameter('action', sfConfig::get('sf_error_404_action')); 489 } 490 } 491 else 492 { 493 $array = explode('/', trim($pathInfo, '/')); 494 $count = count($array); 495 496 for ($i = 0; $i < $count; $i++) 497 { 498 // see if there's a value associated with this parameter, 499 // if not we're done with path data 500 if ($count > ($i + 1)) 501 { 502 $this->getParameterHolder()->setByRef($array[$i], $array[++$i]); 503 } 504 } 505 } 506 } 507 508 // merge POST parameters 509 if (get_magic_quotes_gpc()) 510 { 511 $_POST = sfToolkit::stripslashesDeep((array) $_POST); 512 } 513 $this->getParameterHolder()->addByRef($_POST); 514 515 // move symfony parameters in a protected namespace (parameters prefixed with _sf_) 516 foreach ($this->getParameterHolder()->getAll() as $key => $value) 517 { 518 if (0 === stripos($key, '_sf_')) 519 { 520 $this->getParameterHolder()->remove($key); 521 $this->setParameter($key, $value, 'symfony/request/sfWebRequest'); 522 unset($_GET[$key]); 523 } 524 } 525 526 if (sfConfig::get('sf_logging_enabled')) 527 { 528 $this->getContext()->getLogger()->info(sprintf('{sfRequest} request parameters %s', str_replace("\n", '', var_export($this->getParameterHolder()->getAll(), true)))); 529 } 530 } 531 532 /** 533 * Moves an uploaded file. 534 * 535 * @param string A file name 536 * @param string An absolute filesystem path to where you would like the 537 * file moved. This includes the new filename as well, since 538 * uploaded files are stored with random names 539 * @param int The octal mode to use for the new file 540 * @param boolean Indicates that we should make the directory before moving the file 541 * @param int The octal mode to use when creating the directory 542 * 543 * @return boolean true, if the file was moved, otherwise false 544 * 545 * @throws <b>sfFileException</b> If a major error occurs while attempting to move the file 546 */ 547 public function moveFile($name, $file, $fileMode = 0666, $create = true, $dirMode = 0777) 548 { 549 if ($this->hasFile($name) && $this->getFileValue($name, 'error') == UPLOAD_ERR_OK && $this->getFileValue($name, 'size') > 0) 550 { 551 // get our directory path from the destination filename 552 $directory = dirname($file); 553 554 if (!is_readable($directory)) 555 { 556 $fmode = 0777; 557 558 if ($create && !@mkdir($directory, $dirMode, true)) 559 { 560 // failed to create the directory 561 $error = 'Failed to create file upload directory "%s"'; 562 $error = sprintf($error, $directory); 563 564 throw new sfFileException($error); 565 } 566 567 // chmod the directory since it doesn't seem to work on 568 // recursive paths 569 @chmod($directory, $dirMode); 570 } 571 else if (!is_dir($directory)) 572 { 573 // the directory path exists but it's not a directory 574 $error = 'File upload path "%s" exists, but is not a directory'; 575 $error = sprintf($error, $directory); 576 577 throw new sfFileException($error); 578 } 579 else if (!is_writable($directory)) 580 { 581 // the directory isn't writable 582 $error = 'File upload path "%s" is not writable'; 583 $error = sprintf($error, $directory); 584 585 throw new sfFileException($error); 586 } 587 588 if (@move_uploaded_file($this->getFileValue($name, 'tmp_name'), $file)) 589 { 590 // chmod our file 591 @chmod($file, $fileMode); 592 593 return true; 594 } 595 } 596 597 return false; 598 } 599 600 /** 601 * Returns referer. 602 * 603 * @return string 604 */ 605 public function getReferer() 606 { 607 $pathArray = $this->getPathInfoArray(); 608 609 return isset($pathArray['HTTP_REFERER']) ? $pathArray['HTTP_REFERER'] : ''; 610 } 611 612 /** 613 * Returns current host name. 614 * 615 * @return string 616 */ 617 public function getHost() 618 { 619 $pathArray = $this->getPathInfoArray(); 620 621 return isset($pathArray['HTTP_X_FORWARDED_HOST']) ? $pathArray['HTTP_X_FORWARDED_HOST'] : (isset($pathArray['HTTP_HOST']) ? $pathArray['HTTP_HOST'] : ''); 622 } 623 624 /** 625 * Returns current script name. 626 * 627 * @return string 628 */ 629 public function getScriptName() 630 { 631 $pathArray = $this->getPathInfoArray(); 632 633 return isset($pathArray['SCRIPT_NAME']) ? $pathArray['SCRIPT_NAME'] : (isset($pathArray['ORIG_SCRIPT_NAME']) ? $pathArray['ORIG_SCRIPT_NAME'] : ''); 634 } 635 636 /** 637 * Returns request method. 638 * 639 * @return string 640 */ 641 public function getMethodName() 642 { 643 $pathArray = $this->getPathInfoArray(); 644 645 return isset($pathArray['REQUEST_METHOD']) ? $pathArray['REQUEST_METHOD'] : 'GET'; 646 } 647 648 /** 649 * Gets a list of languages acceptable by the client browser 650 * 651 * @return array Languages ordered in the user browser preferences 652 */ 653 public function getLanguages() 654 { 655 if ($this->languages) 656 { 657 return $this->languages; 658 } 659 660 if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) 661 { 662 return array(); 663 } 664 665 $languages = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT_LANGUAGE']); 666 foreach ($languages as $lang) 667 { 668 if (strstr($lang, '-')) 669 { 670 $codes = explode('-', $lang); 671 if ($codes[0] == 'i') 672 { 673 // Language not listed in ISO 639 that are not variants 674 // of any listed language, which can be registerd with the 675 // i-prefix, such as i-cherokee 676 if (count($codes) > 1) 677 { 678 $lang = $codes[1]; 679 } 680 } 681 else 682 { 683 for ($i = 0, $max = count($codes); $i < $max; $i++) 684 { 685 if ($i == 0) 686 { 687 $lang = strtolower($codes[0]); 688 } 689 else 690 { 691 $lang .= '_'.strtoupper($codes[$i]); 692 } 693 } 694 } 695 } 696 697 $this->languages[] = $lang; 698 } 699 700 return $this->languages; 701 } 702 703 /** 704 * Gets a list of charsets acceptable by the client browser. 705 * 706 * @return array List of charsets in preferable order 707 */ 708 public function getCharsets() 709 { 710 if ($this->charsets) 711 { 712 return $this->charsets; 713 } 714 715 if (!isset($_SERVER['HTTP_ACCEPT_CHARSET'])) 716 { 717 return array(); 718 } 719 720 $this->charsets = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT_CHARSET']); 721 722 return $this->charsets; 723 } 724 725 /** 726 * Gets a list of content types acceptable by the client browser 727 * 728 * @return array Languages ordered in the user browser preferences 729 */ 730 public function getAcceptableContentTypes() 731 { 732 if ($this->acceptableContentTypes) 733 { 734 return $this->acceptableContentTypes; 735 } 736 737 if (!isset($_SERVER['HTTP_ACCEPT'])) 738 { 739 return array(); 740 } 741 742 $this->acceptableContentTypes = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT']); 743 744 return $this->acceptableContentTypes; 745 } 746 747 /** 748 * Returns true id the request is a XMLHttpRequest (via prototype 'HTTP_X_REQUESTED_WITH' header). 749 * 750 * @return boolean 751 */ 752 public function isXmlHttpRequest() 753 { 754 return ($this->getHttpHeader('X_REQUESTED_WITH') == 'XMLHttpRequest'); 755 } 756 757 public function getHttpHeader($name, $prefix = 'http') 758 { 759 if ($prefix) 760 { 761 $prefix = strtoupper($prefix).'_'; 762 } 763 764 $name = $prefix.strtoupper(strtr($name, '-', '_')); 765 766 $pathArray = $this->getPathInfoArray(); 767 768 return isset($pathArray[$name]) ? stripslashes($pathArray[$name]) : null; 769 } 770 771 /** 772 * Gets a cookie value. 773 * 774 * @return mixed 775 */ 776 public function getCookie($name, $defaultValue = null) 777 { 778 $retval = $defaultValue; 779 780 if (isset($_COOKIE[$name])) 781 { 782 $retval = get_magic_quotes_gpc() ? stripslashes($_COOKIE[$name]) : $_COOKIE[$name]; 783 } 784 785 return $retval; 786 } 787 788 /** 789 * Returns true if the current request is secure (HTTPS protocol). 790 * 791 * @return boolean 792 */ 793 public function isSecure() 794 { 795 $pathArray = $this->getPathInfoArray(); 796 797 return ( 798 (isset($pathArray['HTTPS']) && strtolower($pathArray['HTTPS']) == 'on') 799 || 800 (isset($pathArray['HTTP_X_FORWARDED_PROTO']) && strtolower($pathArray['HTTP_X_FORWARDED_PROTO']) == 'https') 801 ); 802 } 803 804 /** 805 * Retrieves relative root url. 806 * 807 * @return string URL 808 */ 809 public function getRelativeUrlRoot() 810 { 811 if ($this->relativeUrlRoot === null) 812 { 813 $this->relativeUrlRoot = sfConfig::get('sf_relative_url_root', preg_replace('#/[^/]+\.php5?$#', '', $this->getScriptName())); 814 } 815 816 return $this->relativeUrlRoot; 817 } 818 819 /** 820 * Sets the relative root url for the current web request. 821 * 822 * @param string Value for the url 823 */ 824 public function setRelativeUrlRoot($value) 825 { 826 $this->relativeUrlRoot = $value; 827 } 828 829 /** 830 * Executes the shutdown procedure. 831 * 832 */ 833 public function shutdown() 834 { 835 } 836 837 /** 838 * Splits an HTTP header for the current web request. 839 * 840 * @param string Header to split 841 */ 842 public function splitHttpAcceptHeader($header) 843 { 844 $values = array(); 845 foreach (array_filter(explode(',', $header)) as $value) 846 { 847 // Cut off any q-value that might come after a semi-colon 848 if ($pos = strpos($value, ';')) 849 { 850 $q = (float) trim(substr($value, $pos + 3)); 851 $value = trim(substr($value, 0, $pos)); 852 } 853 else 854 { 855 $q = 1; 856 } 857 858 $values[$value] = $q; 859 } 860 861 arsort($values); 862 863 return array_keys($values); 864 } 865 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |