eZ Publish  [4.0]
ezhttptool.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZHTTPTool class
00004 //
00005 // Created on: <18-Apr-2002 14:05:21 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \defgroup eZHTTP HTTP utilities
00032     \ingroup eZUtils */
00033 
00034 /*!
00035   \class eZHTTPTool ezhttptool.php
00036   \ingroup eZHTTP
00037   \brief Provides access to HTTP post,get and session variables
00038 
00039   See PHP manual on <a href="http://www.php.net/manual/fi/language.variables.predefined.php">Predefined Variables</a> for more information.
00040 
00041 */
00042 
00043 require_once( "lib/ezutils/classes/ezdebug.php" );
00044 require_once( "lib/ezutils/classes/ezsession.php" );
00045 //include_once( "lib/ezutils/classes/ezsys.php" );
00046 
00047 class eZHTTPTool
00048 {
00049     /*!
00050      Initializes the class. Use eZHTTPTool::instance to get a single instance.
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      Sets the post variable \a $var to \a $value.
00065      \sa postVariable
00066     */
00067     function setPostVariable( $var, $value )
00068     {
00069         $_POST[$var] = $value;
00070     }
00071 
00072     /*!
00073      \return a reference to the HTTP post variable $var, or null if it does not exist.
00074      \sa variable
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      \return true if the HTTP post variable $var exist.
00089      \sa hasVariable
00090     */
00091     function hasPostVariable( $var )
00092     {
00093         return isset( $_POST[$var] );
00094     }
00095 
00096     /*!
00097      Sets the get variable \a $var to \a $value.
00098      \sa getVariable
00099     */
00100     function setGetVariable( $var, $value )
00101     {
00102         $_GET[$var] = $value;
00103     }
00104 
00105     /*!
00106      \return a reference to the HTTP get variable $var, or null if it does not exist.
00107      \sa variable
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      \return true if the HTTP get variable $var exist.
00122      \sa hasVariable
00123     */
00124     function hasGetVariable( $var )
00125     {
00126         return isset( $_GET[$var] );
00127     }
00128 
00129     /*!
00130      \return true if the HTTP post/get variable $var exists.
00131      \sa hasPostVariable
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      \return a reference to the HTTP post/get variable $var, or null if it does not exist.
00148      \sa postVariable
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      \return the attributes for this object.
00166     */
00167     function attributes()
00168     {
00169         return array( "post", "get", "session" );
00170     }
00171 
00172     /*!
00173      \return true if the attribute $attr exist.
00174     */
00175     function hasAttribute( $attr )
00176     {
00177         return in_array( $attr, $this->attributes() );
00178     }
00179 
00180     /*!
00181      \return the value for the attribute $attr or null if the attribute does not exist.
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      \return the unique instance of the HTTP tool
00199     */
00200     static function instance()
00201     {
00202         if ( !isset( $GLOBALS["eZHTTPToolInstance"] ) ||
00203              !( $GLOBALS["eZHTTPToolInstance"] instanceof eZHTTPTool ) )
00204         {
00205             $GLOBALS["eZHTTPToolInstance"] = new eZHTTPTool();
00206             $GLOBALS["eZHTTPToolInstance"]->createPostVarsFromImageButtons();
00207             eZSessionStart();
00208         }
00209 
00210         return $GLOBALS["eZHTTPToolInstance"];
00211     }
00212 
00213     /*!
00214      \static
00215 
00216      Sends a http request to the specified host. Using https:// requires PHP 4.3.0, and compiled in OpenSSL support.
00217 
00218      \param uri http/https address, only path to send request to eZ Publish.
00219             examples: http://ez.no, https://secure.ez.no, ssl://secure.ez.no, content/view/full/2
00220      \param port which port to connect to, default 80
00221      \param postParameters post parameters array (optional), if no post parameters are present, a get request will be send.
00222      \param userAgent user agent, default will be eZ Publish
00223      \param passthrough will send result directly to client, default true
00224 
00225      \return result if http request, or return false if an error occurs.
00226              If pipetrough, program will end here.
00227 
00228     */
00229     static function sendHTTPRequest( $uri, $port = 80, $postParameters = false, $userAgent = 'eZ Publish', $passthrough = true )
00230     {
00231         preg_match( "/^((http[s]?:\/\/)([a-zA-Z0-9_.]+))?([\/]?[~]?(\.?[^.]+[~]?)*)/i", $uri, $matches );
00232         $protocol = $matches[2];
00233         $host = $matches[3];
00234         $path = $matches[4];
00235         if ( !$path )
00236         {
00237             $path = '/';
00238         }
00239 
00240         $data = '';
00241         if ( $postParameters )
00242         {
00243             $method = 'POST';
00244             $dataCount = 0;
00245             foreach( array_keys( $postParameters ) as $paramName )
00246             {
00247                 if ( $dataCount > 0 )
00248                 {
00249                     $data .= '&';
00250                 }
00251                 ++$dataCount;
00252                 if ( !is_array( $postParameters[$paramName] ) )
00253                 {
00254                     $data .= urlencode( $paramName ) . '=' . urlencode( $postParameters[$paramName] );
00255                 }
00256                 else
00257                 {
00258                     foreach( $postParameters[$paramName] as $value )
00259                     {
00260                         $data .= urlencode( $paramName ) . '[]=' . urlencode( $value );
00261                     }
00262                 }
00263             }
00264         }
00265         else
00266         {
00267             $method = 'GET';
00268         }
00269 
00270         if ( !$host )
00271         {
00272             $host = $_SERVER['HTTP_HOST'];
00273             $filename = $host;
00274             if ( $path[0] != '/' )
00275             {
00276                 $path = $_SERVER['SCRIPT_NAME'] . '/' . $path;
00277             }
00278             else
00279             {
00280                 $path = $_SERVER['SCRIPT_NAME'] . $path;
00281             }
00282         }
00283         else{
00284             if ( !$protocol || $protocol == 'https://' )
00285             {
00286                 $filename = 'ssl://' . $host;
00287             }
00288             else
00289             {
00290                 $filename = 'tcp://' . $host;
00291             }
00292         }
00293 
00294         // make sure we have a valid hostname or call to fsockopen() will fail
00295         $parsedUrl = parse_url( $filename );
00296         $ip = isset( $parsedUrl[ 'host' ] ) ? gethostbyname( $parsedUrl[ 'host' ] ) : '';
00297         $checkIP = ip2long( $ip );
00298         if ( $checkIP == -1 or $checkIP === false )
00299         {
00300             return false;
00301         }
00302 
00303         $fp = fsockopen( $filename, $port );
00304 
00305         // make sure we have a valid stream resource or calls to other file
00306         // functions will fail
00307         if ( !$fp )
00308         {
00309             return false;
00310         }
00311 
00312         $request = $method . ' ' . $path . ' ' . 'HTTP/1.1' . "\r\n" .
00313              "Host: $host\r\n" .
00314              "Accept: */*\r\n" .
00315              "Content-type: application/x-www-form-urlencoded\r\n" .
00316              "Content-length: " . strlen( $data ) . "\r\n" .
00317              "User-Agent: $userAgent\r\n" .
00318              "Pragma: no-cache\r\n" .
00319              "Connection: close\r\n\r\n";
00320 
00321         fputs( $fp, $request );
00322         if ( $method == 'POST' )
00323         {
00324             fputs( $fp, $data );
00325         }
00326 
00327         $buf = '';
00328         if ( $passthrough )
00329         {
00330             ob_end_clean();
00331             $header = true;
00332 
00333             $character = '';
00334             while( $header )
00335             {
00336                 $buffer = $character;
00337                 while ( !feof( $fp ) )
00338                 {
00339                     $character = fgetc( $fp );
00340                     if ( $character == "\r" )
00341                     {
00342                         fgetc( $fp );
00343                         $character = fgetc( $fp );
00344                         if ( $character == "\r" )
00345                         {
00346                             fgetc( $fp );
00347                             $header = false;
00348                         }
00349                         break;
00350                     }
00351                     else
00352                     {
00353                         $buffer .= $character;
00354                     }
00355                 }
00356 
00357                 header( $buffer );
00358             }
00359 
00360             header( 'Content-Location: ' . $uri );
00361 
00362             fpassthru( $fp );
00363             require_once( 'lib/ezutils/classes/ezexecution.php' );
00364             eZExecution::cleanExit();
00365         }
00366         else
00367         {
00368             $buf = '';
00369             while ( !feof( $fp ) )
00370             {
00371                 $buf .= fgets( $fp, 128 );
00372             }
00373         }
00374 
00375         fclose($fp);
00376         return $buf;
00377     }
00378 
00379     /*!
00380      \static
00381     */
00382     static function parseHTTPResponse( &$response, &$header, &$body )
00383     {
00384         if ( $response )
00385         {
00386             $crlf = "\r\n";
00387 
00388             // split header and body
00389             $pos = strpos( $response, $crlf . $crlf );
00390             if ( $pos !== false )
00391             {
00392                 $headerBuf = substr( $response, 0, $pos );
00393                 $body = substr( $response, $pos + 2 * strlen( $crlf ) );
00394 
00395                 // parse headers
00396                 $header = array();
00397                 $lines = explode( $crlf, $headerBuf );
00398                 foreach ( $lines as $line )
00399                 {
00400                     if ( ( $pos = strpos( $line, ':') ) !== false )
00401                     {
00402                         $header[strtolower( trim( substr( $line, 0, $pos ) ) )] = trim( substr( $line, $pos+1 ) );
00403                     }
00404                 }
00405 
00406                 return true;
00407             }
00408         }
00409 
00410         return false;
00411     }
00412 
00413     /*!
00414      \static
00415 
00416      Returns username from HTTP authentication or false if not logged in.
00417      See http://en.php.net/features.http-auth why you can`t safely use $_SERVER['PHP_AUTH_USER'].
00418     */
00419     static function username()
00420     {
00421         $ini = eZINI::instance();
00422         $AUTHKey = $ini->variable( 'SiteSettings', 'HTTPAUTHServerVariable' );
00423         $matches = array();
00424         if ( array_key_exists( 'PHP_AUTH_USER', $_SERVER ) )
00425         {
00426             return $_SERVER['PHP_AUTH_USER'];
00427         }
00428         elseif ( substr( php_sapi_name(), 0, 3 ) == 'cgi' and
00429                  array_key_exists( $AUTHKey, $_SERVER ) and
00430                  preg_match('/Basic\s+(.*)$/i', $_SERVER[$AUTHKey], $matches ) )
00431         {
00432             list( $name, $password ) = explode( ':', base64_decode( $matches[1] ) );
00433             return $name;
00434         }
00435         return false;
00436     }
00437 
00438     /*!
00439      \static
00440 
00441      Returns password from HTTP authentication or false if not logged in.
00442      See http://en.php.net/features.http-auth why you can`t safely use $_SERVER['PHP_AUTH_PW'].
00443     */
00444     static function password()
00445     {
00446         $ini = eZINI::instance();
00447         $AUTHKey = $ini->variable( 'SiteSettings', 'HTTPAUTHServerVariable' );
00448         $matches = array();
00449         if ( array_key_exists( 'PHP_AUTH_PW', $_SERVER ) )
00450         {
00451             return $_SERVER['PHP_AUTH_PW'];
00452         }
00453         elseif ( substr( php_sapi_name(), 0, 3 ) == 'cgi' and
00454                  array_key_exists( $AUTHKey, $_SERVER ) and
00455                  preg_match('/Basic\s+(.*)$/i', $_SERVER[$AUTHKey], $matches ) )
00456         {
00457             list( $name, $password ) = explode( ':', base64_decode( $matches[1] ) );
00458             return $password;
00459         }
00460         return false;
00461     }
00462 
00463     /*!
00464      \static
00465      Sends a redirect path to the browser telling it to
00466      load the new path.
00467      By default only \a $path is required, other parameters
00468      will be fetched automatically to create a HTTP/1.1
00469      compatible header.
00470      The input \a $parameters may contain the following keys.
00471      - host - the name of the host, default will fetch the currenty hostname
00472      - protocol - which protocol to use, default will use HTTP
00473      - port - the port on the host
00474      - username - a username which is required to login on the site
00475      - password - if username is supplied this password will be used for authentication
00476 
00477      The path may be specified relativily \c rel/ative, from root \c /a/root, with hostname
00478      change \c //myhost.com/a/root/rel/ative, with protocol \c http://myhost.com/a/root/rel/ative.
00479      Also port may be placed in the path string.
00480      It is recommended that the path only contain a plain root path and instead send the rest
00481      as optional parameters, the support for different kinds of paths is only incase you get
00482      URLs externally which contains any of the above cases.
00483 
00484      \note The redirection does not happen immedietaly and the script execution will continue.
00485     */
00486     static function createRedirectUrl( $path, $parameters = array() )
00487     {
00488         $parameters = array_merge( array( 'host' => false,
00489                                           'protocol' => false,
00490                                           'port' => false,
00491                                           'username' => false,
00492                                           'password' => false,
00493                                           'override_host' => false,
00494                                           'override_protocol' => false,
00495                                           'override_port' => false,
00496                                           'override_username' => false,
00497                                           'override_password' => false,
00498                                           'pre_url' => true ),
00499                                    $parameters );
00500         $host = $parameters['host'];
00501         $protocol = $parameters['protocol'];
00502         $port = $parameters['port'];
00503         $username = $parameters['username'];
00504         $password = $parameters['password'];
00505         if ( preg_match( '#^([a-zA-Z0-9]+):(.+)$#', $path, $matches ) )
00506         {
00507             if ( $matches[1] )
00508                 $protocol = $matches[1];
00509             $path = $matches[2];
00510 
00511         }
00512         if ( preg_match( '#^//((([a-zA-Z0-9_.]+)(:([a-zA-Z0-9_.]+))?)@)?([^./:]+(\.[^./:]+)*)(:([0-9]+))?(.*)$#', $path, $matches ) )
00513         {
00514             if ( $matches[6] )
00515             {
00516                 $host = $matches[6];
00517             }
00518 
00519             if ( $matches[3] )
00520                 $username = $matches[3];
00521             if ( $matches[5] )
00522                 $password = $matches[5];
00523             if ( $matches[9] )
00524                 $port = $matches[9];
00525             $path = $matches[10];
00526         }
00527         if ( $parameters['pre_url'] )
00528         {
00529             if ( strlen( $path ) > 0 and
00530                  $path[0] != '/' )
00531             {
00532                 $preURL = eZSys::serverVariable( 'SCRIPT_URL' );
00533                 if ( strlen( $preURL ) > 0 and
00534                      $preURL[strlen($preURL) - 1] != '/' )
00535                     $preURL .= '/';
00536                 $path = $preURL . $path;
00537             }
00538         }
00539 
00540         if ( $parameters['override_host'] )
00541             $host = $parameters['override_host'];
00542         if ( $parameters['override_port'] )
00543             $port = $parameters['override_port'];
00544         if ( !is_string( $host ) )
00545             $host = eZSys::hostname();
00546         if ( !is_string( $protocol ) )
00547         {
00548             $protocol = 'http';
00549             // Default to https if SSL is enabled
00550 
00551             // Check if SSL port is defined in site.ini
00552             $ini = eZINI::instance();
00553             $sslPort = 443;
00554             if ( $ini->hasVariable( 'SiteSettings', 'SSLPort' ) )
00555             {
00556                 $sslPort = $ini->variable( 'SiteSettings', 'SSLPort' );
00557             }
00558 
00559             if ( eZSys::serverPort() == $sslPort )
00560             {
00561                 $protocol = 'https';
00562                 $port = false;
00563             }
00564         }
00565         if ( $parameters['override_protocol'] )
00566             $host = $parameters['override_protocol'];
00567 
00568         $uri = $protocol . '://';
00569         if ( $parameters['override_username'] )
00570             $username = $parameters['override_username'];
00571         if ( $parameters['override_password'] )
00572             $password = $parameters['override_password'];
00573         if ( $username )
00574         {
00575             $uri .= $username;
00576             if ( $password )
00577                 $uri .= ':' . $password;
00578             $uri .= '@';
00579         }
00580         $uri .= $host;
00581         if ( $port )
00582             $uri .= ':' . $port;
00583         $uri .= $path;
00584         return $uri;
00585     }
00586 
00587     /**
00588      * \static
00589      * Performs an HTTP redirect.
00590      *
00591      * \param  $path  The path to redirect
00592      * \param  $parameters  \see createRedirectUrl()
00593      * \param  $status  The HTTP status code as a string
00594      * \param  $encodeURL  Encode the URL. This should normally be true, but
00595      * may be set to false to avoid double encoding when redirect() is called
00596      * twice.
00597      */
00598     static function redirect( $path, $parameters = array(), $status = false, $encodeURL = true )
00599     {
00600         $url = eZHTTPTool::createRedirectUrl( $path, $parameters );
00601         if ( strlen( $status ) > 0 )
00602         {
00603             header( $_SERVER['SERVER_PROTOCOL'] .  " " . $status );
00604             eZHTTPTool::headerVariable( "Status", $status );
00605         }
00606 
00607         if ( $encodeURL )
00608         {
00609             $url = eZURI::encodeURL( $url );
00610         }
00611 
00612         eZHTTPTool::headerVariable( 'Location', $url );
00613 
00614         /* Fix for redirecting using workflows and apache 2 */
00615         echo '<HTML><HEAD>';
00616         echo '<META HTTP-EQUIV="Refresh" Content="0;URL='. htmlspecialchars( $url ) .'">';
00617         echo '<META HTTP-EQUIV="Location" Content="'. htmlspecialchars( $url ) .'">';
00618         echo '</HEAD><BODY></BODY></HTML>';
00619     }
00620 
00621     /*!
00622      \static
00623      Sets the header variable \a $headerName to have the data \a $headerData.
00624      \note Calls PHPs header() with a constructed string.
00625     */
00626     static function headerVariable( $headerName, $headerData )
00627     {
00628         header( $headerName .': '. $headerData );
00629     }
00630 
00631     static function removeMagicQuotes()
00632     {
00633         foreach ( array_keys( $_POST ) as $key )
00634         {
00635             if ( !is_array( $_POST[$key] ) )
00636             {
00637                 $_POST[$key] = str_replace( "\'", "'", $_POST[$key] );
00638                 $_POST[$key] = str_replace( '\"', '"', $_POST[$key] );
00639                 $_POST[$key] = str_replace( '\\\\', '\\', $_POST[$key] );
00640             }
00641             else
00642             {
00643                 foreach ( array_keys( $_POST[$key] ) as $arrayKey )
00644                 {
00645                     $_POST[$key][$arrayKey] = str_replace( "\'", "'", $_POST[$key][$arrayKey] );
00646                     $_POST[$key][$arrayKey] = str_replace( '\"', '"', $_POST[$key][$arrayKey] );
00647                     $_POST[$key][$arrayKey] = str_replace( '\\\\', '\\', $_POST[$key][$arrayKey] );
00648                 }
00649             }
00650         }
00651         foreach ( array_keys( $_GET ) as $key )
00652         {
00653             if ( !is_array( $_GET[$key] ) )
00654             {
00655                 $_GET[$key] = str_replace( "\'", "'", $_GET[$key] );
00656                 $_GET[$key] = str_replace( '\"', '"', $_GET[$key] );
00657                 $_GET[$key] = str_replace( '\\\\', '\\', $_GET[$key] );
00658             }
00659             else
00660             {
00661                 foreach ( array_keys( $_GET[$key] ) as $arrayKey )
00662                 {
00663                     $_GET[$key][$arrayKey] = str_replace( "\'", "'", $_GET[$key][$arrayKey] );
00664                     $_GET[$key][$arrayKey] = str_replace( '\"', '"', $_GET[$key][$arrayKey] );
00665                     $_GET[$key][$arrayKey] = str_replace( '\\\\', '\\', $_GET[$key][$arrayKey] );
00666                 }
00667             }
00668         }
00669     }
00670 
00671     function createPostVarsFromImageButtons()
00672     {
00673         foreach ( array_keys( $_POST ) as $key )
00674         {
00675             if ( substr( $key, -2 ) == '_x' )
00676             {
00677                 $yKey = substr( $key, 0, -2 ) . '_y';
00678                 if ( array_key_exists( $yKey, $_POST ) )
00679                 {
00680                     $keyClean = substr( $key, 0, -2 );
00681                     $matches = array();
00682                     if ( preg_match( "/_(\d+)$/", $keyClean, $matches ) )
00683                     {
00684                         $value = $matches[1];
00685                         $keyClean = preg_replace( "/(_\d+)$/","", $keyClean );
00686                         $_POST[$keyClean] = $value;
00687 //                         eZDebug::writeDebug( $_POST[$keyClean], "We have create new  Post Var with name $keyClean and value $value:" );
00688                     }
00689                     else
00690                     {
00691                         $_POST[$keyClean] = true;
00692 //                         eZDebug::writeDebug( $_POST[$keyClean], "We have create new  Post Var with name $keyClean and value true:" );
00693                     }
00694                 }
00695             }
00696         }
00697     }
00698 
00699     /*!
00700      Sets the session variable $name to value $value.
00701     */
00702     function getSessionKey()
00703     {
00704         return session_id();
00705     }
00706 
00707     function setSessionKey( $sessionKey )
00708     {
00709         return session_id( $sessionKey );
00710     }
00711 
00712     function setSessionVariable( $name, $value )
00713     {
00714         $_SESSION[$name] =& $value;
00715     }
00716 
00717     /*!
00718      Removes the session variable $name.
00719     */
00720     function removeSessionVariable( $name )
00721     {
00722         unset( $_SESSION[$name] );
00723     }
00724 
00725     /*!
00726      \return true if the session variable $name exist.
00727     */
00728     function hasSessionVariable( $name )
00729     {
00730         return isset( $_SESSION[$name] );
00731     }
00732 
00733     /*!
00734      \return the session variable $name.
00735     */
00736     function &sessionVariable( $name )
00737     {
00738         return $_SESSION[$name];
00739     }
00740 
00741     /*!
00742      \return the session id
00743     */
00744     function sessionID()
00745     {
00746         return session_id();
00747     }
00748 
00749     /*!
00750      \static
00751      \param $url
00752      \param $justCheckURL if true, we should check url only not downloading data.
00753      \return data from \p $url, false if invalid URL
00754     */
00755     static function getDataByURL( $url, $justCheckURL = false, $userAgent = false )
00756     {
00757         // First try CURL
00758         if ( extension_loaded( 'curl' ) )
00759         {
00760             $ch = curl_init( $url );
00761             if ( $justCheckURL )
00762             {
00763                 curl_setopt( $ch, CURLOPT_TIMEOUT, 15 );
00764                 curl_setopt( $ch, CURLOPT_FAILONERROR, 1 );
00765                 curl_setopt( $ch, CURLOPT_NOBODY, 1 );
00766             }
00767 
00768             if ( $userAgent )
00769             {
00770                 curl_setopt( $ch, CURLOPT_USERAGENT, $userAgent );
00771             }
00772 
00773             $ini = eZINI::instance();
00774             $proxy = $ini->hasVariable( 'ProxySettings', 'ProxyServer' ) ? $ini->variable( 'ProxySettings', 'ProxyServer' ) : false;
00775             // If we should use proxy
00776             if ( $proxy )
00777             {
00778                 curl_setopt ( $ch, CURLOPT_PROXY , $proxy );
00779                 $userName = $ini->hasVariable( 'ProxySettings', 'User' ) ? $ini->variable( 'ProxySettings', 'User' ) : false;
00780                 $password = $ini->hasVariable( 'ProxySettings', 'Password' ) ? $ini->variable( 'ProxySettings', 'Password' ) : false;
00781                 if ( $userName )
00782                 {
00783                     curl_setopt ( $ch, CURLOPT_PROXYUSERPWD, "$userName:$password" );
00784                 }
00785             }
00786             // If we should check url without downloading data from it.
00787             if ( $justCheckURL )
00788             {
00789                 if ( !curl_exec( $ch ) )
00790                 {
00791                     curl_close( $ch );
00792                     return false;
00793                 }
00794 
00795                 curl_close( $ch );
00796                 return true;
00797             }
00798             // Getting data
00799             ob_start();
00800             if ( !curl_exec( $ch ) )
00801             {
00802                 curl_close( $ch );
00803                 ob_end_clean();
00804                 return false;
00805             }
00806 
00807             curl_close ( $ch );
00808             $data = ob_get_contents();
00809             ob_end_clean();
00810 
00811             return $data;
00812         }
00813 
00814         if ( $userAgent )
00815         {
00816             ini_set( 'user_agent', $userAgent );
00817         }
00818 
00819         // Open and read url
00820         $fid = fopen( $url, 'r' );
00821         if ( $fid === false )
00822         {
00823             return false;
00824         }
00825 
00826         if ( $justCheckURL )
00827         {
00828             if ( $fid )
00829                 fclose( $fid );
00830 
00831             return $fid;
00832         }
00833 
00834         $data = "";
00835         do
00836         {
00837             $dataBody = fread( $fid, 8192 );
00838             if ( strlen( $dataBody ) == 0 )
00839                 break;
00840             $data .= $dataBody;
00841         } while( true );
00842 
00843         fclose( $fid );
00844         return $data;
00845     }
00846 }
00847 
00848 ?>