00001 <?php
00002
00003
00004
00005 if (oxConfig::getInstance()->getConfigParam( 'blAdodbSessionHandler' ) )
00006 require_once getShopBasePath() . 'core/adodb/session/adodb-session.php';
00007
00014 class oxSession extends oxSuperCfg
00015 {
00021 protected $_sName = 'sid';
00022
00027 protected $_sId = null;
00028
00034 protected static $_blIsNewSession = false;
00035
00039 protected static $_instance = null;
00040
00045 protected static $_oUser = null;
00046
00053 protected $_blNewSession = false;
00054
00060 protected $_sErrorMsg = null;
00061
00067 protected $_oBasket = null;
00068
00074 protected $_aRequireCookiesInFncs = array( 'register' => null,
00075 'account' => null,
00076 'tobasket',
00077 'login_noredirect'
00078 );
00079
00083 protected $_aPersistentParams = array("actshop", "lang", "currency", "language", "tpllanguage");
00084
00090 public static function getInstance()
00091 {
00092 if ( defined('OXID_PHP_UNIT')) {
00093 if ( isset( modSession::$unitMOD) && is_object( modSession::$unitMOD)) {
00094 return modSession::$unitMOD;
00095 }
00096 }
00097 if (!isset(self::$_instance)) {
00098 self::$_instance = oxNew( 'oxsession' );
00099 }
00100 return self::$_instance;
00101 }
00102
00108 public function getId()
00109 {
00110 return $this->_sId;
00111 }
00112
00120 public function setId($sVal)
00121 {
00122 $this->_sId = $sVal;
00123 }
00124
00132 public function setName($sVal)
00133 {
00134 $this->_sName = $sVal;
00135 }
00136
00142 public function getName()
00143 {
00144 return $this->_sName;
00145 }
00146
00154 public function start()
00155 {
00156 $sid = null;
00157
00158 if ( $this->isAdmin() ) {
00159 $this->setName("admin_sid");
00160 } else {
00161 $this->setName("sid");
00162 }
00163
00164 $sForceSidParam = oxConfig::getParameter('force_sid');
00165 $sSidParam = oxConfig::getParameter($this->getName());
00166
00167 $blUseCookies = $this->getConfig()->getConfigParam( 'blSessionUseCookies') || $this->isAdmin();
00168
00169
00170 if ($sForceSidParam) {
00171 $sid = $sForceSidParam;
00172 } elseif ($blUseCookies && $this->_getCookieSid()) {
00173 $sid = $this->_getCookieSid();
00174 } elseif($sSidParam) {
00175 $sid = $sSidParam;
00176 }
00177
00178
00179
00180 if ( !$sid) {
00181 $this->initNewSession();
00182 self::$_blIsNewSession = true;
00183 } else {
00184 $this->_setSessionId($sid);
00185 }
00186
00187
00188
00189 if ($this->_allowSessionStart()) {
00190
00191 @session_start();
00192
00193
00194 if ($this->_sId != session_id()) {
00195 $this->_setSessionId(session_id());
00196 }
00197 }
00198
00199
00200 if (!$this->_getCookieSid() && !oxUtils::getInstance()->isSearchEngine() && $this->_isSwappedClient() ) {
00201 $this->initNewSession();
00202 }
00203
00204 $sClass = oxConfig::getParameter( 'cl' );
00205 $sFunction = oxConfig::getParameter( 'fnc' );
00206
00207 if ( !$this->_checkMandatoryCookieSupport( $sClass, $sFunction ) ) {
00208 $oEx = oxNew( 'oxCookieException' );
00209 $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
00210 throw $oEx;
00211 }
00212 }
00213
00219 public function initNewSession()
00220 {
00221
00222 $aPersistent = array();
00223 foreach ($this->_aPersistentParams as $sParam) {
00224 if ( self::getVar($sParam)) {
00225 $aPersistent[$sParam] = self::getVar($sParam);
00226 }
00227 }
00228
00229 $sid = md5(oxUtilsObject::getInstance()->generateUID());
00230
00231 $this->_setSessionId($sid);
00232 session_unset();
00233
00234
00235 foreach ($aPersistent as $key => $sParam) {
00236 self::setVar($key, $aPersistent[$key]);
00237 }
00238 }
00239
00245 public function freeze()
00246 {
00247
00248 self::setVar( $this->_getBasketName(), serialize( $this->getBasket() ) );
00249
00250 session_write_close();
00251 }
00252
00258 public function destroy()
00259 {
00260
00261 unset($_SESSION);
00262 session_destroy();
00263 }
00264
00272 public static function hasVar( $name )
00273 {
00274 if ( defined( 'OXID_PHP_UNIT' ) ) {
00275 if ( isset( modSession::$unitMOD ) && is_object( modSession::$unitMOD ) ) {
00276 try{
00277 $sVal = modSession::getInstance()->getVar( $name );
00278 return isset( $sVal );
00279 } catch( Exception $e ) {
00280
00281 }
00282 }
00283 }
00284
00285 return isset($_SESSION[$name]);
00286 }
00287
00296 public static function setVar( $name, $value)
00297 {
00298 if ( defined( 'OXID_PHP_UNIT' ) ) {
00299 if ( isset( modSession::$unitMOD ) && is_object( modSession::$unitMOD ) ) {
00300 try{
00301 return modSession::getInstance()->setVar( $name, $value );
00302 } catch( Exception $e ) {
00303
00304 }
00305 }
00306 }
00307
00308 $_SESSION[$name] = $value;
00309
00310 }
00311
00319 public static function getVar( $name )
00320 {
00321 if ( defined( 'OXID_PHP_UNIT' ) ) {
00322 if ( isset( modSession::$unitMOD ) && is_object( modSession::$unitMOD ) ) {
00323 try{
00324 return modSession::getInstance()->getVar( $name );
00325 } catch( Exception $e ) {
00326
00327 }
00328 }
00329 }
00330
00331 if ( isset( $_SESSION[$name] )) {
00332 return $_SESSION[$name];
00333 } else {
00334 return null;
00335 }
00336 }
00337
00345 public static function deleteVar( $name )
00346 {
00347 if ( defined( 'OXID_PHP_UNIT' ) ) {
00348 if ( isset( modSession::$unitMOD ) && is_object( modSession::$unitMOD ) ) {
00349 try{
00350 return modSession::getInstance()->setVar( $name, null );
00351 } catch( Exception $e ) {
00352
00353 }
00354 }
00355 }
00356
00357 $_SESSION[$name] = null;
00358
00359 unset($_SESSION[$name]);
00360 }
00361
00369 public function url($url)
00370 {
00371 $myConfig = $this->getConfig();
00372 if (strpos(" ".$url, "https:") === 1 && !$myConfig->isSsl()) {
00373 $blForceSID = true;
00374 }
00375 if (strpos(" ".$url, "http:") === 1 && $myConfig->isSsl()) {
00376 $blForceSID = true;
00377 }
00378
00379 $blUseCookies = $myConfig->getConfigParam( 'blSessionUseCookies' ) || $this->isAdmin();
00380
00381 $sSeparator = strstr($url, "?") !== false ? "&" : "?";
00382
00383 if ($blUseCookies && $this->_getCookieSid()) {
00384
00385 $url .= $sSeparator;
00386
00387
00388
00389 if ($blForceSID) {
00390 $url .= 'force_sid=' . $this->getId() . '&';
00391 }
00392 } elseif (oxUtils::getInstance()->isSearchEngine()) {
00393 $url .= $sSeparator;
00394
00395
00396 $sLangParam = oxConfig::getParameter( "lang" );
00397 $sConfLang = $myConfig->getConfigParam( 'sDefaultLang' );
00398 if ( (int) $sLangParam != (int) $sConfLang ) {
00399 $url .= "lang=" . $sLangParam . "&";
00400 }
00401 } elseif ($this->sid()) {
00402
00403
00404
00405
00406
00407 $url .= $sSeparator . $this->sid(). '&';
00408 }
00409
00410 return $url;
00411 }
00412
00420 public function sid()
00421 {
00422 if ( !$this->getId() ) {
00423 return false;
00424 }
00425
00426 $myConfig = $this->getConfig();
00427 $blUseCookies = $myConfig->getConfigParam( 'blSessionUseCookies' ) || $this->isAdmin();
00428
00429
00430 if (!$blUseCookies || !$this->_getCookieSid()) {
00431 $sRet = $this->getName()."=".$this->getId();
00432 }
00433
00434 if (oxUtils::getInstance()->isSearchEngine() && is_array($myConfig->getConfigParam( 'aCacheViews' ) ) && !$this->isAdmin() ) {
00435
00436 $sRet = '';
00437
00438 $sShopId = $myConfig->getShopId();
00439 if ( $sShopId != 1) {
00440 $sRet = "shp=" . $sShopId;
00441 }
00442 }
00443
00444 return $sRet;
00445 }
00446
00452 public function hiddenSid()
00453 {
00454 if ( $this->isAdmin()) {
00455 return '';
00456 }
00457
00458 return "<input type=\"hidden\" name=\"force_sid\" value=\"". $this->getId() . "\">";
00459 }
00460
00466 public function getBasket()
00467 {
00468 if ( $this->_oBasket === null ) {
00469 $sBasket = self::getVar( $this->_getBasketName() );
00470 if ( $sBasket && $oBasket = unserialize( $sBasket ) ) {
00471 $this->setBasket( $oBasket );
00472 } else {
00473 $this->setBasket( oxNew( 'oxbasket' ) );
00474 }
00475 }
00476
00477 return $this->_oBasket;
00478 }
00479
00487 public function setBasket( $oBasket )
00488 {
00489
00490 $this->_oBasket = $oBasket;
00491 }
00492
00498 public function delBasket()
00499 {
00500 $this->setBasket( null );
00501 self::deleteVar( $this->_getBasketName());
00502 }
00503
00509 public function isNewSession()
00510 {
00511 return self::$_blIsNewSession;
00512 }
00513
00519 protected function _allowSessionStart()
00520 {
00521 $blAllowSessionStart = true;
00522 if ( oxUtils::getInstance()->isSearchEngine() ) {
00523 $blAllowSessionStart = false;
00524 }
00525
00526 if ( oxConfig::getParameter( 'skipSession' ) ) {
00527 $blAllowSessionStart = false;
00528 }
00529
00530
00531
00532
00533 return $blAllowSessionStart;
00534 }
00535
00545 protected function _checkMandatoryCookieSupport( $sClass, $sFunction )
00546 {
00547 $myConfig = $this->getConfig();
00548
00549
00550 if (!$myConfig->getConfigParam( 'blSessionEnforceCookies' ) || (oxUtilsServer::getInstance()->getOxCookie($this->getName())) || !$sClass) {
00551 return true;
00552 }
00553
00554 if($sFunction && in_array($sFunction, $this->_aRequireCookiesInFncs)) {
00555 return false;
00556 }
00557
00558 if (array_key_exists($sClass, $this->_aRequireCookiesInFncs)) {
00559 return false;
00560 }
00561
00562
00563 return true;
00564 }
00565
00573 protected function _isSwappedClient()
00574 {
00575 $myConfig = $this->getConfig();
00576 $myUtils = oxUtils::getInstance();
00577
00578 $blSwapped = false;
00579
00580
00581 if ( $myUtils->isSearchEngine() ) {
00582 return false;
00583 }
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595 $sAgent = oxUtilsServer::getInstance()->getServerVar( 'HTTP_USER_AGENT' );
00596 $sExistingAgent = self::getVar( 'sessionagent' );
00597 if ( $this->_checkUserAgent( $sAgent, $sExistingAgent ) ) {
00598 $blSwapped = true;
00599 }
00600
00601
00602
00603
00604
00605
00606 if ( $myConfig->getConfigParam( 'blAdodbSessionHandler' ) ) {
00607 if ( $this->_checkSid() ) {
00608 $blSwapped = true;
00609 }
00610 }
00611
00612 $blDisableCookieCheck = $myConfig->getConfigParam( 'blDisableCookieCheck' );
00613 if ( !$blDisableCookieCheck ) {
00614 $sCookieSid = oxUtilsServer::getInstance()->getOxCookie( 'sid_key' );
00615 $aSessCookieSetOnce = self::getVar("sessioncookieisset");
00616 if ( $this->_checkCookies( $sCookieSid, $aSessCookieSetOnce ) ) {
00617 $blSwapped = true;
00618 }
00619 }
00620
00621 return $blSwapped;
00622 }
00623
00632 protected function _checkUserAgent( $sAgent, $sExistingAgent)
00633 {
00634 $blIgnoreBrowserChange = oxConfig::getParameter("remoteaccess") == "true" && !$this->isAdmin();
00635 if ($sAgent && $sExistingAgent && $sAgent != $sExistingAgent && (!$blIgnoreBrowserChange)) {
00636 $this->_sErrorMsg = "Different browser ($sExistingAgent, $sAgent), creating new SID...<br>";
00637 return true;
00638 } elseif (!isset($sExistingAgent)) {
00639 self::setVar("sessionagent", $sAgent);
00640 }
00641 return false;
00642 }
00643
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00681 protected function _checkSid()
00682 {
00683
00684 $sSID = oxDb::getDb()->GetOne("select SessionID from oxsessions where SessionID = '".$this->getId()."'");
00685
00686
00687
00688 if ( !$this->_blNewSession && (!isset( $sSID) || !$sSID)) {
00689
00690 $this->_sErrorMsg = "Session has expired in the past and someone uses this sid to reactivate it, creating new SID...<br>";
00691 return true;
00692 }
00693 return false;
00694 }
00695
00705 protected function _checkCookies( $sCookieSid, $aSessCookieSetOnce )
00706 {
00707 $myConfig = $this->getConfig();
00708 $blSwapped = false;
00709
00710 if ( isset( $aSessCookieSetOnce[$myConfig->getCurrentShopURL()] ) ) {
00711 $blSessCookieSetOnce = $aSessCookieSetOnce[$myConfig->getCurrentShopURL()];
00712 } else {
00713 $blSessCookieSetOnce = false;
00714 }
00715
00716
00717 if ( $blSessCookieSetOnce && !$sCookieSid ) {
00718 if ( $myConfig->getConfigParam( 'iDebug' ) ) {
00719 $this->_sErrorMsg = "Cookie not found, creating new SID...<br>";
00720 $this->_sErrorMsg .= "Cookie: $sCookieSid<br>";
00721 $this->_sErrorMsg .= "Session: $blSessCookieSetOnce<br>";
00722 $this->_sErrorMsg .= "URL: ".$myConfig->getCurrentShopURL()."<br>";
00723 }
00724 $blSwapped = true;
00725 }
00726
00727
00728 if ( $sCookieSid == "oxid" && !$blSessCookieSetOnce ) {
00729 $aSessCookieSetOnce[$myConfig->getCurrentShopURL()] = "ox_true";
00730 self::setVar( "sessioncookieisset", $aSessCookieSetOnce );
00731 }
00732
00733
00734 if ( !$sCookieSid ) {
00735 oxUtilsServer::getInstance()->setOxCookie( 'sid_key', 'oxid' );
00736 }
00737 return $blSwapped;
00738 }
00739
00747 protected function _setSessionId($sSessId)
00748 {
00749
00750 if ($sSessId && session_id() != $sSessId) {
00751 $this->_blNewSession = true;
00752 }
00753
00754 session_id($sSessId);
00755
00756 $this->setId($sSessId);
00757
00758 if (!$this->_allowSessionStart()) {
00759 oxUtilsServer::getInstance()->setOxCookie($this->getName(), null);
00760 return;
00761 }
00762
00763
00764 oxUtilsServer::getInstance()->setOxCookie($this->getName(), $sSessId);
00765
00766 if ( $this->_sErrorMsg) {
00767
00768 echo $this->_sErrorMsg;
00769 $this->_sErrorMsg = null;
00770 }
00771 }
00772
00778 protected function _getBasketName()
00779 {
00780 $myConfig = $this->getConfig();
00781 if( $myConfig->getConfigParam( 'blMallSharedBasket' ) == 0) {
00782 return $myConfig->getShopId()."_basket";
00783 } else {
00784 return "basket";
00785 }
00786 }
00787
00793 protected function _getCookieSid()
00794 {
00795 return oxUtilsServer::getInstance()->getOxCookie($this->getName());
00796 }
00797
00798 }