Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezhttptool.php
Go to the documentation of this file.
1 <?php
23 class eZHTTPTool
24 {
28 public $UseFullUrl;
32 public function __construct()
33 {
34 $this->UseFullUrl = false;
35 }
36
41 function setPostVariable( $var, $value )
42 {
43 $_POST[$var] = $value;
44 }
45
50 function postVariable( $var, $fallbackValue = null )
51 {
52 $ret = $fallbackValue;
53 if ( isset( $_POST[$var] ) )
54 {
55 $ret = $_POST[$var];
56 }
57 else if ( $ret === null )
58 {
59 eZDebug::writeWarning( "Undefined post variable: $var",
60 "eZHTTPTool" );
61 }
62 return $ret;
63 }
64
69 function hasPostVariable( $var )
70 {
71 return isset( $_POST[$var] );
72 }
73
78 function setGetVariable( $var, $value )
79 {
80 $_GET[$var] = $value;
81 }
82
87 function getVariable( $var, $fallbackValue = null )
88 {
89 $ret = $fallbackValue;
90 if ( isset( $_GET[$var] ) )
91 {
92 $ret = $_GET[$var];
93 }
94 else if ( $ret === null )
95 {
96 eZDebug::writeWarning( "Undefined get variable: $var",
97 "eZHTTPTool" );
98 }
99 return $ret;
100 }
101
106 function hasGetVariable( $var )
107 {
108 return isset( $_GET[$var] );
109 }
110
115 function hasVariable( $var )
116 {
117
118 if ( isset( $_POST[$var] ) )
119 {
120 return isset( $_POST[$var] );
121 }
122 else
123 {
124 return isset( $_GET[$var] );
125 }
126 }
127
132 function variable( $var, $fallbackValue = null )
133 {
134 if ( isset( $_POST[$var] ) )
135 {
136 return $_POST[$var];
137 }
138 else if ( isset( $_GET[$var] ) )
139 {
140 return $_GET[$var];
141 }
142 $ret = $fallbackValue;
143 if ( $ret === null )
144 {
145 eZDebug::writeWarning( "Undefined post/get variable: $var",
146 "eZHTTPTool" );
147 }
148 return $ret;
149 }
150
154 function attributes()
155 {
156 return array( "post", "get", "session" );
157 }
158
162 function hasAttribute( $attr )
163 {
164 return in_array( $attr, $this->attributes() );
165 }
166
170 function attribute( $attr )
171 {
172 if ( $attr == "post" )
173 return $_POST;
174 if ( $attr == "get" )
175 return $_GET;
176 if ( $attr == "session" )
177 {
178 return eZSession::get();
179 }
180 $retValue = null;
181 return $retValue;
182 }
183
189 static function instance()
190 {
191 if ( !isset( $GLOBALS["eZHTTPToolInstance"] ) ||
192 !( $GLOBALS["eZHTTPToolInstance"] instanceof eZHTTPTool ) )
193 {
194 $GLOBALS["eZHTTPToolInstance"] = new eZHTTPTool();
195 $GLOBALS["eZHTTPToolInstance"]->createPostVarsFromImageButtons();
196 }
197
198 return $GLOBALS["eZHTTPToolInstance"];
199 }
200
214 static function sendHTTPRequest( $uri, $port = false, $postParameters = false, $userAgent = 'eZ Publish', $passthrough = true, array $cookies = array() )
215 {
216 preg_match( "/^((http[s]?:\/\/)([a-zA-Z0-9_.-]+)(\:(d+))?)?([\/]?[~]?(\.?[^.]+[~]?)*)/i", $uri, $matches );
217 $protocol = $matches[2];
218 $host = $matches[3];
219 $uriPort = $matches[5];
220 $path = $matches[6];
221
222 // Use port from uri if set and port parameter evaluates to false
223 if ( $uriPort && !$port )
224 $port = $uriPort;
225 else if ( !$port )
226 $port = ( $protocol === 'https://' ? 443 : 80 );
227
228 if ( !$path )
229 {
230 $path = '/';
231 }
232
233 $data = '';
234 if ( $postParameters )
235 {
236 $method = 'POST';
237 foreach( $postParameters as $paramName => $paramData )
238 {
239 if ( !is_array( $paramData) )
240 {
241 if ( $data !== '' )
242 $data .= '&';
243 $data .= urlencode( $paramName ) . '=' . urlencode( $paramData );
244 }
245 else
246 {
247 foreach( $paramData as $value )
248 {
249 if ( $data !== '' )
250 $data .= '&';
251 $data .= urlencode( $paramName ) . '[]=' . urlencode( $value );
252 }
253 }
254 }
255 }
256 else
257 {
258 $method = 'GET';
259 }
260
261 if ( !$host )
262 {
263 $host = $_SERVER['HTTP_HOST'];
264 $filename = $host;
265 if ( $path[0] != '/' )
266 {
267 $path = $_SERVER['SCRIPT_NAME'] . '/' . $path;
268 }
269 else
270 {
271 $path = $_SERVER['SCRIPT_NAME'] . $path;
272 }
273 }
274 else
275 {
276 if ( !$protocol || $protocol == 'https://' )
277 {
278 $filename = 'ssl://' . $host;
279 }
280 else
281 {
282 $filename = 'tcp://' . $host;
283 }
284 }
285
286 // make sure we have a valid hostname or call to fsockopen() will fail
287 $parsedUrl = parse_url( $filename );
288 $ip = isset( $parsedUrl[ 'host' ] ) ? gethostbyname( $parsedUrl[ 'host' ] ) : '';
289 $checkIP = ip2long( $ip );
290 if ( $checkIP == -1 || $checkIP === false )
291 {
292 return false;
293 }
294
295 $fp = fsockopen( $filename, $port );
296
297 // make sure we have a valid stream resource or calls to other file
298 // functions will fail
299 if ( !$fp )
300 {
301 return false;
302 }
303
304 $cookieStr = '';
305 foreach ( $cookies as $name => $value )
306 {
307 if ( $cookieStr === '' )
308 $cookieStr = "Cookie: $name=$value";
309 else
310 $cookieStr .= "; $name=$value";
311 }
312
313 $usePort = ( $port != 80 && $protocol === 'http://' ) && ( $port != 443 && $protocol === 'https://' );
314 $request = $method . ' ' . $path . ' ' . 'HTTP/1.1' . "\r\n" .
315 "Host: $host" . ( $usePort ? ":$port": '' ) . "\r\n" .
316 "Accept: */*\r\n" .
317 "Content-type: application/x-www-form-urlencoded\r\n" .
318 "Content-length: " . strlen( $data ) . "\r\n" .
319 $cookieStr .
320 "User-Agent: $userAgent\r\n" .
321 "Pragma: no-cache\r\n" .
322 "Connection: close\r\n\r\n";
323
324 fputs( $fp, $request );
325 if ( $method == 'POST' )
326 {
327 fputs( $fp, $data );
328 }
329
330 $buf = '';
331 if ( $passthrough )
332 {
333 ob_end_clean();
334 $header = true;
335
336 $character = '';
337 while( $header )
338 {
339 $buffer = $character;
340 while ( !feof( $fp ) )
341 {
342 $character = fgetc( $fp );
343 if ( $character == "\r" )
344 {
345 fgetc( $fp );
346 $character = fgetc( $fp );
347 if ( $character == "\r" )
348 {
349 fgetc( $fp );
350 $header = false;
351 }
352 break;
353 }
354 else
355 {
356 $buffer .= $character;
357 }
358 }
359
360 header( $buffer );
361 }
362
363 header( 'Content-Location: ' . $protocol . $host . ( $usePort ? ":$port": '' ) . $path );
364
365 fpassthru( $fp );
366 eZExecution::cleanExit();
367 }
368 else
369 {
370 $buf = '';
371 while ( !feof( $fp ) )
372 {
373 $buf .= fgets( $fp, 128 );
374 }
375 }
376
377 fclose($fp);
378 return $buf;
379 }
380
384 static function parseHTTPResponse( &$response, &$header, &$body )
385 {
386 if ( $response )
387 {
388 $crlf = "\r\n";
389
390 // split header and body
391 $pos = strpos( $response, $crlf . $crlf );
392 if ( $pos !== false )
393 {
394 $headerBuf = substr( $response, 0, $pos );
395 $body = substr( $response, $pos + 2 * strlen( $crlf ) );
396
397 // parse headers
398 $header = array();
399 $lines = explode( $crlf, $headerBuf );
400 foreach ( $lines as $line )
401 {
402 if ( ( $pos = strpos( $line, ':') ) !== false )
403 {
404 $header[strtolower( trim( substr( $line, 0, $pos ) ) )] = trim( substr( $line, $pos+1 ) );
405 }
406 }
407
408 return true;
409 }
410 }
411
412 return false;
413 }
414
421 static function username()
422 {
423 $ini = eZINI::instance();
424 $AUTHKey = $ini->variable( 'SiteSettings', 'HTTPAUTHServerVariable' );
425 $matches = array();
426 if ( array_key_exists( 'PHP_AUTH_USER', $_SERVER ) )
427 {
428 return $_SERVER['PHP_AUTH_USER'];
429 }
430 elseif ( substr( php_sapi_name(), 0, 3 ) == 'cgi' and
431 array_key_exists( $AUTHKey, $_SERVER ) and
432 preg_match('/Basic\s+(.*)$/i', $_SERVER[$AUTHKey], $matches ) )
433 {
434 list( $name, $password ) = explode( ':', base64_decode( $matches[1] ) );
435 return $name;
436 }
437 return false;
438 }
439
446 static function password()
447 {
448 $ini = eZINI::instance();
449 $AUTHKey = $ini->variable( 'SiteSettings', 'HTTPAUTHServerVariable' );
450 $matches = array();
451 if ( array_key_exists( 'PHP_AUTH_PW', $_SERVER ) )
452 {
453 return $_SERVER['PHP_AUTH_PW'];
454 }
455 elseif ( substr( php_sapi_name(), 0, 3 ) == 'cgi' and
456 array_key_exists( $AUTHKey, $_SERVER ) and
457 preg_match('/Basic\s+(.*)$/i', $_SERVER[$AUTHKey], $matches ) )
458 {
459 list( $name, $password ) = explode( ':', base64_decode( $matches[1] ) );
460 return $password;
461 }
462 return false;
463 }
464
488 static function createRedirectUrl( $path, $parameters = array() )
489 {
490 $parameters = array_merge( array( 'host' => false,
491 'protocol' => false,
492 'port' => false,
493 'username' => false,
494 'password' => false,
495 'override_host' => false,
496 'override_protocol' => false,
497 'override_port' => false,
498 'override_username' => false,
499 'override_password' => false,
500 'pre_url' => true ),
501 $parameters );
502 $host = $parameters['host'];
503 $protocol = $parameters['protocol'];
504 $port = $parameters['port'];
505 $username = $parameters['username'];
506 $password = $parameters['password'];
507 if ( preg_match( '#^([a-zA-Z0-9]+):(.+)$#', $path, $matches ) )
508 {
509 if ( $matches[1] )
510 $protocol = $matches[1];
511 $path = $matches[2];
512
513 }
514 if ( preg_match( '#^//((([a-zA-Z0-9_.]+)(:([a-zA-Z0-9_.]+))?)@)?([^./:]+(\.[^./:]+)*)(:([0-9]+))?(.*)$#', $path, $matches ) )
515 {
516 if ( $matches[6] )
517 {
518 $host = $matches[6];
519 }
520
521 if ( $matches[3] )
522 $username = $matches[3];
523 if ( $matches[5] )
524 $password = $matches[5];
525 if ( $matches[9] )
526 $port = $matches[9];
527 $path = $matches[10];
528 }
529 if ( $parameters['pre_url'] )
530 {
531 if ( strlen( $path ) > 0 and
532 $path[0] != '/' )
533 {
534 $preURL = eZSys::serverVariable( 'SCRIPT_URL' );
535 if ( strlen( $preURL ) > 0 and
536 $preURL[strlen($preURL) - 1] != '/' )
537 $preURL .= '/';
538 $path = $preURL . $path;
539 }
540 }
541
542 if ( $parameters['override_host'] )
543 $host = $parameters['override_host'];
544 if ( $parameters['override_port'] )
545 $port = $parameters['override_port'];
546 if ( !is_string( $host ) )
547 $host = eZSys::hostname();
548 if ( !is_string( $protocol ) )
549 {
550 $protocol = eZSys::serverProtocol();
551
552 // Default to https if SSL is enabled
553 if ( eZSys::isSSLNow() )
554 {
555 $port = false;
556 }
557 }
558 if ( $parameters['override_protocol'] )
559 $protocol = $parameters['override_protocol'];
560
561 $uri = $protocol . '://';
562 if ( $parameters['override_username'] )
563 $username = $parameters['override_username'];
564 if ( $parameters['override_password'] )
565 $password = $parameters['override_password'];
566 if ( $username )
567 {
568 $uri .= $username;
569 if ( $password )
570 $uri .= ':' . $password;
571 $uri .= '@';
572 }
573 $uri .= $host;
574 if ( $port )
575 $uri .= ':' . $port;
576 $uri .= $path;
577 return $uri;
578 }
579
593 static function redirect( $path, $parameters = array(), $status = false, $encodeURL = true, $returnRedirectObject = false )
594 {
595 $url = eZHTTPTool::createRedirectUrl( $path, $parameters );
596 if ( strlen( $status ) > 0 )
597 {
598 header( $_SERVER['SERVER_PROTOCOL'] . " " . $status );
599 eZHTTPTool::headerVariable( "Status", $status );
600 }
601
602 if ( $encodeURL )
603 {
604 $url = eZURI::encodeURL( $url );
605 }
606
607 eZHTTPTool::headerVariable( 'Location', $url );
608 /* Fix for redirecting using workflows and apache 2 */
609 $escapedUrl = htmlspecialchars( $url );
610 $content = <<<EOT
611 <HTML><HEAD>
612 <META HTTP-EQUIV="Refresh" Content="0;URL=$escapedUrl">
613 <META HTTP-EQUIV="Location" Content="$escapedUrl">
614 </HEAD><BODY></BODY></HTML>
615 EOT;
616
617 if ( $returnRedirectObject )
618 {
619 return new ezpKernelRedirect( $url, $status ?: null, $content );
620 }
621
622 echo $content;
623 }
624
630 static function headerVariable( $headerName, $headerData )
631 {
632 header( $headerName .': '. $headerData );
633 }
634
635 function createPostVarsFromImageButtons()
636 {
637 foreach ( array_keys( $_POST ) as $key )
638 {
639 if ( substr( $key, -2 ) == '_x' )
640 {
641 $yKey = substr( $key, 0, -2 ) . '_y';
642 if ( array_key_exists( $yKey, $_POST ) )
643 {
644 $keyClean = substr( $key, 0, -2 );
645 $matches = array();
646 if ( preg_match( "/_(\d+)$/", $keyClean, $matches ) )
647 {
648 $value = $matches[1];
649 $keyClean = preg_replace( "/(_\d+)$/","", $keyClean );
650 $_POST[$keyClean] = $value;
651 // eZDebug::writeDebug( $_POST[$keyClean], "We have create new Post Var with name $keyClean and value $value:" );
652 }
653 else
654 {
655 $_POST[$keyClean] = true;
656 // eZDebug::writeDebug( $_POST[$keyClean], "We have create new Post Var with name $keyClean and value true:" );
657 }
658 }
659 }
660 }
661 }
662
669 function setSessionVariable( $name, $value )
670 {
671 eZSession::set( $name, $value );
672 }
673
680 function removeSessionVariable( $name )
681 {
682 return eZSession::unsetkey( $name );
683 }
684
692 function hasSessionVariable( $name, $forceStart = true )
693 {
694 return eZSession::issetkey( $name, $forceStart );
695 }
696
705 function &sessionVariable( $name, $fallbackValue = null )
706 {
707 return eZSession::get( $name, $fallbackValue );
708 }
709
715 function sessionID()
716 {
717 return session_id();
718 }
719
726 function setSessionID( $sessionKey )
727 {
728 return session_id( $sessionKey );
729 }
730
731
739 static function getDataByURL( $url, $justCheckURL = false, $userAgent = false, $forceGetRequest = false )
740 {
741 // First try CURL
742 if ( extension_loaded( 'curl' ) )
743 {
744 $ch = curl_init( $url );
745 // Options used to perform in a similar way than PHP's fopen()
746 curl_setopt_array(
747 $ch,
748 array(
749 CURLOPT_RETURNTRANSFER => true,
750 CURLOPT_FOLLOWLOCATION => true,
751 )
752 );
753 $ini = eZINI::instance();
754 if ( $justCheckURL )
755 {
756 $connTimeout = $ini->hasVariable( 'LinkCheck', 'ConnectTimeout' ) ? (int)$ini->variable( 'LinkCheck', 'ConnectTimeout' ) : 3;
757 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $connTimeout );
758 curl_setopt( $ch, CURLOPT_TIMEOUT, 15 );
759 curl_setopt( $ch, CURLOPT_FAILONERROR, true );
760 curl_setopt( $ch, CURLOPT_NOBODY, true );
761 // force curl to use the 'GET' http method instead of 'HEAD' when just checking url
762 if ( $forceGetRequest )
763 {
764 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
765 }
766 }
767
768 if ( $userAgent )
769 {
770 curl_setopt( $ch, CURLOPT_USERAGENT, $userAgent );
771 }
772
773 $proxy = $ini->hasVariable( 'ProxySettings', 'ProxyServer' ) ? $ini->variable( 'ProxySettings', 'ProxyServer' ) : false;
774 // If we should use proxy
775 if ( $proxy )
776 {
777 curl_setopt ( $ch, CURLOPT_PROXY , $proxy );
778 $userName = $ini->hasVariable( 'ProxySettings', 'User' ) ? $ini->variable( 'ProxySettings', 'User' ) : false;
779 $password = $ini->hasVariable( 'ProxySettings', 'Password' ) ? $ini->variable( 'ProxySettings', 'Password' ) : false;
780 if ( $userName )
781 {
782 curl_setopt ( $ch, CURLOPT_PROXYUSERPWD, "$userName:$password" );
783 }
784 }
785
786 $result = curl_exec( $ch );
787 curl_close( $ch );
788
789 // If we should check url without downloading data from it.
790 if ( $justCheckURL )
791 {
792 if ( $result !== false )
793 {
794 return true;
795 }
796 if ( !$forceGetRequest )
797 {
798 // if HEAD request failed with anything other than a 404, repeating using a GET request
799 $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
800 if ( $httpCode != 404 )
801 {
802 return self::getDataByURL( $url, $justCheckURL, $userAgent, true );
803 }
804 }
805 return false;
806 }
807
808 return $result;
809 }
810
811 if ( $userAgent )
812 {
813 ini_set( 'user_agent', $userAgent );
814 }
815
816 // Open and read url
817 $fid = fopen( $url, 'r' );
818 if ( $fid === false )
819 {
820 return false;
821 }
822
823 if ( $justCheckURL )
824 {
825 if ( $fid )
826 fclose( $fid );
827
828 return $fid;
829 }
830
831 $data = "";
832 do
833 {
834 $dataBody = fread( $fid, 8192 );
835 if ( strlen( $dataBody ) == 0 )
836 break;
837 $data .= $dataBody;
838 } while( true );
839
840 fclose( $fid );
841 return $data;
842 }
843 }
844
845 ?>
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:502
eZExecution\cleanExit
static cleanExit()
Definition: ezexecution.php:100
eZHTTPTool
Provides access to HTTP post,get and session variables.
Definition: ezhttptool.php:24
eZHTTPTool\attributes
attributes()
Definition: ezhttptool.php:154
eZHTTPTool\hasAttribute
hasAttribute( $attr)
Definition: ezhttptool.php:162
eZHTTPTool\parseHTTPResponse
static parseHTTPResponse(&$response, &$header, &$body)
Definition: ezhttptool.php:384
eZHTTPTool\hasVariable
hasVariable( $var)
Definition: ezhttptool.php:115
eZHTTPTool\removeSessionVariable
removeSessionVariable( $name)
Definition: ezhttptool.php:680
eZHTTPTool\attribute
attribute( $attr)
Definition: ezhttptool.php:170
eZHTTPTool\createPostVarsFromImageButtons
createPostVarsFromImageButtons()
Definition: ezhttptool.php:635
eZHTTPTool\sessionVariable
& sessionVariable( $name, $fallbackValue=null)
Definition: ezhttptool.php:705
eZHTTPTool\hasSessionVariable
hasSessionVariable( $name, $forceStart=true)
Definition: ezhttptool.php:692
eZHTTPTool\headerVariable
static headerVariable( $headerName, $headerData)
Definition: ezhttptool.php:630
eZHTTPTool\createRedirectUrl
static createRedirectUrl( $path, $parameters=array())
Definition: ezhttptool.php:488
eZHTTPTool\$UseFullUrl
$UseFullUrl
Definition: ezhttptool.php:28
eZHTTPTool\setPostVariable
setPostVariable( $var, $value)
Definition: ezhttptool.php:41
eZHTTPTool\sendHTTPRequest
static sendHTTPRequest( $uri, $port=false, $postParameters=false, $userAgent='eZ Publish', $passthrough=true, array $cookies=array())
Definition: ezhttptool.php:214
eZHTTPTool\sessionID
sessionID()
Definition: ezhttptool.php:715
eZHTTPTool\setGetVariable
setGetVariable( $var, $value)
Definition: ezhttptool.php:78
eZHTTPTool\getDataByURL
static getDataByURL( $url, $justCheckURL=false, $userAgent=false, $forceGetRequest=false)
Definition: ezhttptool.php:739
eZHTTPTool\username
static username()
Definition: ezhttptool.php:421
eZHTTPTool\redirect
static redirect( $path, $parameters=array(), $status=false, $encodeURL=true, $returnRedirectObject=false)
Definition: ezhttptool.php:593
eZHTTPTool\__construct
__construct()
Definition: ezhttptool.php:32
eZHTTPTool\postVariable
postVariable( $var, $fallbackValue=null)
Definition: ezhttptool.php:50
eZHTTPTool\password
static password()
Definition: ezhttptool.php:446
eZHTTPTool\setSessionVariable
setSessionVariable( $name, $value)
Definition: ezhttptool.php:669
eZHTTPTool\setSessionID
setSessionID( $sessionKey)
Definition: ezhttptool.php:726
eZHTTPTool\hasGetVariable
hasGetVariable( $var)
Definition: ezhttptool.php:106
eZHTTPTool\getVariable
getVariable( $var, $fallbackValue=null)
Definition: ezhttptool.php:87
eZHTTPTool\variable
variable( $var, $fallbackValue=null)
Definition: ezhttptool.php:132
eZHTTPTool\instance
static instance()
Definition: ezhttptool.php:189
eZHTTPTool\hasPostVariable
hasPostVariable( $var)
Definition: ezhttptool.php:69
eZSys\isSSLNow
static isSSLNow()
Definition: ezsys.php:744
eZSys\serverVariable
static serverVariable( $variableName, $quiet=false)
Definition: ezsys.php:856
eZSys\serverProtocol
static serverProtocol()
Definition: ezsys.php:779
eZSys\hostname
static hostname()
Definition: ezsys.php:676
eZURI\encodeURL
static encodeURL( $url)
Definition: ezuri.php:194
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.