OXID eShop CE  4.9.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 
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  $oConfig = $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') && $oConfig->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  $aInvAdress = $this->cleanBillingAddress($aInvAdress);
423 
424  $aDelAdress = $this->_getDelAddressData();
425  $aDelAdress = $this->cleanDeliveryAddress($aDelAdress);
426 
428  $oUser = oxNew('oxuser');
429 
430  try {
431 
432  $oUser->checkValues($sUser, $sPassword, $sPassword2, $aInvAdress, $aDelAdress);
433 
434  $iActState = $blActiveLogin ? 0 : 1;
435 
436  // setting values
437  $oUser->oxuser__oxusername = new oxField($sUser, oxField::T_RAW);
438  $oUser->setPassword($sPassword);
439  $oUser->oxuser__oxactive = new oxField($iActState, oxField::T_RAW);
440 
441  // used for checking if user email currently subscribed
442  $iSubscriptionStatus = $oUser->getNewsSubscription()->getOptInStatus();
443 
444  $oUser->createUser();
445  $oUser->load($oUser->getId());
446  $oUser->changeUserData($oUser->oxuser__oxusername->value, $sPassword, $sPassword, $aInvAdress, $aDelAdress);
447 
448  if ($blActiveLogin) {
449  // accepting terms..
450  $oUser->acceptTerms();
451  }
452 
453  $sUserId = oxRegistry::getSession()->getVariable("su");
454  $sRecEmail = oxRegistry::getSession()->getVariable("re");
455  if ($this->getConfig()->getConfigParam('blInvitationsEnabled') && $sUserId && $sRecEmail) {
456  // setting registration credit points..
457  $oUser->setCreditPointsForRegistrant($sUserId, $sRecEmail);
458  }
459 
460  // assigning to newsletter
461  $blOptin = oxRegistry::getConfig()->getRequestParameter('blnewssubscribed');
462  if ($blOptin && $iSubscriptionStatus == 1) {
463  // if user was assigned to newsletter
464  // and is creating account with newsletter checked,
465  // don't require confirm
466  $oUser->getNewsSubscription()->setOptInStatus(1);
467  $oUser->addToGroup('oxidnewsletter');
468  $this->_blNewsSubscriptionStatus = 1;
469  } else {
470  $blOrderOptInEmailParam = $this->getConfig()->getConfigParam('blOrderOptInEmail');
471  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription($blOptin, $blOrderOptInEmailParam);
472  }
473 
474  $oUser->addToGroup('oxidnotyetordered');
475  $oUser->logout();
476 
477  } catch (oxUserException $oEx) {
478  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
479 
480  return false;
481  } catch (oxInputException $oEx) {
482  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
483 
484  return false;
485  } catch (oxConnectionException $oEx) {
486  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
487 
488  return false;
489  }
490 
491  if (!$blActiveLogin) {
492 
493  oxRegistry::getSession()->setVariable('usr', $oUser->getId());
494  $this->_afterLogin($oUser);
495 
496 
497  // order remark
498  //V #427: order remark for new users
499  $sOrderRemark = oxRegistry::getConfig()->getRequestParameter('order_remark', true);
500  if ($sOrderRemark) {
501  oxRegistry::getSession()->setVariable('ordrem', $sOrderRemark);
502  }
503  }
504 
505  // send register eMail
506  //TODO: move into user
507  if ((int) oxRegistry::getConfig()->getRequestParameter('option') == 3) {
508  $oxEMail = oxNew('oxemail');
509  if ($blActiveLogin) {
510  $oxEMail->sendRegisterConfirmEmail($oUser);
511  } else {
512  $oxEMail->sendRegisterEmail($oUser);
513  }
514  }
515 
516  // new registered
517  $this->_blIsNewUser = true;
518 
519  $sAction = 'payment?new_user=1&success=1';
520  if ($this->_blNewsSubscriptionStatus !== null && !$this->_blNewsSubscriptionStatus) {
521  $sAction = 'payment?new_user=1&success=1&newslettererror=4';
522  }
523 
524  return $sAction;
525  }
526 
532  public function registerUser()
533  {
534  // registered new user ?
535  if ($this->createuser() != false && $this->_blIsNewUser) {
536  if ($this->_blNewsSubscriptionStatus === null || $this->_blNewsSubscriptionStatus) {
537  return 'register?success=1';
538  } else {
539  return 'register?success=1&newslettererror=4';
540  }
541  } else {
542  // problems with registration ...
543  $this->logout();
544  }
545  }
546 
550  protected function _saveInvitor()
551  {
552  if ($this->getConfig()->getConfigParam('blInvitationsEnabled')) {
553  $this->getInvitor();
554  $this->setRecipient();
555  }
556  }
557 
561  protected function _saveDeliveryAddressState()
562  {
563  $oSession = oxRegistry::getSession();
564 
565  $blShow = oxRegistry::getConfig()->getRequestParameter('blshowshipaddress');
566  if (!isset($blShow)) {
567  $blShow = $oSession->getVariable('blshowshipaddress');
568  }
569 
570  $oSession->setVariable('blshowshipaddress', $blShow);
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  $aDelAdress = $this->cleanDeliveryAddress($aDelAdress);
601 
602  // if user company name, user name and additional info has special chars
603  $aInvAdress = oxRegistry::getConfig()->getRequestParameter('invadr', true);
604  $aInvAdress = $this->cleanBillingAddress($aInvAdress);
605 
606  $sUserName = $oUser->oxuser__oxusername->value;
607  $sPassword = $sPassword2 = $oUser->oxuser__oxpassword->value;
608 
609  try { // testing user input
610  $oUser->changeUserData($sUserName, $sPassword, $sPassword2, $aInvAdress, $aDelAdress);
611  // assigning to newsletter
612  if (($blOptin = oxRegistry::getConfig()->getRequestParameter('blnewssubscribed')) === null) {
613  $blOptin = $oUser->getNewsSubscription()->getOptInStatus();
614  }
615  // check if email address changed, if so, force check news subscription settings.
616  $sBillingUsername = $aInvAdress['oxuser__oxusername'];
617  $blForceCheckOptIn = ($sBillingUsername !== null && $sBillingUsername !== $sUserName);
618  $blEmailParam = $this->getConfig()->getConfigParam('blOrderOptInEmail');
619  $this->_blNewsSubscriptionStatus = $oUser->setNewsSubscription($blOptin, $blEmailParam, $blForceCheckOptIn);
620 
621  } catch (oxUserException $oEx) { // errors in input
622  // marking error code
623  //TODO
624  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
625 
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 
631  return;
632  } catch (oxConnectionException $oEx) {
633  //connection to external resource broken, change message and pass to the view
634  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true);
635 
636  return;
637  }
638 
639 
640  // order remark
641  $sOrderRemark = oxRegistry::getConfig()->getRequestParameter('order_remark', true);
642 
643  if ($sOrderRemark) {
644  oxRegistry::getSession()->setVariable('ordrem', $sOrderRemark);
645  } else {
646  oxRegistry::getSession()->deleteVariable('ordrem');
647  }
648 
649  if ($oBasket = $this->getSession()->getBasket()) {
650  $oBasket->setBasketUser(null);
651  $oBasket->onUpdate();
652  }
653 
654  return true;
655  }
656 
664  private function cleanBillingAddress($aBillingAddress)
665  {
666  if (is_array($aBillingAddress)) {
667  $skipFields = array('oxuser__oxid', 'oxid', 'oxuser__oxpoints', 'oxpoints', 'oxuser__oxboni', 'oxboni');
668  $aBillingAddress = array_change_key_case($aBillingAddress);
669  $aBillingAddress = array_diff_key($aBillingAddress, array_flip($skipFields));
670  }
671  return $aBillingAddress;
672  }
673 
681  private function cleanDeliveryAddress($aDeliveryAddress)
682  {
683  if (is_array($aDeliveryAddress)) {
684  $skipFields = array('oxaddress__oxid', 'oxid', 'oxaddress__oxuserid', 'oxuserid', 'oxaddress__oxaddressuserid', 'oxaddressuserid');
685  $aDeliveryAddress = array_change_key_case($aDeliveryAddress);
686  $aDeliveryAddress = array_diff_key($aDeliveryAddress, array_flip($skipFields));
687  }
688  return $aDeliveryAddress;
689  }
690 
697  protected function _getDelAddressData()
698  {
699  // if user company name, user name and additional info has special chars
700  $blShowShipAddressParameter = oxRegistry::getConfig()->getRequestParameter('blshowshipaddress');
701  $blShowShipAddressVariable = oxRegistry::getSession()->getVariable('blshowshipaddress');
702  $sDeliveryAddressParameter = oxRegistry::getConfig()->getRequestParameter('deladr', true);
703  $aDeladr = ($blShowShipAddressParameter || $blShowShipAddressVariable) ? $sDeliveryAddressParameter : array();
704  $aDelAdress = $aDeladr;
705 
706  if (is_array($aDeladr)) {
707  // checking if data is filled
708  if (isset($aDeladr['oxaddress__oxsal'])) {
709  unset($aDeladr['oxaddress__oxsal']);
710  }
711  if (!count($aDeladr) || implode('', $aDeladr) == '') {
712  // resetting to avoid empty records
713  $aDelAdress = array();
714  }
715  }
716 
717  return $aDelAdress;
718  }
719 
725  protected function _getLogoutLink()
726  {
727  $oConfig = $this->getConfig();
728 
729  $sLogoutLink = $oConfig->isSsl() ? $oConfig->getShopSecureHomeUrl() : $oConfig->getShopHomeUrl();
730  $sLogoutLink .= 'cl=' . $oConfig->getRequestParameter('cl') . $this->getParent()->getDynUrlParams();
731  if ($sParam = $oConfig->getRequestParameter('anid')) {
732  $sLogoutLink .= '&amp;anid=' . $sParam;
733  }
734  if ($sParam = $oConfig->getRequestParameter('cnid')) {
735  $sLogoutLink .= '&amp;cnid=' . $sParam;
736  }
737  if ($sParam = $oConfig->getRequestParameter('mnid')) {
738  $sLogoutLink .= '&amp;mnid=' . $sParam;
739  }
740  if ($sParam = $oConfig->getRequestParameter('tpl')) {
741  $sLogoutLink .= '&amp;tpl=' . $sParam;
742  }
743  if ($sParam = $oConfig->getRequestParameter('oxloadid')) {
744  $sLogoutLink .= '&amp;oxloadid=' . $sParam;
745  }
746  if ($sParam = $oConfig->getRequestParameter('recommid')) {
747  $sLogoutLink .= '&amp;recommid=' . $sParam;
748  }
749 
750  return $sLogoutLink . '&amp;fnc=logout';
751  }
752 
758  public function setLoginStatus($iStatus)
759  {
760  $this->_iLoginStatus = $iStatus;
761  }
762 
771  public function getLoginStatus()
772  {
773  return $this->_iLoginStatus;
774  }
775 
779  public function getInvitor()
780  {
781  $sSu = oxRegistry::getSession()->getVariable('su');
782 
783  if (!$sSu && ($sSuNew = oxRegistry::getConfig()->getRequestParameter('su'))) {
784  oxRegistry::getSession()->setVariable('su', $sSuNew);
785  }
786  }
787 
791  public function setRecipient()
792  {
793  $sRe = oxRegistry::getSession()->getVariable('re');
794  if (!$sRe && ($sReNew = oxRegistry::getConfig()->getRequestParameter('re'))) {
795  oxRegistry::getSession()->setVariable('re', $sReNew);
796  }
797  }
798 }