OXID eShop CE  4.8.12
 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  $aInvAdress = $this->cleanBillingAddress($aInvAdress);
477 
478  $aDelAdress = $this->_getDelAddressData();
479  $aDelAdress = $this->cleanDeliveryAddress($aDelAdress);
480 
481  $oUser = oxNew( 'oxuser' );
482 
483  try {
484 
485  $oUser->checkValues( $sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
486 
487  $iActState = $blActiveLogin ? 0 : 1;
488 
489  // setting values
490  $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
491  $oUser->setPassword( $sPassword );
492  $oUser->oxuser__oxactive = new oxField( $iActState, oxField::T_RAW);
493 
494  // used for checking if user email currently subscribed
495  $iSubscriptionStatus = $oUser->getNewsSubscription()->getOptInStatus();
496 
497  $oUser->createUser();
498  $oUser->load($oUser->getId());
499  $oUser->changeUserData( $oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress );
500 
501  if ( $blActiveLogin ) {
502  // accepting terms..
503  $oUser->acceptTerms();
504  }
505 
506  $sUserId = oxSession::getVar( "su" );
507  $sRecEmail = oxSession::getVar( "re" );
508  if ( $this->getConfig()->getConfigParam( 'blInvitationsEnabled' ) && $sUserId && $sRecEmail ) {
509  // setting registration credit points..
510  $oUser->setCreditPointsForRegistrant( $sUserId, $sRecEmail );
511  }
512 
513  // assigning to newsletter
514  $blOptin = oxRegistry::getConfig()->getRequestParameter( 'blnewssubscribed' );
515  if ( $blOptin && $iSubscriptionStatus == 1 ) {
516  // if user was assigned to newsletter and is creating account with newsletter checked, don't require confirm
517  $oUser->getNewsSubscription()->setOptInStatus(1);
518  $oUser->addToGroup( 'oxidnewsletter' );
519  $this->_blNewsSubscriptionStatus = 1;
520  } else {
521  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ) );
522  }
523 
524  $oUser->addToGroup( 'oxidnotyetordered' );
525  $oUser->addDynGroup( oxSession::getVar( 'dgr' ), $myConfig->getConfigParam( 'aDeniedDynGroups' ) );
526  $oUser->logout();
527 
528  } catch ( oxUserException $oEx ) {
529  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
530  return false;
531  } catch( oxInputException $oEx ){
532  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
533  return false;
534  } catch( oxConnectionException $oEx ){
535  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true );
536  return false;
537  }
538 
539  $this->_setOrderRemark( $oUser );
540 
541  // send register eMail
542  //TODO: move into user
543  $this->_sendRegistrationEmail( $oUser );
544 
545  // new registered
546  $this->_blIsNewUser = true;
547 
548  $sAction = 'payment?new_user=1&success=1';
549  if ($this->_blNewsSubscriptionStatus !== null && !$this->_blNewsSubscriptionStatus) {
550  $sAction = 'payment?new_user=1&success=1&newslettererror=4';
551  }
552 
553  return $sAction;
554  }
555 
561  public function registerUser()
562  {
563  // registered new user ?
564  if ( $this->createuser()!= false && $this->_blIsNewUser ) {
565  if ( $this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus ) {
566  return 'register?success=1';
567  } else {
568  return 'register?success=1&newslettererror=4';
569  }
570  } else {
571  // problems with registration ...
572  $this->logout();
573  }
574  }
575 
589  protected function _changeUser_noRedirect( )
590  {
591  if (!$this->getSession()->checkSessionChallenge()) {
592  return;
593  }
594 
595  // no user ?
596  $oUser = $this->getUser();
597  if ( !$oUser ) {
598  return;
599  }
600 
601  // collecting values to check
602  $aDelAdress = $this->_getDelAddressData();
603  $aDelAdress = $this->cleanDeliveryAddress($aDelAdress);
604 
605  // if user company name, user name and additional info has special chars
606  $aInvAdress = oxConfig::getParameter( 'invadr', true );
607  $aInvAdress = $this->cleanBillingAddress($aInvAdress);
608 
609  $sUserName = $oUser->oxuser__oxusername->value;
610  $sPassword = $sPassword2 = $oUser->oxuser__oxpassword->value;
611 
612  try { // testing user input
613  $oUser->changeUserData( $sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress );
614  // assigning to newsletter
615  if (($blOptin = oxConfig::getParameter( 'blnewssubscribed' )) === null) {
616  $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
617  }
618  // check if email address changed, if so, force check news subscription settings.
619  $blForceCheckOptIn = ( $aInvAdress['oxuser__oxusername'] !== null && $aInvAdress['oxuser__oxusername'] !== $sUserName );
620  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription( $blOptin, $this->getConfig()->getConfigParam( 'blOrderOptInEmail' ), $blForceCheckOptIn );
621 
622  } catch ( oxUserException $oEx ) { // errors in input
623  // marking error code
624  //TODO
625  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
626  return;
627  } catch(oxInputException $oEx) {
628  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
629  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'input_not_all_fields');
630  return;
631  } catch(oxConnectionException $oEx){
632  //connection to external resource broken, change message and pass to the view
633  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
634  return;
635  }
636 
637 
638  // order remark
639  $sOrderRemark = oxConfig::getParameter( 'order_remark', true );
640 
641  if ( $sOrderRemark ) {
642  oxSession::setVar( 'ordrem', $sOrderRemark );
643  } else {
644  oxSession::deleteVar( 'ordrem' );
645  }
646 
647  if ( $oBasket = $this->getSession()->getBasket() ) {
648  $oBasket->onUpdate();
649  }
650  return true;
651  }
652 
660  private function cleanBillingAddress($aBillingAddress)
661  {
662  if (is_array($aBillingAddress)) {
663  $skipFields = array('oxuser__oxid', 'oxid', 'oxuser__oxpoints', 'oxpoints', 'oxuser__oxboni', 'oxboni');
664  $aBillingAddress = array_diff_key($aBillingAddress, array_flip($skipFields));
665  }
666  return $aBillingAddress;
667  }
668 
676  private function cleanDeliveryAddress($aDeliveryAddress)
677  {
678  if (is_array($aDeliveryAddress)) {
679  $skipFields = array('oxaddress__oxid', 'oxid', 'oxaddress__oxuserid', 'oxuserid', 'oxaddress__oxaddressuserid', 'oxaddressuserid');
680  $aDeliveryAddress = array_diff_key($aDeliveryAddress, array_flip($skipFields));
681  }
682  return $aDeliveryAddress;
683  }
684 
691  protected function _getDelAddressData()
692  {
693  // if user company name, user name and additional info has special chars
694  $aDelAdress = $aDeladr = (oxConfig::getParameter( 'blshowshipaddress' ) || oxSession::getVar( 'blshowshipaddress' )) ? oxConfig::getParameter( 'deladr', true ) : array();
695 
696  if ( is_array( $aDeladr ) ) {
697  // checking if data is filled
698  if ( isset( $aDeladr['oxaddress__oxsal'] ) ) {
699  unset( $aDeladr['oxaddress__oxsal'] );
700  }
701  if ( !count( $aDeladr ) || implode( '', $aDeladr ) == '' ) {
702  // resetting to avoid empty records
703  $aDelAdress = array();
704  }
705  }
706  return $aDelAdress;
707  }
708 
714  protected function _getLogoutLink()
715  {
716  $oConfig = $this->getConfig();
717 
718  $sLogoutLink = $oConfig->isSsl()? $oConfig->getShopSecureHomeUrl() : $oConfig->getShopHomeUrl();
719  $sLogoutLink .= 'cl='.$oConfig->getRequestParameter('cl').$this->getParent()->getDynUrlParams();
720  if ( $sParam = $oConfig->getRequestParameter('anid') ) {
721  $sLogoutLink .= '&amp;anid='.$sParam;
722  }
723  if ( $sParam = $oConfig->getRequestParameter('cnid') ) {
724  $sLogoutLink .= '&amp;cnid='.$sParam;
725  }
726  if ( $sParam = $oConfig->getRequestParameter('mnid') ) {
727  $sLogoutLink .= '&amp;mnid='.$sParam;
728  }
729  if ( $sParam = $oConfig->getRequestParameter('tpl') ) {
730  $sLogoutLink .= '&amp;tpl='.$sParam;
731  }
732  if ( $sParam = $oConfig->getRequestParameter('oxloadid') ) {
733  $sLogoutLink .= '&amp;oxloadid='.$sParam;
734  }
735  if ( $sParam = $oConfig->getRequestParameter('recommid') ) {
736  $sLogoutLink .= '&amp;recommid='.$sParam;
737  }
738  return $sLogoutLink.'&amp;fnc=logout';
739  }
740 
748  public function setLoginStatus( $iStatus )
749  {
750  $this->_iLoginStatus = $iStatus;
751  }
752 
761  public function getLoginStatus()
762  {
763  return $this->_iLoginStatus;
764  }
765 
771  public function getInvitor()
772  {
773  $sSu = oxSession::getVar( 'su' );
774  if ( !$sSu && ( $sSuNew = oxConfig::getParameter( 'su' ) ) ) {
775  oxSession::setVar( 'su', $sSuNew );
776  }
777  }
778 
784  public function setRecipient()
785  {
786  $sRe = oxSession::getVar( 're' );
787  if ( !$sRe && ( $sReNew = oxConfig::getParameter( 're' ) ) ) {
788  oxSession::setVar( 're', $sReNew );
789  }
790  }
791 
799  public function _setOrderRemark( $oUser )
800  {
801  $blActiveLogin = $this->_getActiveLogin();
802  if ( !$blActiveLogin ) {
803 
804  oxRegistry::getSession()->setVariable( 'usr', $oUser->getId() );
805  $this->_afterLogin( $oUser );
806 
807 
808  // order remark
809  //V #427: order remark for new users
810  $sOrderRemark = oxRegistry::getConfig()->getRequestParameter( 'order_remark', true );
811  if ( $sOrderRemark ) {
812  oxRegistry::getSession()->setVariable( 'ordrem', $sOrderRemark );
813  }
814  }
815  }
816 
824  public function _sendRegistrationEmail( $oUser )
825  {
826  $blActiveLogin = $this->_getActiveLogin();
827  if ( (int) oxRegistry::getConfig()->getRequestParameter( 'option' ) == 3 ) {
828  $oxEMail = oxNew( 'oxemail' );
829  if ( $blActiveLogin ) {
830  $oxEMail->sendRegisterConfirmEmail( $oUser );
831  } else {
832  $oxEMail->sendRegisterEmail( $oUser );
833  }
834  }
835  }
836 }