OXID eShop CE  4.8.11
 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  oxRegistry::getSession()->delBasket();
361  }
362 
371  public function logout()
372  {
373  $myConfig = $this->getConfig();
374  $oUser = oxNew( 'oxuser' );
375 
376  if ( $oUser->logout() ) {
377 
378  $this->setLoginStatus( USER_LOGOUT );
379 
380  // finalizing ..
381  $this->_afterLogout();
382 
383 
384  if ( $this->getParent()->isEnabledPrivateSales() ) {
385  return 'account';
386  }
387 
388  // redirecting if user logs out in SSL mode
389  if ( oxConfig::getParameter('redirect') && $myConfig->getConfigParam( 'sSSLShopURL' ) ) {
390  oxRegistry::getUtils()->redirect( $this->_getLogoutLink());
391  }
392  }
393  }
394 
404  public function changeUser( )
405  {
406  $blUserRegistered = $this->_changeUser_noRedirect();
407 
408  if ( $blUserRegistered === true ) {
409  return 'payment';
410  } else {
411  return $blUserRegistered;
412  }
413  }
414 
421  public function changeuser_testvalues()
422  {
423  // skip updating user info if this is just form reload
424  // on selecting delivery address
425  // We do redirect only on success not to loose errors.
426 
427  if ( $this->_changeUser_noRedirect() ) {
428  return 'account_user';
429  }
430  }
431 
453  public function createUser()
454  {
455  $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
456  $this->_setActiveLogin( $blActiveLogin );
457 
458  $myConfig = $this->getConfig();
459  if ( $blActiveLogin && !oxConfig::getParameter( 'ord_agb' ) && $myConfig->getConfigParam( 'blConfirmAGB' ) ) {
460  oxRegistry::get("oxUtilsView")->addErrorToDisplay( 'READ_AND_CONFIRM_TERMS', false, true );
461  return;
462  }
463 
464  $myUtils = oxRegistry::getUtils();
465 
466  // collecting values to check
467  $sUser = oxConfig::getParameter( 'lgn_usr' );
468 
469  // first pass
470  $sPassword = oxConfig::getParameter( 'lgn_pwd', true );
471 
472  // second pass
473  $sPassword2 = oxConfig::getParameter( 'lgn_pwd2', true );
474 
475  $aInvAdress = oxConfig::getParameter( 'invadr', true );
476  $aDelAdress = $this->_getDelAddressData();
477 
478  $oUser = oxNew( 'oxuser' );
479 
480  try {
481 
482  $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
483 
484  $iActState = $blActiveLogin ? 0 : 1;
485 
486  // setting values
487  $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
488  $oUser->setPassword( $sPassword );
489  $oUser->oxuser__oxactive = new oxField( $iActState, oxField::T_RAW);
490 
491  // used for checking if user email currently subscribed
492  $iSubscriptionStatus = $oUser->getNewsSubscription()->getOptInStatus();
493 
494  $oUser->createUser();
495  $oUser->load($oUser->getId());
496  $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
497 
498  if ( $blActiveLogin ) {
499  // accepting terms..
500  $oUser->acceptTerms();
501  }
502 
503  $sUserId = oxSession::getVar( "su" );
504  $sRecEmail = oxSession::getVar( "re" );
505  if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId && $sRecEmail ) {
506  // setting registration credit points..
507  $oUser->setCreditPointsForRegistrant( $sUserId, $sRecEmail );
508  }
509 
510  // assigning to newsletter
511  $blOptin = oxRegistry::getConfig()->getRequestParameter( 'blnewssubscribed' );
512  if ( $blOptin && $iSubscriptionStatus == 1 ) {
513  // if user was assigned to newsletter and is creating account with newsletter checked, don't require confirm
514  $oUser->getNewsSubscription()->setOptInStatus(1);
515  $oUser->addToGroup( 'oxidnewsletter' );
516  $this->_blNewsSubscriptionStatus = 1;
517  } else {
518  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
519  }
520 
521  $oUser->addToGroup( 'oxidnotyetordered' );
522  $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
523  $oUser->logout();
524 
525  } catch ( oxUserException $oEx ) {
526  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
527  return false;
528  } catch( oxInputException $oEx ){
529  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
530  return false;
531  } catch( oxConnectionException $oEx ){
532  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
533  return false;
534  }
535 
536  $this->_setOrderRemark( $oUser );
537 
538  // send register eMail
539  //TODO: move into user
540  $this->_sendRegistrationEmail( $oUser );
541 
542  // new registered
543  $this->_blIsNewUser = true;
544 
545  $sAction = 'payment?new_user=1&success=1';
546  if ($this->_blNewsSubscriptionStatus !== null && !$this->_blNewsSubscriptionStatus) {
547  $sAction = 'payment?new_user=1&success=1&newslettererror=4';
548  }
549 
550  return $sAction;
551  }
552 
558  public function registerUser()
559  {
560  // registered new user ?
561  if ( $this->createuser()!= false && $this->_blIsNewUser ) {
562  if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
563  return 'register?success=1';
564  } else {
565  return 'register?success=1&newslettererror=4';
566  }
567  } else {
568  // problems with registration ...
569  $this->logout();
570  }
571  }
572 
586  protected function _changeUser_noRedirect( )
587  {
588  if (!$this->getSession()->checkSessionChallenge()) {
589  return;
590  }
591 
592  // no user ?
593  $oUser = $this->getUser();
594  if ( !$oUser ) {
595  return;
596  }
597 
598  // collecting values to check
599  $aDelAdress = $this->_getDelAddressData();
600 
601  // if user company name, user name and additional info has special chars
602  $aInvAdress = oxConfig::getParameter( 'invadr', true );
603 
604  $sUserName = $oUser->oxuser__oxusername->value;
605  $sPassword = $sPassword2 = $oUser->oxuser__oxpassword->value;
606 
607  try { // testing user input
608  $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
609  // assigning to newsletter
610  if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
611  $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
612  }
613  // check if email address changed, if so, force check news subscription settings.
614  $blForceCheckOptIn = ( $aInvAdress['oxuser__oxusername'] !== null && $aInvAdress['oxuser__oxusername'] !== $sUserName );
615  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ), $blForceCheckOptIn );
616 
617  } catch ( oxUserException $oEx ) { // errors in input
618  // marking error code
619  //TODO
620  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
621  return;
622  } catch(oxInputException $oEx) {
623  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
624  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'input_not_all_fields');
625  return;
626  } catch(oxConnectionException $oEx){
627  //connection to external resource broken, change message and pass to the view
628  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
629  return;
630  }
631 
632 
633  // order remark
634  $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
635 
636  if ( $sOrderRemark ) {
637  oxSession::setVar( 'ordrem', $sOrderRemark );
638  } else {
639  oxSession::deleteVar( 'ordrem' );
640  }
641 
642  if ( $oBasket = $this->getSession()->getBasket() ) {
643  $oBasket->onUpdate();
644  }
645  return true;
646  }
647 
654  protected function _getDelAddressData()
655  {
656  // if user company name, user name and additional info has special chars
657  $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', true ) : array();
658 
659  if ( is_array( $aDeladr ) ) {
660  // checking if data is filled
661  if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
662  unset( $aDeladr['oxaddress__oxsal'] );
663  }
664  if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
665  // resetting to avoid empty records
666  $aDelAdress = array();
667  }
668  }
669  return $aDelAdress;
670  }
671 
677  protected function _getLogoutLink()
678  {
679  $oConfig = $this->getConfig();
680 
681  $sLogoutLink = $oConfig->isSsl()? $oConfig->getShopSecureHomeUrl() : $oConfig->getShopHomeUrl();
682  $sLogoutLink .= 'cl='.$oConfig->getRequestParameter('cl').$this->getParent()->getDynUrlParams();
683  if ( $sParam = $oConfig->getRequestParameter('anid') ) {
684  $sLogoutLink .= '&amp;anid='.$sParam;
685  }
686  if ( $sParam = $oConfig->getRequestParameter('cnid') ) {
687  $sLogoutLink .= '&amp;cnid='.$sParam;
688  }
689  if ( $sParam = $oConfig->getRequestParameter('mnid') ) {
690  $sLogoutLink .= '&amp;mnid='.$sParam;
691  }
692  if ( $sParam = $oConfig->getRequestParameter('tpl') ) {
693  $sLogoutLink .= '&amp;tpl='.$sParam;
694  }
695  if ( $sParam = $oConfig->getRequestParameter('oxloadid') ) {
696  $sLogoutLink .= '&amp;oxloadid='.$sParam;
697  }
698  if ( $sParam = $oConfig->getRequestParameter('recommid') ) {
699  $sLogoutLink .= '&amp;recommid='.$sParam;
700  }
701  return $sLogoutLink.'&amp;fnc=logout';
702  }
703 
711  public function setLoginStatus( $iStatus )
712  {
713  $this->_iLoginStatus = $iStatus;
714  }
715 
724  public function getLoginStatus()
725  {
726  return $this->_iLoginStatus;
727  }
728 
734  public function getInvitor()
735  {
736  $sSu = oxSession::getVar( 'su' );
737  if ( !$sSu && ( $sSuNew = oxConfig::getParameter( 'su' ) ) ) {
738  oxSession::setVar( 'su', $sSuNew );
739  }
740  }
741 
747  public function setRecipient()
748  {
749  $sRe = oxSession::getVar( 're' );
750  if ( !$sRe && ( $sReNew = oxConfig::getParameter( 're' ) ) ) {
751  oxSession::setVar( 're', $sReNew );
752  }
753  }
754 
762  public function _setOrderRemark( $oUser )
763  {
764  $blActiveLogin = $this->_getActiveLogin();
765  if ( !$blActiveLogin ) {
766 
767  oxRegistry::getSession()->setVariable( 'usr', $oUser->getId() );
768  $this->_afterLogin( $oUser );
769 
770 
771  // order remark
772  //V #427: order remark for new users
773  $sOrderRemark = oxRegistry::getConfig()->getRequestParameter( 'order_remark', true );
774  if ( $sOrderRemark ) {
775  oxRegistry::getSession()->setVariable( 'ordrem', $sOrderRemark );
776  }
777  }
778  }
779 
787  public function _sendRegistrationEmail( $oUser )
788  {
789  $blActiveLogin = $this->_getActiveLogin();
790  if ( (int) oxRegistry::getConfig()->getRequestParameter( 'option' ) == 3 ) {
791  $oxEMail = oxNew( 'oxemail' );
792  if ( $blActiveLogin ) {
793  $oxEMail->sendRegisterConfirmEmail( $oUser );
794  } else {
795  $oxEMail->sendRegisterEmail( $oUser );
796  }
797  }
798  }
799 }