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 );
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 
00390     public function changeuser_testvalues()
00391     {
00392         // skip updating user info if this is just form reload
00393         // on selecting delivery address
00394 
00395         $this->_changeUser_noRedirect();
00396     }
00397 
00419     public function createUser()
00420     {
00421         $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
00422 
00423         $myConfig = $this->getConfig();
00424         if ( $blActiveLogin && !oxConfig::getParameter( 'ord_agb' ) && $myConfig->getConfigParam( 'blConfirmAGB' ) ) {
00425             oxRegistry::get("oxUtilsView")->addErrorToDisplay( 'ORDER_READANDCONFIRMTERMS', false, true );
00426             return;
00427         }
00428 
00429         $myUtils  = oxRegistry::getUtils();
00430 
00431         // collecting values to check
00432         $sUser = oxConfig::getParameter( 'lgn_usr' );
00433 
00434         // first pass
00435         $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
00436 
00437         // second pass
00438         $sPassword2 = oxConfig::getParameter( 'lgn_pwd2', true );
00439 
00440         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00441         $aDelAdress = $this->_getDelAddressData();
00442 
00443         $oUser = oxNew( 'oxuser' );
00444 
00445         try {
00446 
00447             $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00448 
00449             $iActState = $blActiveLogin ? 0 : 1;
00450 
00451             // setting values
00452             $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
00453             $oUser->setPassword( $sPassword );
00454             $oUser->oxuser__oxactive   = new oxField( $iActState, oxField::T_RAW);
00455 
00456             $oUser->createUser();
00457             $oUser->load( $oUser->getId() );
00458             $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
00459 
00460             if ( $blActiveLogin ) {
00461                 // accepting terms..
00462                 $oUser->acceptTerms();
00463             }
00464 
00465             $sUserId = oxSession::getVar( "su" );
00466             $sRecEmail = oxSession::getVar( "re" );
00467             if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId && $sRecEmail ) {
00468                 // setting registration credit points..
00469                 $oUser->setCreditPointsForRegistrant( $sUserId, $sRecEmail );
00470             }
00471 
00472             // assigning to newsletter
00473             $blOptin = oxConfig::getParameter( 'blnewssubscribed' );
00474             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00475 
00476             $oUser->addToGroup( 'oxidnotyetordered' );
00477             $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
00478             $oUser->logout();
00479 
00480         } catch ( oxUserException $oEx ) {
00481             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00482             return false;
00483         } catch( oxInputException $oEx ){
00484             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00485             return false;
00486         } catch( oxConnectionException $oEx ){
00487             oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
00488             return false;
00489         }
00490 
00491         if ( !$blActiveLogin ) {
00492             if ( !$sPassword ) {
00493                 oxSession::setVar( 'usr', $oUser->getId() );
00494                 $this->_afterLogin( $oUser );
00495             } elseif ( $this->login() == 'user' ) {
00496                 return false;
00497             }
00498 
00499             // order remark
00500             //V #427: order remark for new users
00501             $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00502             if ( $sOrderRemark ) {
00503                 oxSession::setVar( 'ordrem', $sOrderRemark );
00504             }
00505         }
00506 
00507         // send register eMail
00508         //TODO: move into user
00509         if ( (int) oxConfig::getParameter( 'option' ) == 3 ) {
00510             $oxEMail = oxNew( 'oxemail' );
00511             if ( $blActiveLogin ) {
00512                 $oxEMail->sendRegisterConfirmEmail( $oUser );
00513             } else {
00514                 $oxEMail->sendRegisterEmail( $oUser );
00515             }
00516         }
00517 
00518         // new registered
00519         $this->_blIsNewUser = true;
00520 
00521         return 'payment';
00522     }
00523 
00529     public function registerUser()
00530     {
00531         // registered new user ?
00532         if ( $this->createuser()!= false && $this->_blIsNewUser ) {
00533             if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
00534                 return 'register?success=1';
00535             } else {
00536                 return 'register?success=1&newslettererror=4';
00537             }
00538         } else {
00539             // problems with registration ...
00540             $this->logout();
00541         }
00542     }
00543 
00557     protected function _changeUser_noRedirect( )
00558     {
00559         if (!$this->getSession()->checkSessionChallenge()) {
00560             return;
00561         }
00562 
00563         // no user ?
00564         $oUser = $this->getUser();
00565         if ( !$oUser ) {
00566             return;
00567         }
00568 
00569         // collecting values to check
00570         $aDelAdress = $this->_getDelAddressData();
00571 
00572         // if user company name, user name and additional info has special chars
00573         $aInvAdress = oxConfig::getParameter( 'invadr', true );
00574 
00575         $sUserName  = $oUser->oxuser__oxusername->value;
00576         $sPassword  = $sPassword2 = $oUser->oxuser__oxpassword->value;
00577 
00578         try { // testing user input
00579             $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00580             // assigning to newsletter
00581             if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
00582                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00583             }
00584             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00585 
00586         } catch ( oxUserException $oEx ) { // errors in input
00587             // marking error code
00588             //TODO
00589             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00590             return;
00591         } catch(oxInputException $oEx) {
00592             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00593             return;
00594         } catch(oxConnectionException $oEx){
00595              //connection to external resource broken, change message and pass to the view
00596             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00597             return;
00598         }
00599 
00600 
00601         // order remark
00602         $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00603 
00604         if ( $sOrderRemark ) {
00605             oxSession::setVar( 'ordrem', $sOrderRemark );
00606         } else {
00607             oxSession::deleteVar( 'ordrem' );
00608         }
00609 
00610         if ( $oBasket = $this->getSession()->getBasket() ) {
00611             $oBasket->onUpdate();
00612         }
00613         return true;
00614     }
00615 
00622     protected function _getDelAddressData()
00623     {
00624         // if user company name, user name and additional info has special chars
00625         $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', true ) : array();
00626 
00627         if ( is_array( $aDeladr ) ) {
00628             // checking if data is filled
00629             if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
00630                 unset( $aDeladr['oxaddress__oxsal'] );
00631             }
00632             if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
00633                 // resetting to avoid empty records
00634                 $aDelAdress = array();
00635             }
00636         }
00637         return $aDelAdress;
00638     }
00639 
00645     protected function _getLogoutLink()
00646     {
00647         $myConfig = $this->getConfig();
00648         $sLogoutLink = $myConfig->getShopSecureHomeUrl();
00649         if ( $myConfig->isSsl() ) {
00650             $sLogoutLink = $myConfig->getShopHomeUrl();
00651         }
00652         $sLogoutLink .= 'cl='.oxConfig::getParameter('cl').$this->getParent()->getDynUrlParams();
00653         if ( $sParam = oxConfig::getParameter('anid') ) {
00654             $sLogoutLink .= '&amp;anid='.$sParam;
00655         }
00656         if ( $sParam = oxConfig::getParameter('cnid') ) {
00657             $sLogoutLink .= '&amp;cnid='.$sParam;
00658         }
00659         if ( $sParam = oxConfig::getParameter('mnid') ) {
00660             $sLogoutLink .= '&amp;mnid='.$sParam;
00661         }
00662         if ( $sParam = oxConfig::getParameter('tpl') ) {
00663             $sLogoutLink .= '&amp;tpl='.$sParam;
00664         }
00665         if ( $sParam = oxConfig::getParameter('oxloadid') ) {
00666             $sLogoutLink .= '&amp;oxloadid='.$sParam;
00667         }
00668         return $sLogoutLink.'&amp;fnc=logout';
00669     }
00670 
00678     public function setLoginStatus( $iStatus )
00679     {
00680         $this->_iLoginStatus = $iStatus;
00681     }
00682 
00691     public function getLoginStatus()
00692     {
00693         return $this->_iLoginStatus;
00694     }
00695 
00701     public function getInvitor()
00702     {
00703         $sSu = oxSession::getVar( 'su' );
00704         if ( !$sSu && ( $sSuNew = oxConfig::getParameter( 'su' ) ) ) {
00705             oxSession::setVar( 'su', $sSuNew );
00706         }
00707     }
00708 
00714     public function setRecipient()
00715     {
00716         $sRe = oxSession::getVar( 're' );
00717         if ( !$sRe && ( $sReNew = oxConfig::getParameter( 're' ) ) ) {
00718             oxSession::setVar( 're', $sReNew );
00719         }
00720     }
00721 }