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
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
00058
00059
00060
00061
00062 if ( defined('OXID_PHP_UNIT')) {
00063
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
00083
00084 if ( $sCookiePath = $this->getConfig()->getConfigParam( 'sCookiePath' ) ) {
00085 $sPath = $sCookiePath;
00086 } elseif ( $aCookiePaths = $this->getConfig()->getConfigParam( 'aCookiePaths' ) ) {
00087
00088 $sShopId = $this->getConfig()->getShopId();
00089 $sPath = isset( $aCookiePaths[$sShopId] ) ? $aCookiePaths[$sShopId] : $sPath;
00090 }
00091
00092
00093 return $sPath ? $sPath : "";
00094 }
00095
00106 protected function _getCookieDomain( $sDomain )
00107 {
00108 $sDomain = $sDomain ? $sDomain : "";
00109
00110
00111
00112 if ( !$sDomain ) {
00113
00114 if ( $sCookieDomain = $this->getConfig()->getConfigParam( 'sCookieDomain' ) ) {
00115 $sDomain = $sCookieDomain;
00116 } elseif ( $aCookieDomains = $this->getConfig()->getConfigParam( 'aCookieDomains' ) ) {
00117
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
00228 return null;
00229 }
00230 return $this->_aUserCookie[$sShopId];
00231 }
00232
00233 return $this->_aUserCookie[$sShopId] = $this->getOxCookie( 'oxid_'.$sShopId );
00234 }
00235 }