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 
01225     protected function _getLoginQueryHashedWithMD5($sUser, $sPassword, $sShopID, $blAdmin)
01226     {
01227         $myConfig = $this->getConfig();
01228         $oDb = oxDb::getDb();
01229 
01230         $sUserSelect = "oxuser.oxusername = " . $oDb->quote($sUser);
01231         $sPassSelect = " oxuser.oxpassword = BINARY MD5( CONCAT( " . $oDb->quote($sPassword) . ", UNHEX( oxuser.oxpasssalt ) ) ) ";
01232         $sShopSelect = "";
01233 
01234 
01235         // admin view: can only login with higher than 'user' rights
01236         if ($blAdmin) {
01237             $sShopSelect = " and ( oxrights != 'user' ) ";
01238         }
01239 
01240         $sSelect = "select `oxid` from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01241 
01242 
01243         return $sSelect;
01244     }
01245 
01258     protected function _getLoginQuery($sUser, $sPassword, $sShopID, $blAdmin)
01259     {
01260         $myConfig = $this->getConfig();
01261         $oDb = oxDb::getDb();
01262 
01263         $sUserSelect = "oxuser.oxusername = " . $oDb->quote($sUser);
01264 
01265         $sShopSelect = "";
01266         // admin view: can only login with higher than 'user' rights
01267         if ($blAdmin) {
01268             $sShopSelect = " and ( oxrights != 'user' ) ";
01269         }
01270 
01271         $sSalt = $oDb->getOne("SELECT `oxpasssalt` FROM `oxuser` WHERE  " . $sUserSelect . $sShopSelect);
01272 
01273         $sPassSelect = " oxuser.oxpassword = " . $oDb->quote($this->encodePassword($sPassword, $sSalt));
01274 
01275         $sSelect = "select `oxid` from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01276 
01277 
01278         return $sSelect;
01279     }
01280 
01290     protected function _getShopSelect($myConfig, $sShopID, $blAdmin)
01291     {
01292         $sShopSelect = "";
01293         // admin view: can only login with higher than 'user' rights
01294         if ($blAdmin) {
01295             $sShopSelect = " and ( oxrights != 'user' ) ";
01296         }
01297 
01298         return $sShopSelect;
01299     }
01300 
01315     public function login($sUser, $sPassword, $blCookie = false)
01316     {
01317         if ($this->isAdmin() && !count(oxRegistry::get("oxUtilsServer")->getOxCookie())) {
01319             $oEx = oxNew('oxCookieException');
01320             $oEx->setMessage('ERROR_MESSAGE_COOKIE_NOCOOKIE');
01321             throw $oEx;
01322         }
01323 
01324         $oConfig = $this->getConfig();
01325 
01326 
01327         if ($sPassword) {
01328 
01329             $sShopID = $oConfig->getShopId();
01330             $this->_dbLogin($sUser, $sPassword, $sShopID);
01331         }
01332 
01333 
01334 
01335 
01336         //login successful?
01337         if ($this->oxuser__oxid->value) {
01338 
01339             // yes, successful login
01340 
01341             //resetting active user
01342             $this->setUser(null);
01343 
01344             if ($this->isAdmin()) {
01345                 oxRegistry::getSession()->setVariable('auth', $this->oxuser__oxid->value);
01346             } else {
01347                 oxRegistry::getSession()->setVariable('usr', $this->oxuser__oxid->value);
01348             }
01349 
01350             // cookie must be set ?
01351             if ($blCookie && $oConfig->getConfigParam('blShowRememberMe')) {
01352                 oxRegistry::get("oxUtilsServer")->setUserCookie($this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $oConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value);
01353             }
01354 
01355             return true;
01356         } else {
01358             $oEx = oxNew('oxUserException');
01359             $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
01360             throw $oEx;
01361         }
01362     }
01363 
01369     public function logout()
01370     {
01371         // deleting session info
01372         oxRegistry::getSession()->deleteVariable('usr'); // for front end
01373         oxRegistry::getSession()->deleteVariable('auth'); // for back end
01374         oxRegistry::getSession()->deleteVariable('dynvalue');
01375         oxRegistry::getSession()->deleteVariable('paymentid');
01376         // oxRegistry::getSession()->deleteVariable( 'deladrid' );
01377 
01378         // delete cookie
01379         oxRegistry::get("oxUtilsServer")->deleteUserCookie($this->getConfig()->getShopID());
01380 
01381         // unsetting global user
01382         $this->setUser(null);
01383 
01384         return true;
01385     }
01386 
01393     public function loadAdminUser()
01394     {
01395         return $this->loadActiveUser(true);
01396     }
01397 
01406     public function loadActiveUser($blForceAdmin = false)
01407     {
01408         $oConfig = $this->getConfig();
01409 
01410         $blAdmin = $this->isAdmin() || $blForceAdmin;
01411 
01412         // first - checking session info
01413         $sUserID = $blAdmin ? oxRegistry::getSession()->getVariable('auth') : oxRegistry::getSession()->getVariable('usr');
01414 
01415         // trying automatic login (by 'remember me' cookie)
01416         $blFoundInCookie = false;
01417         if (!$sUserID && !$blAdmin && $oConfig->getConfigParam('blShowRememberMe')) {
01418             $sUserID = $this->_getCookieUserId();
01419             $blFoundInCookie = $sUserID ? true : false;
01420         }
01421 
01422         // If facebook connection is enabled, trying to login user using Facebook ID
01423         if (!$sUserID && !$blAdmin && $oConfig->getConfigParam("bl_showFbConnect")) {
01424             $sUserID = $this->_getFacebookUserId();
01425         }
01426 
01427         // checking user results
01428         if ($sUserID) {
01429             if ($this->load($sUserID)) {
01430                 // storing into session
01431                 if ($blAdmin) {
01432                     oxRegistry::getSession()->setVariable('auth', $sUserID);
01433                 } else {
01434                     oxRegistry::getSession()->setVariable('usr', $sUserID);
01435                 }
01436 
01437                 // marking the way user was loaded
01438                 $this->_blLoadedFromCookie = $blFoundInCookie;
01439 
01440                 return true;
01441             }
01442         } else {
01443             // no user
01444             if ($blAdmin) {
01445                 oxRegistry::getSession()->deleteVariable('auth');
01446             } else {
01447                 oxRegistry::getSession()->deleteVariable('usr');
01448             }
01449 
01450             return false;
01451         }
01452     }
01453 
01459     protected function _getFacebookUserId()
01460     {
01461         $oDb = oxDb::getDb();
01462         $oFb = oxRegistry::get("oxFb");
01463         $oConfig = $this->getConfig();
01464         if ($oFb->isConnected() && $oFb->getUser()) {
01465             $sUserSelect = "oxuser.oxfbid = " . $oDb->quote($oFb->getUser());
01466             $sShopSelect = "";
01467 
01468 
01469             $sSelect = "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01470             $sUserID = $oDb->getOne($sSelect);
01471         }
01472 
01473         return $sUserID;
01474     }
01475 
01481     protected function _getCookieUserId()
01482     {
01483         $sUserID = null;
01484         $oConfig = $this->getConfig();
01485         $sShopID = $oConfig->getShopId();
01486         if (($sSet = oxRegistry::get("oxUtilsServer")->getUserCookie($sShopID))) {
01487             $oDb = oxDb::getDb();
01488             $aData = explode('@@@', $sSet);
01489             $sUser = $aData[0];
01490             $sPWD = @$aData[1];
01491 
01492             $sSelect = 'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = ' . $oDb->quote($sUser);
01493 
01494             $rs = $oDb->select($sSelect);
01495             if ($rs != false && $rs->recordCount() > 0) {
01496                 while (!$rs->EOF) {
01497                     $sTest = crypt($rs->fields[1], $rs->fields[2]);
01498                     if ($sTest == $sPWD) {
01499                         // found
01500                         $sUserID = $rs->fields[0];
01501                         break;
01502                     }
01503                     $rs->moveNext();
01504                 }
01505             }
01506             // if cookie info is not valid, remove it.
01507             if (!$sUserID) {
01508                 oxRegistry::get('oxUtilsServer')->deleteUserCookie($sShopID);
01509             }
01510         }
01511 
01512         return $sUserID;
01513     }
01514 
01525     protected function _ldapLogin($sUser, $sPassword, $sShopID, $sShopSelect)
01526     {
01527         $aLDAPParams = $this->getConfig()->getConfigParam('aLDAPParams');
01528         $oLDAP = oxNew("oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT']);
01529 
01530         // maybe this is LDAP user but supplied email Address instead of LDAP login
01531         $oDb = oxDb::getDb();
01532         $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = " . $oDb->quote($sUser) . " $sShopSelect");
01533         if (isset($sLDAPKey) && $sLDAPKey) {
01534             $sUser = $sLDAPKey;
01535         }
01536 
01537         //$throws oxConnectionException
01538         $oLDAP->login($sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01539 
01540         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01541         if (isset($aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01542             // login successful
01543 
01544             // check if user is already in database
01545             $sSelect = "select oxid from oxuser where oxuser.oxusername = " . $oDb->quote($aData['OXUSERNAME']) . " $sShopSelect";
01546             $sOXID = $oDb->getOne($sSelect);
01547 
01548             if (!isset($sOXID) || !$sOXID) {
01549                 // we need to create a new user
01550                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01551                 $this->setId();
01552 
01553                 // map all user data fields
01554                 foreach ($aData as $fldname => $value) {
01555                     $sField = "oxuser__" . strtolower($fldname);
01556                     $this->$sField = new oxField($aData[$fldname]);
01557                 }
01558 
01559                 $this->oxuser__oxactive = new oxField(1);
01560                 $this->oxuser__oxshopid = new oxField($sShopID);
01561                 $this->oxuser__oxldapkey = new oxField($sUser);
01562                 $this->oxuser__oxrights = new oxField("user");
01563                 $this->setPassword("ldap user");
01564 
01565                 $this->save();
01566             } else {
01567                 // LDAP user is already in OXID DB, load it
01568                 $this->load($sOXID);
01569             }
01570         } else {
01572             $oEx = oxNew('oxUserException');
01573             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01574             throw $oEx;
01575         }
01576     }
01577 
01584     protected function _getUserRights()
01585     {
01586         // previously user had no rights defined
01587         if (!$this->oxuser__oxrights->value) {
01588             return 'user';
01589         }
01590 
01591         $oDb = oxDb::getDb();
01592         $myConfig = $this->getConfig();
01593         $sAuthRights = null;
01594 
01595         // choosing possible user rights index
01596         $sAuthUserID = $this->isAdmin() ? oxRegistry::getSession()->getVariable('auth') : null;
01597         $sAuthUserID = $sAuthUserID ? $sAuthUserID : oxRegistry::getSession()->getVariable('usr');
01598         if ($sAuthUserID) {
01599             $sAuthRights = $oDb->getOne('select oxrights from ' . $this->getViewName() . ' where oxid=' . $oDb->quote($sAuthUserID));
01600         }
01601 
01602         //preventing user rights edit for non admin
01603         $aRights = array();
01604 
01605         // selecting current users rights ...
01606         if ($sCurrRights = $oDb->getOne('select oxrights from ' . $this->getViewName() . ' where oxid=' . $oDb->quote($this->getId()))) {
01607             $aRights[] = $sCurrRights;
01608         }
01609         $aRights[] = 'user';
01610 
01611         if (!$sAuthRights || !($sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId())) {
01612             return current($aRights);
01613         } elseif ($sAuthRights == $myConfig->getShopId()) {
01614             $aRights[] = $sAuthRights;
01615             if (!in_array($this->oxuser__oxrights->value, $aRights)) {
01616                 return current($aRights);
01617             }
01618         }
01619 
01620         // leaving as it was set ...
01621         return $this->oxuser__oxrights->value;
01622     }
01623 
01629     protected function _insert()
01630     {
01631 
01632         // set oxcreate date
01633         $this->oxuser__oxcreate = new oxField(date('Y-m-d H:i:s'), oxField::T_RAW);
01634 
01635         if (!isset($this->oxuser__oxboni->value)) {
01636             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01637         }
01638 
01639         return parent::_insert();
01640     }
01641 
01647     protected function _update()
01648     {
01649         //V #M418: for not registered users, don't change boni during update
01650         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01651             $this->_aSkipSaveFields[] = 'oxboni';
01652         }
01653 
01654         // don't change this field
01655         $this->_aSkipSaveFields[] = 'oxcreate';
01656         if (!$this->isAdmin()) {
01657             $this->_aSkipSaveFields[] = 'oxcustnr';
01658             $this->_aSkipSaveFields[] = 'oxrights';
01659         }
01660 
01661         // updating subscription information
01662         if (($blUpdate = parent::_update())) {
01663             $this->getNewsSubscription()->updateSubscription($this);
01664         }
01665 
01666         return $blUpdate;
01667     }
01668 
01676     public function checkIfEmailExists($sEmail)
01677     {
01678         $myConfig = $this->getConfig();
01679         $oDb = oxDb::getDb();
01680         $iShopId = $myConfig->getShopId();
01681         $blExists = false;
01682 
01683         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = ' . $oDb->quote($sEmail);
01684         if (($sOxid = $this->getId())) {
01685             $sQ .= " and oxid <> " . $oDb->quote($sOxid);
01686         }
01687         $oRs = $oDb->select($sQ, false, false);
01688         if ($oRs != false && $oRs->recordCount() > 0) {
01689 
01690             if ($this->_blMallUsers) {
01691 
01692                 $blExists = true;
01693                 if ($oRs->fields[1] == 'user' && !$oRs->fields[2]) {
01694 
01695                     // password is not set - allow to override
01696                     $blExists = false;
01697                 }
01698             } else {
01699 
01700                 $blExists = false;
01701                 while (!$oRs->EOF) {
01702                     if ($oRs->fields[1] != 'user') {
01703 
01704                         // exists admin with same login - must not allow
01705                         $blExists = true;
01706                         break;
01707                     } elseif ($oRs->fields[0] == $iShopId && $oRs->fields[2]) {
01708 
01709                         // exists same login (with password) in same shop
01710                         $blExists = true;
01711                         break;
01712                     }
01713 
01714                     $oRs->moveNext();
01715                 }
01716             }
01717         }
01718 
01719         return $blExists;
01720     }
01721 
01729     public function getUserRecommLists($sOXID = null)
01730     {
01731         if (!$sOXID) {
01732             $sOXID = $this->getId();
01733         }
01734 
01735         // sets active page
01736         $iActPage = (int) oxRegistry::getConfig()->getRequestParameter('pgNr');
01737         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01738 
01739         // load only lists which we show on screen
01740         $iNrofCatArticles = $this->getConfig()->getConfigParam('iNrofCatArticles');
01741         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01742 
01743 
01744         $oRecommList = oxNew('oxList');
01745         $oRecommList->init('oxrecommlist');
01746         $oRecommList->setSqlLimit($iNrofCatArticles * $iActPage, $iNrofCatArticles);
01747         $iShopId = $this->getConfig()->getShopId();
01748         $sSelect = 'select * from oxrecommlists where oxuserid =' . oxDb::getDb()->quote($sOXID) . ' and oxshopid ="' . $iShopId . '"';
01749         $oRecommList->selectString($sSelect);
01750 
01751         return $oRecommList;
01752     }
01753 
01761     public function getRecommListsCount($sOx = null)
01762     {
01763         if (!$sOx) {
01764             $sOx = $this->getId();
01765         }
01766 
01767         if ($this->_iCntRecommLists === null || $sOx) {
01768             $oDb = oxDb::getDb();
01769             $this->_iCntRecommLists = 0;
01770             $iShopId = $this->getConfig()->getShopId();
01771             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote($sOx) . ' and oxshopid ="' . $iShopId . '"';
01772             $this->_iCntRecommLists = $oDb->getOne($sSelect);
01773         }
01774 
01775         return $this->_iCntRecommLists;
01776     }
01777 
01784     protected function _setAutoGroups($sCountryId)
01785     {
01786         // assigning automatically to specific groups
01787         $blForeigner = true;
01788         $blForeignGroupExists = false;
01789         $blInlandGroupExists = false;
01790 
01791         $aHomeCountry = $this->getConfig()->getConfigParam('aHomeCountry');
01792         // foreigner ?
01793         if (is_array($aHomeCountry)) {
01794             if (in_array($sCountryId, $aHomeCountry)) {
01795                 $blForeigner = false;
01796             }
01797         } elseif ($sCountryId == $aHomeCountry) {
01798             $blForeigner = false;
01799         }
01800 
01801         if ($this->inGroup('oxidforeigncustomer')) {
01802             $blForeignGroupExists = true;
01803             if (!$blForeigner) {
01804                 $this->removeFromGroup('oxidforeigncustomer');
01805             }
01806         }
01807 
01808         if ($this->inGroup('oxidnewcustomer')) {
01809             $blInlandGroupExists = true;
01810             if ($blForeigner) {
01811                 $this->removeFromGroup('oxidnewcustomer');
01812             }
01813         }
01814 
01815         if (!$blForeignGroupExists && $blForeigner) {
01816             $this->addToGroup('oxidforeigncustomer');
01817         }
01818         if (!$blInlandGroupExists && !$blForeigner) {
01819             $this->addToGroup('oxidnewcustomer');
01820         }
01821     }
01822 
01823 
01832     public function loadUserByUpdateId($sUid)
01833     {
01834         $oDb = oxDb::getDb();
01835         $sQ = "select oxid from " . $this->getViewName() . " where oxupdateexp >= " . time() . " and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = " . $oDb->quote($sUid);
01836         if ($sUserId = $oDb->getOne($sQ)) {
01837             return $this->load($sUserId);
01838         }
01839     }
01840 
01846     public function setUpdateKey($blReset = false)
01847     {
01848         $sUpKey = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
01849         $iUpTime = $blReset ? 0 : oxRegistry::get("oxUtilsDate")->getTime() + $this->getUpdateLinkTerm();
01850 
01851         // generating key
01852         $this->oxuser__oxupdatekey = new oxField($sUpKey, oxField::T_RAW);
01853 
01854         // setting expiration time for 6 hours
01855         $this->oxuser__oxupdateexp = new oxField($iUpTime, oxField::T_RAW);
01856 
01857         // saving
01858         $this->save();
01859     }
01860 
01866     public function getUpdateLinkTerm()
01867     {
01868         return 3600 * 6;
01869     }
01870 
01878     public function isExpiredUpdateId($sKey)
01879     {
01880         $oDb = oxDb::getDb();
01881         $sQ = "select 1 from " . $this->getViewName() . " where oxupdateexp >= " . time() . " and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = " . $oDb->quote($sKey);
01882 
01883         return !((bool) $oDb->getOne($sQ));
01884     }
01885 
01891     public function getUpdateId()
01892     {
01893         if ($this->_sUpdateKey === null) {
01894             $this->setUpdateKey();
01895             $this->_sUpdateKey = md5($this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value);
01896         }
01897 
01898         return $this->_sUpdateKey;
01899     }
01900 
01909     public function encodePassword($sPassword, $sSalt)
01910     {
01912         $oSha512Hasher = oxNew('oxSha512Hasher');
01914         $oHasher = oxNew('oxPasswordHasher', $oSha512Hasher);
01915 
01916         return $oHasher->hash($sPassword, $sSalt);
01917     }
01918 
01928     public function prepareSalt($sSalt)
01929     {
01931         $oOpenSSLFunctionalityChecker = oxNew('oxOpenSSLFunctionalityChecker');
01933         $oGenerator = oxNew('oxPasswordSaltGenerator', $oOpenSSLFunctionalityChecker);
01934 
01935         return $oGenerator->generate();
01936     }
01937 
01947     public function decodeSalt($sSaltHex)
01948     {
01949         return ($sSaltHex ? oxDb::getDb()->getOne("select UNHEX( '{$sSaltHex}' )") : '');
01950     }
01951 
01957     public function setPassword($sPassword = null)
01958     {
01960         $oOpenSSLFunctionalityChecker = oxNew('oxOpenSSLFunctionalityChecker');
01961         // setting salt if password is not empty
01963         $oSaltGenerator = oxNew('oxPasswordSaltGenerator', $oOpenSSLFunctionalityChecker);
01964 
01965         $sSalt = $sPassword ? $oSaltGenerator->generate() : '';
01966 
01967         // encoding only if password was not empty (e.g. user registration without pass)
01968         $sPassword = $sPassword ? $this->encodePassword($sPassword, $sSalt) : '';
01969 
01970         $this->oxuser__oxpassword = new oxField($sPassword, oxField::T_RAW);
01971         $this->oxuser__oxpasssalt = new oxField($sSalt, oxField::T_RAW);
01972     }
01973 
01981     public function isSamePassword($sNewPass)
01982     {
01983         return $this->encodePassword($sNewPass, $this->oxuser__oxpasssalt->value) == $this->oxuser__oxpassword->value;
01984     }
01985 
01991     public function isLoadedFromCookie()
01992     {
01993         return $this->_blLoadedFromCookie;
01994     }
01995 
02004     public function getPasswordHash()
02005     {
02006         $sHash = null;
02007         if ($this->oxuser__oxpassword->value) {
02008             $sHash = $this->oxuser__oxpassword->value;
02009         }
02010 
02011         return $sHash;
02012     }
02013 
02021     public function getReviewUserHash($sUserId)
02022     {
02023         $oDb = oxDb::getDb();
02024         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote($sUserId) . '');
02025 
02026         return $sReviewUserHash;
02027     }
02028 
02036     public function getReviewUserId($sReviewUserHash)
02037     {
02038         $oDb = oxDb::getDb();
02039         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote($sReviewUserHash) . '');
02040 
02041         return $sUserId;
02042     }
02043 
02051     public function getState()
02052     {
02053         return $this->getStateId();
02054     }
02055 
02061     public function getStateId()
02062     {
02063         return $this->oxuser__oxstateid->value;
02064     }
02065 
02073     public function getStateTitle($sId = null)
02074     {
02075         $oState = $this->_getStateObject();
02076 
02077         if (is_null($sId)) {
02078             $sId = $this->getStateId();
02079         }
02080 
02081         return $oState->getTitleById($sId);
02082     }
02083 
02089     public function isTermsAccepted()
02090     {
02091         $oDb = oxDb::getDb();
02092         $sShopId = $this->getConfig()->getShopId();
02093         $sUserId = $oDb->quote($this->getId());
02094 
02095         return (bool) $oDb->getOne("select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'");
02096     }
02097 
02101     public function acceptTerms()
02102     {
02103         $oDb = oxDb::getDb();
02104         $sUserId = $oDb->quote($this->getId());
02105         $sShopId = $this->getConfig()->getShopId();
02106         $sVersion = oxNew("oxcontent")->getTermsVersion();
02107 
02108         $oDb->execute("replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'");
02109     }
02110 
02120     public function setCreditPointsForRegistrant($sUserId, $sRecEmail)
02121     {
02122         $blSet = false;
02123         $oDb = oxDb::getDb();
02124         $iPoints = $this->getConfig()->getConfigParam('dPointsForRegistration');
02125         // check if this invitation is still not accepted
02126         $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);
02127         if ($iPoints && $iPending) {
02128             $this->oxuser__oxpoints = new oxField($iPoints, oxField::T_RAW);
02129             if ($blSet = $this->save()) {
02130                 // updating users statistics
02131                 $oDb->execute("UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = " . $oDb->quote($sUserId) . " and md5(oxemail) = " . $oDb->quote($sRecEmail));
02132                 $oInvUser = oxNew("oxuser");
02133                 if ($oInvUser->load($sUserId)) {
02134                     $blSet = $oInvUser->setCreditPointsForInviter();
02135                 }
02136             }
02137         }
02138         oxRegistry::getSession()->deleteVariable('su');
02139         oxRegistry::getSession()->deleteVariable('re');
02140 
02141         return $blSet;
02142     }
02143 
02149     public function setCreditPointsForInviter()
02150     {
02151         $blSet = false;
02152         $iPoints = $this->getConfig()->getConfigParam('dPointsForInvitation');
02153         if ($iPoints) {
02154             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02155             $this->oxuser__oxpoints = new oxField($iNewPoints, oxField::T_RAW);
02156             $blSet = $this->save();
02157         }
02158 
02159         return $blSet;
02160     }
02161 
02167     public function updateFbId()
02168     {
02169         $oFb = oxRegistry::get("oxFb");
02170         $blRet = false;
02171 
02172         if ($oFb->isConnected() && $oFb->getUser()) {
02173             $this->oxuser__oxfbid = new oxField($oFb->getUser());
02174             $blRet = $this->save();
02175         }
02176 
02177         return $blRet;
02178     }
02179 
02185     public function updateInvitationStatistics($aRecEmail)
02186     {
02187         $oDb = oxDb::getDb();
02188         $sUserId = $this->getId();
02189 
02190         if ($sUserId && is_array($aRecEmail) && count($aRecEmail) > 0) {
02191             //iserting statistics about invitation
02192             $sDate = oxRegistry::get("oxUtilsDate")->formatDBDate(date("Y-m-d"), true);
02193             $aRecEmail = oxDb::getInstance()->quoteArray($aRecEmail);
02194             foreach ($aRecEmail as $sRecEmail) {
02195                 $sSql = "INSERT INTO oxinvitations SET oxuserid = " . $oDb->quote($sUserId) . ", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02196                 $oDb->execute($sSql);
02197             }
02198         }
02199     }
02200 
02208     public function getIdByUserName($sUserName)
02209     {
02210         $oDb = oxDb::getDb();
02211         $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = " . $oDb->quote($sUserName);
02212         if (!$this->getConfig()->getConfigParam('blMallUsers')) {
02213             $sQ .= " AND `oxshopid` = " . $oDb->quote($this->getConfig()->getShopId());
02214         }
02215 
02216         return $oDb->getOne($sQ);
02217     }
02218 
02224     public function hasAccount()
02225     {
02226 
02227         return (bool) $this->oxuser__oxpassword->value;
02228     }
02229 
02235     public function isPriceViewModeNetto()
02236     {
02237         return (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
02238     }
02239 
02249     protected function _dbLogin($sUser, $sPassword, $sShopID)
02250     {
02251         $blOldHash = false;
02252         $oDb = oxDb::getDb();
02253 
02254         if ($this->_isDemoShop() && $this->isAdmin()) {
02255             $sUserOxId = $oDb->getOne($this->_getDemoShopLoginQuery($sUser, $sPassword));
02256         } else {
02257             $sUserOxId = $oDb->getOne($this->_getLoginQuery($sUser, $sPassword, $sShopID, $this->isAdmin()));
02258             if (!$sUserOxId) {
02259                 $sUserOxId = $oDb->getOne($this->_getLoginQueryHashedWithMD5($sUser, $sPassword, $sShopID, $this->isAdmin()));
02260                 $blOldHash = true;
02261             }
02262         }
02263 
02264         if ($sUserOxId) {
02265             if (!$this->load($sUserOxId)) {
02267                 $oEx = oxNew('oxUserException');
02268                 $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
02269                 throw $oEx;
02270             } elseif ($blOldHash && $this->getId()) {
02271                 $this->setPassword($sPassword);
02272                 $this->save();
02273             }
02274         }
02275     }
02276 
02282     protected function _isDemoShop()
02283     {
02284         $blDemoMode = false;
02285 
02286         if ($this->getConfig()->isDemoShop()) {
02287             $blDemoMode = true;
02288         }
02289 
02290         return $blDemoMode;
02291     }
02292 
02303     protected function _getDemoShopLoginQuery($sUser, $sPassword)
02304     {
02305         if ($sPassword == "admin" && $sUser == "admin") {
02306             $sSelect = "SELECT `oxid` FROM `oxuser` WHERE `oxrights` = 'malladmin' ";
02307         } else {
02309             $oEx = oxNew('oxUserException');
02310             $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
02311             throw $oEx;
02312         }
02313 
02314         return $sSelect;
02315     }
02316 }