oxuser.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxUser extends oxBase
00009 {
00014     protected $_blDisableShopCheck = true;
00015 
00020     protected $_oNewsSubscription = null;
00021 
00026     protected $_sCoreTbl = 'oxuser';
00027 
00032     protected $_sClassName = 'oxuser';
00033 
00039     protected $_aBaskets = array();
00040 
00046     protected $_oGroups;
00047 
00053     protected $_aAddresses = array();
00054 
00060     protected $_oPayments;
00061 
00067     protected $_oRecommList;
00068 
00074     protected $_blMallUsers = false;
00075 
00081     protected static $_aUserCookie = array();
00082 
00088     protected $_iCntNoticeListArticles = null;
00089 
00095     protected $_iCntWishListArticles = null;
00096 
00102     protected $_iCntRecommLists = null;
00103 
00109     protected $_sUpdateKey = null;
00110 
00116     protected $_blLoadedFromCookie  = null;
00117 
00123     protected $_sSelAddressId = null;
00124 
00130     protected $_oSelAddress = null;
00131 
00137     protected $_sWishId = null;
00138 
00144     public function __construct()
00145     {
00146         $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00147 
00148         parent::__construct();
00149         $this->init( 'oxuser' );
00150     }
00151 
00159     public function setMallUsersStatus( $blOn = false )
00160     {
00161         $this->_blMallUsers = $blOn;
00162     }
00163 
00171     public function __get( $sParamName )
00172     {
00173         // it saves memory using - loads data only if it is used
00174         switch ( $sParamName ) {
00175             case 'oGroups':
00176                 return $this->_oGroups = $this->getUserGroups();
00177                 break;
00178             case 'iCntNoticeListArticles':
00179                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00180                 break;
00181             case 'iCntWishListArticles':
00182                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00183                 break;
00184             case 'iCntRecommLists':
00185                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00186                 break;
00187             case 'oAddresses':
00188                 return $this->getUserAddresses();
00189                 break;
00190             case 'oPayments':
00191                 return $this->_oPayments = $this->getUserPayments();
00192                 break;
00193             case 'oxuser__oxcountry':
00194                 return $this->oxuser__oxcountry = $this->getUserCountry();
00195                 break;
00196             case 'sDBOptin':
00197                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00198                 break;
00199             case 'sEmailFailed':
00200                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00201                 break;
00202         }
00203     }
00204 
00210     public function getNewsSubscription()
00211     {
00212         if ( $this->_oNewsSubscription !== null ) {
00213             return $this->_oNewsSubscription;
00214         }
00215 
00216         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00217 
00218         // if subscription object is not set yet - we should create one
00219         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00220             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00221 
00222                 // no subscription defined yet - creating one
00223                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00224                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00225                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00226                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00227                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00228             }
00229         }
00230 
00231         return $this->_oNewsSubscription;
00232     }
00233 
00243     public function getUserCountry( $sCountryId = null, $iLang = null )
00244     {
00245         $oDb = oxDb::getDb();
00246         if ( !$sCountryId ) {
00247             $sCountryId = $this->oxuser__oxcountryid->value;
00248         }
00249 
00250         $sViewName = getViewName( 'oxcountry', $iLang );
00251         $sQ = "select oxtitle from {$sViewName} where oxid = " . $oDb->quote( $sCountryId ) . " ";
00252         $this->oxuser__oxcountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00253 
00254         return $this->oxuser__oxcountry;
00255     }
00256 
00264     public function getUserCountryId( $sCountry = null )
00265     {
00266         $oDb = oxDb::getDb();
00267         $sQ = "select oxid from ".getviewName( "oxcountry" )." where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00268         $sCountryId = $oDb->getOne( $sQ );
00269 
00270         return $sCountryId;
00271     }
00272 
00280     public function getUserGroups( $sOXID = null )
00281     {
00282 
00283         if ( isset( $this->_oGroups ) ) {
00284             return $this->_oGroups;
00285         }
00286 
00287         if ( !$sOXID ) {
00288             $sOXID = $this->getId();
00289         }
00290 
00291         $sViewName = getViewName( "oxgroups" );
00292         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00293         $sSelect  = "select {$sViewName}.* from {$sViewName} left join oxobject2group on oxobject2group.oxgroupsid = {$sViewName}.oxid
00294                      where oxobject2group.oxobjectid = " . oxDb::getDb()->quote( $sOXID );
00295         $this->_oGroups->selectString( $sSelect );
00296         return $this->_oGroups;
00297     }
00298 
00306     public function getUserAddresses( $sUserId = null )
00307     {
00308         $sUserId = isset( $sUserId ) ? $sUserId : $this->getId();
00309         if ( !isset( $this->_aAddresses[$sUserId] ) ) {
00310             $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId );
00311 
00312             //P
00313             $this->_aAddresses[$sUserId] = oxNew( "oxlist" );
00314             $this->_aAddresses[$sUserId]->init( "oxaddress" );
00315             $this->_aAddresses[$sUserId]->selectString( $sSelect );
00316 
00317             // marking selected
00318             if ( $sAddressId = $this->getSelectedAddressId() ) {
00319                 foreach ( $this->_aAddresses[$sUserId] as $oAddress ) {
00320                     if ( $oAddress->getId() === $sAddressId ) {
00321                         $oAddress->setSelected();
00322                         break;
00323                     }
00324                 }
00325             }
00326         }
00327         return $this->_aAddresses[$sUserId];
00328     }
00329 
00337     public function setSelectedAddressId( $sAddressId )
00338     {
00339         $this->_sSelAddressId = $sAddressId;
00340     }
00341 
00347     public function getSelectedAddressId()
00348     {
00349         if ( $this->_sSelAddressId !== null ) {
00350             return $this->_sSelAddressId;
00351         }
00352 
00353         $sAddressId = oxConfig::getParameter( "oxaddressid");
00354         if ( !$sAddressId && !oxConfig::getParameter( 'reloadaddress' ) ) {
00355             $sAddressId = oxSession::getVar( "deladrid" );
00356         }
00357         return $sAddressId;
00358     }
00359 
00365     protected function _getWishListId()
00366     {
00367         $this->_sWishId = null;
00368         // check if we have to set it here
00369         $oBasket = $this->getSession()->getBasket();
00370         foreach ( $oBasket->getContents() as $oBasketItem ) {
00371             if ( $this->_sWishId = $oBasketItem->getWishId() ) {
00372                 // stop on first found
00373                 break;
00374             }
00375         }
00376         return $this->_sWishId;
00377     }
00378 
00387     public function getSelectedAddress( $sWishId = false )
00388     {
00389         if ( $this->_oSelAddress !== null ) {
00390             return $this->_oSelAddress;
00391         }
00392 
00393         $oSelectedAddress = null;
00394         $oAddresses = $this->getUserAddresses();
00395         if ( $oAddresses->count() ) {
00396             if ( $sAddressId = $this->getSelectedAddressId() ) {
00397                 foreach ( $oAddresses as $oAddress ) {
00398                     if ( $oAddress->getId() == $sAddressId ) {
00399                         $oAddress->selected = 1;                            
00400                         $oAddress->setSelected();
00401                         $oSelectedAddress = $oAddress;
00402                         break;
00403                     }
00404                 }
00405             }
00406             
00407             // in case none is set - setting first one
00408             if ( !$oSelectedAddress ) {
00409                 if ( !$sAddressId || $sAddressId >= 0 ) {
00410                     $oAddresses->rewind();
00411                     $oAddress = $oAddresses->current();
00412                 } else {
00413                     $aAddresses = $oAddresses->getArray();
00414                     $oAddress   = array_pop( $aAddresses );                    
00415                 }
00416                 $oAddress->selected = 1;
00417                 $oAddress->setSelected();
00418                 $oSelectedAddress = $oAddress;                
00419             }
00420         }
00421         $this->_oSelAddress = $oSelectedAddress;
00422         return $oSelectedAddress;
00423     }
00424 
00432     public function getUserPayments( $sOXID = null )
00433     {
00434         if ( $this->_oPayments === null ) {
00435 
00436             if ( !$sOXID ) {
00437                 $sOXID = $this->getId();
00438             }
00439 
00440             $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00441 
00442             $this->_oPayments = oxNew( 'oxlist' );
00443             $this->_oPayments->init( 'oxUserPayment' );
00444             $this->_oPayments->selectString( $sSelect );
00445 
00446             $myUtils = oxUtils::getInstance();
00447             foreach ( $this->_oPayments as $oPayment ) {
00448                 // add custom fields to this class
00449                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00450             }
00451         }
00452 
00453         return $this->_oPayments;
00454     }
00455 
00461     public function save()
00462     {
00463         $myConfig  = oxConfig::getInstance();
00464 
00465         $blAddRemark = false;
00466         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00467             $blAddRemark = true;
00468             //save oxregister value
00469             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00470         }
00471 
00472         // setting user rights
00473         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00474 
00475         // processing birth date which came from output as array
00476         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00477             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00478         }
00479 
00480         // checking if user Facebook ID should be updated
00481         if ( $myConfig->getConfigParam( "bl_showFbConnect" ) ) {
00482             $oFb = oxFb::getInstance();
00483             if ( $oFb->isConnected() && $oFb->getUser() ) {
00484                  $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
00485             }
00486         }
00487 
00488         $blRet = parent::save();
00489 
00490         //add registered remark
00491         if ( $blAddRemark && $blRet ) {
00492             $oRemark = oxNew( 'oxremark' );
00493             $oRemark->oxremark__oxtext     = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00494             $oRemark->oxremark__oxtype     = new oxField('r', oxField::T_RAW);
00495             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00496             $oRemark->save();
00497         }
00498 
00499         return $blRet;
00500     }
00501 
00507     public function allowDerivedUpdate()
00508     {
00509         return true;
00510     }
00511 
00519     public function inGroup( $sGroupID )
00520     {
00521         $blIn = false;
00522         if ( ( $oGroups = $this->getUserGroups() ) ) {
00523             $blIn = isset( $oGroups[ $sGroupID ] );
00524         }
00525 
00526         return $blIn;
00527     }
00528 
00537     public function delete( $sOXID = null )
00538     {
00539 
00540         if ( !$sOXID ) {
00541             $sOXID = $this->getId();
00542         }
00543         if ( !$sOXID ) {
00544             return false;
00545         }
00546 
00547         $blDeleted = parent::delete( $sOXID );
00548 
00549         if ( $blDeleted ) {
00550             $oDB = oxDb::getDb();
00551             $sOXIDQuoted = $oDB->quote($sOXID);
00552 
00553             // deleting stored payment, address, group dependencies, remarks info
00554             $rs = $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00555             $rs = $oDB->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00556 
00557             // deleting notice/wish lists
00558             $rs = $oDB->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00559             $rs = $oDB->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00560 
00561             // deleting newsletter subscription
00562             $rs = $oDB->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00563 
00564             // delivery and delivery sets
00565             $rs = $oDB->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00566 
00567             // discounts
00568             $rs = $oDB->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00569 
00570 
00571             // and leaving all order related information
00572             $rs = $oDB->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00573 
00574             $blDeleted = $rs->EOF;
00575         }
00576 
00577         return $blDeleted;
00578     }
00579 
00587     public function load( $oxID )
00588     {
00589 
00590         $blRet = parent::Load( $oxID );
00591 
00592         // convert date's to international format
00593         if ( isset( $this->oxuser__oxcreate->value ) ) {
00594             $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00595         }
00596 
00597         return $blRet;
00598     }
00599 
00607     public function exists( $sOXID = null )
00608     {
00609         $oDb = oxDb::getDb();
00610         if ( !$sOXID ) {
00611             $sOXID = $this->getId();
00612         }
00613 
00614         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00615                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).'';
00616 
00617         if ( $sOXID ) {
00618             $sSelect.= " or oxid = ".$oDb->quote( $sOXID ) . " ) ";
00619         } else {
00620             $sSelect.= ' ) ';
00621         }
00622 
00623         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00624             $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00625         }
00626 
00627         $blExists = false;
00628         if ( ( $sOxid = oxDb::getDb()->getOne( $sSelect ) ) ) {
00629              // update - set oxid
00630             $this->setId( $sOxid );
00631             $blExists = true;
00632         }
00633         return $blExists;
00634     }
00635 
00644     public function getOrders( $iLimit = false, $iPage = 0 )
00645     {
00646         $myConfig = $this->getConfig();
00647         $oOrders = oxNew( 'oxlist' );
00648         $oOrders->init( 'oxorder' );
00649 
00650         if ( $iLimit !== false ) {
00651             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00652         }
00653 
00654         //P
00655         // Lists does not support loading from two tables, so orders
00656         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00657         // forcing to load product info which is used in templates
00658         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00659 
00660         //loading order for registered user
00661         if ( $this->oxuser__oxregister->value > 1 ) {
00662             $sQ = 'select * from oxorder where oxuserid = "'.$this->getId().'" and oxorderdate >= ' . oxDb::getDb()->quote( $this->oxuser__oxregister->value ) . ' ';
00663 
00664             //#1546 - shopid check added, if it is not multishop
00665 
00666             $sQ .= ' order by oxorderdate desc ';
00667             $oOrders->selectString( $sQ );
00668         }
00669 
00670         return $oOrders;
00671     }
00672 
00678     public function getOrderCount()
00679     {
00680         $iCnt = 0;
00681         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00682             $oDb = oxDb::getDb();
00683             $sQ  = 'select count(*) from oxorder where oxuserid = "'.$this->getId().'" AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00684             $iCnt = (int) $oDb->getOne( $sQ );
00685         }
00686 
00687         return $iCnt;
00688     }
00689 
00695     public function getNoticeListArtCnt()
00696     {
00697         if ( $this->_iCntNoticeListArticles === null ) {
00698             $this->_iCntNoticeListArticles = 0;
00699             if ( $this->getId() ) {
00700                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00701             }
00702         }
00703         return $this->_iCntNoticeListArticles;
00704     }
00705 
00711     public function getWishListArtCnt()
00712     {
00713         if ( $this->_iCntWishListArticles === null ) {
00714             $this->_iCntWishListArticles = false;
00715             if ( $this->getId() ) {
00716                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00717             }
00718         }
00719         return $this->_iCntWishListArticles;
00720     }
00721 
00728     public function getActiveCountry()
00729     {
00730         $sDeliveryCountry = '';
00731         $soxAddressId = oxSession::getVar( 'deladrid' );
00732         if ( $soxAddressId ) {
00733             $oDelAddress = oxNew( 'oxaddress' );
00734             $oDelAddress->load( $soxAddressId );
00735             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00736         } elseif ( $this->getId() ) {
00737             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00738         } else {
00739             $oUser = oxNew( 'oxuser' );
00740             if ( $oUser->loadActiveUser() ) {
00741                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00742             }
00743         }
00744 
00745         return $sDeliveryCountry;
00746     }
00747 
00755     public function createUser()
00756     {
00757         $oDB = oxDb::getDb();
00758         $sShopID = $this->getConfig()->getShopId();
00759 
00760         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00761         $sSelect = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00762         if ( !$this->_blMallUsers ) {
00763             $sSelect .= " and oxshopid = '{$sShopID}' ";
00764         }
00765         $sOXID = $oDB->getOne( $sSelect );
00766 
00767         // user without password found - lets use
00768         if ( isset( $sOXID ) && $sOXID ) {
00769             // try to update
00770             $this->delete( $sOXID );
00771         } elseif ( $this->_blMallUsers ) {
00772             // must be sure if there is no dublicate user
00773             $sQ = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00774             if ( $oDB->getOne( $sQ ) ) {
00775                 $oEx = oxNew( 'oxUserException' );
00776                 $oLang = oxLang::getInstance();
00777                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00778                 throw $oEx;
00779             }
00780         }
00781 
00782         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00783         if ( ( $blOK = $this->save() ) ) {
00784             // dropping/cleaning old delivery address/payment info
00785             $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00786             $oDB->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00787         } else {
00788             $oEx = oxNew( 'oxUserException' );
00789             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00790             throw $oEx;
00791         }
00792 
00793         return $blOK;
00794     }
00795 
00803     public function addToGroup( $sGroupID )
00804     {
00805         if ( !$this->inGroup( $sGroupID ) ) {
00806             $oNewGroup = oxNew( 'oxobject2group' );
00807             $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00808             $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00809             if ( $oNewGroup->save() ) {
00810                 $this->_oGroups[$sGroupID] = $oNewGroup;
00811                 return true;
00812             }
00813         }
00814         return false;
00815     }
00816 
00824     public function removeFromGroup( $sGroupID = null )
00825     {
00826         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00827             $oGroups = oxNew( 'oxlist' );
00828             $oGroups->init( 'oxobject2group' );
00829             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00830             $oGroups->selectString( $sSelect );
00831             foreach ( $oGroups as $oRemgroup ) {
00832                 if ( $oRemgroup->delete() ) {
00833                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00834                 }
00835             }
00836         }
00837     }
00838 
00847     public function onOrderExecute( $oBasket, $iSuccess )
00848     {
00849 
00850         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00851             //adding user to particular customer groups
00852             if ( !$this->oxuser__oxdisableautogrp->value ) {
00853 
00854                 $myConfig = $this->getConfig();
00855                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00856                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00857 
00858                 $this->addToGroup( 'oxidcustomer' );
00859                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00860                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00861                     $this->addToGroup( 'oxidsmallcust' );
00862                 }
00863                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00864                     $this->addToGroup( 'oxidmiddlecust' );
00865                 }
00866                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00867                     $this->addToGroup( 'oxidgoodcust' );
00868                 }
00869             }
00870 
00871             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00872                 $this->removeFromGroup( 'oxidnotyetordered' );
00873             }
00874         }
00875     }
00876 
00884     public function getBasket( $sName )
00885     {
00886         if ( !isset( $this->_aBaskets[$sName] ) ) {
00887             $oBasket = oxNew( 'oxuserbasket' );
00888             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00889 
00890             // creating if it does not exist
00891             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00892                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00893                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00894 
00895                 // marking basket as new (it will not be saved in DB yet)
00896                 $oBasket->setIsNewBasket();
00897             }
00898 
00899             $this->_aBaskets[$sName] = $oBasket;
00900         }
00901 
00902         return $this->_aBaskets[$sName];
00903     }
00904 
00913     public function convertBirthday( $aData )
00914     {
00915 
00916         // preparing data to process
00917         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00918         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00919         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00920 
00921         // leaving empty if not set
00922         if ( !$iYear && !$iMonth && !$iDay )
00923             return "";
00924 
00925         // year
00926         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00927             $iYear = date('Y');
00928 
00929         // month
00930         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00931             $iMonth = 1;
00932 
00933         // maximum nuber of days in month
00934         $iMaxDays = 31;
00935         switch( $iMonth) {
00936             case 2 :
00937                 if ($iMaxDays > 28)
00938                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00939                 break;
00940             case 4  :
00941             case 6  :
00942             case 9  :
00943             case 11 :
00944                 $iMaxDays = min(30, $iMaxDays);
00945                 break;
00946         }
00947 
00948         // day
00949         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00950             $iDay = 1;
00951         }
00952 
00953         // whole date
00954         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00955     }
00956 
00963     public function getBoni()
00964     {
00965         return 1000;
00966     }
00967 
00980     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00981     {
00982         // preparing input
00983         $sDynGoup = strtolower( trim( $sDynGoup ) );
00984 
00985         // setting denied groups from admin settings also
00986         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
00987 
00988         // default state ..
00989         $blAdd = false;
00990 
00991         // user assignment to dyn group is not allowed
00992         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
00993             $blAdd = false;
00994         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
00995             // trying to add user to prohibited user group?
00996             $blAdd = false;
00997         } elseif ( $this->addToGroup( $sDynGoup ) ) {
00998             $blAdd = true;
00999         }
01000 
01001         // cleanup
01002         oxSession::deleteVar( 'dgr' );
01003 
01004         return $blAdd;
01005     }
01006 
01025     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01026     {
01027         // 1. checking user name
01028         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01029 
01030         // 2. cheking email
01031         $this->_checkEmail( $sLogin );
01032 
01033         // 3. password
01034         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01035 
01036         // 4. required fields
01037         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01038 
01039         // 5. country check
01040         $this->_checkCountries( $aInvAddress, $aDelAddress );
01041 
01042         // 6. vat id check.
01043             $this->_checkVatId( $aInvAddress );
01044 
01045 
01046         // throwing first validation error
01047         if ( $oError = oxInputValidator::getInstance()->getFirstValidationError( $this->getId() ) ) {
01048             throw $oError;
01049         }
01050     }
01051 
01060     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
01061     {
01062         // assigning to newsletter
01063         $blSuccess = false;
01064         $myConfig  = $this->getConfig();
01065         $mySession = $this->getSession();
01066 
01067         // user wants to get newsletter messages or no ?
01068         $oNewsSubscription = $this->getNewsSubscription();
01069         if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
01070             if ( !$blSendOptIn ) {
01071 
01072                 // double-opt-in check is disabled - assigning automatically
01073                 $this->addToGroup( 'oxidnewsletter' );
01074                 // and setting subscribed status
01075                 $oNewsSubscription->setOptInStatus( 1 );
01076                 $blSuccess = true;
01077             } else {
01078 
01079                 $oNewsSubscription->setOptInStatus( 2 );
01080 
01081                 // double-opt-in check enabled - sending confirmation email and setting waiting status
01082                 if ( !$mySession->getVar( "blDBOptInMailAlreadyDone" ) ) {
01083 
01084                     // sending double-opt-in mail
01085                     $oEmail = oxNew( 'oxemail' );
01086                     $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01087 
01088                     if ( $blSuccess ) {
01089                         //setting in seesion parameter to force sending email only once (#2033)
01090                         oxSession::setVar( "blDBOptInMailAlreadyDone", true );
01091                     }
01092                 } else {
01093                     // mail already was sent, so just confirming that
01094                     $blSuccess = true;
01095                 }
01096             }
01097         } elseif ( !$blSubscribe ) {
01098             // removing user from newsletter subscribers
01099             $this->removeFromGroup( 'oxidnewsletter' );
01100             $oNewsSubscription->setOptInStatus( 0 );
01101             $blSuccess = true;
01102         }
01103 
01104         return $blSuccess;
01105     }
01106 
01124     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01125     {
01126         // validating values before saving. If validation fails - exception is thrown
01127         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01128 
01129         // input data is fine - lets save updated user info
01130         $this->assign( $aInvAddress );
01131 
01132 
01133         // update old or add new delivery address
01134         $this->_assignAddress( $aDelAddress );
01135 
01136         // saving new values
01137         if ( $this->save() ) {
01138 
01139             // assigning automatically to specific groups
01140             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01141             $this->_setAutoGroups( $sCountryId );
01142         }
01143     }
01144 
01155     public function addUserAddress( $oUser )
01156     {
01157         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01158             return false;
01159         }
01160 
01161         $oAddress = oxNew( 'oxaddress' );
01162         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01163         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01164         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01165         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01166         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01167         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01168         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01169         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01170         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01171         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01172         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01173 
01174         // adding new address
01175         if ( $oAddress->save() ) {
01176             // resetting addresses
01177             $this->_aAddresses = null;
01178             return $oAddress->getId();
01179         }
01180     }
01181 
01189     protected function _assignAddress( $aDelAddress )
01190     {
01191         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01192             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01193             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01194             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01195             $oAddress = oxNew( 'oxaddress' );
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             oxSession::setVar( 'deladrid', $oAddress->getId() );
01206         } else {
01207             // resetting
01208             oxSession::setVar( 'deladrid', null );
01209         }
01210     }
01211 
01224     public function login( $sUser, $sPassword, $blCookie = false)
01225     {
01226         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01227             $oEx = oxNew( 'oxCookieException' );
01228             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01229             throw $oEx;
01230         }
01231 
01232         $myConfig = $this->getConfig();
01233         if ( $sPassword ) {
01234 
01235             $sShopID = $myConfig->getShopId();
01236             $oDb = oxDb::getDb();
01237 
01238             $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01239             $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01240             $sShopSelect = "";
01241 
01242 
01243             // admin view: can only login with higher than 'user' rights
01244             if ( $this->isAdmin() ) {
01245                 $sShopSelect = " and ( oxrights != 'user' ) ";
01246             }
01247 
01248             $sWhat = "oxid";
01249 
01250             $sSelect =  "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01251             if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
01252                 if ( $sPassword == "admin" && $sUser == "admin" ) {
01253                     $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01254                 } else {
01255                     $oEx = oxNew( 'oxUserException' );
01256                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01257                     throw $oEx;
01258                 }
01259             }
01260 
01261             // load from DB
01262             $aData = $oDb->getAll( $sSelect );
01263             $sOXID = @$aData[0][0];
01264             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01265 
01266                 if ( !$this->load( $sOXID ) ) {
01267                     $oEx = oxNew( 'oxUserException' );
01268                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01269                     throw $oEx;
01270                 }
01271             }
01272         }
01273 
01274 
01275         //login successfull?
01276         if ( $this->oxuser__oxid->value ) {
01277             // yes, successful login
01278 
01279             //reseting active user
01280             $this->setUser( null );
01281 
01282             if ( $this->isAdmin() ) {
01283                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01284             } else {
01285                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01286             }
01287 
01288             // cookie must be set ?
01289             if ( $blCookie ) {
01290                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01291             }
01292 
01293             //load basket from the database
01294             try {
01295                 if ($oBasket = $this->getSession()->getBasket()) {
01296                     $oBasket->load();
01297                 }
01298 
01299             } catch (Exception $oE) {
01300                 //just ignore it
01301             }
01302 
01303             return true;
01304         } else {
01305             $oEx = oxNew( 'oxUserException' );
01306             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01307             throw $oEx;
01308         }
01309     }
01310 
01316     public function logout()
01317     {
01318         // deleting session info
01319         oxSession::deleteVar( 'usr' );  // for front end
01320         oxSession::deleteVar( 'auth' ); // for back end
01321         oxSession::deleteVar( 'dgr' );
01322         oxSession::deleteVar( 'dynvalue' );
01323         oxSession::deleteVar( 'paymentid' );
01324         // oxSession::deleteVar( 'deladrid' );
01325 
01326         // delete cookie
01327         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01328 
01329         // unsetting global user
01330         $this->setUser( null );
01331 
01332         return true;
01333     }
01334 
01341     public function loadAdminUser()
01342     {
01343         return $this->loadActiveUser( true );
01344     }
01345 
01354     public function loadActiveUser( $blForceAdmin = false )
01355     {
01356         $myConfig = $this->getConfig();
01357 
01358         $blAdmin = $this->isAdmin() || $blForceAdmin;
01359         $oDB = oxDb::getDb();
01360 
01361         // first - checking session info
01362         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01363         $blFoundInCookie = false;
01364 
01365         //trying automatic login (by 'remember me' cookie)
01366         if ( !$sUserID && !$blAdmin ) {
01367             $sShopID = $myConfig->getShopId();
01368             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01369                 $aData = explode( '@@@', $sSet );
01370                 $sUser = $aData[0];
01371                 $sPWD  = @$aData[1];
01372 
01373                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDB->quote($sUser);
01374 
01375 
01376                 $oDB = oxDb::getDb();
01377                 $rs = $oDB->execute( $sSelect );
01378                 if ( $rs != false && $rs->recordCount() > 0 ) {
01379                     while (!$rs->EOF) {
01380                         $sTest = crypt( $rs->fields[1], 'ox' );
01381                         if ( $sTest == $sPWD ) {
01382                             // found
01383                             $sUserID = $rs->fields[0];
01384                             $blFoundInCookie = true;
01385                             break;
01386                         }
01387                         $rs->moveNext();
01388                     }
01389                 }
01390             }
01391         }
01392 
01393         // Checking if user is connected via Facebook connect.
01394         // If yes, trying to login user using user Facebook ID
01395         if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01396             $oFb = oxFb::getInstance();
01397             if ( $oFb->isConnected() && $oFb->getUser() ) {
01398                 $sUserSelect = "oxuser.oxfbid = " . $oDB->quote( $oFb->getUser() );
01399                 $sShopSelect = "";
01400 
01401 
01402                 $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01403                 $sUserID = $oDB->getOne( $sSelect );
01404             }
01405         }
01406 
01407         // checking user results
01408         if ( $sUserID ) {
01409             if ( $this->load( $sUserID ) ) {
01410                 // storing into session
01411                 if ($blAdmin) {
01412                     oxSession::setVar( 'auth', $sUserID );
01413                 } else {
01414                     oxSession::setVar( 'usr', $sUserID );
01415                 }
01416 
01417                 // marking the way user was loaded
01418                 $this->_blLoadedFromCookie = $blFoundInCookie;
01419                 return true;
01420             }
01421         } else {
01422             // no user
01423             if ($blAdmin) {
01424                 oxSession::deleteVar( 'auth' );
01425             } else {
01426                 oxSession::deleteVar( 'usr' );
01427             }
01428 
01429             return false;
01430         }
01431     }
01432 
01445     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01446     {
01447         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01448         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01449 
01450         // maybe this is LDAP user but supplied email Address instead of LDAP login
01451         $oDb = oxDb::getDb();
01452         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01453         if ( isset( $sLDAPKey) && $sLDAPKey) {
01454             $sUser = $sLDAPKey;
01455         }
01456 
01457         //$throws oxConnectionException
01458         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01459 
01460         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01461         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01462             // login successful
01463 
01464             // check if user is already in database
01465             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01466             $sOXID = $oDb->getOne( $sSelect );
01467 
01468             if ( !isset( $sOXID ) || !$sOXID ) {
01469                 // we need to create a new user
01470                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01471                 $this->setId();
01472 
01473                 // map all user data fields
01474                 foreach ( $aData as $fldname => $value) {
01475                     $sField = "oxuser__".strtolower( $fldname);
01476                     $this->$sField = new oxField( $aData[$fldname] );
01477                 }
01478 
01479                 $this->oxuser__oxactive  = new oxField( 1 );
01480                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01481                 $this->oxuser__oxldapkey = new oxField( $sUser );
01482                 $this->oxuser__oxrights  = new oxField( "user" );
01483                 $this->setPassword( "ldap user" );
01484 
01485                 $this->save();
01486             } else {
01487                 // LDAP user is already in OXID DB, load it
01488                 $this->load( $sOXID);
01489             }
01490 
01491         } else {
01492             $oEx = oxNew( 'oxUserException' );
01493             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01494             throw $oEx;
01495         }
01496     }
01497 
01504     protected function _getUserRights()
01505     {
01506         // previously user had no rights defined
01507         if ( !$this->oxuser__oxrights->value )
01508             return 'user';
01509 
01510         $oDB = oxDb::getDb();
01511         $myConfig    = $this->getConfig();
01512         $sAuthRights = null;
01513 
01514         // choosing possible user rights index
01515         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01516         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01517         if ( $sAuthUserID ) {
01518             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01519         }
01520 
01521         //preventing user rights edit for non admin
01522         $aRights = array();
01523 
01524         // selecting current users rights ...
01525         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01526             $aRights[] = $sCurrRights;
01527         }
01528         $aRights[] = 'user';
01529 
01530         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01531             return current( $aRights );
01532         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01533             $aRights[] = $sAuthRights;
01534             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01535                 return current( $aRights );
01536             }
01537         }
01538 
01539         // leaving as it was set ...
01540         return $this->oxuser__oxrights->value;
01541     }
01542 
01552     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01553     {
01554 
01555         /*if ( !$myConfig->blMallUsers ) {
01556             $sShopID = $myConfig->getShopId();
01557             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01558         }*/
01559 
01560         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01561     }
01562 
01568     protected function _insert()
01569     {
01570 
01571         // set oxcreate date
01572         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01573 
01574         if ( !isset( $this->oxuser__oxboni->value ) ) {
01575             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01576         }
01577 
01578         return parent::_insert();
01579     }
01580 
01586     protected function _update()
01587     {
01588         //V #M418: for not registered users, don't change boni during update
01589         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01590             $this->_aSkipSaveFields[] = 'oxboni';
01591         }
01592 
01593         // don't change this field
01594         $this->_aSkipSaveFields[] = 'oxcreate';
01595         if ( !$this->isAdmin() ) {
01596             $this->_aSkipSaveFields[] = 'oxcustnr';
01597             $this->_aSkipSaveFields[] = 'oxrights';
01598         }
01599 
01600         // updating subscription information
01601         if ( ( $blUpdate = parent::_update() ) ) {
01602             $this->getNewsSubscription()->updateSubscription( $this );
01603         }
01604 
01605         return $blUpdate;
01606     }
01607 
01623     protected function _checkLogin( $sLogin, $aInvAddress )
01624     {
01625         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01626         oxInputValidator::getInstance()->checkLogin( $this, $sLogin, $aInvAddress );
01627         return $sLogin;
01628     }
01629 
01637     public function checkIfEmailExists( $sEmail )
01638     {
01639         $myConfig = $this->getConfig();
01640         $oDB = oxDb::getDb();
01641         $iShopId = $myConfig->getShopId();
01642         $blExists = false;
01643 
01644         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01645         if ( ( $sOxid = $this->getId() ) ) {
01646             $sQ .= " and oxid <> '$sOxid' ";
01647         }
01648 
01649         $oRs = $oDB->execute( $sQ );
01650         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01651 
01652             if ( $this->_blMallUsers ) {
01653 
01654                 $blExists = true;
01655                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01656 
01657                     // password is not set - allow to override
01658                     $blExists = false;
01659                 }
01660             } else {
01661 
01662                 $blExists = false;
01663                 while ( !$oRs->EOF ) {
01664                     if ( $oRs->fields[1] != 'user' ) {
01665 
01666                         // exists admin with same login - must not allow
01667                         $blExists = true;
01668                         break;
01669                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01670 
01671                         // exists same login (with password) in same shop
01672                         $blExists = true;
01673                         break;
01674                     }
01675 
01676                     $oRs->moveNext();
01677                 }
01678             }
01679         }
01680         return $blExists;
01681     }
01682 
01690     public function getUserRecommLists( $sOXID = null )
01691     {
01692         if ( !$sOXID )
01693             $sOXID = $this->getId();
01694 
01695         // sets active page
01696         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01697         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01698 
01699         // load only lists which we show on screen
01700         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01701         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01702 
01703 
01704         $oRecommList = oxNew( 'oxlist' );
01705         $oRecommList->init( 'oxrecommlist' );
01706         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01707         $iShopId = $this->getConfig()->getShopId();
01708         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01709         $oRecommList->selectString( $sSelect );
01710 
01711         return $oRecommList;
01712     }
01713 
01721     public function getRecommListsCount( $sOx = null )
01722     {
01723         if ( !$sOx ) {
01724             $sOx = $this->getId();
01725         }
01726 
01727         if ( $this->_iCntRecommLists === null || $sOx ) {
01728             $oDb = oxDb::getDb();
01729             $this->_iCntRecommLists = 0;
01730             $iShopId = $this->getConfig()->getShopId();
01731             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01732             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01733         }
01734         return $this->_iCntRecommLists;
01735     }
01736 
01747     protected function _checkEmail( $sEmail )
01748     {
01749         oxInputValidator::getInstance()->checkEmail( $this, $sEmail );
01750     }
01751 
01766     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01767     {
01768         return $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01769     }
01770 
01783     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01784     {
01785         return oxInputValidator::getInstance()->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01786     }
01787 
01798     protected function _checkCountries( $aInvAddress, $aDelAddress )
01799     {
01800         oxInputValidator::getInstance()->checkCountries( $this, $aInvAddress, $aDelAddress );
01801     }
01802 
01814     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01815     {
01816         oxInputValidator::getInstance()->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01817     }
01818 
01829     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01830     {
01831         oxInputValidator::getInstance()->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01832     }
01833 
01844     protected function _checkVatId( $aInvAddress )
01845     {
01846         oxInputValidator::getInstance()->checkVatId( $this, $aInvAddress );
01847     }
01848 
01857     protected function _setAutoGroups( $sCountryId )
01858     {
01859         // assigning automatically to specific groups
01860         $blForeigner = true;
01861         $blForeignGroupExists = false;
01862         $blInlandGroupExists = false;
01863 
01864         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01865         // foreigner ?
01866         if ( is_array($aHomeCountry)) {
01867             if (in_array($sCountryId, $aHomeCountry)) {
01868                 $blForeigner = false;
01869             }
01870         } elseif ($sCountryId == $aHomeCountry) {
01871             $blForeigner = false;
01872         }
01873 
01874         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01875             $blForeignGroupExists = true;
01876             if ( !$blForeigner ) {
01877                 $this->removeFromGroup( 'oxidforeigncustomer' );
01878             }
01879         }
01880 
01881         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01882             $blInlandGroupExists = true;
01883             if ( $blForeigner ) {
01884                 $this->removeFromGroup( 'oxidnewcustomer' );
01885             }
01886         }
01887 
01888         if ( !$this->oxuser__oxdisableautogrp->value ) {
01889             if ( !$blForeignGroupExists && $blForeigner ) {
01890                 $this->addToGroup( 'oxidforeigncustomer' );
01891             }
01892             if ( !$blInlandGroupExists && !$blForeigner ) {
01893                 $this->addToGroup( 'oxidnewcustomer' );
01894             }
01895         }
01896     }
01897 
01906     protected function _hasUserAddress( $sUserId )
01907     {
01908         $oAddresses = $this->getUserAddresses();
01909         if ( $oAddresses && count($oAddresses)>0 ) {
01910             $oAddresses->rewind() ;
01911             foreach ($oAddresses as $key => $oAddress) {
01912                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01913                     return true;
01914                 }
01915             }
01916         }
01917         return false;
01918     }
01919 
01932     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
01933     {
01934         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
01935     }
01936 
01946     protected function _deleteUserCookie( $sShopId = null )
01947     {
01948         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
01949     }
01950 
01960     protected static function _getUserCookie( $sShopId = null )
01961     {
01962         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
01963     }
01964 
01965 
01974     public function loadUserByUpdateId( $sUid )
01975     {
01976         $oDb = oxDb::getDb();
01977         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
01978         if ( $sUserId = $oDb->getOne( $sQ ) ) {
01979             return $this->load( $sUserId );
01980         }
01981     }
01982 
01990     public function setUpdateKey( $blReset = false )
01991     {
01992         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
01993         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
01994 
01995         // generating key
01996         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
01997 
01998         // setting expiration time for 6 hours
01999         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02000 
02001         // saving
02002         $this->save();
02003     }
02004 
02010     public function getUpdateLinkTerm()
02011     {
02012         return 3600 * 6;
02013     }
02014 
02022     public function isExpiredUpdateId( $sKey )
02023     {
02024         $oDb = oxDb::getDb();
02025         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02026         return !( (bool) $oDb->getOne( $sQ ) );
02027     }
02028 
02034     public function getUpdateId()
02035     {
02036         if ( $this->_sUpdateKey === null ) {
02037             $this->setUpdateKey();
02038             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02039         }
02040         return $this->_sUpdateKey;
02041     }
02042 
02051     public function encodePassword( $sPassword, $sSalt )
02052     {
02053         $oDb = oxDb::getDb();
02054         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02055     }
02056 
02064     public function prepareSalt( $sSalt )
02065     {
02066         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02067     }
02068 
02076     public function decodeSalt( $sSaltHex )
02077     {
02078         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02079     }
02080 
02088     public function setPassword( $sPassword = null )
02089     {
02090         // setting salt if password is not empty
02091         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02092 
02093         // encoding only if password was not empty (e.g. user registration without pass)
02094         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02095 
02096         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02097         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02098     }
02099 
02107     public function isSamePassword( $sNewPass )
02108     {
02109         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02110     }
02111 
02117     public function isLoadedFromCookie()
02118     {
02119         return $this->_blLoadedFromCookie;
02120     }
02121 
02128     public function getPasswordHash()
02129     {
02130         $sHash = null;
02131         if ( $this->oxuser__oxpassword->value ) {
02132             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02133                 // decodable pass ?
02134                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02135             } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02136                 // plain pass ?
02137                 $this->setPassword( $this->oxuser__oxpassword->value );
02138             }
02139             $sHash = $this->oxuser__oxpassword->value;
02140         }
02141         return $sHash;
02142     }
02143 
02152     public static function getAdminUser()
02153     {
02154         return self::getActiveUser( true );
02155     }
02156 
02167     public static function getActiveUser( $blForceAdmin = false )
02168     {
02169         $oUser = oxNew( 'oxuser' );
02170         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02171             return $oUser;
02172         } else {
02173             return false;
02174         }
02175     }
02176 
02184     public function getReviewUserHash( $sUserId )
02185     {
02186         $oDb = oxDb::getDb();
02187         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02188         return $sReviewUserHash;
02189     }
02190 
02198     public function getReviewUserId( $sReviewUserHash )
02199     {
02200         $oDb = oxDb::getDb();
02201         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02202         return $sUserId;
02203     }
02204 
02210     public function getState()
02211     {
02212         return $this->oxuser__oxstateid->value;
02213     }
02214 
02220     public function isTermsAccepted()
02221     {
02222         $sShopId = $this->getConfig()->getShopId();
02223         $sUserId = $this->getId();
02224         return (bool) oxDb::getDb()->getOne( "select 1 from oxacceptedterms where oxuserid='{$sUserId}' and oxshopid='{$sShopId}'" );
02225     }
02226 
02232     public function acceptTerms()
02233     {
02234         $sUserId  = $this->getId();
02235         $sShopId  = $this->getConfig()->getShopId();
02236         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02237 
02238         oxDb::getDb()->execute( "replace oxacceptedterms set oxuserid='{$sUserId}', oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02239     }
02240 
02249     public function setCreditPointsForRegistrant( $sUserId )
02250     {
02251         $blSet   = false;
02252         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02253         if ( $iPoints ) {
02254             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02255             if ( $blSet = $this->save() ) {
02256                 $oDb = oxDb::getDb();
02257 
02258                 // updating users statistics
02259                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ". $oDb->quote( $sUserId ) );
02260 
02261                 $oInvUser = oxNew( "oxuser" );
02262                 if ( $oInvUser->load( $sUserId ) ) {
02263                     $blSet = $oInvUser->setCreditPointsForInviter();
02264                 }
02265             }
02266 
02267             oxSession::deleteVar( 'su' );
02268         }
02269 
02270         return $blSet;
02271     }
02272 
02278     public function setCreditPointsForInviter()
02279     {
02280         $blSet   = false;
02281         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02282         if ( $iPoints ) {
02283             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02284             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02285             $blSet = $this->save();
02286         }
02287 
02288         return $blSet;
02289     }
02290 
02296     public function updateFbId()
02297     {
02298         $oFb = oxFb::getInstance();
02299         $blRet = false;
02300 
02301         if ( $oFb->isConnected() && $oFb->getUser() ) {
02302              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02303              $blRet = $this->save();
02304         }
02305 
02306         return $blRet;
02307     }
02308 }