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 
00014 class oxcmp_user extends oxView
00015 {
00016 
00022     protected $_blIsNewUser = false;
00023 
00029     protected $_blIsComponent = true;
00030 
00036     protected $_blNewsSubscriptionStatus = null;
00037 
00046     protected $_iLoginStatus = null;
00047 
00053     protected $_sTermsVer = null;
00054 
00060     protected $_aAllowedClasses = array(
00061         'register',
00062         'forgotpwd',
00063         'content',
00064         'account',
00065         'clearcookies',
00066         'oxwServiceMenu',
00067     );
00068 
00076     public function init()
00077     {
00078         $this->_saveDeliveryAddressState();
00079         $this->_loadSessionUser();
00080         $this->_saveInvitor();
00081 
00082         parent::init();
00083     }
00084 
00091     public function render()
00092     {
00093         // checks if private sales allows further tasks
00094         $this->_checkPsState();
00095 
00096         parent::render();
00097 
00098         return $this->getUser();
00099     }
00100 
00109     protected function _checkPsState()
00110     {
00111         $oConfig = $this->getConfig();
00112         if ($this->getParent()->isEnabledPrivateSales()) {
00113             // load session user
00114             $oUser = $this->getUser();
00115             $sClass = $this->getParent()->getClassName();
00116 
00117             // no session user
00118             if (!$oUser && !in_array($sClass, $this->_aAllowedClasses)) {
00119                 oxRegistry::getUtils()->redirect($oConfig->getShopHomeURL() . 'cl=account', false, 302);
00120             }
00121 
00122             if ($oUser && !$oUser->isTermsAccepted() && !in_array($sClass, $this->_aAllowedClasses)) {
00123                 oxRegistry::getUtils()->redirect($oConfig->getShopHomeURL() . 'cl=account&term=1', false, 302);
00124             }
00125         }
00126     }
00127 
00133     protected function _loadSessionUser()
00134     {
00135         $myConfig = $this->getConfig();
00136         $oUser = $this->getUser();
00137 
00138         // no session user
00139         if (!$oUser) {
00140             return;
00141         }
00142 
00143         // this user is blocked, deny him
00144         if ($oUser->inGroup('oxidblocked')) {
00145             $sUrl = $myConfig->getShopHomeURL() . 'cl=content&tpl=user_blocked.tpl';
00146             oxRegistry::getUtils()->redirect($sUrl, true, 302);
00147         }
00148 
00149         // TODO: move this to a proper place
00150         if ($oUser->isLoadedFromCookie() && !$myConfig->getConfigParam('blPerfNoBasketSaving')) {
00151 
00152             if ($oBasket = $this->getSession()->getBasket()) {
00153                 $oBasket->load();
00154                 $oBasket->onUpdate();
00155             }
00156         }
00157     }
00158 
00172     public function login()
00173     {
00174         $sUser = oxRegistry::getConfig()->getRequestParameter('lgn_usr');
00175         $sPassword = oxRegistry::getConfig()->getRequestParameter('lgn_pwd', true);
00176         $sCookie = oxRegistry::getConfig()->getRequestParameter('lgn_cook');
00177         //$blFbLogin = oxRegistry::getConfig()->getRequestParameter( 'fblogin' );
00178 
00179         $this->setLoginStatus(USER_LOGIN_FAIL);
00180 
00181         // trying to login user
00182         try {
00183             $oUser = oxNew('oxuser');
00184             $oUser->login($sUser, $sPassword, $sCookie);
00185             $this->setLoginStatus(USER_LOGIN_SUCCESS);
00186         } catch (oxUserException $oEx) {
00187             // for login component send excpetion text to a custom component (if defined)
00188             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, '', false);
00189 
00190             return 'user';
00191         } catch (oxCookieException $oEx) {
00192             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
00193 
00194             return 'user';
00195         }
00196 
00197         // finalizing ..
00198         return $this->_afterLogin($oUser);
00199     }
00200 
00216     protected function _afterLogin($oUser)
00217     {
00218         $oSession = $this->getSession();
00219 
00220         // generating new session id after login
00221         if ($this->getLoginStatus() === USER_LOGIN_SUCCESS) {
00222             $oSession->regenerateSessionId();
00223         }
00224 
00225         $myConfig = $this->getConfig();
00226 
00227         // this user is blocked, deny him
00228         if ($oUser->inGroup('oxidblocked')) {
00229             $sUrl = $myConfig->getShopHomeURL() . 'cl=content&tpl=user_blocked.tpl';
00230             oxRegistry::getUtils()->redirect($sUrl, true, 302);
00231         }
00232 
00233         // recalc basket
00234         if ($oBasket = $oSession->getBasket()) {
00235             $oBasket->onUpdate();
00236         }
00237 
00238 
00239         return 'payment';
00240     }
00241 
00246     public function login_noredirect()
00247     {
00248         $blAgb = oxRegistry::getConfig()->getRequestParameter('ord_agb');
00249         $oConfig = $this->getConfig();
00250         if ($this->getParent()->isEnabledPrivateSales() && $blAgb !== null && ($oUser = $this->getUser())) {
00251             if ($blAgb) {
00252                 $oUser->acceptTerms();
00253             }
00254         } else {
00255             $this->login();
00256 
00257             if (!$this->isAdmin() && !$this->getConfig()->getConfigParam('blPerfNoBasketSaving')) {
00258                 //load basket from the database
00259                 try {
00260                     if ($oBasket = $this->getSession()->getBasket()) {
00261                         $oBasket->load();
00262                     }
00263                 } catch (Exception $oE) {
00264                     //just ignore it
00265                 }
00266             }
00267 
00268 
00269         }
00270     }
00271 
00276     public function login_updateFbId()
00277     {
00278         $this->login();
00279 
00280         if ($oUser = $this->getUser()) {
00281             //updating user Facebook ID
00282             if ($oUser->updateFbId()) {
00283                 oxRegistry::getSession()->setVariable('_blFbUserIdUpdated', true);
00284             }
00285         }
00286     }
00287 
00294     protected function _afterLogout()
00295     {
00296         oxRegistry::getSession()->deleteVariable('paymentid');
00297         oxRegistry::getSession()->deleteVariable('sShipSet');
00298         oxRegistry::getSession()->deleteVariable('deladrid');
00299         oxRegistry::getSession()->deleteVariable('dynvalue');
00300 
00301         // resetting & recalc basket
00302         if (($oBasket = $this->getSession()->getBasket())) {
00303             $oBasket->resetUserInfo();
00304             $oBasket->onUpdate();
00305         }
00306     }
00307 
00316     public function logout()
00317     {
00318         $myConfig = $this->getConfig();
00319         $oUser = oxNew('oxuser');
00320 
00321         if ($oUser->logout()) {
00322 
00323             $this->setLoginStatus(USER_LOGOUT);
00324 
00325             // finalizing ..
00326             $this->_afterLogout();
00327 
00328 
00329             if ($this->getParent()->isEnabledPrivateSales()) {
00330                 return 'account';
00331             }
00332 
00333             // redirecting if user logs out in SSL mode
00334             if (oxRegistry::getConfig()->getRequestParameter('redirect') && $myConfig->getConfigParam('sSSLShopURL')) {
00335                 oxRegistry::getUtils()->redirect($this->_getLogoutLink());
00336             }
00337         }
00338     }
00339 
00349     public function changeUser()
00350     {
00351         $blUserRegistered = $this->_changeUser_noRedirect();
00352 
00353         if ($blUserRegistered === true) {
00354             return 'payment';
00355         } else {
00356             return $blUserRegistered;
00357         }
00358     }
00359 
00366     public function changeuser_testvalues()
00367     {
00368         // skip updating user info if this is just form reload
00369         // on selecting delivery address
00370         // We do redirect only on success not to loose errors.
00371 
00372         if ($this->_changeUser_noRedirect()) {
00373             return 'account_user';
00374         }
00375     }
00376 
00397     public function createUser()
00398     {
00399         $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
00400 
00401         $oConfig = $this->getConfig();
00402 
00403         if ($blActiveLogin && !$oConfig->getRequestParameter('ord_agb') && $oConfig->getConfigParam('blConfirmAGB')) {
00404             oxRegistry::get("oxUtilsView")->addErrorToDisplay('READ_AND_CONFIRM_TERMS', false, true);
00405 
00406             return;
00407         }
00408 
00409         // collecting values to check
00410         $sUser = $oConfig->getRequestParameter('lgn_usr');
00411 
00412         // first pass
00413         $sPassword = $oConfig->getRequestParameter('lgn_pwd', true);
00414 
00415         // second pass
00416         $sPassword2 = $oConfig->getRequestParameter('lgn_pwd2', true);
00417 
00418         $aInvAdress = $oConfig->getRequestParameter('invadr', true);
00419         $aDelAdress = $this->_getDelAddressData();
00420 
00422         $oUser = oxNew('oxuser');
00423 
00424         try {
00425 
00426             $oUser->checkValues($sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress);
00427 
00428             $iActState = $blActiveLogin ? 0 : 1;
00429 
00430             // setting values
00431             $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
00432             $oUser->setPassword($sPassword);
00433             $oUser->oxuser__oxactive = new oxField($iActState, oxField::T_RAW);
00434 
00435             // used for checking if user email currently subscribed
00436             $iSubscriptionStatus = $oUser->getNewsSubscription()->getOptInStatus();
00437 
00438             $oUser->createUser();
00439             $oUser->load($oUser->getId());
00440             $oUser->changeUserData($oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress);
00441 
00442             if ($blActiveLogin) {
00443                 // accepting terms..
00444                 $oUser->acceptTerms();
00445             }
00446 
00447             $sUserId = oxRegistry::getSession()->getVariable("su");
00448             $sRecEmail = oxRegistry::getSession()->getVariable("re");
00449             if ($this->getConfig()->getConfigParam('blInvitationsEnabled') && $sUserId && $sRecEmail) {
00450                 // setting registration credit points..
00451                 $oUser->setCreditPointsForRegistrant($sUserId, $sRecEmail);
00452             }
00453 
00454             // assigning to newsletter
00455             $blOptin = oxRegistry::getConfig()->getRequestParameter('blnewssubscribed');
00456             if ($blOptin && $iSubscriptionStatus == 1) {
00457                 // if user was assigned to newsletter
00458                 // and is creating account with newsletter checked,
00459                 // don't require confirm
00460                 $oUser->getNewsSubscription()->setOptInStatus(1);
00461                 $oUser->addToGroup('oxidnewsletter');
00462                 $this->_blNewsSubscriptionStatus = 1;
00463             } else {
00464                 $blOrderOptInEmailParam = $this->getConfig()->getConfigParam('blOrderOptInEmail');
00465                 $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription($blOptin, $blOrderOptInEmailParam);
00466             }
00467 
00468             $oUser->addToGroup('oxidnotyetordered');
00469             $oUser->logout();
00470 
00471         } catch (oxUserException $oEx) {
00472             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00473 
00474             return false;
00475         } catch (oxInputException $oEx) {
00476             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00477 
00478             return false;
00479         } catch (oxConnectionException $oEx) {
00480             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00481 
00482             return false;
00483         }
00484 
00485         if (!$blActiveLogin) {
00486 
00487             oxRegistry::getSession()->setVariable('usr', $oUser->getId());
00488             $this->_afterLogin($oUser);
00489 
00490 
00491             // order remark
00492             //V #427: order remark for new users
00493             $sOrderRemark = oxRegistry::getConfig()->getRequestParameter('order_remark', true);
00494             if ($sOrderRemark) {
00495                 oxRegistry::getSession()->setVariable('ordrem', $sOrderRemark);
00496             }
00497         }
00498 
00499         // send register eMail
00500         //TODO: move into user
00501         if ((int) oxRegistry::getConfig()->getRequestParameter('option') == 3) {
00502             $oxEMail = oxNew('oxemail');
00503             if ($blActiveLogin) {
00504                 $oxEMail->sendRegisterConfirmEmail($oUser);
00505             } else {
00506                 $oxEMail->sendRegisterEmail($oUser);
00507             }
00508         }
00509 
00510         // new registered
00511         $this->_blIsNewUser = true;
00512 
00513         $sAction = 'payment?new_user=1&success=1';
00514         if ($this->_blNewsSubscriptionStatus !== null && !$this->_blNewsSubscriptionStatus) {
00515             $sAction = 'payment?new_user=1&success=1&newslettererror=4';
00516         }
00517 
00518         return $sAction;
00519     }
00520 
00526     public function registerUser()
00527     {
00528         // registered new user ?
00529         if ($this->createuser() != false && $this->_blIsNewUser) {
00530             if ($this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus) {
00531                 return 'register?success=1';
00532             } else {
00533                 return 'register?success=1&newslettererror=4';
00534             }
00535         } else {
00536             // problems with registration ...
00537             $this->logout();
00538         }
00539     }
00540 
00544     protected function _saveInvitor()
00545     {
00546         if ($this->getConfig()->getConfigParam('blInvitationsEnabled')) {
00547             $this->getInvitor();
00548             $this->setRecipient();
00549         }
00550     }
00551 
00555     protected function _saveDeliveryAddressState()
00556     {
00557         $oSession = oxRegistry::getSession();
00558 
00559         $blShow = oxRegistry::getConfig()->getRequestParameter('blshowshipaddress');
00560         if (!isset($blShow)) {
00561             $blShow = $oSession->getVariable('blshowshipaddress');
00562         }
00563 
00564         $oSession->setVariable('blshowshipaddress', $blShow);
00565     }
00566 
00580     protected function _changeUser_noRedirect()
00581     {
00582         if (!$this->getSession()->checkSessionChallenge()) {
00583             return;
00584         }
00585 
00586         // no user ?
00587         $oUser = $this->getUser();
00588         if (!$oUser) {
00589             return;
00590         }
00591 
00592         // collecting values to check
00593         $aDelAdress = $this->_getDelAddressData();
00594 
00595         // if user company name, user name and additional info has special chars
00596         $aInvAdress = oxRegistry::getConfig()->getRequestParameter('invadr', true);
00597 
00598         $sUserName = $oUser->oxuser__oxusername->value;
00599         $sPassword = $sPassword2 = $oUser->oxuser__oxpassword->value;
00600 
00601         try { // testing user input
00602             $oUser->changeUserData($sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress);
00603             // assigning to newsletter
00604             if (($blOptin = oxRegistry::getConfig()->getRequestParameter('blnewssubscribed')) === null) {
00605                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00606             }
00607             // check if email address changed, if so, force check news subscription settings.
00608             $sBillingUsername = $aInvAdress['oxuser__oxusername'];
00609             $blForceCheckOptIn = ($sBillingUsername !== null && $sBillingUsername !== $sUserName);
00610             $blEmailParam = $this->getConfig()->getConfigParam('blOrderOptInEmail');
00611             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription($blOptin, $blEmailParam, $blForceCheckOptIn);
00612 
00613         } catch (oxUserException $oEx) { // errors in input
00614             // marking error code
00615             //TODO
00616             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00617 
00618             return;
00619         } catch (oxInputException $oEx) {
00620             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00621             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'input_not_all_fields');
00622 
00623             return;
00624         } catch (oxConnectionException $oEx) {
00625             //connection to external resource broken, change message and pass to the view
00626             oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
00627 
00628             return;
00629         }
00630 
00631 
00632         // order remark
00633         $sOrderRemark = oxRegistry::getConfig()->getRequestParameter('order_remark', true);
00634 
00635         if ($sOrderRemark) {
00636             oxRegistry::getSession()->setVariable('ordrem', $sOrderRemark);
00637         } else {
00638             oxRegistry::getSession()->deleteVariable('ordrem');
00639         }
00640 
00641         if ($oBasket = $this->getSession()->getBasket()) {
00642             $oBasket->onUpdate();
00643         }
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         $blShowShipAddressParameter = oxRegistry::getConfig()->getRequestParameter('blshowshipaddress');
00658         $blShowShipAddressVariable = oxRegistry::getSession()->getVariable('blshowshipaddress');
00659         $sDeliveryAddressParameter = oxRegistry::getConfig()->getRequestParameter('deladr', true);
00660         $aDeladr = ($blShowShipAddressParameter || $blShowShipAddressVariable) ? $sDeliveryAddressParameter : array();
00661         $aDelAdress = $aDeladr;
00662 
00663         if (is_array($aDeladr)) {
00664             // checking if data is filled
00665             if (isset($aDeladr['oxaddress__oxsal'])) {
00666                 unset($aDeladr['oxaddress__oxsal']);
00667             }
00668             if (!count($aDeladr) || implode('', $aDeladr) == '') {
00669                 // resetting to avoid empty records
00670                 $aDelAdress = array();
00671             }
00672         }
00673 
00674         return $aDelAdress;
00675     }
00676 
00682     protected function _getLogoutLink()
00683     {
00684         $oConfig = $this->getConfig();
00685 
00686         $sLogoutLink = $oConfig->isSsl() ? $oConfig->getShopSecureHomeUrl() : $oConfig->getShopHomeUrl();
00687         $sLogoutLink .= 'cl=' . $oConfig->getRequestParameter('cl') . $this->getParent()->getDynUrlParams();
00688         if ($sParam = $oConfig->getRequestParameter('anid')) {
00689             $sLogoutLink .= '&amp;anid=' . $sParam;
00690         }
00691         if ($sParam = $oConfig->getRequestParameter('cnid')) {
00692             $sLogoutLink .= '&amp;cnid=' . $sParam;
00693         }
00694         if ($sParam = $oConfig->getRequestParameter('mnid')) {
00695             $sLogoutLink .= '&amp;mnid=' . $sParam;
00696         }
00697         if ($sParam = $oConfig->getRequestParameter('tpl')) {
00698             $sLogoutLink .= '&amp;tpl=' . $sParam;
00699         }
00700         if ($sParam = $oConfig->getRequestParameter('oxloadid')) {
00701             $sLogoutLink .= '&amp;oxloadid=' . $sParam;
00702         }
00703         if ($sParam = $oConfig->getRequestParameter('recommid')) {
00704             $sLogoutLink .= '&amp;recommid=' . $sParam;
00705         }
00706 
00707         return $sLogoutLink . '&amp;fnc=logout';
00708     }
00709 
00715     public function setLoginStatus($iStatus)
00716     {
00717         $this->_iLoginStatus = $iStatus;
00718     }
00719 
00728     public function getLoginStatus()
00729     {
00730         return $this->_iLoginStatus;
00731     }
00732 
00736     public function getInvitor()
00737     {
00738         $sSu = oxRegistry::getSession()->getVariable('su');
00739 
00740         if (!$sSu && ($sSuNew = oxRegistry::getConfig()->getRequestParameter('su'))) {
00741             oxRegistry::getSession()->setVariable('su', $sSuNew);
00742         }
00743     }
00744 
00748     public function setRecipient()
00749     {
00750         $sRe = oxRegistry::getSession()->getVariable('re');
00751         if (!$sRe && ($sReNew = oxRegistry::getConfig()->getRequestParameter('re'))) {
00752             oxRegistry::getSession()->setVariable('re', $sReNew);
00753         }
00754     }
00755 }