[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZHTTPTool class 4 // 5 // Created on: <18-Apr-2002 14:05:21 amos> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! \defgroup eZHTTP HTTP utilities 30 \ingroup eZUtils */ 31 32 /*! 33 \class eZHTTPTool ezhttptool.php 34 \ingroup eZHTTP 35 \brief Provides access to HTTP post,get and session variables 36 37 See PHP manual on <a href="http://www.php.net/manual/fi/language.variables.predefined.php">Predefined Variables</a> for more information. 38 39 */ 40 41 include_once ( "lib/ezutils/classes/ezdebug.php" ); 42 include_once ( "lib/ezutils/classes/ezsession.php" ); 43 include_once ( "lib/ezutils/classes/ezsys.php" ); 44 45 class eZHTTPTool 46 { 47 /*! 48 Initializes the class. Use eZHTTPTool::instance to get a single instance. 49 */ 50 function eZHTTPTool() 51 { 52 $this->UseFullUrl = false; 53 $magicQuote = get_magic_quotes_gpc(); 54 55 if ( $magicQuote == 1 ) 56 { 57 eZHTTPTool::removeMagicQuotes(); 58 } 59 } 60 61 /*! 62 Sets the post variable \a $var to \a $value. 63 \sa postVariable 64 */ 65 function setPostVariable( $var, $value ) 66 { 67 $_POST[$var] = $value; 68 } 69 70 /*! 71 \return a reference to the HTTP post variable $var, or null if it does not exist. 72 \sa variable 73 */ 74 function postVariable( $var ) 75 { 76 $ret = null; 77 if ( isset( $_POST[$var] ) ) 78 $ret = $_POST[$var]; 79 else 80 eZDebug::writeWarning( "Undefined post variable: $var", 81 "eZHTTPTool" ); 82 return $ret; 83 } 84 85 /*! 86 \return true if the HTTP post variable $var exist. 87 \sa hasVariable 88 */ 89 function hasPostVariable( $var ) 90 { 91 return isset( $_POST[$var] ); 92 } 93 94 /*! 95 Sets the get variable \a $var to \a $value. 96 \sa getVariable 97 */ 98 function setGetVariable( $var, $value ) 99 { 100 $_GET[$var] = $value; 101 } 102 103 /*! 104 \return a reference to the HTTP get variable $var, or null if it does not exist. 105 \sa variable 106 */ 107 function getVariable( $var ) 108 { 109 $ret = null; 110 if ( isset( $_GET[$var] ) ) 111 $ret = $_GET[$var]; 112 else 113 eZDebug::writeWarning( "Undefined get variable: $var", 114 "eZHTTPTool" ); 115 return $ret; 116 } 117 118 /*! 119 \return true if the HTTP get variable $var exist. 120 \sa hasVariable 121 */ 122 function hasGetVariable( $var ) 123 { 124 return isset( $_GET[$var] ); 125 } 126 127 /*! 128 \return true if the HTTP post/get variable $var exists. 129 \sa hasPostVariable 130 */ 131 function hasVariable( $var ) 132 { 133 134 if ( isset( $_POST[$var] ) ) 135 { 136 return isset( $_POST[$var] ); 137 } 138 else 139 { 140 return isset( $_GET[$var] ); 141 } 142 } 143 144 /*! 145 \return a reference to the HTTP post/get variable $var, or null if it does not exist. 146 \sa postVariable 147 */ 148 function variable( $var ) 149 { 150 if ( isset( $_POST[$var] ) ) 151 { 152 return $_POST[$var]; 153 } 154 else if ( isset( $_GET[$var] ) ) 155 { 156 return $_GET[$var]; 157 } 158 $ret = false; 159 return $ret; 160 } 161 162 /*! 163 \return the attributes for this object. 164 */ 165 function attributes() 166 { 167 return array( "post", "get", "session" ); 168 } 169 170 /*! 171 \return true if the attribute $attr exist. 172 */ 173 function hasAttribute( $attr ) 174 { 175 return in_array( $attr, $this->attributes() ); 176 } 177 178 /*! 179 \return the value for the attribute $attr or null if the attribute does not exist. 180 */ 181 function &attribute( $attr ) 182 { 183 if ( $attr == "post" ) 184 return $_POST; 185 if ( $attr == "get" ) 186 return $_GET; 187 if ( $attr == "session" ) 188 { 189 return $_SESSION; 190 } 191 $retValue = null; 192 return $retValue; 193 } 194 195 /*! 196 \return the unique instance of the HTTP tool 197 */ 198 function &instance() 199 { 200 $instance =& $GLOBALS["eZHTTPToolInstance"]; 201 if ( get_class( $instance ) != "ezhttptool" ) 202 { 203 $instance = new eZHTTPTool(); 204 $instance->createPostVarsFromImageButtons(); 205 eZSessionStart(); 206 } 207 return $instance; 208 } 209 210 /*! 211 \static 212 213 Sends a http request to the specified host. Using https:// requires PHP 4.3.0, and compiled in OpenSSL support. 214 215 \param http/https address, only path to send request to eZ publish. 216 examples: http://ez.no, https://secure.ez.no, ssl://secure.ez.no, content/view/full/2 217 \param port, default 80 218 \param post parameters array (optional), if no post parameters are present, a get request will be send. 219 \param user agent, default will be eZ publish 220 \param passtrough, will send result directly to client, default false 221 222 \return result if http request, or return false if an error occurs. 223 If pipetrough, program will end here. 224 225 */ 226 function sendHTTPRequest( $uri, $port = 80, $postParameters = false, $userAgent = 'eZ publish', $passtrough = true ) 227 { 228 preg_match( "/^((http[s]?:\/\/)([a-zA-Z0-9_.]+))?([\/]?[~]?(\.?[^.]+[~]?)*)/i", $uri, $matches ); 229 $protocol = $matches[2]; 230 $host = $matches[3]; 231 $path = $matches[4]; 232 if ( !$path ) 233 { 234 $path = '/'; 235 } 236 237 $data = ''; 238 if ( $postParameters ) 239 { 240 $method = 'POST'; 241 $dataCount = 0; 242 foreach( array_keys( $postParameters ) as $paramName ) 243 { 244 if ( $dataCount > 0 ) 245 { 246 $data .= '&'; 247 } 248 ++$dataCount; 249 if ( !is_array( $postParameters[$paramName] ) ) 250 { 251 $data .= urlencode( $paramName ) . '=' . urlencode( $postParameters[$paramName] ); 252 } 253 else 254 { 255 foreach( $postParameters[$paramName] as $value ) 256 { 257 $data .= urlencode( $paramName ) . '[]=' . urlencode( $value ); 258 } 259 } 260 } 261 } 262 else 263 { 264 $method = 'GET'; 265 } 266 267 if ( !$host ) 268 { 269 $host = $_SERVER['HTTP_HOST']; 270 $filename = $host; 271 if ( $path[0] != '/' ) 272 { 273 $path = $_SERVER['SCRIPT_NAME'] . '/' . $path; 274 } 275 else 276 { 277 $path = $_SERVER['SCRIPT_NAME'] . $path; 278 } 279 } 280 else{ 281 if ( !$protocol || $protocol == 'https://' ) 282 { 283 $filename = 'ssl://' . $host; 284 } 285 else 286 { 287 $filename = 'tcp://' . $host; 288 } 289 } 290 291 // make sure we have a valid hostname or call to fsockopen() will fail 292 $parsedUrl = parse_url( $filename ); 293 $ip = isset( $parsedUrl[ 'host' ] ) ? gethostbyname( $parsedUrl[ 'host' ] ) : ''; 294 $checkIP = ip2long( $ip ); 295 if ( $checkIP < 0 ) 296 { 297 return false; 298 } 299 300 $fp = fsockopen( $filename, $port ); 301 302 // make sure we have a valid stream resource or calls to other file 303 // functions will fail 304 if ( !$fp ) 305 { 306 return false; 307 } 308 309 $request = $method . ' ' . $path . ' ' . 'HTTP/1.1' . "\r\n" . 310 "Host: $host\r\n" . 311 "Accept: */*\r\n" . 312 "Content-type: application/x-www-form-urlencoded\r\n" . 313 "Content-length: " . strlen( $data ) . "\r\n" . 314 "User-Agent: $userAgent\r\n" . 315 "Pragma: no-cache\r\n" . 316 "Connection: close\r\n\r\n"; 317 318 fputs( $fp, $request ); 319 if ( $method == 'POST' ) 320 { 321 fputs( $fp, $data ); 322 } 323 324 $buf = ''; 325 if ( $passtrough ) 326 { 327 ob_end_clean(); 328 $header = true; 329 330 $character = ''; 331 while( $header ) 332 { 333 $buffer = $character; 334 while ( !feof( $fp ) ) 335 { 336 $character = fgetc( $fp ); 337 if ( $character == "\r" ) 338 { 339 fgetc( $fp ); 340 $character = fgetc( $fp ); 341 if ( $character == "\r" ) 342 { 343 fgetc( $fp ); 344 $header = false; 345 } 346 break; 347 } 348 else 349 { 350 $buffer .= $character; 351 } 352 } 353 354 header( $buffer ); 355 } 356 357 header( 'Content-Location: ' . $uri ); 358 359 fpassthru( $fp ); 360 include_once( 'lib/ezutils/classes/ezexecution.php' ); 361 eZExecution::cleanExit(); 362 } 363 else 364 { 365 $buf = ''; 366 while ( !feof( $fp ) ) 367 { 368 $buf .= fgets( $fp, 128 ); 369 } 370 } 371 372 fclose($fp); 373 return $buf; 374 } 375 376 /*! 377 \static 378 */ 379 function parseHTTPResponse( &$response, &$header, &$body ) 380 { 381 if ( $response ) 382 { 383 $crlf = "\r\n"; 384 385 // split header and body 386 $pos = strpos( $response, $crlf . $crlf ); 387 if ( $pos !== false ) 388 { 389 $headerBuf = substr( $response, 0, $pos ); 390 $body = substr( $response, $pos + 2 * strlen( $crlf ) ); 391 392 // parse headers 393 $header = array(); 394 $lines = explode( $crlf, $headerBuf ); 395 foreach ( $lines as $line ) 396 { 397 if ( ( $pos = strpos( $line, ':') ) !== false ) 398 { 399 $header[strtolower( trim( substr( $line, 0, $pos ) ) )] = trim( substr( $line, $pos+1 ) ); 400 } 401 } 402 403 return true; 404 } 405 } 406 407 return false; 408 } 409 410 411 /*! 412 \static 413 Sends a redirect path to the browser telling it to 414 load the new path. 415 By default only \a $path is required, other parameters 416 will be fetched automatically to create a HTTP/1.1 417 compatible header. 418 The input \a $parameters may contain the following keys. 419 - host - the name of the host, default will fetch the currenty hostname 420 - protocol - which protocol to use, default will use HTTP 421 - port - the port on the host 422 - username - a username which is required to login on the site 423 - password - if username is supplied this password will be used for authentication 424 425 The path may be specified relativily \c rel/ative, from root \c /a/root, with hostname 426 change \c //myhost.com/a/root/rel/ative, with protocol \c http://myhost.com/a/root/rel/ative. 427 Also port may be placed in the path string. 428 It is recommended that the path only contain a plain root path and instead send the rest 429 as optional parameters, the support for different kinds of paths is only incase you get 430 URLs externally which contains any of the above cases. 431 432 \note The redirection does not happen immedietaly and the script execution will continue. 433 */ 434 function createRedirectUrl( $path, $parameters = array() ) 435 { 436 $parameters = array_merge( array( 'host' => false, 437 'protocol' => false, 438 'port' => false, 439 'username' => false, 440 'password' => false, 441 'override_host' => false, 442 'override_protocol' => false, 443 'override_port' => false, 444 'override_username' => false, 445 'override_password' => false, 446 'pre_url' => true ), 447 $parameters ); 448 $host = $parameters['host']; 449 $protocol = $parameters['protocol']; 450 $port = $parameters['port']; 451 $username = $parameters['username']; 452 $password = $parameters['password']; 453 if ( preg_match( '#^([a-zA-Z0-9]+):(.+)$#', $path, $matches ) ) 454 { 455 if ( $matches[1] ) 456 $protocol = $matches[1]; 457 $path = $matches[2]; 458 459 } 460 if ( preg_match( '#^//((([a-zA-Z0-9_.]+)(:([a-zA-Z0-9_.]+))?)@)?([^./:]+(\.[^./:]+)*)(:([0-9]+))?(.*)$#', $path, $matches ) ) 461 { 462 if ( $matches[6] ) 463 { 464 $host = $matches[6]; 465 } 466 467 if ( $matches[3] ) 468 $username = $matches[3]; 469 if ( $matches[5] ) 470 $password = $matches[5]; 471 if ( $matches[9] ) 472 $port = $matches[9]; 473 $path = $matches[10]; 474 } 475 if ( $parameters['pre_url'] ) 476 { 477 if ( strlen( $path ) > 0 and 478 $path[0] != '/' ) 479 { 480 $preURL = eZSys::serverVariable( 'SCRIPT_URL' ); 481 if ( strlen( $preURL ) > 0 and 482 $preURL[strlen($preURL) - 1] != '/' ) 483 $preURL .= '/'; 484 $path = $preURL . $path; 485 } 486 } 487 488 if ( $parameters['override_host'] ) 489 $host = $parameters['override_host']; 490 if ( $parameters['override_port'] ) 491 $port = $parameters['override_port']; 492 if ( !is_string( $host ) ) 493 $host = eZSys::hostname(); 494 if ( !is_string( $protocol ) ) 495 { 496 $protocol = 'http'; 497 // Default to https if SSL is enabled 498 499 // Check if SSL port is defined in site.ini 500 $ini =& eZINI::instance(); 501 $sslPort = 443; 502 if ( $ini->hasVariable( 'SiteSettings', 'SSLPort' ) ) 503 { 504 $sslPort = $ini->variable( 'SiteSettings', 'SSLPort' ); 505 } 506 507 if ( eZSys::serverPort() == $sslPort ) 508 { 509 $protocol = 'https'; 510 $port = false; 511 } 512 } 513 if ( $parameters['override_protocol'] ) 514 $host = $parameters['override_protocol']; 515 516 $uri = $protocol . '://'; 517 if ( $parameters['override_username'] ) 518 $username = $parameters['override_username']; 519 if ( $parameters['override_password'] ) 520 $password = $parameters['override_password']; 521 if ( $username ) 522 { 523 $uri .= $username; 524 if ( $password ) 525 $uri .= ':' . $password; 526 $uri .= '@'; 527 } 528 $uri .= $host; 529 if ( $port ) 530 $uri .= ':' . $port; 531 $uri .= $path; 532 return $uri; 533 } 534 535 function redirect( $path, $parameters = array(), $status = false ) 536 { 537 $uri = eZHTTPTool::createRedirectUrl( $path, $parameters ); 538 if ( strlen( $status ) > 0 ) 539 { 540 header( $_SERVER['SERVER_PROTOCOL'] . " " . $status ); 541 eZHTTPTool::headerVariable( "Status", $status ); 542 } 543 eZHTTPTool::headerVariable( 'Location', $uri ); 544 545 /* Fix for redirecting using workflows and apache 2 */ 546 echo '<HTML><HEAD>'; 547 echo '<META HTTP-EQUIV="Refresh" Content="0;URL='. htmlspecialchars( $uri ) .'">'; 548 echo '<META HTTP-EQUIV="Location" Content="'. htmlspecialchars( $uri ) .'">'; 549 echo '</HEAD><BODY></BODY></HTML>'; 550 } 551 552 /*! 553 \static 554 Sets the header variable \a $headerName to have the data \a $headerData. 555 \note Calls PHPs header() with a constructed string. 556 */ 557 function headerVariable( $headerName, $headerData ) 558 { 559 header( $headerName .': '. $headerData ); 560 } 561 562 function removeMagicQuotes() 563 { 564 foreach ( array_keys( $_POST ) as $key ) 565 { 566 if ( !is_array( $_POST[$key] ) ) 567 { 568 $_POST[$key] = str_replace( "\'", "'", $_POST[$key] ); 569 $_POST[$key] = str_replace( '\"', '"', $_POST[$key] ); 570 $_POST[$key] = str_replace( '\\\\', '\\', $_POST[$key] ); 571 } 572 else 573 { 574 foreach ( array_keys( $_POST[$key] ) as $arrayKey ) 575 { 576 $_POST[$key][$arrayKey] = str_replace( "\'", "'", $_POST[$key][$arrayKey] ); 577 $_POST[$key][$arrayKey] = str_replace( '\"', '"', $_POST[$key][$arrayKey] ); 578 $_POST[$key][$arrayKey] = str_replace( '\\\\', '\\', $_POST[$key][$arrayKey] ); 579 } 580 } 581 } 582 foreach ( array_keys( $_GET ) as $key ) 583 { 584 if ( !is_array( $_GET[$key] ) ) 585 { 586 $_GET[$key] = str_replace( "\'", "'", $_GET[$key] ); 587 $_GET[$key] = str_replace( '\"', '"', $_GET[$key] ); 588 $_GET[$key] = str_replace( '\\\\', '\\', $_GET[$key] ); 589 } 590 else 591 { 592 foreach ( array_keys( $_GET[$key] ) as $arrayKey ) 593 { 594 $_GET[$key][$arrayKey] = str_replace( "\'", "'", $_GET[$key][$arrayKey] ); 595 $_GET[$key][$arrayKey] = str_replace( '\"', '"', $_GET[$key][$arrayKey] ); 596 $_GET[$key][$arrayKey] = str_replace( '\\\\', '\\', $_GET[$key][$arrayKey] ); 597 } 598 } 599 } 600 } 601 602 function createPostVarsFromImageButtons() 603 { 604 foreach ( array_keys( $_POST ) as $key ) 605 { 606 if ( substr( $key, -2 ) == '_x' ) 607 { 608 $yKey = substr( $key, 0, -2 ) . '_y'; 609 if ( array_key_exists( $yKey, $_POST ) ) 610 { 611 $keyClean = substr( $key, 0, -2 ); 612 $matches = array(); 613 if ( preg_match( "/_(\d+)$/", $keyClean, $matches ) ) 614 { 615 $value = $matches[1]; 616 $keyClean = preg_replace( "/(_\d+)$/","", $keyClean ); 617 $_POST[$keyClean] = $value; 618 // eZDebug::writeDebug( $_POST[$keyClean], "We have create new Post Var with name $keyClean and value $value:" ); 619 } 620 else 621 { 622 $_POST[$keyClean] = true; 623 // eZDebug::writeDebug( $_POST[$keyClean], "We have create new Post Var with name $keyClean and value true:" ); 624 } 625 } 626 } 627 } 628 } 629 630 /*! 631 Sets the session variable $name to value $value. 632 */ 633 function getSessionKey() 634 { 635 return session_id(); 636 } 637 638 function setSessionKey( $sessionKey ) 639 { 640 return session_id( $sessionKey ); 641 } 642 643 function setSessionVariable( $name, $value ) 644 { 645 $_SESSION[$name] =& $value; 646 } 647 648 /*! 649 Removes the session variable $name. 650 */ 651 function removeSessionVariable( $name ) 652 { 653 unset( $_SESSION[$name] ); 654 } 655 656 /*! 657 \return true if the session variable $name exist. 658 */ 659 function hasSessionVariable( $name ) 660 { 661 return isset( $_SESSION[$name] ); 662 } 663 664 /*! 665 \return the session variable $name. 666 */ 667 function &sessionVariable( $name ) 668 { 669 return $_SESSION[$name]; 670 } 671 672 /*! 673 \return the session id 674 */ 675 function sessionID() 676 { 677 return session_id(); 678 } 679 680 /*! 681 \static 682 \param $url 683 \param $justCheckURL if true, we should check url only not downloading data. 684 \return data from \p $url, false if invalid URL 685 */ 686 function getDataByURL( $url, $justCheckURL = false ) 687 { 688 // First try CURL 689 if ( extension_loaded( 'curl' ) ) 690 { 691 $ch = curl_init( $url ); 692 if ( $justCheckURL ) 693 { 694 curl_setopt( $ch, CURLOPT_TIMEOUT, 15 ); 695 curl_setopt( $ch, CURLOPT_FAILONERROR, 1 ); 696 curl_setopt( $ch, CURLOPT_NOBODY, 1 ); 697 } 698 699 $ini =& eZINI::instance(); 700 $proxy = $ini->hasVariable( 'ProxySettings', 'ProxyServer' ) ? $ini->variable( 'ProxySettings', 'ProxyServer' ) : false; 701 // If we should use proxy 702 if ( $proxy ) 703 { 704 curl_setopt ( $ch, CURLOPT_PROXY , $proxy ); 705 $userName = $ini->hasVariable( 'ProxySettings', 'User' ) ? $ini->variable( 'ProxySettings', 'User' ) : false; 706 $password = $ini->hasVariable( 'ProxySettings', 'Password' ) ? $ini->variable( 'ProxySettings', 'Password' ) : false; 707 if ( $userName ) 708 { 709 curl_setopt ( $ch, CURLOPT_PROXYUSERPWD, "$userName:$password" ); 710 } 711 } 712 // If we should check url without downloading data from it. 713 if ( $justCheckURL ) 714 { 715 if ( !curl_exec( $ch ) ) 716 return false; 717 718 curl_close( $ch ); 719 return true; 720 } 721 // Getting data 722 ob_start(); 723 if ( !curl_exec( $ch ) ) 724 return false; 725 726 curl_close ( $ch ); 727 $data = ob_get_contents(); 728 ob_end_clean(); 729 730 return $data; 731 } 732 // Open and read url 733 $fid = fopen( $url, 'r' ); 734 if ( $fid === false ) 735 { 736 return false; 737 } 738 739 if ( $justCheckURL ) 740 { 741 if ( $fid ) 742 fclose( $fid ); 743 744 return $fid; 745 } 746 747 $data = ""; 748 do 749 { 750 $dataBody = fread( $fid, 8192 ); 751 if ( strlen( $dataBody ) == 0 ) 752 break; 753 $data .= $dataBody; 754 } while( true ); 755 756 fclose( $fid ); 757 return $data; 758 } 759 } 760 761 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |