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                                         );
00069     public function init()
00070     {
00071         // saving show/hide delivery address state
00072         $blShow = oxConfig::getParameter( 'blshowshipaddress' );
00073         if (!isset($blShow)) {
00074             $blShow = oxSession::getVar( 'blshowshipaddress' );
00075         }
00076         // @deprecated, remove blhideshipaddress checking when basic theme support discontinued
00077         if (oxConfig::getParameter( 'blhideshipaddress' ) || oxSession::getVar( 'blhideshipaddress' )) {
00078             $blShow = false;
00079         }
00080 
00081         oxSession::setVar( 'blshowshipaddress', $blShow );
00082 
00083         // load session user
00084         $this->_loadSessionUser();
00085         if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) ) {
00086             // get invitor ID
00087             $this->getInvitor();
00088             $this->setRecipient();
00089         }
00090 
00091         parent::init();
00092     }
00093 
00103     public function render()
00104     {
00105         // checks if private sales allows further tasks
00106         $this->_checkPsState();
00107 
00108         parent::render();
00109 
00110         // dyn_group feature: if you specify a groupid in URL the user
00111         // will automatically be added to this group later
00112         if ( $sDynGoup = oxConfig::getParameter( 'dgr' ) ) {
00113             oxSession::setVar( 'dgr', $sDynGoup );
00114         }
00115 
00116         return $this->getUser();
00117     }
00118 
00129     protected function _checkPsState()
00130     {
00131         $oConfig = $this->getConfig();
00132         if ( $this->getParent()->isEnabledPrivateSales() ) {
00133             // load session user
00134             $oUser  = $this->getUser();
00135             $sClass = $this->getParent()->getClassName();
00136 
00137             // no session user
00138             if ( !$oUser && !in_array( $sClass, $this->_aAllowedClasses ) ) {
00139                 oxUtils::getInstance()->redirect( $oConfig->getShopHomeURL() . 'cl=account', false, 302 );
00140             }
00141 
00142             if ( $oUser && !$oUser->isTermsAccepted() && !in_array( $sClass, $this->_aAllowedClasses ) ) {
00143                 oxUtils::getInstance()->redirect( $oConfig->getShopHomeURL() . 'cl=account&term=1', false, 302 );
00144             }
00145         }
00146     }
00147 
00153     protected function _loadSessionUser()
00154     {
00155         $myConfig = $this->getConfig();
00156         $oUser = $this->getUser();
00157 
00158         // no session user
00159         if ( !$oUser ) {
00160             return;
00161         }
00162 
00163         // this user is blocked, deny him
00164         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00165             oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() . 'cl=content&tpl=user_blocked.tpl', true, 302  );
00166         }
00167 
00168         // TODO: move this to a proper place
00169         if ( $oUser->isLoadedFromCookie() ) {
00170 
00171             // #1678 R
00172             if ( !$myConfig->getConfigParam( 'blPerfNoBasketSaving' ) ) {
00173                 $myConfig->setGlobalParameter( 'blUserChanged', 1 );
00174             }
00175 
00176             if ( $oBasket = $this->getSession()->getBasket() ) {
00177                 $oBasket->load();
00178                 $oBasket->onUpdate();
00179             }
00180         }
00181     }
00182 
00196     public function login()
00197     {
00198         $sUser     = oxConfig::getParameter( 'lgn_usr' );
00199         $sPassword = oxConfig::getParameter( 'lgn_pwd' );
00200         $sCookie   = oxConfig::getParameter( 'lgn_cook' );
00201         //$blFbLogin = oxConfig::getParameter( 'fblogin' );
00202 
00203         $this->setLoginStatus( USER_LOGIN_FAIL );
00204 
00205         // trying to login user
00206         try {
00207             $oUser = oxNew( 'oxuser' );
00208             $oUser->login( $sUser, $sPassword, $sCookie );
00209             $this->setLoginStatus( USER_LOGIN_SUCCESS );
00210         } catch ( oxUserException $oEx ) {
00211             // for login component send excpetion text to a custom component (if defined)
00212             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00213             return 'user';
00214         } catch( oxCookieException $oEx ){
00215             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00216             return 'user';
00217         }
00218         // finalizing ..
00219         return $this->_afterLogin( $oUser );
00220     }
00221 
00239     protected function _afterLogin( $oUser )
00240     {
00241         $oSession = $this->getSession();
00242 
00243         // generating new session id after login
00244         if ( $this->getLoginStatus() === USER_LOGIN_SUCCESS ) {
00245             $oSession->regenerateSessionId();
00246         }
00247 
00248         $myConfig = $this->getConfig();
00249 
00250         // this user is blocked, deny him
00251         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00252             oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL().'cl=content&tpl=user_blocked.tpl', true, 302 );
00253         }
00254 
00255         // adding to dyn group
00256         $oUser->addDynGroup(oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ));
00257 
00258         // recalc basket
00259         if ( $oBasket = $oSession->getBasket() ) {
00260             $oBasket->onUpdate();
00261         }
00262 
00263         // #1678 R
00264         if ( !$myConfig->getConfigParam( 'blPerfNoBasketSaving' ) ) {
00265             $myConfig->setGlobalParameter( 'blUserChanged', 1);
00266         }
00267 
00268 
00269         return 'payment';
00270     }
00271 
00278     public function login_noredirect()
00279     {
00280         $blAgb = oxConfig::getParameter( 'ord_agb' );
00281         $oConfig = $this->getConfig();
00282         if ( $this->getParent()->isEnabledPrivateSales() && $blAgb !== null && ( $oUser = $this->getUser() ) ) {
00283             if ( $blAgb ) {
00284                 $oUser->acceptTerms();
00285             }
00286         } else {
00287             $this->login();
00288         }
00289     }
00290 
00297     public function login_updateFbId()
00298     {
00299         $this->login();
00300 
00301         if ( $oUser = $this->getUser() ) {
00302             //updating user Facebook ID
00303             if ( $oUser->updateFbId() ) {
00304                 oxSession::setVar( '_blFbUserIdUpdated', true );
00305             }
00306         }
00307     }
00308 
00317     protected function _afterLogout()
00318     {
00319         oxSession::deleteVar( 'paymentid' );
00320         oxSession::deleteVar( 'sShipSet' );
00321         oxSession::deleteVar( 'deladrid' );
00322         oxSession::deleteVar( 'dynvalue' );
00323 
00324         // resetting & recalc basket
00325         if ( ( $oBasket = $this->getSession()->getBasket() ) ) {
00326             $oBasket->resetUserInfo();
00327             $oBasket->onUpdate();
00328         }
00329     }
00330 
00339     public function logout()
00340     {
00341         $myConfig  = $this->getConfig();
00342         $oUser = oxNew( 'oxuser' );
00343 
00344         if ( $oUser->logout() ) {
00345 
00346             $this->setLoginStatus( USER_LOGOUT );
00347 
00348             // finalizing ..
00349             $this->_afterLogout();
00350 
00351 
00352             if ( $this->getParent()->isEnabledPrivateSales() ) {
00353                 return 'account';
00354             }
00355 
00356             // redirecting if user logs out in SSL mode
00357             if ( oxConfig::getParameter('redirect') && $myConfig->getConfigParam( 'sSSLShopURL' ) ) {
00358 
00359                 oxUtils::getInstance()->redirect( $this->_getLogoutLink());
00360             }
00361         }
00362     }
00363 
00373     public function changeUser( )
00374     {
00375         // checking if "open address area" button was clicked
00376         // or reloading form when delivery address was selected
00377         if ( $this->_setupDelAddress() ) {
00378             return;
00379         }
00380 
00381         $blUserRegistered = $this->_changeUser_noRedirect();
00382 
00383         if ( $blUserRegistered === true ) {
00384             return 'payment';
00385         } else {
00386             return $blUserRegistered;
00387         }
00388     }
00389 
00395     public function changeuser_testvalues()
00396     {
00397         // skip updating user info if this is just form reload
00398         // on selecting delivery address
00399 
00400         $this->_changeUser_noRedirect();
00401     }
00402 
00424     public function createUser()
00425     {
00426         // checking if "open address area" button was clicked
00427         if ( $blSetup = $this->_setupDelAddress() ) {
00428             return;
00429         }
00430 
00431         $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
00432 
00433         $myConfig = $this->getConfig();
00434         if ( $blActiveLogin && !oxConfig::getParameter( 'ord_agb' ) && $myConfig->getConfigParam( 'blConfirmAGB' ) ) {
00435             oxUtilsView::getInstance()->addErrorToDisplay( 'ORDER_READANDCONFIRMTERMS', false, true );
00436             return;
00437         }
00438 
00439         $myUtils  = oxUtils::getInstance();
00440 
00441         // collecting values to check
00442         $sUser = oxConfig::getParameter( 'lgn_usr' );
00443 
00444         // first pass
00445         $sPassword = oxConfig::getParameter( 'lgn_pwd' );
00446 
00447         // second pass
00448         $sPassword2 = oxConfig::getParameter( 'lgn_pwd2' );
00449 
00450         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00451         $aDelAdress = $this->_getDelAddressData();
00452 
00453         $oUser = oxNew( 'oxuser' );
00454 
00455         try {
00456 
00457             $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00458 
00459             $iActState = $blActiveLogin ? 0 : 1;
00460 
00461             // setting values
00462             $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
00463             $oUser->setPassword( $sPassword );
00464             $oUser->oxuser__oxactive   = new oxField( $iActState, oxField::T_RAW);
00465 
00466             $oUser->createUser();
00467             $oUser->load( $oUser->getId() );
00468             $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
00469 
00470             if ( $blActiveLogin ) {
00471                 // accepting terms..
00472                 $oUser->acceptTerms();
00473             }
00474 
00475             $sUserId = oxSession::getVar( "su" );
00476             $sRecEmail = oxSession::getVar( "re" );
00477             if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId && $sRecEmail ) {
00478                 // setting registration credit points..
00479                 $oUser->setCreditPointsForRegistrant( $sUserId, $sRecEmail );
00480             }
00481 
00482             // assigning to newsletter
00483             $blOptin = oxConfig::getParameter( 'blnewssubscribed' );
00484             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00485 
00486             $oUser->addToGroup( 'oxidnotyetordered' );
00487             $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
00488             $oUser->logout();
00489 
00490         } catch ( oxUserException $oEx ) {
00491             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00492             return false;
00493         } catch( oxInputException $oEx ){
00494             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00495             return false;
00496         } catch( oxConnectionException $oEx ){
00497             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00498             return false;
00499         }
00500 
00501         if ( !$blActiveLogin ) {
00502             if ( !$sPassword ) {
00503                 oxSession::setVar( 'usr', $oUser->getId() );
00504                 $this->_afterLogin( $oUser );
00505             } elseif ( $this->login() == 'user' ) {
00506                 return false;
00507             }
00508 
00509             // order remark
00510             //V #427: order remark for new users
00511             $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00512             if ( $sOrderRemark ) {
00513                 oxSession::setVar( 'ordrem', $sOrderRemark );
00514             }
00515         }
00516 
00517         // send register eMail
00518         //TODO: move into user
00519         if ( (int) oxConfig::getParameter( 'option' ) == 3 ) {
00520             $oxEMail = oxNew( 'oxemail' );
00521             if ( $blActiveLogin ) {
00522                 $oxEMail->sendRegisterConfirmEmail( $oUser );
00523             } else {
00524                 $oxEMail->sendRegisterEmail( $oUser );
00525             }
00526         }
00527 
00528         // new registered
00529         $this->_blIsNewUser = true;
00530 
00531         return 'payment';
00532     }
00533 
00539     public function registerUser()
00540     {
00541         // checking if "open address area" button was clicked
00542         if ( $blSetup = $this->_setupDelAddress() ) {
00543             return;
00544         }
00545 
00546         // registered new user ?
00547         if ( $this->createuser()!= false && $this->_blIsNewUser ) {
00548             if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
00549                 return 'register?success=1';
00550             } else {
00551                 return 'register?success=1&newslettererror=4';
00552             }
00553         } else {
00554             // problems with registration ...
00555             $this->logout();
00556         }
00557     }
00558 
00572     protected function _changeUser_noRedirect( )
00573     {
00574         if (!$this->getSession()->checkSessionChallenge()) {
00575             return;
00576         }
00577 
00578         // no user ?
00579         $oUser = $this->getUser();
00580         if ( !$oUser ) {
00581             return;
00582         }
00583 
00584         // collecting values to check
00585         $aDelAdress = $this->_getDelAddressData();
00586 
00587         // if user company name, user name and additional info has special chars
00588         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00589 
00590         $sUserName  = $oUser->oxuser__oxusername->value;
00591         $sPassword  = $sPassword2 = $oUser->oxuser__oxpassword->value;
00592 
00593         try { // testing user input
00594             $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00595             // assigning to newsletter
00596             if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
00597                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00598             }
00599             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00600 
00601         } catch ( oxUserException $oEx ) { // errors in input
00602             // marking error code
00603             //TODO
00604             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00605             return;
00606         } catch(oxInputException $oEx) {
00607             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00608             return;
00609         } catch(oxConnectionException $oEx){
00610              //connection to external resource broken, change message and pass to the view
00611             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00612             return;
00613         }
00614 
00615 
00616         // order remark
00617         $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00618 
00619         if ( $sOrderRemark ) {
00620             oxSession::setVar( 'ordrem', $sOrderRemark );
00621         } else {
00622             oxSession::deleteVar( 'ordrem' );
00623         }
00624 
00625         if ( $oBasket = $this->getSession()->getBasket() ) {
00626             $oBasket->onUpdate();
00627         }
00628         return true;
00629     }
00630 
00637     protected function _getDelAddressData()
00638     {
00639         // if user company name, user name and additional info has special chars
00640         $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', true ) : array();
00641 
00642         if ( is_array( $aDeladr ) ) {
00643             // checking if data is filled
00644             if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
00645                 unset( $aDeladr['oxaddress__oxsal'] );
00646             }
00647             if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
00648                 // resetting to avoid empty records
00649                 $aDelAdress = array();
00650             }
00651         }
00652         return $aDelAdress;
00653     }
00654 
00660     protected function _getLogoutLink()
00661     {
00662         $myConfig = $this->getConfig();
00663         $sLogoutLink = $myConfig->getShopSecureHomeUrl();
00664         if ( $myConfig->isSsl() ) {
00665             $sLogoutLink = $myConfig->getShopHomeUrl();
00666         }
00667         $sLogoutLink .= 'cl='.oxConfig::getParameter('cl').$this->getParent()->getDynUrlParams();
00668         if ( $sParam = oxConfig::getParameter('anid') ) {
00669             $sLogoutLink .= '&amp;anid='.$sParam;
00670         }
00671         if ( $sParam = oxConfig::getParameter('cnid') ) {
00672             $sLogoutLink .= '&amp;cnid='.$sParam;
00673         }
00674         if ( $sParam = oxConfig::getParameter('mnid') ) {
00675             $sLogoutLink .= '&amp;mnid='.$sParam;
00676         }
00677         if ( $sParam = oxConfig::getParameter('tpl') ) {
00678             $sLogoutLink .= '&amp;tpl='.$sParam;
00679         }
00680         return $sLogoutLink.'&amp;fnc=logout';
00681     }
00682 
00693     protected function _setupDelAddress()
00694     {
00695         return (oxConfig::getParameter( 'blshowshipaddress' ) !== null || oxConfig::getParameter( 'blhideshipaddress' ) !== null) && oxConfig::getParameter( 'userform' ) === null;
00696     }
00697 
00705     public function setLoginStatus( $iStatus )
00706     {
00707         $this->_iLoginStatus = $iStatus;
00708     }
00709 
00718     public function getLoginStatus()
00719     {
00720         return $this->_iLoginStatus;
00721     }
00722 
00728     public function getInvitor()
00729     {
00730         $sSu = oxSession::getVar( 'su' );
00731         if ( !$sSu && ( $sSuNew = oxConfig::getParameter( 'su' ) ) ) {
00732             oxSession::setVar( 'su', $sSuNew );
00733         }
00734     }
00735 
00741     public function setRecipient()
00742     {
00743         $sRe = oxSession::getVar( 're' );
00744         if ( !$sRe && ( $sReNew = oxConfig::getParameter( 're' ) ) ) {
00745             oxSession::setVar( 're', $sReNew );
00746         }
00747     }
00748 }