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                                         );
00060 
00066     protected $_aRawBillingFields = array( 'oxuser__oxcompany', 'oxuser__oxaddinfo', 'oxuser__oxfname',
00067                                            'oxuser__oxlname', 'oxuser__oxstreet', 'oxuser__oxstreetnr',
00068                                            'oxuser__oxcity', 'oxuser__oxfon', 'oxuser__oxfax',
00069                                            'oxuser__oxmobfon', 'oxuser__oxprivfon' );
00070 
00076     protected $_aRawShippingFields = array( 'oxaddress__oxcompany', 'oxaddress__oxaddinfo', 'oxaddress__oxfname',
00077                                             'oxaddress__oxlname', 'oxaddress__oxcity', 'oxaddress__oxstreet',
00078                                             'oxaddress__oxstreetnr', 'oxaddress__oxzip', 'oxaddress__oxfon',
00079                                             'oxaddress__oxfax' );
00089     public function init()
00090     {
00091         // saving show/hide delivery address state
00092         $blShow = oxConfig::getParameter( 'blshowshipaddress' );
00093         if (!isset($blShow)) {
00094             $blShow = oxSession::getVar( 'blshowshipaddress' );
00095         }
00096         // @deprecated, remove blhideshipaddress checking when basic theme support discontinued
00097         if (oxConfig::getParameter( 'blhideshipaddress' ) || oxSession::getVar( 'blhideshipaddress' )) {
00098             $blShow = false;
00099         }
00100 
00101         oxSession::setVar( 'blshowshipaddress', $blShow );
00102 
00103         // load session user
00104         $this->_loadSessionUser();
00105         if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) ) {
00106             // get invitor ID
00107             $this->getInvitor();
00108             $this->setRecipient();
00109         }
00110 
00111         parent::init();
00112     }
00113 
00123     public function render()
00124     {
00125         // checks if private sales allows further tasks
00126         $this->_checkPsState();
00127 
00128         parent::render();
00129 
00130         // dyn_group feature: if you specify a groupid in URL the user
00131         // will automatically be added to this group later
00132         if ( $sDynGoup = oxConfig::getParameter( 'dgr' ) ) {
00133             oxSession::setVar( 'dgr', $sDynGoup );
00134         }
00135 
00136         return $this->getUser();
00137     }
00138 
00149     protected function _checkPsState()
00150     {
00151         $oConfig = $this->getConfig();
00152         if ( $this->getParent()->isEnabledPrivateSales() ) {
00153             // load session user
00154             $oUser  = $this->getUser();
00155             $sClass = $this->getParent()->getClassName();
00156 
00157             // no session user
00158             if ( !$oUser && !in_array( $sClass, $this->_aAllowedClasses ) ) {
00159                 oxUtils::getInstance()->redirect( $oConfig->getShopHomeURL() . 'cl=account', false, 302 );
00160             }
00161 
00162             if ( $oUser && !$oUser->isTermsAccepted() &&
00163                  $oConfig->getConfigParam( 'blConfirmAGB' ) &&
00164                  !in_array( $sClass, $this->_aAllowedClasses ) ) {
00165                 oxUtils::getInstance()->redirect( $oConfig->getShopHomeURL() . 'cl=account&term=1', false, 302 );
00166             }
00167         }
00168     }
00169 
00175     protected function _loadSessionUser()
00176     {
00177         $myConfig = $this->getConfig();
00178         $oUser = $this->getUser();
00179 
00180         // no session user
00181         if ( !$oUser ) {
00182             return;
00183         }
00184 
00185         // this user is blocked, deny him
00186         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00187             oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() . 'cl=content&tpl=user_blocked.tpl', true, 302  );
00188         }
00189 
00190         // TODO: move this to a proper place
00191         if ( $oUser->isLoadedFromCookie() ) {
00192 
00193             // #1678 R
00194             if ( !$myConfig->getConfigParam( 'blPerfNoBasketSaving' ) ) {
00195                 $myConfig->setGlobalParameter( 'blUserChanged', 1 );
00196             }
00197 
00198             if ( $oBasket = $this->getSession()->getBasket() ) {
00199                 $oBasket->load();
00200                 $oBasket->onUpdate();
00201             }
00202         }
00203     }
00204 
00218     public function login()
00219     {
00220         $sUser     = oxConfig::getParameter( 'lgn_usr' );
00221         $sPassword = oxConfig::getParameter( 'lgn_pwd' );
00222         $sCookie   = oxConfig::getParameter( 'lgn_cook' );
00223         //$blFbLogin = oxConfig::getParameter( 'fblogin' );
00224 
00225         $this->setLoginStatus( USER_LOGIN_FAIL );
00226 
00227         // trying to login user
00228         try {
00229             $oUser = oxNew( 'oxuser' );
00230             $oUser->login( $sUser, $sPassword, $sCookie );
00231             $this->setLoginStatus( USER_LOGIN_SUCCESS );
00232         } catch ( oxUserException $oEx ) {
00233             // for login component send excpetion text to a custom component (if defined)
00234             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00235             return 'user';
00236         } catch( oxCookieException $oEx ){
00237             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00238             return 'user';
00239         }
00240         // finalizing ..
00241         return $this->_afterLogin( $oUser );
00242     }
00243 
00261     protected function _afterLogin( $oUser )
00262     {
00263         $oSession = $this->getSession();
00264 
00265         // generating new session id after login
00266         if ( $this->getLoginStatus() === USER_LOGIN_SUCCESS ) {
00267             $oSession->regenerateSessionId();
00268         }
00269 
00270         $myConfig = $this->getConfig();
00271 
00272         // this user is blocked, deny him
00273         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00274             oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL().'cl=content&tpl=user_blocked.tpl', true, 302 );
00275         }
00276 
00277         // adding to dyn group
00278         $oUser->addDynGroup(oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ));
00279 
00280         // recalc basket
00281         if ( $oBasket = $oSession->getBasket() ) {
00282             $oBasket->onUpdate();
00283         }
00284 
00285         // #1678 R
00286         if ( !$myConfig->getConfigParam( 'blPerfNoBasketSaving' ) ) {
00287             $myConfig->setGlobalParameter( 'blUserChanged', 1);
00288         }
00289 
00290 
00291         return 'payment';
00292     }
00293 
00300     public function login_noredirect()
00301     {
00302         $blAgb = oxConfig::getParameter( 'ord_agb' );
00303         $oConfig = $this->getConfig();
00304         if ( $this->getParent()->isEnabledPrivateSales() && $blAgb !== null &&
00305              $oConfig->getConfigParam( 'blConfirmAGB' ) && ( $oUser = $this->getUser() ) ) {
00306             if ( $blAgb ) {
00307                 $oUser->acceptTerms();
00308             }
00309         } else {
00310             $this->login();
00311         }
00312     }
00313 
00320     public function login_updateFbId()
00321     {
00322         $this->login();
00323 
00324         if ( $oUser = $this->getUser() ) {
00325             //updating user Facebook ID
00326             if ( $oUser->updateFbId() ) {
00327                 oxSession::setVar( '_blFbUserIdUpdated', true );
00328             }
00329         }
00330     }
00331 
00340     protected function _afterLogout()
00341     {
00342         oxSession::deleteVar( 'paymentid' );
00343         oxSession::deleteVar( 'sShipSet' );
00344         oxSession::deleteVar( 'deladrid' );
00345         oxSession::deleteVar( 'dynvalue' );
00346 
00347         // resetting & recalc basket
00348         if ( ( $oBasket = $this->getSession()->getBasket() ) ) {
00349             $oBasket->resetUserInfo();
00350             $oBasket->onUpdate();
00351         }
00352     }
00353 
00362     public function logout()
00363     {
00364         $myConfig  = $this->getConfig();
00365         $oUser = oxNew( 'oxuser' );
00366 
00367         if ( $oUser->logout() ) {
00368 
00369             $this->setLoginStatus( USER_LOGOUT );
00370 
00371             // finalizing ..
00372             $this->_afterLogout();
00373 
00374 
00375             if ( $this->getParent()->isEnabledPrivateSales() ) {
00376                 return 'account';
00377             }
00378 
00379             // redirecting if user logs out in SSL mode
00380             if ( oxConfig::getParameter('redirect') && $myConfig->getConfigParam( 'sSSLShopURL' ) ) {
00381 
00382                 oxUtils::getInstance()->redirect( $this->_getLogoutLink());
00383             }
00384         }
00385     }
00386 
00396     public function changeUser( )
00397     {
00398         // checking if "open address area" button was clicked
00399         // or reloading form when delivery address was selected
00400         if ( $this->_setupDelAddress() ) {
00401             return;
00402         }
00403 
00404         $blUserRegistered = $this->_changeUser_noRedirect();
00405 
00406         if ( $blUserRegistered === true ) {
00407             return 'payment';
00408         } else {
00409             return $blUserRegistered;
00410         }
00411     }
00412 
00418     public function changeuser_testvalues()
00419     {
00420         // skip updating user info if this is just form reload
00421         // on selecting delivery address
00422 
00423         $this->_changeUser_noRedirect();
00424     }
00425 
00447     public function createUser()
00448     {
00449         // checking if "open address area" button was clicked
00450         if ( $blSetup = $this->_setupDelAddress() ) {
00451             return;
00452         }
00453 
00454         $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
00455 
00456         $myConfig = $this->getConfig();
00457         if ( $blActiveLogin && !oxConfig::getParameter( 'ord_agb' ) && $myConfig->getConfigParam( 'blConfirmAGB' ) ) {
00458             oxUtilsView::getInstance()->addErrorToDisplay( 'ORDER_READANDCONFIRMTERMS', false, true );
00459             return;
00460         }
00461 
00462         $myUtils  = oxUtils::getInstance();
00463 
00464         // collecting values to check
00465         $sUser = oxConfig::getParameter( 'lgn_usr' );
00466 
00467         // first pass
00468         $sPassword = oxConfig::getParameter( 'lgn_pwd' );
00469 
00470         // second pass
00471         $sPassword2 = oxConfig::getParameter( 'lgn_pwd2' );
00472 
00473         $aInvAdress = oxConfig::getParameter( 'invadr', $this->_aRawBillingFields );
00474         $aDelAdress = $this->_getDelAddressData();
00475 
00476         $oUser = oxNew( 'oxuser' );
00477 
00478         try {
00479 
00480             $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00481 
00482             $iActState = $blActiveLogin ? 0 : 1;
00483 
00484             // setting values
00485             $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
00486             $oUser->setPassword( $sPassword );
00487             $oUser->oxuser__oxactive   = new oxField( $iActState, oxField::T_RAW);
00488 
00489             $oUser->createUser();
00490             $oUser->load( $oUser->getId() );
00491             $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
00492 
00493             if ( $blActiveLogin ) {
00494                 // accepting terms..
00495                 $oUser->acceptTerms();
00496             }
00497 
00498             $sUserId = oxSession::getVar( "su" );
00499             $sRecEmail = oxSession::getVar( "re" );
00500             if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId && $sRecEmail ) {
00501                 // setting registration credit points..
00502                 $oUser->setCreditPointsForRegistrant( $sUserId, $sRecEmail );
00503             }
00504 
00505             // assigning to newsletter
00506             $blOptin = oxConfig::getParameter( 'blnewssubscribed' );
00507             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00508 
00509             $oUser->addToGroup( 'oxidnotyetordered' );
00510             $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
00511             $oUser->logout();
00512 
00513         } catch ( oxUserException $oEx ) {
00514             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00515             return false;
00516         } catch( oxInputException $oEx ){
00517             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00518             return false;
00519         } catch( oxConnectionException $oEx ){
00520             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00521             return false;
00522         }
00523 
00524         if ( !$blActiveLogin ) {
00525             if ( !$sPassword ) {
00526                 oxSession::setVar( 'usr', $oUser->getId() );
00527                 $this->_afterLogin( $oUser );
00528             } elseif ( $this->login() == 'user' ) {
00529                 return false;
00530             }
00531 
00532             // order remark
00533             //V #427: order remark for new users
00534             $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00535             if ( $sOrderRemark ) {
00536                 oxSession::setVar( 'ordrem', $sOrderRemark );
00537             }
00538         }
00539 
00540         // send register eMail
00541         //TODO: move into user
00542         if ( (int) oxConfig::getParameter( 'option' ) == 3 ) {
00543             $oxEMail = oxNew( 'oxemail' );
00544             if ( $blActiveLogin ) {
00545                 $oxEMail->sendRegisterConfirmEmail( $oUser );
00546             } else {
00547                 $oxEMail->sendRegisterEmail( $oUser );
00548             }
00549         }
00550 
00551         // new registered
00552         $this->_blIsNewUser = true;
00553 
00554         return 'payment';
00555     }
00556 
00562     public function registerUser()
00563     {
00564         // checking if "open address area" button was clicked
00565         if ( $blSetup = $this->_setupDelAddress() ) {
00566             return;
00567         }
00568 
00569         // registered new user ?
00570         if ( $this->createuser()!= false && $this->_blIsNewUser ) {
00571             if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
00572                 return 'register?success=1';
00573             } else {
00574                 return 'register?success=1&newslettererror=4';
00575             }
00576         } else {
00577             // problems with registration ...
00578             $this->logout();
00579         }
00580     }
00581 
00595     protected function _changeUser_noRedirect( )
00596     {
00597         if (!$this->getSession()->checkSessionChallenge()) {
00598             return;
00599         }
00600 
00601         // no user ?
00602         $oUser = $this->getUser();
00603         if ( !$oUser ) {
00604             return;
00605         }
00606 
00607         // collecting values to check
00608         $aDelAdress = $this->_getDelAddressData();
00609 
00610         // if user company name, user name and additional info has special chars
00611         $aInvAdress = oxConfig::getParameter( 'invadr', $this->_aRawBillingFields );
00612 
00613         $sUserName  = $oUser->oxuser__oxusername->value;
00614         $sPassword  = $sPassword2 = $oUser->oxuser__oxpassword->value;
00615 
00616         try { // testing user input
00617             $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00618             // assigning to newsletter
00619             if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
00620                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00621             }
00622             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00623 
00624         } catch ( oxUserException $oEx ) { // errors in input
00625             // marking error code
00626             //TODO
00627             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00628             return;
00629         } catch(oxInputException $oEx) {
00630             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00631             return;
00632         } catch(oxConnectionException $oEx){
00633              //connection to external resource broken, change message and pass to the view
00634             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00635             return;
00636         }
00637 
00638 
00639         // order remark
00640         $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00641 
00642         if ( $sOrderRemark ) {
00643             oxSession::setVar( 'ordrem', $sOrderRemark );
00644         } else {
00645             oxSession::deleteVar( 'ordrem' );
00646         }
00647 
00648         if ( $oBasket = $this->getSession()->getBasket() ) {
00649             $oBasket->onUpdate();
00650         }
00651         return true;
00652     }
00653 
00660     protected function _getDelAddressData()
00661     {
00662         // if user company name, user name and additional info has special chars
00663         $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', $this->_aRawShippingFields ) : array();
00664 
00665         if ( is_array( $aDeladr ) ) {
00666             // checking if data is filled
00667             if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
00668                 unset( $aDeladr['oxaddress__oxsal'] );
00669             }
00670             if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
00671                 // resetting to avoid empty records
00672                 $aDelAdress = array();
00673             }
00674         }
00675         return $aDelAdress;
00676     }
00677 
00683     protected function _getLogoutLink()
00684     {
00685         $myConfig = $this->getConfig();
00686         $sLogoutLink = $myConfig->getShopSecureHomeUrl();
00687         if ( $myConfig->isSsl() ) {
00688             $sLogoutLink = $myConfig->getShopHomeUrl();
00689         }
00690         $sLogoutLink .= 'cl='.oxConfig::getParameter('cl').$this->getParent()->getDynUrlParams();
00691         if ( $sParam = oxConfig::getParameter('anid') ) {
00692             $sLogoutLink .= '&amp;anid='.$sParam;
00693         }
00694         if ( $sParam = oxConfig::getParameter('cnid') ) {
00695             $sLogoutLink .= '&amp;cnid='.$sParam;
00696         }
00697         if ( $sParam = oxConfig::getParameter('mnid') ) {
00698             $sLogoutLink .= '&amp;mnid='.$sParam;
00699         }
00700         if ( $sParam = oxConfig::getParameter('tpl') ) {
00701             $sLogoutLink .= '&amp;tpl='.$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 }