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         return 'payment';
00544     }
00545 
00551     public function registerUser()
00552     {
00553         // registered new user ?
00554         if ( $this->createuser()!= false && $this->_blIsNewUser ) {
00555             if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
00556                 return 'register?success=1';
00557             } else {
00558                 return 'register?success=1&newslettererror=4';
00559             }
00560         } else {
00561             // problems with registration ...
00562             $this->logout();
00563         }
00564     }
00565 
00579     protected function _changeUser_noRedirect( )
00580     {
00581         if (!$this->getSession()->checkSessionChallenge()) {
00582             return;
00583         }
00584 
00585         // no user ?
00586         $oUser = $this->getUser();
00587         if ( !$oUser ) {
00588             return;
00589         }
00590 
00591         // collecting values to check
00592         $aDelAdress = $this->_getDelAddressData();
00593 
00594         // if user company name, user name and additional info has special chars
00595         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00596 
00597         $sUserName  = $oUser->oxuser__oxusername->value;
00598         $sPassword  = $sPassword2 = $oUser->oxuser__oxpassword->value;
00599 
00600         try { // testing user input
00601             $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00602             // assigning to newsletter
00603             if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
00604                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00605             }
00606             // check if email address changed, if so, force check news subscription settings.
00607             $blForceCheckOptIn = ( $aInvAdress['oxuser__oxusername'] !== null && $aInvAdress['oxuser__oxusername'] !== $sUserName );
00608             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ), $blForceCheckOptIn );
00609 
00610         } catch ( oxUserException $oEx ) { // errors in input
00611             // marking error code
00612             //TODO
00613             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00614             return;
00615         } catch(oxInputException $oEx) {
00616             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00617             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'input_not_all_fields');
00618             return;
00619         } catch(oxConnectionException $oEx){
00620              //connection to external resource broken, change message and pass to the view
00621             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00622             return;
00623         }
00624 
00625 
00626         // order remark
00627         $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00628 
00629         if ( $sOrderRemark ) {
00630             oxSession::setVar( 'ordrem', $sOrderRemark );
00631         } else {
00632             oxSession::deleteVar( 'ordrem' );
00633         }
00634 
00635         if ( $oBasket = $this->getSession()->getBasket() ) {
00636             $oBasket->onUpdate();
00637         }
00638         return true;
00639     }
00640 
00647     protected function _getDelAddressData()
00648     {
00649         // if user company name, user name and additional info has special chars
00650         $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', true ) : array();
00651 
00652         if ( is_array( $aDeladr ) ) {
00653             // checking if data is filled
00654             if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
00655                 unset( $aDeladr['oxaddress__oxsal'] );
00656             }
00657             if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
00658                 // resetting to avoid empty records
00659                 $aDelAdress = array();
00660             }
00661         }
00662         return $aDelAdress;
00663     }
00664 
00670     protected function _getLogoutLink()
00671     {
00672         $oConfig = $this->getConfig();
00673 
00674         $sLogoutLink = $oConfig->isSsl()? $oConfig->getShopSecureHomeUrl() : $oConfig->getShopHomeUrl();
00675         $sLogoutLink .= 'cl='.$oConfig->getRequestParameter('cl').$this->getParent()->getDynUrlParams();
00676         if ( $sParam = $oConfig->getRequestParameter('anid') ) {
00677             $sLogoutLink .= '&amp;anid='.$sParam;
00678         }
00679         if ( $sParam = $oConfig->getRequestParameter('cnid') ) {
00680             $sLogoutLink .= '&amp;cnid='.$sParam;
00681         }
00682         if ( $sParam = $oConfig->getRequestParameter('mnid') ) {
00683             $sLogoutLink .= '&amp;mnid='.$sParam;
00684         }
00685         if ( $sParam = $oConfig->getRequestParameter('tpl') ) {
00686             $sLogoutLink .= '&amp;tpl='.$sParam;
00687         }
00688         if ( $sParam = $oConfig->getRequestParameter('oxloadid') ) {
00689             $sLogoutLink .= '&amp;oxloadid='.$sParam;
00690         }
00691         if ( $sParam = $oConfig->getRequestParameter('recommid') ) {
00692             $sLogoutLink .= '&amp;recommid='.$sParam;
00693         }
00694         return $sLogoutLink.'&amp;fnc=logout';
00695     }
00696 
00704     public function setLoginStatus( $iStatus )
00705     {
00706         $this->_iLoginStatus = $iStatus;
00707     }
00708 
00717     public function getLoginStatus()
00718     {
00719         return $this->_iLoginStatus;
00720     }
00721 
00727     public function getInvitor()
00728     {
00729         $sSu = oxSession::getVar( 'su' );
00730         if ( !$sSu && ( $sSuNew = oxConfig::getParameter( 'su' ) ) ) {
00731             oxSession::setVar( 'su', $sSuNew );
00732         }
00733     }
00734 
00740     public function setRecipient()
00741     {
00742         $sRe = oxSession::getVar( 're' );
00743         if ( !$sRe && ( $sReNew = oxConfig::getParameter( 're' ) ) ) {
00744             oxSession::setVar( 're', $sReNew );
00745         }
00746     }
00747 
00755     public function _setOrderRemark( $oUser )
00756     {
00757         $blActiveLogin = $this->_getActiveLogin();
00758         if ( !$blActiveLogin ) {
00759 
00760             oxRegistry::getSession()->setVariable( 'usr', $oUser->getId() );
00761             $this->_afterLogin( $oUser );
00762 
00763 
00764             // order remark
00765             //V #427: order remark for new users
00766             $sOrderRemark = oxRegistry::getConfig()->getRequestParameter( 'order_remark', true );
00767             if ( $sOrderRemark ) {
00768                 oxRegistry::getSession()->setVariable( 'ordrem', $sOrderRemark );
00769             }
00770         }
00771     }
00772 
00780     public function _sendRegistrationEmail( $oUser )
00781     {
00782         $blActiveLogin = $this->_getActiveLogin();
00783         if ( (int) oxRegistry::getConfig()->getRequestParameter( 'option' ) == 3 ) {
00784             $oxEMail = oxNew( 'oxemail' );
00785             if ( $blActiveLogin ) {
00786                 $oxEMail->sendRegisterConfirmEmail( $oUser );
00787             } else {
00788                 $oxEMail->sendRegisterEmail( $oUser );
00789             }
00790         }
00791     }
00792 }