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

Generated by  doxygen 1.6.2