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 
00051     public function init()
00052     {
00053         // load session user
00054         $this->_loadSessionUser();
00055 
00056         parent::init();
00057     }
00058 
00071     public function render()
00072     {
00073         parent::render();
00074 
00075         // dyn_group feature: if you specify a groupid in URL the user
00076         // will automatically be added to this group later
00077         if ( $sDynGoup = oxConfig::getParameter( 'dgr' ) ) {
00078             oxSession::setVar( 'dgr', $sDynGoup );
00079         }
00080 
00081         /*
00082         if ( $blNewsReg = oxConfig::getParameter( 'blnewssubscribed' )) {
00083             $this->_oParent->setNewsSubscribed( $blNewsReg );
00084             // Passing to view. Left for compatibility reasons for a while. Will be removed in future
00085             $this->_oParent->addTplParam( 'blnewssubscribed', $this->_oParent->isNewsSubscribed() );
00086         }*/
00087 
00088         if ( $aInvAdress = oxConfig::getParameter( 'invadr') ) {
00089             $this->_oParent->addTplParam( 'invadr', $aInvAdress );
00090         }
00091 
00092         if ( ( $aDelAdress = oxConfig::getParameter( 'deladr') ) && !oxConfig::getParameter( 'reloadaddress' ) ) {
00093                $this->_oParent->addTplParam( 'deladr', $aDelAdress );
00094         }
00095 
00096         if ( $sUser = oxConfig::getParameter( 'lgn_usr' ) ) {
00097             $this->_oParent->addTplParam( 'lgn_usr', $sUser );
00098         }
00099 
00100         /*
00101         if ( $aDelAdressID = oxConfig::getParameter( 'deladrid' ) ) {
00102             $oAddress = oxNew( 'oxaddress' );
00103             $oAddress->load( $aDelAdressID );
00104             $this->_oParent->setDelAddress( $oAddress );
00105             $this->_oParent->addTplParam( 'delivadr', $this->_oParent->getDelAddress() );
00106         }
00107 
00108         // clicked on show address ?
00109         if ( $blShowAddress = oxSession::getVar( 'blshowshipaddress' ) ) {
00110             $this->_oParent->setShowShipAddress( 1 );
00111             // Passing to view. Left for compatibility reasons for a while. Will be removed in future
00112             $this->_oParent->addTplParam( 'blshowshipaddress', 1 );
00113         }*/
00114 
00115         return $this->getUser();
00116     }
00117 
00123     protected function _loadSessionUser()
00124     {
00125         $myConfig = $this->getConfig();
00126         $oUser = $this->getUser();
00127 
00128         // no session user
00129         if ( !$oUser ) {
00130             return;
00131         }
00132 
00133         // this user is blocked, deny him
00134         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00135             oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() . 'cl=content&tpl=user_blocked.tpl' );
00136         }
00137 
00138         // TODO: we need todo something with this !!!
00139         if ( $oUser->isLoadedFromCookie() ) {
00140 
00141                 // #1678 R
00142                 if ( !$myConfig->getConfigParam( 'blPerfNoBasketSaving' ) ) {
00143                     $myConfig->setGlobalParameter( 'blUserChanged', 1 );
00144                 }
00145 
00146             if ( $oBasket = $this->getSession()->getBasket() ) {
00147                 $oBasket->onUpdate();
00148             }
00149         }
00150     }
00151 
00165     public function login()
00166     {
00167         $sUser     = oxConfig::getParameter( 'lgn_usr' );
00168         $sPassword = oxConfig::getParameter( 'lgn_pwd' );
00169         $sCookie   = oxConfig::getParameter( 'lgn_cook' );
00170         $sOpenId   = oxConfig::getParameter( 'lgn_openid' );
00171 
00172         $this->setLoginStatus( USER_LOGIN_FAIL );
00173 
00174         // trying to login user
00175         try {
00176             $oUser = oxNew( 'oxuser' );
00177             if ( $sOpenId ) {
00178                 $iOldErrorReproting = error_reporting();
00179                 error_reporting($iOldErrorReproting & ~E_STRICT);
00180                 $oOpenId = oxNew( "oxOpenID" );
00181                 $oOpenId->authenticateOid( $sOpenId, $this->_getReturnUrl() );
00182                 error_reporting($iOldErrorReproting);
00183             } else {
00184                 $oUser->login( $sUser, $sPassword, $sCookie );
00185             }
00186             $this->setLoginStatus( USER_LOGIN_SUCCESS );
00187         } catch ( oxUserException $oEx ) {
00188             // for login component send excpetion text to a custom component (if defined)
00189             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00190             return 'user';
00191         } catch( oxCookieException $oEx ){
00192             oxUtilsView::getInstance()->addErrorToDisplay( $oEx );
00193             return 'user';
00194         }
00195         // finalizing ..
00196         return $this->_afterLogin( $oUser );
00197     }
00198 
00216     protected function _afterLogin( $oUser )
00217     {
00218         $myConfig = $this->getConfig();
00219 
00220         // this user is blocked, deny him
00221         if ( $oUser->inGroup( 'oxidblocked' ) ) {
00222             oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL().'cl=content&tpl=user_blocked.tpl' );
00223         }
00224 
00225         // adding to dyn group
00226         $oUser->addDynGroup(oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ));
00227 
00228         // recalc basket
00229         if ( $oBasket = $this->getSession()->getBasket() ) {
00230             $oBasket->onUpdate();
00231         }
00232 
00233             // #1678 R
00234             if ( !$myConfig->getConfigParam( 'blPerfNoBasketSaving' ) ) {
00235                 $myConfig->setGlobalParameter( 'blUserChanged', 1);
00236             }
00237 
00238 
00239         return 'payment';
00240     }
00241 
00248     public function login_noredirect()
00249     {
00250         $this->login();
00251     }
00252 
00261     protected function _afterLogout()
00262     {
00263         oxSession::deleteVar( 'paymentid' );
00264         oxSession::deleteVar( 'sShipSet' );
00265         oxSession::deleteVar( 'deladrid' );
00266         oxSession::deleteVar( 'dynvalue' );
00267 
00268         // resetting & recalc basket
00269         if ( ( $oBasket = $this->getSession()->getBasket() ) ) {
00270             $oBasket->resetUserInfo();
00271             $oBasket->onUpdate();
00272         }
00273     }
00274 
00283     public function logout()
00284     {
00285         $myConfig  = $this->getConfig();
00286         $oUser = oxNew( 'oxuser' );
00287 
00288         if ( $oUser->logout() ) {
00289 
00290             $this->setLoginStatus( USER_LOGOUT );
00291 
00292             // finalizing ..
00293             $this->_afterLogout();
00294 
00295 
00296             // redirecting if user logs out in SSL mode
00297             if ( oxConfig::getParameter('redirect') && $myConfig->getConfigParam( 'sSSLShopURL' ) ) {
00298 
00299                 oxUtils::getInstance()->redirect( $this->_getLogoutLink());
00300             }
00301         }
00302     }
00303 
00313     public function changeUser( )
00314     {
00315         // checking if "open address area" button was clicked
00316         // or reloading form when delivery address was selected
00317         if ( $this->_setupDelAddress() ) {
00318             return;
00319         }
00320 
00321         $blUserRegistered = $this->_changeUser_noRedirect( );
00322 
00323         if ( $blUserRegistered === true ) {
00324             return 'payment';
00325         } else {
00326             return $blUserRegistered;
00327         }
00328     }
00329 
00335     public function changeuser_testvalues()
00336     {
00337         // skip updating user info if this is just form reload
00338         // on selecting delivery address
00339 
00340         $this->_changeUser_noRedirect();
00341     }
00342 
00364     public function createUser()
00365     {
00366         // checking if "open address area" button was clicked
00367         if ( $blSetup = $this->_setupDelAddress() ) {
00368             return;
00369         }
00370 
00371         $myConfig = $this->getConfig();
00372         $myUtils  = oxUtils::getInstance();
00373 
00374         // collecting values to check
00375         $sUser = oxConfig::getParameter( 'lgn_usr' );
00376 
00377         // first pass
00378         $sPassword = oxConfig::getParameter( 'lgn_pwd' );
00379 
00380         // second pass
00381         $sPassword2 = oxConfig::getParameter( 'lgn_pwd2' );
00382 
00383         $aRawVal = array('oxuser__oxcompany', 'oxuser__oxaddinfo', 'oxuser__oxfname', 'oxuser__oxlname', 'oxuser__oxcity');
00384         $aInvAdress = oxConfig::getParameter( 'invadr', $aRawVal );
00385         $aDelAdress = $this->_getDelAddressData();
00386 
00387         $oUser = oxNew( 'oxuser' );
00388 
00389         try {
00390 
00391             $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00392 
00393             // setting values
00394             $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
00395             $oUser->setPassword( $sPassword );
00396             $oUser->oxuser__oxactive   = new oxField(1, oxField::T_RAW);
00397 
00398             $oUser->createUser();
00399             $oUser->load( $oUser->getId() );
00400             $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
00401 
00402             // assigning to newsletter
00403             $blOptin = oxConfig::getParameter( 'blnewssubscribed' );
00404             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, true );
00405 
00406             $oUser->logout();
00407 
00408         } catch ( oxUserException $oEx ) {
00409             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00410             return false;
00411         } catch( oxInputException $oEx ){
00412             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00413             return false;
00414         } catch( oxConnectionException $oEx ){
00415             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00416             return false;
00417         }
00418 
00419         if ( !$sPassword ) {
00420             oxSession::setVar( 'usr', $oUser->getId() );
00421             $this->_afterLogin( $oUser );
00422         } elseif ( $this->login() == 'user' ) {
00423             return false;
00424         }
00425 
00426         // order remark
00427         //V #427: order remark for new users
00428         $sOrd_Remark = oxConfig::getParameter( 'order_remark' );
00429         if ( $sOrd_Remark ) {
00430             oxSession::setVar( 'ordrem', $sOrd_Remark );
00431         }
00432 
00433         // send register eMail
00434         //TODO: move into user
00435         if ( (int) oxConfig::getParameter( 'option' ) == 3 ) {
00436             $oxEMail = oxNew( 'oxemail' );
00437             $oxEMail->sendRegisterEmail( $oUser );
00438         }
00439 
00440         // new registered
00441         $this->_blIsNewUser = true;
00442 
00443         return 'payment';
00444     }
00445 
00451     public function registerUser()
00452     {
00453         // checking if "open address area" button was clicked
00454         if ( $blSetup = $this->_setupDelAddress() ) {
00455             return;
00456         }
00457 
00458         // registered new user ?
00459         if ( $this->createuser()!= false && $this->_blIsNewUser ) {
00460             // #1672 R
00461             $this->getUser()->addToGroup( 'oxidnotyetordered' );
00462 
00463             if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
00464                 return 'register?success=1';
00465             } else {
00466                 return 'register?success=1&newslettererror=4';
00467             }
00468         } else { // problems with registration ...
00469             $this->logout();
00470         }
00471     }
00472 
00486     protected function _changeUser_noRedirect( )
00487     {
00488         // no user ?
00489         $oUser = $this->getUser();
00490         if ( !$oUser ) {
00491             return;
00492         }
00493 
00494         // collecting values to check
00495         $aDelAdress = $this->_getDelAddressData();
00496         // if user company name, user name and additional info has special chars
00497         $aRawVal = array('oxuser__oxcompany', 'oxuser__oxaddinfo', 'oxuser__oxfname',
00498                             'oxuser__oxlname', 'oxuser__oxstreet', 'oxuser__oxstreetnr',
00499                             'oxuser__oxcity', 'oxuser__oxfon', 'oxuser__oxfax',
00500                             'oxuser__oxmobfon', 'oxuser__oxprivfon');
00501 
00502         $aInvAdress = oxConfig::getParameter( 'invadr', $aRawVal );
00503 
00504         $sUserName  = $oUser->oxuser__oxusername->value;
00505         $sPassword  = $sPassword2 = $oUser->oxuser__oxpassword->value;
00506 
00507         try { // testing user input
00508             $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
00509             // assigning to newsletter
00510             if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
00511                 $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
00512             }
00513             $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
00514 
00515         } catch ( oxUserException $oEx ) { // errors in input
00516             // marking error code
00517             //TODO
00518             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00519             return;
00520         } catch(oxInputException $oEx) {
00521             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00522             return;
00523         } catch(oxConnectionException $oEx){
00524              //connection to external resource broken, change message and pass to the view
00525             oxUtilsView::getInstance()->addErrorToDisplay($oEx, false, true);
00526             return;
00527         }
00528 
00529 
00530         // order remark
00531         $sOrd_Remark = oxConfig::getParameter( 'order_remark' );
00532         if ( $sOrd_Remark ) {
00533             oxSession::setVar( 'ordrem', $sOrd_Remark );
00534         }
00535 
00536         if ( $oBasket = $this->getSession()->getBasket() ) {
00537             $oBasket->onUpdate();
00538         }
00539         return true;
00540     }
00541 
00548     protected function _getDelAddressData()
00549     {
00550         // if user company name, user name and additional info has special chars
00551         $aRawVal = array('oxaddress__oxcompany', 'oxaddress__oxaddinfo', 'oxaddress__oxfname', 'oxaddress__oxlname', 'oxaddress__oxcity');
00552         $aDelAdress = $aDeladr = oxConfig::getParameter( 'deladr', $aRawVal );
00553 
00554         if ( is_array( $aDeladr ) ) {
00555             // checking if data is filled
00556             if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
00557                 unset( $aDeladr['oxaddress__oxsal'] );
00558             }
00559             if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
00560                 // resetting to avoid empty records
00561                 $aDelAdress = array();
00562             }
00563         }
00564         return $aDelAdress;
00565     }
00566 
00572     protected function _getLogoutLink()
00573     {
00574         $myConfig = $this->getConfig();
00575         $sLogoutLink = $myConfig->getShopSecureHomeUrl();
00576         if ( $myConfig->isSsl() ) {
00577             $sLogoutLink = $myConfig->getShopHomeUrl();
00578         }
00579         $sLogoutLink .= 'cl='.oxConfig::getParameter('cl').$this->_oParent->getDynUrlParams();
00580         if ( $sParam = oxConfig::getParameter('anid') ) {
00581             $sLogoutLink .= '&amp;anid='.$sParam;
00582         }
00583         if ( $sParam = oxConfig::getParameter('cnid') ) {
00584             $sLogoutLink .= '&amp;cnid='.$sParam;
00585         }
00586         if ( $sParam = oxConfig::getParameter('mnid') ) {
00587             $sLogoutLink .= '&amp;mnid='.$sParam;
00588         }
00589         if ( $sParam = oxConfig::getParameter('tpl') ) {
00590             $sLogoutLink .= '&amp;tpl='.$sParam;
00591         }
00592         return $sLogoutLink.'&amp;fnc=logout';
00593     }
00594 
00604     protected function _setupDelAddress()
00605     {
00606         $blShowIt = false;
00607         $blShowShipAddress = $blSessShowAddress = (int) oxSession::getVar( 'blshowshipaddress' );
00608 
00609         // user clicked on button to hide
00610         if ( $blHideAddress = oxConfig::getParameter( 'blhideshipaddress' ) ) {
00611             $blShowShipAddress = 0;
00612             $blShowIt = true;
00613 
00614             // unsetting delivery address
00615             oxSession::deleteVar( 'deladdrid' );
00616         } else {
00617 
00618             $blShowAddress = oxConfig::getParameter( 'blshowshipaddress' )? 1 : 0;
00619             // user clicked on button to show
00620             if ( $blShowAddress != $blSessShowAddress ) {
00621                 $blShowShipAddress = 1;
00622                 $blShowIt = true;
00623             }
00624         }
00625 
00626         oxSession::setVar( 'blshowshipaddress', $blShowShipAddress );
00627         $this->_oParent->_aViewData['blshowshipaddress'] = $blShowShipAddress;
00628 
00629         return $blShowIt;
00630     }
00631 
00638     public function loginOid()
00639     {
00640         $this->setLoginStatus( USER_LOGIN_FAIL );
00641 
00642         $iOldErrorReproting = error_reporting();
00643         //for 3rd part library disabling our E_STRICT error reporting
00644         error_reporting($iOldErrorReproting & ~E_STRICT);
00645         try {
00646             $oOpenId = oxNew( "oxOpenID" );
00647             $aData = $oOpenId->getOidResponse( $this->_getReturnUrl() );
00648         } catch ( oxUserException $oEx ) {
00649             // for login component send excpetion text to a custom component (if defined)
00650             oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00651         }
00652         error_reporting($iOldErrorReproting);
00653         if ( count( $aData ) < 1 ) {
00654             oxUtils::getInstance()->redirect($this->getConfig()->getShopHomeURL().'cl=register');
00655         }
00656         if ( $aData['email'] ) {
00657             $oUser = oxNew( 'oxuser' );
00658             $oUser->oxuser__oxusername = new oxField($aData['email'], oxField::T_RAW);
00659 
00660             // if such user does not exist - creating it
00661             if ( !$oUser->exists() ) {
00662                 $oUser->oxuser__oxpassword = new oxField($oUser->getOpenIdPassword(), oxField::T_RAW);
00663                 $oUser->oxuser__oxactive   = new oxField(1, oxField::T_RAW);
00664                 $oUser->oxuser__oxrights   = new oxField('user', oxField::T_RAW);
00665                 $oUser->oxuser__oxshopid   = new oxField($this->getConfig()->getShopId(), oxField::T_RAW);
00666                 list ($sFName, $sLName)    = explode(' ', $aData['fullname']);
00667                 $oUser->oxuser__oxfname    = new oxField($sFName, oxField::T_RAW);
00668                 $oUser->oxuser__oxlname    = new oxField($sLName, oxField::T_RAW);
00669 
00670                 $oUser->oxuser__oxsal      = new oxField($this->_getUserTitle($aData['gender']), oxField::T_RAW);
00671                 $oUser->oxuser__oxisopenid = new oxField(1, oxField::T_RAW);
00672                 if ( $sCountryId = $oUser->getUserCountryId( $aData['country'] ) ) {
00673                     $oUser->oxuser__oxcountryid = new oxField( $sCountryId, oxField::T_RAW );
00674                 }
00675                 if ( $aData['postcode'] ) {
00676                     $oUser->oxuser__oxzip = new oxField( $aData['postcode'], oxField::T_RAW );
00677                 }
00678                 $oUser->save();
00679             } else {
00680                 $oUser->load( $oUser->getId() );
00681                 //if existing user loggins first time with openid
00682                 if ( $oUser->oxuser__oxisopenid->value == 0 ) {
00683                     if ( !$oUser->oxuser__oxpassword->value ) {
00684                         $oUser->oxuser__oxisopenid = new oxField(1, oxField::T_RAW);
00685                         $oUser->oxuser__oxpassword = new oxField($oUser->getOpenIdPassword(), oxField::T_RAW);
00686                     } else {
00687                         $oUser->oxuser__oxisopenid = new oxField(2, oxField::T_RAW);
00688                     }
00689                     $oUser->save();
00690                 }
00691             }
00692 
00693             try {
00694                 $oUser->openIdLogin( $oUser->oxuser__oxusername->value );
00695                 $this->setLoginStatus( USER_LOGIN_SUCCESS );
00696             } catch ( oxUserException $oEx ) {
00697                 // for login component send excpetion text to a custom component (if defined)
00698                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, true );
00699             }
00700 
00701             // finalizing ..
00702             $this->_afterLogin( $oUser );
00703             $this->getParent()->setFncName( null );
00704             oxUtils::getInstance()->redirect($this->getParent()->getLink());
00705         }
00706     }
00707 
00715     protected function _getUserTitle( $sGender )
00716     {
00717         if ( $sGender == "F" ) {
00718             return oxLang::getInstance()->translateString( "ACCOUNT_USER_MRS" );
00719         } else {
00720             return oxLang::getInstance()->translateString( "ACCOUNT_USER_MR" );
00721         }
00722     }
00723 
00729     protected function _getReturnUrl()
00730     {
00731         $this->getParent()->setFncName( 'loginOid' );
00732         $sReturnUrl = str_replace( '&amp;', '&', $this->getParent()->getLink() );
00733         if ( !strpos( $sReturnUrl, 'loginOid' ) ) {
00734             if ( !strpos( $sReturnUrl, 'loginOid' ) ) {
00735                 $sReturnUrl = $sReturnUrl . "&fnc=loginOid";
00736             } else {
00737                 $sReturnUrl = $sReturnUrl . "?fnc=loginOid";
00738             }
00739         }
00740         return $sReturnUrl;
00741     }
00742 
00750     public function setLoginStatus( $iStatus )
00751     {
00752         $this->_iLoginStatus = $iStatus;
00753     }
00754 
00763     public function getLoginStatus()
00764     {
00765         return $this->_iLoginStatus;
00766     }
00767 }

Generated on Mon Oct 26 20:07:20 2009 for OXID eShop CE by  doxygen 1.5.5