00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 include_once( "lib/ezutils/classes/ezdebug.php" );
00044 include_once( "lib/ezutils/classes/ezsession.php" );
00045 include_once( "lib/ezutils/classes/ezsys.php" );
00046
00047 class eZHTTPTool
00048 {
00049
00050
00051
00052 function eZHTTPTool()
00053 {
00054 $this->UseFullUrl = false;
00055 $magicQuote = get_magic_quotes_gpc();
00056
00057 if ( $magicQuote == 1 )
00058 {
00059 eZHTTPTool::removeMagicQuotes();
00060 }
00061 }
00062
00063
00064
00065
00066
00067 function setPostVariable( $var, $value )
00068 {
00069 $_POST[$var] = $value;
00070 }
00071
00072
00073
00074
00075
00076 function postVariable( $var )
00077 {
00078 $ret = null;
00079 if ( isset( $_POST[$var] ) )
00080 $ret = $_POST[$var];
00081 else
00082 eZDebug::writeWarning( "Undefined post variable: $var",
00083 "eZHTTPTool" );
00084 return $ret;
00085 }
00086
00087
00088
00089
00090
00091 function hasPostVariable( $var )
00092 {
00093 return isset( $_POST[$var] );
00094 }
00095
00096
00097
00098
00099
00100 function setGetVariable( $var, $value )
00101 {
00102 $_GET[$var] = $value;
00103 }
00104
00105
00106
00107
00108
00109 function getVariable( $var )
00110 {
00111 $ret = null;
00112 if ( isset( $_GET[$var] ) )
00113 $ret = $_GET[$var];
00114 else
00115 eZDebug::writeWarning( "Undefined get variable: $var",
00116 "eZHTTPTool" );
00117 return $ret;
00118 }
00119
00120
00121
00122
00123
00124 function hasGetVariable( $var )
00125 {
00126 return isset( $_GET[$var] );
00127 }
00128
00129
00130
00131
00132
00133 function hasVariable( $var )
00134 {
00135
00136 if ( isset( $_POST[$var] ) )
00137 {
00138 return isset( $_POST[$var] );
00139 }
00140 else
00141 {
00142 return isset( $_GET[$var] );
00143 }
00144 }
00145
00146
00147
00148
00149
00150 function variable( $var )
00151 {
00152 if ( isset( $_POST[$var] ) )
00153 {
00154 return $_POST[$var];
00155 }
00156 else if ( isset( $_GET[$var] ) )
00157 {
00158 return $_GET[$var];
00159 }
00160 $ret = false;
00161 return $ret;
00162 }
00163
00164
00165
00166
00167 function attributes()
00168 {
00169 return array( "post", "get", "session" );
00170 }
00171
00172
00173
00174
00175 function hasAttribute( $attr )
00176 {
00177 return in_array( $attr, $this->attributes() );
00178 }
00179
00180
00181
00182
00183 function &attribute( $attr )
00184 {
00185 if ( $attr == "post" )
00186 return $_POST;
00187 if ( $attr == "get" )
00188 return $_GET;
00189 if ( $attr == "session" )
00190 {
00191 return $_SESSION;
00192 }
00193 $retValue = null;
00194 return $retValue;
00195 }
00196
00197
00198
00199
00200 function &instance()
00201 {
00202 $instance =& $GLOBALS["eZHTTPToolInstance"];
00203 if ( get_class( $instance ) != "ezhttptool" )
00204 {
00205 $instance = new eZHTTPTool();
00206 $instance->createPostVarsFromImageButtons();
00207 eZSessionStart();
00208 }
00209 return $instance;
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 function sendHTTPRequest( $uri, $port = 80, $postParameters = false, $userAgent = 'eZ publish', $passtrough = true )
00229 {
00230 preg_match( "/^((http[s]?:\/\/)([a-zA-Z0-9_.]+))?([\/]?[~]?(\.?[^.]+[~]?)*)/i", $uri, $matches );
00231 $protocol = $matches[2];
00232 $host = $matches[3];
00233 $path = $matches[4];
00234 if ( !$path )
00235 {
00236 $path = '/';
00237 }
00238
00239 $data = '';
00240 if ( $postParameters )
00241 {
00242 $method = 'POST';
00243 $dataCount = 0;
00244 foreach( array_keys( $postParameters ) as $paramName )
00245 {
00246 if ( $dataCount > 0 )
00247 {
00248 $data .= '&';
00249 }
00250 ++$dataCount;
00251 if ( !is_array( $postParameters[$paramName] ) )
00252 {
00253 $data .= urlencode( $paramName ) . '=' . urlencode( $postParameters[$paramName] );
00254 }
00255 else
00256 {
00257 foreach( $postParameters[$paramName] as $value )
00258 {
00259 $data .= urlencode( $paramName ) . '[]=' . urlencode( $value );
00260 }
00261 }
00262 }
00263 }
00264 else
00265 {
00266 $method = 'GET';
00267 }
00268
00269 if ( !$host )
00270 {
00271 $host = $_SERVER['HTTP_HOST'];
00272 $filename = $host;
00273 if ( $path[0] != '/' )
00274 {
00275 $path = $_SERVER['SCRIPT_NAME'] . '/' . $path;
00276 }
00277 else
00278 {
00279 $path = $_SERVER['SCRIPT_NAME'] . $path;
00280 }
00281 }
00282 else{
00283 if ( !$protocol || $protocol == 'https://' )
00284 {
00285 $filename = 'ssl://' . $host;
00286 }
00287 else
00288 {
00289 $filename = 'tcp://' . $host;
00290 }
00291 }
00292
00293
00294 $parsedUrl = parse_url( $filename );
00295 $ip = isset( $parsedUrl[ 'host' ] ) ? gethostbyname( $parsedUrl[ 'host' ] ) : '';
00296 $checkIP = ip2long( $ip );
00297 if ( $checkIP == -1 or $checkIP === false )
00298 {
00299 return false;
00300 }
00301
00302 $fp = fsockopen( $filename, $port );
00303
00304
00305
00306 if ( !$fp )
00307 {
00308 return false;
00309 }
00310
00311 $request = $method . ' ' . $path . ' ' . 'HTTP/1.1' . "\r\n" .
00312 "Host: $host\r\n" .
00313 "Accept: */*\r\n" .
00314 "Content-type: application/x-www-form-urlencoded\r\n" .
00315 "Content-length: " . strlen( $data ) . "\r\n" .
00316 "User-Agent: $userAgent\r\n" .
00317 "Pragma: no-cache\r\n" .
00318 "Connection: close\r\n\r\n";
00319
00320 fputs( $fp, $request );
00321 if ( $method == 'POST' )
00322 {
00323 fputs( $fp, $data );
00324 }
00325
00326 $buf = '';
00327 if ( $passtrough )
00328 {
00329 ob_end_clean();
00330 $header = true;
00331
00332 $character = '';
00333 while( $header )
00334 {
00335 $buffer = $character;
00336 while ( !feof( $fp ) )
00337 {
00338 $character = fgetc( $fp );
00339 if ( $character == "\r" )
00340 {
00341 fgetc( $fp );
00342 $character = fgetc( $fp );
00343 if ( $character == "\r" )
00344 {
00345 fgetc( $fp );
00346 $header = false;
00347 }
00348 break;
00349 }
00350 else
00351 {
00352 $buffer .= $character;
00353 }
00354 }
00355
00356 header( $buffer );
00357 }
00358
00359 header( 'Content-Location: ' . $uri );
00360
00361 fpassthru( $fp );
00362 include_once( 'lib/ezutils/classes/ezexecution.php' );
00363 eZExecution::cleanExit();
00364 }
00365 else
00366 {
00367 $buf = '';
00368 while ( !feof( $fp ) )
00369 {
00370 $buf .= fgets( $fp, 128 );
00371 }
00372 }
00373
00374 fclose($fp);
00375 return $buf;
00376 }
00377
00378
00379
00380
00381 function parseHTTPResponse( &$response, &$header, &$body )
00382 {
00383 if ( $response )
00384 {
00385 $crlf = "\r\n";
00386
00387
00388 $pos = strpos( $response, $crlf . $crlf );
00389 if ( $pos !== false )
00390 {
00391 $headerBuf = substr( $response, 0, $pos );
00392 $body = substr( $response, $pos + 2 * strlen( $crlf ) );
00393
00394
00395 $header = array();
00396 $lines = explode( $crlf, $headerBuf );
00397 foreach ( $lines as $line )
00398 {
00399 if ( ( $pos = strpos( $line, ':') ) !== false )
00400 {
00401 $header[strtolower( trim( substr( $line, 0, $pos ) ) )] = trim( substr( $line, $pos+1 ) );
00402 }
00403 }
00404
00405 return true;
00406 }
00407 }
00408
00409 return false;
00410 }
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436 function createRedirectUrl( $path, $parameters = array() )
00437 {
00438 $parameters = array_merge( array( 'host' => false,
00439 'protocol' => false,
00440 'port' => false,
00441 'username' => false,
00442 'password' => false,
00443 'override_host' => false,
00444 'override_protocol' => false,
00445 'override_port' => false,
00446 'override_username' => false,
00447 'override_password' => false,
00448 'pre_url' => true ),
00449 $parameters );
00450 $host = $parameters['host'];
00451 $protocol = $parameters['protocol'];
00452 $port = $parameters['port'];
00453 $username = $parameters['username'];
00454 $password = $parameters['password'];
00455 if ( preg_match( '#^([a-zA-Z0-9]+):(.+)$#', $path, $matches ) )
00456 {
00457 if ( $matches[1] )
00458 $protocol = $matches[1];
00459 $path = $matches[2];
00460
00461 }
00462 if ( preg_match( '#^//((([a-zA-Z0-9_.]+)(:([a-zA-Z0-9_.]+))?)@)?([^./:]+(\.[^./:]+)*)(:([0-9]+))?(.*)$#', $path, $matches ) )
00463 {
00464 if ( $matches[6] )
00465 {
00466 $host = $matches[6];
00467 }
00468
00469 if ( $matches[3] )
00470 $username = $matches[3];
00471 if ( $matches[5] )
00472 $password = $matches[5];
00473 if ( $matches[9] )
00474 $port = $matches[9];
00475 $path = $matches[10];
00476 }
00477 if ( $parameters['pre_url'] )
00478 {
00479 if ( strlen( $path ) > 0 and
00480 $path[0] != '/' )
00481 {
00482 $preURL = eZSys::serverVariable( 'SCRIPT_URL' );
00483 if ( strlen( $preURL ) > 0 and
00484 $preURL[strlen($preURL) - 1] != '/' )
00485 $preURL .= '/';
00486 $path = $preURL . $path;
00487 }
00488 }
00489
00490 if ( $parameters['override_host'] )
00491 $host = $parameters['override_host'];
00492 if ( $parameters['override_port'] )
00493 $port = $parameters['override_port'];
00494 if ( !is_string( $host ) )
00495 $host = eZSys::hostname();
00496 if ( !is_string( $protocol ) )
00497 {
00498 $protocol = 'http';
00499
00500
00501
00502 $ini =& eZINI::instance();
00503 $sslPort = 443;
00504 if ( $ini->hasVariable( 'SiteSettings', 'SSLPort' ) )
00505 {
00506 $sslPort = $ini->variable( 'SiteSettings', 'SSLPort' );
00507 }
00508
00509 if ( eZSys::serverPort() == $sslPort )
00510 {
00511 $protocol = 'https';
00512 $port = false;
00513 }
00514 }
00515 if ( $parameters['override_protocol'] )
00516 $host = $parameters['override_protocol'];
00517
00518 $uri = $protocol . '://';
00519 if ( $parameters['override_username'] )
00520 $username = $parameters['override_username'];
00521 if ( $parameters['override_password'] )
00522 $password = $parameters['override_password'];
00523 if ( $username )
00524 {
00525 $uri .= $username;
00526 if ( $password )
00527 $uri .= ':' . $password;
00528 $uri .= '@';
00529 }
00530 $uri .= $host;
00531 if ( $port )
00532 $uri .= ':' . $port;
00533 $uri .= $path;
00534 return $uri;
00535 }
00536
00537 function redirect( $path, $parameters = array(), $status = false )
00538 {
00539 $uri = eZHTTPTool::createRedirectUrl( $path, $parameters );
00540 if ( strlen( $status ) > 0 )
00541 {
00542 header( $_SERVER['SERVER_PROTOCOL'] . " " . $status );
00543 eZHTTPTool::headerVariable( "Status", $status );
00544 }
00545 eZHTTPTool::headerVariable( 'Location', $uri );
00546
00547
00548 echo '<HTML><HEAD>';
00549 echo '<META HTTP-EQUIV="Refresh" Content="0;URL='. htmlspecialchars( $uri ) .'">';
00550 echo '<META HTTP-EQUIV="Location" Content="'. htmlspecialchars( $uri ) .'">';
00551 echo '</HEAD><BODY></BODY></HTML>';
00552 }
00553
00554
00555
00556
00557
00558
00559 function headerVariable( $headerName, $headerData )
00560 {
00561 header( $headerName .': '. $headerData );
00562 }
00563
00564 function removeMagicQuotes()
00565 {
00566 foreach ( array_keys( $_POST ) as $key )
00567 {
00568 if ( !is_array( $_POST[$key] ) )
00569 {
00570 $_POST[$key] = str_replace( "\'", "'", $_POST[$key] );
00571 $_POST[$key] = str_replace( '\"', '"', $_POST[$key] );
00572 $_POST[$key] = str_replace( '\\\\', '\\', $_POST[$key] );
00573 }
00574 else
00575 {
00576 foreach ( array_keys( $_POST[$key] ) as $arrayKey )
00577 {
00578 $_POST[$key][$arrayKey] = str_replace( "\'", "'", $_POST[$key][$arrayKey] );
00579 $_POST[$key][$arrayKey] = str_replace( '\"', '"', $_POST[$key][$arrayKey] );
00580 $_POST[$key][$arrayKey] = str_replace( '\\\\', '\\', $_POST[$key][$arrayKey] );
00581 }
00582 }
00583 }
00584 foreach ( array_keys( $_GET ) as $key )
00585 {
00586 if ( !is_array( $_GET[$key] ) )
00587 {
00588 $_GET[$key] = str_replace( "\'", "'", $_GET[$key] );
00589 $_GET[$key] = str_replace( '\"', '"', $_GET[$key] );
00590 $_GET[$key] = str_replace( '\\\\', '\\', $_GET[$key] );
00591 }
00592 else
00593 {
00594 foreach ( array_keys( $_GET[$key] ) as $arrayKey )
00595 {
00596 $_GET[$key][$arrayKey] = str_replace( "\'", "'", $_GET[$key][$arrayKey] );
00597 $_GET[$key][$arrayKey] = str_replace( '\"', '"', $_GET[$key][$arrayKey] );
00598 $_GET[$key][$arrayKey] = str_replace( '\\\\', '\\', $_GET[$key][$arrayKey] );
00599 }
00600 }
00601 }
00602 }
00603
00604 function createPostVarsFromImageButtons()
00605 {
00606 foreach ( array_keys( $_POST ) as $key )
00607 {
00608 if ( substr( $key, -2 ) == '_x' )
00609 {
00610 $yKey = substr( $key, 0, -2 ) . '_y';
00611 if ( array_key_exists( $yKey, $_POST ) )
00612 {
00613 $keyClean = substr( $key, 0, -2 );
00614 $matches = array();
00615 if ( preg_match( "/_(\d+)$/", $keyClean, $matches ) )
00616 {
00617 $value = $matches[1];
00618 $keyClean = preg_replace( "/(_\d+)$/","", $keyClean );
00619 $_POST[$keyClean] = $value;
00620
00621 }
00622 else
00623 {
00624 $_POST[$keyClean] = true;
00625
00626 }
00627 }
00628 }
00629 }
00630 }
00631
00632
00633
00634
00635 function getSessionKey()
00636 {
00637 return session_id();
00638 }
00639
00640 function setSessionKey( $sessionKey )
00641 {
00642 return session_id( $sessionKey );
00643 }
00644
00645 function setSessionVariable( $name, $value )
00646 {
00647 $_SESSION[$name] =& $value;
00648 }
00649
00650
00651
00652
00653 function removeSessionVariable( $name )
00654 {
00655 unset( $_SESSION[$name] );
00656 }
00657
00658
00659
00660
00661 function hasSessionVariable( $name )
00662 {
00663 return isset( $_SESSION[$name] );
00664 }
00665
00666
00667
00668
00669 function &sessionVariable( $name )
00670 {
00671 return $_SESSION[$name];
00672 }
00673
00674
00675
00676
00677 function sessionID()
00678 {
00679 return session_id();
00680 }
00681
00682
00683
00684
00685
00686
00687
00688 function getDataByURL( $url, $justCheckURL = false, $userAgent = false )
00689 {
00690
00691 if ( extension_loaded( 'curl' ) )
00692 {
00693 $ch = curl_init( $url );
00694 if ( $justCheckURL )
00695 {
00696 curl_setopt( $ch, CURLOPT_TIMEOUT, 15 );
00697 curl_setopt( $ch, CURLOPT_FAILONERROR, 1 );
00698 curl_setopt( $ch, CURLOPT_NOBODY, 1 );
00699 }
00700
00701 if ( $userAgent )
00702 {
00703 curl_setopt( $ch, CURLOPT_USERAGENT, $userAgent );
00704 }
00705
00706 $ini =& eZINI::instance();
00707 $proxy = $ini->hasVariable( 'ProxySettings', 'ProxyServer' ) ? $ini->variable( 'ProxySettings', 'ProxyServer' ) : false;
00708
00709 if ( $proxy )
00710 {
00711 curl_setopt ( $ch, CURLOPT_PROXY , $proxy );
00712 $userName = $ini->hasVariable( 'ProxySettings', 'User' ) ? $ini->variable( 'ProxySettings', 'User' ) : false;
00713 $password = $ini->hasVariable( 'ProxySettings', 'Password' ) ? $ini->variable( 'ProxySettings', 'Password' ) : false;
00714 if ( $userName )
00715 {
00716 curl_setopt ( $ch, CURLOPT_PROXYUSERPWD, "$userName:$password" );
00717 }
00718 }
00719
00720 if ( $justCheckURL )
00721 {
00722 if ( !curl_exec( $ch ) )
00723 return false;
00724
00725 curl_close( $ch );
00726 return true;
00727 }
00728
00729 ob_start();
00730 if ( !curl_exec( $ch ) )
00731 return false;
00732
00733 curl_close ( $ch );
00734 $data = ob_get_contents();
00735 ob_end_clean();
00736
00737 return $data;
00738 }
00739
00740 if ( $userAgent )
00741 {
00742 ini_set( 'user_agent', $userAgent );
00743 }
00744
00745
00746 $fid = fopen( $url, 'r' );
00747 if ( $fid === false )
00748 {
00749 return false;
00750 }
00751
00752 if ( $justCheckURL )
00753 {
00754 if ( $fid )
00755 fclose( $fid );
00756
00757 return $fid;
00758 }
00759
00760 $data = "";
00761 do
00762 {
00763 $dataBody = fread( $fid, 8192 );
00764 if ( strlen( $dataBody ) == 0 )
00765 break;
00766 $data .= $dataBody;
00767 } while( true );
00768
00769 fclose( $fid );
00770 return $data;
00771 }
00772 }
00773
00774 ?>