[ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * THttpRequest, THttpCookie, THttpCookieCollection, TUri class file 4 * 5 * @author Qiang Xue <qiang.xue@gmail.com> 6 * @link http://www.pradosoft.com/ 7 * @copyright Copyright © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: THttpRequest.php 1539 2006-12-02 19:20:40Z xue $ 10 * @package System.Web 11 */ 12 13 Prado::using('System.Web.TUrlManager'); 14 15 /** 16 * THttpRequest class 17 * 18 * THttpRequest provides storage and access scheme for user request sent via HTTP. 19 * It also encapsulates a uniform way to parse and construct URLs. 20 * 21 * User post data can be retrieved from THttpRequest by using it like an associative array. 22 * For example, to test if a user supplies a variable named 'param1', you can use, 23 * <code> 24 * if(isset($request['param1'])) ... 25 * // equivalent to: 26 * // if($request->contains('param1')) ... 27 * </code> 28 * To get the value of 'param1', use, 29 * <code> 30 * $value=$request['param1']; 31 * // equivalent to: 32 * // $value=$request->itemAt('param1'); 33 * </code> 34 * To traverse the user post data, use 35 * <code> 36 * foreach($request as $name=>$value) ... 37 * </code> 38 * Note, POST and GET variables are merged together in THttpRequest. 39 * If a variable name appears in both POST and GET data, then POST data 40 * takes precedence. 41 * 42 * To construct a URL that can be recognized by Prado, use {@link constructUrl()}. 43 * The format of the recognizable URLs is determined according to 44 * {@link setUrlManager UrlManager}. By default, the following two formats 45 * are recognized: 46 * <code> 47 * /index.php?ServiceID=ServiceParameter&Name1=Value1&Name2=Value2 48 * /index.php/ServiceID,ServiceParameter/Name1,Value1/Name2,Value2 49 * </code> 50 * The first format is called 'Get' while the second 'Path', which is specified 51 * via {@link setUrlFormat UrlFormat}. For advanced users who want to use 52 * their own URL formats, they can write customized URL management modules 53 * and install the managers as application modules and set {@link setUrlManager UrlManager}. 54 * 55 * The ServiceID in the above URLs is as defined in the application configuration 56 * (e.g. the default page service's service ID is 'page'). 57 * As a consequence, your GET variable names should not conflict with the service 58 * IDs that your application supports. 59 * 60 * THttpRequest also provides the cookies sent by the user, user information such 61 * as his browser capabilities, accepted languages, etc. 62 * 63 * By default, THttpRequest is registered with {@link TApplication} as the 64 * request module. It can be accessed via {@link TApplication::getRequest()}. 65 * 66 * @author Qiang Xue <qiang.xue@gmail.com> 67 * @version $Id: THttpRequest.php 1539 2006-12-02 19:20:40Z xue $ 68 * @package System.Web 69 * @since 3.0 70 */ 71 class THttpRequest extends TApplicationComponent implements IteratorAggregate,ArrayAccess,Countable,IModule 72 { 73 /** 74 * @var TUrlManager the URL manager module 75 */ 76 private $_urlManager=null; 77 /** 78 * @var string the ID of the URL manager module 79 */ 80 private $_urlManagerID=''; 81 /** 82 * @var string Separator used to separate GET variable name and value when URL format is Path. 83 */ 84 private $_separator=','; 85 /** 86 * @var string requested service ID 87 */ 88 private $_serviceID=null; 89 /** 90 * @var string requested service parameter 91 */ 92 private $_serviceParam=null; 93 /** 94 * @var THttpCookieCollection cookies sent from user 95 */ 96 private $_cookies=null; 97 /** 98 * @var string requested URI (URL w/o host info) 99 */ 100 private $_requestUri; 101 /** 102 * @var string path info of URL 103 */ 104 private $_pathInfo; 105 /** 106 * @var boolean whether the session ID should be kept in cookie only 107 */ 108 private $_cookieOnly=false; 109 private $_urlFormat=THttpRequestUrlFormat::Get; 110 private $_services; 111 private $_requestResolved=false; 112 private $_enableCookieValidation=false; 113 /** 114 * @var string request URL 115 */ 116 private $_url=null; 117 118 /** 119 * @var string module id 120 */ 121 private $_id; 122 123 /** 124 * @var array contains all request variables 125 */ 126 private $_items=array(); 127 128 /** 129 * @return string id of this module 130 */ 131 public function getID() 132 { 133 return $this->_id; 134 } 135 136 /** 137 * @param string id of this module 138 */ 139 public function setID($value) 140 { 141 $this->_id=$value; 142 } 143 144 /** 145 * Initializes the module. 146 * This method is required by IModule and is invoked by application. 147 * @param TXmlElement module configuration 148 */ 149 public function init($config) 150 { 151 if(empty($this->_urlManagerID)) 152 { 153 $this->_urlManager=new TUrlManager; 154 $this->_urlManager->init(null); 155 } 156 else 157 { 158 $this->_urlManager=$this->getApplication()->getModule($this->_urlManagerID); 159 if($this->_urlManager===null) 160 throw new TConfigurationException('httprequest_urlmanager_inexist',$this->_urlManagerID); 161 if(!($this->_urlManager instanceof TUrlManager)) 162 throw new TConfigurationException('httprequest_urlmanager_invalid',$this->_urlManagerID); 163 } 164 165 // Fill in default request info when the script is run in command line 166 if(php_sapi_name()==='cli') 167 { 168 $_SERVER['REMOTE_ADDR']='127.0.0.1'; 169 $_SERVER['REQUEST_METHOD']='GET'; 170 $_SERVER['SERVER_NAME']='localhost'; 171 $_SERVER['SERVER_PORT']=80; 172 $_SERVER['HTTP_USER_AGENT']=''; 173 } 174 175 $this->_cookieOnly=(int)ini_get('session.use_cookies') && (int)ini_get('session.use_only_cookies'); 176 177 // Info about server variables: 178 // PHP_SELF contains real URI (w/ path info, w/o query string) 179 // SCRIPT_NAME is the real URI for the requested script (w/o path info and query string) 180 // QUERY_STRING is the string following the '?' in the ur (eg the a=x part in http://foo/bar?a=x) 181 // REQUEST_URI contains the URI part entered in the browser address bar 182 // SCRIPT_FILENAME is the file path to the executing script 183 if(isset($_SERVER['REQUEST_URI'])) 184 $this->_requestUri=$_SERVER['REQUEST_URI']; 185 else // TBD: in this case, SCRIPT_NAME need to be escaped 186 $this->_requestUri=$_SERVER['SCRIPT_NAME'].(empty($_SERVER['QUERY_STRING'])?'':'?'.$_SERVER['QUERY_STRING']); 187 188 if(isset($_SERVER['PATH_INFO'])) 189 $this->_pathInfo=$_SERVER['PATH_INFO']; 190 else if(strpos($_SERVER['PHP_SELF'],$_SERVER['SCRIPT_NAME'])===0) 191 $this->_pathInfo=substr($_SERVER['PHP_SELF'],strlen($_SERVER['SCRIPT_NAME'])); 192 else 193 $this->_pathInfo=''; 194 195 if(get_magic_quotes_gpc()) 196 { 197 if(isset($_GET)) 198 $_GET=$this->stripSlashes($_GET); 199 if(isset($_POST)) 200 $_POST=$this->stripSlashes($_POST); 201 if(isset($_REQUEST)) 202 $_REQUEST=$this->stripSlashes($_REQUEST); 203 if(isset($_COOKIE)) 204 $_COOKIE=$this->stripSlashes($_COOKIE); 205 } 206 207 $this->getApplication()->setRequest($this); 208 } 209 210 /** 211 * Strips slashes from input data. 212 * This method is applied when magic quotes is enabled. 213 * @param mixed input data to be processed 214 * @param mixed processed data 215 */ 216 public function stripSlashes(&$data) 217 { 218 return is_array($data)?array_map(array($this,'stripSlashes'),$data):stripslashes($data); 219 } 220 221 /** 222 * @return TUri the request URL 223 */ 224 public function getUrl() 225 { 226 if($this->_url===null) 227 { 228 $secure=$this->getIsSecureConnection(); 229 $url=$secure?'https://':'http://'; 230 if(empty($_SERVER['HTTP_HOST'])) 231 { 232 $url.=$_SERVER['SERVER_NAME']; 233 $port=$_SERVER['SERVER_PORT']; 234 if(($port!=80 && !$secure) || ($port!=443 && $secure)) 235 $url.=':'.$port; 236 } 237 else 238 $url.=$_SERVER['HTTP_HOST']; 239 $url.=$this->getRequestUri(); 240 $this->_url=new TUri($url); 241 } 242 return $this->_url; 243 } 244 245 /** 246 * @return string the ID of the URL manager module 247 */ 248 public function getUrlManager() 249 { 250 return $this->_urlManagerID; 251 } 252 253 /** 254 * Sets the URL manager module. 255 * By default, {@link TUrlManager} is used for managing URLs. 256 * You may specify a different module for URL managing tasks 257 * by loading it as an application module and setting this property 258 * with the module ID. 259 * @param string the ID of the URL manager module 260 */ 261 public function setUrlManager($value) 262 { 263 $this->_urlManagerID=$value; 264 } 265 266 /** 267 * @return TUrlManager the URL manager module 268 */ 269 public function getUrlManagerModule() 270 { 271 return $this->_urlManager; 272 } 273 274 /** 275 * @return THttpRequestUrlFormat the format of URLs. Defaults to THttpRequestUrlFormat::Get. 276 */ 277 public function getUrlFormat() 278 { 279 return $this->_urlFormat; 280 } 281 282 /** 283 * Sets the format of URLs constructed and interpretted by the request module. 284 * A Get URL format is like index.php?name1=value1&name2=value2 285 * while a Path URL format is like index.php/name1,value1/name2,value. 286 * Changing the UrlFormat will affect {@link constructUrl} and how GET variables 287 * are parsed. 288 * @param THttpRequestUrlFormat the format of URLs. 289 */ 290 public function setUrlFormat($value) 291 { 292 $this->_urlFormat=TPropertyValue::ensureEnum($value,'THttpRequestUrlFormat'); 293 } 294 295 /** 296 * @return string separator used to separate GET variable name and value when URL format is Path. Defaults to comma ','. 297 */ 298 public function getUrlParamSeparator() 299 { 300 return $this->_separator; 301 } 302 303 /** 304 * @param string separator used to separate GET variable name and value when URL format is Path. 305 * @throws TInvalidDataValueException if the separator is not a single character 306 */ 307 public function setUrlParamSeparator($value) 308 { 309 if(strlen($value)===1) 310 $this->_separator=$value; 311 else 312 throw new TInvalidDataValueException('httprequest_separator_invalid'); 313 } 314 315 /** 316 * @return string request type, can be GET, POST, HEAD, or PUT 317 */ 318 public function getRequestType() 319 { 320 return $_SERVER['REQUEST_METHOD']; 321 } 322 323 /** 324 * @return boolean if the request is sent via secure channel (https) 325 */ 326 public function getIsSecureConnection() 327 { 328 return isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'],'off'); 329 } 330 331 /** 332 * @return string part of the request URL after script name and before question mark. 333 */ 334 public function getPathInfo() 335 { 336 return $this->_pathInfo; 337 } 338 339 /** 340 * @return string part of that request URL after the question mark 341 */ 342 public function getQueryString() 343 { 344 return isset($_SERVER['QUERY_STRING'])?$_SERVER['QUERY_STRING']:''; 345 } 346 347 /** 348 * @return string part of that request URL after the host info (including pathinfo and query string) 349 */ 350 public function getRequestUri() 351 { 352 return $this->_requestUri; 353 } 354 355 /** 356 * @return string schema and hostname of the requested URL 357 */ 358 public function getBaseUrl() 359 { 360 return ($this->getIsSecureConnection() ? "https://" : "http://") . $_SERVER ['HTTP_HOST']; 361 } 362 363 /** 364 * @return string entry script URL (w/o host part) 365 */ 366 public function getApplicationUrl() 367 { 368 return $_SERVER['SCRIPT_NAME']; 369 } 370 371 /** 372 * @return string entry script URL (w/ host part) 373 */ 374 public function getAbsoluteApplicationUrl() 375 { 376 return $this->getBaseUrl() . $this->getApplicationUrl(); 377 } 378 379 /** 380 * @return string application entry script file path (processed w/ realpath()) 381 */ 382 public function getApplicationFilePath() 383 { 384 return realpath($_SERVER['SCRIPT_FILENAME']); 385 } 386 387 /** 388 * @return string server name 389 */ 390 public function getServerName() 391 { 392 return $_SERVER['SERVER_NAME']; 393 } 394 395 /** 396 * @return integer server port number 397 */ 398 public function getServerPort() 399 { 400 return $_SERVER['SERVER_PORT']; 401 } 402 403 /** 404 * @return string URL referrer, null if not present 405 */ 406 public function getUrlReferrer() 407 { 408 return isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:null; 409 } 410 411 /** 412 * @return array user browser capabilities 413 * @see get_browser 414 */ 415 public function getBrowser() 416 { 417 return get_browser(); 418 } 419 420 /** 421 * @return string user agent 422 */ 423 public function getUserAgent() 424 { 425 return $_SERVER['HTTP_USER_AGENT']; 426 } 427 428 /** 429 * @return string user IP address 430 */ 431 public function getUserHostAddress() 432 { 433 return $_SERVER['REMOTE_ADDR']; 434 } 435 436 /** 437 * @return string user host name, null if cannot be determined 438 */ 439 public function getUserHost() 440 { 441 return isset($_SERVER['REMOTE_HOST'])?$_SERVER['REMOTE_HOST']:null; 442 } 443 444 /** 445 * @return string user browser accept types 446 */ 447 public function getAcceptTypes() 448 { 449 // TBD: break it into array?? 450 return $_SERVER['HTTP_ACCEPT']; 451 } 452 453 /** 454 * Returns a list of user preferred languages. 455 * The languages are returned as an array. Each array element 456 * represents a single language preference. The languages are ordered 457 * according to user preferences. The first language is the most preferred. 458 * @return array list of user preferred languages. 459 */ 460 public function getUserLanguages() 461 { 462 return Prado::getUserLanguages(); 463 } 464 465 /** 466 * @return boolean whether cookies should be validated. Defaults to false. 467 */ 468 public function getEnableCookieValidation() 469 { 470 return $this->_enableCookieValidation; 471 } 472 473 /** 474 * @param boolean whether cookies should be validated. 475 */ 476 public function setEnableCookieValidation($value) 477 { 478 $this->_enableCookieValidation=TPropertyValue::ensureBoolean($value); 479 } 480 481 /** 482 * @return THttpCookieCollection list of cookies to be sent 483 */ 484 public function getCookies() 485 { 486 if($this->_cookies===null) 487 { 488 $this->_cookies=new THttpCookieCollection; 489 if($this->getEnableCookieValidation()) 490 { 491 $sm=$this->getApplication()->getSecurityManager(); 492 foreach($_COOKIE as $key=>$value) 493 { 494 if(($value=$sm->validateData($value))!==false) 495 $this->_cookies->add(new THttpCookie($key,$value)); 496 } 497 } 498 else 499 { 500 foreach($_COOKIE as $key=>$value) 501 $this->_cookies->add(new THttpCookie($key,$value)); 502 } 503 } 504 return $this->_cookies; 505 } 506 507 /** 508 * @return array list of uploaded files. 509 */ 510 public function getUploadedFiles() 511 { 512 return $_FILES; 513 } 514 515 /** 516 * @return array list of server variables. 517 */ 518 public function getServerVariables() 519 { 520 return $_SERVER; 521 } 522 523 /** 524 * @return array list of environment variables. 525 */ 526 public function getEnvironmentVariables() 527 { 528 return $_ENV; 529 } 530 531 /** 532 * Constructs a URL that can be recognized by PRADO. 533 * The actual construction work is done by the URL manager module. 534 * This method may append session information to the generated URL if needed. 535 * You may provide your own URL manager module by setting {@link setUrlManager UrlManager} 536 * to provide your own URL scheme. 537 * @param string service ID 538 * @param string service parameter 539 * @param array GET parameters, null if not needed 540 * @param boolean whether to encode the ampersand in URL, defaults to true. 541 * @param boolean whether to encode the GET parameters (their names and values), defaults to false. 542 * @return string URL 543 * @see TUrlManager::constructUrl 544 */ 545 public function constructUrl($serviceID,$serviceParam,$getItems=null,$encodeAmpersand=true,$encodeGetItems=true) 546 { 547 $url=$this->_urlManager->constructUrl($serviceID,$serviceParam,$getItems,$encodeAmpersand,$encodeGetItems); 548 if(defined('SID') && SID != '' && !$this->_cookieOnly) 549 return $url . (strpos($url,'?')===false? '?' : ($encodeAmpersand?'&':'&')) . SID; 550 else 551 return $url; 552 } 553 554 /** 555 * Parses the request URL and returns an array of input parameters (excluding GET variables). 556 * You may override this method to support customized URL format. 557 * @return array list of input parameters, indexed by parameter names 558 * @see TUrlManager::parseUrl 559 */ 560 protected function parseUrl() 561 { 562 return $this->_urlManager->parseUrl(); 563 } 564 565 /** 566 * Resolves the requested servie. 567 * This method implements a URL-based service resolution. 568 * A URL in the format of /index.php?sp=serviceID.serviceParameter 569 * will be resolved with the serviceID and the serviceParameter. 570 * You may override this method to provide your own way of service resolution. 571 * @see constructUrl 572 */ 573 public function resolveRequest() 574 { 575 Prado::trace("Resolving request from ".$_SERVER['REMOTE_ADDR'],'System.Web.THttpRequest'); 576 $this->_requestResolved=true; 577 $this->_items=array_merge($_GET,$this->parseUrl(),$_POST); 578 foreach($this->_services as $id) 579 { 580 if($this->contains($id)) 581 { 582 $this->setServiceID($id); 583 $this->setServiceParameter($this->itemAt($id)); 584 break; 585 } 586 } 587 } 588 589 /** 590 * @return boolean true if request is already resolved, false otherwise. 591 */ 592 public function getRequestResolved() 593 { 594 return $this->_requestResolved; 595 } 596 597 /** 598 * @return array IDs of the available services 599 */ 600 public function getAvailableServices() 601 { 602 return $this->_services; 603 } 604 605 /** 606 * @param array IDs of the available services 607 */ 608 public function setAvailableServices($services) 609 { 610 $this->_services=$services; 611 } 612 613 /** 614 * @return string requested service ID 615 */ 616 public function getServiceID() 617 { 618 return $this->_serviceID; 619 } 620 621 /** 622 * Sets the requested service ID. 623 * @param string requested service ID 624 */ 625 public function setServiceID($value) 626 { 627 $this->_serviceID=$value; 628 } 629 630 /** 631 * @return string requested service parameter 632 */ 633 public function getServiceParameter() 634 { 635 return $this->_serviceParam; 636 } 637 638 /** 639 * Sets the requested service parameter. 640 * @param string requested service parameter 641 */ 642 public function setServiceParameter($value) 643 { 644 $this->_serviceParam=$value; 645 } 646 647 //------ The following methods enable THttpRequest to be TMap-like ----- 648 649 /** 650 * Returns an iterator for traversing the items in the list. 651 * This method is required by the interface IteratorAggregate. 652 * @return Iterator an iterator for traversing the items in the list. 653 */ 654 public function getIterator() 655 { 656 return new TMapIterator($this->_items); 657 } 658 659 /** 660 * @return integer the number of items in the request 661 */ 662 public function getCount() 663 { 664 return count($this->_items); 665 } 666 667 /** 668 * Returns the number of items in the request. 669 * This method is required by Countable interface. 670 * @return integer number of items in the request. 671 */ 672 public function count() 673 { 674 return $this->getCount(); 675 } 676 677 /** 678 * @return array the key list 679 */ 680 public function getKeys() 681 { 682 return array_keys($this->_items); 683 } 684 685 /** 686 * Returns the item with the specified key. 687 * This method is exactly the same as {@link offsetGet}. 688 * @param mixed the key 689 * @return mixed the element at the offset, null if no element is found at the offset 690 */ 691 public function itemAt($key) 692 { 693 return isset($this->_items[$key]) ? $this->_items[$key] : null; 694 } 695 696 /** 697 * Adds an item into the request. 698 * Note, if the specified key already exists, the old value will be overwritten. 699 * @param mixed key 700 * @param mixed value 701 */ 702 public function add($key,$value) 703 { 704 $this->_items[$key]=$value; 705 } 706 707 /** 708 * Removes an item from the request by its key. 709 * @param mixed the key of the item to be removed 710 * @return mixed the removed value, null if no such key exists. 711 * @throws TInvalidOperationException if the item cannot be removed 712 */ 713 public function remove($key) 714 { 715 if(isset($this->_items[$key]) || array_key_exists($key,$this->_items)) 716 { 717 $value=$this->_items[$key]; 718 unset($this->_items[$key]); 719 return $value; 720 } 721 else 722 return null; 723 } 724 725 /** 726 * Removes all items in the request. 727 */ 728 public function clear() 729 { 730 foreach(array_keys($this->_items) as $key) 731 $this->remove($key); 732 } 733 734 /** 735 * @param mixed the key 736 * @return boolean whether the request contains an item with the specified key 737 */ 738 public function contains($key) 739 { 740 return isset($this->_items[$key]) || array_key_exists($key,$this->_items); 741 } 742 743 /** 744 * @return array the list of items in array 745 */ 746 public function toArray() 747 { 748 return $this->_items; 749 } 750 751 /** 752 * Returns whether there is an element at the specified offset. 753 * This method is required by the interface ArrayAccess. 754 * @param mixed the offset to check on 755 * @return boolean 756 */ 757 public function offsetExists($offset) 758 { 759 return $this->contains($offset); 760 } 761 762 /** 763 * Returns the element at the specified offset. 764 * This method is required by the interface ArrayAccess. 765 * @param integer the offset to retrieve element. 766 * @return mixed the element at the offset, null if no element is found at the offset 767 */ 768 public function offsetGet($offset) 769 { 770 return $this->itemAt($offset); 771 } 772 773 /** 774 * Sets the element at the specified offset. 775 * This method is required by the interface ArrayAccess. 776 * @param integer the offset to set element 777 * @param mixed the element value 778 */ 779 public function offsetSet($offset,$item) 780 { 781 $this->add($offset,$item); 782 } 783 784 /** 785 * Unsets the element at the specified offset. 786 * This method is required by the interface ArrayAccess. 787 * @param mixed the offset to unset element 788 */ 789 public function offsetUnset($offset) 790 { 791 $this->remove($offset); 792 } 793 } 794 795 /** 796 * THttpCookieCollection class. 797 * 798 * THttpCookieCollection implements a collection class to store cookies. 799 * Besides using all functionalities from {@link TList}, you can also 800 * retrieve a cookie by its name using either {@link findCookieByName} or 801 * simply: 802 * <code> 803 * $cookie=$collection[$cookieName]; 804 * </code> 805 * 806 * @author Qiang Xue <qiang.xue@gmail.com> 807 * @version $Id: THttpRequest.php 1539 2006-12-02 19:20:40Z xue $ 808 * @package System.Web 809 * @since 3.0 810 */ 811 class THttpCookieCollection extends TList 812 { 813 /** 814 * @var mixed owner of this collection 815 */ 816 private $_o; 817 818 /** 819 * Constructor. 820 * @param mixed owner of this collection. 821 */ 822 public function __construct($owner=null) 823 { 824 $this->_o=$owner; 825 } 826 827 /** 828 * Inserts an item at the specified position. 829 * This overrides the parent implementation by performing additional 830 * operations for each newly added THttpCookie object. 831 * @param integer the speicified position. 832 * @param mixed new item 833 * @throws TInvalidDataTypeException if the item to be inserted is not a THttpCookie object. 834 */ 835 public function insertAt($index,$item) 836 { 837 if($item instanceof THttpCookie) 838 { 839 parent::insertAt($index,$item); 840 if($this->_o instanceof THttpResponse) 841 $this->_o->addCookie($item); 842 } 843 else 844 throw new TInvalidDataTypeException('authorizationrulecollection_authorizationrule_required'); 845 } 846 847 /** 848 * Removes an item at the specified position. 849 * This overrides the parent implementation by performing additional 850 * cleanup work when removing a TCookie object. 851 * @param integer the index of the item to be removed. 852 * @return mixed the removed item. 853 */ 854 public function removeAt($index) 855 { 856 $item=parent::removeAt($index); 857 if($this->_o instanceof THttpResponse) 858 $this->_o->removeCookie($item); 859 return $item; 860 } 861 862 /** 863 * @param integer|string index of the cookie in the collection or the cookie's name 864 * @return THttpCookie the cookie found 865 */ 866 public function itemAt($index) 867 { 868 if(is_integer($index)) 869 return parent::itemAt($index); 870 else 871 return $this->findCookieByName($index); 872 } 873 874 /** 875 * Finds the cookie with the specified name. 876 * @param string the name of the cookie to be looked for 877 * @return THttpCookie the cookie, null if not found 878 */ 879 public function findCookieByName($name) 880 { 881 foreach($this as $cookie) 882 if($cookie->getName()===$name) 883 return $cookie; 884 return null; 885 } 886 } 887 888 /** 889 * THttpCookie class. 890 * 891 * A THttpCookie instance stores a single cookie, including the cookie name, value, 892 * domain, path, expire, and secure. 893 * 894 * @author Qiang Xue <qiang.xue@gmail.com> 895 * @version $Id: THttpRequest.php 1539 2006-12-02 19:20:40Z xue $ 896 * @package System.Web 897 * @since 3.0 898 */ 899 class THttpCookie extends TComponent 900 { 901 /** 902 * @var string domain of the cookie 903 */ 904 private $_domain=''; 905 /** 906 * @var string name of the cookie 907 */ 908 private $_name; 909 /** 910 * @var string value of the cookie 911 */ 912 private $_value=0; 913 /** 914 * @var integer expire of the cookie 915 */ 916 private $_expire=0; 917 /** 918 * @var string path of the cookie 919 */ 920 private $_path='/'; 921 /** 922 * @var boolean whether cookie should be sent via secure connection 923 */ 924 private $_secure=false; 925 926 /** 927 * Constructor. 928 * @param string name of this cookie 929 * @param string value of this cookie 930 */ 931 public function __construct($name,$value) 932 { 933 $this->_name=$name; 934 $this->_value=$value; 935 } 936 937 /** 938 * @return string the domain to associate the cookie with 939 */ 940 public function getDomain() 941 { 942 return $this->_domain; 943 } 944 945 /** 946 * @param string the domain to associate the cookie with 947 */ 948 public function setDomain($value) 949 { 950 $this->_domain=$value; 951 } 952 953 /** 954 * @return integer the time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. 955 */ 956 public function getExpire() 957 { 958 return $this->_expire; 959 } 960 961 /** 962 * @param integer the time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. 963 */ 964 public function setExpire($value) 965 { 966 $this->_expire=TPropertyValue::ensureInteger($value); 967 } 968 969 /** 970 * @return string the name of the cookie 971 */ 972 public function getName() 973 { 974 return $this->_name; 975 } 976 977 /** 978 * @param string the name of the cookie 979 */ 980 public function setName($value) 981 { 982 $this->_name=$value; 983 } 984 985 /** 986 * @return string the value of the cookie 987 */ 988 public function getValue() 989 { 990 return $this->_value; 991 } 992 993 /** 994 * @param string the value of the cookie 995 */ 996 public function setValue($value) 997 { 998 $this->_value=$value; 999 } 1000 1001 /** 1002 * @return string the path on the server in which the cookie will be available on, default is '/' 1003 */ 1004 public function getPath() 1005 { 1006 return $this->_path; 1007 } 1008 1009 /** 1010 * @param string the path on the server in which the cookie will be available on 1011 */ 1012 public function setPath($value) 1013 { 1014 $this->_path=$value; 1015 } 1016 1017 /** 1018 * @return boolean whether the cookie should only be transmitted over a secure HTTPS connection 1019 */ 1020 public function getSecure() 1021 { 1022 return $this->_secure; 1023 } 1024 1025 /** 1026 * @param boolean ether the cookie should only be transmitted over a secure HTTPS connection 1027 */ 1028 public function setSecure($value) 1029 { 1030 $this->_secure=TPropertyValue::ensureBoolean($value); 1031 } 1032 } 1033 1034 /** 1035 * TUri class 1036 * 1037 * TUri represents a URI. Given a URI 1038 * http://joe:whatever@example.com:8080/path/to/script.php?param=value#anchor 1039 * it will be decomposed as follows, 1040 * - scheme: http 1041 * - host: example.com 1042 * - port: 8080 1043 * - user: joe 1044 * - password: whatever 1045 * - path: /path/to/script.php 1046 * - query: param=value 1047 * - fragment: anchor 1048 * 1049 * @author Qiang Xue <qiang.xue@gmail.com> 1050 * @version $Id: THttpRequest.php 1539 2006-12-02 19:20:40Z xue $ 1051 * @package System.Web 1052 * @since 3.0 1053 */ 1054 class TUri extends TComponent 1055 { 1056 /** 1057 * @var array list of default ports for known schemes 1058 */ 1059 private static $_defaultPort=array( 1060 'ftp'=>21, 1061 'gopher'=>70, 1062 'http'=>80, 1063 'https'=>443, 1064 'news'=>119, 1065 'nntp'=>119, 1066 'wais'=>210, 1067 'telnet'=>23 1068 ); 1069 /** 1070 * @var string scheme of the URI 1071 */ 1072 private $_scheme; 1073 /** 1074 * @var string host name of the URI 1075 */ 1076 private $_host; 1077 /** 1078 * @var integer port of the URI 1079 */ 1080 private $_port; 1081 /** 1082 * @var string user of the URI 1083 */ 1084 private $_user; 1085 /** 1086 * @var string password of the URI 1087 */ 1088 private $_pass; 1089 /** 1090 * @var string path of the URI 1091 */ 1092 private $_path; 1093 /** 1094 * @var string query string of the URI 1095 */ 1096 private $_query; 1097 /** 1098 * @var string fragment of the URI 1099 */ 1100 private $_fragment; 1101 /** 1102 * @var string the URI 1103 */ 1104 private $_uri; 1105 1106 /** 1107 * Constructor. 1108 * Decomposes the specified URI into parts. 1109 * @param string URI to be represented 1110 * @throws TInvalidDataValueException if URI is of bad format 1111 */ 1112 public function __construct($uri) 1113 { 1114 if(($ret=@parse_url($uri))!==false) 1115 { 1116 // decoding??? 1117 $this->_scheme=isset($ret['scheme'])?$ret['scheme']:''; 1118 $this->_host=isset($ret['host'])?$ret['host']:''; 1119 $this->_port=isset($ret['port'])?$ret['port']:''; 1120 $this->_user=isset($ret['user'])?$ret['user']:''; 1121 $this->_pass=isset($ret['pass'])?$ret['pass']:''; 1122 $this->_path=isset($ret['path'])?$ret['path']:''; 1123 $this->_query=isset($ret['query'])?$ret['query']:''; 1124 $this->_fragment=isset($ret['fragment'])?$ret['fragment']:''; 1125 $this->_uri=$uri; 1126 } 1127 else 1128 { 1129 throw new TInvalidDataValueException('uri_format_invalid',$uri); 1130 } 1131 } 1132 1133 /** 1134 * @return string URI 1135 */ 1136 public function getUri() 1137 { 1138 return $this->_uri; 1139 } 1140 1141 /** 1142 * @return string scheme of the URI, such as 'http', 'https', 'ftp', etc. 1143 */ 1144 public function getScheme() 1145 { 1146 return $this->_scheme; 1147 } 1148 1149 /** 1150 * @return string hostname of the URI 1151 */ 1152 public function getHost() 1153 { 1154 return $this->_host; 1155 } 1156 1157 /** 1158 * @return integer port number of the URI 1159 */ 1160 public function getPort() 1161 { 1162 return $this->_port; 1163 } 1164 1165 /** 1166 * @return string username of the URI 1167 */ 1168 public function getUser() 1169 { 1170 return $this->_user; 1171 } 1172 1173 /** 1174 * @return string password of the URI 1175 */ 1176 public function getPassword() 1177 { 1178 return $this->_pass; 1179 } 1180 1181 /** 1182 * @return string path of the URI 1183 */ 1184 public function getPath() 1185 { 1186 return $this->_path; 1187 } 1188 1189 /** 1190 * @return string query string of the URI 1191 */ 1192 public function getQuery() 1193 { 1194 return $this->_query; 1195 } 1196 1197 /** 1198 * @return string fragment of the URI 1199 */ 1200 public function getFragment() 1201 { 1202 return $this->_fragment; 1203 } 1204 } 1205 1206 /** 1207 * THttpRequestUrlFormat class. 1208 * THttpRequestUrlFormat defines the enumerable type for the possible URL formats 1209 * that can be recognized by {@link THttpRequest}. 1210 * 1211 * The following enumerable values are defined: 1212 * - Get: the URL format is like /path/to/index.php?name1=value1&name2=value2... 1213 * - Path: the URL format is like /path/to/index.php/name1,value1/name2,value2... 1214 * 1215 * @author Qiang Xue <qiang.xue@gmail.com> 1216 * @version $Id: THttpRequest.php 1539 2006-12-02 19:20:40Z xue $ 1217 * @package System.Web 1218 * @since 3.0.4 1219 */ 1220 class THttpRequestUrlFormat extends TEnumerable 1221 { 1222 const Get='Get'; 1223 const Path='Path'; 1224 } 1225 1226 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |