OXID eShop CE  4.10.7
 All Classes Namespaces Files Functions Variables Pages
oxuser.php
Go to the documentation of this file.
1 <?php
2 
9 class oxUser extends oxBase
10 {
11 
17  protected $_blDisableShopCheck = true;
18 
24  protected $_oNewsSubscription = null;
25 
31  protected $_sClassName = 'oxuser';
32 
38  protected $_aBaskets = array();
39 
45  protected $_oGroups;
46 
52  protected $_aAddresses = array();
53 
59  protected $_oPayments;
60 
68  protected $_oRecommList;
69 
75  protected $_blMallUsers = false;
76 
82  protected static $_aUserCookie = array();
83 
89  protected $_iCntNoticeListArticles = null;
90 
96  protected $_iCntWishListArticles = null;
97 
105  protected $_iCntRecommLists = null;
106 
112  protected $_sUpdateKey = null;
113 
119  protected $_blLoadedFromCookie = null;
120 
126  protected $_sSelAddressId = null;
127 
133  protected $_oSelAddress = null;
134 
140  protected $_sWishId = null;
141 
147  protected $_oUserCountryTitle = null;
148 
152  protected $_oStateObject = null;
153 
159  protected function _getStateObject()
160  {
161  if (is_null($this->_oStateObject)) {
162  $this->_oStateObject = oxNew('oxState');
163  }
164 
165  return $this->_oStateObject;
166  }
167 
173  public function __construct()
174  {
175  $this->setMallUsersStatus($this->getConfig()->getConfigParam('blMallUsers'));
176 
178  $this->init('oxuser');
179  }
180 
186  public function setMallUsersStatus($blOn = false)
187  {
188  $this->_blMallUsers = $blOn;
189  }
190 
198  public function __get($sParamName)
199  {
200  // it saves memory using - loads data only if it is used
201  switch ($sParamName) {
202  case 'oGroups':
203  return $this->_oGroups = $this->getUserGroups();
204  break;
205  case 'iCntNoticeListArticles':
206  return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
207  break;
208  case 'iCntWishListArticles':
209  return $this->_iCntWishListArticles = $this->getWishListArtCnt();
210  break;
211  // @deprecated since v5.3 (2016-06-17); Listmania will be moved to an own module.
212  case 'iCntRecommLists':
213  return $this->_iCntRecommLists = $this->getRecommListsCount();
214  break;
215  // END deprecated
216  case 'oAddresses':
217  return $this->getUserAddresses();
218  break;
219  case 'oPayments':
220  return $this->_oPayments = $this->getUserPayments();
221  break;
222  case 'oxuser__oxcountry':
223  return $this->oxuser__oxcountry = $this->getUserCountry();
224  break;
225  case 'sDBOptin':
226  return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
227  break;
228  case 'sEmailFailed':
229  return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
230  break;
231  }
232  }
233 
239  public function getNewsSubscription()
240  {
241  if ($this->_oNewsSubscription !== null) {
243  }
244 
245  $this->_oNewsSubscription = oxNew('oxnewssubscribed');
246 
247  // if subscription object is not set yet - we should create one
248  if (!$this->_oNewsSubscription->loadFromUserId($this->getId())) {
249  if (!$this->_oNewsSubscription->loadFromEmail($this->oxuser__oxusername->value)) {
250 
251  // no subscription defined yet - creating one
252  $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
253  $this->_oNewsSubscription->oxnewssubscribed__oxemail = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
254  $this->_oNewsSubscription->oxnewssubscribed__oxsal = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
255  $this->_oNewsSubscription->oxnewssubscribed__oxfname = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
256  $this->_oNewsSubscription->oxnewssubscribed__oxlname = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
257  }
258  }
259 
261  }
262 
272  public function getUserCountry($sCountryId = null, $iLang = null)
273  {
274  if ($this->_oUserCountryTitle == null || $sCountryId) {
275  $sId = $sCountryId ? $sCountryId : $this->oxuser__oxcountryid->value;
276  $oDb = oxDb::getDb();
277  $sViewName = getViewName('oxcountry', $iLang);
278  $sQ = "select oxtitle from {$sViewName} where oxid = " . $oDb->quote($sId) . " ";
279  $oCountry = new oxField($oDb->getOne($sQ), oxField::T_RAW);
280  if (!$sCountryId) {
281  $this->_oUserCountryTitle = $oCountry;
282  }
283  } else {
285  }
286 
287  return $oCountry;
288  }
289 
297  public function getUserCountryId($sCountry = null)
298  {
299  $oDb = oxDb::getDb();
300  $sQ = "select oxid from " . getviewName("oxcountry") . " where oxactive = '1' and oxisoalpha2 = " . $oDb->quote($sCountry) . " ";
301  $sCountryId = $oDb->getOne($sQ);
302 
303  return $sCountryId;
304  }
305 
313  public function getUserGroups($sOXID = null)
314  {
315 
316  if (isset($this->_oGroups)) {
317  return $this->_oGroups;
318  }
319 
320  if (!$sOXID) {
321  $sOXID = $this->getId();
322  }
323 
324  $sViewName = getViewName("oxgroups");
325  $this->_oGroups = oxNew('oxList', 'oxgroups');
326  $sSelect = "select {$sViewName}.* from {$sViewName} left join oxobject2group on oxobject2group.oxgroupsid = {$sViewName}.oxid
327  where oxobject2group.oxobjectid = " . oxDb::getDb()->quote($sOXID);
328  $this->_oGroups->selectString($sSelect);
329 
330  return $this->_oGroups;
331  }
332 
340  public function getUserAddresses($sUserId = null)
341  {
342  $sUserId = isset($sUserId) ? $sUserId : $this->getId();
343  if (!isset($this->_aAddresses[$sUserId])) {
344  $oUserAddressList = oxNew('oxUserAddressList');
345  $oUserAddressList->load($sUserId);
346  $this->_aAddresses[$sUserId] = $oUserAddressList;
347 
348  // marking selected
349  if ($sAddressId = $this->getSelectedAddressId()) {
350  foreach ($this->_aAddresses[$sUserId] as $oAddress) {
351  if ($oAddress->getId() === $sAddressId) {
352  $oAddress->setSelected();
353  break;
354  }
355  }
356  }
357  }
358 
359  return $this->_aAddresses[$sUserId];
360  }
361 
367  public function setSelectedAddressId($sAddressId)
368  {
369  $this->_sSelAddressId = $sAddressId;
370  }
371 
377  public function getSelectedAddressId()
378  {
379  if ($this->_sSelAddressId !== null) {
380  return $this->_sSelAddressId;
381  }
382 
383  $sAddressId = oxRegistry::getConfig()->getRequestParameter("oxaddressid");
384  if (!$sAddressId && !oxRegistry::getConfig()->getRequestParameter('reloadaddress')) {
385  $sAddressId = oxRegistry::getSession()->getVariable("deladrid");
386  }
387 
388  return $sAddressId;
389  }
390 
396  protected function _getWishListId()
397  {
398  $this->_sWishId = null;
399  // check if we have to set it here
400  $oBasket = $this->getSession()->getBasket();
401  foreach ($oBasket->getContents() as $oBasketItem) {
402  if ($this->_sWishId = $oBasketItem->getWishId()) {
403  // stop on first found
404  break;
405  }
406  }
407 
408  return $this->_sWishId;
409  }
410 
419  public function getSelectedAddress($sWishId = false)
420  {
421  if ($this->_oSelAddress !== null) {
422  return $this->_oSelAddress;
423  }
424 
425  $oSelectedAddress = null;
426  $oAddresses = $this->getUserAddresses();
427  if ($oAddresses->count()) {
428  if ($sAddressId = $this->getSelectedAddressId()) {
429  foreach ($oAddresses as $oAddress) {
430  if ($oAddress->getId() == $sAddressId) {
431  $oAddress->selected = 1;
432  $oAddress->setSelected();
433  $oSelectedAddress = $oAddress;
434  break;
435  }
436  }
437  }
438 
439  // in case none is set - setting first one
440  if (!$oSelectedAddress) {
441  if (!$sAddressId || $sAddressId >= 0) {
442  $oAddresses->rewind();
443  $oAddress = $oAddresses->current();
444  } else {
445  $aAddresses = $oAddresses->getArray();
446  $oAddress = array_pop($aAddresses);
447  }
448  $oAddress->selected = 1;
449  $oAddress->setSelected();
450  $oSelectedAddress = $oAddress;
451  }
452  }
453  $this->_oSelAddress = $oSelectedAddress;
454 
455  return $oSelectedAddress;
456  }
457 
465  public function getUserPayments($sOXID = null)
466  {
467  if ($this->_oPayments === null) {
468 
469  if (!$sOXID) {
470  $sOXID = $this->getId();
471  }
472 
473  $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote($sOXID) . ' ';
474 
475  $this->_oPayments = oxNew('oxList');
476  $this->_oPayments->init('oxUserPayment');
477  $this->_oPayments->selectString($sSelect);
478  }
479 
480  return $this->_oPayments;
481  }
482 
488  public function save()
489  {
491 
492  $blAddRemark = false;
493  if ($this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
494  $blAddRemark = true;
495  //save oxregister value
496  $this->oxuser__oxregister = new oxField(date('Y-m-d H:i:s'), oxField::T_RAW);
497  }
498 
499  // setting user rights
500  $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
501 
502  // processing birth date which came from output as array
503  if (is_array($this->oxuser__oxbirthdate->value)) {
504  $this->oxuser__oxbirthdate = new oxField($this->convertBirthday($this->oxuser__oxbirthdate->value), oxField::T_RAW);
505  }
506 
507  // @deprecated since v5.3 (2016-05-20); Facebook will be extracted into module.
508  // checking if user Facebook ID should be updated
509  if ($myConfig->getConfigParam("bl_showFbConnect")) {
510  $oFb = oxRegistry::get("oxFb");
511  if ($oFb->isConnected() && $oFb->getUser()) {
512  $this->oxuser__oxfbid = new oxField($oFb->getUser());
513  }
514  }
515  // END deprecated
516 
517  $blRet = parent::save();
518 
519  //add registered remark
520  if ($blAddRemark && $blRet) {
521  $oRemark = oxNew('oxremark');
522  $oRemark->oxremark__oxtext = new oxField(oxRegistry::getLang()->translateString('usrRegistered', null, true), oxField::T_RAW);
523  $oRemark->oxremark__oxtype = new oxField('r', oxField::T_RAW);
524  $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
525  $oRemark->save();
526  }
527 
528  return $blRet;
529  }
530 
536  public function allowDerivedUpdate()
537  {
538  return true;
539  }
540 
548  public function inGroup($sGroupID)
549  {
550  $blIn = false;
551  if (($oGroups = $this->getUserGroups())) {
552  $blIn = isset($oGroups[$sGroupID]);
553  }
554 
555  return $blIn;
556  }
557 
566  public function delete($sOXID = null)
567  {
568 
569  if (!$sOXID) {
570  $sOXID = $this->getId();
571  }
572  if (!$sOXID) {
573  return false;
574  }
575 
576  $blDeleted = parent::delete($sOXID);
577 
578  if ($blDeleted) {
579  $oDb = oxDb::getDb();
580  $sOXIDQuoted = $oDb->quote($sOXID);
581 
582  // deleting stored payment, address, group dependencies, remarks info
583  $rs = $oDb->execute("delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}");
584  $rs = $oDb->execute("delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}");
585 
586  // deleting notice/wish lists
587  $rs = $oDb->execute("delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}");
588  $rs = $oDb->execute("delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}");
589 
590  // deleting newsletter subscription
591  $rs = $oDb->execute("delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}");
592 
593  // delivery and delivery sets
594  $rs = $oDb->execute("delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
595 
596  // discounts
597  $rs = $oDb->execute("delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
598 
599 
600  // and leaving all order related information
601  $rs = $oDb->execute("delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'");
602 
603  $blDeleted = $rs->EOF;
604  }
605 
606  return $blDeleted;
607  }
608 
616  public function load($oxID)
617  {
618 
619  $blRet = parent::load($oxID);
620 
621  // convert date's to international format
622  if (isset($this->oxuser__oxcreate->value)) {
623  $this->oxuser__oxcreate->setValue(oxRegistry::get("oxUtilsDate")->formatDBDate($this->oxuser__oxcreate->value));
624  }
625 
626  // change newsSubcription user id
627  if (isset($this->_oNewsSubscription)) {
628  $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($oxID, oxField::T_RAW);
629  }
630 
631  return $blRet;
632  }
633 
641  public function exists($sOXID = null)
642  {
643  if (!$sOXID) {
644  $sOXID = $this->getId();
645  }
646  //#5901 if physical record exists return true unconditionally
647  if (parent::exists($sOXID)) {
648  $this->setId($sOXID);
649  return true;
650  }
651 
652  //additional username check
653  //This part is used by not yet saved user object, to detect the case when such username exists in db.
654  //Basically it is called when anonymous visitor enters existing username for newsletter subscription
655  //see Newsletter::send()
656  //TODO: transfer this validation to newsletter part
657  $sShopSelect = '';
658  if (!$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
659  $sShopSelect = ' AND oxshopid = "' . $this->getConfig()->getShopId() . '" ';
660  }
661  $oDb = oxDb::getDb();
662  $sSelect = 'SELECT oxid FROM ' . $this->getViewName() . '
663  WHERE ( oxusername = ' . $oDb->quote($this->oxuser__oxusername->value) . ' ) ';
664  $sSelect .= $sShopSelect;
665 
666  if (($sOxid = $oDb->getOne($sSelect, false, false))) {
667  // update - set oxid
668  $this->setId($sOxid);
669 
670  return true;
671  }
672 
673  return false;
674  }
675 
684  public function getOrders($iLimit = false, $iPage = 0)
685  {
686  $oOrders = oxNew('oxList');
687  $oOrders->init('oxorder');
688 
689  if ($iLimit !== false) {
690  $oOrders->setSqlLimit($iLimit * $iPage, $iLimit);
691  }
692 
693  //P
694  // Lists does not support loading from two tables, so orders
695  // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
696  // forcing to load product info which is used in templates
697  // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
698 
699  //loading order for registered user
700  if ($this->oxuser__oxregister->value > 1) {
701  $oDb = oxDb::getDb();
702  $sQ = 'select * from oxorder where oxuserid = ' . $oDb->quote($this->getId()) . ' and oxorderdate >= ' . $oDb->quote($this->oxuser__oxregister->value) . ' ';
703 
704  //#1546 - shopid check added, if it is not multishop
705 
706  $sQ .= ' order by oxorderdate desc ';
707  $oOrders->selectString($sQ);
708  }
709 
710  return $oOrders;
711  }
712 
718  public function getOrderCount()
719  {
720  $iCnt = 0;
721  if ($this->getId() && $this->oxuser__oxregister->value > 1) {
722  $oDb = oxDb::getDb();
723  $sQ = 'select count(*) from oxorder where oxuserid = ' . $oDb->quote($this->getId()) . ' AND oxorderdate >= ' . $oDb->quote($this->oxuser__oxregister->value) . ' and oxshopid = "' . $this->getConfig()->getShopId() . '" ';
724  $iCnt = (int) $oDb->getOne($sQ);
725  }
726 
727  return $iCnt;
728  }
729 
735  public function getNoticeListArtCnt()
736  {
737  if ($this->_iCntNoticeListArticles === null) {
738  $this->_iCntNoticeListArticles = 0;
739  if ($this->getId()) {
740  $this->_iCntNoticeListArticles = $this->getBasket('noticelist')->getItemCount();
741  }
742  }
743 
745  }
746 
752  public function getWishListArtCnt()
753  {
754  if ($this->_iCntWishListArticles === null) {
755  $this->_iCntWishListArticles = false;
756  if ($this->getId()) {
757  $this->_iCntWishListArticles = $this->getBasket('wishlist')->getItemCount();
758  }
759  }
760 
762  }
763 
769  public function getEncodedDeliveryAddress()
770  {
771  return md5($this->_getMergedAddressFields());
772  }
773 
780  public function getActiveCountry()
781  {
782  $sDeliveryCountry = '';
783  $soxAddressId = oxRegistry::getSession()->getVariable('deladrid');
784  if ($soxAddressId) {
785  $oDelAddress = oxNew('oxaddress');
786  $oDelAddress->load($soxAddressId);
787  $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
788  } elseif ($this->getId()) {
789  $sDeliveryCountry = $this->oxuser__oxcountryid->value;
790  } else {
791  $oUser = oxNew('oxuser');
792  if ($oUser->loadActiveUser()) {
793  $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
794  }
795  }
796 
797  return $sDeliveryCountry;
798  }
799 
807  public function createUser()
808  {
809  $oDb = oxDb::getDb();
810  $sShopID = $this->getConfig()->getShopId();
811 
812  // check if user exists AND there is no password - in this case we update otherwise we try to insert
813  $sSelect = "select oxid from oxuser where oxusername = " . $oDb->quote($this->oxuser__oxusername->value) . " and oxpassword = '' ";
814  if (!$this->_blMallUsers) {
815  $sSelect .= " and oxshopid = '{$sShopID}' ";
816  }
817  $sOXID = $oDb->getOne($sSelect, false, false);
818 
819  // user without password found - lets use
820  if (isset($sOXID) && $sOXID) {
821  // try to update
822  $this->delete($sOXID);
823  } elseif ($this->_blMallUsers) {
824  // must be sure if there is no dublicate user
825  $sQ = "select oxid from oxuser where oxusername = " . $oDb->quote($this->oxuser__oxusername->value) . " and oxusername != '' ";
826  if ($oDb->getOne($sQ, false, false)) {
828  $oEx = oxNew('oxUserException');
829  $oLang = oxRegistry::getLang();
830  $oEx->setMessage(sprintf($oLang->translateString('ERROR_MESSAGE_USER_USEREXISTS', $oLang->getTplLanguage()), $this->oxuser__oxusername->value));
831  throw $oEx;
832  }
833  }
834 
835  $this->oxuser__oxshopid = new oxField($sShopID, oxField::T_RAW);
836  if (($blOK = $this->save())) {
837  // dropping/cleaning old delivery address/payment info
838  $oDb->execute("delete from oxaddress where oxaddress.oxuserid = " . $oDb->quote($this->oxuser__oxid->value) . " ");
839  $oDb->execute("update oxuserpayments set oxuserpayments.oxuserid = " . $oDb->quote($this->oxuser__oxusername->value) . " where oxuserpayments.oxuserid = " . $oDb->quote($this->oxuser__oxid->value) . " ");
840  } else {
842  $oEx = oxNew('oxUserException');
843  $oEx->setMessage('EXCEPTION_USER_USERCREATIONFAILED');
844  throw $oEx;
845  }
846 
847  return $blOK;
848  }
849 
857  public function addToGroup($sGroupID)
858  {
859  if (!$this->inGroup($sGroupID)) {
860  // create oxgroup object
861  $oGroup = oxNew('oxGroups');
862  if ($oGroup->load($sGroupID)) {
863  $oNewGroup = oxNew('oxobject2group');
864  $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
865  $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
866  if ($oNewGroup->save()) {
867  $this->_oGroups[$sGroupID] = $oGroup;
868 
869  return true;
870  }
871  }
872  }
873 
874  return false;
875  }
876 
882  public function removeFromGroup($sGroupID = null)
883  {
884  if ($sGroupID != null && $this->inGroup($sGroupID)) {
885  $oGroups = oxNew('oxList');
886  $oGroups->init('oxobject2group');
887  $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "' . $this->getId() . '" and oxobject2group.oxgroupsid = "' . $sGroupID . '" ';
888  $oGroups->selectString($sSelect);
889  foreach ($oGroups as $oRemgroup) {
890  if ($oRemgroup->delete()) {
891  unset($this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value]);
892  }
893  }
894  }
895  }
896 
903  public function onOrderExecute($oBasket, $iSuccess)
904  {
905 
906  if (is_numeric($iSuccess) && $iSuccess != 2 && $iSuccess <= 3) {
907  //adding user to particular customer groups
908  $myConfig = $this->getConfig();
909  $dMidlleCustPrice = (float) $myConfig->getConfigParam('sMidlleCustPrice');
910  $dLargeCustPrice = (float) $myConfig->getConfigParam('sLargeCustPrice');
911 
912  $this->addToGroup('oxidcustomer');
913  $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
914  if ($dBasketPrice < $dMidlleCustPrice) {
915  $this->addToGroup('oxidsmallcust');
916  }
917  if ($dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice) {
918  $this->addToGroup('oxidmiddlecust');
919  }
920  if ($dBasketPrice >= $dLargeCustPrice) {
921  $this->addToGroup('oxidgoodcust');
922  }
923 
924  if ($this->inGroup('oxidnotyetordered')) {
925  $this->removeFromGroup('oxidnotyetordered');
926  }
927  }
928  }
929 
937  public function getBasket($sName)
938  {
939  if (!isset($this->_aBaskets[$sName])) {
940  $oBasket = oxNew('oxuserbasket');
941  $aWhere = array('oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName);
942 
943  // creating if it does not exist
944  if (!$oBasket->assignRecord($oBasket->buildSelectString($aWhere))) {
945  $oBasket->oxuserbaskets__oxtitle = new oxField($sName);
946  $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
947 
948  // marking basket as new (it will not be saved in DB yet)
949  $oBasket->setIsNewBasket();
950  }
951 
952  $this->_aBaskets[$sName] = $oBasket;
953  }
954 
955  return $this->_aBaskets[$sName];
956  }
957 
966  public function convertBirthday($aData)
967  {
968 
969  // preparing data to process
970  $iYear = isset($aData['year']) ? ((int) $aData['year']) : false;
971  $iMonth = isset($aData['month']) ? ((int) $aData['month']) : false;
972  $iDay = isset($aData['day']) ? ((int) $aData['day']) : false;
973 
974  // leaving empty if not set
975  if (!$iYear && !$iMonth && !$iDay) {
976  return "";
977  }
978 
979  // year
980  if (!$iYear || $iYear < 1000 || $iYear > 9999) {
981  $iYear = date('Y');
982  }
983 
984  // month
985  if (!$iMonth || $iMonth < 1 || $iMonth > 12) {
986  $iMonth = 1;
987  }
988 
989  // maximum nuber of days in month
990  $iMaxDays = 31;
991  switch ($iMonth) {
992  case 2:
993  if ($iMaxDays > 28) {
994  $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
995  }
996  break;
997  case 4:
998  case 6:
999  case 9:
1000  case 11:
1001  $iMaxDays = min(30, $iMaxDays);
1002  break;
1003  }
1004 
1005  // day
1006  if (!$iDay || $iDay < 1 || $iDay > $iMaxDays) {
1007  $iDay = 1;
1008  }
1009 
1010  // whole date
1011  return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
1012  }
1013 
1019  public function getBoni()
1020  {
1021  if (!$iBoni = $this->getConfig()->getConfigParam('iCreditRating')) {
1022  $iBoni = 1000;
1023  }
1024 
1025  return $iBoni;
1026  }
1027 
1044  public function checkValues($sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress)
1045  {
1047  $oInputValidator = oxRegistry::get('oxInputValidator');
1048 
1049  // 1. checking user name
1050  $sLogin = $oInputValidator->checkLogin($this, $sLogin, $aInvAddress);
1051 
1052  // 2. checking email
1053  $oInputValidator->checkEmail($this, $sLogin, $aInvAddress);
1054 
1055  // 3. password
1056  $oInputValidator->checkPassword($this, $sPassword, $sPassword2, ((int) oxRegistry::getConfig()->getRequestParameter('option') == 3));
1057 
1058  // 4. required fields
1059  $oInputValidator->checkRequiredFields($this, $aInvAddress, $aDelAddress);
1060 
1061  // 5. country check
1062  $oInputValidator->checkCountries($this, $aInvAddress, $aDelAddress);
1063 
1064  // 6. vat id check.
1065  $oInputValidator->checkVatId($this, $aInvAddress);
1066 
1067 
1068  // throwing first validation error
1069  if ($oError = oxRegistry::get("oxInputValidator")->getFirstValidationError()) {
1070  throw $oError;
1071  }
1072  }
1073 
1083  public function setNewsSubscription($blSubscribe, $blSendOptIn, $blForceCheckOptIn = false)
1084  {
1085  // assigning to newsletter
1086  $blSuccess = false;
1087  $myConfig = $this->getConfig();
1088  $mySession = $this->getSession();
1089 
1090  // user wants to get newsletter messages or no ?
1091  $oNewsSubscription = $this->getNewsSubscription();
1092  if ($oNewsSubscription) {
1093  if ($blSubscribe && ($blForceCheckOptIn || ($iOptInStatus = $oNewsSubscription->getOptInStatus()) != 1)) {
1094  if (!$blSendOptIn) {
1095 
1096  // double-opt-in check is disabled - assigning automatically
1097  $this->addToGroup('oxidnewsletter');
1098  // and setting subscribed status
1099  $oNewsSubscription->setOptInStatus(1);
1100  $blSuccess = true;
1101  } else {
1102 
1103  // double-opt-in check enabled - sending confirmation email and setting waiting status
1104  if ($iOptInStatus != 2) {
1105  // sending double-opt-in mail
1106  $oEmail = oxNew('oxemail');
1107  $blSuccess = $oEmail->sendNewsletterDbOptInMail($this);
1108  } else {
1109  // mail already was sent, so just confirming that
1110  $blSuccess = true;
1111  }
1112 
1113  $oNewsSubscription->setOptInStatus(2);
1114  }
1115  } elseif (!$blSubscribe) {
1116  // removing user from newsletter subscribers
1117  $this->removeFromGroup('oxidnewsletter');
1118  $oNewsSubscription->setOptInStatus(0);
1119  $blSuccess = true;
1120  }
1121  }
1122 
1123  return $blSuccess;
1124  }
1125 
1141  public function changeUserData($sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress)
1142  {
1143  // validating values before saving. If validation fails - exception is thrown
1144  $this->checkValues($sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress);
1145  // input data is fine - lets save updated user info
1146 
1147  $this->assign($aInvAddress);
1148 
1149 
1150  // update old or add new delivery address
1151  $this->_assignAddress($aDelAddress);
1152 
1153  // saving new values
1154  if ($this->save()) {
1155 
1156  // assigning automatically to specific groups
1157  $sCountryId = isset($aInvAddress['oxuser__oxcountryid']) ? $aInvAddress['oxuser__oxcountryid'] : '';
1158  $this->_setAutoGroups($sCountryId);
1159  }
1160  }
1161 
1167  protected function _getMergedAddressFields()
1168  {
1169  $sDelAddress = '';
1170  $sDelAddress .= $this->oxuser__oxcompany;
1171  $sDelAddress .= $this->oxuser__oxusername;
1172  $sDelAddress .= $this->oxuser__oxfname;
1173  $sDelAddress .= $this->oxuser__oxlname;
1174  $sDelAddress .= $this->oxuser__oxstreet;
1175  $sDelAddress .= $this->oxuser__oxstreetnr;
1176  $sDelAddress .= $this->oxuser__oxaddinfo;
1177  $sDelAddress .= $this->oxuser__oxustid;
1178  $sDelAddress .= $this->oxuser__oxcity;
1179  $sDelAddress .= $this->oxuser__oxcountryid;
1180  $sDelAddress .= $this->oxuser__oxstateid;
1181  $sDelAddress .= $this->oxuser__oxzip;
1182  $sDelAddress .= $this->oxuser__oxfon;
1183  $sDelAddress .= $this->oxuser__oxfax;
1184  $sDelAddress .= $this->oxuser__oxsal;
1185 
1186  return $sDelAddress;
1187  }
1188 
1194  protected function _assignAddress($aDelAddress)
1195  {
1196  if (is_array($aDelAddress) && count($aDelAddress)) {
1197 
1198  $sAddressId = $this->getConfig()->getRequestParameter('oxaddressid');
1199  $sAddressId = ($sAddressId === null || $sAddressId == -1 || $sAddressId == -2) ? null : $sAddressId;
1200 
1201  $oAddress = oxNew('oxaddress');
1202  $oAddress->setId($sAddressId);
1203  $oAddress->load($sAddressId);
1204  $oAddress->assign($aDelAddress);
1205  $oAddress->oxaddress__oxuserid = new oxField($this->getId(), oxField::T_RAW);
1206  $oAddress->oxaddress__oxcountry = $this->getUserCountry($oAddress->oxaddress__oxcountryid->value);
1207  $oAddress->save();
1208 
1209  // resetting addresses
1210  $this->_aAddresses = null;
1211 
1212  // saving delivery Address for later use
1213  oxRegistry::getSession()->setVariable('deladrid', $oAddress->getId());
1214  } else {
1215  // resetting
1216  oxRegistry::getSession()->setVariable('deladrid', null);
1217  }
1218  }
1219 
1233  protected function _getLoginQueryHashedWithMD5($sUser, $sPassword, $sShopID, $blAdmin)
1234  {
1235  $myConfig = $this->getConfig();
1236  $oDb = oxDb::getDb();
1237 
1238  $sUserSelect = "oxuser.oxusername = " . $oDb->quote($sUser);
1239  $sPassSelect = " oxuser.oxpassword = BINARY MD5( CONCAT( " . $oDb->quote($sPassword) . ", UNHEX( oxuser.oxpasssalt ) ) ) ";
1240  $sShopSelect = "";
1241 
1242 
1243  // admin view: can only login with higher than 'user' rights
1244  if ($blAdmin) {
1245  $sShopSelect = " and ( oxrights != 'user' ) ";
1246  }
1247 
1248  $sSelect = "select `oxid` from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
1249 
1250 
1251  return $sSelect;
1252  }
1253 
1266  protected function _getLoginQuery($sUser, $sPassword, $sShopID, $blAdmin)
1267  {
1268  $myConfig = $this->getConfig();
1269  $oDb = oxDb::getDb();
1270 
1271  $sUserSelect = "oxuser.oxusername = " . $oDb->quote($sUser);
1272 
1273  $sShopSelect = "";
1274  // admin view: can only login with higher than 'user' rights
1275  if ($blAdmin) {
1276  $sShopSelect = " and ( oxrights != 'user' ) ";
1277  }
1278 
1279  $sSalt = $oDb->getOne("SELECT `oxpasssalt` FROM `oxuser` WHERE " . $sUserSelect . $sShopSelect);
1280 
1281  $sPassSelect = " oxuser.oxpassword = " . $oDb->quote($this->encodePassword($sPassword, $sSalt));
1282 
1283  $sSelect = "select `oxid` from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
1284 
1285 
1286  return $sSelect;
1287  }
1288 
1298  protected function _getShopSelect($myConfig, $sShopID, $blAdmin)
1299  {
1300  $sShopSelect = "";
1301  // admin view: can only login with higher than 'user' rights
1302  if ($blAdmin) {
1303  $sShopSelect = " and ( oxrights != 'user' ) ";
1304  }
1305 
1306  return $sShopSelect;
1307  }
1308 
1323  public function login($sUser, $sPassword, $blCookie = false)
1324  {
1325  if ($this->isAdmin() && !count(oxRegistry::get("oxUtilsServer")->getOxCookie())) {
1327  $oEx = oxNew('oxCookieException');
1328  $oEx->setMessage('ERROR_MESSAGE_COOKIE_NOCOOKIE');
1329  throw $oEx;
1330  }
1331 
1332  $oConfig = $this->getConfig();
1333 
1334 
1335  if ($sPassword) {
1336 
1337  $sShopID = $oConfig->getShopId();
1338  $this->_dbLogin($sUser, $sPassword, $sShopID);
1339  }
1340 
1341 
1342 
1343 
1344  //login successful?
1345  if ($this->oxuser__oxid->value) {
1346 
1347  // yes, successful login
1348 
1349  //resetting active user
1350  $this->setUser(null);
1351 
1352  if ($this->isAdmin()) {
1353  oxRegistry::getSession()->setVariable('auth', $this->oxuser__oxid->value);
1354  } else {
1355  oxRegistry::getSession()->setVariable('usr', $this->oxuser__oxid->value);
1356  }
1357 
1358  // cookie must be set ?
1359  if ($blCookie && $oConfig->getConfigParam('blShowRememberMe')) {
1360  oxRegistry::get("oxUtilsServer")->setUserCookie($this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $oConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value);
1361  }
1362 
1363  return true;
1364  } else {
1366  $oEx = oxNew('oxUserException');
1367  $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
1368  throw $oEx;
1369  }
1370  }
1371 
1377  public function logout()
1378  {
1379  // deleting session info
1380  oxRegistry::getSession()->deleteVariable('usr'); // for front end
1381  oxRegistry::getSession()->deleteVariable('auth'); // for back end
1382  oxRegistry::getSession()->deleteVariable('dynvalue');
1383  oxRegistry::getSession()->deleteVariable('paymentid');
1384  // oxRegistry::getSession()->deleteVariable( 'deladrid' );
1385 
1386  // delete cookie
1387  oxRegistry::get("oxUtilsServer")->deleteUserCookie($this->getConfig()->getShopID());
1388 
1389  // unsetting global user
1390  $this->setUser(null);
1391 
1392  return true;
1393  }
1394 
1401  public function loadAdminUser()
1402  {
1403  return $this->loadActiveUser(true);
1404  }
1405 
1414  public function loadActiveUser($blForceAdmin = false)
1415  {
1416  $oConfig = $this->getConfig();
1417 
1418  $blAdmin = $this->isAdmin() || $blForceAdmin;
1419 
1420  // first - checking session info
1421  $sUserID = $blAdmin ? oxRegistry::getSession()->getVariable('auth') : oxRegistry::getSession()->getVariable('usr');
1422 
1423  // trying automatic login (by 'remember me' cookie)
1424  $blFoundInCookie = false;
1425  if (!$sUserID && !$blAdmin && $oConfig->getConfigParam('blShowRememberMe')) {
1426  $sUserID = $this->_getCookieUserId();
1427  $blFoundInCookie = $sUserID ? true : false;
1428  }
1429 
1430  // @deprecated since v5.3 (2016-05-20); Facebook will be extracted into module.
1431  // If facebook connection is enabled, trying to login user using Facebook ID
1432  if (!$sUserID && !$blAdmin && $oConfig->getConfigParam("bl_showFbConnect")) {
1433  $sUserID = $this->_getFacebookUserId();
1434  }
1435  // END deprecated
1436 
1437  // checking user results
1438  if ($sUserID) {
1439  if ($this->load($sUserID)) {
1440  // storing into session
1441  if ($blAdmin) {
1442  oxRegistry::getSession()->setVariable('auth', $sUserID);
1443  } else {
1444  oxRegistry::getSession()->setVariable('usr', $sUserID);
1445  }
1446 
1447  // marking the way user was loaded
1448  $this->_blLoadedFromCookie = $blFoundInCookie;
1449 
1450  return true;
1451  }
1452  } else {
1453  // no user
1454  if ($blAdmin) {
1455  oxRegistry::getSession()->deleteVariable('auth');
1456  } else {
1457  oxRegistry::getSession()->deleteVariable('usr');
1458  }
1459 
1460  return false;
1461  }
1462  }
1463 
1471  protected function _getFacebookUserId()
1472  {
1473  $oDb = oxDb::getDb();
1474  $oFb = oxRegistry::get("oxFb");
1475  $oConfig = $this->getConfig();
1476  if ($oFb->isConnected() && $oFb->getUser()) {
1477  $sUserSelect = "oxuser.oxfbid = " . $oDb->quote($oFb->getUser());
1478  $sShopSelect = "";
1479 
1480 
1481  $sSelect = "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
1482  $sUserID = $oDb->getOne($sSelect);
1483  }
1484 
1485  return $sUserID;
1486  }
1487 
1493  protected function _getCookieUserId()
1494  {
1495  $sUserID = null;
1496  $oConfig = $this->getConfig();
1497  $sShopID = $oConfig->getShopId();
1498  if (($sSet = oxRegistry::get("oxUtilsServer")->getUserCookie($sShopID))) {
1499  $oDb = oxDb::getDb();
1500  $aData = explode('@@@', $sSet);
1501  $sUser = $aData[0];
1502  $sPWD = @$aData[1];
1503 
1504  $sSelect = 'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and oxuser.oxactive = 1 and oxuser.oxusername = ' . $oDb->quote($sUser);
1505 
1506  $rs = $oDb->select($sSelect);
1507  if ($rs != false && $rs->recordCount() > 0) {
1508  while (!$rs->EOF) {
1509  $sTest = crypt($rs->fields[1], $rs->fields[2]);
1510  if ($sTest == $sPWD) {
1511  // found
1512  $sUserID = $rs->fields[0];
1513  break;
1514  }
1515  $rs->moveNext();
1516  }
1517  }
1518  // if cookie info is not valid, remove it.
1519  if (!$sUserID) {
1520  oxRegistry::get('oxUtilsServer')->deleteUserCookie($sShopID);
1521  }
1522  }
1523 
1524  return $sUserID;
1525  }
1526 
1539  protected function _ldapLogin($sUser, $sPassword, $sShopID, $sShopSelect)
1540  {
1541  $aLDAPParams = $this->getConfig()->getConfigParam('aLDAPParams');
1542  $oLDAP = oxNew("oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT']);
1543 
1544  // maybe this is LDAP user but supplied email Address instead of LDAP login
1545  $oDb = oxDb::getDb();
1546  $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = " . $oDb->quote($sUser) . " $sShopSelect");
1547  if (isset($sLDAPKey) && $sLDAPKey) {
1548  $sUser = $sLDAPKey;
1549  }
1550 
1551  //$throws oxConnectionException
1552  $oLDAP->login($sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
1553 
1554  $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
1555  if (isset($aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
1556  // login successful
1557 
1558  // check if user is already in database
1559  $sSelect = "select oxid from oxuser where oxuser.oxusername = " . $oDb->quote($aData['OXUSERNAME']) . " $sShopSelect";
1560  $sOXID = $oDb->getOne($sSelect);
1561 
1562  if (!isset($sOXID) || !$sOXID) {
1563  // we need to create a new user
1564  //$oUser->oxuser__oxid->setValue($oUser->setId());
1565  $this->setId();
1566 
1567  // map all user data fields
1568  foreach ($aData as $fldname => $value) {
1569  $sField = "oxuser__" . strtolower($fldname);
1570  $this->$sField = new oxField($aData[$fldname]);
1571  }
1572 
1573  $this->oxuser__oxactive = new oxField(1);
1574  $this->oxuser__oxshopid = new oxField($sShopID);
1575  $this->oxuser__oxldapkey = new oxField($sUser);
1576  $this->oxuser__oxrights = new oxField("user");
1577  $this->setPassword("ldap user");
1578 
1579  $this->save();
1580  } else {
1581  // LDAP user is already in OXID DB, load it
1582  $this->load($sOXID);
1583  }
1584  } else {
1586  $oEx = oxNew('oxUserException');
1587  $oEx->setMessage('EXCEPTION_USER_NOVALUES');
1588  throw $oEx;
1589  }
1590  }
1591 
1598  protected function _getUserRights()
1599  {
1600  // previously user had no rights defined
1601  if (!$this->oxuser__oxrights->value) {
1602  return 'user';
1603  }
1604 
1605  $oDb = oxDb::getDb();
1606  $myConfig = $this->getConfig();
1607  $sAuthRights = null;
1608 
1609  // choosing possible user rights index
1610  $sAuthUserID = $this->isAdmin() ? oxRegistry::getSession()->getVariable('auth') : null;
1611  $sAuthUserID = $sAuthUserID ? $sAuthUserID : oxRegistry::getSession()->getVariable('usr');
1612  if ($sAuthUserID) {
1613  $sAuthRights = $oDb->getOne('select oxrights from ' . $this->getViewName() . ' where oxid=' . $oDb->quote($sAuthUserID));
1614  }
1615 
1616  //preventing user rights edit for non admin
1617  $aRights = array();
1618 
1619  // selecting current users rights ...
1620  if ($sCurrRights = $oDb->getOne('select oxrights from ' . $this->getViewName() . ' where oxid=' . $oDb->quote($this->getId()))) {
1621  $aRights[] = $sCurrRights;
1622  }
1623  $aRights[] = 'user';
1624 
1625  if (!$sAuthRights || !($sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId())) {
1626  return current($aRights);
1627  } elseif ($sAuthRights == $myConfig->getShopId()) {
1628  $aRights[] = $sAuthRights;
1629  if (!in_array($this->oxuser__oxrights->value, $aRights)) {
1630  return current($aRights);
1631  }
1632  }
1633 
1634  // leaving as it was set ...
1635  return $this->oxuser__oxrights->value;
1636  }
1637 
1643  protected function _insert()
1644  {
1645 
1646  // set oxcreate date
1647  $this->oxuser__oxcreate = new oxField(date('Y-m-d H:i:s'), oxField::T_RAW);
1648 
1649  if (!isset($this->oxuser__oxboni->value)) {
1650  $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
1651  }
1652 
1653  return parent::_insert();
1654  }
1655 
1661  protected function _update()
1662  {
1663  //V #M418: for not registered users, don't change boni during update
1664  if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
1665  $this->_aSkipSaveFields[] = 'oxboni';
1666  }
1667 
1668  // don't change this field
1669  $this->_aSkipSaveFields[] = 'oxcreate';
1670  if (!$this->isAdmin()) {
1671  $this->_aSkipSaveFields[] = 'oxcustnr';
1672  $this->_aSkipSaveFields[] = 'oxrights';
1673  }
1674 
1675  // updating subscription information
1676  if (($blUpdate = parent::_update())) {
1677  $this->getNewsSubscription()->updateSubscription($this);
1678  }
1679 
1680  return $blUpdate;
1681  }
1682 
1690  public function checkIfEmailExists($sEmail)
1691  {
1692  $myConfig = $this->getConfig();
1693  $oDb = oxDb::getDb();
1694  $iShopId = $myConfig->getShopId();
1695  $blExists = false;
1696 
1697  $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = ' . $oDb->quote($sEmail);
1698  if (($sOxid = $this->getId())) {
1699  $sQ .= " and oxid <> " . $oDb->quote($sOxid);
1700  }
1701  $oRs = $oDb->select($sQ, false, false);
1702  if ($oRs != false && $oRs->recordCount() > 0) {
1703 
1704  if ($this->_blMallUsers) {
1705 
1706  $blExists = true;
1707  if ($oRs->fields[1] == 'user' && !$oRs->fields[2]) {
1708 
1709  // password is not set - allow to override
1710  $blExists = false;
1711  }
1712  } else {
1713 
1714  $blExists = false;
1715  while (!$oRs->EOF) {
1716  if ($oRs->fields[1] != 'user') {
1717 
1718  // exists admin with same login - must not allow
1719  $blExists = true;
1720  break;
1721  } elseif ($oRs->fields[0] == $iShopId && $oRs->fields[2]) {
1722 
1723  // exists same login (with password) in same shop
1724  $blExists = true;
1725  break;
1726  }
1727 
1728  $oRs->moveNext();
1729  }
1730  }
1731  }
1732 
1733  return $blExists;
1734  }
1735 
1745  public function getUserRecommLists($sOXID = null)
1746  {
1747  if (!$sOXID) {
1748  $sOXID = $this->getId();
1749  }
1750 
1751  // sets active page
1752  $iActPage = (int) oxRegistry::getConfig()->getRequestParameter('pgNr');
1753  $iActPage = ($iActPage < 0) ? 0 : $iActPage;
1754 
1755  // load only lists which we show on screen
1756  $iNrofCatArticles = $this->getConfig()->getConfigParam('iNrofCatArticles');
1757  $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
1758 
1759 
1760  $oRecommList = oxNew('oxList');
1761  $oRecommList->init('oxrecommlist');
1762  $oRecommList->setSqlLimit($iNrofCatArticles * $iActPage, $iNrofCatArticles);
1763  $iShopId = $this->getConfig()->getShopId();
1764  $sSelect = 'select * from oxrecommlists where oxuserid =' . oxDb::getDb()->quote($sOXID) . ' and oxshopid ="' . $iShopId . '"';
1765  $oRecommList->selectString($sSelect);
1766 
1767  return $oRecommList;
1768  }
1769 
1779  public function getRecommListsCount($sOx = null)
1780  {
1781  if (!$sOx) {
1782  $sOx = $this->getId();
1783  }
1784 
1785  if ($this->_iCntRecommLists === null || $sOx) {
1786  $oDb = oxDb::getDb();
1787  $this->_iCntRecommLists = 0;
1788  $iShopId = $this->getConfig()->getShopId();
1789  $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote($sOx) . ' and oxshopid ="' . $iShopId . '"';
1790  $this->_iCntRecommLists = $oDb->getOne($sSelect);
1791  }
1792 
1793  return $this->_iCntRecommLists;
1794  }
1795 
1802  protected function _setAutoGroups($sCountryId)
1803  {
1804  // assigning automatically to specific groups
1805  $blForeigner = true;
1806  $blForeignGroupExists = false;
1807  $blInlandGroupExists = false;
1808 
1809  $aHomeCountry = $this->getConfig()->getConfigParam('aHomeCountry');
1810  // foreigner ?
1811  if (is_array($aHomeCountry)) {
1812  if (in_array($sCountryId, $aHomeCountry)) {
1813  $blForeigner = false;
1814  }
1815  } elseif ($sCountryId == $aHomeCountry) {
1816  $blForeigner = false;
1817  }
1818 
1819  if ($this->inGroup('oxidforeigncustomer')) {
1820  $blForeignGroupExists = true;
1821  if (!$blForeigner) {
1822  $this->removeFromGroup('oxidforeigncustomer');
1823  }
1824  }
1825 
1826  if ($this->inGroup('oxidnewcustomer')) {
1827  $blInlandGroupExists = true;
1828  if ($blForeigner) {
1829  $this->removeFromGroup('oxidnewcustomer');
1830  }
1831  }
1832 
1833  if (!$blForeignGroupExists && $blForeigner) {
1834  $this->addToGroup('oxidforeigncustomer');
1835  }
1836  if (!$blInlandGroupExists && !$blForeigner) {
1837  $this->addToGroup('oxidnewcustomer');
1838  }
1839  }
1840 
1841 
1850  public function loadUserByUpdateId($sUid)
1851  {
1852  $oDb = oxDb::getDb();
1853  $sQ = "select oxid from " . $this->getViewName() . " where oxupdateexp >= " . time() . " and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = " . $oDb->quote($sUid);
1854  if ($sUserId = $oDb->getOne($sQ)) {
1855  return $this->load($sUserId);
1856  }
1857  }
1858 
1864  public function setUpdateKey($blReset = false)
1865  {
1866  $sUpKey = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
1867  $iUpTime = $blReset ? 0 : oxRegistry::get("oxUtilsDate")->getTime() + $this->getUpdateLinkTerm();
1868 
1869  // generating key
1870  $this->oxuser__oxupdatekey = new oxField($sUpKey, oxField::T_RAW);
1871 
1872  // setting expiration time for 6 hours
1873  $this->oxuser__oxupdateexp = new oxField($iUpTime, oxField::T_RAW);
1874 
1875  // saving
1876  $this->save();
1877  }
1878 
1884  public function getUpdateLinkTerm()
1885  {
1886  return 3600 * 6;
1887  }
1888 
1896  public function isExpiredUpdateId($sKey)
1897  {
1898  $oDb = oxDb::getDb();
1899  $sQ = "select 1 from " . $this->getViewName() . " where oxupdateexp >= " . time() . " and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = " . $oDb->quote($sKey);
1900 
1901  return !((bool) $oDb->getOne($sQ));
1902  }
1903 
1909  public function getUpdateId()
1910  {
1911  if ($this->_sUpdateKey === null) {
1912  $this->setUpdateKey();
1913  $this->_sUpdateKey = md5($this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value);
1914  }
1915 
1916  return $this->_sUpdateKey;
1917  }
1918 
1927  public function encodePassword($sPassword, $sSalt)
1928  {
1930  $oSha512Hasher = oxNew('oxSha512Hasher');
1932  $oHasher = oxNew('oxPasswordHasher', $oSha512Hasher);
1933 
1934  return $oHasher->hash($sPassword, $sSalt);
1935  }
1936 
1946  public function prepareSalt($sSalt)
1947  {
1949  $oOpenSSLFunctionalityChecker = oxNew('oxOpenSSLFunctionalityChecker');
1951  $oGenerator = oxNew('oxPasswordSaltGenerator', $oOpenSSLFunctionalityChecker);
1952 
1953  return $oGenerator->generate();
1954  }
1955 
1965  public function decodeSalt($sSaltHex)
1966  {
1967  return ($sSaltHex ? oxDb::getDb()->getOne("select UNHEX( '{$sSaltHex}' )") : '');
1968  }
1969 
1975  public function setPassword($sPassword = null)
1976  {
1978  $oOpenSSLFunctionalityChecker = oxNew('oxOpenSSLFunctionalityChecker');
1979  // setting salt if password is not empty
1981  $oSaltGenerator = oxNew('oxPasswordSaltGenerator', $oOpenSSLFunctionalityChecker);
1982 
1983  $sSalt = $sPassword ? $oSaltGenerator->generate() : '';
1984 
1985  // encoding only if password was not empty (e.g. user registration without pass)
1986  $sPassword = $sPassword ? $this->encodePassword($sPassword, $sSalt) : '';
1987 
1988  $this->oxuser__oxpassword = new oxField($sPassword, oxField::T_RAW);
1989  $this->oxuser__oxpasssalt = new oxField($sSalt, oxField::T_RAW);
1990  }
1991 
1999  public function isSamePassword($sNewPass)
2000  {
2001  return $this->encodePassword($sNewPass, $this->oxuser__oxpasssalt->value) == $this->oxuser__oxpassword->value;
2002  }
2003 
2009  public function isLoadedFromCookie()
2010  {
2012  }
2013 
2022  public function getPasswordHash()
2023  {
2024  $sHash = null;
2025  if ($this->oxuser__oxpassword->value) {
2026  $sHash = $this->oxuser__oxpassword->value;
2027  }
2028 
2029  return $sHash;
2030  }
2031 
2039  public function getReviewUserHash($sUserId)
2040  {
2041  $oDb = oxDb::getDb();
2042  $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote($sUserId) . '');
2043 
2044  return $sReviewUserHash;
2045  }
2046 
2054  public function getReviewUserId($sReviewUserHash)
2055  {
2056  $oDb = oxDb::getDb();
2057  $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote($sReviewUserHash) . '');
2058 
2059  return $sUserId;
2060  }
2061 
2069  public function getState()
2070  {
2071  return $this->getStateId();
2072  }
2073 
2079  public function getStateId()
2080  {
2081  return $this->oxuser__oxstateid->value;
2082  }
2083 
2091  public function getStateTitle($sId = null)
2092  {
2093  $oState = $this->_getStateObject();
2094 
2095  if (is_null($sId)) {
2096  $sId = $this->getStateId();
2097  }
2098 
2099  return $oState->getTitleById($sId);
2100  }
2101 
2107  public function isTermsAccepted()
2108  {
2109  $oDb = oxDb::getDb();
2110  $sShopId = $this->getConfig()->getShopId();
2111  $sUserId = $oDb->quote($this->getId());
2112 
2113  return (bool) $oDb->getOne("select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'");
2114  }
2115 
2119  public function acceptTerms()
2120  {
2121  $oDb = oxDb::getDb();
2122  $sUserId = $oDb->quote($this->getId());
2123  $sShopId = $this->getConfig()->getShopId();
2124  $sVersion = oxNew("oxcontent")->getTermsVersion();
2125 
2126  $oDb->execute("replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'");
2127  }
2128 
2138  public function setCreditPointsForRegistrant($sUserId, $sRecEmail)
2139  {
2140  $blSet = false;
2141  $oDb = oxDb::getDb();
2142  $iPoints = $this->getConfig()->getConfigParam('dPointsForRegistration');
2143  // check if this invitation is still not accepted
2144  $iPending = $oDb->getOne("select count(oxuserid) from oxinvitations where oxuserid = " . $oDb->quote($sUserId) . " and md5(oxemail) = " . $oDb->quote($sRecEmail) . " and oxpending = 1 and oxaccepted = 0", false, false);
2145  if ($iPoints && $iPending) {
2146  $this->oxuser__oxpoints = new oxField($iPoints, oxField::T_RAW);
2147  if ($blSet = $this->save()) {
2148  // updating users statistics
2149  $oDb->execute("UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = " . $oDb->quote($sUserId) . " and md5(oxemail) = " . $oDb->quote($sRecEmail));
2150  $oInvUser = oxNew("oxuser");
2151  if ($oInvUser->load($sUserId)) {
2152  $blSet = $oInvUser->setCreditPointsForInviter();
2153  }
2154  }
2155  }
2156  oxRegistry::getSession()->deleteVariable('su');
2157  oxRegistry::getSession()->deleteVariable('re');
2158 
2159  return $blSet;
2160  }
2161 
2167  public function setCreditPointsForInviter()
2168  {
2169  $blSet = false;
2170  $iPoints = $this->getConfig()->getConfigParam('dPointsForInvitation');
2171  if ($iPoints) {
2172  $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
2173  $this->oxuser__oxpoints = new oxField($iNewPoints, oxField::T_RAW);
2174  $blSet = $this->save();
2175  }
2176 
2177  return $blSet;
2178  }
2179 
2187  public function updateFbId()
2188  {
2189  $oFb = oxRegistry::get("oxFb");
2190  $blRet = false;
2191 
2192  if ($oFb->isConnected() && $oFb->getUser()) {
2193  $this->oxuser__oxfbid = new oxField($oFb->getUser());
2194  $blRet = $this->save();
2195  }
2196 
2197  return $blRet;
2198  }
2199 
2205  public function updateInvitationStatistics($aRecEmail)
2206  {
2207  $oDb = oxDb::getDb();
2208  $sUserId = $this->getId();
2209 
2210  if ($sUserId && is_array($aRecEmail) && count($aRecEmail) > 0) {
2211  //iserting statistics about invitation
2212  $sDate = oxRegistry::get("oxUtilsDate")->formatDBDate(date("Y-m-d"), true);
2213  $aRecEmail = oxDb::getInstance()->quoteArray($aRecEmail);
2214  foreach ($aRecEmail as $sRecEmail) {
2215  $sSql = "INSERT INTO oxinvitations SET oxuserid = " . $oDb->quote($sUserId) . ", oxemail = $sRecEmail, oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
2216  $oDb->execute($sSql);
2217  }
2218  }
2219  }
2220 
2228  public function getIdByUserName($sUserName)
2229  {
2230  $oDb = oxDb::getDb();
2231  $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = " . $oDb->quote($sUserName);
2232  if (!$this->getConfig()->getConfigParam('blMallUsers')) {
2233  $sQ .= " AND `oxshopid` = " . $oDb->quote($this->getConfig()->getShopId());
2234  }
2235 
2236  return $oDb->getOne($sQ);
2237  }
2238 
2244  public function hasAccount()
2245  {
2246 
2247  return (bool) $this->oxuser__oxpassword->value;
2248  }
2249 
2255  public function isPriceViewModeNetto()
2256  {
2257  return (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
2258  }
2259 
2269  protected function _dbLogin($sUser, $sPassword, $sShopID)
2270  {
2271  $blOldHash = false;
2272  $oDb = oxDb::getDb();
2273 
2274  if ($this->_isDemoShop() && $this->isAdmin()) {
2275  $sUserOxId = $oDb->getOne($this->_getDemoShopLoginQuery($sUser, $sPassword));
2276  } else {
2277  $sUserOxId = $oDb->getOne($this->_getLoginQuery($sUser, $sPassword, $sShopID, $this->isAdmin()));
2278  if (!$sUserOxId) {
2279  $sUserOxId = $oDb->getOne($this->_getLoginQueryHashedWithMD5($sUser, $sPassword, $sShopID, $this->isAdmin()));
2280  $blOldHash = true;
2281  }
2282  }
2283 
2284  if ($sUserOxId) {
2285  if (!$this->load($sUserOxId)) {
2287  $oEx = oxNew('oxUserException');
2288  $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
2289  throw $oEx;
2290  } elseif ($blOldHash && $this->getId()) {
2291  $this->setPassword($sPassword);
2292  $this->save();
2293  }
2294  }
2295  }
2296 
2302  protected function _isDemoShop()
2303  {
2304  $blDemoMode = false;
2305 
2306  if ($this->getConfig()->isDemoShop()) {
2307  $blDemoMode = true;
2308  }
2309 
2310  return $blDemoMode;
2311  }
2312 
2323  protected function _getDemoShopLoginQuery($sUser, $sPassword)
2324  {
2325  if ($sPassword == "admin" && $sUser == "admin") {
2326  $sSelect = "SELECT `oxid` FROM `oxuser` WHERE `oxrights` = 'malladmin' ";
2327  } else {
2329  $oEx = oxNew('oxUserException');
2330  $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
2331  throw $oEx;
2332  }
2333 
2334  return $sSelect;
2335  }
2336 }