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