eZ Publish  [trunk]
ezhttptool.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZHTTPTool class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*! \defgroup eZHTTP HTTP utilities
00012     \ingroup eZUtils */
00013 
00014 /*!
00015   \class eZHTTPTool ezhttptool.php
00016   \ingroup eZHTTP
00017   \brief Provides access to HTTP post,get and session variables
00018 
00019   See PHP manual on <a href="http://www.php.net/manual/fi/language.variables.predefined.php">Predefined Variables</a> for more information.
00020 
00021 */
00022 
00023 class eZHTTPTool
00024 {
00025     /*!
00026      Initializes the class. Use eZHTTPTool::instance to get a single instance.
00027     */
00028     function eZHTTPTool()
00029     {
00030         $this->UseFullUrl = false;
00031         $magicQuote = get_magic_quotes_gpc();
00032 
00033         if ( $magicQuote == 1 )
00034         {
00035             eZHTTPTool::removeMagicQuotes();
00036         }
00037     }
00038 
00039     /*!
00040      Sets the post variable \a $var to \a $value.
00041      \sa postVariable
00042     */
00043     function setPostVariable( $var, $value )
00044     {
00045         $_POST[$var] = $value;
00046     }
00047 
00048     /*!
00049      \return the HTTP post variable $var, or $fallbackValue if the post variable does not exist , or null if $fallbackValue is omitted.
00050      \sa variable
00051     */
00052    function postVariable( $var, $fallbackValue = null )
00053     {
00054         $ret = $fallbackValue;
00055         if ( isset( $_POST[$var] ) )
00056         {
00057             $ret = $_POST[$var];
00058         }
00059         else if ( $ret === null )
00060         {
00061             eZDebug::writeWarning( "Undefined post variable: $var",
00062                                    "eZHTTPTool" );
00063         }
00064         return $ret;
00065     }
00066 
00067     /*!
00068      \return true if the HTTP post variable $var exist.
00069      \sa hasVariable
00070     */
00071     function hasPostVariable( $var )
00072     {
00073         return isset( $_POST[$var] );
00074     }
00075 
00076     /*!
00077      Sets the get variable \a $var to \a $value.
00078      \sa getVariable
00079     */
00080     function setGetVariable( $var, $value )
00081     {
00082         $_GET[$var] = $value;
00083     }
00084 
00085     /*!
00086      \return the HTTP get variable $var, or $fallbackValue if the get variable does not exist, or null if $fallbackValue is omitted.
00087      \sa variable
00088     */
00089     function getVariable( $var, $fallbackValue = null )
00090     {
00091         $ret = $fallbackValue;
00092         if ( isset( $_GET[$var] ) )
00093         {
00094             $ret = $_GET[$var];
00095         }
00096         else if ( $ret === null )
00097         {
00098             eZDebug::writeWarning( "Undefined get variable: $var",
00099                                    "eZHTTPTool" );
00100         }
00101         return $ret;
00102     }
00103 
00104     /*!
00105      \return true if the HTTP get variable $var exist.
00106      \sa hasVariable
00107     */
00108     function hasGetVariable( $var )
00109     {
00110         return isset( $_GET[$var] );
00111     }
00112 
00113     /*!
00114      \return true if the HTTP post/get variable $var exists.
00115      \sa hasPostVariable
00116     */
00117     function hasVariable( $var )
00118     {
00119 
00120         if ( isset( $_POST[$var] ) )
00121         {
00122             return isset( $_POST[$var] );
00123         }
00124         else
00125         {
00126             return isset( $_GET[$var] );
00127         }
00128     }
00129 
00130     /*!
00131      \return the HTTP post/get variable $var, or $fallbackValue if the post/get variable does not exist , or null if $fallbackValue is omitted.
00132      \sa postVariable
00133     */
00134     function variable( $var, $fallbackValue = null )
00135     {
00136         if ( isset( $_POST[$var] ) )
00137         {
00138             return $_POST[$var];
00139         }
00140         else if ( isset( $_GET[$var] ) )
00141         {
00142             return $_GET[$var];
00143         }
00144         $ret = $fallbackValue;
00145         if ( $ret === null )
00146         {
00147             eZDebug::writeWarning( "Undefined post/get variable: $var",
00148                                    "eZHTTPTool" );
00149         }
00150         return $ret;
00151     }
00152 
00153     /*!
00154      \return the attributes for this object.
00155     */
00156     function attributes()
00157     {
00158         return array( "post", "get", "session" );
00159     }
00160 
00161     /*!
00162      \return true if the attribute $attr exist.
00163     */
00164     function hasAttribute( $attr )
00165     {
00166         return in_array( $attr, $this->attributes() );
00167     }
00168 
00169     /*!
00170      \return the value for the attribute $attr or null if the attribute does not exist.
00171     */
00172     function attribute( $attr )
00173     {
00174         if ( $attr == "post" )
00175             return $_POST;
00176         if ( $attr == "get" )
00177             return $_GET;
00178         if ( $attr == "session" )
00179         {
00180             return eZSession::get();
00181         }
00182         $retValue = null;
00183         return $retValue;
00184     }
00185 
00186     /**
00187      * Return a unique instance of the eZHTTPTool class
00188      *
00189      * @return eZHTTPTool
00190      */
00191     static function instance()
00192     {
00193         if ( !isset( $GLOBALS["eZHTTPToolInstance"] ) ||
00194              !( $GLOBALS["eZHTTPToolInstance"] instanceof eZHTTPTool ) )
00195         {
00196             $GLOBALS["eZHTTPToolInstance"] = new eZHTTPTool();
00197             $GLOBALS["eZHTTPToolInstance"]->createPostVarsFromImageButtons();
00198         }
00199 
00200         return $GLOBALS["eZHTTPToolInstance"];
00201     }
00202 
00203     /**
00204      * Sends a http request to the specified host. Using https:// requires compiled in OpenSSL support.
00205      *
00206      * @param string $uri http/https address or only path to send request to current eZ Publish instance
00207      *        examples: http://ez.no, https://secure.ez.no or content/view/full/2
00208      * @param int|false $port Which port to connect to, default 80, uses port in $uri if present when $port = false
00209      * @param array|false $postParameters Optional post parameters array, if no post parameters are present, a GET request will be sent
00210      * @param string $userAgent User agent string, default will be 'eZ Publish'
00211      * @param bool $passthrough Will send result directly to client, default: true
00212      * @param array $cookies Optional hash of cookie name => values to add to http header
00213      * @return string|false String if http request, or false if an error occurs.
00214      *         If $passthrough = true, program will end here and send result directly to client.
00215     */
00216     static function sendHTTPRequest( $uri, $port = false, $postParameters = false, $userAgent = 'eZ Publish', $passthrough = true, array $cookies = array() )
00217     {
00218         preg_match( "/^((http[s]?:\/\/)([a-zA-Z0-9_.-]+)(\:(d+))?)?([\/]?[~]?(\.?[^.]+[~]?)*)/i", $uri, $matches );
00219         $protocol = $matches[2];
00220         $host     = $matches[3];
00221         $uriPort  = $matches[5];
00222         $path     = $matches[6];
00223 
00224         // Use port from uri if  set and port parameter evaluates to false
00225         if ( $uriPort && !$port )
00226             $port = $uriPort;
00227         else if ( !$port )
00228             $port = 80;
00229 
00230         if ( !$path )
00231         {
00232             $path = '/';
00233         }
00234 
00235         $data = '';
00236         if ( $postParameters )
00237         {
00238             $method = 'POST';
00239             foreach( $postParameters as $paramName => $paramData )
00240             {
00241                 if ( !is_array( $paramData) )
00242                 {
00243                     if ( $data !== '' )
00244                         $data .= '&';
00245                     $data .= urlencode( $paramName ) . '=' . urlencode( $paramData );
00246                 }
00247                 else
00248                 {
00249                     foreach( $paramData as $value )
00250                     {
00251                         if ( $data !== '' )
00252                             $data .= '&';
00253                         $data .= urlencode( $paramName ) . '[]=' . urlencode( $value );
00254                     }
00255                 }
00256             }
00257         }
00258         else
00259         {
00260             $method = 'GET';
00261         }
00262 
00263         if ( !$host )
00264         {
00265             $host = $_SERVER['HTTP_HOST'];
00266             $filename = $host;
00267             if ( $path[0] != '/' )
00268             {
00269                 $path = $_SERVER['SCRIPT_NAME'] . '/' . $path;
00270             }
00271             else
00272             {
00273                 $path = $_SERVER['SCRIPT_NAME'] . $path;
00274             }
00275         }
00276         else
00277         {
00278             if ( !$protocol || $protocol == 'https://' )
00279             {
00280                 $filename = 'ssl://' . $host;
00281             }
00282             else
00283             {
00284                 $filename = 'tcp://' . $host;
00285             }
00286         }
00287 
00288         // make sure we have a valid hostname or call to fsockopen() will fail
00289         $parsedUrl = parse_url( $filename );
00290         $ip = isset( $parsedUrl[ 'host' ] ) ? gethostbyname( $parsedUrl[ 'host' ] ) : '';
00291         $checkIP = ip2long( $ip );
00292         if ( $checkIP == -1 || $checkIP === false )
00293         {
00294             return false;
00295         }
00296 
00297         $fp = fsockopen( $filename, $port );
00298 
00299         // make sure we have a valid stream resource or calls to other file
00300         // functions will fail
00301         if ( !$fp )
00302         {
00303             return false;
00304         }
00305 
00306         $cookieStr = '';
00307         foreach ( $cookies as $name => $value )
00308         {
00309             if ( $cookieStr === '' )
00310                 $cookieStr = "Cookie: $name=$value";
00311             else
00312                 $cookieStr .= "; $name=$value";
00313         }
00314 
00315         $usePort = ( $port != 80 && $protocol === 'http://' ) && ( $port != 443 && $protocol === 'https://' );
00316         $request = $method . ' ' . $path . ' ' . 'HTTP/1.1' . "\r\n" .
00317              "Host: $host" . ( $usePort ? ":$port": '' ) . "\r\n" .
00318              "Accept: */*\r\n" .
00319              "Content-type: application/x-www-form-urlencoded\r\n" .
00320              "Content-length: " . strlen( $data ) . "\r\n" .
00321              $cookieStr .
00322              "User-Agent: $userAgent\r\n" .
00323              "Pragma: no-cache\r\n" .
00324              "Connection: close\r\n\r\n";
00325 
00326         fputs( $fp, $request );
00327         if ( $method == 'POST' )
00328         {
00329             fputs( $fp, $data );
00330         }
00331 
00332         $buf = '';
00333         if ( $passthrough )
00334         {
00335             ob_end_clean();
00336             $header = true;
00337 
00338             $character = '';
00339             while( $header )
00340             {
00341                 $buffer = $character;
00342                 while ( !feof( $fp ) )
00343                 {
00344                     $character = fgetc( $fp );
00345                     if ( $character == "\r" )
00346                     {
00347                         fgetc( $fp );
00348                         $character = fgetc( $fp );
00349                         if ( $character == "\r" )
00350                         {
00351                             fgetc( $fp );
00352                             $header = false;
00353                         }
00354                         break;
00355                     }
00356                     else
00357                     {
00358                         $buffer .= $character;
00359                     }
00360                 }
00361 
00362                 header( $buffer );
00363             }
00364 
00365             header( 'Content-Location: ' . $protocol . $host . ( $usePort ? ":$port": '' ) . $path );
00366 
00367             fpassthru( $fp );
00368             eZExecution::cleanExit();
00369         }
00370         else
00371         {
00372             $buf = '';
00373             while ( !feof( $fp ) )
00374             {
00375                 $buf .= fgets( $fp, 128 );
00376             }
00377         }
00378 
00379         fclose($fp);
00380         return $buf;
00381     }
00382 
00383     /*!
00384      \static
00385     */
00386     static function parseHTTPResponse( &$response, &$header, &$body )
00387     {
00388         if ( $response )
00389         {
00390             $crlf = "\r\n";
00391 
00392             // split header and body
00393             $pos = strpos( $response, $crlf . $crlf );
00394             if ( $pos !== false )
00395             {
00396                 $headerBuf = substr( $response, 0, $pos );
00397                 $body = substr( $response, $pos + 2 * strlen( $crlf ) );
00398 
00399                 // parse headers
00400                 $header = array();
00401                 $lines = explode( $crlf, $headerBuf );
00402                 foreach ( $lines as $line )
00403                 {
00404                     if ( ( $pos = strpos( $line, ':') ) !== false )
00405                     {
00406                         $header[strtolower( trim( substr( $line, 0, $pos ) ) )] = trim( substr( $line, $pos+1 ) );
00407                     }
00408                 }
00409 
00410                 return true;
00411             }
00412         }
00413 
00414         return false;
00415     }
00416 
00417     /*!
00418      \static
00419 
00420      Returns username from HTTP authentication or false if not logged in.
00421      See http://en.php.net/features.http-auth why you can`t safely use $_SERVER['PHP_AUTH_USER'].
00422     */
00423     static function username()
00424     {
00425         $ini = eZINI::instance();
00426         $AUTHKey = $ini->variable( 'SiteSettings', 'HTTPAUTHServerVariable' );
00427         $matches = array();
00428         if ( array_key_exists( 'PHP_AUTH_USER', $_SERVER ) )
00429         {
00430             return $_SERVER['PHP_AUTH_USER'];
00431         }
00432         elseif ( substr( php_sapi_name(), 0, 3 ) == 'cgi' and
00433                  array_key_exists( $AUTHKey, $_SERVER ) and
00434                  preg_match('/Basic\s+(.*)$/i', $_SERVER[$AUTHKey], $matches ) )
00435         {
00436             list( $name, $password ) = explode( ':', base64_decode( $matches[1] ) );
00437             return $name;
00438         }
00439         return false;
00440     }
00441 
00442     /*!
00443      \static
00444 
00445      Returns password from HTTP authentication or false if not logged in.
00446      See http://en.php.net/features.http-auth why you can`t safely use $_SERVER['PHP_AUTH_PW'].
00447     */
00448     static function password()
00449     {
00450         $ini = eZINI::instance();
00451         $AUTHKey = $ini->variable( 'SiteSettings', 'HTTPAUTHServerVariable' );
00452         $matches = array();
00453         if ( array_key_exists( 'PHP_AUTH_PW', $_SERVER ) )
00454         {
00455             return $_SERVER['PHP_AUTH_PW'];
00456         }
00457         elseif ( substr( php_sapi_name(), 0, 3 ) == 'cgi' and
00458                  array_key_exists( $AUTHKey, $_SERVER ) and
00459                  preg_match('/Basic\s+(.*)$/i', $_SERVER[$AUTHKey], $matches ) )
00460         {
00461             list( $name, $password ) = explode( ':', base64_decode( $matches[1] ) );
00462             return $password;
00463         }
00464         return false;
00465     }
00466 
00467     /*!
00468      \static
00469      Sends a redirect path to the browser telling it to
00470      load the new path.
00471      By default only \a $path is required, other parameters
00472      will be fetched automatically to create a HTTP/1.1
00473      compatible header.
00474      The input \a $parameters may contain the following keys.
00475      - host - the name of the host, default will fetch the currenty hostname
00476      - protocol - which protocol to use, default will use HTTP
00477      - port - the port on the host
00478      - username - a username which is required to login on the site
00479      - password - if username is supplied this password will be used for authentication
00480 
00481      The path may be specified relativily \c rel/ative, from root \c /a/root, with hostname
00482      change \c //myhost.com/a/root/rel/ative, with protocol \c http://myhost.com/a/root/rel/ative.
00483      Also port may be placed in the path string.
00484      It is recommended that the path only contain a plain root path and instead send the rest
00485      as optional parameters, the support for different kinds of paths is only incase you get
00486      URLs externally which contains any of the above cases.
00487 
00488      \note The redirection does not happen immedietaly and the script execution will continue.
00489     */
00490     static function createRedirectUrl( $path, $parameters = array() )
00491     {
00492         $parameters = array_merge( array( 'host' => false,
00493                                           'protocol' => false,
00494                                           'port' => false,
00495                                           'username' => false,
00496                                           'password' => false,
00497                                           'override_host' => false,
00498                                           'override_protocol' => false,
00499                                           'override_port' => false,
00500                                           'override_username' => false,
00501                                           'override_password' => false,
00502                                           'pre_url' => true ),
00503                                    $parameters );
00504         $host = $parameters['host'];
00505         $protocol = $parameters['protocol'];
00506         $port = $parameters['port'];
00507         $username = $parameters['username'];
00508         $password = $parameters['password'];
00509         if ( preg_match( '#^([a-zA-Z0-9]+):(.+)$#', $path, $matches ) )
00510         {
00511             if ( $matches[1] )
00512                 $protocol = $matches[1];
00513             $path = $matches[2];
00514 
00515         }
00516         if ( preg_match( '#^//((([a-zA-Z0-9_.]+)(:([a-zA-Z0-9_.]+))?)@)?([^./:]+(\.[^./:]+)*)(:([0-9]+))?(.*)$#', $path, $matches ) )
00517         {
00518             if ( $matches[6] )
00519             {
00520                 $host = $matches[6];
00521             }
00522 
00523             if ( $matches[3] )
00524                 $username = $matches[3];
00525             if ( $matches[5] )
00526                 $password = $matches[5];
00527             if ( $matches[9] )
00528                 $port = $matches[9];
00529             $path = $matches[10];
00530         }
00531         if ( $parameters['pre_url'] )
00532         {
00533             if ( strlen( $path ) > 0 and
00534                  $path[0] != '/' )
00535             {
00536                 $preURL = eZSys::serverVariable( 'SCRIPT_URL' );
00537                 if ( strlen( $preURL ) > 0 and
00538                      $preURL[strlen($preURL) - 1] != '/' )
00539                     $preURL .= '/';
00540                 $path = $preURL . $path;
00541             }
00542         }
00543 
00544         if ( $parameters['override_host'] )
00545             $host = $parameters['override_host'];
00546         if ( $parameters['override_port'] )
00547             $port = $parameters['override_port'];
00548         if ( !is_string( $host ) )
00549             $host = eZSys::hostname();
00550         if ( !is_string( $protocol ) )
00551         {
00552             $protocol = 'http';
00553             // Default to https if SSL is enabled
00554 
00555             // Check if SSL port is defined in site.ini
00556             $ini = eZINI::instance();
00557             $sslPort = 443;
00558             if ( $ini->hasVariable( 'SiteSettings', 'SSLPort' ) )
00559             {
00560                 $sslPort = $ini->variable( 'SiteSettings', 'SSLPort' );
00561             }
00562 
00563             if ( eZSys::serverPort() == $sslPort )
00564             {
00565                 $protocol = 'https';
00566                 $port = false;
00567             }
00568         }
00569         if ( $parameters['override_protocol'] )
00570             $protocol = $parameters['override_protocol'];
00571 
00572         $uri = $protocol . '://';
00573         if ( $parameters['override_username'] )
00574             $username = $parameters['override_username'];
00575         if ( $parameters['override_password'] )
00576             $password = $parameters['override_password'];
00577         if ( $username )
00578         {
00579             $uri .= $username;
00580             if ( $password )
00581                 $uri .= ':' . $password;
00582             $uri .= '@';
00583         }
00584         $uri .= $host;
00585         if ( $port )
00586             $uri .= ':' . $port;
00587         $uri .= $path;
00588         return $uri;
00589     }
00590 
00591     /**
00592      * \static
00593      * Performs an HTTP redirect.
00594      *
00595      * \param  $path  The path to redirect
00596      * \param  $parameters  \see createRedirectUrl()
00597      * \param  $status  The HTTP status code as a string
00598      * \param  $encodeURL  Encode the URL. This should normally be true, but
00599      * may be set to false to avoid double encoding when redirect() is called
00600      * twice.
00601      */
00602     static function redirect( $path, $parameters = array(), $status = false, $encodeURL = true )
00603     {
00604         $url = eZHTTPTool::createRedirectUrl( $path, $parameters );
00605         if ( strlen( $status ) > 0 )
00606         {
00607             header( $_SERVER['SERVER_PROTOCOL'] .  " " . $status );
00608             eZHTTPTool::headerVariable( "Status", $status );
00609         }
00610 
00611         if ( $encodeURL )
00612         {
00613             $url = eZURI::encodeURL( $url );
00614         }
00615 
00616         eZHTTPTool::headerVariable( 'Location', $url );
00617 
00618         /* Fix for redirecting using workflows and apache 2 */
00619         echo '<HTML><HEAD>';
00620         echo '<META HTTP-EQUIV="Refresh" Content="0;URL='. htmlspecialchars( $url ) .'">';
00621         echo '<META HTTP-EQUIV="Location" Content="'. htmlspecialchars( $url ) .'">';
00622         echo '</HEAD><BODY></BODY></HTML>';
00623     }
00624 
00625     /*!
00626      \static
00627      Sets the header variable \a $headerName to have the data \a $headerData.
00628      \note Calls PHPs header() with a constructed string.
00629     */
00630     static function headerVariable( $headerName, $headerData )
00631     {
00632         header( $headerName .': '. $headerData );
00633     }
00634 
00635     static function removeMagicQuotes()
00636     {
00637         foreach ( array_keys( $_POST ) as $key )
00638         {
00639             if ( !is_array( $_POST[$key] ) )
00640             {
00641                 $_POST[$key] = str_replace( "\'", "'", $_POST[$key] );
00642                 $_POST[$key] = str_replace( '\"', '"', $_POST[$key] );
00643                 $_POST[$key] = str_replace( '\\\\', '\\', $_POST[$key] );
00644             }
00645             else
00646             {
00647                 foreach ( array_keys( $_POST[$key] ) as $arrayKey )
00648                 {
00649                     $_POST[$key][$arrayKey] = str_replace( "\'", "'", $_POST[$key][$arrayKey] );
00650                     $_POST[$key][$arrayKey] = str_replace( '\"', '"', $_POST[$key][$arrayKey] );
00651                     $_POST[$key][$arrayKey] = str_replace( '\\\\', '\\', $_POST[$key][$arrayKey] );
00652                 }
00653             }
00654         }
00655         foreach ( array_keys( $_GET ) as $key )
00656         {
00657             if ( !is_array( $_GET[$key] ) )
00658             {
00659                 $_GET[$key] = str_replace( "\'", "'", $_GET[$key] );
00660                 $_GET[$key] = str_replace( '\"', '"', $_GET[$key] );
00661                 $_GET[$key] = str_replace( '\\\\', '\\', $_GET[$key] );
00662             }
00663             else
00664             {
00665                 foreach ( array_keys( $_GET[$key] ) as $arrayKey )
00666                 {
00667                     $_GET[$key][$arrayKey] = str_replace( "\'", "'", $_GET[$key][$arrayKey] );
00668                     $_GET[$key][$arrayKey] = str_replace( '\"', '"', $_GET[$key][$arrayKey] );
00669                     $_GET[$key][$arrayKey] = str_replace( '\\\\', '\\', $_GET[$key][$arrayKey] );
00670                 }
00671             }
00672         }
00673     }
00674 
00675     function createPostVarsFromImageButtons()
00676     {
00677         foreach ( array_keys( $_POST ) as $key )
00678         {
00679             if ( substr( $key, -2 ) == '_x' )
00680             {
00681                 $yKey = substr( $key, 0, -2 ) . '_y';
00682                 if ( array_key_exists( $yKey, $_POST ) )
00683                 {
00684                     $keyClean = substr( $key, 0, -2 );
00685                     $matches = array();
00686                     if ( preg_match( "/_(\d+)$/", $keyClean, $matches ) )
00687                     {
00688                         $value = $matches[1];
00689                         $keyClean = preg_replace( "/(_\d+)$/","", $keyClean );
00690                         $_POST[$keyClean] = $value;
00691 //                         eZDebug::writeDebug( $_POST[$keyClean], "We have create new  Post Var with name $keyClean and value $value:" );
00692                     }
00693                     else
00694                     {
00695                         $_POST[$keyClean] = true;
00696 //                         eZDebug::writeDebug( $_POST[$keyClean], "We have create new  Post Var with name $keyClean and value true:" );
00697                     }
00698                 }
00699             }
00700         }
00701     }
00702 
00703     /**
00704      * Return the session id
00705      *
00706      * @deprecated Since 4.4, use ->sessionID instead!
00707      * @return string
00708      */
00709     function getSessionKey()
00710     {
00711         return session_id();
00712     }
00713 
00714     /**
00715      * Sets a new session id
00716      *
00717      * @deprecated Since 4.4, use ->setSessionID instead!
00718      * @param string $sessionKey Allowed characters in the range a-z A-Z 0-9 , (comma) and - (minus)
00719      * @return string Current(old) session id
00720     */
00721     function setSessionKey( $sessionKey )
00722     {
00723         return session_id( $sessionKey );
00724     }
00725 
00726     /**
00727      * Sets the session variable $name to value $value.
00728      *
00729      * @param string $name
00730      * @param mixed $value
00731     */
00732     function setSessionVariable( $name, $value )
00733     {
00734         eZSession::set( $name, $value );
00735     }
00736 
00737     /**
00738      * Unset the session variable $name
00739      *
00740      * @param string $name
00741      * @return bool
00742      */
00743     function removeSessionVariable( $name )
00744     {
00745         return eZSession::unsetkey( $name );
00746     }
00747 
00748     /**
00749      * Check if session variable $name exists
00750      *
00751      * @param string $name
00752      * @param bool $forceStart Force session start if true (default)
00753      * @return bool|null Null if session has not started and $forceStart is false
00754      */
00755     function hasSessionVariable( $name, $forceStart = true )
00756     {
00757         return eZSession::issetkey( $name, $forceStart );
00758     }
00759 
00760     /**
00761      * Get session variable $name
00762      *
00763      * @param string $name
00764      * @param mixed $fallbackValue Return this if session has not started OR name is undefined
00765      *              if null(default), then force start session and return null if undefined.
00766      * @return mixed ByRef
00767      */
00768     function &sessionVariable( $name, $fallbackValue = null )
00769     {
00770         return eZSession::get( $name, $fallbackValue );
00771     }
00772 
00773     /**
00774      * Return the session id
00775      *
00776      * @return string
00777      */
00778     function sessionID()
00779     {
00780         return session_id();
00781     }
00782 
00783     /**
00784      * Sets a new session id
00785      *
00786      * @param string $sessionKey Allowed characters in the range a-z A-Z 0-9 , (comma) and - (minus)
00787      * @return string Current(old) session id
00788     */
00789     function setSessionID( $sessionKey )
00790     {
00791         return session_id( $sessionKey );
00792     }
00793 
00794     /*!
00795      \static
00796      \param $url
00797      \param $justCheckURL if true, we should check url only not downloading data.
00798      \return data from \p $url, false if invalid URL
00799     */
00800     static function getDataByURL( $url, $justCheckURL = false, $userAgent = false )
00801     {
00802         // First try CURL
00803         if ( extension_loaded( 'curl' ) )
00804         {
00805             $ch = curl_init( $url );
00806             // Options used to perform in a similar way than PHP's fopen()
00807             curl_setopt_array(
00808                 $ch,
00809                 array(
00810                     CURLOPT_FOLLOWLOCATION => true,
00811                     CURLOPT_SSL_VERIFYPEER => false
00812                 )
00813             );
00814             if ( $justCheckURL )
00815             {
00816                 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 2 );
00817                 curl_setopt( $ch, CURLOPT_TIMEOUT, 15 );
00818                 curl_setopt( $ch, CURLOPT_FAILONERROR, 1 );
00819                 curl_setopt( $ch, CURLOPT_NOBODY, 1 );
00820             }
00821 
00822             if ( $userAgent )
00823             {
00824                 curl_setopt( $ch, CURLOPT_USERAGENT, $userAgent );
00825             }
00826 
00827             $ini = eZINI::instance();
00828             $proxy = $ini->hasVariable( 'ProxySettings', 'ProxyServer' ) ? $ini->variable( 'ProxySettings', 'ProxyServer' ) : false;
00829             // If we should use proxy
00830             if ( $proxy )
00831             {
00832                 curl_setopt ( $ch, CURLOPT_PROXY , $proxy );
00833                 $userName = $ini->hasVariable( 'ProxySettings', 'User' ) ? $ini->variable( 'ProxySettings', 'User' ) : false;
00834                 $password = $ini->hasVariable( 'ProxySettings', 'Password' ) ? $ini->variable( 'ProxySettings', 'Password' ) : false;
00835                 if ( $userName )
00836                 {
00837                     curl_setopt ( $ch, CURLOPT_PROXYUSERPWD, "$userName:$password" );
00838                 }
00839             }
00840             // If we should check url without downloading data from it.
00841             if ( $justCheckURL )
00842             {
00843                 if ( !curl_exec( $ch ) )
00844                 {
00845                     curl_close( $ch );
00846                     return false;
00847                 }
00848 
00849                 curl_close( $ch );
00850                 return true;
00851             }
00852             // Getting data
00853             ob_start();
00854             if ( !curl_exec( $ch ) )
00855             {
00856                 curl_close( $ch );
00857                 ob_end_clean();
00858                 return false;
00859             }
00860 
00861             curl_close ( $ch );
00862             $data = ob_get_contents();
00863             ob_end_clean();
00864 
00865             return $data;
00866         }
00867 
00868         if ( $userAgent )
00869         {
00870             ini_set( 'user_agent', $userAgent );
00871         }
00872 
00873         // Open and read url
00874         $fid = fopen( $url, 'r' );
00875         if ( $fid === false )
00876         {
00877             return false;
00878         }
00879 
00880         if ( $justCheckURL )
00881         {
00882             if ( $fid )
00883                 fclose( $fid );
00884 
00885             return $fid;
00886         }
00887 
00888         $data = "";
00889         do
00890         {
00891             $dataBody = fread( $fid, 8192 );
00892             if ( strlen( $dataBody ) == 0 )
00893                 break;
00894             $data .= $dataBody;
00895         } while( true );
00896 
00897         fclose( $fid );
00898         return $data;
00899     }
00900 }
00901 
00902 ?>