oxutilsserver.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxUtilsServer extends oxSuperCfg
00007 {
00013     private static $_instance = null;
00014 
00020     protected $_aUserCookie = array();
00021 
00027     public static function getInstance()
00028     {
00029         // disable caching for test modules
00030         if ( defined( 'OXID_PHP_UNIT' ) ) {
00031             static $inst = array();
00032             self::$_instance = $inst[oxClassCacheKey()];
00033         }
00034 
00035         if ( !self::$_instance instanceof oxUtilsServer ) {
00036             self::$_instance = oxNew( 'oxUtilsServer');
00037             if ( defined( 'OXID_PHP_UNIT' ) ) {
00038                 $inst[oxClassCacheKey()] = self::$_instance;
00039             }
00040         }
00041         return self::$_instance;
00042     }
00043 
00055     public function setOxCookie( $sName, $sValue = "", $iExpire = 0, $sPath = '/', $sDomain = null )
00056     {
00057         //TODO: since setcookie takes more than just 4 params..
00058         // would be nice to have it sending through https only, if in https mode
00059         // or allowing only http access to cookie [no JS access - reduces XSS attack possibility]
00060         // ref: http://lt.php.net/manual/en/function.setcookie.php
00061 
00062         if ( defined('OXID_PHP_UNIT')) {
00063             // do NOT set cookies in php unit.
00064             return;
00065         }
00066 
00067         return setcookie( $sName, $sValue, $iExpire, $this->_getCookiePath( $sPath ), $this->_getCookieDomain( $sDomain ) );
00068     }
00069 
00080     protected function _getCookiePath( $sPath )
00081     {
00082         // possibility for users to define cookie path
00083         // @deprecated use "aCookiePaths" instead
00084         if ( $sCookiePath = $this->getConfig()->getConfigParam( 'sCookiePath' ) ) {
00085             $sPath = $sCookiePath;
00086         } elseif ( $aCookiePaths = $this->getConfig()->getConfigParam( 'aCookiePaths' ) ) {
00087             // in case user wants to have shop specific setup
00088             $sShopId = $this->getConfig()->getShopId();
00089             $sPath = isset( $aCookiePaths[$sShopId] ) ? $aCookiePaths[$sShopId] : $sPath;
00090         }
00091 
00092         // from php doc: .. You may also replace an argument with an empty string ("") in order to skip that argument..
00093         return $sPath ? $sPath : "";
00094     }
00095 
00106     protected function _getCookieDomain( $sDomain )
00107     {
00108         $sDomain = $sDomain ? $sDomain : "";
00109 
00110         // on special cases, like separate domain for SSL, cookies must be defined on domain specific path
00111         // please have a look at
00112         if ( !$sDomain ) {
00113             // @deprecated use "aCookieDomains" instead
00114             if ( $sCookieDomain = $this->getConfig()->getConfigParam( 'sCookieDomain' ) ) {
00115                 $sDomain = $sCookieDomain;
00116             } elseif ( $aCookieDomains = $this->getConfig()->getConfigParam( 'aCookieDomains' ) ) {
00117                 // in case user wants to have shop specific setup
00118                 $sShopId = $this->getConfig()->getShopId();
00119                 $sDomain = isset( $aCookieDomains[$sShopId] ) ? $aCookieDomains[$sShopId] : $sDomain;
00120             }
00121         }
00122         return $sDomain;
00123     }
00124 
00133     public function getOxCookie( $sName = null )
00134     {
00135         $sValue = null;
00136         if ( $sName && isset( $_COOKIE[$sName] ) ) {
00137             $sValue = oxConfig::checkSpecialChars($_COOKIE[$sName]);
00138         } elseif ( $sName && !isset( $_COOKIE[$sName] ) ) {
00139             $sValue = null;
00140         } elseif ( !$sName && isset( $_COOKIE ) ) {
00141             $sValue = $_COOKIE;
00142         }
00143         return $sValue;
00144     }
00145 
00151     public function getRemoteAddress()
00152     {
00153         if ( isset( $_SERVER["HTTP_X_FORWARDED_FOR"] ) ) {
00154             $sIP = $_SERVER["HTTP_X_FORWARDED_FOR"];
00155             $sIP = preg_replace('/,.*$/', '', $sIP);
00156         } elseif ( isset( $_SERVER["HTTP_CLIENT_IP"] ) ) {
00157             $sIP = $_SERVER["HTTP_CLIENT_IP"];
00158         } else {
00159             $sIP = $_SERVER["REMOTE_ADDR"];
00160         }
00161         return $sIP;
00162     }
00163 
00171     public function getServerVar( $sServVar = null )
00172     {
00173         $sValue = null;
00174         if ( isset( $_SERVER ) ) {
00175             if ( $sServVar && isset( $_SERVER[$sServVar] ) ) {
00176                 $sValue = $_SERVER[$sServVar];
00177             } elseif ( !$sServVar ) {
00178                 $sValue = $_SERVER;
00179             }
00180         }
00181         return $sValue;
00182     }
00183 
00194     public function setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
00195     {
00196         $sShopId = ( !$sShopId ) ? $this->getConfig()->getShopId() : $sShopId;
00197         $this->_aUserCookie[$sShopId] = $sUser . '@@@' . crypt( $sPassword, 'ox' );
00198         $this->setOxCookie( 'oxid_' . $sShopId, $this->_aUserCookie[$sShopId], oxUtilsDate::getInstance()->getTime() + $iTimeout, '/' );
00199     }
00200 
00208     public function deleteUserCookie( $sShopId = null )
00209     {
00210         $sShopId = ( !$sShopId ) ? $this->getConfig()->getShopId() : $sShopId;
00211         $this->_aUserCookie[$sShopId] = '';
00212         $this->setOxCookie( 'oxid_'.$sShopId, '', oxUtilsDate::getInstance()->getTime() - 3600, '/' );
00213     }
00214 
00222     public function getUserCookie( $sShopId = null )
00223     {
00224         $sShopId = ( !$sShopId ) ? parent::getConfig()->getShopID() : $sShopId;
00225         if ( $this->_aUserCookie[$sShopId] !== null ) {
00226             if ( !$this->_aUserCookie[$sShopId] ) {
00227                 // cookie has been deleted
00228                 return null;
00229             }
00230             return $this->_aUserCookie[$sShopId];
00231         }
00232 
00233         return $this->_aUserCookie[$sShopId] = $this->getOxCookie( 'oxid_'.$sShopId );
00234     }
00235 }

Generated on Mon Oct 26 20:07:17 2009 for OXID eShop CE by  doxygen 1.5.5