ezhttptool.php

00001 <?php
00002 //
00003 // Definition of eZHTTPTool class
00004 //
00005 // Created on: <18-Apr-2002 14:05:21 amos>
00006 //
00007 // Copyright (C) 1999-2003 Exponential. All rights reserved.
00008 //
00009 // This source file is part of the Exponential (tm) Open Source Content
00010 // Management System.
00011 //
00012 // This file may be distributed and/or modified under the terms of the
00013 // "GNU General Public License" version 2 as published by the Free
00014 // Software Foundation and appearing in the file LICENSE.GPL included in
00015 // the packaging of this file.
00016 //
00017 // Licencees holding valid "Exponential professional licences" may use this
00018 // file in accordance with the "Exponential professional licence" Agreement
00019 // provided with the Software.
00020 //
00021 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
00022 // THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00023 // PURPOSE.
00024 //
00025 // The "Exponential professional licence" is available at
00026 // http://ez.no/products/licences/professional/. For pricing of this licence
00027 // please contact us via e-mail to licence@ez.no. Further contact
00028 // information is available at http://ez.no/home/contact/.
00029 //
00030 // The "GNU General Public License" (GPL) is available at
00031 // http://www.gnu.org/copyleft/gpl.html.
00032 //
00033 // Contact licence@ez.no if any conditions of this licencing isn't clear to
00034 // you.
00035 //
00036 
00049 include_once( "lib/ezutils/classes/ezdebug.php" );
00050 include_once( "lib/ezutils/classes/ezsession.php" );
00051 include_once( "lib/ezutils/classes/ezsys.php" );
00052 
eZHTTPTool
00054 {
eZHTTPTool()
00059     {
00060         $magicQuote = get_magic_quotes_gpc();
00061 
00062         if ( $magicQuote == 1 )
00063         {
00064             eZHTTPTool::removeMagicQuotes();
00065         }
00066     }
00067 
setPostVariable( $var, $value )
00073     {
00074         $post_vars =& $GLOBALS["HTTP_POST_VARS"];
00075         $post_vars[$var] =& $value;
00076     }
00077 
postVariable( $var )
00083     {
00084         $post_vars =& $GLOBALS["HTTP_POST_VARS"];
00085         $ret = null;
00086         if ( isset( $post_vars[$var] ) )
00087             $ret =& $post_vars[$var];
00088         else
00089             eZDebug::writeWarning( "Undefined post variable: $var",
00090                                    "eZHTTPTool" );
00091         return $ret;
00092     }
00093 
hasPostVariable( $var )
00099     {
00100         $post_vars =& $GLOBALS["HTTP_POST_VARS"];
00101         return isset( $post_vars[$var] );
00102     }
00103 
setGetVariable( $var, $value )
00109     {
00110         $get_vars =& $GLOBALS["_GET"];
00111         $get_vars[$var] =& $value;
00112     }
00113 
getVariable( $var )
00119     {
00120         $get_vars =& $GLOBALS["_GET"];
00121         $ret = null;
00122         if ( isset( $get_vars[$var] ) )
00123             $ret =& $get_vars[$var];
00124         else
00125             eZDebug::writeWarning( "Undefined get variable: $var",
00126                                    "eZHTTPTool" );
00127         return $ret;
00128     }
00129 
hasGetVariable( $var )
00135     {
00136         $get_vars =& $GLOBALS["_GET"];
00137         return isset( $get_vars[$var] );
00138     }
00139 
hasVariable( $var )
00145     {
00146 
00147         if ( isset( $GLOBALS["HTTP_POST_VARS"][$var] ) )
00148             return isset( $GLOBALS["HTTP_POST_VARS"][$var] );
00149         else
00150         {
00151             return isset( $GLOBALS["_GET"][$var] );
00152         }
00153     }
00154 
variable( $var )
00160     {
00161         if ( isset( $GLOBALS["HTTP_POST_VARS"][$var] ) )
00162             return $GLOBALS["HTTP_POST_VARS"][$var];
00163         else
00164         {
00165             if ( isset( $GLOBALS["_GET"][$var] ) )
00166                 return $GLOBALS["_GET"][$var];
00167             else
00168                 return false;
00169         }
00170     }
00171 
attributes()
00176     {
00177         return array( "post", "get", "session" );
00178     }
00179 
hasAttribute( $attr )
00184     {
00185         return $attr == "post" or $attr == "get" or $attr == "session";
00186     }
00187 
attribute( $attr )
00192     {
00193         if ( $attr == "post" )
00194             return $GLOBALS["HTTP_POST_VARS"];
00195         if ( $attr == "get" )
00196             return $GLOBALS["_GET"];
00197         if ( $attr == "session" )
00198         {
00199             eZSessionStart();
00200             return $GLOBALS["HTTP_SESSION_VARS"];
00201         }
00202         return null;
00203     }
00204 
instance()
00209     {
00210         $instance =& $GLOBALS["eZHTTPToolInstance"];
00211         if ( get_class( $instance ) != "ezhttptool" )
00212         {
00213             $eZHTTPTool();
00214             $instance->createPostVarsFromImageButtons();
00215         }
00216         return $instance;
00217     }
00218 
createRedirectUrl( $path, $parameters = array() )
00243     {
00244         $parameters = array_merge( array( 'host' => false,
00245                                           'protocol' => false,
00246                                           'port' => false,
00247                                           'username' => false,
00248                                           'password' => false,
00249                                           'pre_url' => true ),
00250                                    $parameters );
00251         $host = $parameters['host'];
00252         $protocol = $parameters['protocol'];
00253         $port = $parameters['port'];
00254         $username = $parameters['username'];
00255         $password = $parameters['password'];
00256         if ( preg_match( '#^([a-zA-Z0-9]+):(.+)$#', $path, $matches ) )
00257         {
00258             if ( $matches[1] )
00259                 $protocol = $matches[1];
00260             $path = $matches[2];
00261         }
00262         if ( preg_match( '#^//([^./:]+(\.[^./:]+)+)(:([0-9]+))?(.*)$#', $path, $matches ) )
00263         {
00264             if ( $matches[1] )
00265                 $host = $matches[1];
00266             $path = $matches[5];
00267             if ( $matches[4] )
00268                 $port = $matches[4];
00269         }
00270         if ( $parameters['pre_url'] )
00271         {
00272             if ( strlen( $path ) > 0 and
00273                  $path[0] != '/' )
00274             {
00275                 $preURL = eZSys::serverVariable( 'SCRIPT_URL' );
00276                 if ( strlen( $preURL ) > 0 and
00277                      $preURL[strlen($preURL) - 1] != '/' )
00278                     $preURL .= '/';
00279                 $path = $preURL . $path;
00280             }
00281         }
00282 
00283         if ( !is_string( $host ) )
00284             $host = eZSys::hostname();
00285         if ( !is_string( $protocol ) )
00286         {
00287             $protocol = 'http';
00288             // Default to https if SSL is enabled
00289 
00290             // Check if SSL port is defined in site.ini
00291             $ini =& eZINI::instance();
00292             $sslPort = 443;
00293             if ( $ini->hasVariable( 'SiteSettings', 'SSLPort' ) )
00294             {
00295                 $sslPort = $ini->variable( 'SiteSettings', 'SSLPort' );
00296             }
00297 
00298             if ( eZSys::serverPort() == $sslPort )
00299             {
00300                 $protocol = 'https';
00301             }
00302         }
00303 
00304         $uri = $protocol . '://';
00305         if ( $username )
00306         {
00307             $uri .= $username;
00308             if ( $password )
00309                 $uri .= ':' . $password;
00310             $uri .= '@';
00311         }
00312         $uri .= $host;
00313         if ( $port )
00314             $uri .= ':' . $port;
00315         $uri .= $path;
00316         return $uri;
00317     }
00318 
00319     function redirect( $path, $parameters = array() )
00320     {
00321         $uri = eZHTTPTool::createRedirectUrl( $path, $parameters );
00322         eZHTTPTool::headerVariable( 'Location', $uri );
00323     }
00324 
00330     function headerVariable( $headerName, $headerData )
00331     {
00332         header( "$headerName: $headerData" );
00333     }
00334 
00335     function removeMagicQuotes()
00336     {
00337         foreach ( array_keys( $GLOBALS["HTTP_POST_VARS"] ) as $key )
00338         {
00339             if ( !is_array( $GLOBALS["HTTP_POST_VARS"][$key] ) )
00340             {
00341                 $GLOBALS["HTTP_POST_VARS"][$key] = str_replace( "\'", "'", $GLOBALS["HTTP_POST_VARS"][$key] );
00342                 $GLOBALS["HTTP_POST_VARS"][$key] = str_replace( '\"', '"', $GLOBALS["HTTP_POST_VARS"][$key] );
00343             }
00344         }
00345         foreach ( array_keys( $GLOBALS["_GET"] ) as $key )
00346         {
00347             if ( !is_array( $GLOBALS["_GET"][$key] ) )
00348             {
00349                 $GLOBALS["_GET"][$key] = str_replace( "\'", "'", $GLOBALS["_GET"][$key] );
00350                 $GLOBALS["_GET"][$key] = str_replace( '\"', '"', $GLOBALS["_GET"][$key] );
00351             }
00352          }
00353     }
00354 
00355     function createPostVarsFromImageButtons()
00356     {
00357         foreach ( array_keys( $GLOBALS["HTTP_POST_VARS"] ) as $key )
00358         {
00359             if ( substr( $key, -2 ) == '_x' )
00360             {
00361                 $yKey = substr( $key, 0, -2 ) . '_y';
00362                 if ( array_key_exists( $yKey, $GLOBALS["HTTP_POST_VARS"] ) )
00363                 {
00364                     $keyClean = substr( $key, 0, -2 );
00365                     $matches = array();
00366                     if ( preg_match( "/_(\d+)$/", $keyClean, $matches ) )
00367                     {
00368                         $value =  $matches[1];
00369                         $keyClean = preg_replace( "/(_\d+)$/","", $keyClean );
00370                         $GLOBALS["HTTP_POST_VARS"][$keyClean] = $value;
00371                         eZDebug::writeDebug( $GLOBALS["HTTP_POST_VARS"][$keyClean], "We have create new  Post Var with name $keyClean and value $value:" );
00372                     }
00373                     else
00374                     {
00375                         $GLOBALS["HTTP_POST_VARS"][$keyClean] = true;
00376                         eZDebug::writeDebug( $GLOBALS["HTTP_POST_VARS"][$keyClean], "We have create new  Post Var with name $keyClean and value true:" );
00377                     }
00378                 }
00379             }
00380         }
00381     }
00382 
00386     function getSessionKey()
00387     {
00388         eZSessionStart();
00389         return session_id();
00390     }
00391 
00392     function setSessionKey( $sessionKey )
00393     {
00394         eZSessionStart();
00395         return session_id( $sessionKey );
00396     }
00397 
00398     function setSessionVariable( $name, $value )
00399     {
00400         eZSessionStart();
00401         global $HTTP_SESSION_VARS;
00402         session_register( $name );
00403         $HTTP_SESSION_VARS[$name] =& $value;
00404     }
00405 
00409     function removeSessionVariable( $name )
00410     {
00411         eZSessionStart();
00412         session_unregister( $name );
00413     }
00414 
00418     function hasSessionVariable( $name )
00419     {
00420         eZSessionStart();
00421         global $HTTP_SESSION_VARS;
00422         return isset( $HTTP_SESSION_VARS[$name] );
00423     }
00424 
00428     function &sessionVariable( $name )
00429     {
00430         eZSessionStart();
00431         global $HTTP_SESSION_VARS;
00432         return $HTTP_SESSION_VARS[$name];
00433     }
00434 
00438     function sessionID()
00439     {
00440         eZSessionStart();
00441         return session_id();
00442     }
00443 }
00444 
00445 ?>
 

Exponential