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             self::$_instance = modInstances::getMod( __CLASS__ );
00032         }
00033 
00034         if ( !self::$_instance instanceof oxUtilsServer ) {
00035             self::$_instance = oxNew( 'oxUtilsServer');
00036             if ( defined( 'OXID_PHP_UNIT' ) ) {
00037                 modInstances::addMod( __CLASS__, self::$_instance);
00038             }
00039         }
00040         return self::$_instance;
00041     }
00042 
00054     public function setOxCookie( $sName, $sValue = "", $iExpire = 0, $sPath = '/', $sDomain = null )
00055     {
00056         //TODO: since setcookie takes more than just 4 params..
00057         // would be nice to have it sending through https only, if in https mode
00058         // or allowing only http access to cookie [no JS access - reduces XSS attack possibility]
00059         // ref: http://lt.php.net/manual/en/function.setcookie.php
00060 
00061         if ( defined('OXID_PHP_UNIT')) {
00062             // do NOT set cookies in php unit.
00063             return;
00064         }
00065 
00066         return setcookie( $sName, $sValue, $iExpire, $this->_getCookiePath( $sPath ), $this->_getCookieDomain( $sDomain ) );
00067     }
00068 
00079     protected function _getCookiePath( $sPath )
00080     {
00081         // possibility for users to define cookie path
00082         // @deprecated use "aCookiePaths" instead
00083         if ( $sCookiePath = $this->getConfig()->getConfigParam( 'sCookiePath' ) ) {
00084             $sPath = $sCookiePath;
00085         } elseif ( $aCookiePaths = $this->getConfig()->getConfigParam( 'aCookiePaths' ) ) {
00086             // in case user wants to have shop specific setup
00087             $sShopId = $this->getConfig()->getShopId();
00088             $sPath = isset( $aCookiePaths[$sShopId] ) ? $aCookiePaths[$sShopId] : $sPath;
00089         }
00090 
00091         // from php doc: .. You may also replace an argument with an empty string ("") in order to skip that argument..
00092         return $sPath ? $sPath : "";
00093     }
00094 
00105     protected function _getCookieDomain( $sDomain )
00106     {
00107         $sDomain = $sDomain ? $sDomain : "";
00108 
00109         // on special cases, like separate domain for SSL, cookies must be defined on domain specific path
00110         // please have a look at
00111         if ( !$sDomain ) {
00112             // @deprecated use "aCookieDomains" instead
00113             if ( $sCookieDomain = $this->getConfig()->getConfigParam( 'sCookieDomain' ) ) {
00114                 $sDomain = $sCookieDomain;
00115             } elseif ( $aCookieDomains = $this->getConfig()->getConfigParam( 'aCookieDomains' ) ) {
00116                 // in case user wants to have shop specific setup
00117                 $sShopId = $this->getConfig()->getShopId();
00118                 $sDomain = isset( $aCookieDomains[$sShopId] ) ? $aCookieDomains[$sShopId] : $sDomain;
00119             }
00120         }
00121         return $sDomain;
00122     }
00123 
00132     public function getOxCookie( $sName = null )
00133     {
00134         $sValue = null;
00135         if ( $sName && isset( $_COOKIE[$sName] ) ) {
00136             $sValue = oxConfig::checkSpecialChars($_COOKIE[$sName]);
00137         } elseif ( $sName && !isset( $_COOKIE[$sName] ) ) {
00138             $sValue = null;
00139         } elseif ( !$sName && isset( $_COOKIE ) ) {
00140             $sValue = $_COOKIE;
00141         }
00142         return $sValue;
00143     }
00144 
00150     public function getRemoteAddress()
00151     {
00152         if ( isset( $_SERVER["HTTP_X_FORWARDED_FOR"] ) ) {
00153             $sIP = $_SERVER["HTTP_X_FORWARDED_FOR"];
00154             $sIP = preg_replace('/,.*$/', '', $sIP);
00155         } elseif ( isset( $_SERVER["HTTP_CLIENT_IP"] ) ) {
00156             $sIP = $_SERVER["HTTP_CLIENT_IP"];
00157         } else {
00158             $sIP = $_SERVER["REMOTE_ADDR"];
00159         }
00160         return $sIP;
00161     }
00162 
00170     public function getServerVar( $sServVar = null )
00171     {
00172         $sValue = null;
00173         if ( isset( $_SERVER ) ) {
00174             if ( $sServVar && isset( $_SERVER[$sServVar] ) ) {
00175                 $sValue = $_SERVER[$sServVar];
00176             } elseif ( !$sServVar ) {
00177                 $sValue = $_SERVER;
00178             }
00179         }
00180         return $sValue;
00181     }
00182 
00193     public function setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
00194     {
00195         $sShopId = ( !$sShopId ) ? $this->getConfig()->getShopId() : $sShopId;
00196         $this->_aUserCookie[$sShopId] = $sUser . '@@@' . crypt( $sPassword, 'ox' );
00197         $this->setOxCookie( 'oxid_' . $sShopId, $this->_aUserCookie[$sShopId], oxUtilsDate::getInstance()->getTime() + $iTimeout, '/' );
00198     }
00199 
00207     public function deleteUserCookie( $sShopId = null )
00208     {
00209         $sShopId = ( !$sShopId ) ? $this->getConfig()->getShopId() : $sShopId;
00210         $this->_aUserCookie[$sShopId] = '';
00211         $this->setOxCookie( 'oxid_'.$sShopId, '', oxUtilsDate::getInstance()->getTime() - 3600, '/' );
00212     }
00213 
00221     public function getUserCookie( $sShopId = null )
00222     {
00223         $sShopId = ( !$sShopId ) ? parent::getConfig()->getShopID() : $sShopId;
00224         if ( $this->_aUserCookie[$sShopId] !== null ) {
00225             if ( !$this->_aUserCookie[$sShopId] ) {
00226                 // cookie has been deleted
00227                 return null;
00228             }
00229             return $this->_aUserCookie[$sShopId];
00230         }
00231 
00232         return $this->_aUserCookie[$sShopId] = $this->getOxCookie( 'oxid_'.$sShopId );
00233     }
00234 
00241     public function isTrustedClientIp()
00242     {
00243         $blTrusted = false;
00244         $aTrustedIPs = ( array ) $this->getConfig()->getConfigParam( "aTrustedIPs" );
00245         if ( count( $aTrustedIPs ) ) {
00246             $blTrusted = in_array( $this->getRemoteAddress(), $aTrustedIPs );
00247         }
00248 
00249         return $blTrusted;
00250     }
00251 }

Generated by  doxygen 1.6.2