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         $oDb = oxDb::getDb();
00636         if (!$sOXID) {
00637             $sOXID = $this->getId();
00638         }
00639 
00640         $sShopSelect = '';
00641         if (!$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00642             $sShopSelect = ' AND oxshopid = "' . $this->getConfig()->getShopId() . '" ';
00643         }
00644 
00645         //#4543 Query optimisation by splitting it into two, might need an logics optimisation as well
00646         if ($sOXID) {
00647             $sSelect = 'SELECT oxid FROM ' . $this->getViewName() . '
00648                     WHERE ( oxid = ' . $oDb->quote($sOXID) . ' ) ';
00649             $sSelect .= $sShopSelect;
00650 
00651             if (($sOxid = $oDb->getOne($sSelect, false, false))) {
00652                 // update - set oxid
00653                 $this->setId($sOxid);
00654 
00655                 return true;
00656             }
00657         }
00658 
00659         $sSelect = 'SELECT oxid FROM ' . $this->getViewName() . '
00660                     WHERE ( oxusername = ' . $oDb->quote($this->oxuser__oxusername->value) . ' ) ';
00661         $sSelect .= $sShopSelect;
00662 
00663         if (($sOxid = $oDb->getOne($sSelect, false, false))) {
00664             // update - set oxid
00665             $this->setId($sOxid);
00666 
00667             return true;
00668         }
00669 
00670         return false;
00671     }
00672 
00681     public function getOrders($iLimit = false, $iPage = 0)
00682     {
00683         $oOrders = oxNew('oxList');
00684         $oOrders->init('oxorder');
00685 
00686         if ($iLimit !== false) {
00687             $oOrders->setSqlLimit($iLimit * $iPage, $iLimit);
00688         }
00689 
00690         //P
00691         // Lists does not support loading from two tables, so orders
00692         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00693         // forcing to load product info which is used in templates
00694         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00695 
00696         //loading order for registered user
00697         if ($this->oxuser__oxregister->value > 1) {
00698             $oDb = oxDb::getDb();
00699             $sQ = 'select * from oxorder where oxuserid = ' . $oDb->quote($this->getId()) . ' and oxorderdate >= ' . $oDb->quote($this->oxuser__oxregister->value) . ' ';
00700 
00701             //#1546 - shopid check added, if it is not multishop
00702 
00703             $sQ .= ' order by oxorderdate desc ';
00704             $oOrders->selectString($sQ);
00705         }
00706 
00707         return $oOrders;
00708     }
00709 
00715     public function getOrderCount()
00716     {
00717         $iCnt = 0;
00718         if ($this->getId() && $this->oxuser__oxregister->value > 1) {
00719             $oDb = oxDb::getDb();
00720             $sQ = 'select count(*) from oxorder where oxuserid = ' . $oDb->quote($this->getId()) . ' AND oxorderdate >= ' . $oDb->quote($this->oxuser__oxregister->value) . ' and oxshopid = "' . $this->getConfig()->getShopId() . '" ';
00721             $iCnt = (int) $oDb->getOne($sQ);
00722         }
00723 
00724         return $iCnt;
00725     }
00726 
00732     public function getNoticeListArtCnt()
00733     {
00734         if ($this->_iCntNoticeListArticles === null) {
00735             $this->_iCntNoticeListArticles = 0;
00736             if ($this->getId()) {
00737                 $this->_iCntNoticeListArticles = $this->getBasket('noticelist')->getItemCount();
00738             }
00739         }
00740 
00741         return $this->_iCntNoticeListArticles;
00742     }
00743 
00749     public function getWishListArtCnt()
00750     {
00751         if ($this->_iCntWishListArticles === null) {
00752             $this->_iCntWishListArticles = false;
00753             if ($this->getId()) {
00754                 $this->_iCntWishListArticles = $this->getBasket('wishlist')->getItemCount();
00755             }
00756         }
00757 
00758         return $this->_iCntWishListArticles;
00759     }
00760 
00766     public function getEncodedDeliveryAddress()
00767     {
00768         return md5($this->_getMergedAddressFields());
00769     }
00770 
00777     public function getActiveCountry()
00778     {
00779         $sDeliveryCountry = '';
00780         $soxAddressId = oxRegistry::getSession()->getVariable('deladrid');
00781         if ($soxAddressId) {
00782             $oDelAddress = oxNew('oxaddress');
00783             $oDelAddress->load($soxAddressId);
00784             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00785         } elseif ($this->getId()) {
00786             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00787         } else {
00788             $oUser = oxNew('oxuser');
00789             if ($oUser->loadActiveUser()) {
00790                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00791             }
00792         }
00793 
00794         return $sDeliveryCountry;
00795     }
00796 
00804     public function createUser()
00805     {
00806         $oDb = oxDb::getDb();
00807         $sShopID = $this->getConfig()->getShopId();
00808 
00809         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00810         $sSelect = "select oxid from oxuser where oxusername = " . $oDb->quote($this->oxuser__oxusername->value) . " and oxpassword = '' ";
00811         if (!$this->_blMallUsers) {
00812             $sSelect .= " and oxshopid = '{$sShopID}' ";
00813         }
00814         $sOXID = $oDb->getOne($sSelect, false, false);
00815 
00816         // user without password found - lets use
00817         if (isset($sOXID) && $sOXID) {
00818             // try to update
00819             $this->delete($sOXID);
00820         } elseif ($this->_blMallUsers) {
00821             // must be sure if there is no dublicate user
00822             $sQ = "select oxid from oxuser where oxusername = " . $oDb->quote($this->oxuser__oxusername->value) . " and oxusername != '' ";
00823             if ($oDb->getOne($sQ, false, false)) {
00825                 $oEx = oxNew('oxUserException');
00826                 $oLang = oxRegistry::getLang();
00827                 $oEx->setMessage(sprintf($oLang->translateString('ERROR_MESSAGE_USER_USEREXISTS', $oLang->getTplLanguage()), $this->oxuser__oxusername->value));
00828                 throw $oEx;
00829             }
00830         }
00831 
00832         $this->oxuser__oxshopid = new oxField($sShopID, oxField::T_RAW);
00833         if (($blOK = $this->save())) {
00834             // dropping/cleaning old delivery address/payment info
00835             $oDb->execute("delete from oxaddress where oxaddress.oxuserid = " . $oDb->quote($this->oxuser__oxid->value) . " ");
00836             $oDb->execute("update oxuserpayments set oxuserpayments.oxuserid = " . $oDb->quote($this->oxuser__oxusername->value) . " where oxuserpayments.oxuserid = " . $oDb->quote($this->oxuser__oxid->value) . " ");
00837         } else {
00839             $oEx = oxNew('oxUserException');
00840             $oEx->setMessage('EXCEPTION_USER_USERCREATIONFAILED');
00841             throw $oEx;
00842         }
00843 
00844         return $blOK;
00845     }
00846 
00854     public function addToGroup($sGroupID)
00855     {
00856         if (!$this->inGroup($sGroupID)) {
00857             // create oxgroup object
00858             $oGroup = oxNew('oxGroups');
00859             if ($oGroup->load($sGroupID)) {
00860                 $oNewGroup = oxNew('oxobject2group');
00861                 $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00862                 $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00863                 if ($oNewGroup->save()) {
00864                     $this->_oGroups[$sGroupID] = $oGroup;
00865 
00866                     return true;
00867                 }
00868             }
00869         }
00870 
00871         return false;
00872     }
00873 
00879     public function removeFromGroup($sGroupID = null)
00880     {
00881         if ($sGroupID != null && $this->inGroup($sGroupID)) {
00882             $oGroups = oxNew('oxList');
00883             $oGroups->init('oxobject2group');
00884             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "' . $this->getId() . '" and oxobject2group.oxgroupsid = "' . $sGroupID . '" ';
00885             $oGroups->selectString($sSelect);
00886             foreach ($oGroups as $oRemgroup) {
00887                 if ($oRemgroup->delete()) {
00888                     unset($this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value]);
00889                 }
00890             }
00891         }
00892     }
00893 
00900     public function onOrderExecute($oBasket, $iSuccess)
00901     {
00902 
00903         if (is_numeric($iSuccess) && $iSuccess != 2 && $iSuccess <= 3) {
00904             //adding user to particular customer groups
00905             $myConfig = $this->getConfig();
00906             $dMidlleCustPrice = (float) $myConfig->getConfigParam('sMidlleCustPrice');
00907             $dLargeCustPrice = (float) $myConfig->getConfigParam('sLargeCustPrice');
00908 
00909             $this->addToGroup('oxidcustomer');
00910             $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00911             if ($dBasketPrice < $dMidlleCustPrice) {
00912                 $this->addToGroup('oxidsmallcust');
00913             }
00914             if ($dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice) {
00915                 $this->addToGroup('oxidmiddlecust');
00916             }
00917             if ($dBasketPrice >= $dLargeCustPrice) {
00918                 $this->addToGroup('oxidgoodcust');
00919             }
00920 
00921             if ($this->inGroup('oxidnotyetordered')) {
00922                 $this->removeFromGroup('oxidnotyetordered');
00923             }
00924         }
00925     }
00926 
00934     public function getBasket($sName)
00935     {
00936         if (!isset($this->_aBaskets[$sName])) {
00937             $oBasket = oxNew('oxuserbasket');
00938             $aWhere = array('oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName);
00939 
00940             // creating if it does not exist
00941             if (!$oBasket->assignRecord($oBasket->buildSelectString($aWhere))) {
00942                 $oBasket->oxuserbaskets__oxtitle = new oxField($sName);
00943                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00944 
00945                 // marking basket as new (it will not be saved in DB yet)
00946                 $oBasket->setIsNewBasket();
00947             }
00948 
00949             $this->_aBaskets[$sName] = $oBasket;
00950         }
00951 
00952         return $this->_aBaskets[$sName];
00953     }
00954 
00963     public function convertBirthday($aData)
00964     {
00965 
00966         // preparing data to process
00967         $iYear = isset($aData['year']) ? ((int) $aData['year']) : false;
00968         $iMonth = isset($aData['month']) ? ((int) $aData['month']) : false;
00969         $iDay = isset($aData['day']) ? ((int) $aData['day']) : false;
00970 
00971         // leaving empty if not set
00972         if (!$iYear && !$iMonth && !$iDay) {
00973             return "";
00974         }
00975 
00976         // year
00977         if (!$iYear || $iYear < 1000 || $iYear > 9999) {
00978             $iYear = date('Y');
00979         }
00980 
00981         // month
00982         if (!$iMonth || $iMonth < 1 || $iMonth > 12) {
00983             $iMonth = 1;
00984         }
00985 
00986         // maximum nuber of days in month
00987         $iMaxDays = 31;
00988         switch ($iMonth) {
00989             case 2:
00990                 if ($iMaxDays > 28) {
00991                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00992                 }
00993                 break;
00994             case 4:
00995             case 6:
00996             case 9:
00997             case 11:
00998                 $iMaxDays = min(30, $iMaxDays);
00999                 break;
01000         }
01001 
01002         // day
01003         if (!$iDay || $iDay < 1 || $iDay > $iMaxDays) {
01004             $iDay = 1;
01005         }
01006 
01007         // whole date
01008         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
01009     }
01010 
01016     public function getBoni()
01017     {
01018         if (!$iBoni = $this->getConfig()->getConfigParam('iCreditRating')) {
01019             $iBoni = 1000;
01020         }
01021 
01022         return $iBoni;
01023     }
01024 
01041     public function checkValues($sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress)
01042     {
01044         $oInputValidator = oxRegistry::get('oxInputValidator');
01045 
01046         // 1. checking user name
01047         $sLogin = $oInputValidator->checkLogin($this, $sLogin, $aInvAddress);
01048 
01049         // 2. checking email
01050         $oInputValidator->checkEmail($this, $sLogin, $aInvAddress);
01051 
01052         // 3. password
01053         $oInputValidator->checkPassword($this, $sPassword, $sPassword2, ((int) oxRegistry::getConfig()->getRequestParameter('option') == 3));
01054 
01055         // 4. required fields
01056         $oInputValidator->checkRequiredFields($this, $aInvAddress, $aDelAddress);
01057 
01058         // 5. country check
01059         $oInputValidator->checkCountries($this, $aInvAddress, $aDelAddress);
01060 
01061         // 6. vat id check.
01062         $oInputValidator->checkVatId($this, $aInvAddress);
01063 
01064 
01065         // throwing first validation error
01066         if ($oError = oxRegistry::get("oxInputValidator")->getFirstValidationError()) {
01067             throw $oError;
01068         }
01069     }
01070 
01080     public function setNewsSubscription($blSubscribe, $blSendOptIn, $blForceCheckOptIn = false)
01081     {
01082         // assigning to newsletter
01083         $blSuccess = false;
01084         $myConfig = $this->getConfig();
01085         $mySession = $this->getSession();
01086 
01087         // user wants to get newsletter messages or no ?
01088         $oNewsSubscription = $this->getNewsSubscription();
01089         if ($oNewsSubscription) {
01090             if ($blSubscribe && ($blForceCheckOptIn || ($iOptInStatus = $oNewsSubscription->getOptInStatus()) != 1)) {
01091                 if (!$blSendOptIn) {
01092 
01093                     // double-opt-in check is disabled - assigning automatically
01094                     $this->addToGroup('oxidnewsletter');
01095                     // and setting subscribed status
01096                     $oNewsSubscription->setOptInStatus(1);
01097                     $blSuccess = true;
01098                 } else {
01099 
01100                     // double-opt-in check enabled - sending confirmation email and setting waiting status
01101                     if ($iOptInStatus != 2) {
01102                         // sending double-opt-in mail
01103                         $oEmail = oxNew('oxemail');
01104                         $blSuccess = $oEmail->sendNewsletterDbOptInMail($this);
01105                     } else {
01106                         // mail already was sent, so just confirming that
01107                         $blSuccess = true;
01108                     }
01109 
01110                     $oNewsSubscription->setOptInStatus(2);
01111                 }
01112             } elseif (!$blSubscribe) {
01113                 // removing user from newsletter subscribers
01114                 $this->removeFromGroup('oxidnewsletter');
01115                 $oNewsSubscription->setOptInStatus(0);
01116                 $blSuccess = true;
01117             }
01118         }
01119 
01120         return $blSuccess;
01121     }
01122 
01138     public function changeUserData($sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress)
01139     {
01140         // validating values before saving. If validation fails - exception is thrown
01141         $this->checkValues($sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress);
01142         // input data is fine - lets save updated user info
01143 
01144         $this->assign($aInvAddress);
01145 
01146 
01147         // update old or add new delivery address
01148         $this->_assignAddress($aDelAddress);
01149 
01150         // saving new values
01151         if ($this->save()) {
01152 
01153             // assigning automatically to specific groups
01154             $sCountryId = isset($aInvAddress['oxuser__oxcountryid']) ? $aInvAddress['oxuser__oxcountryid'] : '';
01155             $this->_setAutoGroups($sCountryId);
01156         }
01157     }
01158 
01164     protected function _getMergedAddressFields()
01165     {
01166         $sDelAddress = '';
01167         $sDelAddress .= $this->oxuser__oxcompany;
01168         $sDelAddress .= $this->oxuser__oxusername;
01169         $sDelAddress .= $this->oxuser__oxfname;
01170         $sDelAddress .= $this->oxuser__oxlname;
01171         $sDelAddress .= $this->oxuser__oxstreet;
01172         $sDelAddress .= $this->oxuser__oxstreetnr;
01173         $sDelAddress .= $this->oxuser__oxaddinfo;
01174         $sDelAddress .= $this->oxuser__oxustid;
01175         $sDelAddress .= $this->oxuser__oxcity;
01176         $sDelAddress .= $this->oxuser__oxcountryid;
01177         $sDelAddress .= $this->oxuser__oxstateid;
01178         $sDelAddress .= $this->oxuser__oxzip;
01179         $sDelAddress .= $this->oxuser__oxfon;
01180         $sDelAddress .= $this->oxuser__oxfax;
01181         $sDelAddress .= $this->oxuser__oxsal;
01182 
01183         return $sDelAddress;
01184     }
01185 
01191     protected function _assignAddress($aDelAddress)
01192     {
01193         if (is_array($aDelAddress) && count($aDelAddress)) {
01194 
01195             $sAddressId = $this->getConfig()->getRequestParameter('oxaddressid');
01196             $sAddressId = ($sAddressId === null || $sAddressId == -1 || $sAddressId == -2) ? null : $sAddressId;
01197 
01198             $oAddress = oxNew('oxaddress');
01199             $oAddress->setId($sAddressId);
01200             $oAddress->load($sAddressId);
01201             $oAddress->assign($aDelAddress);
01202             $oAddress->oxaddress__oxuserid = new oxField($this->getId(), oxField::T_RAW);
01203             $oAddress->oxaddress__oxcountry = $this->getUserCountry($oAddress->oxaddress__oxcountryid->value);
01204             $oAddress->save();
01205 
01206             // resetting addresses
01207             $this->_aAddresses = null;
01208 
01209             // saving delivery Address for later use
01210             oxRegistry::getSession()->setVariable('deladrid', $oAddress->getId());
01211         } else {
01212             // resetting
01213             oxRegistry::getSession()->setVariable('deladrid', null);
01214         }
01215     }
01216 
01227     protected function _getLoginQueryHashedWithMD5($sUser, $sPassword, $sShopID, $blAdmin)
01228     {
01229         $myConfig = $this->getConfig();
01230         $oDb = oxDb::getDb();
01231 
01232         $sUserSelect = "oxuser.oxusername = " . $oDb->quote($sUser);
01233         $sPassSelect = " oxuser.oxpassword = BINARY MD5( CONCAT( " . $oDb->quote($sPassword) . ", UNHEX( oxuser.oxpasssalt ) ) ) ";
01234         $sShopSelect = "";
01235 
01236 
01237         // admin view: can only login with higher than 'user' rights
01238         if ($blAdmin) {
01239             $sShopSelect = " and ( oxrights != 'user' ) ";
01240         }
01241 
01242         $sSelect = "select `oxid` from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01243 
01244 
01245         return $sSelect;
01246     }
01247 
01260     protected function _getLoginQuery($sUser, $sPassword, $sShopID, $blAdmin)
01261     {
01262         $myConfig = $this->getConfig();
01263         $oDb = oxDb::getDb();
01264 
01265         $sUserSelect = "oxuser.oxusername = " . $oDb->quote($sUser);
01266 
01267         $sSalt = $oDb->getOne("SELECT `oxpasssalt` FROM `oxuser` WHERE  " . $sUserSelect);
01268 
01269         $sPassSelect = " oxuser.oxpassword = " . $oDb->quote($this->encodePassword($sPassword, $sSalt));
01270         $sShopSelect = "";
01271 
01272 
01273         // admin view: can only login with higher than 'user' rights
01274         if ($blAdmin) {
01275             $sShopSelect = " and ( oxrights != 'user' ) ";
01276         }
01277 
01278         $sSelect = "select `oxid` from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01279 
01280 
01281         return $sSelect;
01282     }
01283 
01293     protected function _getShopSelect($myConfig, $sShopID, $blAdmin)
01294     {
01295         $sShopSelect = "";
01296         // admin view: can only login with higher than 'user' rights
01297         if ($blAdmin) {
01298             $sShopSelect = " and ( oxrights != 'user' ) ";
01299         }
01300 
01301         return $sShopSelect;
01302     }
01303 
01318     public function login($sUser, $sPassword, $blCookie = false)
01319     {
01320         if ($this->isAdmin() && !count(oxRegistry::get("oxUtilsServer")->getOxCookie())) {
01322             $oEx = oxNew('oxCookieException');
01323             $oEx->setMessage('ERROR_MESSAGE_COOKIE_NOCOOKIE');
01324             throw $oEx;
01325         }
01326 
01327         $oConfig = $this->getConfig();
01328 
01329 
01330         if ($sPassword) {
01331 
01332             $sShopID = $oConfig->getShopId();
01333             $this->_dbLogin($sUser, $sPassword, $sShopID);
01334         }
01335 
01336 
01337 
01338 
01339         //login successful?
01340         if ($this->oxuser__oxid->value) {
01341 
01342             // yes, successful login
01343 
01344             //resetting active user
01345             $this->setUser(null);
01346 
01347             if ($this->isAdmin()) {
01348                 oxRegistry::getSession()->setVariable('auth', $this->oxuser__oxid->value);
01349             } else {
01350                 oxRegistry::getSession()->setVariable('usr', $this->oxuser__oxid->value);
01351             }
01352 
01353             // cookie must be set ?
01354             if ($blCookie && $oConfig->getConfigParam('blShowRememberMe')) {
01355                 oxRegistry::get("oxUtilsServer")->setUserCookie($this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $oConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value);
01356             }
01357 
01358             return true;
01359         } else {
01361             $oEx = oxNew('oxUserException');
01362             $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
01363             throw $oEx;
01364         }
01365     }
01366 
01372     public function logout()
01373     {
01374         // deleting session info
01375         oxRegistry::getSession()->deleteVariable('usr'); // for front end
01376         oxRegistry::getSession()->deleteVariable('auth'); // for back end
01377         oxRegistry::getSession()->deleteVariable('dynvalue');
01378         oxRegistry::getSession()->deleteVariable('paymentid');
01379         // oxRegistry::getSession()->deleteVariable( 'deladrid' );
01380 
01381         // delete cookie
01382         oxRegistry::get("oxUtilsServer")->deleteUserCookie($this->getConfig()->getShopID());
01383 
01384         // unsetting global user
01385         $this->setUser(null);
01386 
01387         return true;
01388     }
01389 
01396     public function loadAdminUser()
01397     {
01398         return $this->loadActiveUser(true);
01399     }
01400 
01409     public function loadActiveUser($blForceAdmin = false)
01410     {
01411         $oConfig = $this->getConfig();
01412 
01413         $blAdmin = $this->isAdmin() || $blForceAdmin;
01414 
01415         // first - checking session info
01416         $sUserID = $blAdmin ? oxRegistry::getSession()->getVariable('auth') : oxRegistry::getSession()->getVariable('usr');
01417 
01418         // trying automatic login (by 'remember me' cookie)
01419         $blFoundInCookie = false;
01420         if (!$sUserID && !$blAdmin && $oConfig->getConfigParam('blShowRememberMe')) {
01421             $sUserID = $this->_getCookieUserId();
01422             $blFoundInCookie = $sUserID ? true : false;
01423         }
01424 
01425         // If facebook connection is enabled, trying to login user using Facebook ID
01426         if (!$sUserID && !$blAdmin && $oConfig->getConfigParam("bl_showFbConnect")) {
01427             $sUserID = $this->_getFacebookUserId();
01428         }
01429 
01430         // checking user results
01431         if ($sUserID) {
01432             if ($this->load($sUserID)) {
01433                 // storing into session
01434                 if ($blAdmin) {
01435                     oxRegistry::getSession()->setVariable('auth', $sUserID);
01436                 } else {
01437                     oxRegistry::getSession()->setVariable('usr', $sUserID);
01438                 }
01439 
01440                 // marking the way user was loaded
01441                 $this->_blLoadedFromCookie = $blFoundInCookie;
01442 
01443                 return true;
01444             }
01445         } else {
01446             // no user
01447             if ($blAdmin) {
01448                 oxRegistry::getSession()->deleteVariable('auth');
01449             } else {
01450                 oxRegistry::getSession()->deleteVariable('usr');
01451             }
01452 
01453             return false;
01454         }
01455     }
01456 
01462     protected function _getFacebookUserId()
01463     {
01464         $oDb = oxDb::getDb();
01465         $oFb = oxRegistry::get("oxFb");
01466         $oConfig = $this->getConfig();
01467         if ($oFb->isConnected() && $oFb->getUser()) {
01468             $sUserSelect = "oxuser.oxfbid = " . $oDb->quote($oFb->getUser());
01469             $sShopSelect = "";
01470 
01471 
01472             $sSelect = "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01473             $sUserID = $oDb->getOne($sSelect);
01474         }
01475 
01476         return $sUserID;
01477     }
01478 
01484     protected function _getCookieUserId()
01485     {
01486         $sUserID = null;
01487         $oConfig = $this->getConfig();
01488         $sShopID = $oConfig->getShopId();
01489         if (($sSet = oxRegistry::get("oxUtilsServer")->getUserCookie($sShopID))) {
01490             $oDb = oxDb::getDb();
01491             $aData = explode('@@@', $sSet);
01492             $sUser = $aData[0];
01493             $sPWD = @$aData[1];
01494 
01495             $sSelect = 'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = ' . $oDb->quote($sUser);
01496 
01497             $rs = $oDb->select($sSelect);
01498             if ($rs != false && $rs->recordCount() > 0) {
01499                 while (!$rs->EOF) {
01500                     $sTest = crypt($rs->fields[1], $rs->fields[2]);
01501                     if ($sTest == $sPWD) {
01502                         // found
01503                         $sUserID = $rs->fields[0];
01504                         break;
01505                     }
01506                     $rs->moveNext();
01507                 }
01508             }
01509             // if cookie info is not valid, remove it.
01510             if (!$sUserID) {
01511                 oxRegistry::get('oxUtilsServer')->deleteUserCookie($sShopID);
01512             }
01513         }
01514 
01515         return $sUserID;
01516     }
01517 
01528     protected function _ldapLogin($sUser, $sPassword, $sShopID, $sShopSelect)
01529     {
01530         $aLDAPParams = $this->getConfig()->getConfigParam('aLDAPParams');
01531         $oLDAP = oxNew("oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT']);
01532 
01533         // maybe this is LDAP user but supplied email Address instead of LDAP login
01534         $oDb = oxDb::getDb();
01535         $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = " . $oDb->quote($sUser) . " $sShopSelect");
01536         if (isset($sLDAPKey) && $sLDAPKey) {
01537             $sUser = $sLDAPKey;
01538         }
01539 
01540         //$throws oxConnectionException
01541         $oLDAP->login($sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01542 
01543         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01544         if (isset($aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01545             // login successful
01546 
01547             // check if user is already in database
01548             $sSelect = "select oxid from oxuser where oxuser.oxusername = " . $oDb->quote($aData['OXUSERNAME']) . " $sShopSelect";
01549             $sOXID = $oDb->getOne($sSelect);
01550 
01551             if (!isset($sOXID) || !$sOXID) {
01552                 // we need to create a new user
01553                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01554                 $this->setId();
01555 
01556                 // map all user data fields
01557                 foreach ($aData as $fldname => $value) {
01558                     $sField = "oxuser__" . strtolower($fldname);
01559                     $this->$sField = new oxField($aData[$fldname]);
01560                 }
01561 
01562                 $this->oxuser__oxactive = new oxField(1);
01563                 $this->oxuser__oxshopid = new oxField($sShopID);
01564                 $this->oxuser__oxldapkey = new oxField($sUser);
01565                 $this->oxuser__oxrights = new oxField("user");
01566                 $this->setPassword("ldap user");
01567 
01568                 $this->save();
01569             } else {
01570                 // LDAP user is already in OXID DB, load it
01571                 $this->load($sOXID);
01572             }
01573         } else {
01575             $oEx = oxNew('oxUserException');
01576             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01577             throw $oEx;
01578         }
01579     }
01580 
01587     protected function _getUserRights()
01588     {
01589         // previously user had no rights defined
01590         if (!$this->oxuser__oxrights->value) {
01591             return 'user';
01592         }
01593 
01594         $oDb = oxDb::getDb();
01595         $myConfig = $this->getConfig();
01596         $sAuthRights = null;
01597 
01598         // choosing possible user rights index
01599         $sAuthUserID = $this->isAdmin() ? oxRegistry::getSession()->getVariable('auth') : null;
01600         $sAuthUserID = $sAuthUserID ? $sAuthUserID : oxRegistry::getSession()->getVariable('usr');
01601         if ($sAuthUserID) {
01602             $sAuthRights = $oDb->getOne('select oxrights from ' . $this->getViewName() . ' where oxid=' . $oDb->quote($sAuthUserID));
01603         }
01604 
01605         //preventing user rights edit for non admin
01606         $aRights = array();
01607 
01608         // selecting current users rights ...
01609         if ($sCurrRights = $oDb->getOne('select oxrights from ' . $this->getViewName() . ' where oxid=' . $oDb->quote($this->getId()))) {
01610             $aRights[] = $sCurrRights;
01611         }
01612         $aRights[] = 'user';
01613 
01614         if (!$sAuthRights || !($sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId())) {
01615             return current($aRights);
01616         } elseif ($sAuthRights == $myConfig->getShopId()) {
01617             $aRights[] = $sAuthRights;
01618             if (!in_array($this->oxuser__oxrights->value, $aRights)) {
01619                 return current($aRights);
01620             }
01621         }
01622 
01623         // leaving as it was set ...
01624         return $this->oxuser__oxrights->value;
01625     }
01626 
01632     protected function _insert()
01633     {
01634 
01635         // set oxcreate date
01636         $this->oxuser__oxcreate = new oxField(date('Y-m-d H:i:s'), oxField::T_RAW);
01637 
01638         if (!isset($this->oxuser__oxboni->value)) {
01639             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01640         }
01641 
01642         return parent::_insert();
01643     }
01644 
01650     protected function _update()
01651     {
01652         //V #M418: for not registered users, don't change boni during update
01653         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01654             $this->_aSkipSaveFields[] = 'oxboni';
01655         }
01656 
01657         // don't change this field
01658         $this->_aSkipSaveFields[] = 'oxcreate';
01659         if (!$this->isAdmin()) {
01660             $this->_aSkipSaveFields[] = 'oxcustnr';
01661             $this->_aSkipSaveFields[] = 'oxrights';
01662         }
01663 
01664         // updating subscription information
01665         if (($blUpdate = parent::_update())) {
01666             $this->getNewsSubscription()->updateSubscription($this);
01667         }
01668 
01669         return $blUpdate;
01670     }
01671 
01679     public function checkIfEmailExists($sEmail)
01680     {
01681         $myConfig = $this->getConfig();
01682         $oDb = oxDb::getDb();
01683         $iShopId = $myConfig->getShopId();
01684         $blExists = false;
01685 
01686         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = ' . $oDb->quote($sEmail);
01687         if (($sOxid = $this->getId())) {
01688             $sQ .= " and oxid <> " . $oDb->quote($sOxid);
01689         }
01690         $oRs = $oDb->select($sQ, false, false);
01691         if ($oRs != false && $oRs->recordCount() > 0) {
01692 
01693             if ($this->_blMallUsers) {
01694 
01695                 $blExists = true;
01696                 if ($oRs->fields[1] == 'user' && !$oRs->fields[2]) {
01697 
01698                     // password is not set - allow to override
01699                     $blExists = false;
01700                 }
01701             } else {
01702 
01703                 $blExists = false;
01704                 while (!$oRs->EOF) {
01705                     if ($oRs->fields[1] != 'user') {
01706 
01707                         // exists admin with same login - must not allow
01708                         $blExists = true;
01709                         break;
01710                     } elseif ($oRs->fields[0] == $iShopId && $oRs->fields[2]) {
01711 
01712                         // exists same login (with password) in same shop
01713                         $blExists = true;
01714                         break;
01715                     }
01716 
01717                     $oRs->moveNext();
01718                 }
01719             }
01720         }
01721 
01722         return $blExists;
01723     }
01724 
01732     public function getUserRecommLists($sOXID = null)
01733     {
01734         if (!$sOXID) {
01735             $sOXID = $this->getId();
01736         }
01737 
01738         // sets active page
01739         $iActPage = (int) oxRegistry::getConfig()->getRequestParameter('pgNr');
01740         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01741 
01742         // load only lists which we show on screen
01743         $iNrofCatArticles = $this->getConfig()->getConfigParam('iNrofCatArticles');
01744         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01745 
01746 
01747         $oRecommList = oxNew('oxList');
01748         $oRecommList->init('oxrecommlist');
01749         $oRecommList->setSqlLimit($iNrofCatArticles * $iActPage, $iNrofCatArticles);
01750         $iShopId = $this->getConfig()->getShopId();
01751         $sSelect = 'select * from oxrecommlists where oxuserid =' . oxDb::getDb()->quote($sOXID) . ' and oxshopid ="' . $iShopId . '"';
01752         $oRecommList->selectString($sSelect);
01753 
01754         return $oRecommList;
01755     }
01756 
01764     public function getRecommListsCount($sOx = null)
01765     {
01766         if (!$sOx) {
01767             $sOx = $this->getId();
01768         }
01769 
01770         if ($this->_iCntRecommLists === null || $sOx) {
01771             $oDb = oxDb::getDb();
01772             $this->_iCntRecommLists = 0;
01773             $iShopId = $this->getConfig()->getShopId();
01774             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote($sOx) . ' and oxshopid ="' . $iShopId . '"';
01775             $this->_iCntRecommLists = $oDb->getOne($sSelect);
01776         }
01777 
01778         return $this->_iCntRecommLists;
01779     }
01780 
01787     protected function _setAutoGroups($sCountryId)
01788     {
01789         // assigning automatically to specific groups
01790         $blForeigner = true;
01791         $blForeignGroupExists = false;
01792         $blInlandGroupExists = false;
01793 
01794         $aHomeCountry = $this->getConfig()->getConfigParam('aHomeCountry');
01795         // foreigner ?
01796         if (is_array($aHomeCountry)) {
01797             if (in_array($sCountryId, $aHomeCountry)) {
01798                 $blForeigner = false;
01799             }
01800         } elseif ($sCountryId == $aHomeCountry) {
01801             $blForeigner = false;
01802         }
01803 
01804         if ($this->inGroup('oxidforeigncustomer')) {
01805             $blForeignGroupExists = true;
01806             if (!$blForeigner) {
01807                 $this->removeFromGroup('oxidforeigncustomer');
01808             }
01809         }
01810 
01811         if ($this->inGroup('oxidnewcustomer')) {
01812             $blInlandGroupExists = true;
01813             if ($blForeigner) {
01814                 $this->removeFromGroup('oxidnewcustomer');
01815             }
01816         }
01817 
01818         if (!$blForeignGroupExists && $blForeigner) {
01819             $this->addToGroup('oxidforeigncustomer');
01820         }
01821         if (!$blInlandGroupExists && !$blForeigner) {
01822             $this->addToGroup('oxidnewcustomer');
01823         }
01824     }
01825 
01826 
01835     public function loadUserByUpdateId($sUid)
01836     {
01837         $oDb = oxDb::getDb();
01838         $sQ = "select oxid from " . $this->getViewName() . " where oxupdateexp >= " . time() . " and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = " . $oDb->quote($sUid);
01839         if ($sUserId = $oDb->getOne($sQ)) {
01840             return $this->load($sUserId);
01841         }
01842     }
01843 
01849     public function setUpdateKey($blReset = false)
01850     {
01851         $sUpKey = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
01852         $iUpTime = $blReset ? 0 : oxRegistry::get("oxUtilsDate")->getTime() + $this->getUpdateLinkTerm();
01853 
01854         // generating key
01855         $this->oxuser__oxupdatekey = new oxField($sUpKey, oxField::T_RAW);
01856 
01857         // setting expiration time for 6 hours
01858         $this->oxuser__oxupdateexp = new oxField($iUpTime, oxField::T_RAW);
01859 
01860         // saving
01861         $this->save();
01862     }
01863 
01869     public function getUpdateLinkTerm()
01870     {
01871         return 3600 * 6;
01872     }
01873 
01881     public function isExpiredUpdateId($sKey)
01882     {
01883         $oDb = oxDb::getDb();
01884         $sQ = "select 1 from " . $this->getViewName() . " where oxupdateexp >= " . time() . " and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = " . $oDb->quote($sKey);
01885 
01886         return !((bool) $oDb->getOne($sQ));
01887     }
01888 
01894     public function getUpdateId()
01895     {
01896         if ($this->_sUpdateKey === null) {
01897             $this->setUpdateKey();
01898             $this->_sUpdateKey = md5($this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value);
01899         }
01900 
01901         return $this->_sUpdateKey;
01902     }
01903 
01912     public function encodePassword($sPassword, $sSalt)
01913     {
01915         $oSha512Hasher = oxNew('oxSha512Hasher');
01917         $oHasher = oxNew('oxPasswordHasher', $oSha512Hasher);
01918 
01919         return $oHasher->hash($sPassword, $sSalt);
01920     }
01921 
01931     public function prepareSalt($sSalt)
01932     {
01934         $oOpenSSLFunctionalityChecker = oxNew('oxOpenSSLFunctionalityChecker');
01936         $oGenerator = oxNew('oxPasswordSaltGenerator', $oOpenSSLFunctionalityChecker);
01937 
01938         return $oGenerator->generate();
01939     }
01940 
01950     public function decodeSalt($sSaltHex)
01951     {
01952         return ($sSaltHex ? oxDb::getDb()->getOne("select UNHEX( '{$sSaltHex}' )") : '');
01953     }
01954 
01960     public function setPassword($sPassword = null)
01961     {
01963         $oOpenSSLFunctionalityChecker = oxNew('oxOpenSSLFunctionalityChecker');
01964         // setting salt if password is not empty
01966         $oSaltGenerator = oxNew('oxPasswordSaltGenerator', $oOpenSSLFunctionalityChecker);
01967 
01968         $sSalt = $sPassword ? $oSaltGenerator->generate() : '';
01969 
01970         // encoding only if password was not empty (e.g. user registration without pass)
01971         $sPassword = $sPassword ? $this->encodePassword($sPassword, $sSalt) : '';
01972 
01973         $this->oxuser__oxpassword = new oxField($sPassword, oxField::T_RAW);
01974         $this->oxuser__oxpasssalt = new oxField($sSalt, oxField::T_RAW);
01975     }
01976 
01984     public function isSamePassword($sNewPass)
01985     {
01986         return $this->encodePassword($sNewPass, $this->oxuser__oxpasssalt->value) == $this->oxuser__oxpassword->value;
01987     }
01988 
01994     public function isLoadedFromCookie()
01995     {
01996         return $this->_blLoadedFromCookie;
01997     }
01998 
02007     public function getPasswordHash()
02008     {
02009         $sHash = null;
02010         if ($this->oxuser__oxpassword->value) {
02011             $sHash = $this->oxuser__oxpassword->value;
02012         }
02013 
02014         return $sHash;
02015     }
02016 
02024     public function getReviewUserHash($sUserId)
02025     {
02026         $oDb = oxDb::getDb();
02027         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote($sUserId) . '');
02028 
02029         return $sReviewUserHash;
02030     }
02031 
02039     public function getReviewUserId($sReviewUserHash)
02040     {
02041         $oDb = oxDb::getDb();
02042         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote($sReviewUserHash) . '');
02043 
02044         return $sUserId;
02045     }
02046 
02054     public function getState()
02055     {
02056         return $this->getStateId();
02057     }
02058 
02064     public function getStateId()
02065     {
02066         return $this->oxuser__oxstateid->value;
02067     }
02068 
02076     public function getStateTitle($sId = null)
02077     {
02078         $oState = $this->_getStateObject();
02079 
02080         if (is_null($sId)) {
02081             $sId = $this->getStateId();
02082         }
02083 
02084         return $oState->getTitleById($sId);
02085     }
02086 
02092     public function isTermsAccepted()
02093     {
02094         $oDb = oxDb::getDb();
02095         $sShopId = $this->getConfig()->getShopId();
02096         $sUserId = $oDb->quote($this->getId());
02097 
02098         return (bool) $oDb->getOne("select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'");
02099     }
02100 
02104     public function acceptTerms()
02105     {
02106         $oDb = oxDb::getDb();
02107         $sUserId = $oDb->quote($this->getId());
02108         $sShopId = $this->getConfig()->getShopId();
02109         $sVersion = oxNew("oxcontent")->getTermsVersion();
02110 
02111         $oDb->execute("replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'");
02112     }
02113 
02123     public function setCreditPointsForRegistrant($sUserId, $sRecEmail)
02124     {
02125         $blSet = false;
02126         $oDb = oxDb::getDb();
02127         $iPoints = $this->getConfig()->getConfigParam('dPointsForRegistration');
02128         // check if this invitation is still not accepted
02129         $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);
02130         if ($iPoints && $iPending) {
02131             $this->oxuser__oxpoints = new oxField($iPoints, oxField::T_RAW);
02132             if ($blSet = $this->save()) {
02133                 // updating users statistics
02134                 $oDb->execute("UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = " . $oDb->quote($sUserId) . " and md5(oxemail) = " . $oDb->quote($sRecEmail));
02135                 $oInvUser = oxNew("oxuser");
02136                 if ($oInvUser->load($sUserId)) {
02137                     $blSet = $oInvUser->setCreditPointsForInviter();
02138                 }
02139             }
02140         }
02141         oxRegistry::getSession()->deleteVariable('su');
02142         oxRegistry::getSession()->deleteVariable('re');
02143 
02144         return $blSet;
02145     }
02146 
02152     public function setCreditPointsForInviter()
02153     {
02154         $blSet = false;
02155         $iPoints = $this->getConfig()->getConfigParam('dPointsForInvitation');
02156         if ($iPoints) {
02157             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02158             $this->oxuser__oxpoints = new oxField($iNewPoints, oxField::T_RAW);
02159             $blSet = $this->save();
02160         }
02161 
02162         return $blSet;
02163     }
02164 
02170     public function updateFbId()
02171     {
02172         $oFb = oxRegistry::get("oxFb");
02173         $blRet = false;
02174 
02175         if ($oFb->isConnected() && $oFb->getUser()) {
02176             $this->oxuser__oxfbid = new oxField($oFb->getUser());
02177             $blRet = $this->save();
02178         }
02179 
02180         return $blRet;
02181     }
02182 
02188     public function updateInvitationStatistics($aRecEmail)
02189     {
02190         $oDb = oxDb::getDb();
02191         $sUserId = $this->getId();
02192 
02193         if ($sUserId && is_array($aRecEmail) && count($aRecEmail) > 0) {
02194             //iserting statistics about invitation
02195             $sDate = oxRegistry::get("oxUtilsDate")->formatDBDate(date("Y-m-d"), true);
02196             $aRecEmail = oxDb::getInstance()->quoteArray($aRecEmail);
02197             foreach ($aRecEmail as $sRecEmail) {
02198                 $sSql = "INSERT INTO oxinvitations SET oxuserid = " . $oDb->quote($sUserId) . ", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02199                 $oDb->execute($sSql);
02200             }
02201         }
02202     }
02203 
02211     public function getIdByUserName($sUserName)
02212     {
02213         $oDb = oxDb::getDb();
02214         $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = " . $oDb->quote($sUserName);
02215         if (!$this->getConfig()->getConfigParam('blMallUsers')) {
02216             $sQ .= " AND `oxshopid` = " . $oDb->quote($this->getConfig()->getShopId());
02217         }
02218 
02219         return $oDb->getOne($sQ);
02220     }
02221 
02227     public function hasAccount()
02228     {
02229 
02230         return (bool) $this->oxuser__oxpassword->value;
02231     }
02232 
02238     public function isPriceViewModeNetto()
02239     {
02240         return (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
02241     }
02242 
02252     protected function _dbLogin($sUser, $sPassword, $sShopID)
02253     {
02254         $blOldHash = false;
02255         $oDb = oxDb::getDb();
02256 
02257         if ($this->_isDemoShop() && $this->isAdmin()) {
02258             $sUserOxId = $oDb->getOne($this->_getDemoShopLoginQuery($sUser, $sPassword));
02259         } else {
02260             $sUserOxId = $oDb->getOne($this->_getLoginQuery($sUser, $sPassword, $sShopID, $this->isAdmin()));
02261             if (!$sUserOxId) {
02262                 $sUserOxId = $oDb->getOne($this->_getLoginQueryHashedWithMD5($sUser, $sPassword, $sShopID, $this->isAdmin()));
02263                 $blOldHash = true;
02264             }
02265         }
02266 
02267         if ($sUserOxId) {
02268             if (!$this->load($sUserOxId)) {
02270                 $oEx = oxNew('oxUserException');
02271                 $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
02272                 throw $oEx;
02273             } elseif ($blOldHash && $this->getId()) {
02274                 $this->setPassword($sPassword);
02275                 $this->save();
02276             }
02277         }
02278     }
02279 
02285     protected function _isDemoShop()
02286     {
02287         $blDemoMode = false;
02288 
02289         if ($this->getConfig()->isDemoShop()) {
02290             $blDemoMode = true;
02291         }
02292 
02293         return $blDemoMode;
02294     }
02295 
02306     protected function _getDemoShopLoginQuery($sUser, $sPassword)
02307     {
02308         if ($sPassword == "admin" && $sUser == "admin") {
02309             $sSelect = "SELECT `oxid` FROM `oxuser` WHERE `oxrights` = 'malladmin' ";
02310         } else {
02312             $oEx = oxNew('oxUserException');
02313             $oEx->setMessage('ERROR_MESSAGE_USER_NOVALIDLOGIN');
02314             throw $oEx;
02315         }
02316 
02317         return $sSelect;
02318     }
02319 }