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                                         );
00067     protected $_blActiveLogin = false;
00068 
00076     protected function _setActiveLogin( $blActiveLogin )
00077     {
00078         $this->_blActiveLogin = $blActiveLogin;
00079     }
00080 
00086     protected function _getActiveLogin()
00087     {
00088         return $this->_blActiveLogin;
00089     }
00090 
00100     public function init()
00101     {
00102         // saving show/hide delivery address state
00103         $blShow = oxConfig::getParameter( 'blshowshipaddress' );
00104         if (!isset($blShow)) {
00105             $blShow = oxSession::getVar( 'blshowshipaddress' );
00106         }
00107 
00108         oxSession::setVar( 'blshowshipaddress', $blShow );
00109 
00110         // load session user
00111         $this->_loadSessionUser();
00112         if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) ) {
00113             // get invitor ID
00114             $this->getInvitor();
00115             $this->setRecipient();
00116         }
00117 
00118         // dyn_group feature: if you specify a groupid in URL the user
00119         // will automatically be added to this group later
00120         if ( $sDynGoup = oxConfig::getParameter( 'dgr' ) ) {
00121             oxSession::setVar( 'dgr', $sDynGoup );
00122         }
00123 
00124         parent::init();
00125     }
00126 
00136     public function render()
00137     {
00138         // checks if private sales allows further tasks
00139         $this->_checkPsState();
00140 
00141         parent::render();
00142 
00143         return $this->getUser();
00144     }
00145 
00156     protected function _checkPsState()
00157     {
00158         $oConfig = $this->getConfig();
00159         if ( $this->getParent()->isEnabledPrivateSales() ) {
00160             // load session user
00161             $oUser  = $this->getUser();
00162             $sClass = $this->getParent()->getClassName();
00163 
00164             // no session user
00165             if ( !$oUser && !in_array( $sClass, $this->_aAllowedClasses ) ) {
00166                 oxRegistry::getUtils()->redirect( $oConfig->getShopHomeURL() . 'cl=account', false, 302 );
00167             }
00168 
00169             if ( $oUser && !$oUser->isTermsAccepted() && !in_array( $sClass, $this->_aAllowedClasses ) ) {
00170                 oxRegistry::getUtils()->redirect( $oConfig->getShopHomeURL() . 'cl=account&term=1', false, 302 );
00171             }
00172         }
00173     }
00174 
00180     protected function _loadSessionUser()
00181     {
00182         $myConfig = $this->getConfig();
00183         $oUser = $this->getUser();
00184 
00185         // no session user
00186         if ( !$oUser ) {
00187             return;
00188         }
00189 
00190         // this user is blocked, deny him
00191         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00192             oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL() . 'cl=content&tpl=user_blocked.tpl', true, 302  );
00193         }
00194 
00195         // TODO: move this to a proper place
00196         if ( $oUser->isLoadedFromCookie() && !$myConfig->getConfigParam( 'blPerfNoBasketSaving' )) {
00197 
00198             if ( $oBasket = $this->getSession()->getBasket() ) {
00199                 $oBasket->load();
00200                 $oBasket->onUpdate();
00201             }
00202         }
00203     }
00204 
00218     public function login()
00219     {
00220         $sUser     = oxConfig::getParameter( 'lgn_usr' );
00221         $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
00222         $sCookie   = oxConfig::getParameter( 'lgn_cook' );
00223         //$blFbLogin = oxConfig::getParameter( 'fblogin' );
00224 
00225         $this->setLoginStatus( USER_LOGIN_FAIL );
00226 
00227         // trying to login user
00228         try {
00229             $oUser = oxNew( 'oxuser' );
00230             $oUser->login( $sUser, $sPassword, $sCookie );
00231             $this->setLoginStatus( USER_LOGIN_SUCCESS );
00232         } catch ( oxUserException $oEx ) {
00233             // for login component send excpetion text to a custom component (if defined)
00234             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true, '', false );
00235             return 'user';
00236         } catch( oxCookieException $oEx ){
00237             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
00238             return 'user';
00239         }
00240         // finalizing ..
00241         return $this->_afterLogin( $oUser );
00242     }
00243 
00261     protected function _afterLogin( $oUser )
00262     {
00263         $oSession = $this->getSession();
00264 
00265         // generating new session id after login
00266         if ( $this->getLoginStatus() === USER_LOGIN_SUCCESS ) {
00267             $oSession->regenerateSessionId();
00268         }
00269 
00270         $myConfig = $this->getConfig();
00271 
00272         // this user is blocked, deny him
00273         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00274             oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL().'cl=content&tpl=user_blocked.tpl', true, 302 );
00275         }
00276 
00277         // adding to dyn group
00278         $oUser->addDynGroup(oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ));
00279 
00280         // recalc basket
00281         if ( $oBasket = $oSession->getBasket() ) {
00282             $oBasket->onUpdate();
00283         }
00284 
00285 
00286         return 'payment';
00287     }
00288 
00295     public function login_noredirect()
00296     {
00297         $blAgb = oxConfig::getParameter( 'ord_agb' );
00298         $oConfig = $this->getConfig();
00299         if ( $this->getParent()->isEnabledPrivateSales() && $blAgb !== null && ( $oUser = $this->getUser() ) ) {
00300             if ( $blAgb ) {
00301                 $oUser->acceptTerms();
00302             }
00303         } else {
00304             $this->login();
00305 
00306             if ( !$this->isAdmin() && !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' )) {
00307                 //load basket from the database
00308                 try {
00309                     if ( $oBasket = $this->getSession()->getBasket() ) {
00310                         $oBasket->load();
00311                     }
00312                 } catch ( Exception $oE ) {
00313                     //just ignore it
00314                 }
00315             }
00316 
00317 
00318         }
00319     }
00320 
00327     public function login_updateFbId()
00328     {
00329         $this->login();
00330 
00331         if ( $oUser = $this->getUser() ) {
00332             //updating user Facebook ID
00333             if ( $oUser->updateFbId() ) {
00334                 oxSession::setVar( '_blFbUserIdUpdated', true );
00335             }
00336         }
00337     }
00338 
00347     protected function _afterLogout()
00348     {
00349         oxSession::deleteVar( 'paymentid' );
00350         oxSession::deleteVar( 'sShipSet' );
00351         oxSession::deleteVar( 'deladrid' );
00352         oxSession::deleteVar( 'dynvalue' );
00353 
00354         // resetting & recalc basket
00355         if ( ( $oBasket = $this->getSession()->getBasket() ) ) {
00356             $oBasket->resetUserInfo();
00357             $oBasket->onUpdate();
00358         }
00359     }
00360 
00369     public function logout()
00370     {
00371         $myConfig  = $this->getConfig();
00372         $oUser = oxNew( 'oxuser' );
00373 
00374         if ( $oUser->logout() ) {
00375 
00376             $this->setLoginStatus( USER_LOGOUT );
00377 
00378             // finalizing ..
00379             $this->_afterLogout();
00380 
00381 
00382             if ( $this->getParent()->isEnabledPrivateSales() ) {
00383                 return 'account';
00384             }
00385 
00386             // redirecting if user logs out in SSL mode
00387             if ( oxConfig::getParameter('redirect') && $myConfig->getConfigParam( 'sSSLShopURL' ) ) {
00388                 oxRegistry::getUtils()->redirect( $this->_getLogoutLink());
00389             }
00390         }
00391     }
00392 
00402     public function changeUser( )
00403     {
00404         $blUserRegistered = $this->_changeUser_noRedirect();
00405 
00406         if ( $blUserRegistered === true ) {
00407             return 'payment';
00408         } else {
00409             return $blUserRegistered;
00410         }
00411     }
00412 
00419     public function changeuser_testvalues()
00420     {
00421         // skip updating user info if this is just form reload
00422         // on selecting delivery address
00423         // We do redirect only on success not to loose errors.
00424 
00425         if ( $this->_changeUser_noRedirect() ) {
00426             return 'account_user';
00427         }
00428     }
00429 
00451     public function createUser()
00452     {
00453         $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
00454         $this->_setActiveLogin( $blActiveLogin );
00455 
00456         $myConfig = $this->getConfig();
00457         if ( $blActiveLogin && !oxConfig::getParameter( 'ord_agb' ) && $myConfig->getConfigParam( 'blConfirmAGB' ) ) {
00458             oxRegistry::get("oxUtilsView")->addErrorToDisplay( 'READ_AND_CONFIRM_TERMS', false, true );
00459             return;
00460         }
00461 
00462         $myUtils  = oxRegistry::getUtils();
00463 
00464         // collecting values to check
00465         $sUser = oxConfig::getParameter( 'lgn_usr' );
00466 
00467         // first pass
00468         $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
00469 
00470         // second pass
00471         $sPassword2 = oxConfig::getParameter( 'lgn_pwd2', true );
00472 
00473         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00474         $aDelAdress = $this->_getDelAddressData();
00475 
00476         $oUser = oxNew( 'oxuser' );
00477 
00478         try {
00479 
00480             $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00481 
00482             $iActState = $blActiveLogin ? 0 : 1;
00483 
00484             // setting values
00485             $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
00486             $oUser->setPassword( $sPassword );
00487             $oUser->oxuser__oxactive   = new oxField( $iActState, oxField::T_RAW);
00488 
00489             // used for checking if user email currently subscribed
00490             $iSubscriptionStatus = $oUser->getNewsSubscription()->getOptInStatus();
00491 
00492             $oUser->createUser();
00493             $oUser->load($oUser->getId());
00494             $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
00495 
00496             if ( $blActiveLogin ) {
00497                 // accepting terms..
00498                 $oUser->acceptTerms();
00499             }
00500 
00501             $sUserId = oxSession::getVar( "su" );
00502             $sRecEmail = oxSession::getVar( "re" );
00503             if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId && $sRecEmail ) {
00504                 // setting registration credit points..
00505                 $oUser->setCreditPointsForRegistrant( $sUserId, $sRecEmail );
00506             }
00507 
00508             // assigning to newsletter
00509             $blOptin = oxRegistry::getConfig()->getRequestParameter( 'blnewssubscribed' );
00510             if ( $blOptin && $iSubscriptionStatus == 1 ) {
00511                 // if user was assigned to newsletter and is creating account with newsletter checked, don't require confirm
00512                 $oUser->getNewsSubscription()->setOptInStatus(1);
00513                 $oUser->addToGroup( 'oxidnewsletter' );
00514                 $this->_blNewsSubscriptionStatus = 1;
00515             } else {
00516                 $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00517             }
00518 
00519             $oUser->addToGroup( 'oxidnotyetordered' );
00520             $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
00521             $oUser->logout();
00522 
00523         } catch ( oxUserException $oEx ) {
00524             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00525             return false;
00526         } catch( oxInputException $oEx ){
00527             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00528             return false;
00529         } catch( oxConnectionException $oEx ){
00530             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00531             return false;
00532         }
00533 
00534         $this->_setOrderRemark( $oUser );
00535 
00536         // send register eMail
00537         //TODO: move into user
00538         $this->_sendRegistrationEmail( $oUser );
00539 
00540         // new registered
00541         $this->_blIsNewUser = true;
00542 
00543         $sAction = 'payment?new_user=1&success=1';
00544         if ($this->_blNewsSubscriptionStatus !== null && !$this->_blNewsSubscriptionStatus) {
00545             $sAction = 'payment?new_user=1&success=1&newslettererror=4';
00546         }
00547 
00548         return $sAction;
00549     }
00550 
00556     public function registerUser()
00557     {
00558         // registered new user ?
00559         if ( $this->createuser()!= false && $this->_blIsNewUser ) {
00560             if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
00561                 return 'register?success=1';
00562             } else {
00563                 return 'register?success=1&newslettererror=4';
00564             }
00565         } else {
00566             // problems with registration ...
00567             $this->logout();
00568         }
00569     }
00570 
00584     protected function _changeUser_noRedirect( )
00585     {
00586         if (!$this->getSession()->checkSessionChallenge()) {
00587             return;
00588         }
00589 
00590         // no user ?
00591         $oUser = $this->getUser();
00592         if ( !$oUser ) {
00593             return;
00594         }
00595 
00596         // collecting values to check
00597         $aDelAdress = $this->_getDelAddressData();
00598 
00599         // if user company name, user name and additional info has special chars
00600         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00601 
00602         $sUserName  = $oUser->oxuser__oxusername->value;
00603         $sPassword  = $sPassword2 = $oUser->oxuser__oxpassword->value;
00604 
00605         try { // testing user input
00606             $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00607             // assigning to newsletter
00608             if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
00609                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00610             }
00611             // check if email address changed, if so, force check news subscription settings.
00612             $blForceCheckOptIn = ( $aInvAdress['oxuser__oxusername'] !== null && $aInvAdress['oxuser__oxusername'] !== $sUserName );
00613             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ), $blForceCheckOptIn );
00614 
00615         } catch ( oxUserException $oEx ) { // errors in input
00616             // marking error code
00617             //TODO
00618             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00619             return;
00620         } catch(oxInputException $oEx) {
00621             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00622             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'input_not_all_fields');
00623             return;
00624         } catch(oxConnectionException $oEx){
00625              //connection to external resource broken, change message and pass to the view
00626             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00627             return;
00628         }
00629 
00630 
00631         // order remark
00632         $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00633 
00634         if ( $sOrderRemark ) {
00635             oxSession::setVar( 'ordrem', $sOrderRemark );
00636         } else {
00637             oxSession::deleteVar( 'ordrem' );
00638         }
00639 
00640         if ( $oBasket = $this->getSession()->getBasket() ) {
00641             $oBasket->onUpdate();
00642         }
00643         return true;
00644     }
00645 
00652     protected function _getDelAddressData()
00653     {
00654         // if user company name, user name and additional info has special chars
00655         $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', true ) : array();
00656 
00657         if ( is_array( $aDeladr ) ) {
00658             // checking if data is filled
00659             if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
00660                 unset( $aDeladr['oxaddress__oxsal'] );
00661             }
00662             if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
00663                 // resetting to avoid empty records
00664                 $aDelAdress = array();
00665             }
00666         }
00667         return $aDelAdress;
00668     }
00669 
00675     protected function _getLogoutLink()
00676     {
00677         $oConfig = $this->getConfig();
00678 
00679         $sLogoutLink = $oConfig->isSsl()? $oConfig->getShopSecureHomeUrl() : $oConfig->getShopHomeUrl();
00680         $sLogoutLink .= 'cl='.$oConfig->getRequestParameter('cl').$this->getParent()->getDynUrlParams();
00681         if ( $sParam = $oConfig->getRequestParameter('anid') ) {
00682             $sLogoutLink .= '&amp;anid='.$sParam;
00683         }
00684         if ( $sParam = $oConfig->getRequestParameter('cnid') ) {
00685             $sLogoutLink .= '&amp;cnid='.$sParam;
00686         }
00687         if ( $sParam = $oConfig->getRequestParameter('mnid') ) {
00688             $sLogoutLink .= '&amp;mnid='.$sParam;
00689         }
00690         if ( $sParam = $oConfig->getRequestParameter('tpl') ) {
00691             $sLogoutLink .= '&amp;tpl='.$sParam;
00692         }
00693         if ( $sParam = $oConfig->getRequestParameter('oxloadid') ) {
00694             $sLogoutLink .= '&amp;oxloadid='.$sParam;
00695         }
00696         if ( $sParam = $oConfig->getRequestParameter('recommid') ) {
00697             $sLogoutLink .= '&amp;recommid='.$sParam;
00698         }
00699         return $sLogoutLink.'&amp;fnc=logout';
00700     }
00701 
00709     public function setLoginStatus( $iStatus )
00710     {
00711         $this->_iLoginStatus = $iStatus;
00712     }
00713 
00722     public function getLoginStatus()
00723     {
00724         return $this->_iLoginStatus;
00725     }
00726 
00732     public function getInvitor()
00733     {
00734         $sSu = oxSession::getVar( 'su' );
00735         if ( !$sSu && ( $sSuNew = oxConfig::getParameter( 'su' ) ) ) {
00736             oxSession::setVar( 'su', $sSuNew );
00737         }
00738     }
00739 
00745     public function setRecipient()
00746     {
00747         $sRe = oxSession::getVar( 're' );
00748         if ( !$sRe && ( $sReNew = oxConfig::getParameter( 're' ) ) ) {
00749             oxSession::setVar( 're', $sReNew );
00750         }
00751     }
00752 
00760     public function _setOrderRemark( $oUser )
00761     {
00762         $blActiveLogin = $this->_getActiveLogin();
00763         if ( !$blActiveLogin ) {
00764 
00765             oxRegistry::getSession()->setVariable( 'usr', $oUser->getId() );
00766             $this->_afterLogin( $oUser );
00767 
00768 
00769             // order remark
00770             //V #427: order remark for new users
00771             $sOrderRemark = oxRegistry::getConfig()->getRequestParameter( 'order_remark', true );
00772             if ( $sOrderRemark ) {
00773                 oxRegistry::getSession()->setVariable( 'ordrem', $sOrderRemark );
00774             }
00775         }
00776     }
00777 
00785     public function _sendRegistrationEmail( $oUser )
00786     {
00787         $blActiveLogin = $this->_getActiveLogin();
00788         if ( (int) oxRegistry::getConfig()->getRequestParameter( 'option' ) == 3 ) {
00789             $oxEMail = oxNew( 'oxemail' );
00790             if ( $blActiveLogin ) {
00791                 $oxEMail->sendRegisterConfirmEmail( $oUser );
00792             } else {
00793                 $oxEMail->sendRegisterEmail( $oUser );
00794             }
00795         }
00796     }
00797 }