oxsession.php

Go to the documentation of this file.
00001 <?php
00002 
00003 
00004 // Including database session managing class if needed.
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         //forcing sid for SSL<->nonSSL transitions
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         //creating new sid
00180         if ( !$sid) {
00181             $this->initNewSession();
00182             self::$_blIsNewSession = true;
00183         } else {
00184             $this->_setSessionId($sid);
00185         }
00186 
00187 
00188         //starting session if only we can
00189         if ($this->_allowSessionStart()) {
00190 
00191             @session_start();
00192 
00193             //special handling for new ZP cluster session, as in that case session_start() regenerates id
00194             if ($this->_sId != session_id()) {
00195                 $this->_setSessionId(session_id());
00196             }
00197         }
00198 
00199         //checking for swapped client in case cookies are not available
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         //check if we have mandatory cookie support
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         //saving persistent params if old session exists
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         //restoring persistent params to session
00235         foreach ($aPersistent as $key => $sParam) {
00236             self::setVar($key, $aPersistent[$key]);
00237         }
00238     }
00239 
00245     public function freeze()
00246     {
00247         // storing basket ..
00248         self::setVar( $this->_getBasketName(), serialize( $this->getBasket() ) );
00249 
00250         session_write_close();
00251     }
00252 
00258     public function destroy()
00259     {
00260         //session_unset();
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                     // if exception is thrown, use default
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                     // if exception is thrown, use default
00304                 }
00305             }
00306         }
00307 
00308         $_SESSION[$name] = $value;
00309         //logger( "set sessionvar : $name -> $value");
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                     // if exception is thrown, use default
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                     // if exception is thrown, use default
00353                 }
00354             }
00355         }
00356 
00357         $_SESSION[$name] = null;
00358         //logger( "delete sessionvar : $name");
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 ?  "&amp;" : "?";
00382 
00383         if ($blUseCookies && $this->_getCookieSid()) {
00384             //cookies are supported so we do nothing
00385             $url .= $sSeparator;
00386 
00387             //or this is SSL link in non SSL environment (or vice versa)
00388             //and we force sid here
00389             if ($blForceSID) {
00390                 $url .= 'force_sid=' . $this->getId() . '&amp;';
00391             }
00392         } elseif (oxUtils::getInstance()->isSearchEngine()) {
00393             $url .= $sSeparator;
00394 
00395             //adding lang parameter for search engines
00396             $sLangParam = oxConfig::getParameter( "lang" );
00397             $sConfLang = $myConfig->getConfigParam( 'sDefaultLang' );
00398             if ( (int) $sLangParam != (int) $sConfLang ) {
00399                 $url   .= "lang=" . $sLangParam . "&amp;";
00400             }
00401         } elseif ($this->sid()) {
00402             //removing dublicate params
00403             //..hopefully this is not needed
00404             //$url    = ereg_replace("[&?]+$", "", $url);
00405 
00406             //cookies are not supported or this is first time visit
00407             $url   .= $sSeparator . $this->sid(). '&amp;';
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         //no cookie?
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         // sets basket session object
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         /*if ($this->_getCookieSid())
00531             $blAllowSessionStart = true;*/
00532 
00533         return $blAllowSessionStart;
00534     }
00535 
00545     protected function _checkMandatoryCookieSupport( $sClass, $sFunction )
00546     {
00547         $myConfig  = $this->getConfig();
00548 
00549         //no mandatory cookie needed
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         //otherwise cookies are mandatories and we don't have them
00563         return true;
00564     }
00565 
00573     protected function _isSwappedClient()
00574     {
00575         $myConfig = $this->getConfig();
00576         $myUtils  = oxUtils::getInstance();
00577 
00578         $blSwapped = false;
00579 
00580         //checking search engine
00581         if ( $myUtils->isSearchEngine() ) {
00582             return false;
00583         }
00584 
00585         /*
00586         //T2007-05-14
00587         //checking 'skipSession' paramter to prevent new session generation for popup
00588         elseif("x" == $this->getId() && !oxConfig::getParameter('skipSession'))
00589         {
00590             $this->_sErrorMsg = "Refered from search engine, creating new SID...<br>";
00591 
00592             $blSwapped = true;
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         if ( $this->_checkByTimeOut() )
00603             $blSwapped = true;
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     protected function _checkByTimeOut()
00652     {
00653         $myConfig = $this->getConfig();
00654         $iTimeStamp = oxUtilsDate::getInstance()->getTime();
00655 
00656         // #660
00657         $iSessionTimeout = null;
00658         if( $this->isAdmin() )
00659             $iSessionTimeout = $myConfig->getConfigParam( 'iSessionTimeoutAdmin' );
00660         if ( !$this->isAdmin() || !$iSessionTimeout )
00661             $iSessionTimeout = $myConfig->getConfigParam( 'iSessionTimeout' );
00662         if (!$iSessionTimeout)
00663             $iSessionTimeout = 60;
00664 
00665         $iTimeout = 60 * $iSessionTimeout;
00666         $iExistingTimeStamp = self::getVar( "sessiontimestamp");
00667         if ( $iExistingTimeStamp && ( $iExistingTimeStamp + $iTimeout < $iTimeStamp ) ) {
00668             $this->_sErrorMsg = "Shop timeout($iTimeStamp - $iExistingTimeStamp = ".($iTimeStamp - $iExistingTimeStamp)." ),
00669                                                                                                 creating new SID...<br>";
00670             return true;
00671         }
00672         self::setVar("sessiontimestamp", $iTimeStamp);
00673         return false;
00674     }*/
00675 
00681     protected function _checkSid()
00682     {
00683         //matze changed sesskey to SessionID because structure of oxsession changed!!
00684         $sSID = oxDb::getDb()->GetOne("select SessionID from oxsessions where SessionID = '".$this->getId()."'");
00685 
00686         //2007-05-14
00687         //we check _blNewSession as well as this may be actually new session not written to db yet
00688         if ( !$this->_blNewSession && (!isset( $sSID) || !$sSID)) {
00689             // this means, that this session has expired in the past and someone uses this sid to reactivate it
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         //if cookie was there once but now is gone it means we have to reset
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         //if we detect the cookie then set session var for possible later use
00728         if ( $sCookieSid == "oxid" && !$blSessCookieSetOnce ) {
00729             $aSessCookieSetOnce[$myConfig->getCurrentShopURL()] = "ox_true";
00730             self::setVar( "sessioncookieisset", $aSessCookieSetOnce );
00731         }
00732 
00733         //if we have no cookie then try to set it
00734         if ( !$sCookieSid ) {
00735             oxUtilsServer::getInstance()->setOxCookie( 'sid_key', 'oxid' );
00736         }
00737         return $blSwapped;
00738     }
00739 
00747     protected function _setSessionId($sSessId)
00748     {
00749         //marking this session as new one, as it might be not writen to db yet
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         //setting session cookie
00764          oxUtilsServer::getInstance()->setOxCookie($this->getName(), $sSessId);
00765 
00766         if ( $this->_sErrorMsg) {
00767             //display debug error msg
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 }

Generated on Thu Feb 19 15:02:22 2009 for OXID eShop CE by  doxygen 1.5.5