eZ Publish  [4.0]
ezsys.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZSys class
00004 //
00005 // Created on: <01-Mar-2002 13:48:53 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 // Portions are modifications on patches by Andreas B�ckler and Francis Nart
00031 //
00032 
00033 /*!
00034   \class eZSys ezsys.php
00035   \ingroup eZUtils
00036   \brief Easy access to various system settings
00037 
00038   The system is checked to see whether a virtualhost-less setup is used
00039   and sets the appropriate variables which can be fetched with
00040   siteDir(), wwwDir() and indexFile().
00041   It also detects file and enviroment separators, fetch them with
00042   fileSeparator() and envSeparator().
00043 
00044   Example:
00045 \code
00046 // Run the init in the index file
00047 eZSys::init( eZINI::instance() );
00048 print( eZSys::indexFile() );
00049 print( eZSys::wwwDir() );
00050 \endcode
00051 */
00052 
00053 class eZSys
00054 {
00055     const DEBUG_INTERNALS = false;
00056 
00057     /*!
00058      Initializes the object with settings taken from the current script run.
00059     */
00060     function eZSys()
00061     {
00062         $this->Attributes = array( "magickQuotes" => true,
00063                                    "hostname" => true );
00064         // Determine OS specific settings
00065         $uname = php_uname();
00066         if ( substr( $uname, 0, 7 ) == "Windows" )
00067         {
00068             $this->OSType = "win32";
00069             $this->OS = "windows";
00070             $this->FileSystemType = "win32";
00071             $this->FileSeparator = "\\";
00072             $this->LineSeparator= "\r\n";
00073             $this->EnvSeparator = ";";
00074             $this->ShellEscapeCharacter = '"';
00075             $this->BackupFilename = '.bak';
00076         }
00077         else if ( substr( $uname, 0, 3 ) == "Mac" )
00078         {
00079             $this->OSType = "mac";
00080             $this->OS = "mac";
00081             $this->FileSystemType = "unix";
00082             $this->FileSeparator = "/";
00083             $this->LineSeparator= "\r";
00084             $this->EnvSeparator = ":";
00085             $this->ShellEscapeCharacter = "'";
00086             $this->BackupFilename = '~';
00087         }
00088         else
00089         {
00090             $this->OSType = "unix";
00091             if ( strtolower( substr( $uname, 0, 5 ) ) == 'linux' )
00092             {
00093                 $this->OS = 'linux';
00094             }
00095             else if ( strtolower( substr( $uname, 0, 0 ) ) == 'freebsd' )
00096             {
00097                 $this->OS = 'freebsd';
00098             }
00099             else
00100             {
00101                 $this->OS = false;
00102             }
00103             $this->FileSystemType = "unix";
00104             $this->FileSeparator = "/";
00105             $this->LineSeparator= "\n";
00106             $this->EnvSeparator = ":";
00107             $this->ShellEscapeCharacter = "'";
00108             $this->BackupFilename = '~';
00109         }
00110 
00111         $magicQuote = get_magic_quotes_gpc();
00112 
00113         if ( $magicQuote == 1 )
00114         {
00115             eZSys::removeMagicQuotes();
00116         }
00117 
00118         $this->AccessPath = array();
00119     }
00120 
00121     function removeMagicQuotes()
00122     {
00123         $globalVariables = array( '_SERVER', '_ENV' );
00124         foreach ( $globalVariables as $globalVariable )
00125         {
00126             foreach ( array_keys( $GLOBALS[$globalVariable] ) as $key )
00127             {
00128                 if ( !is_array( $GLOBALS[$globalVariable][$key] ) )
00129                 {
00130                     $GLOBALS[$globalVariable][$key] = stripslashes( $GLOBALS[$globalVariable][$key] );
00131                 }
00132             }
00133         }
00134     }
00135 
00136     /*!
00137      \static
00138      \return the os type, either \c "win32", \c "unix" or \c "mac"
00139     */
00140     static function osType()
00141     {
00142         return eZSys::instance()->OSType;
00143     }
00144 
00145     /*!
00146      \static
00147      \return the name of the specific os or \c false if it could not be determined.
00148      Currently detects:
00149      - windows (win32)
00150      - mac (mac)
00151      - linux (unix)
00152      - freebsd (unix)
00153     */
00154     static function osName()
00155     {
00156         return eZSys::instance()->OS;
00157     }
00158 
00159     /*!
00160      \static
00161      \return the filesystem type, either \c "win32" or \c "unix"
00162     */
00163     static function filesystemType()
00164     {
00165         return eZSys::instance()->FileSystemType;
00166     }
00167 
00168     /*!
00169      Returns the string which is used for file separators on the current OS (server).
00170      \static
00171     */
00172     static function fileSeparator()
00173     {
00174         return eZSys::instance()->FileSeparator;
00175     }
00176 
00177     /*!
00178      \static
00179      \return the PHP version as text.
00180      \note Calls phpversion().
00181     */
00182     static function phpVersionText()
00183     {
00184         return phpversion();
00185     }
00186 
00187     /*!
00188      \static
00189      \return the PHP version as an array with the version elements.
00190      \example
00191      array( 4, 3, 4 )
00192      \endexample
00193     */
00194     static function phpVersion()
00195     {
00196         $text = eZSys::phpVersionText();
00197         $elements = explode( '.', $text );
00198         return $elements;
00199     }
00200 
00201     /*!
00202      \return \c true if the PHP version is equal or higher than \a $requiredVersion.
00203      \param $requiredVersion must be an array with version number.
00204 
00205      \code
00206      eZSys::isPHPVersionSufficient( array( 4, 1, 0 ) );
00207      \endcode
00208     */
00209     static function isPHPVersionSufficient( $requiredVersion )
00210     {
00211         if ( !is_array( $requiredVersion ) )
00212             return false;
00213         $phpVersion = eZSys::phpVersion();
00214         $len = min( count( $phpVersion ), count( $requiredVersion ) );
00215          for ( $i = 0; $i < $len; ++$i )
00216         {
00217             if ( $phpVersion[$i] > $requiredVersion[$i] )
00218                 return true;
00219             if ( $phpVersion[$i] < $requiredVersion[$i] )
00220                 return false;
00221         }
00222         return true;
00223     }
00224 
00225     /*!
00226      \static
00227      Determins if the script got executed over the web or the shell/commandoline.
00228     */
00229     static function isShellExecution()
00230     {
00231         $sapiType = php_sapi_name();
00232 
00233         if ( $sapiType == 'cli' )
00234             return true;
00235 
00236         // For CGI we have to check, if the script has been executed over shell.
00237         // Currently it looks like the HTTP_HOST variable is the most reasonable to check.
00238         if ( substr( $sapiType, 0, 3 ) == 'cgi' )
00239         {
00240             if ( !eZSys::serverVariable( 'HTTP_HOST', true ) )
00241                 return true;
00242             else
00243                 return false;
00244         }
00245         return false;
00246     }
00247 
00248     /*!
00249      \static
00250      Escape a string to be used as a shell argument and return it.
00251     */
00252     static function escapeShellArgument( $argument )
00253     {
00254         $escapeChar = eZSys::instance()->ShellEscapeCharacter;
00255         $argument = str_replace( "\\", "\\\\", $argument );
00256         if ( $escapeChar == "'" )
00257         {
00258             $argument = str_replace( $escapeChar, $escapeChar . "\\" . $escapeChar . $escapeChar, $argument );
00259         }
00260         else
00261         {
00262             $argument = str_replace( $escapeChar, "\\" . $escapeChar, $argument );
00263         }
00264         $argument = $escapeChar . $argument . $escapeChar;
00265         return $argument;
00266     }
00267 
00268     /*!
00269      \static
00270      Replaces % elements in the argument text \a $argumentText using the replace list \a $replaceList.
00271      It will also properly escape the argument.
00272      \sa splitArgumentIntoElements, mergeArgumentElements
00273     */
00274     static function createShellArgument( $argumentText, $replaceList )
00275     {
00276         $instance = eZSys::instance();
00277         $elements = $instance->splitArgumentIntoElements( $argumentText );
00278         $replacedElements = array();
00279         foreach ( $elements as $element )
00280         {
00281             if ( is_string( $element ) )
00282             {
00283                 $replacedElements[] = strtr( $element, $replaceList );
00284                 continue;
00285             }
00286             $replacedElements[] = $element;
00287         }
00288         $text = $instance->mergeArgumentElements( $replacedElements );
00289         return $text;
00290     }
00291 
00292     /*!
00293      \static
00294      Splits the argument text into argument array elements.
00295      It will split text on spaces and set them as strings in the array,
00296      spaces will be counted and inserted as integers with the space count.
00297      Text placed in quotes will also be parsed, this allows for spaces in the text.
00298      \code
00299      $list = splitArgumentIntoElements( "-geometry 100x100" );
00300 
00301      var_dump( $list ); // will give: array( "-geometry", 1, "100x100" );
00302      \endcode
00303 
00304      You can then easily modify the elements separately and create the argument text with mergeArgumentElements().
00305     */
00306     static function splitArgumentIntoElements( $argumentText )
00307     {
00308         $argumentElements = array();
00309         $pos = 0;
00310 
00311         while ( $pos < strlen( $argumentText ) )
00312         {
00313             if ( $argumentText[$pos] == '"' || $argumentText[$pos] == "'" )
00314             {
00315                 $quoteStartPos = $pos + 1;
00316                 $quoteEndPos = $pos + 1;
00317                 while ( $quoteEndPos < strlen( $argumentText ) )
00318                 {
00319                     $tmpPos = strpos( $argumentText, $argumentText[$pos], $quoteEndPos );
00320                     if ( $tmpPos !== false and
00321                          $argumentText[$tmpPos - 1] != "\\" );
00322                     {
00323                         $quoteEndPos = $tmpPos;
00324                         break;
00325                     }
00326                     if ( $tmpPos === false )
00327                     {
00328                         $quoteEndPos = strlen( $argumentText );
00329                         break;
00330                     }
00331                     $quoteEndPos = $tmpPos + 1;
00332                 }
00333                 $argumentElements[] = substr( $argumentText, $quoteStartPos, $quoteEndPos - $quoteStartPos );
00334                 $pos = $quoteEndPos + 1;
00335             }
00336             else if ( $argumentText[$pos] == ' ' )
00337             {
00338                 $spacePos = $pos;
00339                 $spaceEndPos = $pos;
00340                 while ( $spaceEndPos < strlen( $argumentText ) )
00341                 {
00342                     if ( $argumentText[$spaceEndPos] != ' ' )
00343                         break;
00344                     ++$spaceEndPos;
00345                 }
00346                 $spaceText = substr( $argumentText, $spacePos, $spaceEndPos - $spacePos );
00347                 $spaceCount = strlen( $spaceText );
00348                 if ( $spaceCount > 0 )
00349                     $argumentElements[] = $spaceCount;
00350                 $pos = $spaceEndPos;
00351             }
00352             else
00353             {
00354                 $spacePos = strpos( $argumentText, ' ', $pos );
00355                 if ( $spacePos !== false )
00356                 {
00357                     $argumentElements[] = substr( $argumentText, $pos, $spacePos - $pos );
00358                     $spaceEndPos = $spacePos + 1;
00359                     while ( $spaceEndPos < strlen( $argumentText ) )
00360                     {
00361                         if ( $argumentText[$spaceEndPos] != ' ' )
00362                             break;
00363                         ++$spaceEndPos;
00364                     }
00365                     $spaceText = substr( $argumentText, $spacePos, $spaceEndPos - $spacePos );
00366                     $spaceCount = strlen( $spaceText );
00367                     if ( $spaceCount > 0 )
00368                         $argumentElements[] = $spaceCount;
00369                     $pos = $spaceEndPos;
00370                 }
00371                 else
00372                 {
00373                     $argumentElements[] = substr( $argumentText, $pos );
00374                     $pos = strlen( $argumentText );
00375                 }
00376             }
00377         }
00378         return $argumentElements;
00379     }
00380 
00381     /*!
00382      \static
00383      Merges an argument list created by splitArgumentIntoElements() back into a text string.
00384      The argument text will be properly quoted.
00385     */
00386     static function mergeArgumentElements( $argumentElements )
00387     {
00388         $instance = eZSys::instance();
00389         $argumentText = '';
00390         foreach ( $argumentElements as $element )
00391         {
00392             if ( is_int( $element ) )
00393             {
00394                 $argumentText .= str_repeat( ' ', $element );
00395             }
00396             else if ( is_string( $element ) )
00397             {
00398                 $argumentText .= $instance->escapeShellArgument( $element );
00399             }
00400         }
00401         return $argumentText;
00402     }
00403 
00404     /*!
00405      \static
00406      \return the backup filename for this platform, returns .bak for win32 and ~ for unix and mac.
00407     */
00408     static function backupFilename()
00409     {
00410         return eZSys::instance()->BackupFilename;
00411     }
00412 
00413     /*!
00414      Returns the string which is used for line separators on the current OS (server).
00415      \static
00416     */
00417     static function lineSeparator()
00418     {
00419         return eZSys::instance()->LineSeparator;
00420     }
00421 
00422     /*!
00423      Returns the string which is used for enviroment separators on the current OS (server).
00424      \static
00425     */
00426     static function envSeparator()
00427     {
00428         return eZSys::instance()->EnvSeparator;
00429     }
00430 
00431     /*!
00432      \static
00433      \return the directory used for storing various kinds of files like cache, temporary files and logs.
00434     */
00435     static function varDirectory()
00436     {
00437         //include_once( 'lib/ezutils/classes/ezini.php' );
00438         $ini = eZINI::instance();
00439         //include_once( 'lib/ezfile/classes/ezdir.php' );
00440         return eZDir::path( array( $ini->variable( 'FileSettings', 'VarDir' ) ) );
00441     }
00442 
00443     /*!
00444      \static
00445      \ return the directory used for storing various kinds of files like images, audio and more.
00446      \Note This will include the varDirectory().
00447     */
00448     static function storageDirectory()
00449     {
00450         //include_once( 'lib/ezutils/classes/ezini.php' );
00451         //include_once( 'lib/ezfile/classes/ezdir.php' );
00452         $ini = eZINI::instance();
00453         $varDir = eZSys::varDirectory();
00454         $storageDir = $ini->variable( 'FileSettings', 'StorageDir' );
00455         return eZDir::path( array( $varDir, $storageDir ) );
00456     }
00457 
00458     /*!
00459      \static
00460      \return the directory used for storing cache files.
00461      \note This will include the varDirectory().
00462     */
00463     static function cacheDirectory()
00464     {
00465         //include_once( 'lib/ezutils/classes/ezini.php' );
00466         $ini = eZINI::instance();
00467         $cacheDir = $ini->variable( 'FileSettings', 'CacheDir' );
00468 
00469         //include_once( 'lib/ezfile/classes/ezdir.php' );
00470         if ( $cacheDir[0] == "/" )
00471         {
00472             return eZDir::path( array( $cacheDir ) );
00473         }
00474         else
00475         {
00476             return eZDir::path( array( eZSys::varDirectory(), $cacheDir ) );
00477         }
00478     }
00479 
00480     /*!
00481      The absolute path to the root directory.
00482      \static
00483     */
00484     static function rootDir()
00485     {
00486         $instance = eZSys::instance();
00487         if ( !$instance->RootDir )
00488         {
00489             $cwd  = getcwd();
00490             $self  = $instance->serverVariable( 'PHP_SELF' );
00491             if ( file_exists( $cwd . $instance->FileSeparator . $self ) or
00492                  file_exists( $cwd . $instance->FileSeparator . $instance->IndexFile ) )
00493             {
00494                 $instance->RootDir = $cwd;
00495             }
00496             else
00497             {
00498                 $instance->RootDir = null;
00499             }
00500         }
00501         return $instance->RootDir;
00502     }
00503 
00504     /*!
00505      The path to where all the code resides.
00506      \static
00507     */
00508     static function siteDir()
00509     {
00510         return eZSys::instance()->SiteDir;
00511     }
00512 
00513     /*!
00514      The relative directory path of the vhless setup.
00515      \static
00516     */
00517     static function wwwDir()
00518     {
00519         return eZSys::instance()->WWWDir;
00520     }
00521 
00522     /*!
00523      The filepath for the index file.
00524      \static
00525     */
00526     static function indexDir( $withAccessList = true )
00527     {
00528         $instance = eZSys::instance();
00529         return $instance->wwwDir() . $instance->indexFile( $withAccessList );
00530     }
00531 
00532     /*!
00533      The filepath for the index file with the access path appended.
00534      \static
00535      \sa indexFileName
00536     */
00537     static function indexFile( $withAccessList = true )
00538     {
00539         $instance = eZSys::instance();
00540         $text = $instance->IndexFile;
00541 
00542         $ini = eZINI::instance();
00543         if ( $ini->variable( 'SiteAccessSettings', 'RemoveSiteAccessIfDefaultAccess' ) == 'enabled' )
00544         {
00545             $defaultAccess = $ini->variable( 'SiteSettings', 'DefaultAccess' );
00546             if ( count( $instance->AccessPath ) > 0 and $instance->AccessPath[0] == $defaultAccess )
00547             {
00548                 $accessPathArray = $instance->AccessPath;
00549                 array_shift( $accessPathArray ); //remove first element from accessPath as this is siteaccess name.
00550                 $accessPath = implode( '/', $accessPathArray );
00551                 $text .= '/' . $accessPath;
00552 
00553                 // Make sure we never return just a single '/'.
00554                 if ( $text == "/" )
00555                     $text = "";
00556 
00557                 return $text;
00558             }
00559         }
00560 
00561         if ( $withAccessList and count( $instance->AccessPath ) > 0 )
00562         {
00563             $accessPath = implode( '/', $instance->AccessPath );
00564 
00565             require_once( 'access.php' );
00566             if ( isset( $GLOBALS['eZCurrentAccess'] ) &&
00567                  isset( $GLOBALS['eZCurrentAccess']['type'] ) &&
00568                  $GLOBALS['eZCurrentAccess']['type'] == EZ_ACCESS_TYPE_URI &&
00569                  isset( $GLOBALS['eZCurrentAccess']['access_alias'] ) )
00570             {
00571                 $accessPathArray = $instance->AccessPath;
00572                 $accessPathArray[0] = $GLOBALS['eZCurrentAccess']['access_alias'];
00573                 $accessPath = implode( '/', $accessPathArray );
00574             }
00575             $text .= '/' . $accessPath;
00576         }
00577         return $text;
00578     }
00579 
00580     /*!
00581      The filepath for the index file.
00582      \static
00583     */
00584     static function indexFileName()
00585     {
00586         return eZSys::instance()->IndexFile;
00587     }
00588 
00589     /*!
00590      Returns the current hostname.
00591      \static
00592     */
00593     static function hostname()
00594     {
00595         $forwardedHostsString = eZSys::serverVariable( 'HTTP_X_FORWARDED_HOST', true );
00596         if ( $forwardedHostsString !== null )
00597         {
00598             $forwardedHosts = explode( ',', $forwardedHostsString );
00599             return $forwardedHosts[0];
00600         }
00601 
00602         return eZSys::serverVariable( 'HTTP_HOST' );
00603     }
00604 
00605     /*!
00606      Determines if SSL is enabled and protocol HTTPS is used.
00607      \return true if current access mode is HTTPS.
00608      \static
00609     */
00610     static function isSSLNow()
00611     {
00612         $ini = eZINI::instance();
00613         $sslPort = $ini->variable( 'SiteSettings', 'SSLPort' );
00614         if ( !$sslPort )
00615             $sslPort = eZSSLZone::DEFAULT_SSL_PORT;
00616         // $nowSSl is true if current access mode is HTTPS.
00617         $nowSSL = ( eZSys::serverPort() == $sslPort );
00618 
00619         //Check if this request might be driven through a ssl proxy
00620         if ( isset ( $_SERVER['HTTP_X_FORWARDED_SERVER'] ) and !$nowSSL )
00621         {
00622             $sslProxyServerName = $ini->variable( 'SiteSettings', 'SSLProxyServerName' );
00623             $nowSSL = ( $sslProxyServerName == $_SERVER['HTTP_X_FORWARDED_SERVER'] );
00624         }
00625         return $nowSSL;
00626     }
00627 
00628     /*!
00629      \static
00630     */
00631     static function serverProtocol()
00632     {
00633         if ( eZSys::isSSLNow() )
00634             return 'https';
00635         else
00636             return 'http';
00637     }
00638 
00639     /*!
00640      Returns the server URL. (protocol with hostname and port)
00641      \static
00642     */
00643     static function serverURL()
00644     {
00645         $host = eZSys::hostname();
00646         $url = '';
00647         if ( $host )
00648         {
00649             if ( eZSys::isSSLNow() )
00650             {
00651                 // https case
00652                 $host = preg_replace( '/:\d+$/', '', $host );
00653 
00654                 $ini = eZINI::instance();
00655                 $sslPort = $ini->variable( 'SiteSettings', 'SSLPort' );
00656 
00657                 $sslPortString = ( $sslPort == eZSSLZone::DEFAULT_SSL_PORT ) ? '' : ":$sslPort";
00658                 $url = "https://" . $host  . $sslPortString;
00659             }
00660             else
00661             {
00662                 $url = "http://" . $host;
00663             }
00664         }
00665         return $url;
00666     }
00667     /*!
00668       \static
00669       \return the port of the server.
00670     */
00671     static function serverPort()
00672     {
00673         if ( empty( $GLOBALS['eZSysServerPort'] ) )
00674         {
00675             $hostname = eZSys::hostname();
00676             if ( preg_match( "/.*:([0-9]+)/", $hostname, $regs ) )
00677             {
00678                 $port = $regs[1];
00679             }
00680             else
00681             {
00682                 $port = eZSys::serverVariable( 'SERVER_PORT' );
00683             }
00684             $GLOBALS['eZSysServerPort'] = $port;
00685         }
00686         return $GLOBALS['eZSysServerPort'];
00687     }
00688 
00689     /*!
00690      Returns true if magick quotes is enabled.
00691      \static
00692     */
00693     static function magickQuotes()
00694     {
00695         return null;
00696     }
00697 
00698     /*!
00699      \return the variable named \a $variableName in the global \c $_SERVER variable.
00700              If the variable is not present an error is shown and \c null is returned.
00701     */
00702     static function serverVariable( $variableName, $quiet = false )
00703     {
00704         if ( !isset( $_SERVER[$variableName] ) )
00705         {
00706             if ( !$quiet )
00707             {
00708                 eZDebug::writeError( "Server variable '$variableName' does not exist", 'eZSys::serverVariable' );
00709             }
00710             $retVal = null;
00711             return $retVal;
00712         }
00713         return $_SERVER[$variableName];
00714     }
00715 
00716     /*!
00717      Sets the server variable named \a $variableName to \a $variableValue.
00718      \note Variables are only set for the current page view.
00719     */
00720     static function setServerVariable( $variableName, $variableValue )
00721     {
00722         $_SERVER;
00723         $_SERVER[$variableName] = $variableValue;
00724     }
00725 
00726     /*!
00727      \return the path string for the server.
00728     */
00729     static function path( $quiet = false )
00730     {
00731         return eZSys::serverVariable( 'PATH', $quiet );
00732     }
00733 
00734     /*!
00735      \return the variable named \a $variableName in the global \c $_ENV variable.
00736              If the variable is not present an error is shown and \c null is returned.
00737     */
00738     static function &environmentVariable( $variableName, $quiet = false )
00739     {
00740         $_ENV;
00741         if ( !isset( $_ENV[$variableName] ) )
00742         {
00743             if ( !$quiet )
00744             {
00745                 eZDebug::writeError( "Environment variable '$variableName' does not exist", 'eZSys::environmentVariable' );
00746             }
00747             $retValue = null;
00748             return $retValue;
00749         }
00750         return $_ENV[$variableName];
00751     }
00752 
00753     /*!
00754      Sets the environment variable named \a $variableName to \a $variableValue.
00755      \note Variables are only set for the current page view.
00756     */
00757     static function setEnvironmentVariable( $variableName, $variableValue )
00758     {
00759         $_ENV;
00760         $_ENV[$variableName] = $variableValue;
00761     }
00762 
00763     function attributes()
00764     {
00765         return array_merge( array( 'wwwdir',
00766                                    'sitedir',
00767                                    'indexfile',
00768                                    'indexdir' ),
00769                             array_keys( $this->Attributes ) );
00770 
00771     }
00772 
00773     /*!
00774      Return true if the attribute $attr is set. Available attributes are
00775      wwwdir, sitedir or indexfile
00776     */
00777     function hasAttribute( $attr )
00778     {
00779         return in_array( $attr, $this->attributes() );
00780     }
00781 
00782     /*!
00783      Returns the attribute value for $attr or null if the attribute does not exist.
00784     */
00785     function attribute( $attr )
00786     {
00787         if ( isset( $this->Attributes[$attr] ) )
00788         {
00789             return $this->$attr();
00790         }
00791         else if ( $attr == 'wwwdir' )
00792         {
00793             return $this->wwwDir();
00794         }
00795         else if ( $attr == 'sitedir' )
00796         {
00797             return $this->siteDir();
00798         }
00799         else if ( $attr == 'indexfile' )
00800         {
00801             return $this->indexFile();
00802         }
00803         else if ( $attr == 'indexdir' )
00804         {
00805             return $this->indexDir();
00806         }
00807 
00808         eZDebug::writeError( "Attribute '$attr' does not exist", 'eZSys::attribute' );
00809         return null;
00810     }
00811 
00812     /*!
00813      \static
00814      Sets the access path which is appended to the index file.
00815      \sa indexFile
00816     */
00817     static function addAccessPath( $path )
00818     {
00819         $instance = eZSys::instance();
00820         if ( !is_array( $path ) )
00821             $path = array( $path );
00822         $instance->AccessPath = array_merge( $instance->AccessPath, $path );
00823     }
00824 
00825     /*!
00826      \static
00827      Empties the access path.
00828     */
00829     static function clearAccessPath()
00830     {
00831         eZSys::instance()->AccessPath = array();
00832     }
00833 
00834     /*!
00835      \static
00836      \return true if debugging of internals is enabled, this will display
00837      which server variables are read.
00838       Set the option with setIsDebugEnabled().
00839     */
00840     static function isDebugEnabled()
00841     {
00842         if ( !isset( $GLOBALS['eZSysDebugInternalsEnabled'] ) )
00843              $GLOBALS['eZSysDebugInternalsEnabled'] = eZSys::DEBUG_INTERNALS;
00844         return $GLOBALS['eZSysDebugInternalsEnabled'];
00845     }
00846 
00847     /*!
00848      \static
00849      Sets whether internal debugging is enabled or not.
00850     */
00851     static function setIsDebugEnabled( $debug )
00852     {
00853         $GLOBALS['eZSysDebugInternalsEnabled'] = $debug;
00854     }
00855 
00856     /*!
00857      Initializes some variables according to some global PHP values.
00858      This function should be called once in the index file with the parameters
00859      stated in the parameter list.
00860      \static
00861     */
00862     static function init( $index = "index.php", $force_VirtualHost = false )
00863     {
00864         $isCGI = ( substr( php_sapi_name(), 0, 3 ) == 'cgi' );
00865 
00866         $instance = eZSys::instance();
00867 
00868         if ( eZSys::isDebugEnabled() )
00869         {
00870             eZDebug::writeNotice( eZSys::serverVariable( 'PHP_SELF' ), 'PHP_SELF' );
00871             eZDebug::writeNotice( eZSys::serverVariable( 'SCRIPT_FILENAME' ), 'SCRIPT_FILENAME' );
00872             eZDebug::writeNotice( eZSys::serverVariable( 'DOCUMENT_ROOT' ), 'DOCUMENT_ROOT' );
00873             eZDebug::writeNotice( eZSys::serverVariable( 'REQUEST_URI' ), 'REQUEST_URI' );
00874             eZDebug::writeNotice( eZSys::serverVariable( 'QUERY_STRING' ), 'QUERY_STRING' );
00875             eZDebug::writeNotice( ini_get( 'include_path' ), 'include_path' );
00876         }
00877 
00878         $phpSelf = eZSys::serverVariable( 'PHP_SELF' );
00879 
00880         // Find out, where our files are.
00881         if ( preg_match( "!(.*/)$index$!", eZSys::serverVariable( 'SCRIPT_FILENAME' ), $regs ) )
00882         {
00883             $siteDir = $regs[1];
00884             $index = "/$index";
00885         }
00886         elseif ( preg_match( "!(.*/)$index/?!", $phpSelf, $regs ) )
00887         {
00888             // Some people using CGI have their $_SERVER['SCRIPT_FILENAME'] not right... so we are trying this.
00889             $siteDir = eZSys::serverVariable( 'DOCUMENT_ROOT' ) . $regs[1];
00890             $index = "/$index";
00891         }
00892         else
00893         {
00894             // Fallback... doesn't work with virtual-hosts, but better than nothing
00895             $siteDir = "./";
00896             $index = "/$index";
00897         }
00898         if ( $isCGI and !$force_VirtualHost )
00899         {
00900             $index .= '?';
00901         }
00902 
00903         // Setting the right include_path
00904         $includePath = ini_get( "include_path" );
00905         if ( trim( $includePath ) != "" )
00906         {
00907             $includePath = $includePath . $instance->envSeparator() . $siteDir;
00908         }
00909         else
00910         {
00911             $includePath = $siteDir;
00912         }
00913         ini_set( "include_path", $includePath );
00914 
00915         $scriptName = eZSys::serverVariable( 'SCRIPT_NAME' );
00916         // Get the webdir.
00917 
00918         $wwwDir = "";
00919 
00920         if ( $force_VirtualHost )
00921         {
00922             $wwwDir = "";
00923         }
00924         else
00925         {
00926             if ( preg_match( "!(.*)$index$!", $scriptName, $regs ) )
00927                 $wwwDir = $regs[1];
00928             if ( preg_match( "!(.*)$index$!", $phpSelf, $regs ) )
00929                 $wwwDir = $regs[1];
00930         }
00931 
00932         if ( ! $isCGI || $force_VirtualHost )
00933         {
00934             $requestURI = eZSys::serverVariable( 'REQUEST_URI' );
00935         }
00936         else
00937         {
00938             $requestURI = eZSys::serverVariable( 'QUERY_STRING' );
00939 
00940             /* take out PHPSESSID, if url-encoded */
00941             if ( preg_match( "/(.*)&PHPSESSID=[^&]+(.*)/", $requestURI, $matches ) )
00942             {
00943                 $requestURI = $matches[1].$matches[2];
00944             }
00945         }
00946 
00947         // Fallback... Finding the paths above failed, so $_SERVER['PHP_SELF'] is not set right.
00948         if ( $siteDir == "./" )
00949             $phpSelf = $requestURI;
00950 
00951         if ( ! $isCGI )
00952         {
00953             $index_reg = str_replace( ".", "\\.", $index );
00954             // Trick: Rewrite setup doesn't have index.php in $_SERVER['PHP_SELF'], so we don't want an $index
00955             if ( !preg_match( "!.*$index_reg.*!", $phpSelf ) || $force_VirtualHost )
00956             {
00957                 $index = "";
00958             }
00959             else
00960             {
00961                 if ( eZSys::isDebugEnabled() )
00962                 {
00963                     eZDebug::writeNotice( "$wwwDir$index", '$wwwDir$index' );
00964                 }
00965                 // Get the right $_SERVER['REQUEST_URI'], when using nVH setup.
00966                 if ( preg_match( "!^$wwwDir$index(.*)!", $phpSelf, $req ) )
00967                 {
00968                     if ( !$req[1] )
00969                     {
00970                         if ( $phpSelf != "$wwwDir$index" and preg_match( "!^$wwwDir(.*)!", $requestURI, $req ) )
00971                         {
00972                             $requestURI = $req[1];
00973                             $index = '';
00974                         }
00975                         elseif ( $phpSelf == "$wwwDir$index" and
00976                                ( preg_match( "!^$wwwDir$index(.*)!", $requestURI, $req ) or preg_match( "!^$wwwDir(.*)!", $requestURI, $req ) ) )
00977                         {
00978                             $requestURI = $req[1];
00979                         }
00980                     }
00981                     else
00982                     {
00983                         $requestURI = $req[1];
00984                     }
00985                 }
00986             }
00987         }
00988         if ( $isCGI and $force_VirtualHost )
00989             $index = '';
00990         // Remove url parameters
00991         if ( $isCGI and !$force_VirtualHost )
00992         {
00993             $pattern = "!(\/[^&]+)!";
00994         }
00995         else
00996         {
00997             $pattern = "!([^?]+)!";
00998         }
00999         if ( preg_match( $pattern, $requestURI, $regs ) )
01000         {
01001             $requestURI = $regs[1];
01002         }
01003 
01004         // Remove internal links
01005         if ( preg_match( "!([^#]+)!", $requestURI, $regs ) )
01006         {
01007             $requestURI = $regs[1];
01008         }
01009 
01010         if ( !$isCGI )
01011         {
01012             $currentPath = substr( eZSys::serverVariable( 'SCRIPT_FILENAME' ), 0, -strlen( 'index.php' ) );
01013             if ( strpos( $currentPath, eZSys::serverVariable( 'DOCUMENT_ROOT' )  ) === 0 )
01014             {
01015                 $prependRequest = substr( $currentPath, strlen( eZSys::serverVariable( 'DOCUMENT_ROOT' ) ) );
01016 
01017                 if ( strpos( $requestURI, $prependRequest ) === 0 )
01018                 {
01019                     $requestURI = substr( $requestURI, strlen( $prependRequest ) - 1 );
01020                     $wwwDir = substr( $prependRequest, 0, -1 );
01021                 }
01022             }
01023         }
01024 
01025         $instance->AccessPath = array();
01026         $instance->SiteDir = $siteDir;
01027         $instance->WWWDir = $wwwDir;
01028         $instance->IndexFile = $index;
01029         $instance->RequestURI = $requestURI;
01030 
01031         if ( eZSys::isDebugEnabled() )
01032         {
01033             eZDebug::writeNotice( $instance->SiteDir, 'SiteDir' );
01034             eZDebug::writeNotice( $instance->WWWDir, 'WWWDir' );
01035             eZDebug::writeNotice( $instance->IndexFile, 'IndexFile' );
01036             eZDebug::writeNotice( eZSys::requestURI(), 'eZSys::requestURI()' );
01037         }
01038 
01039     }
01040 
01041     /*!
01042      \return the URI used for parsing modules, views and parameters, may differ from $_SERVER['REQUEST_URI'].
01043     */
01044     static function requestURI()
01045     {
01046         return eZSys::instance()->RequestURI;
01047     }
01048 
01049     /*!
01050      Returns the only legal instance of the eZSys class.
01051      \static
01052     */
01053     static function instance()
01054     {
01055         if ( empty( $GLOBALS['eZSysInstance'] ) )
01056         {
01057             $GLOBALS['eZSysInstance'] = new eZSys();
01058         }
01059         return $GLOBALS['eZSysInstance'];
01060     }
01061 
01062     /*!
01063      A wrapper for php's crc32 function.
01064      \return the crc32 polynomial as unsigned int
01065     */
01066     static function ezcrc32( $string )
01067     {
01068         //include_once( 'lib/ezutils/classes/ezini.php' );
01069         $ini = eZINI::instance();
01070 
01071         if ( $ini->variable( 'SiteSettings', '64bitCompatibilityMode' ) === 'enabled' )
01072             $checksum = sprintf( '%u', crc32( $string ) );
01073         else
01074             $checksum = crc32( $string );
01075 
01076         return $checksum;
01077     }
01078 
01079     /*!
01080      Returns the schema of the request.
01081      */
01082     static function protocolSchema()
01083     {
01084         $schema = '';
01085         if( preg_match( "#^([a-zA-Z]+)/.*$#", eZSys::serverVariable( 'SERVER_PROTOCOL' ), $schemaMatches ) )
01086         {
01087             $schema = strtolower( $schemaMatches[1] ) . '://';
01088         }
01089 
01090         return $schema;
01091     }
01092 
01093     /*!
01094      Wraps around the built-in glob() function to provide same functionality
01095      for systems (e.g Solaris) that does not support GLOB_BRACE.
01096 
01097      \static
01098     */
01099     static function globBrace( $pattern, $flags = 0 )
01100     {
01101         if ( defined( 'GLOB_BRACE' ) )
01102         {
01103             $flags = $flags | GLOB_BRACE;
01104             return glob( $pattern, $flags );
01105         }
01106         else
01107         {
01108             $result = array();
01109             $files = self::simulateGlobBrace( array( $pattern ) );
01110             foreach( $files as $file )
01111             {
01112                 $globList = glob( $file, $flags );
01113                 if ( is_array( $globList ) )
01114                 {
01115                     $result = array_merge( $result, $globList );
01116                 }
01117             }
01118             return $result;
01119         }
01120     }
01121 
01122     /*!
01123      Expands a list of filenames like GLOB_BRACE does.
01124 
01125      GLOB_BRACE is non POSIX and only available in GNU glibc. This is needed to
01126      support operating systems like Solars.
01127 
01128      \static
01129      \protected
01130      */
01131     static protected function simulateGlobBrace( $filenames )
01132     {
01133        $result = array();
01134 
01135        foreach ( $filenames as $filename )
01136        {
01137            if ( strpos( $filename, '{' ) === false )
01138            {
01139                $result[] = $filename;
01140                continue;
01141            }
01142 
01143            if ( preg_match( '/^(.*)\{(.*?)(?<!\\\\)\}(.*)$/', $filename, $match ) )
01144            {
01145                $variants = preg_split( '/(?<!\\\\),/', $match[2] );
01146 
01147                $newFilenames = array();
01148                foreach ( $variants as $variant )
01149                {
01150                    $newFilenames[] = $match[1] . $variant . $match[3];
01151                }
01152 
01153                $newFilenames = self::simulateGlobBrace( $newFilenames );
01154                $result = array_merge( $result, $newFilenames );
01155            }
01156            else
01157            {
01158                $result[] = $filename;
01159            }
01160        }
01161 
01162        return $result;
01163     }
01164 
01165 
01166     /// The line separator used in files
01167     public $LineSeparator;
01168     /// The directory separator used for files
01169     public $FileSeparator;
01170     /// The list separator used for env variables
01171     public $EnvSeparator;
01172     /// The absolute path to the root directory.
01173     public $RootDir;
01174     /// The path to where all the code resides
01175     public $SiteDir;
01176     /// The access path of the current site view
01177     public $AccessPath;
01178     /// The relative directory path of the vhless setup
01179     public $WWWDir;
01180     /// The filepath for the index
01181     public $IndexFile;
01182     /// The uri which is used for parsing module/view information from, may differ from $_SERVER['REQUEST_URI']
01183     public $RequestURI;
01184     /// The type of filesystem, is either win32 or unix. This often used to determine os specific paths.
01185     public $FileSystemType;
01186     /// The character to be used in shell escaping, this character is OS specific
01187     public $ShellEscapeCharacter;
01188     public $OSType;
01189 }
01190 
01191 ?>