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, '', 'oxwservicemenu' );
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 
00360                 oxRegistry::getUtils()->redirect( $this->_getLogoutLink());
00361             }
00362         }
00363     }
00364 
00374     public function changeUser( )
00375     {
00376         $blUserRegistered = $this->_changeUser_noRedirect();
00377 
00378         if ( $blUserRegistered === true ) {
00379             return 'payment';
00380         } else {
00381             return $blUserRegistered;
00382         }
00383     }
00384 
00391     public function changeuser_testvalues()
00392     {
00393         // skip updating user info if this is just form reload
00394         // on selecting delivery address
00395 
00396         $this->_changeUser_noRedirect();
00397         return 'account_user';
00398     }
00399 
00421     public function createUser()
00422     {
00423         $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
00424 
00425         $myConfig = $this->getConfig();
00426         if ( $blActiveLogin && !oxConfig::getParameter( 'ord_agb' ) && $myConfig->getConfigParam( 'blConfirmAGB' ) ) {
00427             oxRegistry::get("oxUtilsView")->addErrorToDisplay( 'ORDER_READANDCONFIRMTERMS', false, true );
00428             return;
00429         }
00430 
00431         $myUtils  = oxRegistry::getUtils();
00432 
00433         // collecting values to check
00434         $sUser = oxConfig::getParameter( 'lgn_usr' );
00435 
00436         // first pass
00437         $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
00438 
00439         // second pass
00440         $sPassword2 = oxConfig::getParameter( 'lgn_pwd2', true );
00441 
00442         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00443         $aDelAdress = $this->_getDelAddressData();
00444 
00445         $oUser = oxNew( 'oxuser' );
00446 
00447         try {
00448 
00449             $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00450 
00451             $iActState = $blActiveLogin ? 0 : 1;
00452 
00453             // setting values
00454             $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
00455             $oUser->setPassword( $sPassword );
00456             $oUser->oxuser__oxactive   = new oxField( $iActState, oxField::T_RAW);
00457 
00458             // used for checking if user email currently subscribed
00459             $iSubscriptionStatus = $oUser->getNewsSubscription()->getOptInStatus();
00460 
00461             $oUser->createUser();
00462             $oUser->load( $oUser->getId() );
00463             $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
00464 
00465             if ( $blActiveLogin ) {
00466                 // accepting terms..
00467                 $oUser->acceptTerms();
00468             }
00469 
00470             $sUserId = oxSession::getVar( "su" );
00471             $sRecEmail = oxSession::getVar( "re" );
00472             if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId && $sRecEmail ) {
00473                 // setting registration credit points..
00474                 $oUser->setCreditPointsForRegistrant( $sUserId, $sRecEmail );
00475             }
00476 
00477             // assigning to newsletter
00478             $blOptin = oxRegistry::getConfig()->getRequestParameter( 'blnewssubscribed' );
00479             if ( $blOptin && $iSubscriptionStatus == 1 ) {
00480                 // if user was assigned to newsletter and is creating account with newsletter checked, don't require confirm
00481                 $oUser->getNewsSubscription()->setOptInStatus(1);
00482                 $oUser->addToGroup( 'oxidnewsletter' );
00483                 $this->_blNewsSubscriptionStatus = 1;
00484             } else {
00485                 $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00486             }
00487 
00488             $oUser->addToGroup( 'oxidnotyetordered' );
00489             $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
00490             $oUser->logout();
00491 
00492         } catch ( oxUserException $oEx ) {
00493             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00494             return false;
00495         } catch( oxInputException $oEx ){
00496             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00497             return false;
00498         } catch( oxConnectionException $oEx ){
00499             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00500             return false;
00501         }
00502 
00503         if ( !$blActiveLogin ) {
00504             if ( !$sPassword ) {
00505                 oxSession::setVar( 'usr', $oUser->getId() );
00506                 $this->_afterLogin( $oUser );
00507             } elseif ( $this->login() == 'user' ) {
00508                 return false;
00509             }
00510 
00511             // order remark
00512             //V #427: order remark for new users
00513             $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00514             if ( $sOrderRemark ) {
00515                 oxSession::setVar( 'ordrem', $sOrderRemark );
00516             }
00517         }
00518 
00519         // send register eMail
00520         //TODO: move into user
00521         if ( (int) oxConfig::getParameter( 'option' ) == 3 ) {
00522             $oxEMail = oxNew( 'oxemail' );
00523             if ( $blActiveLogin ) {
00524                 $oxEMail->sendRegisterConfirmEmail( $oUser );
00525             } else {
00526                 $oxEMail->sendRegisterEmail( $oUser );
00527             }
00528         }
00529 
00530         // new registered
00531         $this->_blIsNewUser = true;
00532 
00533         return 'payment';
00534     }
00535 
00541     public function registerUser()
00542     {
00543         // registered new user ?
00544         if ( $this->createuser()!= false && $this->_blIsNewUser ) {
00545             if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
00546                 return 'register?success=1';
00547             } else {
00548                 return 'register?success=1&newslettererror=4';
00549             }
00550         } else {
00551             // problems with registration ...
00552             $this->logout();
00553         }
00554     }
00555 
00569     protected function _changeUser_noRedirect( )
00570     {
00571         if (!$this->getSession()->checkSessionChallenge()) {
00572             return;
00573         }
00574 
00575         // no user ?
00576         $oUser = $this->getUser();
00577         if ( !$oUser ) {
00578             return;
00579         }
00580 
00581         // collecting values to check
00582         $aDelAdress = $this->_getDelAddressData();
00583 
00584         // if user company name, user name and additional info has special chars
00585         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00586 
00587         $sUserName  = $oUser->oxuser__oxusername->value;
00588         $sPassword  = $sPassword2 = $oUser->oxuser__oxpassword->value;
00589 
00590         try { // testing user input
00591             $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00592             // assigning to newsletter
00593             if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
00594                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00595             }
00596             // check if email address changed, if so, force check news subscription settings.
00597             $blForceCheckOptIn = ( $aInvAdress['oxuser__oxusername'] !== $sUserName );
00598             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ), $blForceCheckOptIn );
00599 
00600         } catch ( oxUserException $oEx ) { // errors in input
00601             // marking error code
00602             //TODO
00603             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00604             return;
00605         } catch(oxInputException $oEx) {
00606             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00607             return;
00608         } catch(oxConnectionException $oEx){
00609              //connection to external resource broken, change message and pass to the view
00610             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00611             return;
00612         }
00613 
00614 
00615         // order remark
00616         $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00617 
00618         if ( $sOrderRemark ) {
00619             oxSession::setVar( 'ordrem', $sOrderRemark );
00620         } else {
00621             oxSession::deleteVar( 'ordrem' );
00622         }
00623 
00624         if ( $oBasket = $this->getSession()->getBasket() ) {
00625             $oBasket->onUpdate();
00626         }
00627         return true;
00628     }
00629 
00636     protected function _getDelAddressData()
00637     {
00638         // if user company name, user name and additional info has special chars
00639         $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', true ) : array();
00640 
00641         if ( is_array( $aDeladr ) ) {
00642             // checking if data is filled
00643             if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
00644                 unset( $aDeladr['oxaddress__oxsal'] );
00645             }
00646             if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
00647                 // resetting to avoid empty records
00648                 $aDelAdress = array();
00649             }
00650         }
00651         return $aDelAdress;
00652     }
00653 
00659     protected function _getLogoutLink()
00660     {
00661         $myConfig = $this->getConfig();
00662         $sLogoutLink = $myConfig->getShopSecureHomeUrl();
00663         if ( $myConfig->isSsl() ) {
00664             $sLogoutLink = $myConfig->getShopHomeUrl();
00665         }
00666         $sLogoutLink .= 'cl='.oxConfig::getParameter('cl').$this->getParent()->getDynUrlParams();
00667         if ( $sParam = oxConfig::getParameter('anid') ) {
00668             $sLogoutLink .= '&amp;anid='.$sParam;
00669         }
00670         if ( $sParam = oxConfig::getParameter('cnid') ) {
00671             $sLogoutLink .= '&amp;cnid='.$sParam;
00672         }
00673         if ( $sParam = oxConfig::getParameter('mnid') ) {
00674             $sLogoutLink .= '&amp;mnid='.$sParam;
00675         }
00676         if ( $sParam = oxConfig::getParameter('tpl') ) {
00677             $sLogoutLink .= '&amp;tpl='.$sParam;
00678         }
00679         if ( $sParam = oxConfig::getParameter('oxloadid') ) {
00680             $sLogoutLink .= '&amp;oxloadid='.$sParam;
00681         }
00682         if ( $sParam = oxConfig::getParameter('recommid') ) {
00683             $sLogoutLink .= '&amp;recommid='.$sParam;
00684         }
00685         return $sLogoutLink.'&amp;fnc=logout';
00686     }
00687 
00695     public function setLoginStatus( $iStatus )
00696     {
00697         $this->_iLoginStatus = $iStatus;
00698     }
00699 
00708     public function getLoginStatus()
00709     {
00710         return $this->_iLoginStatus;
00711     }
00712 
00718     public function getInvitor()
00719     {
00720         $sSu = oxSession::getVar( 'su' );
00721         if ( !$sSu && ( $sSuNew = oxConfig::getParameter( 'su' ) ) ) {
00722             oxSession::setVar( 'su', $sSuNew );
00723         }
00724     }
00725 
00731     public function setRecipient()
00732     {
00733         $sRe = oxSession::getVar( 're' );
00734         if ( !$sRe && ( $sReNew = oxConfig::getParameter( 're' ) ) ) {
00735             oxSession::setVar( 're', $sReNew );
00736         }
00737     }
00738 }