OXID eShop CE  4.9.8
 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  oxRegistry::getSession()->delBasket();
309  }
310 
319  public function logout()
320  {
321  $myConfig = $this->getConfig();
322  $oUser = oxNew('oxuser');
323 
324  if ($oUser->logout()) {
325 
326  $this->setLoginStatus(USER_LOGOUT);
327 
328  // finalizing ..
329  $this->_afterLogout();
330 
331 
332  if ($this->getParent()->isEnabledPrivateSales()) {
333  return 'account';
334  }
335 
336  // redirecting if user logs out in SSL mode
337  if (oxRegistry::getConfig()->getRequestParameter('redirect') && $myConfig->getConfigParam('sSSLShopURL')) {
338  oxRegistry::getUtils()->redirect($this->_getLogoutLink());
339  }
340  }
341  }
342 
352  public function changeUser()
353  {
354  $blUserRegistered = $this->_changeUser_noRedirect();
355 
356  if ($blUserRegistered === true) {
357  return 'payment';
358  } else {
359  return $blUserRegistered;
360  }
361  }
362 
369  public function changeuser_testvalues()
370  {
371  // skip updating user info if this is just form reload
372  // on selecting delivery address
373  // We do redirect only on success not to loose errors.
374 
375  if ($this->_changeUser_noRedirect()) {
376  return 'account_user';
377  }
378  }
379 
400  public function createUser()
401  {
402  $blActiveLogin = $this->getParent()->isEnabledPrivateSales();
403 
404  $oConfig = $this->getConfig();
405 
406  if ($blActiveLogin && !$oConfig->getRequestParameter('ord_agb') && $oConfig->getConfigParam('blConfirmAGB')) {
407  oxRegistry::get("oxUtilsView")->addErrorToDisplay('READ_AND_CONFIRM_TERMS', false, true);
408 
409  return;
410  }
411 
412  // collecting values to check
413  $sUser = $oConfig->getRequestParameter('lgn_usr');
414 
415  // first pass
416  $sPassword = $oConfig->getRequestParameter('lgn_pwd', true);
417 
418  // second pass
419  $sPassword2 = $oConfig->getRequestParameter('lgn_pwd2', true);
420 
421  $aInvAdress = $oConfig->getRequestParameter('invadr', true);
422  $aDelAdress = $this->_getDelAddressData();
423 
425  $oUser = oxNew('oxuser');
426 
427  try {
428 
429  $oUser->checkValues($sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress);
430 
431  $iActState = $blActiveLogin ? 0 : 1;
432 
433  // setting values
434  $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
435  $oUser->setPassword($sPassword);
436  $oUser->oxuser__oxactive = new oxField($iActState, oxField::T_RAW);
437 
438  // used for checking if user email currently subscribed
439  $iSubscriptionStatus = $oUser->getNewsSubscription()->getOptInStatus();
440 
441  $oUser->createUser();
442  $oUser->load($oUser->getId());
443  $oUser->changeUserData($oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress);
444 
445  if ($blActiveLogin) {
446  // accepting terms..
447  $oUser->acceptTerms();
448  }
449 
450  $sUserId = oxRegistry::getSession()->getVariable("su");
451  $sRecEmail = oxRegistry::getSession()->getVariable("re");
452  if ($this->getConfig()->getConfigParam('blInvitationsEnabled') && $sUserId && $sRecEmail) {
453  // setting registration credit points..
454  $oUser->setCreditPointsForRegistrant($sUserId, $sRecEmail);
455  }
456 
457  // assigning to newsletter
458  $blOptin = oxRegistry::getConfig()->getRequestParameter('blnewssubscribed');
459  if ($blOptin && $iSubscriptionStatus == 1) {
460  // if user was assigned to newsletter
461  // and is creating account with newsletter checked,
462  // don't require confirm
463  $oUser->getNewsSubscription()->setOptInStatus(1);
464  $oUser->addToGroup('oxidnewsletter');
465  $this->_blNewsSubscriptionStatus = 1;
466  } else {
467  $blOrderOptInEmailParam = $this->getConfig()->getConfigParam('blOrderOptInEmail');
468  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription($blOptin, $blOrderOptInEmailParam);
469  }
470 
471  $oUser->addToGroup('oxidnotyetordered');
472  $oUser->logout();
473 
474  } catch (oxUserException $oEx) {
475  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
476 
477  return false;
478  } catch (oxInputException $oEx) {
479  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
480 
481  return false;
482  } catch (oxConnectionException $oEx) {
483  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
484 
485  return false;
486  }
487 
488  if (!$blActiveLogin) {
489 
490  oxRegistry::getSession()->setVariable('usr', $oUser->getId());
491  $this->_afterLogin($oUser);
492 
493 
494  // order remark
495  //V #427: order remark for new users
496  $sOrderRemark = oxRegistry::getConfig()->getRequestParameter('order_remark', true);
497  if ($sOrderRemark) {
498  oxRegistry::getSession()->setVariable('ordrem', $sOrderRemark);
499  }
500  }
501 
502  // send register eMail
503  //TODO: move into user
504  if ((int) oxRegistry::getConfig()->getRequestParameter('option') == 3) {
505  $oxEMail = oxNew('oxemail');
506  if ($blActiveLogin) {
507  $oxEMail->sendRegisterConfirmEmail($oUser);
508  } else {
509  $oxEMail->sendRegisterEmail($oUser);
510  }
511  }
512 
513  // new registered
514  $this->_blIsNewUser = true;
515 
516  $sAction = 'payment?new_user=1&success=1';
517  if ($this->_blNewsSubscriptionStatus !== null && !$this->_blNewsSubscriptionStatus) {
518  $sAction = 'payment?new_user=1&success=1&newslettererror=4';
519  }
520 
521  return $sAction;
522  }
523 
529  public function registerUser()
530  {
531  // registered new user ?
532  if ($this->createuser() != false && $this->_blIsNewUser) {
533  if ($this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus) {
534  return 'register?success=1';
535  } else {
536  return 'register?success=1&newslettererror=4';
537  }
538  } else {
539  // problems with registration ...
540  $this->logout();
541  }
542  }
543 
547  protected function _saveInvitor()
548  {
549  if ($this->getConfig()->getConfigParam('blInvitationsEnabled')) {
550  $this->getInvitor();
551  $this->setRecipient();
552  }
553  }
554 
558  protected function _saveDeliveryAddressState()
559  {
560  $oSession = oxRegistry::getSession();
561 
562  $blShow = oxRegistry::getConfig()->getRequestParameter('blshowshipaddress');
563  if (!isset($blShow)) {
564  $blShow = $oSession->getVariable('blshowshipaddress');
565  }
566 
567  $oSession->setVariable('blshowshipaddress', $blShow);
568  }
569 
583  protected function _changeUser_noRedirect()
584  {
585  if (!$this->getSession()->checkSessionChallenge()) {
586  return;
587  }
588 
589  // no user ?
590  $oUser = $this->getUser();
591  if (!$oUser) {
592  return;
593  }
594 
595  // collecting values to check
596  $aDelAdress = $this->_getDelAddressData();
597 
598  // if user company name, user name and additional info has special chars
599  $aInvAdress = oxRegistry::getConfig()->getRequestParameter('invadr', true);
600 
601  $sUserName = $oUser->oxuser__oxusername->value;
602  $sPassword = $sPassword2 = $oUser->oxuser__oxpassword->value;
603 
604  try { // testing user input
605  $oUser->changeUserData($sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress);
606  // assigning to newsletter
607  if (($blOptin = oxRegistry::getConfig()->getRequestParameter('blnewssubscribed')) === null) {
608  $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
609  }
610  // check if email address changed, if so, force check news subscription settings.
611  $sBillingUsername = $aInvAdress['oxuser__oxusername'];
612  $blForceCheckOptIn = ($sBillingUsername !== null && $sBillingUsername !== $sUserName);
613  $blEmailParam = $this->getConfig()->getConfigParam('blOrderOptInEmail');
614  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription($blOptin, $blEmailParam, $blForceCheckOptIn);
615 
616  } catch (oxUserException $oEx) { // errors in input
617  // marking error code
618  //TODO
619  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
620 
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 
626  return;
627  } catch (oxConnectionException $oEx) {
628  //connection to external resource broken, change message and pass to the view
629  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
630 
631  return;
632  }
633 
634 
635  // order remark
636  $sOrderRemark = oxRegistry::getConfig()->getRequestParameter('order_remark', true);
637 
638  if ($sOrderRemark) {
639  oxRegistry::getSession()->setVariable('ordrem', $sOrderRemark);
640  } else {
641  oxRegistry::getSession()->deleteVariable('ordrem');
642  }
643 
644  if ($oBasket = $this->getSession()->getBasket()) {
645  $oBasket->setBasketUser(null);
646  $oBasket->onUpdate();
647  }
648 
649  return true;
650  }
651 
658  protected function _getDelAddressData()
659  {
660  // if user company name, user name and additional info has special chars
661  $blShowShipAddressParameter = oxRegistry::getConfig()->getRequestParameter('blshowshipaddress');
662  $blShowShipAddressVariable = oxRegistry::getSession()->getVariable('blshowshipaddress');
663  $sDeliveryAddressParameter = oxRegistry::getConfig()->getRequestParameter('deladr', true);
664  $aDeladr = ($blShowShipAddressParameter || $blShowShipAddressVariable) ? $sDeliveryAddressParameter : array();
665  $aDelAdress = $aDeladr;
666 
667  if (is_array($aDeladr)) {
668  // checking if data is filled
669  if (isset($aDeladr['oxaddress__oxsal'])) {
670  unset($aDeladr['oxaddress__oxsal']);
671  }
672  if (!count($aDeladr) || implode('', $aDeladr) == '') {
673  // resetting to avoid empty records
674  $aDelAdress = array();
675  }
676  }
677 
678  return $aDelAdress;
679  }
680 
686  protected function _getLogoutLink()
687  {
688  $oConfig = $this->getConfig();
689 
690  $sLogoutLink = $oConfig->isSsl() ? $oConfig->getShopSecureHomeUrl() : $oConfig->getShopHomeUrl();
691  $sLogoutLink .= 'cl=' . $oConfig->getRequestParameter('cl') . $this->getParent()->getDynUrlParams();
692  if ($sParam = $oConfig->getRequestParameter('anid')) {
693  $sLogoutLink .= '&amp;anid=' . $sParam;
694  }
695  if ($sParam = $oConfig->getRequestParameter('cnid')) {
696  $sLogoutLink .= '&amp;cnid=' . $sParam;
697  }
698  if ($sParam = $oConfig->getRequestParameter('mnid')) {
699  $sLogoutLink .= '&amp;mnid=' . $sParam;
700  }
701  if ($sParam = $oConfig->getRequestParameter('tpl')) {
702  $sLogoutLink .= '&amp;tpl=' . $sParam;
703  }
704  if ($sParam = $oConfig->getRequestParameter('oxloadid')) {
705  $sLogoutLink .= '&amp;oxloadid=' . $sParam;
706  }
707  if ($sParam = $oConfig->getRequestParameter('recommid')) {
708  $sLogoutLink .= '&amp;recommid=' . $sParam;
709  }
710 
711  return $sLogoutLink . '&amp;fnc=logout';
712  }
713 
719  public function setLoginStatus($iStatus)
720  {
721  $this->_iLoginStatus = $iStatus;
722  }
723 
732  public function getLoginStatus()
733  {
734  return $this->_iLoginStatus;
735  }
736 
740  public function getInvitor()
741  {
742  $sSu = oxRegistry::getSession()->getVariable('su');
743 
744  if (!$sSu && ($sSuNew = oxRegistry::getConfig()->getRequestParameter('su'))) {
745  oxRegistry::getSession()->setVariable('su', $sSuNew);
746  }
747  }
748 
752  public function setRecipient()
753  {
754  $sRe = oxRegistry::getSession()->getVariable('re');
755  if (!$sRe && ($sReNew = oxRegistry::getConfig()->getRequestParameter('re'))) {
756  oxRegistry::getSession()->setVariable('re', $sReNew);
757  }
758  }
759 }