OXID eShop CE  4.8.10
 All Classes Files Functions Variables Pages
oxcmp_user.php
Go to the documentation of this file.
1 <?php
2 
3 // defining login/logout states
4 define( 'USER_LOGIN_SUCCESS', 1 );
5 define( 'USER_LOGIN_FAIL', 2 );
6 define( 'USER_LOGOUT', 3 );
7 
13 class oxcmp_user extends oxView
14 {
19  protected $_blIsNewUser = false;
20 
25  protected $_blIsComponent = true;
26 
31  protected $_blNewsSubscriptionStatus = null;
32 
40  protected $_iLoginStatus = null;
41 
47  protected $_sTermsVer = null;
48 
54  protected $_aAllowedClasses = array(
55  'register',
56  'forgotpwd',
57  'content',
58  'account',
59  'clearcookies',
60  'oxwServiceMenu',
61  );
67  protected $_blActiveLogin = false;
68 
76  protected function _setActiveLogin( $blActiveLogin )
77  {
78  $this->_blActiveLogin = $blActiveLogin;
79  }
80 
86  protected function _getActiveLogin()
87  {
88  return $this->_blActiveLogin;
89  }
90 
100  public function init()
101  {
102  // saving show/hide delivery address state
103  $blShow = oxConfig::getParameter( 'blshowshipaddress' );
104  if (!isset($blShow)) {
105  $blShow = oxSession::getVar( 'blshowshipaddress' );
106  }
107 
108  oxSession::setVar( 'blshowshipaddress', $blShow );
109 
110  // load session user
111  $this->_loadSessionUser();
112  if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) ) {
113  // get invitor ID
114  $this->getInvitor();
115  $this->setRecipient();
116  }
117 
118  // dyn_group feature: if you specify a groupid in URL the user
119  // will automatically be added to this group later
120  if ( $sDynGoup = oxConfig::getParameter( 'dgr' ) ) {
121  oxSession::setVar( 'dgr', $sDynGoup );
122  }
123 
124  parent::init();
125  }
126 
136  public function render()
137  {
138  // checks if private sales allows further tasks
139  $this->_checkPsState();
140 
141  parent::render();
142 
143  return $this->getUser();
144  }
145 
156  protected function _checkPsState()
157  {
158  $oConfig = $this->getConfig();
159  if ( $this->getParent()->isEnabledPrivateSales() ) {
160  // load session user
161  $oUser = $this->getUser();
162  $sClass = $this->getParent()->getClassName();
163 
164  // no session user
165  if ( !$oUser && !in_array( $sClass, $this->_aAllowedClasses ) ) {
166  oxRegistry::getUtils()->redirect( $oConfig->getShopHomeURL() . 'cl=account', false, 302 );
167  }
168 
169  if ( $oUser && !$oUser->isTermsAccepted() && !in_array( $sClass, $this->_aAllowedClasses ) ) {
170  oxRegistry::getUtils()->redirect( $oConfig->getShopHomeURL() . 'cl=account&term=1', false, 302 );
171  }
172  }
173  }
174 
180  protected function _loadSessionUser()
181  {
182  $myConfig = $this->getConfig();
183  $oUser = $this->getUser();
184 
185  // no session user
186  if ( !$oUser ) {
187  return;
188  }
189 
190  // this user is blocked, deny him
191  if ( $oUser->inGroup( 'oxidblocked' ) ) {
192  oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL() . 'cl=content&tpl=user_blocked.tpl', true, 302 );
193  }
194 
195  // TODO: move this to a proper place
196  if ( $oUser->isLoadedFromCookie() && !$myConfig->getConfigParam( 'blPerfNoBasketSaving' )) {
197 
198  if ( $oBasket = $this->getSession()->getBasket() ) {
199  $oBasket->load();
200  $oBasket->onUpdate();
201  }
202  }
203  }
204 
218  public function login()
219  {
220  $sUser = oxConfig::getParameter( 'lgn_usr' );
221  $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
222  $sCookie = oxConfig::getParameter( 'lgn_cook' );
223  //$blFbLogin = oxConfig::getParameter( 'fblogin' );
224 
226 
227  // trying to login user
228  try {
229  $oUser = oxNew( 'oxuser' );
230  $oUser->login( $sUser, $sPassword, $sCookie );
232  } catch ( oxUserException $oEx ) {
233  // for login component send excpetion text to a custom component (if defined)
234  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true, '', false );
235  return 'user';
236  } catch( oxCookieException $oEx ){
237  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx );
238  return 'user';
239  }
240  // finalizing ..
241  return $this->_afterLogin( $oUser );
242  }
243 
261  protected function _afterLogin( $oUser )
262  {
263  $oSession = $this->getSession();
264 
265  // generating new session id after login
266  if ( $this->getLoginStatus() === USER_LOGIN_SUCCESS ) {
267  $oSession->regenerateSessionId();
268  }
269 
270  $myConfig = $this->getConfig();
271 
272  // this user is blocked, deny him
273  if ( $oUser->inGroup( 'oxidblocked' ) ) {
274  oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL().'cl=content&tpl=user_blocked.tpl', true, 302 );
275  }
276 
277  // adding to dyn group
278  $oUser->addDynGroup(oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ));
279 
280  // recalc basket
281  if ( $oBasket = $oSession->getBasket() ) {
282  $oBasket->onUpdate();
283  }
284 
285 
286  return 'payment';
287  }
288 
295  public function login_noredirect()
296  {
297  $blAgb = oxConfig::getParameter( 'ord_agb' );
298  $oConfig = $this->getConfig();
299  if ( $this->getParent()->isEnabledPrivateSales() && $blAgb !== null && ( $oUser = $this->getUser() ) ) {
300  if ( $blAgb ) {
301  $oUser->acceptTerms();
302  }
303  } else {
304  $this->login();
305 
306  if ( !$this->isAdmin() && !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' )) {
307  //load basket from the database
308  try {
309  if ( $oBasket = $this->getSession()->getBasket() ) {
310  $oBasket->load();
311  }
312  } catch ( Exception $oE ) {
313  //just ignore it
314  }
315  }
316 
317 
318  }
319  }
320 
327  public function login_updateFbId()
328  {
329  $this->login();
330 
331  if ( $oUser = $this->getUser() ) {
332  //updating user Facebook ID
333  if ( $oUser->updateFbId() ) {
334  oxSession::setVar( '_blFbUserIdUpdated', true );
335  }
336  }
337  }
338 
347  protected function _afterLogout()
348  {
349  oxSession::deleteVar( 'paymentid' );
350  oxSession::deleteVar( 'sShipSet' );
351  oxSession::deleteVar( 'deladrid' );
352  oxSession::deleteVar( 'dynvalue' );
353 
354  // resetting & recalc basket
355  if ( ( $oBasket = $this->getSession()->getBasket() ) ) {
356  $oBasket->resetUserInfo();
357  $oBasket->onUpdate();
358  }
359  }
360 
369  public function logout()
370  {
371  $myConfig = $this->getConfig();
372  $oUser = oxNew( 'oxuser' );
373 
374  if ( $oUser->logout() ) {
375 
376  $this->setLoginStatus( USER_LOGOUT );
377 
378  // finalizing ..
379  $this->_afterLogout();
380 
381 
382  if ( $this->getParent()->isEnabledPrivateSales() ) {
383  return 'account';
384  }
385 
386  // redirecting if user logs out in SSL mode
387  if ( oxConfig::getParameter('redirect') && $myConfig->getConfigParam( 'sSSLShopURL' ) ) {
388  oxRegistry::getUtils()->redirect( $this->_getLogoutLink());
389  }
390  }
391  }
392 
402  public function changeUser( )
403  {
404  $blUserRegistered = $this->_changeUser_noRedirect();
405 
406  if ( $blUserRegistered === true ) {
407  return 'payment';
408  } else {
409  return $blUserRegistered;
410  }
411  }
412 
419  public function changeuser_testvalues()
420  {
421  // skip updating user info if this is just form reload
422  // on selecting delivery address
423  // We do redirect only on success not to loose errors.
424 
425  if ( $this->_changeUser_noRedirect() ) {
426  return 'account_user';
427  }
428  }
429 
451  public function createUser()
452  {
453  $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
454  $this->_setActiveLogin( $blActiveLogin );
455 
456  $myConfig = $this->getConfig();
457  if ( $blActiveLogin && !oxConfig::getParameter( 'ord_agb' ) && $myConfig->getConfigParam( 'blConfirmAGB' ) ) {
458  oxRegistry::get("oxUtilsView")->addErrorToDisplay( 'READ_AND_CONFIRM_TERMS', false, true );
459  return;
460  }
461 
462  $myUtils = oxRegistry::getUtils();
463 
464  // collecting values to check
465  $sUser = oxConfig::getParameter( 'lgn_usr' );
466 
467  // first pass
468  $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
469 
470  // second pass
471  $sPassword2 = oxConfig::getParameter( 'lgn_pwd2', true );
472 
473  $aInvAdress = oxConfig::getParameter( 'invadr', true );
474  $aDelAdress = $this->_getDelAddressData();
475 
476  $oUser = oxNew( 'oxuser' );
477 
478  try {
479 
480  $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
481 
482  $iActState = $blActiveLogin ? 0 : 1;
483 
484  // setting values
485  $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
486  $oUser->setPassword( $sPassword );
487  $oUser->oxuser__oxactive = new oxField( $iActState, oxField::T_RAW);
488 
489  // used for checking if user email currently subscribed
490  $iSubscriptionStatus = $oUser->getNewsSubscription()->getOptInStatus();
491 
492  $oUser->createUser();
493  $oUser->load($oUser->getId());
494  $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
495 
496  if ( $blActiveLogin ) {
497  // accepting terms..
498  $oUser->acceptTerms();
499  }
500 
501  $sUserId = oxSession::getVar( "su" );
502  $sRecEmail = oxSession::getVar( "re" );
503  if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId && $sRecEmail ) {
504  // setting registration credit points..
505  $oUser->setCreditPointsForRegistrant( $sUserId, $sRecEmail );
506  }
507 
508  // assigning to newsletter
509  $blOptin = oxRegistry::getConfig()->getRequestParameter( 'blnewssubscribed' );
510  if ( $blOptin && $iSubscriptionStatus == 1 ) {
511  // if user was assigned to newsletter and is creating account with newsletter checked, don't require confirm
512  $oUser->getNewsSubscription()->setOptInStatus(1);
513  $oUser->addToGroup( 'oxidnewsletter' );
514  $this->_blNewsSubscriptionStatus = 1;
515  } else {
516  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
517  }
518 
519  $oUser->addToGroup( 'oxidnotyetordered' );
520  $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
521  $oUser->logout();
522 
523  } catch ( oxUserException $oEx ) {
524  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
525  return false;
526  } catch( oxInputException $oEx ){
527  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
528  return false;
529  } catch( oxConnectionException $oEx ){
530  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
531  return false;
532  }
533 
534  $this->_setOrderRemark( $oUser );
535 
536  // send register eMail
537  //TODO: move into user
538  $this->_sendRegistrationEmail( $oUser );
539 
540  // new registered
541  $this->_blIsNewUser = true;
542 
543  $sAction = 'payment?new_user=1&success=1';
544  if ($this->_blNewsSubscriptionStatus !== null && !$this->_blNewsSubscriptionStatus) {
545  $sAction = 'payment?new_user=1&success=1&newslettererror=4';
546  }
547 
548  return $sAction;
549  }
550 
556  public function registerUser()
557  {
558  // registered new user ?
559  if ( $this->createuser()!= false && $this->_blIsNewUser ) {
560  if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
561  return 'register?success=1';
562  } else {
563  return 'register?success=1&newslettererror=4';
564  }
565  } else {
566  // problems with registration ...
567  $this->logout();
568  }
569  }
570 
584  protected function _changeUser_noRedirect( )
585  {
586  if (!$this->getSession()->checkSessionChallenge()) {
587  return;
588  }
589 
590  // no user ?
591  $oUser = $this->getUser();
592  if ( !$oUser ) {
593  return;
594  }
595 
596  // collecting values to check
597  $aDelAdress = $this->_getDelAddressData();
598 
599  // if user company name, user name and additional info has special chars
600  $aInvAdress = oxConfig::getParameter( 'invadr', true );
601 
602  $sUserName = $oUser->oxuser__oxusername->value;
603  $sPassword = $sPassword2 = $oUser->oxuser__oxpassword->value;
604 
605  try { // testing user input
606  $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
607  // assigning to newsletter
608  if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
609  $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
610  }
611  // check if email address changed, if so, force check news subscription settings.
612  $blForceCheckOptIn = ( $aInvAdress['oxuser__oxusername'] !== null && $aInvAdress['oxuser__oxusername'] !== $sUserName );
613  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ), $blForceCheckOptIn );
614 
615  } catch ( oxUserException $oEx ) { // errors in input
616  // marking error code
617  //TODO
618  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
619  return;
620  } catch(oxInputException $oEx) {
621  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
622  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'input_not_all_fields');
623  return;
624  } catch(oxConnectionException $oEx){
625  //connection to external resource broken, change message and pass to the view
626  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
627  return;
628  }
629 
630 
631  // order remark
632  $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
633 
634  if ( $sOrderRemark ) {
635  oxSession::setVar( 'ordrem', $sOrderRemark );
636  } else {
637  oxSession::deleteVar( 'ordrem' );
638  }
639 
640  if ( $oBasket = $this->getSession()->getBasket() ) {
641  $oBasket->onUpdate();
642  }
643  return true;
644  }
645 
652  protected function _getDelAddressData()
653  {
654  // if user company name, user name and additional info has special chars
655  $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', true ) : array();
656 
657  if ( is_array( $aDeladr ) ) {
658  // checking if data is filled
659  if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
660  unset( $aDeladr['oxaddress__oxsal'] );
661  }
662  if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
663  // resetting to avoid empty records
664  $aDelAdress = array();
665  }
666  }
667  return $aDelAdress;
668  }
669 
675  protected function _getLogoutLink()
676  {
677  $oConfig = $this->getConfig();
678 
679  $sLogoutLink = $oConfig->isSsl()? $oConfig->getShopSecureHomeUrl() : $oConfig->getShopHomeUrl();
680  $sLogoutLink .= 'cl='.$oConfig->getRequestParameter('cl').$this->getParent()->getDynUrlParams();
681  if ( $sParam = $oConfig->getRequestParameter('anid') ) {
682  $sLogoutLink .= '&amp;anid='.$sParam;
683  }
684  if ( $sParam = $oConfig->getRequestParameter('cnid') ) {
685  $sLogoutLink .= '&amp;cnid='.$sParam;
686  }
687  if ( $sParam = $oConfig->getRequestParameter('mnid') ) {
688  $sLogoutLink .= '&amp;mnid='.$sParam;
689  }
690  if ( $sParam = $oConfig->getRequestParameter('tpl') ) {
691  $sLogoutLink .= '&amp;tpl='.$sParam;
692  }
693  if ( $sParam = $oConfig->getRequestParameter('oxloadid') ) {
694  $sLogoutLink .= '&amp;oxloadid='.$sParam;
695  }
696  if ( $sParam = $oConfig->getRequestParameter('recommid') ) {
697  $sLogoutLink .= '&amp;recommid='.$sParam;
698  }
699  return $sLogoutLink.'&amp;fnc=logout';
700  }
701 
709  public function setLoginStatus( $iStatus )
710  {
711  $this->_iLoginStatus = $iStatus;
712  }
713 
722  public function getLoginStatus()
723  {
724  return $this->_iLoginStatus;
725  }
726 
732  public function getInvitor()
733  {
734  $sSu = oxSession::getVar( 'su' );
735  if ( !$sSu && ( $sSuNew = oxConfig::getParameter( 'su' ) ) ) {
736  oxSession::setVar( 'su', $sSuNew );
737  }
738  }
739 
745  public function setRecipient()
746  {
747  $sRe = oxSession::getVar( 're' );
748  if ( !$sRe && ( $sReNew = oxConfig::getParameter( 're' ) ) ) {
749  oxSession::setVar( 're', $sReNew );
750  }
751  }
752 
760  public function _setOrderRemark( $oUser )
761  {
762  $blActiveLogin = $this->_getActiveLogin();
763  if ( !$blActiveLogin ) {
764 
765  oxRegistry::getSession()->setVariable( 'usr', $oUser->getId() );
766  $this->_afterLogin( $oUser );
767 
768 
769  // order remark
770  //V #427: order remark for new users
771  $sOrderRemark = oxRegistry::getConfig()->getRequestParameter( 'order_remark', true );
772  if ( $sOrderRemark ) {
773  oxRegistry::getSession()->setVariable( 'ordrem', $sOrderRemark );
774  }
775  }
776  }
777 
785  public function _sendRegistrationEmail( $oUser )
786  {
787  $blActiveLogin = $this->_getActiveLogin();
788  if ( (int) oxRegistry::getConfig()->getRequestParameter( 'option' ) == 3 ) {
789  $oxEMail = oxNew( 'oxemail' );
790  if ( $blActiveLogin ) {
791  $oxEMail->sendRegisterConfirmEmail( $oUser );
792  } else {
793  $oxEMail->sendRegisterEmail( $oUser );
794  }
795  }
796  }
797 }