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() && !$myConfig->getConfigParam( 'blPerfNoBasketSaving' )) {
00170 
00171             // #1678 R
00172             // @deprecated since 2012-03-14 in version 4.5.8. This parameter will not be used anymore
00173             if ( !$myConfig->getConfigParam( 'blPerfNoBasketSaving' ) ) {
00174                 $myConfig->setGlobalParameter( 'blUserChanged', 1 );
00175             }
00176             // @end deprecated
00177 
00178             if ( $oBasket = $this->getSession()->getBasket() ) {
00179                 $oBasket->load();
00180                 $oBasket->onUpdate();
00181             }
00182         }
00183     }
00184 
00198     public function login()
00199     {
00200         $sUser     = oxConfig::getParameter( 'lgn_usr' );
00201         $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
00202         $sCookie   = oxConfig::getParameter( 'lgn_cook' );
00203         //$blFbLogin = oxConfig::getParameter( 'fblogin' );
00204 
00205         $this->setLoginStatus( USER_LOGIN_FAIL );
00206 
00207         // trying to login user
00208         try {
00209             $oUser = oxNew( 'oxuser' );
00210             $oUser->login( $sUser, $sPassword, $sCookie );
00211             $this->setLoginStatus( USER_LOGIN_SUCCESS );
00212         } catch ( oxUserException $oEx ) {
00213             // for login component send excpetion text to a custom component (if defined)
00214             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00215             return 'user';
00216         } catch( oxCookieException $oEx ){
00217             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00218             return 'user';
00219         }
00220         // finalizing ..
00221         return $this->_afterLogin( $oUser );
00222     }
00223 
00241     protected function _afterLogin( $oUser )
00242     {
00243         $oSession = $this->getSession();
00244 
00245         // generating new session id after login
00246         if ( $this->getLoginStatus() === USER_LOGIN_SUCCESS ) {
00247             $oSession->regenerateSessionId();
00248         }
00249 
00250         $myConfig = $this->getConfig();
00251 
00252         // this user is blocked, deny him
00253         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00254             oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL().'cl=content&tpl=user_blocked.tpl', true, 302 );
00255         }
00256 
00257         // adding to dyn group
00258         $oUser->addDynGroup(oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ));
00259 
00260         // recalc basket
00261         if ( $oBasket = $oSession->getBasket() ) {
00262             $oBasket->onUpdate();
00263         }
00264 
00265         // #1678 R
00266         // @deprecated since 2012-03-14 in version 4.5.8. This parameter will not be used anymore
00267         if ( !$myConfig->getConfigParam( 'blPerfNoBasketSaving' ) ) {
00268             $myConfig->setGlobalParameter( 'blUserChanged', 1);
00269         }
00270         // @end deprecated
00271 
00272 
00273         return 'payment';
00274     }
00275 
00282     public function login_noredirect()
00283     {
00284         $blAgb = oxConfig::getParameter( 'ord_agb' );
00285         $oConfig = $this->getConfig();
00286         if ( $this->getParent()->isEnabledPrivateSales() && $blAgb !== null && ( $oUser = $this->getUser() ) ) {
00287             if ( $blAgb ) {
00288                 $oUser->acceptTerms();
00289             }
00290         } else {
00291             $this->login();
00292         }
00293     }
00294 
00301     public function login_updateFbId()
00302     {
00303         $this->login();
00304 
00305         if ( $oUser = $this->getUser() ) {
00306             //updating user Facebook ID
00307             if ( $oUser->updateFbId() ) {
00308                 oxSession::setVar( '_blFbUserIdUpdated', true );
00309             }
00310         }
00311     }
00312 
00321     protected function _afterLogout()
00322     {
00323         oxSession::deleteVar( 'paymentid' );
00324         oxSession::deleteVar( 'sShipSet' );
00325         oxSession::deleteVar( 'deladrid' );
00326         oxSession::deleteVar( 'dynvalue' );
00327 
00328         // resetting & recalc basket
00329         if ( ( $oBasket = $this->getSession()->getBasket() ) ) {
00330             $oBasket->resetUserInfo();
00331             $oBasket->onUpdate();
00332         }
00333     }
00334 
00343     public function logout()
00344     {
00345         $myConfig  = $this->getConfig();
00346         $oUser = oxNew( 'oxuser' );
00347 
00348         if ( $oUser->logout() ) {
00349 
00350             $this->setLoginStatus( USER_LOGOUT );
00351 
00352             // finalizing ..
00353             $this->_afterLogout();
00354 
00355 
00356             if ( $this->getParent()->isEnabledPrivateSales() ) {
00357                 return 'account';
00358             }
00359 
00360             // redirecting if user logs out in SSL mode
00361             if ( oxConfig::getParameter('redirect') && $myConfig->getConfigParam( 'sSSLShopURL' ) ) {
00362 
00363                 oxUtils::getInstance()->redirect( $this->_getLogoutLink());
00364             }
00365         }
00366     }
00367 
00377     public function changeUser( )
00378     {
00379         // checking if "open address area" button was clicked
00380         // or reloading form when delivery address was selected
00381         if ( $this->_setupDelAddress() ) {
00382             return;
00383         }
00384 
00385         $blUserRegistered = $this->_changeUser_noRedirect();
00386 
00387         if ( $blUserRegistered === true ) {
00388             return 'payment';
00389         } else {
00390             return $blUserRegistered;
00391         }
00392     }
00393 
00399     public function changeuser_testvalues()
00400     {
00401         // skip updating user info if this is just form reload
00402         // on selecting delivery address
00403 
00404         $this->_changeUser_noRedirect();
00405     }
00406 
00428     public function createUser()
00429     {
00430         // checking if "open address area" button was clicked
00431         if ( $blSetup = $this->_setupDelAddress() ) {
00432             return;
00433         }
00434 
00435         $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
00436 
00437         $myConfig = $this->getConfig();
00438         if ( $blActiveLogin && !oxConfig::getParameter( 'ord_agb' ) && $myConfig->getConfigParam( 'blConfirmAGB' ) ) {
00439             oxUtilsView::getInstance()->addErrorToDisplay( 'ORDER_READANDCONFIRMTERMS', false, true );
00440             return;
00441         }
00442 
00443         $myUtils  = oxUtils::getInstance();
00444 
00445         // collecting values to check
00446         $sUser = oxConfig::getParameter( 'lgn_usr' );
00447 
00448         // first pass
00449         $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
00450 
00451         // second pass
00452         $sPassword2 = oxConfig::getParameter( 'lgn_pwd2', true );
00453 
00454         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00455         $aDelAdress = $this->_getDelAddressData();
00456 
00457         $oUser = oxNew( 'oxuser' );
00458 
00459         try {
00460 
00461             $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00462 
00463             $iActState = $blActiveLogin ? 0 : 1;
00464 
00465             // setting values
00466             $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
00467             $oUser->setPassword( $sPassword );
00468             $oUser->oxuser__oxactive   = new oxField( $iActState, oxField::T_RAW);
00469 
00470             $oUser->createUser();
00471             $oUser->load( $oUser->getId() );
00472             $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
00473 
00474             if ( $blActiveLogin ) {
00475                 // accepting terms..
00476                 $oUser->acceptTerms();
00477             }
00478 
00479             $sUserId = oxSession::getVar( "su" );
00480             $sRecEmail = oxSession::getVar( "re" );
00481             if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId && $sRecEmail ) {
00482                 // setting registration credit points..
00483                 $oUser->setCreditPointsForRegistrant( $sUserId, $sRecEmail );
00484             }
00485 
00486             // assigning to newsletter
00487             $blOptin = oxConfig::getParameter( 'blnewssubscribed' );
00488             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00489 
00490             $oUser->addToGroup( 'oxidnotyetordered' );
00491             $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
00492             $oUser->logout();
00493 
00494         } catch ( oxUserException $oEx ) {
00495             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00496             return false;
00497         } catch( oxInputException $oEx ){
00498             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00499             return false;
00500         } catch( oxConnectionException $oEx ){
00501             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00502             return false;
00503         }
00504 
00505         if ( !$blActiveLogin ) {
00506             if ( !$sPassword ) {
00507                 oxSession::setVar( 'usr', $oUser->getId() );
00508                 $this->_afterLogin( $oUser );
00509             } elseif ( $this->login() == 'user' ) {
00510                 return false;
00511             }
00512 
00513             // order remark
00514             //V #427: order remark for new users
00515             $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00516             if ( $sOrderRemark ) {
00517                 oxSession::setVar( 'ordrem', $sOrderRemark );
00518             }
00519         }
00520 
00521         // send register eMail
00522         //TODO: move into user
00523         if ( (int) oxConfig::getParameter( 'option' ) == 3 ) {
00524             $oxEMail = oxNew( 'oxemail' );
00525             if ( $blActiveLogin ) {
00526                 $oxEMail->sendRegisterConfirmEmail( $oUser );
00527             } else {
00528                 $oxEMail->sendRegisterEmail( $oUser );
00529             }
00530         }
00531 
00532         // new registered
00533         $this->_blIsNewUser = true;
00534 
00535         return 'payment';
00536     }
00537 
00543     public function registerUser()
00544     {
00545         // checking if "open address area" button was clicked
00546         if ( $blSetup = $this->_setupDelAddress() ) {
00547             return;
00548         }
00549 
00550         // registered new user ?
00551         if ( $this->createuser()!= false && $this->_blIsNewUser ) {
00552             if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
00553                 return 'register?success=1';
00554             } else {
00555                 return 'register?success=1&newslettererror=4';
00556             }
00557         } else {
00558             // problems with registration ...
00559             $this->logout();
00560         }
00561     }
00562 
00576     protected function _changeUser_noRedirect( )
00577     {
00578         if (!$this->getSession()->checkSessionChallenge()) {
00579             return;
00580         }
00581 
00582         // no user ?
00583         $oUser = $this->getUser();
00584         if ( !$oUser ) {
00585             return;
00586         }
00587 
00588         // collecting values to check
00589         $aDelAdress = $this->_getDelAddressData();
00590 
00591         // if user company name, user name and additional info has special chars
00592         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00593 
00594         $sUserName  = $oUser->oxuser__oxusername->value;
00595         $sPassword  = $sPassword2 = $oUser->oxuser__oxpassword->value;
00596 
00597         try { // testing user input
00598             $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00599             // assigning to newsletter
00600             if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
00601                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00602             }
00603             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00604 
00605         } catch ( oxUserException $oEx ) { // errors in input
00606             // marking error code
00607             //TODO
00608             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00609             return;
00610         } catch(oxInputException $oEx) {
00611             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00612             return;
00613         } catch(oxConnectionException $oEx){
00614              //connection to external resource broken, change message and pass to the view
00615             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00616             return;
00617         }
00618 
00619 
00620         // order remark
00621         $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00622 
00623         if ( $sOrderRemark ) {
00624             oxSession::setVar( 'ordrem', $sOrderRemark );
00625         } else {
00626             oxSession::deleteVar( 'ordrem' );
00627         }
00628 
00629         if ( $oBasket = $this->getSession()->getBasket() ) {
00630             $oBasket->onUpdate();
00631         }
00632         return true;
00633     }
00634 
00641     protected function _getDelAddressData()
00642     {
00643         // if user company name, user name and additional info has special chars
00644         $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', true ) : array();
00645 
00646         if ( is_array( $aDeladr ) ) {
00647             // checking if data is filled
00648             if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
00649                 unset( $aDeladr['oxaddress__oxsal'] );
00650             }
00651             if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
00652                 // resetting to avoid empty records
00653                 $aDelAdress = array();
00654             }
00655         }
00656         return $aDelAdress;
00657     }
00658 
00664     protected function _getLogoutLink()
00665     {
00666         $myConfig = $this->getConfig();
00667         $sLogoutLink = $myConfig->getShopSecureHomeUrl();
00668         if ( $myConfig->isSsl() ) {
00669             $sLogoutLink = $myConfig->getShopHomeUrl();
00670         }
00671         $sLogoutLink .= 'cl='.oxConfig::getParameter('cl').$this->getParent()->getDynUrlParams();
00672         if ( $sParam = oxConfig::getParameter('anid') ) {
00673             $sLogoutLink .= '&amp;anid='.$sParam;
00674         }
00675         if ( $sParam = oxConfig::getParameter('cnid') ) {
00676             $sLogoutLink .= '&amp;cnid='.$sParam;
00677         }
00678         if ( $sParam = oxConfig::getParameter('mnid') ) {
00679             $sLogoutLink .= '&amp;mnid='.$sParam;
00680         }
00681         if ( $sParam = oxConfig::getParameter('tpl') ) {
00682             $sLogoutLink .= '&amp;tpl='.$sParam;
00683         }
00684         return $sLogoutLink.'&amp;fnc=logout';
00685     }
00686 
00697     protected function _setupDelAddress()
00698     {
00699         return (oxConfig::getParameter( 'blshowshipaddress' ) !== null || oxConfig::getParameter( 'blhideshipaddress' ) !== null) && oxConfig::getParameter( 'userform' ) === null;
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 }