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 
00400     public function changeuser_testvalues()
00401     {
00402         // skip updating user info if this is just form reload
00403         // on selecting delivery address
00404         // We do redirect only on success not to loose errors.
00405 
00406         if ( $this->_changeUser_noRedirect() ) {
00407             return 'account_user';
00408         }
00409     }
00410 
00432     public function createUser()
00433     {
00434         // checking if "open address area" button was clicked
00435         if ( $blSetup = $this->_setupDelAddress() ) {
00436             return;
00437         }
00438 
00439         $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
00440 
00441         $myConfig = $this->getConfig();
00442         if ( $blActiveLogin && !oxConfig::getParameter( 'ord_agb' ) && $myConfig->getConfigParam( 'blConfirmAGB' ) ) {
00443             oxUtilsView::getInstance()->addErrorToDisplay( 'ORDER_READANDCONFIRMTERMS', false, true );
00444             return;
00445         }
00446 
00447         $myUtils  = oxUtils::getInstance();
00448 
00449         // collecting values to check
00450         $sUser = oxConfig::getParameter( 'lgn_usr' );
00451 
00452         // first pass
00453         $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
00454 
00455         // second pass
00456         $sPassword2 = oxConfig::getParameter( 'lgn_pwd2', true );
00457 
00458         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00459         $aDelAdress = $this->_getDelAddressData();
00460 
00461         $oUser = oxNew( 'oxuser' );
00462 
00463         try {
00464 
00465             $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00466 
00467             $iActState = $blActiveLogin ? 0 : 1;
00468 
00469             // setting values
00470             $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
00471             $oUser->setPassword( $sPassword );
00472             $oUser->oxuser__oxactive   = new oxField( $iActState, oxField::T_RAW);
00473 
00474             // used for checking if user email currently subscribed
00475             $iSubscriptionStatus = $oUser->getNewsSubscription()->getOptInStatus();
00476 
00477             $oUser->createUser();
00478             $oUser->load( $oUser->getId() );
00479             $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
00480 
00481             if ( $blActiveLogin ) {
00482                 // accepting terms..
00483                 $oUser->acceptTerms();
00484             }
00485 
00486             $sUserId = oxSession::getVar( "su" );
00487             $sRecEmail = oxSession::getVar( "re" );
00488             if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId && $sRecEmail ) {
00489                 // setting registration credit points..
00490                 $oUser->setCreditPointsForRegistrant( $sUserId, $sRecEmail );
00491             }
00492 
00493             // assigning to newsletter
00494             $blOptin = oxConfig::getParameter( 'blnewssubscribed' );
00495             if ( $blOptin && $iSubscriptionStatus == 1 ) {
00496                 // if user was assigned to newsletter and is creating account with newsletter checked, don't require confirm
00497                 $oUser->getNewsSubscription()->setOptInStatus(1);
00498                 $oUser->addToGroup( 'oxidnewsletter' );
00499                 $this->_blNewsSubscriptionStatus = 1;
00500             } else {
00501                 $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00502             }
00503 
00504             $oUser->addToGroup( 'oxidnotyetordered' );
00505             $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
00506             $oUser->logout();
00507 
00508         } catch ( oxUserException $oEx ) {
00509             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00510             return false;
00511         } catch( oxInputException $oEx ){
00512             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00513             return false;
00514         } catch( oxConnectionException $oEx ){
00515             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00516             return false;
00517         }
00518 
00519         if ( !$blActiveLogin ) {
00520             if ( !$sPassword ) {
00521                 oxSession::setVar( 'usr', $oUser->getId() );
00522                 $this->_afterLogin( $oUser );
00523             } elseif ( $this->login() == 'user' ) {
00524                 return false;
00525             }
00526 
00527             // order remark
00528             //V #427: order remark for new users
00529             $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00530             if ( $sOrderRemark ) {
00531                 oxSession::setVar( 'ordrem', $sOrderRemark );
00532             }
00533         }
00534 
00535         // send register eMail
00536         //TODO: move into user
00537         if ( (int) oxConfig::getParameter( 'option' ) == 3 ) {
00538             $oxEMail = oxNew( 'oxemail' );
00539             if ( $blActiveLogin ) {
00540                 $oxEMail->sendRegisterConfirmEmail( $oUser );
00541             } else {
00542                 $oxEMail->sendRegisterEmail( $oUser );
00543             }
00544         }
00545 
00546         // new registered
00547         $this->_blIsNewUser = true;
00548 
00549         return 'payment';
00550     }
00551 
00557     public function registerUser()
00558     {
00559         // checking if "open address area" button was clicked
00560         if ( $blSetup = $this->_setupDelAddress() ) {
00561             return;
00562         }
00563 
00564         // registered new user ?
00565         if ( $this->createuser()!= false && $this->_blIsNewUser ) {
00566             if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
00567                 return 'register?success=1';
00568             } else {
00569                 return 'register?success=1&newslettererror=4';
00570             }
00571         } else {
00572             // problems with registration ...
00573             $this->logout();
00574         }
00575     }
00576 
00590     protected function _changeUser_noRedirect( )
00591     {
00592         if (!$this->getSession()->checkSessionChallenge()) {
00593             return;
00594         }
00595 
00596         // no user ?
00597         $oUser = $this->getUser();
00598         if ( !$oUser ) {
00599             return;
00600         }
00601 
00602         // collecting values to check
00603         $aDelAdress = $this->_getDelAddressData();
00604 
00605         // if user company name, user name and additional info has special chars
00606         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00607 
00608         $sUserName  = $oUser->oxuser__oxusername->value;
00609         $sPassword  = $sPassword2 = $oUser->oxuser__oxpassword->value;
00610 
00611         try { // testing user input
00612             $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00613             // assigning to newsletter
00614             if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
00615                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00616             }
00617             // check if email address changed, if so, force check news subscription settings.
00618             $blForceCheckOptIn = ( $aInvAdress['oxuser__oxusername'] !== null && $aInvAdress['oxuser__oxusername'] !== $sUserName );
00619             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ), $blForceCheckOptIn );
00620 
00621         } catch ( oxUserException $oEx ) { // errors in input
00622             // marking error code
00623             //TODO
00624             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00625             return;
00626         } catch(oxInputException $oEx) {
00627             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00628             return;
00629         } catch(oxConnectionException $oEx){
00630              //connection to external resource broken, change message and pass to the view
00631             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00632             return;
00633         }
00634 
00635 
00636         // order remark
00637         $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00638 
00639         if ( $sOrderRemark ) {
00640             oxSession::setVar( 'ordrem', $sOrderRemark );
00641         } else {
00642             oxSession::deleteVar( 'ordrem' );
00643         }
00644 
00645         if ( $oBasket = $this->getSession()->getBasket() ) {
00646             $oBasket->onUpdate();
00647         }
00648         return true;
00649     }
00650 
00657     protected function _getDelAddressData()
00658     {
00659         // if user company name, user name and additional info has special chars
00660         $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', true ) : array();
00661 
00662         if ( is_array( $aDeladr ) ) {
00663             // checking if data is filled
00664             if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
00665                 unset( $aDeladr['oxaddress__oxsal'] );
00666             }
00667             if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
00668                 // resetting to avoid empty records
00669                 $aDelAdress = array();
00670             }
00671         }
00672         return $aDelAdress;
00673     }
00674 
00680     protected function _getLogoutLink()
00681     {
00682         $myConfig = $this->getConfig();
00683         $sLogoutLink = $myConfig->getShopHomeUrl();
00684         if ( $myConfig->isSsl() ) {
00685             $sLogoutLink = $myConfig->getShopSecureHomeUrl();
00686         }
00687         $sLogoutLink .= 'cl='.oxConfig::getParameter('cl').$this->getParent()->getDynUrlParams();
00688         if ( $sParam = oxConfig::getParameter('anid') ) {
00689             $sLogoutLink .= '&amp;anid='.$sParam;
00690         }
00691         if ( $sParam = oxConfig::getParameter('cnid') ) {
00692             $sLogoutLink .= '&amp;cnid='.$sParam;
00693         }
00694         if ( $sParam = oxConfig::getParameter('mnid') ) {
00695             $sLogoutLink .= '&amp;mnid='.$sParam;
00696         }
00697         if ( $sParam = oxConfig::getParameter('tpl') ) {
00698             $sLogoutLink .= '&amp;tpl='.$sParam;
00699         }
00700         if ( $sParam = oxConfig::getParameter('recommid') ) {
00701             $sLogoutLink .= '&amp;recommid='.$sParam;
00702         }
00703         return $sLogoutLink.'&amp;fnc=logout';
00704     }
00705 
00716     protected function _setupDelAddress()
00717     {
00718         return (oxConfig::getParameter( 'blshowshipaddress' ) !== null || oxConfig::getParameter( 'blhideshipaddress' ) !== null) && oxConfig::getParameter( 'userform' ) === null;
00719     }
00720 
00728     public function setLoginStatus( $iStatus )
00729     {
00730         $this->_iLoginStatus = $iStatus;
00731     }
00732 
00741     public function getLoginStatus()
00742     {
00743         return $this->_iLoginStatus;
00744     }
00745 
00751     public function getInvitor()
00752     {
00753         $sSu = oxSession::getVar( 'su' );
00754         if ( !$sSu && ( $sSuNew = oxConfig::getParameter( 'su' ) ) ) {
00755             oxSession::setVar( 'su', $sSuNew );
00756         }
00757     }
00758 
00764     public function setRecipient()
00765     {
00766         $sRe = oxSession::getVar( 're' );
00767         if ( !$sRe && ( $sReNew = oxConfig::getParameter( 're' ) ) ) {
00768             oxSession::setVar( 're', $sReNew );
00769         }
00770     }
00771 }