OXID eShop CE  4.9.7
 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 
14 class oxcmp_user extends oxView
15 {
16 
22  protected $_blIsNewUser = false;
23 
29  protected $_blIsComponent = true;
30 
36  protected $_blNewsSubscriptionStatus = null;
37 
46  protected $_iLoginStatus = null;
47 
53  protected $_sTermsVer = null;
54 
60  protected $_aAllowedClasses = array(
61  'register',
62  'forgotpwd',
63  'content',
64  'account',
65  'clearcookies',
66  'oxwServiceMenu',
67  );
68 
76  public function init()
77  {
79  $this->_loadSessionUser();
80  $this->_saveInvitor();
81 
82  parent::init();
83  }
84 
91  public function render()
92  {
93  // checks if private sales allows further tasks
94  $this->_checkPsState();
95 
97 
98  return $this->getUser();
99  }
100 
109  protected function _checkPsState()
110  {
111  $oConfig = $this->getConfig();
112  if ($this->getParent()->isEnabledPrivateSales()) {
113  // load session user
114  $oUser = $this->getUser();
115  $sClass = $this->getParent()->getClassName();
116 
117  // no session user
118  if (!$oUser && !in_array($sClass, $this->_aAllowedClasses)) {
119  oxRegistry::getUtils()->redirect($oConfig->getShopHomeURL() . 'cl=account', false, 302);
120  }
121 
122  if ($oUser && !$oUser->isTermsAccepted() && !in_array($sClass, $this->_aAllowedClasses)) {
123  oxRegistry::getUtils()->redirect($oConfig->getShopHomeURL() . 'cl=account&term=1', false, 302);
124  }
125  }
126  }
127 
133  protected function _loadSessionUser()
134  {
135  $myConfig = $this->getConfig();
136  $oUser = $this->getUser();
137 
138  // no session user
139  if (!$oUser) {
140  return;
141  }
142 
143  // this user is blocked, deny him
144  if ($oUser->inGroup('oxidblocked')) {
145  $sUrl = $myConfig->getShopHomeURL() . 'cl=content&tpl=user_blocked.tpl';
146  oxRegistry::getUtils()->redirect($sUrl, true, 302);
147  }
148 
149  // TODO: move this to a proper place
150  if ($oUser->isLoadedFromCookie() && !$myConfig->getConfigParam('blPerfNoBasketSaving')) {
151 
152  if ($oBasket = $this->getSession()->getBasket()) {
153  $oBasket->load();
154  $oBasket->onUpdate();
155  }
156  }
157  }
158 
172  public function login()
173  {
174  $sUser = oxRegistry::getConfig()->getRequestParameter('lgn_usr');
175  $sPassword = oxRegistry::getConfig()->getRequestParameter('lgn_pwd', true);
176  $sCookie = oxRegistry::getConfig()->getRequestParameter('lgn_cook');
177  //$blFbLogin = oxRegistry::getConfig()->getRequestParameter( 'fblogin' );
178 
180 
181  // trying to login user
182  try {
184  $oUser = oxNew('oxuser');
185  $oUser->login($sUser, $sPassword, $sCookie);
187  } catch (oxUserException $oEx) {
188  // for login component send excpetion text to a custom component (if defined)
189  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, '', false);
190 
191  return 'user';
192  } catch (oxCookieException $oEx) {
193  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx);
194 
195  return 'user';
196  }
197 
198  // finalizing ..
199  return $this->_afterLogin($oUser);
200  }
201 
217  protected function _afterLogin($oUser)
218  {
219  $oSession = $this->getSession();
220 
221  // generating new session id after login
222  if ($this->getLoginStatus() === USER_LOGIN_SUCCESS) {
223  $oSession->regenerateSessionId();
224  }
225 
226  $myConfig = $this->getConfig();
227 
228  // this user is blocked, deny him
229  if ($oUser->inGroup('oxidblocked')) {
230  $sUrl = $myConfig->getShopHomeURL() . 'cl=content&tpl=user_blocked.tpl';
231  oxRegistry::getUtils()->redirect($sUrl, true, 302);
232  }
233 
234  // recalc basket
235  if ($oBasket = $oSession->getBasket()) {
236  $oBasket->onUpdate();
237  }
238 
239 
240  return 'payment';
241  }
242 
247  public function login_noredirect()
248  {
249  $blAgb = oxRegistry::getConfig()->getRequestParameter('ord_agb');
250  $oConfig = $this->getConfig();
251  if ($this->getParent()->isEnabledPrivateSales() && $blAgb !== null && ($oUser = $this->getUser())) {
252  if ($blAgb) {
253  $oUser->acceptTerms();
254  }
255  } else {
256  $this->login();
257 
258  if (!$this->isAdmin() && !$this->getConfig()->getConfigParam('blPerfNoBasketSaving')) {
259  //load basket from the database
260  try {
261  if ($oBasket = $this->getSession()->getBasket()) {
262  $oBasket->load();
263  }
264  } catch (Exception $oE) {
265  //just ignore it
266  }
267  }
268 
269 
270  }
271  }
272 
277  public function login_updateFbId()
278  {
279  $this->login();
280 
281  if ($oUser = $this->getUser()) {
282  //updating user Facebook ID
283  if ($oUser->updateFbId()) {
284  oxRegistry::getSession()->setVariable('_blFbUserIdUpdated', true);
285  }
286  }
287  }
288 
295  protected function _afterLogout()
296  {
297  oxRegistry::getSession()->deleteVariable('paymentid');
298  oxRegistry::getSession()->deleteVariable('sShipSet');
299  oxRegistry::getSession()->deleteVariable('deladrid');
300  oxRegistry::getSession()->deleteVariable('dynvalue');
301 
302  // resetting & recalc basket
303  if (($oBasket = $this->getSession()->getBasket())) {
304  $oBasket->resetUserInfo();
305  $oBasket->onUpdate();
306  }
307  }
308 
317  public function logout()
318  {
319  $myConfig = $this->getConfig();
320  $oUser = oxNew('oxuser');
321 
322  if ($oUser->logout()) {
323 
324  $this->setLoginStatus(USER_LOGOUT);
325 
326  // finalizing ..
327  $this->_afterLogout();
328 
329 
330  if ($this->getParent()->isEnabledPrivateSales()) {
331  return 'account';
332  }
333 
334  // redirecting if user logs out in SSL mode
335  if (oxRegistry::getConfig()->getRequestParameter('redirect') && $myConfig->getConfigParam('sSSLShopURL')) {
336  oxRegistry::getUtils()->redirect($this->_getLogoutLink());
337  }
338  }
339  }
340 
350  public function changeUser()
351  {
352  $blUserRegistered = $this->_changeUser_noRedirect();
353 
354  if ($blUserRegistered === true) {
355  return 'payment';
356  } else {
357  return $blUserRegistered;
358  }
359  }
360 
367  public function changeuser_testvalues()
368  {
369  // skip updating user info if this is just form reload
370  // on selecting delivery address
371  // We do redirect only on success not to loose errors.
372 
373  if ($this->_changeUser_noRedirect()) {
374  return 'account_user';
375  }
376  }
377 
398  public function createUser()
399  {
400  $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
401 
402  $oConfig = $this->getConfig();
403 
404  if ($blActiveLogin && !$oConfig->getRequestParameter('ord_agb') && $oConfig->getConfigParam('blConfirmAGB')) {
405  oxRegistry::get("oxUtilsView")->addErrorToDisplay('READ_AND_CONFIRM_TERMS', false, true);
406 
407  return;
408  }
409 
410  // collecting values to check
411  $sUser = $oConfig->getRequestParameter('lgn_usr');
412 
413  // first pass
414  $sPassword = $oConfig->getRequestParameter('lgn_pwd', true);
415 
416  // second pass
417  $sPassword2 = $oConfig->getRequestParameter('lgn_pwd2', true);
418 
419  $aInvAdress = $oConfig->getRequestParameter('invadr', true);
420  $aDelAdress = $this->_getDelAddressData();
421 
423  $oUser = oxNew('oxuser');
424 
425  try {
426 
427  $oUser->checkValues($sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress);
428 
429  $iActState = $blActiveLogin ? 0 : 1;
430 
431  // setting values
432  $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
433  $oUser->setPassword($sPassword);
434  $oUser->oxuser__oxactive = new oxField($iActState, oxField::T_RAW);
435 
436  // used for checking if user email currently subscribed
437  $iSubscriptionStatus = $oUser->getNewsSubscription()->getOptInStatus();
438 
439  $oUser->createUser();
440  $oUser->load($oUser->getId());
441  $oUser->changeUserData($oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress);
442 
443  if ($blActiveLogin) {
444  // accepting terms..
445  $oUser->acceptTerms();
446  }
447 
448  $sUserId = oxRegistry::getSession()->getVariable("su");
449  $sRecEmail = oxRegistry::getSession()->getVariable("re");
450  if ($this->getConfig()->getConfigParam('blInvitationsEnabled') && $sUserId && $sRecEmail) {
451  // setting registration credit points..
452  $oUser->setCreditPointsForRegistrant($sUserId, $sRecEmail);
453  }
454 
455  // assigning to newsletter
456  $blOptin = oxRegistry::getConfig()->getRequestParameter('blnewssubscribed');
457  if ($blOptin && $iSubscriptionStatus == 1) {
458  // if user was assigned to newsletter
459  // and is creating account with newsletter checked,
460  // don't require confirm
461  $oUser->getNewsSubscription()->setOptInStatus(1);
462  $oUser->addToGroup('oxidnewsletter');
463  $this->_blNewsSubscriptionStatus = 1;
464  } else {
465  $blOrderOptInEmailParam = $this->getConfig()->getConfigParam('blOrderOptInEmail');
466  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription($blOptin, $blOrderOptInEmailParam);
467  }
468 
469  $oUser->addToGroup('oxidnotyetordered');
470  $oUser->logout();
471 
472  } catch (oxUserException $oEx) {
473  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
474 
475  return false;
476  } catch (oxInputException $oEx) {
477  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
478 
479  return false;
480  } catch (oxConnectionException $oEx) {
481  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
482 
483  return false;
484  }
485 
486  if (!$blActiveLogin) {
487 
488  oxRegistry::getSession()->setVariable('usr', $oUser->getId());
489  $this->_afterLogin($oUser);
490 
491 
492  // order remark
493  //V #427: order remark for new users
494  $sOrderRemark = oxRegistry::getConfig()->getRequestParameter('order_remark', true);
495  if ($sOrderRemark) {
496  oxRegistry::getSession()->setVariable('ordrem', $sOrderRemark);
497  }
498  }
499 
500  // send register eMail
501  //TODO: move into user
502  if ((int) oxRegistry::getConfig()->getRequestParameter('option') == 3) {
503  $oxEMail = oxNew('oxemail');
504  if ($blActiveLogin) {
505  $oxEMail->sendRegisterConfirmEmail($oUser);
506  } else {
507  $oxEMail->sendRegisterEmail($oUser);
508  }
509  }
510 
511  // new registered
512  $this->_blIsNewUser = true;
513 
514  $sAction = 'payment?new_user=1&success=1';
515  if ($this->_blNewsSubscriptionStatus !== null && !$this->_blNewsSubscriptionStatus) {
516  $sAction = 'payment?new_user=1&success=1&newslettererror=4';
517  }
518 
519  return $sAction;
520  }
521 
527  public function registerUser()
528  {
529  // registered new user ?
530  if ($this->createuser() != false && $this->_blIsNewUser) {
531  if ($this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus) {
532  return 'register?success=1';
533  } else {
534  return 'register?success=1&newslettererror=4';
535  }
536  } else {
537  // problems with registration ...
538  $this->logout();
539  }
540  }
541 
545  protected function _saveInvitor()
546  {
547  if ($this->getConfig()->getConfigParam('blInvitationsEnabled')) {
548  $this->getInvitor();
549  $this->setRecipient();
550  }
551  }
552 
556  protected function _saveDeliveryAddressState()
557  {
558  $oSession = oxRegistry::getSession();
559 
560  $blShow = oxRegistry::getConfig()->getRequestParameter('blshowshipaddress');
561  if (!isset($blShow)) {
562  $blShow = $oSession->getVariable('blshowshipaddress');
563  }
564 
565  $oSession->setVariable('blshowshipaddress', $blShow);
566  }
567 
581  protected function _changeUser_noRedirect()
582  {
583  if (!$this->getSession()->checkSessionChallenge()) {
584  return;
585  }
586 
587  // no user ?
588  $oUser = $this->getUser();
589  if (!$oUser) {
590  return;
591  }
592 
593  // collecting values to check
594  $aDelAdress = $this->_getDelAddressData();
595 
596  // if user company name, user name and additional info has special chars
597  $aInvAdress = oxRegistry::getConfig()->getRequestParameter('invadr', true);
598 
599  $sUserName = $oUser->oxuser__oxusername->value;
600  $sPassword = $sPassword2 = $oUser->oxuser__oxpassword->value;
601 
602  try { // testing user input
603  $oUser->changeUserData($sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress);
604  // assigning to newsletter
605  if (($blOptin = oxRegistry::getConfig()->getRequestParameter('blnewssubscribed')) === null) {
606  $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
607  }
608  // check if email address changed, if so, force check news subscription settings.
609  $sBillingUsername = $aInvAdress['oxuser__oxusername'];
610  $blForceCheckOptIn = ($sBillingUsername !== null && $sBillingUsername !== $sUserName);
611  $blEmailParam = $this->getConfig()->getConfigParam('blOrderOptInEmail');
612  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription($blOptin, $blEmailParam, $blForceCheckOptIn);
613 
614  } catch (oxUserException $oEx) { // errors in input
615  // marking error code
616  //TODO
617  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
618 
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 
624  return;
625  } catch (oxConnectionException $oEx) {
626  //connection to external resource broken, change message and pass to the view
627  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
628 
629  return;
630  }
631 
632 
633  // order remark
634  $sOrderRemark = oxRegistry::getConfig()->getRequestParameter('order_remark', true);
635 
636  if ($sOrderRemark) {
637  oxRegistry::getSession()->setVariable('ordrem', $sOrderRemark);
638  } else {
639  oxRegistry::getSession()->deleteVariable('ordrem');
640  }
641 
642  if ($oBasket = $this->getSession()->getBasket()) {
643  $oBasket->onUpdate();
644  }
645 
646  return true;
647  }
648 
655  protected function _getDelAddressData()
656  {
657  // if user company name, user name and additional info has special chars
658  $blShowShipAddressParameter = oxRegistry::getConfig()->getRequestParameter('blshowshipaddress');
659  $blShowShipAddressVariable = oxRegistry::getSession()->getVariable('blshowshipaddress');
660  $sDeliveryAddressParameter = oxRegistry::getConfig()->getRequestParameter('deladr', true);
661  $aDeladr = ($blShowShipAddressParameter || $blShowShipAddressVariable) ? $sDeliveryAddressParameter : array();
662  $aDelAdress = $aDeladr;
663 
664  if (is_array($aDeladr)) {
665  // checking if data is filled
666  if (isset($aDeladr['oxaddress__oxsal'])) {
667  unset($aDeladr['oxaddress__oxsal']);
668  }
669  if (!count($aDeladr) || implode('', $aDeladr) == '') {
670  // resetting to avoid empty records
671  $aDelAdress = array();
672  }
673  }
674 
675  return $aDelAdress;
676  }
677 
683  protected function _getLogoutLink()
684  {
685  $oConfig = $this->getConfig();
686 
687  $sLogoutLink = $oConfig->isSsl() ? $oConfig->getShopSecureHomeUrl() : $oConfig->getShopHomeUrl();
688  $sLogoutLink .= 'cl=' . $oConfig->getRequestParameter('cl') . $this->getParent()->getDynUrlParams();
689  if ($sParam = $oConfig->getRequestParameter('anid')) {
690  $sLogoutLink .= '&amp;anid=' . $sParam;
691  }
692  if ($sParam = $oConfig->getRequestParameter('cnid')) {
693  $sLogoutLink .= '&amp;cnid=' . $sParam;
694  }
695  if ($sParam = $oConfig->getRequestParameter('mnid')) {
696  $sLogoutLink .= '&amp;mnid=' . $sParam;
697  }
698  if ($sParam = $oConfig->getRequestParameter('tpl')) {
699  $sLogoutLink .= '&amp;tpl=' . $sParam;
700  }
701  if ($sParam = $oConfig->getRequestParameter('oxloadid')) {
702  $sLogoutLink .= '&amp;oxloadid=' . $sParam;
703  }
704  if ($sParam = $oConfig->getRequestParameter('recommid')) {
705  $sLogoutLink .= '&amp;recommid=' . $sParam;
706  }
707 
708  return $sLogoutLink . '&amp;fnc=logout';
709  }
710 
716  public function setLoginStatus($iStatus)
717  {
718  $this->_iLoginStatus = $iStatus;
719  }
720 
729  public function getLoginStatus()
730  {
731  return $this->_iLoginStatus;
732  }
733 
737  public function getInvitor()
738  {
739  $sSu = oxRegistry::getSession()->getVariable('su');
740 
741  if (!$sSu && ($sSuNew = oxRegistry::getConfig()->getRequestParameter('su'))) {
742  oxRegistry::getSession()->setVariable('su', $sSuNew);
743  }
744  }
745 
749  public function setRecipient()
750  {
751  $sRe = oxRegistry::getSession()->getVariable('re');
752  if (!$sRe && ($sReNew = oxRegistry::getConfig()->getRequestParameter('re'))) {
753  oxRegistry::getSession()->setVariable('re', $sReNew);
754  }
755  }
756 }