oxcmp_user.php

Go to the documentation of this file.
00001 <?php
00002 
00003 // defining login/logout states
00004 define( 'USER_LOGIN_SUCCESS', 1 );
00005 define( 'USER_LOGIN_FAIL', 2 );
00006 define( 'USER_LOGOUT', 3 );
00007 
00013 class oxcmp_user extends oxView
00014 {
00019     protected $_blIsNewUser    = false;
00020 
00025     protected $_blIsComponent = true;
00026 
00031     protected $_blNewsSubscriptionStatus = null;
00032 
00040     protected $_iLoginStatus = null;
00041 
00047     protected $_sTermsVer = null;
00048 
00054     protected $_aAllowedClasses = array(
00055                                         'register',
00056                                         'forgotpwd',
00057                                         'content',
00058                                         'account',
00059                                         'clearcookies',
00060                                         'oxwServiceMenu',
00061                                         );
00071     public function init()
00072     {
00073         // saving show/hide delivery address state
00074         $blShow = oxConfig::getParameter( 'blshowshipaddress' );
00075         if (!isset($blShow)) {
00076             $blShow = oxSession::getVar( 'blshowshipaddress' );
00077         }
00078 
00079         oxSession::setVar( 'blshowshipaddress', $blShow );
00080 
00081         // load session user
00082         $this->_loadSessionUser();
00083         if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) ) {
00084             // get invitor ID
00085             $this->getInvitor();
00086             $this->setRecipient();
00087         }
00088 
00089         parent::init();
00090     }
00091 
00101     public function render()
00102     {
00103         // checks if private sales allows further tasks
00104         $this->_checkPsState();
00105 
00106         parent::render();
00107 
00108         // dyn_group feature: if you specify a groupid in URL the user
00109         // will automatically be added to this group later
00110         if ( $sDynGoup = oxConfig::getParameter( 'dgr' ) ) {
00111             oxSession::setVar( 'dgr', $sDynGoup );
00112         }
00113 
00114         return $this->getUser();
00115     }
00116 
00127     protected function _checkPsState()
00128     {
00129         $oConfig = $this->getConfig();
00130         if ( $this->getParent()->isEnabledPrivateSales() ) {
00131             // load session user
00132             $oUser  = $this->getUser();
00133             $sClass = $this->getParent()->getClassName();
00134 
00135             // no session user
00136             if ( !$oUser && !in_array( $sClass, $this->_aAllowedClasses ) ) {
00137                 oxRegistry::getUtils()->redirect( $oConfig->getShopHomeURL() . 'cl=account', false, 302 );
00138             }
00139 
00140             if ( $oUser && !$oUser->isTermsAccepted() && !in_array( $sClass, $this->_aAllowedClasses ) ) {
00141                 oxRegistry::getUtils()->redirect( $oConfig->getShopHomeURL() . 'cl=account&term=1', false, 302 );
00142             }
00143         }
00144     }
00145 
00151     protected function _loadSessionUser()
00152     {
00153         $myConfig = $this->getConfig();
00154         $oUser = $this->getUser();
00155 
00156         // no session user
00157         if ( !$oUser ) {
00158             return;
00159         }
00160 
00161         // this user is blocked, deny him
00162         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00163             oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL() . 'cl=content&tpl=user_blocked.tpl', true, 302  );
00164         }
00165 
00166         // TODO: move this to a proper place
00167         if ( $oUser->isLoadedFromCookie() && !$myConfig->getConfigParam( 'blPerfNoBasketSaving' )) {
00168 
00169             if ( $oBasket = $this->getSession()->getBasket() ) {
00170                 $oBasket->load();
00171                 $oBasket->onUpdate();
00172             }
00173         }
00174     }
00175 
00189     public function login()
00190     {
00191         $sUser     = oxConfig::getParameter( 'lgn_usr' );
00192         $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
00193         $sCookie   = oxConfig::getParameter( 'lgn_cook' );
00194         //$blFbLogin = oxConfig::getParameter( 'fblogin' );
00195 
00196         $this->setLoginStatus( USER_LOGIN_FAIL );
00197 
00198         // trying to login user
00199         try {
00200             $oUser = oxNew( 'oxuser' );
00201             $oUser->login( $sUser, $sPassword, $sCookie );
00202             $this->setLoginStatus( USER_LOGIN_SUCCESS );
00203         } catch ( oxUserException $oEx ) {
00204             // for login component send excpetion text to a custom component (if defined)
00205             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true, '', false );
00206             return 'user';
00207         } catch( oxCookieException $oEx ){
00208             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00209             return 'user';
00210         }
00211         // finalizing ..
00212         return $this->_afterLogin( $oUser );
00213     }
00214 
00232     protected function _afterLogin( $oUser )
00233     {
00234         $oSession = $this->getSession();
00235 
00236         // generating new session id after login
00237         if ( $this->getLoginStatus() === USER_LOGIN_SUCCESS ) {
00238             $oSession->regenerateSessionId();
00239         }
00240 
00241         $myConfig = $this->getConfig();
00242 
00243         // this user is blocked, deny him
00244         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00245             oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL().'cl=content&tpl=user_blocked.tpl', true, 302 );
00246         }
00247 
00248         // adding to dyn group
00249         $oUser->addDynGroup(oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ));
00250 
00251         // recalc basket
00252         if ( $oBasket = $oSession->getBasket() ) {
00253             $oBasket->onUpdate();
00254         }
00255 
00256 
00257         return 'payment';
00258     }
00259 
00266     public function login_noredirect()
00267     {
00268         $blAgb = oxConfig::getParameter( 'ord_agb' );
00269         $oConfig = $this->getConfig();
00270         if ( $this->getParent()->isEnabledPrivateSales() && $blAgb !== null && ( $oUser = $this->getUser() ) ) {
00271             if ( $blAgb ) {
00272                 $oUser->acceptTerms();
00273             }
00274         } else {
00275             $this->login();
00276 
00277             if ( !$this->isAdmin() && !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' )) {
00278                 //load basket from the database
00279                 try {
00280                     if ( $oBasket = $this->getSession()->getBasket() ) {
00281                         $oBasket->load();
00282                     }
00283                 } catch ( Exception $oE ) {
00284                     //just ignore it
00285                 }
00286             }
00287 
00288 
00289         }
00290     }
00291 
00298     public function login_updateFbId()
00299     {
00300         $this->login();
00301 
00302         if ( $oUser = $this->getUser() ) {
00303             //updating user Facebook ID
00304             if ( $oUser->updateFbId() ) {
00305                 oxSession::setVar( '_blFbUserIdUpdated', true );
00306             }
00307         }
00308     }
00309 
00318     protected function _afterLogout()
00319     {
00320         oxSession::deleteVar( 'paymentid' );
00321         oxSession::deleteVar( 'sShipSet' );
00322         oxSession::deleteVar( 'deladrid' );
00323         oxSession::deleteVar( 'dynvalue' );
00324 
00325         // resetting & recalc basket
00326         if ( ( $oBasket = $this->getSession()->getBasket() ) ) {
00327             $oBasket->resetUserInfo();
00328             $oBasket->onUpdate();
00329         }
00330     }
00331 
00340     public function logout()
00341     {
00342         $myConfig  = $this->getConfig();
00343         $oUser = oxNew( 'oxuser' );
00344 
00345         if ( $oUser->logout() ) {
00346 
00347             $this->setLoginStatus( USER_LOGOUT );
00348 
00349             // finalizing ..
00350             $this->_afterLogout();
00351 
00352 
00353             if ( $this->getParent()->isEnabledPrivateSales() ) {
00354                 return 'account';
00355             }
00356 
00357             // redirecting if user logs out in SSL mode
00358             if ( oxConfig::getParameter('redirect') && $myConfig->getConfigParam( 'sSSLShopURL' ) ) {
00359                 oxRegistry::getUtils()->redirect( $this->_getLogoutLink());
00360             }
00361         }
00362     }
00363 
00373     public function changeUser( )
00374     {
00375         $blUserRegistered = $this->_changeUser_noRedirect();
00376 
00377         if ( $blUserRegistered === true ) {
00378             return 'payment';
00379         } else {
00380             return $blUserRegistered;
00381         }
00382     }
00383 
00390     public function changeuser_testvalues()
00391     {
00392         // skip updating user info if this is just form reload
00393         // on selecting delivery address
00394         // We do redirect only on success not to loose errors.
00395 
00396         if ( $this->_changeUser_noRedirect() ) {
00397             return 'account_user';
00398         }
00399     }
00400 
00422     public function createUser()
00423     {
00424         $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
00425 
00426         $myConfig = $this->getConfig();
00427         if ( $blActiveLogin && !oxConfig::getParameter( 'ord_agb' ) && $myConfig->getConfigParam( 'blConfirmAGB' ) ) {
00428             oxRegistry::get("oxUtilsView")->addErrorToDisplay( 'ORDER_READANDCONFIRMTERMS', false, true );
00429             return;
00430         }
00431 
00432         $myUtils  = oxRegistry::getUtils();
00433 
00434         // collecting values to check
00435         $sUser = oxConfig::getParameter( 'lgn_usr' );
00436 
00437         // first pass
00438         $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
00439 
00440         // second pass
00441         $sPassword2 = oxConfig::getParameter( 'lgn_pwd2', true );
00442 
00443         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00444         $aDelAdress = $this->_getDelAddressData();
00445 
00446         $oUser = oxNew( 'oxuser' );
00447 
00448         try {
00449 
00450             $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00451 
00452             $iActState = $blActiveLogin ? 0 : 1;
00453 
00454             // setting values
00455             $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
00456             $oUser->setPassword( $sPassword );
00457             $oUser->oxuser__oxactive   = new oxField( $iActState, oxField::T_RAW);
00458 
00459             // used for checking if user email currently subscribed
00460             $iSubscriptionStatus = $oUser->getNewsSubscription()->getOptInStatus();
00461 
00462             $oUser->createUser();
00463             $oUser->load($oUser->getId());
00464             $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
00465 
00466             if ( $blActiveLogin ) {
00467                 // accepting terms..
00468                 $oUser->acceptTerms();
00469             }
00470 
00471             $sUserId = oxSession::getVar( "su" );
00472             $sRecEmail = oxSession::getVar( "re" );
00473             if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId && $sRecEmail ) {
00474                 // setting registration credit points..
00475                 $oUser->setCreditPointsForRegistrant( $sUserId, $sRecEmail );
00476             }
00477 
00478             // assigning to newsletter
00479             $blOptin = oxRegistry::getConfig()->getRequestParameter( 'blnewssubscribed' );
00480             if ( $blOptin && $iSubscriptionStatus == 1 ) {
00481                 // if user was assigned to newsletter and is creating account with newsletter checked, don't require confirm
00482                 $oUser->getNewsSubscription()->setOptInStatus(1);
00483                 $oUser->addToGroup( 'oxidnewsletter' );
00484                 $this->_blNewsSubscriptionStatus = 1;
00485             } else {
00486                 $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00487             }
00488 
00489             $oUser->addToGroup( 'oxidnotyetordered' );
00490             $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
00491             $oUser->logout();
00492 
00493         } catch ( oxUserException $oEx ) {
00494             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00495             return false;
00496         } catch( oxInputException $oEx ){
00497             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00498             return false;
00499         } catch( oxConnectionException $oEx ){
00500             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00501             return false;
00502         }
00503 
00504         if ( !$blActiveLogin ) {
00505 
00506                 oxSession::setVar( 'usr', $oUser->getId() );
00507                 $this->_afterLogin( $oUser );
00508 
00509 
00510             // order remark
00511             //V #427: order remark for new users
00512             $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00513             if ( $sOrderRemark ) {
00514                 oxSession::setVar( 'ordrem', $sOrderRemark );
00515             }
00516         }
00517 
00518         // send register eMail
00519         //TODO: move into user
00520         if ( (int) oxConfig::getParameter( 'option' ) == 3 ) {
00521             $oxEMail = oxNew( 'oxemail' );
00522             if ( $blActiveLogin ) {
00523                 $oxEMail->sendRegisterConfirmEmail( $oUser );
00524             } else {
00525                 $oxEMail->sendRegisterEmail( $oUser );
00526             }
00527         }
00528 
00529         // new registered
00530         $this->_blIsNewUser = true;
00531 
00532         return 'payment';
00533     }
00534 
00540     public function registerUser()
00541     {
00542         // registered new user ?
00543         if ( $this->createuser()!= false && $this->_blIsNewUser ) {
00544             if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
00545                 return 'register?success=1';
00546             } else {
00547                 return 'register?success=1&newslettererror=4';
00548             }
00549         } else {
00550             // problems with registration ...
00551             $this->logout();
00552         }
00553     }
00554 
00568     protected function _changeUser_noRedirect( )
00569     {
00570         if (!$this->getSession()->checkSessionChallenge()) {
00571             return;
00572         }
00573 
00574         // no user ?
00575         $oUser = $this->getUser();
00576         if ( !$oUser ) {
00577             return;
00578         }
00579 
00580         // collecting values to check
00581         $aDelAdress = $this->_getDelAddressData();
00582 
00583         // if user company name, user name and additional info has special chars
00584         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00585 
00586         $sUserName  = $oUser->oxuser__oxusername->value;
00587         $sPassword  = $sPassword2 = $oUser->oxuser__oxpassword->value;
00588 
00589         try { // testing user input
00590             $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00591             // assigning to newsletter
00592             if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
00593                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00594             }
00595             // check if email address changed, if so, force check news subscription settings.
00596             $blForceCheckOptIn = ( $aInvAdress['oxuser__oxusername'] !== null && $aInvAdress['oxuser__oxusername'] !== $sUserName );
00597             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ), $blForceCheckOptIn );
00598 
00599         } catch ( oxUserException $oEx ) { // errors in input
00600             // marking error code
00601             //TODO
00602             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00603             return;
00604         } catch(oxInputException $oEx) {
00605             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00606             return;
00607         } catch(oxConnectionException $oEx){
00608              //connection to external resource broken, change message and pass to the view
00609             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00610             return;
00611         }
00612 
00613 
00614         // order remark
00615         $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00616 
00617         if ( $sOrderRemark ) {
00618             oxSession::setVar( 'ordrem', $sOrderRemark );
00619         } else {
00620             oxSession::deleteVar( 'ordrem' );
00621         }
00622 
00623         if ( $oBasket = $this->getSession()->getBasket() ) {
00624             $oBasket->onUpdate();
00625         }
00626         return true;
00627     }
00628 
00635     protected function _getDelAddressData()
00636     {
00637         // if user company name, user name and additional info has special chars
00638         $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', true ) : array();
00639 
00640         if ( is_array( $aDeladr ) ) {
00641             // checking if data is filled
00642             if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
00643                 unset( $aDeladr['oxaddress__oxsal'] );
00644             }
00645             if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
00646                 // resetting to avoid empty records
00647                 $aDelAdress = array();
00648             }
00649         }
00650         return $aDelAdress;
00651     }
00652 
00658     protected function _getLogoutLink()
00659     {
00660         $oConfig = $this->getConfig();
00661 
00662         $sLogoutLink = $oConfig->isSsl()? $oConfig->getShopSecureHomeUrl() : $oConfig->getShopHomeUrl();
00663         $sLogoutLink .= 'cl='.$oConfig->getRequestParameter('cl').$this->getParent()->getDynUrlParams();
00664         if ( $sParam = $oConfig->getRequestParameter('anid') ) {
00665             $sLogoutLink .= '&amp;anid='.$sParam;
00666         }
00667         if ( $sParam = $oConfig->getRequestParameter('cnid') ) {
00668             $sLogoutLink .= '&amp;cnid='.$sParam;
00669         }
00670         if ( $sParam = $oConfig->getRequestParameter('mnid') ) {
00671             $sLogoutLink .= '&amp;mnid='.$sParam;
00672         }
00673         if ( $sParam = $oConfig->getRequestParameter('tpl') ) {
00674             $sLogoutLink .= '&amp;tpl='.$sParam;
00675         }
00676         if ( $sParam = $oConfig->getRequestParameter('oxloadid') ) {
00677             $sLogoutLink .= '&amp;oxloadid='.$sParam;
00678         }
00679         if ( $sParam = $oConfig->getRequestParameter('recommid') ) {
00680             $sLogoutLink .= '&amp;recommid='.$sParam;
00681         }
00682         return $sLogoutLink.'&amp;fnc=logout';
00683     }
00684 
00692     public function setLoginStatus( $iStatus )
00693     {
00694         $this->_iLoginStatus = $iStatus;
00695     }
00696 
00705     public function getLoginStatus()
00706     {
00707         return $this->_iLoginStatus;
00708     }
00709 
00715     public function getInvitor()
00716     {
00717         $sSu = oxSession::getVar( 'su' );
00718         if ( !$sSu && ( $sSuNew = oxConfig::getParameter( 'su' ) ) ) {
00719             oxSession::setVar( 'su', $sSuNew );
00720         }
00721     }
00722 
00728     public function setRecipient()
00729     {
00730         $sRe = oxSession::getVar( 're' );
00731         if ( !$sRe && ( $sReNew = oxConfig::getParameter( 're' ) ) ) {
00732             oxSession::setVar( 're', $sReNew );
00733         }
00734     }
00735 }