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 
00078     protected function _getCookiePath( $sPath )
00079     {
00080         // from php doc: .. You may also replace an argument with an empty string ("") in order to skip that argument..
00081         return $sPath ? $sPath : "";
00082     }
00083 
00094     protected function _getCookieDomain( $sDomain )
00095     {
00096         $sDomain = $sDomain ? $sDomain : "";
00097 
00098         // on special cases, like separate domain for SSL, cookies must be defined on domain specific path
00099         // please have a look at
00100         if ( !$sDomain ) {
00101             $myConfig = $this->getConfig();
00102             $sCookieDomain = $myConfig->getConfigParam( 'sCookieDomain' );
00103             $sDomain = $sCookieDomain ? $sCookieDomain : "";
00104         }
00105         return $sDomain;
00106     }
00107 
00116     public function getOxCookie( $sName = null )
00117     {
00118         $sValue = null;
00119         if ( $sName && isset( $_COOKIE[$sName] ) ) {
00120             $sValue = oxConfig::checkSpecialChars($_COOKIE[$sName]);
00121         } elseif ( $sName && !isset( $_COOKIE[$sName] ) ) {
00122             $sValue = null;
00123         } elseif ( !$sName && isset( $_COOKIE ) ) {
00124             $sValue = $_COOKIE;
00125         }
00126         return $sValue;
00127     }
00128 
00134     public function getRemoteAddress()
00135     {
00136         if ( isset( $_SERVER["HTTP_X_FORWARDED_FOR"] ) ) {
00137             $sIP = $_SERVER["HTTP_X_FORWARDED_FOR"];
00138         } elseif ( isset( $_SERVER["HTTP_CLIENT_IP"] ) ) {
00139             $sIP = $_SERVER["HTTP_CLIENT_IP"];
00140         } else {
00141             $sIP = $_SERVER["REMOTE_ADDR"];
00142         }
00143         return $sIP;
00144     }
00145 
00153     public function getServerVar( $sServVar = null )
00154     {
00155         $sValue = null;
00156         if ( isset( $_SERVER ) ) {
00157             if ( $sServVar && isset( $_SERVER[$sServVar] ) ) {
00158                 $sValue = $_SERVER[$sServVar];
00159             } elseif ( !$sServVar ) {
00160                 $sValue = $_SERVER;
00161             }
00162         }
00163         return $sValue;
00164     }
00165 
00176     public function setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
00177     {
00178         $sShopId = ( !$sShopId ) ? $this->getConfig()->getShopId() : $sShopId;
00179         $this->_aUserCookie[$sShopId] = $sUser . '@@@' . crypt( $sPassword, 'ox' );
00180         $this->setOxCookie( 'oxid_' . $sShopId, $this->_aUserCookie[$sShopId], oxUtilsDate::getInstance()->getTime() + $iTimeout, '/' );
00181     }
00182 
00190     public function deleteUserCookie( $sShopId = null )
00191     {
00192         $sShopId = ( !$sShopId ) ? $this->getConfig()->getShopId() : $sShopId;
00193         $this->_aUserCookie[$sShopId] = '';
00194         $this->setOxCookie( 'oxid_'.$sShopId, '', oxUtilsDate::getInstance()->getTime() - 3600, '/' );
00195     }
00196 
00204     public function getUserCookie( $sShopId = null )
00205     {
00206         $sShopId = ( !$sShopId ) ? parent::getConfig()->getShopID() : $sShopId;
00207         if ( $this->_aUserCookie[$sShopId] !== null ) {
00208             if ( !$this->_aUserCookie[$sShopId] ) {
00209                 // cookie has been deleted
00210                 return null;
00211             }
00212             return $this->_aUserCookie[$sShopId];
00213         }
00214 
00215         return $this->_aUserCookie[$sShopId] = $this->getOxCookie( 'oxid_'.$sShopId );
00216     }
00217 }

Generated on Tue Aug 18 09:21:06 2009 for OXID eShop CE by  doxygen 1.5.5