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 
00106         if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) ) {
00107             // get invitor ID
00108             $this->getInvitor();
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 );
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 );
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' );
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->onUpdate();
00200             }
00201         }
00202     }
00203 
00217     public function login()
00218     {
00219         $sUser     = oxConfig::getParameter( 'lgn_usr' );
00220         $sPassword = oxConfig::getParameter( 'lgn_pwd' );
00221         $sCookie   = oxConfig::getParameter( 'lgn_cook' );
00222         //$blFbLogin = oxConfig::getParameter( 'fblogin' );
00223 
00224         $this->setLoginStatus( USER_LOGIN_FAIL );
00225 
00226         // trying to login user
00227         try {
00228             $oUser = oxNew( 'oxuser' );
00229             $oUser->login( $sUser, $sPassword, $sCookie );
00230             $this->setLoginStatus( USER_LOGIN_SUCCESS );
00231         } catch ( oxUserException $oEx ) {
00232             // for login component send excpetion text to a custom component (if defined)
00233             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00234             return 'user';
00235         } catch( oxCookieException $oEx ){
00236             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00237             return 'user';
00238         }
00239         // finalizing ..
00240         return $this->_afterLogin( $oUser );
00241     }
00242 
00260     protected function _afterLogin( $oUser )
00261     {
00262         $oSession = $this->getSession();
00263 
00264         // generating new session id after login
00265         if ( $this->getLoginStatus() === USER_LOGIN_SUCCESS ) {
00266             $oSession->regenerateSessionId();
00267         }
00268 
00269         $myConfig = $this->getConfig();
00270 
00271         // this user is blocked, deny him
00272         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00273             oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL().'cl=content&tpl=user_blocked.tpl' );
00274         }
00275 
00276         // adding to dyn group
00277         $oUser->addDynGroup(oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ));
00278 
00279         // recalc basket
00280         if ( $oBasket = $oSession->getBasket() ) {
00281             $oBasket->onUpdate();
00282         }
00283 
00284         // #1678 R
00285         if ( !$myConfig->getConfigParam( 'blPerfNoBasketSaving' ) ) {
00286             $myConfig->setGlobalParameter( 'blUserChanged', 1);
00287         }
00288 
00289 
00290         return 'payment';
00291     }
00292 
00299     public function login_noredirect()
00300     {
00301         $blAgb = oxConfig::getParameter( 'ord_agb' );
00302         $oConfig = $this->getConfig();
00303         if ( $this->getParent()->isEnabledPrivateSales() && $blAgb !== null &&
00304              $oConfig->getConfigParam( 'blConfirmAGB' ) && ( $oUser = $this->getUser() ) ) {
00305             if ( $blAgb ) {
00306                 $oUser->acceptTerms();
00307             }
00308         } else {
00309             $this->login();
00310         }
00311     }
00312 
00319     public function login_updateFbId()
00320     {
00321         $this->login();
00322 
00323         if ( $oUser = $this->getUser() ) {
00324             //updating user Facebook ID
00325             if ( $oUser->updateFbId() ) {
00326                 oxSession::setVar( '_blFbUserIdUpdated', true );
00327             }
00328         }
00329     }
00330 
00339     protected function _afterLogout()
00340     {
00341         oxSession::deleteVar( 'paymentid' );
00342         oxSession::deleteVar( 'sShipSet' );
00343         oxSession::deleteVar( 'deladrid' );
00344         oxSession::deleteVar( 'dynvalue' );
00345 
00346         // resetting & recalc basket
00347         if ( ( $oBasket = $this->getSession()->getBasket() ) ) {
00348             $oBasket->resetUserInfo();
00349             $oBasket->onUpdate();
00350         }
00351     }
00352 
00361     public function logout()
00362     {
00363         $myConfig  = $this->getConfig();
00364         $oUser = oxNew( 'oxuser' );
00365 
00366         if ( $oUser->logout() ) {
00367 
00368             $this->setLoginStatus( USER_LOGOUT );
00369 
00370             // finalizing ..
00371             $this->_afterLogout();
00372 
00373 
00374             if ( $this->getParent()->isEnabledPrivateSales() ) {
00375                 return 'account';
00376             }
00377 
00378             // redirecting if user logs out in SSL mode
00379             if ( oxConfig::getParameter('redirect') && $myConfig->getConfigParam( 'sSSLShopURL' ) ) {
00380 
00381                 oxUtils::getInstance()->redirect( $this->_getLogoutLink());
00382             }
00383         }
00384     }
00385 
00395     public function changeUser( )
00396     {
00397         // checking if "open address area" button was clicked
00398         // or reloading form when delivery address was selected
00399         if ( $this->_setupDelAddress() ) {
00400             return;
00401         }
00402 
00403         $blUserRegistered = $this->_changeUser_noRedirect();
00404 
00405         if ( $blUserRegistered === true ) {
00406             return 'payment';
00407         } else {
00408             return $blUserRegistered;
00409         }
00410     }
00411 
00417     public function changeuser_testvalues()
00418     {
00419         // skip updating user info if this is just form reload
00420         // on selecting delivery address
00421 
00422         $this->_changeUser_noRedirect();
00423     }
00424 
00446     public function createUser()
00447     {
00448         // checking if "open address area" button was clicked
00449         if ( $blSetup = $this->_setupDelAddress() ) {
00450             return;
00451         }
00452 
00453         $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
00454 
00455         $myConfig = $this->getConfig();
00456         if ( $blActiveLogin && !oxConfig::getParameter( 'ord_agb' ) && $myConfig->getConfigParam( 'blConfirmAGB' ) ) {
00457             oxUtilsView::getInstance()->addErrorToDisplay( 'ORDER_READANDCONFIRMTERMS', false, true );
00458             return;
00459         }
00460 
00461         $myUtils  = oxUtils::getInstance();
00462 
00463         // collecting values to check
00464         $sUser = oxConfig::getParameter( 'lgn_usr' );
00465 
00466         // first pass
00467         $sPassword = oxConfig::getParameter( 'lgn_pwd' );
00468 
00469         // second pass
00470         $sPassword2 = oxConfig::getParameter( 'lgn_pwd2' );
00471 
00472         $aInvAdress = oxConfig::getParameter( 'invadr', $this->_aRawBillingFields );
00473         $aDelAdress = $this->_getDelAddressData();
00474 
00475         $oUser = oxNew( 'oxuser' );
00476 
00477         try {
00478 
00479             $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00480 
00481             $iActState = $blActiveLogin ? 0 : 1;
00482 
00483             // setting values
00484             $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
00485             $oUser->setPassword( $sPassword );
00486             $oUser->oxuser__oxactive   = new oxField( $iActState, oxField::T_RAW);
00487 
00488             $oUser->createUser();
00489             $oUser->load( $oUser->getId() );
00490             $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
00491 
00492             if ( $blActiveLogin ) {
00493                 // accepting terms..
00494                 $oUser->acceptTerms();
00495             }
00496 
00497             if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId = oxConfig::getParameter( "su" ) ) {
00498                 // setting registration credit points..
00499                 $oUser->setCreditPointsForRegistrant( $sUserId );
00500             }
00501 
00502             // assigning to newsletter
00503             $blOptin = oxConfig::getParameter( 'blnewssubscribed' );
00504             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00505 
00506             $oUser->addToGroup( 'oxidnotyetordered' );
00507             $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
00508             $oUser->logout();
00509 
00510         } catch ( oxUserException $oEx ) {
00511             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00512             return false;
00513         } catch( oxInputException $oEx ){
00514             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00515             return false;
00516         } catch( oxConnectionException $oEx ){
00517             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00518             return false;
00519         }
00520 
00521         if ( !$blActiveLogin ) {
00522             if ( !$sPassword ) {
00523                 oxSession::setVar( 'usr', $oUser->getId() );
00524                 $this->_afterLogin( $oUser );
00525             } elseif ( $this->login() == 'user' ) {
00526                 return false;
00527             }
00528 
00529             // order remark
00530             //V #427: order remark for new users
00531             $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00532             if ( $sOrderRemark ) {
00533                 oxSession::setVar( 'ordrem', $sOrderRemark );
00534             }
00535         }
00536 
00537         // send register eMail
00538         //TODO: move into user
00539         if ( (int) oxConfig::getParameter( 'option' ) == 3 ) {
00540             $oxEMail = oxNew( 'oxemail' );
00541             if ( $blActiveLogin ) {
00542                 $oxEMail->sendRegisterConfirmEmail( $oUser );
00543             } else {
00544                 $oxEMail->sendRegisterEmail( $oUser );
00545             }
00546         }
00547 
00548         // new registered
00549         $this->_blIsNewUser = true;
00550 
00551         return 'payment';
00552     }
00553 
00559     public function registerUser()
00560     {
00561         // checking if "open address area" button was clicked
00562         if ( $blSetup = $this->_setupDelAddress() ) {
00563             return;
00564         }
00565 
00566         // registered new user ?
00567         if ( $this->createuser()!= false && $this->_blIsNewUser ) {
00568             if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
00569                 return 'register?success=1';
00570             } else {
00571                 return 'register?success=1&newslettererror=4';
00572             }
00573         } else {
00574             // problems with registration ...
00575             $this->logout();
00576         }
00577     }
00578 
00592     protected function _changeUser_noRedirect( )
00593     {
00594         if (!$this->getSession()->checkSessionChallenge()) {
00595             return;
00596         }
00597 
00598         // no user ?
00599         $oUser = $this->getUser();
00600         if ( !$oUser ) {
00601             return;
00602         }
00603 
00604         // collecting values to check
00605         $aDelAdress = $this->_getDelAddressData();
00606 
00607         // if user company name, user name and additional info has special chars
00608         $aInvAdress = oxConfig::getParameter( 'invadr', $this->_aRawBillingFields );
00609 
00610         $sUserName  = $oUser->oxuser__oxusername->value;
00611         $sPassword  = $sPassword2 = $oUser->oxuser__oxpassword->value;
00612 
00613         try { // testing user input
00614             $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00615             // assigning to newsletter
00616             if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
00617                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00618             }
00619             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00620 
00621         } catch ( oxUserException $oEx ) { // errors in input
00622             // marking error code
00623             //TODO
00624             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00625             return;
00626         } catch(oxInputException $oEx) {
00627             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00628             return;
00629         } catch(oxConnectionException $oEx){
00630              //connection to external resource broken, change message and pass to the view
00631             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00632             return;
00633         }
00634 
00635 
00636         // order remark
00637         $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
00638 
00639         if ( $sOrderRemark ) {
00640             oxSession::setVar( 'ordrem', $sOrderRemark );
00641         } else {
00642             oxSession::deleteVar( 'ordrem' );
00643         }
00644 
00645         if ( $oBasket = $this->getSession()->getBasket() ) {
00646             $oBasket->onUpdate();
00647         }
00648         return true;
00649     }
00650 
00657     protected function _getDelAddressData()
00658     {
00659         // if user company name, user name and additional info has special chars
00660         $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', $this->_aRawShippingFields ) : array();
00661 
00662         if ( is_array( $aDeladr ) ) {
00663             // checking if data is filled
00664             if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
00665                 unset( $aDeladr['oxaddress__oxsal'] );
00666             }
00667             if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
00668                 // resetting to avoid empty records
00669                 $aDelAdress = array();
00670             }
00671         }
00672         return $aDelAdress;
00673     }
00674 
00680     protected function _getLogoutLink()
00681     {
00682         $myConfig = $this->getConfig();
00683         $sLogoutLink = $myConfig->getShopSecureHomeUrl();
00684         if ( $myConfig->isSsl() ) {
00685             $sLogoutLink = $myConfig->getShopHomeUrl();
00686         }
00687         $sLogoutLink .= 'cl='.oxConfig::getParameter('cl').$this->getParent()->getDynUrlParams();
00688         if ( $sParam = oxConfig::getParameter('anid') ) {
00689             $sLogoutLink .= '&amp;anid='.$sParam;
00690         }
00691         if ( $sParam = oxConfig::getParameter('cnid') ) {
00692             $sLogoutLink .= '&amp;cnid='.$sParam;
00693         }
00694         if ( $sParam = oxConfig::getParameter('mnid') ) {
00695             $sLogoutLink .= '&amp;mnid='.$sParam;
00696         }
00697         if ( $sParam = oxConfig::getParameter('tpl') ) {
00698             $sLogoutLink .= '&amp;tpl='.$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 }