oxuser.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class oxUser extends oxBase
00010 {
00011 
00017     protected $_blDisableShopCheck = true;
00018 
00024     protected $_oNewsSubscription = null;
00025 
00031     protected $_sClassName = 'oxuser';
00032 
00038     protected $_aBaskets = array();
00039 
00045     protected $_oGroups;
00046 
00052     protected $_aAddresses = array();
00053 
00059     protected $_oPayments;
00060 
00066     protected $_oRecommList;
00067 
00073     protected $_blMallUsers = false;
00074 
00080     protected static $_aUserCookie = array();
00081 
00087     protected $_iCntNoticeListArticles = null;
00088 
00094     protected $_iCntWishListArticles = null;
00095 
00101     protected $_iCntRecommLists = null;
00102 
00108     protected $_sUpdateKey = null;
00109 
00115     protected $_blLoadedFromCookie = null;
00116 
00122     protected $_sSelAddressId = null;
00123 
00129     protected $_oSelAddress = null;
00130 
00136     protected $_sWishId = null;
00137 
00143     protected $_oUserCountryTitle = null;
00144 
00148     protected $_oStateObject = null;
00149 
00155     protected function _getStateObject()
00156     {
00157         if (is_null($this->_oStateObject)) {
00158             $this->_oStateObject = oxNew('oxState');
00159         }
00160 
00161         return $this->_oStateObject;
00162     }
00163 
00169     public function __construct()
00170     {
00171         $this->setMallUsersStatus($this->getConfig()->getConfigParam('blMallUsers'));
00172 
00173         parent::__construct();
00174         $this->init('oxuser');
00175     }
00176 
00182     public function setMallUsersStatus($blOn = false)
00183     {
00184         $this->_blMallUsers = $blOn;
00185     }
00186 
00194     public function __get($sParamName)
00195     {
00196         // it saves memory using - loads data only if it is used
00197         switch ($sParamName) {
00198             case 'oGroups':
00199                 return $this->_oGroups = $this->getUserGroups();
00200                 break;
00201             case 'iCntNoticeListArticles':
00202                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00203                 break;
00204             case 'iCntWishListArticles':
00205                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00206                 break;
00207             case 'iCntRecommLists':
00208                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00209                 break;
00210             case 'oAddresses':
00211                 return $this->getUserAddresses();
00212                 break;
00213             case 'oPayments':
00214                 return $this->_oPayments = $this->getUserPayments();
00215                 break;
00216             case 'oxuser__oxcountry':
00217                 return $this->oxuser__oxcountry = $this->getUserCountry();
00218                 break;
00219             case 'sDBOptin':
00220                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00221                 break;
00222             case 'sEmailFailed':
00223                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00224                 break;
00225         }
00226     }
00227 
00233     public function getNewsSubscription()
00234     {
00235         if ($this->_oNewsSubscription !== null) {
00236             return $this->_oNewsSubscription;
00237         }
00238 
00239         $this->_oNewsSubscription = oxNew('oxnewssubscribed');
00240 
00241         // if subscription object is not set yet - we should create one
00242         if (!$this->_oNewsSubscription->loadFromUserId($this->getId())) {
00243             if (!$this->_oNewsSubscription->loadFromEmail($this->oxuser__oxusername->value)) {
00244 
00245                 // no subscription defined yet - creating one
00246                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00247                 $this->_oNewsSubscription->oxnewssubscribed__oxemail = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00248                 $this->_oNewsSubscription->oxnewssubscribed__oxsal = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00249                 $this->_oNewsSubscription->oxnewssubscribed__oxfname = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00250                 $this->_oNewsSubscription->oxnewssubscribed__oxlname = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00251             }
00252         }
00253 
00254         return $this->_oNewsSubscription;
00255     }
00256 
00266     public function getUserCountry($sCountryId = null, $iLang = null)
00267     {
00268         if ($this->_oUserCountryTitle == null || $sCountryId) {
00269             $sId = $sCountryId ? $sCountryId : $this->oxuser__oxcountryid->value;
00270             $oDb = oxDb::getDb();
00271             $sViewName = getViewName('oxcountry', $iLang);
00272             $sQ = "select oxtitle from {$sViewName} where oxid = " . $oDb->quote($sId) . " ";
00273             $oCountry = new oxField($oDb->getOne($sQ), oxField::T_RAW);
00274             if (!$sCountryId) {
00275                 $this->_oUserCountryTitle = $oCountry;
00276             }
00277         } else {
00278             return $this->_oUserCountryTitle;
00279         }
00280 
00281         return $oCountry;
00282     }
00283 
00291     public function getUserCountryId($sCountry = null)
00292     {
00293         $oDb = oxDb::getDb();
00294         $sQ = "select oxid from " . getviewName("oxcountry") . " where oxactive = '1' and oxisoalpha2 = " . $oDb->quote($sCountry) . " ";
00295         $sCountryId = $oDb->getOne($sQ);
00296 
00297         return $sCountryId;
00298     }
00299 
00307     public function getUserGroups($sOXID = null)
00308     {
00309 
00310         if (isset($this->_oGroups)) {
00311             return $this->_oGroups;
00312         }
00313 
00314         if (!$sOXID) {
00315             $sOXID = $this->getId();
00316         }
00317 
00318         $sViewName = getViewName("oxgroups");
00319         $this->_oGroups = oxNew('oxList', 'oxgroups');
00320         $sSelect = "select {$sViewName}.* from {$sViewName} left join oxobject2group on oxobject2group.oxgroupsid = {$sViewName}.oxid
00321                      where oxobject2group.oxobjectid = " . oxDb::getDb()->quote($sOXID);
00322         $this->_oGroups->selectString($sSelect);
00323 
00324         return $this->_oGroups;
00325     }
00326 
00334     public function getUserAddresses($sUserId = null)
00335     {
00336         $sUserId = isset($sUserId) ? $sUserId : $this->getId();
00337         if (!isset($this->_aAddresses[$sUserId])) {
00338             $oUserAddressList = oxNew('oxUserAddressList');
00339             $oUserAddressList->load($sUserId);
00340             $this->_aAddresses[$sUserId] = $oUserAddressList;
00341 
00342             // marking selected
00343             if ($sAddressId = $this->getSelectedAddressId()) {
00344                 foreach ($this->_aAddresses[$sUserId] as $oAddress) {
00345                     if ($oAddress->getId() === $sAddressId) {
00346                         $oAddress->setSelected();
00347                         break;
00348                     }
00349                 }
00350             }
00351         }
00352 
00353         return $this->_aAddresses[$sUserId];
00354     }
00355 
00361     public function setSelectedAddressId($sAddressId)
00362     {
00363         $this->_sSelAddressId = $sAddressId;
00364     }
00365 
00371     public function getSelectedAddressId()
00372     {
00373         if ($this->_sSelAddressId !== null) {
00374             return $this->_sSelAddressId;
00375         }
00376 
00377         $sAddressId = oxRegistry::getConfig()->getRequestParameter("oxaddressid");
00378         if (!$sAddressId && !oxRegistry::getConfig()->getRequestParameter('reloadaddress')) {
00379             $sAddressId = oxRegistry::getSession()->getVariable("deladrid");
00380         }
00381 
00382         return $sAddressId;
00383     }
00384 
00390     protected function _getWishListId()
00391     {
00392         $this->_sWishId = null;
00393         // check if we have to set it here
00394         $oBasket = $this->getSession()->getBasket();
00395         foreach ($oBasket->getContents() as $oBasketItem) {
00396             if ($this->_sWishId = $oBasketItem->getWishId()) {
00397                 // stop on first found
00398                 break;
00399             }
00400         }
00401 
00402         return $this->_sWishId;
00403     }
00404 
00413     public function getSelectedAddress($sWishId = false)
00414     {
00415         if ($this->_oSelAddress !== null) {
00416             return $this->_oSelAddress;
00417         }
00418 
00419         $oSelectedAddress = null;
00420         $oAddresses = $this->getUserAddresses();
00421         if ($oAddresses->count()) {
00422             if ($sAddressId = $this->getSelectedAddressId()) {
00423                 foreach ($oAddresses as $oAddress) {
00424                     if ($oAddress->getId() == $sAddressId) {
00425                         $oAddress->selected = 1;
00426                         $oAddress->setSelected();
00427                         $oSelectedAddress = $oAddress;
00428                         break;
00429                     }
00430                 }
00431             }
00432 
00433             // in case none is set - setting first one
00434             if (!$oSelectedAddress) {
00435                 if (!$sAddressId || $sAddressId >= 0) {
00436                     $oAddresses->rewind();
00437                     $oAddress = $oAddresses->current();
00438                 } else {
00439                     $aAddresses = $oAddresses->getArray();
00440                     $oAddress = array_pop($aAddresses);
00441                 }
00442                 $oAddress->selected = 1;
00443                 $oAddress->setSelected();
00444                 $oSelectedAddress = $oAddress;
00445             }
00446         }
00447         $this->_oSelAddress = $oSelectedAddress;
00448 
00449         return $oSelectedAddress;
00450     }
00451 
00459     public function getUserPayments($sOXID = null)
00460     {
00461         if ($this->_oPayments === null) {
00462 
00463             if (!$sOXID) {
00464                 $sOXID = $this->getId();
00465             }
00466 
00467             $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote($sOXID) . ' ';
00468 
00469             $this->_oPayments = oxNew('oxList');
00470             $this->_oPayments->init('oxUserPayment');
00471             $this->_oPayments->selectString($sSelect);
00472         }
00473 
00474         return $this->_oPayments;
00475     }
00476 
00482     public function save()
00483     {
00484         $myConfig = oxRegistry::getConfig();
00485 
00486         $blAddRemark = false;
00487         if ($this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
00488             $blAddRemark = true;
00489             //save oxregister value
00490             $this->oxuser__oxregister = new oxField(date('Y-m-d H:i:s'), oxField::T_RAW);
00491         }
00492 
00493         // setting user rights
00494         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00495 
00496         // processing birth date which came from output as array
00497         if (is_array($this->oxuser__oxbirthdate->value)) {
00498             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday($this->oxuser__oxbirthdate->value), oxField::T_RAW);
00499         }
00500 
00501         // checking if user Facebook ID should be updated
00502         if ($myConfig->getConfigParam("bl_showFbConnect")) {
00503             $oFb = oxRegistry::get("oxFb");
00504             if ($oFb->isConnected() && $oFb->getUser()) {
00505                 $this->oxuser__oxfbid = new oxField($oFb->getUser());
00506             }
00507         }
00508 
00509         $blRet = parent::save();
00510 
00511         //add registered remark
00512         if ($blAddRemark && $blRet) {
00513             $oRemark = oxNew('oxremark');
00514             $oRemark->oxremark__oxtext = new oxField(oxRegistry::getLang()->translateString('usrRegistered', null, true), oxField::T_RAW);
00515             $oRemark->oxremark__oxtype = new oxField('r', oxField::T_RAW);
00516             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00517             $oRemark->save();
00518         }
00519 
00520         return $blRet;
00521     }
00522 
00528     public function allowDerivedUpdate()
00529     {
00530         return true;
00531     }
00532 
00540     public function inGroup($sGroupID)
00541     {
00542         $blIn = false;
00543         if (($oGroups = $this->getUserGroups())) {
00544             $blIn = isset($oGroups[$sGroupID]);
00545         }
00546 
00547         return $blIn;
00548     }
00549 
00558     public function delete($sOXID = null)
00559     {
00560 
00561         if (!$sOXID) {
00562             $sOXID = $this->getId();
00563         }
00564         if (!$sOXID) {
00565             return false;
00566         }
00567 
00568         $blDeleted = parent::delete($sOXID);
00569 
00570         if ($blDeleted) {
00571             $oDb = oxDb::getDb();
00572             $sOXIDQuoted = $oDb->quote($sOXID);
00573 
00574             // deleting stored payment, address, group dependencies, remarks info
00575             $rs = $oDb->execute("delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}");
00576             $rs = $oDb->execute("delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}");
00577 
00578             // deleting notice/wish lists
00579             $rs = $oDb->execute("delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}");
00580             $rs = $oDb->execute("delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}");
00581 
00582             // deleting newsletter subscription
00583             $rs = $oDb->execute("delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}");
00584 
00585             // delivery and delivery sets
00586             $rs = $oDb->execute("delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00587 
00588             // discounts
00589             $rs = $oDb->execute("delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00590 
00591 
00592             // and leaving all order related information
00593             $rs = $oDb->execute("delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'");
00594 
00595             $blDeleted = $rs->EOF;
00596         }
00597 
00598         return $blDeleted;
00599     }
00600 
00608     public function load($oxID)
00609     {
00610 
00611         $blRet = parent::load($oxID);
00612 
00613         // convert date's to international format
00614         if (isset($this->oxuser__oxcreate->value)) {
00615             $this->oxuser__oxcreate->setValue(oxRegistry::get("oxUtilsDate")->formatDBDate($this->oxuser__oxcreate->value));
00616         }
00617 
00618         // change newsSubcription user id
00619         if (isset($this->_oNewsSubscription)) {
00620             $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($oxID, oxField::T_RAW);
00621         }
00622 
00623         return $blRet;
00624     }
00625 
00633     public function exists($sOXID = null)
00634     {
00635         if (!$sOXID) {
00636             $sOXID = $this->getId();
00637         }
00638         //#5901 if physical record exists return true unconditionally
00639         if (parent::exists($sOXID)) {
00640             $this->setId($sOXID);
00641             return true;
00642         }
00643 
00644         //additional username check
00645         //This part is used by not yet saved user object, to detect the case when such username exists in db.
00646         //Basically it is called when anonymous visitor enters existing username for newsletter subscription
00647         //see Newsletter::send()
00648         //TODO: transfer this validation to newsletter part
00649         $sShopSelect = '';
00650         if (!$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00651             $sShopSelect = ' AND oxshopid = "' . $this->getConfig()->getShopId() . '" ';
00652         }
00653         $oDb = oxDb::getDb();
00654         $sSelect = 'SELECT oxid FROM ' . $this->getViewName() . '
00655                     WHERE ( oxusername = ' . $oDb->quote($this->oxuser__oxusername->value) . ' ) ';
00656         $sSelect .= $sShopSelect;
00657 
00658         if (($sOxid = $oDb->getOne($sSelect, false, false))) {
00659             // update - set oxid
00660             $this->setId($sOxid);
00661 
00662             return true;
00663         }
00664 
00665         return false;
00666     }
00667 
00676     public function getOrders($iLimit = false, $iPage = 0)
00677     {
00678         $oOrders = oxNew('oxList');
00679         $oOrders->init('oxorder');
00680 
00681         if ($iLimit !== false) {
00682             $oOrders->setSqlLimit($iLimit * $iPage, $iLimit);
00683         }
00684 
00685         //P
00686         // Lists does not support loading from two tables, so orders
00687         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00688         // forcing to load product info which is used in templates
00689         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00690 
00691         //loading order for registered user
00692         if ($this->oxuser__oxregister->value > 1) {
00693             $oDb = oxDb::getDb();
00694             $sQ = 'select * from oxorder where oxuserid = ' . $oDb->quote($this->getId()) . ' and oxorderdate >= ' . $oDb->quote($this->oxuser__oxregister->value) . ' ';
00695 
00696             //#1546 - shopid check added, if it is not multishop
00697 
00698             $sQ .= ' order by oxorderdate desc ';
00699             $oOrders->selectString($sQ);
00700         }
00701 
00702         return $oOrders;
00703     }
00704 
00710     public function getOrderCount()
00711     {
00712         $iCnt = 0;
00713         if ($this->getId() && $this->oxuser__oxregister->value > 1) {
00714             $oDb = oxDb::getDb();
00715             $sQ = 'select count(*) from oxorder where oxuserid = ' . $oDb->quote($this->getId()) . ' AND oxorderdate >= ' . $oDb->quote($this->oxuser__oxregister->value) . ' and oxshopid = "' . $this->getConfig()->getShopId() . '" ';
00716             $iCnt = (int) $oDb->getOne($sQ);
00717         }
00718 
00719         return $iCnt;
00720     }
00721 
00727     public function getNoticeListArtCnt()
00728     {
00729         if ($this->_iCntNoticeListArticles === null) {
00730             $this->_iCntNoticeListArticles = 0;
00731             if ($this->getId()) {
00732                 $this->_iCntNoticeListArticles = $this->getBasket('noticelist')->getItemCount();
00733             }
00734         }
00735 
00736         return $this->_iCntNoticeListArticles;
00737     }
00738 
00744     public function getWishListArtCnt()
00745     {
00746         if ($this->_iCntWishListArticles === null) {
00747             $this->_iCntWishListArticles = false;
00748             if ($this->getId()) {
00749                 $this->_iCntWishListArticles = $this->getBasket('wishlist')->getItemCount();
00750             }
00751         }
00752 
00753         return $this->_iCntWishListArticles;
00754     }
00755 
00761     public function getEncodedDeliveryAddress()
00762     {
00763         return md5($this->_getMergedAddressFields());
00764     }
00765 
00772     public function getActiveCountry()
00773     {
00774         $sDeliveryCountry = '';
00775         $soxAddressId = oxRegistry::getSession()->getVariable('deladrid');
00776         if ($soxAddressId) {
00777             $oDelAddress = oxNew('oxaddress');
00778             $oDelAddress->load($soxAddressId);
00779             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00780         } elseif ($this->getId()) {
00781             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00782         } else {
00783             $oUser = oxNew('oxuser');
00784             if ($oUser->loadActiveUser()) {
00785                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00786             }
00787         }
00788 
00789         return $sDeliveryCountry;
00790     }
00791 
00799     public function createUser()
00800     {
00801         $oDb = oxDb::getDb();
00802         $sShopID = $this->getConfig()->getShopId();
00803 
00804         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00805         $sSelect = "select oxid from oxuser where oxusername = " . $oDb->quote($this->oxuser__oxusername->value) . " and oxpassword = '' ";
00806         if (!$this->_blMallUsers) {
00807             $sSelect .= " and oxshopid = '{$sShopID}' ";
00808         }
00809         $sOXID = $oDb->getOne($sSelect, false, false);
00810 
00811         // user without password found - lets use
00812         if (isset($sOXID) && $sOXID) {
00813             // try to update
00814             $this->delete($sOXID);
00815         } elseif ($this->_blMallUsers) {
00816             // must be sure if there is no dublicate user
00817             $sQ = "select oxid from oxuser where oxusername = " . $oDb->quote($this->oxuser__oxusername->value) . " and oxusername != '' ";
00818             if ($oDb->getOne($sQ, false, false)) {
00820                 $oEx = oxNew('oxUserException');
00821                 $oLang = oxRegistry::getLang();
00822                 $oEx->setMessage(sprintf($oLang->translateString('ERROR_MESSAGE_USER_USEREXISTS', $oLang->getTplLanguage()), $this->oxuser__oxusername->value));
00823                 throw $oEx;
00824             }
00825         }
00826 
00827         $this->oxuser__oxshopid = new oxField($sShopID, oxField::T_RAW);
00828         if (($blOK = $this->save())) {
00829             // dropping/cleaning old delivery address/payment info
00830             $oDb->execute("delete from oxaddress where oxaddress.oxuserid = " . $oDb->quote($this->oxuser__oxid->value) . " ");
00831             $oDb->execute("update oxuserpayments set oxuserpayments.oxuserid = " . $oDb->quote($this->oxuser__oxusername->value) . " where oxuserpayments.oxuserid = " . $oDb->quote($this->oxuser__oxid->value) . " ");
00832         } else {
00834             $oEx = oxNew('oxUserException');
00835             $oEx->setMessage('EXCEPTION_USER_USERCREATIONFAILED');
00836             throw $oEx;
00837         }
00838 
00839         return $blOK;
00840     }
00841 
00849     public function addToGroup($sGroupID)
00850     {
00851         if (!$this->inGroup($sGroupID)) {
00852             // create oxgroup object
00853             $oGroup = oxNew('oxGroups');
00854             if ($oGroup->load($sGroupID)) {
00855                 $oNewGroup = oxNew('oxobject2group');
00856                 $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00857                 $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00858                 if ($oNewGroup->save()) {
00859                     $this->_oGroups[$sGroupID] = $oGroup;
00860 
00861                     return true;
00862                 }
00863             }
00864         }
00865 
00866         return false;
00867     }
00868 
00874     public function removeFromGroup($sGroupID = null)
00875     {
00876         if ($sGroupID != null && $this->inGroup($sGroupID)) {
00877             $oGroups = oxNew('oxList');
00878             $oGroups->init('oxobject2group');
00879             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "' . $this->getId() . '" and oxobject2group.oxgroupsid = "' . $sGroupID . '" ';
00880             $oGroups->selectString($sSelect);
00881             foreach ($oGroups as $oRemgroup) {
00882                 if ($oRemgroup->delete()) {
00883                     unset($this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value]);
00884                 }
00885             }
00886         }
00887     }
00888 
00895     public function onOrderExecute($oBasket, $iSuccess)
00896     {
00897 
00898         if (is_numeric($iSuccess) && $iSuccess != 2 && $iSuccess <= 3) {
00899             //adding user to particular customer groups
00900             $myConfig = $this->getConfig();
00901             $dMidlleCustPrice = (float) $myConfig->getConfigParam('sMidlleCustPrice');
00902             $dLargeCustPrice = (float) $myConfig->getConfigParam('sLargeCustPrice');
00903 
00904             $this->addToGroup('oxidcustomer');
00905             $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00906             if ($dBasketPrice < $dMidlleCustPrice) {
00907                 $this->addToGroup('oxidsmallcust');
00908             }
00909             if ($dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice) {
00910                 $this->addToGroup('oxidmiddlecust');
00911             }
00912             if ($dBasketPrice >= $dLargeCustPrice) {
00913                 $this->addToGroup('oxidgoodcust');
00914             }
00915 
00916             if ($this->inGroup('oxidnotyetordered')) {
00917                 $this->removeFromGroup('oxidnotyetordered');
00918             }
00919         }
00920     }
00921 
00929     public function getBasket($sName)
00930     {
00931         if (!isset($this->_aBaskets[$sName])) {
00932             $oBasket = oxNew('oxuserbasket');
00933             $aWhere = array('oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName);
00934 
00935             // creating if it does not exist
00936             if (!$oBasket->assignRecord($oBasket->buildSelectString($aWhere))) {
00937                 $oBasket->oxuserbaskets__oxtitle = new oxField($sName);
00938                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00939 
00940                 // marking basket as new (it will not be saved in DB yet)
00941                 $oBasket->setIsNewBasket();
00942             }
00943 
00944             $this->_aBaskets[$sName] = $oBasket;
00945         }
00946 
00947         return $this->_aBaskets[$sName];
00948     }
00949 
00958     public function convertBirthday($aData)
00959     {
00960 
00961         // preparing data to process
00962         $iYear = isset($aData['year']) ? ((int) $aData['year']) : false;
00963         $iMonth = isset($aData['month']) ? ((int) $aData['month']) : false;
00964         $iDay = isset($aData['day']) ? ((int) $aData['day']) : false;
00965 
00966         // leaving empty if not set
00967         if (!$iYear && !$iMonth && !$iDay) {
00968             return "";
00969         }
00970 
00971         // year
00972         if (!$iYear || $iYear < 1000 || $iYear > 9999) {
00973             $iYear = date('Y');
00974         }
00975 
00976         // month
00977         if (!$iMonth || $iMonth < 1 || $iMonth > 12) {
00978             $iMonth = 1;
00979         }
00980 
00981         // maximum nuber of days in month
00982         $iMaxDays = 31;
00983         switch ($iMonth) {
00984             case 2:
00985                 if ($iMaxDays > 28) {
00986                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00987                 }
00988                 break;
00989             case 4:
00990             case 6:
00991             case 9:
00992             case 11:
00993                 $iMaxDays = min(30, $iMaxDays);
00994                 break;
00995         }
00996 
00997         // day
00998         if (!$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00999             $iDay = 1;
01000         }
01001 
01002         // whole date
01003         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
01004     }
01005 
01011     public function getBoni()
01012     {
01013         if (!$iBoni = $this->getConfig()->getConfigParam('iCreditRating')) {
01014             $iBoni = 1000;
01015         }
01016 
01017         return $iBoni;
01018     }
01019 
01036     public function checkValues($sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress)
01037     {
01039         $oInputValidator = oxRegistry::get('oxInputValidator');
01040 
01041         // 1. checking user name
01042         $sLogin = $oInputValidator->checkLogin($this, $sLogin, $aInvAddress);
01043 
01044         // 2. checking email
01045         $oInputValidator->checkEmail($this, $sLogin, $aInvAddress);
01046 
01047         // 3. password
01048         $oInputValidator->checkPassword($this, $sPassword, $sPassword2, ((int) oxRegistry::getConfig()->getRequestParameter('option') == 3));
01049 
01050         // 4. required fields
01051         $oInputValidator->checkRequiredFields($this, $aInvAddress, $aDelAddress);
01052 
01053         // 5. country check
01054         $oInputValidator->checkCountries($this, $aInvAddress, $aDelAddress);
01055 
01056         // 6. vat id check.
01057         $oInputValidator->checkVatId($this, $aInvAddress);
01058 
01059 
01060         // throwing first validation error
01061         if ($oError = oxRegistry::get("oxInputValidator")->getFirstValidationError()) {
01062             throw $oError;
01063         }
01064     }
01065 
01075     public function setNewsSubscription($blSubscribe, $blSendOptIn, $blForceCheckOptIn = false)
01076     {
01077         // assigning to newsletter
01078         $blSuccess = false;
01079         $myConfig = $this->getConfig();
01080         $mySession = $this->getSession();
01081 
01082         // user wants to get newsletter messages or no ?
01083         $oNewsSubscription = $this->getNewsSubscription();
01084         if ($oNewsSubscription) {
01085             if ($blSubscribe && ($blForceCheckOptIn || ($iOptInStatus = $oNewsSubscription->getOptInStatus()) != 1)) {
01086                 if (!$blSendOptIn) {
01087 
01088                     // double-opt-in check is disabled - assigning automatically
01089                     $this->addToGroup('oxidnewsletter');
01090                     // and setting subscribed status
01091                     $oNewsSubscription->setOptInStatus(1);
01092                     $blSuccess = true;
01093                 } else {
01094 
01095                     // double-opt-in check enabled - sending confirmation email and setting waiting status
01096                     if ($iOptInStatus != 2) {
01097                         // sending double-opt-in mail
01098                         $oEmail = oxNew('oxemail');
01099                         $blSuccess = $oEmail->sendNewsletterDbOptInMail($this);
01100                     } else {
01101                         // mail already was sent, so just confirming that
01102                         $blSuccess = true;
01103                     }
01104 
01105                     $oNewsSubscription->setOptInStatus(2);
01106                 }
01107             } elseif (!$blSubscribe) {
01108                 // removing user from newsletter subscribers
01109                 $this->removeFromGroup('oxidnewsletter');
01110                 $oNewsSubscription->setOptInStatus(0);
01111                 $blSuccess = true;
01112             }
01113         }
01114 
01115         return $blSuccess;
01116     }
01117 
01133     public function changeUserData($sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress)
01134     {
01135         // validating values before saving. If validation fails - exception is thrown
01136         $this->checkValues($sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress);
01137         // input data is fine - lets save updated user info
01138 
01139         $this->assign($aInvAddress);
01140 
01141 
01142         // update old or add new delivery address
01143         $this->_assignAddress($aDelAddress);
01144 
01145         // saving new values
01146         if ($this->save()) {
01147 
01148             // assigning automatically to specific groups
01149             $sCountryId = isset($aInvAddress['oxuser__oxcountryid']) ? $aInvAddress['oxuser__oxcountryid'] : '';
01150             $this->_setAutoGroups($sCountryId);
01151         }
01152     }
01153 
01159     protected function _getMergedAddressFields()
01160     {
01161         $sDelAddress = '';
01162         $sDelAddress .= $this->oxuser__oxcompany;
01163         $sDelAddress .= $this->oxuser__oxusername;
01164         $sDelAddress .= $this->oxuser__oxfname;
01165         $sDelAddress .= $this->oxuser__oxlname;
01166         $sDelAddress .= $this->oxuser__oxstreet;
01167         $sDelAddress .= $this->oxuser__oxstreetnr;
01168         $sDelAddress .= $this->oxuser__oxaddinfo;
01169         $sDelAddress .= $this->oxuser__oxustid;
01170         $sDelAddress .= $this->oxuser__oxcity;
01171         $sDelAddress .= $this->oxuser__oxcountryid;
01172         $sDelAddress .= $this->oxuser__oxstateid;
01173         $sDelAddress .= $this->oxuser__oxzip;
01174         $sDelAddress .= $this->oxuser__oxfon;
01175         $sDelAddress .= $this->oxuser__oxfax;
01176         $sDelAddress .= $this->oxuser__oxsal;
01177 
01178         return $sDelAddress;
01179     }
01180 
01186     protected function _assignAddress($aDelAddress)
01187     {
01188         if (is_array($aDelAddress) && count($aDelAddress)) {
01189 
01190             $sAddressId = $this->getConfig()->getRequestParameter('oxaddressid');
01191             $sAddressId = ($sAddressId === null || $sAddressId == -1 || $sAddressId == -2) ? null : $sAddressId;
01192 
01193             $oAddress = oxNew('oxaddress');
01194             $oAddress->setId($sAddressId);
01195             $oAddress->load($sAddressId);
01196             $oAddress->assign($aDelAddress);
01197             $oAddress->oxaddress__oxuserid = new oxField($this->getId(), oxField::T_RAW);
01198             $oAddress->oxaddress__oxcountry = $this->getUserCountry($oAddress->oxaddress__oxcountryid->value);
01199             $oAddress->save();
01200 
01201             // resetting addresses
01202             $this->_aAddresses = null;
01203 
01204             // saving delivery Address for later use
01205             oxRegistry::getSession()->setVariable('deladrid', $oAddress->getId());
01206         } else {
01207             // resetting
01208             oxRegistry::getSession()->setVariable('deladrid', null);
01209         }
01210     }
01211 
01222     protected function _getLoginQueryHashedWithMD5($sUser, $sPassword, $sShopID, $blAdmin)
01223     {
01224         $myConfig = $this->getConfig();
01225         $oDb = oxDb::getDb();
01226 
01227         $sUserSelect = "oxuser.oxusername = " . $oDb->quote($sUser);
01228         $sPassSelect = " oxuser.oxpassword = BINARY MD5( CONCAT( " . $oDb->quote($sPassword) . ", UNHEX( oxuser.oxpasssalt ) ) ) ";
01229         $sShopSelect = "";
01230 
01231 
01232         // admin view: can only login with higher than 'user' rights
01233         if ($blAdmin) {
01234             $sShopSelect = " and ( oxrights != 'user' ) ";
01235         }
01236 
01237         $sSelect = "select `oxid` from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01238 
01239 
01240         return $sSelect;
01241     }
01242 
01255     protected function _getLoginQuery($sUser, $sPassword, $sShopID, $blAdmin)
01256     {
01257         $myConfig = $this->getConfig();
01258         $oDb = oxDb::getDb();
01259 
01260         $sUserSelect = "oxuser.oxusername = " . $oDb->quote($sUser);
01261 
01262         $sSalt = $oDb->getOne("SELECT `oxpasssalt` FROM `oxuser` WHERE  " . $sUserSelect);
01263 
01264         $sPassSelect = " oxuser.oxpassword = " . $oDb->quote($this->encodePassword($sPassword, $sSalt));
01265         $sShopSelect = "";
01266 
01267 
01268         // admin view: can only login with higher than 'user' rights
01269         if ($blAdmin) {
01270             $sShopSelect = " and ( oxrights != 'user' ) ";
01271         }
01272 
01273         $sSelect = "select `oxid` from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01274 
01275 
01276         return $sSelect;
01277     }
01278 
01288     protected function _getShopSelect($myConfig, $sShopID, $blAdmin)
01289     {
01290         $sShopSelect = "";
01291         // admin view: can only login with higher than 'user' rights
01292         if ($blAdmin) {
01293             $sShopSelect = " and ( oxrights != 'user' ) ";
01294         }
01295 
01296         return $sShopSelect;
01297     }
01298 
01313     public function login($sUser, $sPassword, $blCookie = false)
01314     {
01315         if ($this->isAdmin() && !count(oxRegistry::get("oxUtilsServer")->getOxCookie())) {
01317             $oEx = oxNew('oxCookieException');
01318             $oEx->setMessage('ERROR_MESSAGE_COOKIE_NOCOOKIE');
01319             throw $oEx;
01320         }
01321 
01322         $oConfig = $this->getConfig();
01323 
01324 
01325         if ($sPassword) {
01326 
01327             $sShopID = $oConfig->getShopId();
01328             $this->_dbLogin($sUser, $sPassword, $sShopID);
01329         }
01330 
01331 
01332 
01333 
01334         //login successful?
01335         if ($this->oxuser__oxid->value) {
01336 
01337             // yes, successful login
01338 
01339             //resetting active user
01340             $this->setUser(null);
01341 
01342             if ($this->isAdmin()) {
01343                 oxRegistry::getSession()->setVariable('auth', $this->oxuser__oxid->value);
01344             } else {
01345                 oxRegistry::getSession()->setVariable('usr', $this->oxuser__oxid->value);
01346             }
01347 
01348             // cookie must be set ?
01349             if ($blCookie && $oConfig->getConfigParam('blShowRememberMe')) {
01350                 oxRegistry::get("oxUtilsServer")->setUserCookie($this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $oConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value);
01351             }
01352 
01353             return true;
01354         } else {
01356             $oEx = oxNew('oxUserException');
01357             $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
01358             throw $oEx;
01359         }
01360     }
01361 
01367     public function logout()
01368     {
01369         // deleting session info
01370         oxRegistry::getSession()->deleteVariable('usr'); // for front end
01371         oxRegistry::getSession()->deleteVariable('auth'); // for back end
01372         oxRegistry::getSession()->deleteVariable('dynvalue');
01373         oxRegistry::getSession()->deleteVariable('paymentid');
01374         // oxRegistry::getSession()->deleteVariable( 'deladrid' );
01375 
01376         // delete cookie
01377         oxRegistry::get("oxUtilsServer")->deleteUserCookie($this->getConfig()->getShopID());
01378 
01379         // unsetting global user
01380         $this->setUser(null);
01381 
01382         return true;
01383     }
01384 
01391     public function loadAdminUser()
01392     {
01393         return $this->loadActiveUser(true);
01394     }
01395 
01404     public function loadActiveUser($blForceAdmin = false)
01405     {
01406         $oConfig = $this->getConfig();
01407 
01408         $blAdmin = $this->isAdmin() || $blForceAdmin;
01409 
01410         // first - checking session info
01411         $sUserID = $blAdmin ? oxRegistry::getSession()->getVariable('auth') : oxRegistry::getSession()->getVariable('usr');
01412 
01413         // trying automatic login (by 'remember me' cookie)
01414         $blFoundInCookie = false;
01415         if (!$sUserID && !$blAdmin && $oConfig->getConfigParam('blShowRememberMe')) {
01416             $sUserID = $this->_getCookieUserId();
01417             $blFoundInCookie = $sUserID ? true : false;
01418         }
01419 
01420         // If facebook connection is enabled, trying to login user using Facebook ID
01421         if (!$sUserID && !$blAdmin && $oConfig->getConfigParam("bl_showFbConnect")) {
01422             $sUserID = $this->_getFacebookUserId();
01423         }
01424 
01425         // checking user results
01426         if ($sUserID) {
01427             if ($this->load($sUserID)) {
01428                 // storing into session
01429                 if ($blAdmin) {
01430                     oxRegistry::getSession()->setVariable('auth', $sUserID);
01431                 } else {
01432                     oxRegistry::getSession()->setVariable('usr', $sUserID);
01433                 }
01434 
01435                 // marking the way user was loaded
01436                 $this->_blLoadedFromCookie = $blFoundInCookie;
01437 
01438                 return true;
01439             }
01440         } else {
01441             // no user
01442             if ($blAdmin) {
01443                 oxRegistry::getSession()->deleteVariable('auth');
01444             } else {
01445                 oxRegistry::getSession()->deleteVariable('usr');
01446             }
01447 
01448             return false;
01449         }
01450     }
01451 
01457     protected function _getFacebookUserId()
01458     {
01459         $oDb = oxDb::getDb();
01460         $oFb = oxRegistry::get("oxFb");
01461         $oConfig = $this->getConfig();
01462         if ($oFb->isConnected() && $oFb->getUser()) {
01463             $sUserSelect = "oxuser.oxfbid = " . $oDb->quote($oFb->getUser());
01464             $sShopSelect = "";
01465 
01466 
01467             $sSelect = "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01468             $sUserID = $oDb->getOne($sSelect);
01469         }
01470 
01471         return $sUserID;
01472     }
01473 
01479     protected function _getCookieUserId()
01480     {
01481         $sUserID = null;
01482         $oConfig = $this->getConfig();
01483         $sShopID = $oConfig->getShopId();
01484         if (($sSet = oxRegistry::get("oxUtilsServer")->getUserCookie($sShopID))) {
01485             $oDb = oxDb::getDb();
01486             $aData = explode('@@@', $sSet);
01487             $sUser = $aData[0];
01488             $sPWD = @$aData[1];
01489 
01490             $sSelect = 'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = ' . $oDb->quote($sUser);
01491 
01492             $rs = $oDb->select($sSelect);
01493             if ($rs != false && $rs->recordCount() > 0) {
01494                 while (!$rs->EOF) {
01495                     $sTest = crypt($rs->fields[1], $rs->fields[2]);
01496                     if ($sTest == $sPWD) {
01497                         // found
01498                         $sUserID = $rs->fields[0];
01499                         break;
01500                     }
01501                     $rs->moveNext();
01502                 }
01503             }
01504             // if cookie info is not valid, remove it.
01505             if (!$sUserID) {
01506                 oxRegistry::get('oxUtilsServer')->deleteUserCookie($sShopID);
01507             }
01508         }
01509 
01510         return $sUserID;
01511     }
01512 
01523     protected function _ldapLogin($sUser, $sPassword, $sShopID, $sShopSelect)
01524     {
01525         $aLDAPParams = $this->getConfig()->getConfigParam('aLDAPParams');
01526         $oLDAP = oxNew("oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT']);
01527 
01528         // maybe this is LDAP user but supplied email Address instead of LDAP login
01529         $oDb = oxDb::getDb();
01530         $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = " . $oDb->quote($sUser) . " $sShopSelect");
01531         if (isset($sLDAPKey) && $sLDAPKey) {
01532             $sUser = $sLDAPKey;
01533         }
01534 
01535         //$throws oxConnectionException
01536         $oLDAP->login($sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01537 
01538         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01539         if (isset($aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01540             // login successful
01541 
01542             // check if user is already in database
01543             $sSelect = "select oxid from oxuser where oxuser.oxusername = " . $oDb->quote($aData['OXUSERNAME']) . " $sShopSelect";
01544             $sOXID = $oDb->getOne($sSelect);
01545 
01546             if (!isset($sOXID) || !$sOXID) {
01547                 // we need to create a new user
01548                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01549                 $this->setId();
01550 
01551                 // map all user data fields
01552                 foreach ($aData as $fldname => $value) {
01553                     $sField = "oxuser__" . strtolower($fldname);
01554                     $this->$sField = new oxField($aData[$fldname]);
01555                 }
01556 
01557                 $this->oxuser__oxactive = new oxField(1);
01558                 $this->oxuser__oxshopid = new oxField($sShopID);
01559                 $this->oxuser__oxldapkey = new oxField($sUser);
01560                 $this->oxuser__oxrights = new oxField("user");
01561                 $this->setPassword("ldap user");
01562 
01563                 $this->save();
01564             } else {
01565                 // LDAP user is already in OXID DB, load it
01566                 $this->load($sOXID);
01567             }
01568         } else {
01570             $oEx = oxNew('oxUserException');
01571             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01572             throw $oEx;
01573         }
01574     }
01575 
01582     protected function _getUserRights()
01583     {
01584         // previously user had no rights defined
01585         if (!$this->oxuser__oxrights->value) {
01586             return 'user';
01587         }
01588 
01589         $oDb = oxDb::getDb();
01590         $myConfig = $this->getConfig();
01591         $sAuthRights = null;
01592 
01593         // choosing possible user rights index
01594         $sAuthUserID = $this->isAdmin() ? oxRegistry::getSession()->getVariable('auth') : null;
01595         $sAuthUserID = $sAuthUserID ? $sAuthUserID : oxRegistry::getSession()->getVariable('usr');
01596         if ($sAuthUserID) {
01597             $sAuthRights = $oDb->getOne('select oxrights from ' . $this->getViewName() . ' where oxid=' . $oDb->quote($sAuthUserID));
01598         }
01599 
01600         //preventing user rights edit for non admin
01601         $aRights = array();
01602 
01603         // selecting current users rights ...
01604         if ($sCurrRights = $oDb->getOne('select oxrights from ' . $this->getViewName() . ' where oxid=' . $oDb->quote($this->getId()))) {
01605             $aRights[] = $sCurrRights;
01606         }
01607         $aRights[] = 'user';
01608 
01609         if (!$sAuthRights || !($sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId())) {
01610             return current($aRights);
01611         } elseif ($sAuthRights == $myConfig->getShopId()) {
01612             $aRights[] = $sAuthRights;
01613             if (!in_array($this->oxuser__oxrights->value, $aRights)) {
01614                 return current($aRights);
01615             }
01616         }
01617 
01618         // leaving as it was set ...
01619         return $this->oxuser__oxrights->value;
01620     }
01621 
01627     protected function _insert()
01628     {
01629 
01630         // set oxcreate date
01631         $this->oxuser__oxcreate = new oxField(date('Y-m-d H:i:s'), oxField::T_RAW);
01632 
01633         if (!isset($this->oxuser__oxboni->value)) {
01634             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01635         }
01636 
01637         return parent::_insert();
01638     }
01639 
01645     protected function _update()
01646     {
01647         //V #M418: for not registered users, don't change boni during update
01648         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01649             $this->_aSkipSaveFields[] = 'oxboni';
01650         }
01651 
01652         // don't change this field
01653         $this->_aSkipSaveFields[] = 'oxcreate';
01654         if (!$this->isAdmin()) {
01655             $this->_aSkipSaveFields[] = 'oxcustnr';
01656             $this->_aSkipSaveFields[] = 'oxrights';
01657         }
01658 
01659         // updating subscription information
01660         if (($blUpdate = parent::_update())) {
01661             $this->getNewsSubscription()->updateSubscription($this);
01662         }
01663 
01664         return $blUpdate;
01665     }
01666 
01674     public function checkIfEmailExists($sEmail)
01675     {
01676         $myConfig = $this->getConfig();
01677         $oDb = oxDb::getDb();
01678         $iShopId = $myConfig->getShopId();
01679         $blExists = false;
01680 
01681         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = ' . $oDb->quote($sEmail);
01682         if (($sOxid = $this->getId())) {
01683             $sQ .= " and oxid <> " . $oDb->quote($sOxid);
01684         }
01685         $oRs = $oDb->select($sQ, false, false);
01686         if ($oRs != false && $oRs->recordCount() > 0) {
01687 
01688             if ($this->_blMallUsers) {
01689 
01690                 $blExists = true;
01691                 if ($oRs->fields[1] == 'user' && !$oRs->fields[2]) {
01692 
01693                     // password is not set - allow to override
01694                     $blExists = false;
01695                 }
01696             } else {
01697 
01698                 $blExists = false;
01699                 while (!$oRs->EOF) {
01700                     if ($oRs->fields[1] != 'user') {
01701 
01702                         // exists admin with same login - must not allow
01703                         $blExists = true;
01704                         break;
01705                     } elseif ($oRs->fields[0] == $iShopId && $oRs->fields[2]) {
01706 
01707                         // exists same login (with password) in same shop
01708                         $blExists = true;
01709                         break;
01710                     }
01711 
01712                     $oRs->moveNext();
01713                 }
01714             }
01715         }
01716 
01717         return $blExists;
01718     }
01719 
01727     public function getUserRecommLists($sOXID = null)
01728     {
01729         if (!$sOXID) {
01730             $sOXID = $this->getId();
01731         }
01732 
01733         // sets active page
01734         $iActPage = (int) oxRegistry::getConfig()->getRequestParameter('pgNr');
01735         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01736 
01737         // load only lists which we show on screen
01738         $iNrofCatArticles = $this->getConfig()->getConfigParam('iNrofCatArticles');
01739         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01740 
01741 
01742         $oRecommList = oxNew('oxList');
01743         $oRecommList->init('oxrecommlist');
01744         $oRecommList->setSqlLimit($iNrofCatArticles * $iActPage, $iNrofCatArticles);
01745         $iShopId = $this->getConfig()->getShopId();
01746         $sSelect = 'select * from oxrecommlists where oxuserid =' . oxDb::getDb()->quote($sOXID) . ' and oxshopid ="' . $iShopId . '"';
01747         $oRecommList->selectString($sSelect);
01748 
01749         return $oRecommList;
01750     }
01751 
01759     public function getRecommListsCount($sOx = null)
01760     {
01761         if (!$sOx) {
01762             $sOx = $this->getId();
01763         }
01764 
01765         if ($this->_iCntRecommLists === null || $sOx) {
01766             $oDb = oxDb::getDb();
01767             $this->_iCntRecommLists = 0;
01768             $iShopId = $this->getConfig()->getShopId();
01769             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote($sOx) . ' and oxshopid ="' . $iShopId . '"';
01770             $this->_iCntRecommLists = $oDb->getOne($sSelect);
01771         }
01772 
01773         return $this->_iCntRecommLists;
01774     }
01775 
01782     protected function _setAutoGroups($sCountryId)
01783     {
01784         // assigning automatically to specific groups
01785         $blForeigner = true;
01786         $blForeignGroupExists = false;
01787         $blInlandGroupExists = false;
01788 
01789         $aHomeCountry = $this->getConfig()->getConfigParam('aHomeCountry');
01790         // foreigner ?
01791         if (is_array($aHomeCountry)) {
01792             if (in_array($sCountryId, $aHomeCountry)) {
01793                 $blForeigner = false;
01794             }
01795         } elseif ($sCountryId == $aHomeCountry) {
01796             $blForeigner = false;
01797         }
01798 
01799         if ($this->inGroup('oxidforeigncustomer')) {
01800             $blForeignGroupExists = true;
01801             if (!$blForeigner) {
01802                 $this->removeFromGroup('oxidforeigncustomer');
01803             }
01804         }
01805 
01806         if ($this->inGroup('oxidnewcustomer')) {
01807             $blInlandGroupExists = true;
01808             if ($blForeigner) {
01809                 $this->removeFromGroup('oxidnewcustomer');
01810             }
01811         }
01812 
01813         if (!$blForeignGroupExists && $blForeigner) {
01814             $this->addToGroup('oxidforeigncustomer');
01815         }
01816         if (!$blInlandGroupExists && !$blForeigner) {
01817             $this->addToGroup('oxidnewcustomer');
01818         }
01819     }
01820 
01821 
01830     public function loadUserByUpdateId($sUid)
01831     {
01832         $oDb = oxDb::getDb();
01833         $sQ = "select oxid from " . $this->getViewName() . " where oxupdateexp >= " . time() . " and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = " . $oDb->quote($sUid);
01834         if ($sUserId = $oDb->getOne($sQ)) {
01835             return $this->load($sUserId);
01836         }
01837     }
01838 
01844     public function setUpdateKey($blReset = false)
01845     {
01846         $sUpKey = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
01847         $iUpTime = $blReset ? 0 : oxRegistry::get("oxUtilsDate")->getTime() + $this->getUpdateLinkTerm();
01848 
01849         // generating key
01850         $this->oxuser__oxupdatekey = new oxField($sUpKey, oxField::T_RAW);
01851 
01852         // setting expiration time for 6 hours
01853         $this->oxuser__oxupdateexp = new oxField($iUpTime, oxField::T_RAW);
01854 
01855         // saving
01856         $this->save();
01857     }
01858 
01864     public function getUpdateLinkTerm()
01865     {
01866         return 3600 * 6;
01867     }
01868 
01876     public function isExpiredUpdateId($sKey)
01877     {
01878         $oDb = oxDb::getDb();
01879         $sQ = "select 1 from " . $this->getViewName() . " where oxupdateexp >= " . time() . " and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = " . $oDb->quote($sKey);
01880 
01881         return !((bool) $oDb->getOne($sQ));
01882     }
01883 
01889     public function getUpdateId()
01890     {
01891         if ($this->_sUpdateKey === null) {
01892             $this->setUpdateKey();
01893             $this->_sUpdateKey = md5($this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value);
01894         }
01895 
01896         return $this->_sUpdateKey;
01897     }
01898 
01907     public function encodePassword($sPassword, $sSalt)
01908     {
01910         $oSha512Hasher = oxNew('oxSha512Hasher');
01912         $oHasher = oxNew('oxPasswordHasher', $oSha512Hasher);
01913 
01914         return $oHasher->hash($sPassword, $sSalt);
01915     }
01916 
01926     public function prepareSalt($sSalt)
01927     {
01929         $oOpenSSLFunctionalityChecker = oxNew('oxOpenSSLFunctionalityChecker');
01931         $oGenerator = oxNew('oxPasswordSaltGenerator', $oOpenSSLFunctionalityChecker);
01932 
01933         return $oGenerator->generate();
01934     }
01935 
01945     public function decodeSalt($sSaltHex)
01946     {
01947         return ($sSaltHex ? oxDb::getDb()->getOne("select UNHEX( '{$sSaltHex}' )") : '');
01948     }
01949 
01955     public function setPassword($sPassword = null)
01956     {
01958         $oOpenSSLFunctionalityChecker = oxNew('oxOpenSSLFunctionalityChecker');
01959         // setting salt if password is not empty
01961         $oSaltGenerator = oxNew('oxPasswordSaltGenerator', $oOpenSSLFunctionalityChecker);
01962 
01963         $sSalt = $sPassword ? $oSaltGenerator->generate() : '';
01964 
01965         // encoding only if password was not empty (e.g. user registration without pass)
01966         $sPassword = $sPassword ? $this->encodePassword($sPassword, $sSalt) : '';
01967 
01968         $this->oxuser__oxpassword = new oxField($sPassword, oxField::T_RAW);
01969         $this->oxuser__oxpasssalt = new oxField($sSalt, oxField::T_RAW);
01970     }
01971 
01979     public function isSamePassword($sNewPass)
01980     {
01981         return $this->encodePassword($sNewPass, $this->oxuser__oxpasssalt->value) == $this->oxuser__oxpassword->value;
01982     }
01983 
01989     public function isLoadedFromCookie()
01990     {
01991         return $this->_blLoadedFromCookie;
01992     }
01993 
02002     public function getPasswordHash()
02003     {
02004         $sHash = null;
02005         if ($this->oxuser__oxpassword->value) {
02006             $sHash = $this->oxuser__oxpassword->value;
02007         }
02008 
02009         return $sHash;
02010     }
02011 
02019     public function getReviewUserHash($sUserId)
02020     {
02021         $oDb = oxDb::getDb();
02022         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote($sUserId) . '');
02023 
02024         return $sReviewUserHash;
02025     }
02026 
02034     public function getReviewUserId($sReviewUserHash)
02035     {
02036         $oDb = oxDb::getDb();
02037         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote($sReviewUserHash) . '');
02038 
02039         return $sUserId;
02040     }
02041 
02049     public function getState()
02050     {
02051         return $this->getStateId();
02052     }
02053 
02059     public function getStateId()
02060     {
02061         return $this->oxuser__oxstateid->value;
02062     }
02063 
02071     public function getStateTitle($sId = null)
02072     {
02073         $oState = $this->_getStateObject();
02074 
02075         if (is_null($sId)) {
02076             $sId = $this->getStateId();
02077         }
02078 
02079         return $oState->getTitleById($sId);
02080     }
02081 
02087     public function isTermsAccepted()
02088     {
02089         $oDb = oxDb::getDb();
02090         $sShopId = $this->getConfig()->getShopId();
02091         $sUserId = $oDb->quote($this->getId());
02092 
02093         return (bool) $oDb->getOne("select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'");
02094     }
02095 
02099     public function acceptTerms()
02100     {
02101         $oDb = oxDb::getDb();
02102         $sUserId = $oDb->quote($this->getId());
02103         $sShopId = $this->getConfig()->getShopId();
02104         $sVersion = oxNew("oxcontent")->getTermsVersion();
02105 
02106         $oDb->execute("replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'");
02107     }
02108 
02118     public function setCreditPointsForRegistrant($sUserId, $sRecEmail)
02119     {
02120         $blSet = false;
02121         $oDb = oxDb::getDb();
02122         $iPoints = $this->getConfig()->getConfigParam('dPointsForRegistration');
02123         // check if this invitation is still not accepted
02124         $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);
02125         if ($iPoints && $iPending) {
02126             $this->oxuser__oxpoints = new oxField($iPoints, oxField::T_RAW);
02127             if ($blSet = $this->save()) {
02128                 // updating users statistics
02129                 $oDb->execute("UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = " . $oDb->quote($sUserId) . " and md5(oxemail) = " . $oDb->quote($sRecEmail));
02130                 $oInvUser = oxNew("oxuser");
02131                 if ($oInvUser->load($sUserId)) {
02132                     $blSet = $oInvUser->setCreditPointsForInviter();
02133                 }
02134             }
02135         }
02136         oxRegistry::getSession()->deleteVariable('su');
02137         oxRegistry::getSession()->deleteVariable('re');
02138 
02139         return $blSet;
02140     }
02141 
02147     public function setCreditPointsForInviter()
02148     {
02149         $blSet = false;
02150         $iPoints = $this->getConfig()->getConfigParam('dPointsForInvitation');
02151         if ($iPoints) {
02152             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02153             $this->oxuser__oxpoints = new oxField($iNewPoints, oxField::T_RAW);
02154             $blSet = $this->save();
02155         }
02156 
02157         return $blSet;
02158     }
02159 
02165     public function updateFbId()
02166     {
02167         $oFb = oxRegistry::get("oxFb");
02168         $blRet = false;
02169 
02170         if ($oFb->isConnected() && $oFb->getUser()) {
02171             $this->oxuser__oxfbid = new oxField($oFb->getUser());
02172             $blRet = $this->save();
02173         }
02174 
02175         return $blRet;
02176     }
02177 
02183     public function updateInvitationStatistics($aRecEmail)
02184     {
02185         $oDb = oxDb::getDb();
02186         $sUserId = $this->getId();
02187 
02188         if ($sUserId && is_array($aRecEmail) && count($aRecEmail) > 0) {
02189             //iserting statistics about invitation
02190             $sDate = oxRegistry::get("oxUtilsDate")->formatDBDate(date("Y-m-d"), true);
02191             $aRecEmail = oxDb::getInstance()->quoteArray($aRecEmail);
02192             foreach ($aRecEmail as $sRecEmail) {
02193                 $sSql = "INSERT INTO oxinvitations SET oxuserid = " . $oDb->quote($sUserId) . ", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02194                 $oDb->execute($sSql);
02195             }
02196         }
02197     }
02198 
02206     public function getIdByUserName($sUserName)
02207     {
02208         $oDb = oxDb::getDb();
02209         $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = " . $oDb->quote($sUserName);
02210         if (!$this->getConfig()->getConfigParam('blMallUsers')) {
02211             $sQ .= " AND `oxshopid` = " . $oDb->quote($this->getConfig()->getShopId());
02212         }
02213 
02214         return $oDb->getOne($sQ);
02215     }
02216 
02222     public function hasAccount()
02223     {
02224 
02225         return (bool) $this->oxuser__oxpassword->value;
02226     }
02227 
02233     public function isPriceViewModeNetto()
02234     {
02235         return (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
02236     }
02237 
02247     protected function _dbLogin($sUser, $sPassword, $sShopID)
02248     {
02249         $blOldHash = false;
02250         $oDb = oxDb::getDb();
02251 
02252         if ($this->_isDemoShop() && $this->isAdmin()) {
02253             $sUserOxId = $oDb->getOne($this->_getDemoShopLoginQuery($sUser, $sPassword));
02254         } else {
02255             $sUserOxId = $oDb->getOne($this->_getLoginQuery($sUser, $sPassword, $sShopID, $this->isAdmin()));
02256             if (!$sUserOxId) {
02257                 $sUserOxId = $oDb->getOne($this->_getLoginQueryHashedWithMD5($sUser, $sPassword, $sShopID, $this->isAdmin()));
02258                 $blOldHash = true;
02259             }
02260         }
02261 
02262         if ($sUserOxId) {
02263             if (!$this->load($sUserOxId)) {
02265                 $oEx = oxNew('oxUserException');
02266                 $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
02267                 throw $oEx;
02268             } elseif ($blOldHash && $this->getId()) {
02269                 $this->setPassword($sPassword);
02270                 $this->save();
02271             }
02272         }
02273     }
02274 
02280     protected function _isDemoShop()
02281     {
02282         $blDemoMode = false;
02283 
02284         if ($this->getConfig()->isDemoShop()) {
02285             $blDemoMode = true;
02286         }
02287 
02288         return $blDemoMode;
02289     }
02290 
02301     protected function _getDemoShopLoginQuery($sUser, $sPassword)
02302     {
02303         if ($sPassword == "admin" && $sUser == "admin") {
02304             $sSelect = "SELECT `oxid` FROM `oxuser` WHERE `oxrights` = 'malladmin' ";
02305         } else {
02307             $oEx = oxNew('oxUserException');
02308             $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
02309             throw $oEx;
02310         }
02311 
02312         return $sSelect;
02313     }
02314 }