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