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 $_sClassName = 'oxuser';
00027 
00033     protected $_aBaskets = array();
00034 
00040     protected $_oGroups;
00041 
00047     protected $_aAddresses = array();
00048 
00054     protected $_oPayments;
00055 
00061     protected $_oRecommList;
00062 
00068     protected $_blMallUsers = false;
00069 
00075     protected static $_aUserCookie = array();
00076 
00082     protected $_iCntNoticeListArticles = null;
00083 
00089     protected $_iCntWishListArticles = null;
00090 
00096     protected $_iCntRecommLists = null;
00097 
00103     protected $_sUpdateKey = null;
00104 
00110     protected $_blLoadedFromCookie  = null;
00111 
00117     protected $_sSelAddressId = null;
00118 
00124     protected $_oSelAddress = null;
00125 
00131     protected $_sWishId = null;
00132 
00138     protected $_oUserCountryTitle = null;
00139 
00145     public function __construct()
00146     {
00147         $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00148 
00149         parent::__construct();
00150         $this->init( 'oxuser' );
00151     }
00152 
00160     public function setMallUsersStatus( $blOn = false )
00161     {
00162         $this->_blMallUsers = $blOn;
00163     }
00164 
00172     public function __get( $sParamName )
00173     {
00174         // it saves memory using - loads data only if it is used
00175         switch ( $sParamName ) {
00176             case 'oGroups':
00177                 return $this->_oGroups = $this->getUserGroups();
00178                 break;
00179             case 'iCntNoticeListArticles':
00180                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00181                 break;
00182             case 'iCntWishListArticles':
00183                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00184                 break;
00185             case 'iCntRecommLists':
00186                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00187                 break;
00188             case 'oAddresses':
00189                 return $this->getUserAddresses();
00190                 break;
00191             case 'oPayments':
00192                 return $this->_oPayments = $this->getUserPayments();
00193                 break;
00194             case 'oxuser__oxcountry':
00195                 return $this->oxuser__oxcountry = $this->getUserCountry();
00196                 break;
00197             case 'sDBOptin':
00198                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00199                 break;
00200             case 'sEmailFailed':
00201                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00202                 break;
00203         }
00204     }
00205 
00211     public function getNewsSubscription()
00212     {
00213         if ( $this->_oNewsSubscription !== null ) {
00214             return $this->_oNewsSubscription;
00215         }
00216 
00217         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00218 
00219         // if subscription object is not set yet - we should create one
00220         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00221             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00222 
00223                 // no subscription defined yet - creating one
00224                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00225                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00226                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00227                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00228                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00229             }
00230         }
00231 
00232         return $this->_oNewsSubscription;
00233     }
00234 
00244     public function getUserCountry( $sCountryId = null, $iLang = null )
00245     {
00246         if ( $this->_oUserCountryTitle == null || $sCountryId ) {
00247             $sId = $sCountryId ? $sCountryId : $this->oxuser__oxcountryid->value;
00248             $oDb = oxDb::getDb();
00249             $sViewName = getViewName( 'oxcountry', $iLang );
00250             $sQ = "select oxtitle from {$sViewName} where oxid = " . $oDb->quote( $sId ) . " ";
00251             $oCountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00252             if ( !$sCountryId ) {
00253                 $this->_oUserCountryTitle = $oCountry;
00254             }
00255         } else {
00256             return $this->_oUserCountryTitle;
00257         }
00258 
00259         return $oCountry;
00260     }
00261 
00269     public function getUserCountryId( $sCountry = null )
00270     {
00271         $oDb = oxDb::getDb();
00272         $sQ = "select oxid from ".getviewName( "oxcountry" )." where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00273         $sCountryId = $oDb->getOne( $sQ );
00274 
00275         return $sCountryId;
00276     }
00277 
00285     public function getUserGroups( $sOXID = null )
00286     {
00287 
00288         if ( isset( $this->_oGroups ) ) {
00289             return $this->_oGroups;
00290         }
00291 
00292         if ( !$sOXID ) {
00293             $sOXID = $this->getId();
00294         }
00295 
00296         $sViewName = getViewName( "oxgroups" );
00297         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00298         $sSelect  = "select {$sViewName}.* from {$sViewName} left join oxobject2group on oxobject2group.oxgroupsid = {$sViewName}.oxid
00299                      where oxobject2group.oxobjectid = " . oxDb::getDb()->quote( $sOXID );
00300         $this->_oGroups->selectString( $sSelect );
00301         return $this->_oGroups;
00302     }
00303 
00311     public function getUserAddresses( $sUserId = null )
00312     {
00313         $sUserId = isset( $sUserId ) ? $sUserId : $this->getId();
00314         if ( !isset( $this->_aAddresses[$sUserId] ) ) {
00315             $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId );
00316 
00317             //P
00318             $this->_aAddresses[$sUserId] = oxNew( "oxlist" );
00319             $this->_aAddresses[$sUserId]->init( "oxaddress" );
00320             $this->_aAddresses[$sUserId]->selectString( $sSelect );
00321 
00322             // marking selected
00323             if ( $sAddressId = $this->getSelectedAddressId() ) {
00324                 foreach ( $this->_aAddresses[$sUserId] as $oAddress ) {
00325                     if ( $oAddress->getId() === $sAddressId ) {
00326                         $oAddress->setSelected();
00327                         break;
00328                     }
00329                 }
00330             }
00331         }
00332         return $this->_aAddresses[$sUserId];
00333     }
00334 
00342     public function setSelectedAddressId( $sAddressId )
00343     {
00344         $this->_sSelAddressId = $sAddressId;
00345     }
00346 
00352     public function getSelectedAddressId()
00353     {
00354         if ( $this->_sSelAddressId !== null ) {
00355             return $this->_sSelAddressId;
00356         }
00357 
00358         $sAddressId = oxConfig::getParameter( "oxaddressid");
00359         if ( !$sAddressId && !oxConfig::getParameter( 'reloadaddress' ) ) {
00360             $sAddressId = oxSession::getVar( "deladrid" );
00361         }
00362         return $sAddressId;
00363     }
00364 
00370     protected function _getWishListId()
00371     {
00372         $this->_sWishId = null;
00373         // check if we have to set it here
00374         $oBasket = $this->getSession()->getBasket();
00375         foreach ( $oBasket->getContents() as $oBasketItem ) {
00376             if ( $this->_sWishId = $oBasketItem->getWishId() ) {
00377                 // stop on first found
00378                 break;
00379             }
00380         }
00381         return $this->_sWishId;
00382     }
00383 
00392     public function getSelectedAddress( $sWishId = false )
00393     {
00394         if ( $this->_oSelAddress !== null ) {
00395             return $this->_oSelAddress;
00396         }
00397 
00398         $oSelectedAddress = null;
00399         $oAddresses = $this->getUserAddresses();
00400         if ( $oAddresses->count() ) {
00401             if ( $sAddressId = $this->getSelectedAddressId() ) {
00402                 foreach ( $oAddresses as $oAddress ) {
00403                     if ( $oAddress->getId() == $sAddressId ) {
00404                         $oAddress->selected = 1;
00405                         $oAddress->setSelected();
00406                         $oSelectedAddress = $oAddress;
00407                         break;
00408                     }
00409                 }
00410             }
00411 
00412             // in case none is set - setting first one
00413             if ( !$oSelectedAddress ) {
00414                 if ( !$sAddressId || $sAddressId >= 0 ) {
00415                     $oAddresses->rewind();
00416                     $oAddress = $oAddresses->current();
00417                 } else {
00418                     $aAddresses = $oAddresses->getArray();
00419                     $oAddress   = array_pop( $aAddresses );
00420                 }
00421                 $oAddress->selected = 1;
00422                 $oAddress->setSelected();
00423                 $oSelectedAddress = $oAddress;
00424             }
00425         }
00426         $this->_oSelAddress = $oSelectedAddress;
00427         return $oSelectedAddress;
00428     }
00429 
00437     public function getUserPayments( $sOXID = null )
00438     {
00439         if ( $this->_oPayments === null ) {
00440 
00441             if ( !$sOXID ) {
00442                 $sOXID = $this->getId();
00443             }
00444 
00445             $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00446 
00447             $this->_oPayments = oxNew( 'oxlist' );
00448             $this->_oPayments->init( 'oxUserPayment' );
00449             $this->_oPayments->selectString( $sSelect );
00450 
00451             $myUtils = oxUtils::getInstance();
00452             foreach ( $this->_oPayments as $oPayment ) {
00453                 // add custom fields to this class
00454                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00455             }
00456         }
00457 
00458         return $this->_oPayments;
00459     }
00460 
00466     public function save()
00467     {
00468         $myConfig  = oxConfig::getInstance();
00469 
00470         $blAddRemark = false;
00471         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00472             $blAddRemark = true;
00473             //save oxregister value
00474             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00475         }
00476 
00477         // setting user rights
00478         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00479 
00480         // processing birth date which came from output as array
00481         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00482             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00483         }
00484 
00485         // checking if user Facebook ID should be updated
00486         if ( $myConfig->getConfigParam( "bl_showFbConnect" ) ) {
00487             $oFb = oxFb::getInstance();
00488             if ( $oFb->isConnected() && $oFb->getUser() ) {
00489                  $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
00490             }
00491         }
00492 
00493         $blRet = parent::save();
00494 
00495         //add registered remark
00496         if ( $blAddRemark && $blRet ) {
00497             $oRemark = oxNew( 'oxremark' );
00498             $oRemark->oxremark__oxtext     = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00499             $oRemark->oxremark__oxtype     = new oxField('r', oxField::T_RAW);
00500             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00501             $oRemark->save();
00502         }
00503 
00504         return $blRet;
00505     }
00506 
00512     public function allowDerivedUpdate()
00513     {
00514         return true;
00515     }
00516 
00524     public function inGroup( $sGroupID )
00525     {
00526         $blIn = false;
00527         if ( ( $oGroups = $this->getUserGroups() ) ) {
00528             $blIn = isset( $oGroups[ $sGroupID ] );
00529         }
00530 
00531         return $blIn;
00532     }
00533 
00542     public function delete( $sOXID = null )
00543     {
00544 
00545         if ( !$sOXID ) {
00546             $sOXID = $this->getId();
00547         }
00548         if ( !$sOXID ) {
00549             return false;
00550         }
00551 
00552         $blDeleted = parent::delete( $sOXID );
00553 
00554         if ( $blDeleted ) {
00555             $oDb = oxDb::getDb();
00556             $sOXIDQuoted = $oDb->quote($sOXID);
00557 
00558             // deleting stored payment, address, group dependencies, remarks info
00559             $rs = $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00560             $rs = $oDb->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00561 
00562             // deleting notice/wish lists
00563             $rs = $oDb->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00564             $rs = $oDb->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00565 
00566             // deleting newsletter subscription
00567             $rs = $oDb->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00568 
00569             // delivery and delivery sets
00570             $rs = $oDb->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00571 
00572             // discounts
00573             $rs = $oDb->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00574 
00575 
00576             // and leaving all order related information
00577             $rs = $oDb->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00578 
00579             $blDeleted = $rs->EOF;
00580         }
00581 
00582         return $blDeleted;
00583     }
00584 
00592     public function load( $oxID )
00593     {
00594 
00595         $blRet = parent::Load( $oxID );
00596 
00597         // convert date's to international format
00598         if ( isset( $this->oxuser__oxcreate->value ) ) {
00599             $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00600         }
00601 
00602         return $blRet;
00603     }
00604 
00612     public function exists( $sOXID = null )
00613     {
00614         $oDb = oxDb::getDb();
00615         if ( !$sOXID ) {
00616             $sOXID = $this->getId();
00617         }
00618 
00619         $sShopSelect = '';
00620         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00621             $sShopSelect = ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00622         }
00623 
00624         //#4543 Query optimisation by splitting it into two, might need an logics optimisation as well
00625         if ( $sOXID ) {
00626             $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00627                     WHERE ( oxid = '.$oDb->quote( $sOXID ).' ) ';
00628             $sSelect .= $sShopSelect;
00629 
00630             if ( ( $sOxid = $oDb->getOne( $sSelect ) ) ) {
00631                 // update - set oxid
00632                 $this->setId( $sOxid );
00633                 return true;
00634             }
00635         }
00636 
00637         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00638                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).' ) ';
00639         $sSelect .= $sShopSelect;
00640 
00641         if ( ( $sOxid = $oDb->getOne( $sSelect ) ) ) {
00642              // update - set oxid
00643             $this->setId( $sOxid );
00644             return true;
00645         }
00646         return false;
00647     }
00648 
00657     public function getOrders( $iLimit = false, $iPage = 0 )
00658     {
00659         $myConfig = $this->getConfig();
00660         $oOrders = oxNew( 'oxlist' );
00661         $oOrders->init( 'oxorder' );
00662 
00663         if ( $iLimit !== false ) {
00664             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00665         }
00666 
00667         //P
00668         // Lists does not support loading from two tables, so orders
00669         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00670         // forcing to load product info which is used in templates
00671         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00672 
00673         //loading order for registered user
00674         if ( $this->oxuser__oxregister->value > 1 ) {
00675             $oDb = oxDb::getDb();
00676             $sQ = 'select * from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' and oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value ) . ' ';
00677 
00678             //#1546 - shopid check added, if it is not multishop
00679 
00680             $sQ .= ' order by oxorderdate desc ';
00681             $oOrders->selectString( $sQ );
00682         }
00683 
00684         return $oOrders;
00685     }
00686 
00692     public function getOrderCount()
00693     {
00694         $iCnt = 0;
00695         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00696             $oDb = oxDb::getDb();
00697             $sQ  = 'select count(*) from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00698             $iCnt = (int) $oDb->getOne( $sQ );
00699         }
00700 
00701         return $iCnt;
00702     }
00703 
00709     public function getNoticeListArtCnt()
00710     {
00711         if ( $this->_iCntNoticeListArticles === null ) {
00712             $this->_iCntNoticeListArticles = 0;
00713             if ( $this->getId() ) {
00714                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00715             }
00716         }
00717         return $this->_iCntNoticeListArticles;
00718     }
00719 
00725     public function getWishListArtCnt()
00726     {
00727         if ( $this->_iCntWishListArticles === null ) {
00728             $this->_iCntWishListArticles = false;
00729             if ( $this->getId() ) {
00730                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00731             }
00732         }
00733         return $this->_iCntWishListArticles;
00734     }
00735 
00742     public function getActiveCountry()
00743     {
00744         $sDeliveryCountry = '';
00745         $soxAddressId = oxSession::getVar( 'deladrid' );
00746         if ( $soxAddressId ) {
00747             $oDelAddress = oxNew( 'oxaddress' );
00748             $oDelAddress->load( $soxAddressId );
00749             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00750         } elseif ( $this->getId() ) {
00751             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00752         } else {
00753             $oUser = oxNew( 'oxuser' );
00754             if ( $oUser->loadActiveUser() ) {
00755                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00756             }
00757         }
00758 
00759         return $sDeliveryCountry;
00760     }
00761 
00769     public function createUser()
00770     {
00771         $oDb = oxDb::getDb();
00772         $sShopID = $this->getConfig()->getShopId();
00773 
00774         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00775         $sSelect = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00776         if ( !$this->_blMallUsers ) {
00777             $sSelect .= " and oxshopid = '{$sShopID}' ";
00778         }
00779         $sOXID = $oDb->getOne( $sSelect, false, false );
00780 
00781         // user without password found - lets use
00782         if ( isset( $sOXID ) && $sOXID ) {
00783             // try to update
00784             $this->delete( $sOXID );
00785         } elseif ( $this->_blMallUsers ) {
00786             // must be sure if there is no dublicate user
00787             $sQ = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00788             if ( $oDb->getOne( $sQ, false, false ) ) {
00789                 $oEx = oxNew( 'oxUserException' );
00790                 $oLang = oxLang::getInstance();
00791                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00792                 throw $oEx;
00793             }
00794         }
00795 
00796         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00797         if ( ( $blOK = $this->save() ) ) {
00798             // dropping/cleaning old delivery address/payment info
00799             $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00800             $oDb->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00801         } else {
00802             $oEx = oxNew( 'oxUserException' );
00803             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00804             throw $oEx;
00805         }
00806 
00807         return $blOK;
00808     }
00809 
00817     public function addToGroup( $sGroupID )
00818     {
00819         if ( !$this->inGroup( $sGroupID ) ) {
00820             // create oxgroup object
00821             $oGroup = oxNew('oxGroups');
00822             if ( $oGroup->load($sGroupID) ) {
00823                 $oNewGroup = oxNew( 'oxobject2group' );
00824                 $oNewGroup->oxobject2group__oxobjectid = new oxField( $this->getId(), oxField::T_RAW );
00825                 $oNewGroup->oxobject2group__oxgroupsid = new oxField( $sGroupID, oxField::T_RAW );
00826                 if ( $oNewGroup->save() ) {
00827                     $this->_oGroups[$sGroupID] = $oGroup;
00828                     return true;
00829                 }
00830             }
00831         }
00832         return false;
00833     }
00834 
00842     public function removeFromGroup( $sGroupID = null )
00843     {
00844         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00845             $oGroups = oxNew( 'oxlist' );
00846             $oGroups->init( 'oxobject2group' );
00847             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00848             $oGroups->selectString( $sSelect );
00849             foreach ( $oGroups as $oRemgroup ) {
00850                 if ( $oRemgroup->delete() ) {
00851                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00852                 }
00853             }
00854         }
00855     }
00856 
00865     public function onOrderExecute( $oBasket, $iSuccess )
00866     {
00867 
00868         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00869             //adding user to particular customer groups
00870             if ( !$this->oxuser__oxdisableautogrp->value ) {
00871 
00872                 $myConfig = $this->getConfig();
00873                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00874                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00875 
00876                 $this->addToGroup( 'oxidcustomer' );
00877                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00878                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00879                     $this->addToGroup( 'oxidsmallcust' );
00880                 }
00881                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00882                     $this->addToGroup( 'oxidmiddlecust' );
00883                 }
00884                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00885                     $this->addToGroup( 'oxidgoodcust' );
00886                 }
00887             }
00888 
00889             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00890                 $this->removeFromGroup( 'oxidnotyetordered' );
00891             }
00892         }
00893     }
00894 
00902     public function getBasket( $sName )
00903     {
00904         if ( !isset( $this->_aBaskets[$sName] ) ) {
00905             $oBasket = oxNew( 'oxuserbasket' );
00906             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00907 
00908             // creating if it does not exist
00909             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00910                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00911                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00912 
00913                 // marking basket as new (it will not be saved in DB yet)
00914                 $oBasket->setIsNewBasket();
00915             }
00916 
00917             $this->_aBaskets[$sName] = $oBasket;
00918         }
00919 
00920         return $this->_aBaskets[$sName];
00921     }
00922 
00931     public function convertBirthday( $aData )
00932     {
00933 
00934         // preparing data to process
00935         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00936         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00937         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00938 
00939         // leaving empty if not set
00940         if ( !$iYear && !$iMonth && !$iDay )
00941             return "";
00942 
00943         // year
00944         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00945             $iYear = date('Y');
00946 
00947         // month
00948         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00949             $iMonth = 1;
00950 
00951         // maximum nuber of days in month
00952         $iMaxDays = 31;
00953         switch( $iMonth) {
00954             case 2 :
00955                 if ($iMaxDays > 28)
00956                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00957                 break;
00958             case 4  :
00959             case 6  :
00960             case 9  :
00961             case 11 :
00962                 $iMaxDays = min(30, $iMaxDays);
00963                 break;
00964         }
00965 
00966         // day
00967         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00968             $iDay = 1;
00969         }
00970 
00971         // whole date
00972         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00973     }
00974 
00981     public function getBoni()
00982     {
00983         return 1000;
00984     }
00985 
00998     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00999     {
01000         // preparing input
01001         $sDynGoup = strtolower( trim( $sDynGoup ) );
01002 
01003         // setting denied groups from admin settings also
01004         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
01005 
01006         // default state ..
01007         $blAdd = false;
01008 
01009         // user assignment to dyn group is not allowed
01010         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
01011             $blAdd = false;
01012         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
01013             // trying to add user to prohibited user group?
01014             $blAdd = false;
01015         } elseif ( $this->addToGroup( $sDynGoup ) ) {
01016             $blAdd = true;
01017         }
01018 
01019         // cleanup
01020         oxSession::deleteVar( 'dgr' );
01021 
01022         return $blAdd;
01023     }
01024 
01043     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01044     {
01045         // 1. checking user name
01046         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01047 
01048         // 2. cheking email
01049         $this->_checkEmail( $sLogin );
01050 
01051         // 3. password
01052         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01053 
01054         // 4. required fields
01055         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01056 
01057         // 5. country check
01058         $this->_checkCountries( $aInvAddress, $aDelAddress );
01059 
01060         // 6. vat id check.
01061             $this->_checkVatId( $aInvAddress );
01062 
01063 
01064         // throwing first validation error
01065         if ( $oError = oxInputValidator::getInstance()->getFirstValidationError() ) {
01066             throw $oError;
01067         }
01068     }
01069 
01078     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
01079     {
01080         // assigning to newsletter
01081         $blSuccess = false;
01082         $myConfig  = $this->getConfig();
01083         $mySession = $this->getSession();
01084 
01085         // user wants to get newsletter messages or no ?
01086         $oNewsSubscription = $this->getNewsSubscription();
01087         if ( $oNewsSubscription ) {
01088             if ( $blSubscribe && ( $iOptInStatus = $oNewsSubscription->getOptInStatus() ) != 1 ) {
01089                 if ( !$blSendOptIn ) {
01090 
01091                     // double-opt-in check is disabled - assigning automatically
01092                     $this->addToGroup( 'oxidnewsletter' );
01093                     // and setting subscribed status
01094                     $oNewsSubscription->setOptInStatus( 1 );
01095                     $blSuccess = true;
01096                 } else {
01097 
01098                     // double-opt-in check enabled - sending confirmation email and setting waiting status
01099                     if ( $iOptInStatus != 2 ) {
01100                         // sending double-opt-in mail
01101                         $oEmail = oxNew( 'oxemail' );
01102                         $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01103                     } else {
01104                         // mail already was sent, so just confirming that
01105                         $blSuccess = true;
01106                     }
01107 
01108                     $oNewsSubscription->setOptInStatus( 2 );
01109                 }
01110             } elseif ( !$blSubscribe ) {
01111                 // removing user from newsletter subscribers
01112                 $this->removeFromGroup( 'oxidnewsletter' );
01113                 $oNewsSubscription->setOptInStatus( 0 );
01114                 $blSuccess = true;
01115             }
01116         }
01117 
01118         return $blSuccess;
01119     }
01120 
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 
01143         // input data is fine - lets save updated user info
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 
01169     public function addUserAddress( $oUser )
01170     {
01171         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01172             return false;
01173         }
01174 
01175         $oAddress = oxNew( 'oxaddress' );
01176         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01177         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01178         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01179         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01180         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01181         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01182         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01183         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01184         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01185         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01186         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01187 
01188         // adding new address
01189         if ( $oAddress->save() ) {
01190             // resetting addresses
01191             $this->_aAddresses = null;
01192             return $oAddress->getId();
01193         }
01194     }
01195 
01203     protected function _assignAddress( $aDelAddress )
01204     {
01205         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01206             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01207             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01208             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01209             $oAddress = oxNew( 'oxaddress' );
01210             $oAddress->assign( $aDelAddress );
01211             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01212             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01213             $oAddress->save();
01214 
01215             // resetting addresses
01216             $this->_aAddresses = null;
01217 
01218             // saving delivery Address for later use
01219             oxSession::setVar( 'deladrid', $oAddress->getId() );
01220         } else {
01221             // resetting
01222             oxSession::setVar( 'deladrid', null );
01223         }
01224     }
01225 
01236     protected function _getLoginQuery( $sUser, $sPassword, $sShopID, $blAdmin )
01237     {
01238         $myConfig = $this->getConfig();
01239         $oDb = oxDb::getDb();
01240 
01241         $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01242         $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01243         
01244         $sShopSelect = $this->_getShopSelect( $myConfig, $sShopID, $blAdmin );
01245 
01246         $blStagingMode = false;
01247         $blDemoMode = false;
01248         $sWhat = "oxid";
01249             if ($myConfig->isDemoShop()) {
01250                 $blDemoMode = true;
01251             }
01252 
01253         $sSelect = "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01254         if ( ( $blDemoMode || $blStagingMode ) && $blAdmin ) {
01255             if ( $sPassword == "admin" && $sUser == "admin" ) {
01256                 $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' ";
01257             } elseif ( $blDemoMode ) {
01258                 $oEx = oxNew( 'oxUserException' );
01259                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01260                 throw $oEx;
01261             }
01262         }
01263 
01264         return $sSelect;
01265     }
01266 
01276     protected function _getShopSelect( $myConfig, $sShopID, $blAdmin )
01277     {
01278         $sShopSelect = "";
01279         // admin view: can only login with higher than 'user' rights
01280         if ( $blAdmin ) {
01281             $sShopSelect = " and ( oxrights != 'user' ) ";
01282         }
01283         
01284         return $sShopSelect;
01285     }
01293     protected function _loadSavedUserBasketAfterLogin()
01294     {
01295         if ( !$this->isAdmin() && !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' )) {
01296             //load basket from the database
01297             try {
01298                 if ( $oBasket = $this->getSession()->getBasket() ) {
01299                     $oBasket->load();
01300                 }
01301             } catch ( Exception $oE ) {
01302                 //just ignore it
01303             }
01304         }
01305     }
01306     
01307 
01320     public function login( $sUser, $sPassword, $blCookie = false)
01321     {
01322         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01323             $oEx = oxNew( 'oxCookieException' );
01324             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01325             throw $oEx;
01326         }
01327 
01328         $myConfig = $this->getConfig();
01329         if ( $sPassword ) {
01330 
01331             $sShopID = $myConfig->getShopId();
01332             $sSelect = $this->_getLoginQuery( $sUser, $sPassword, $sShopID, $this->isAdmin() );
01333 
01334             // load from DB
01335             $aData = oxDb::getDb()->getAll( $sSelect );
01336             $sOXID = @$aData[0][0];
01337             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01338 
01339                 if ( !$this->load( $sOXID ) ) {
01340                     $oEx = oxNew( 'oxUserException' );
01341                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01342                     throw $oEx;
01343                 }
01344             }
01345         }
01346 
01347 
01348         //login successfull?
01349         if ( $this->oxuser__oxid->value ) {
01350             // yes, successful login
01351 
01352             //reseting active user
01353             $this->setUser( null );
01354 
01355             if ( $this->isAdmin() ) {
01356                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01357             } else {
01358                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01359             }
01360 
01361             // cookie must be set ?
01362             if ( $blCookie && $myConfig->getConfigParam( 'blShowRememberMe' ) ) {
01363                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value );
01364             }
01365 
01366             //load basket from the database
01367             $this->_loadSavedUserBasketAfterLogin();
01368 
01369             return true;
01370         } else {
01371             $oEx = oxNew( 'oxUserException' );
01372             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01373             throw $oEx;
01374         }
01375     }
01376 
01382     public function logout()
01383     {
01384         // deleting session info
01385         oxSession::deleteVar( 'usr' );  // for front end
01386         oxSession::deleteVar( 'auth' ); // for back end
01387         oxSession::deleteVar( 'dgr' );
01388         oxSession::deleteVar( 'dynvalue' );
01389         oxSession::deleteVar( 'paymentid' );
01390         // oxSession::deleteVar( 'deladrid' );
01391 
01392         // delete cookie
01393         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01394 
01395         // unsetting global user
01396         $this->setUser( null );
01397 
01398         return true;
01399     }
01400 
01407     public function loadAdminUser()
01408     {
01409         return $this->loadActiveUser( true );
01410     }
01411 
01420     public function loadActiveUser( $blForceAdmin = false )
01421     {
01422         $myConfig = $this->getConfig();
01423 
01424         $blAdmin = $this->isAdmin() || $blForceAdmin;
01425         $oDb = oxDb::getDb();
01426 
01427         // first - checking session info
01428         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01429         $blFoundInCookie = false;
01430 
01431         //trying automatic login (by 'remember me' cookie)
01432         if ( !$sUserID && !$blAdmin && $myConfig->getConfigParam('blShowRememberMe') ) {
01433             $sShopID = $myConfig->getShopId();
01434             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01435                 $aData = explode( '@@@', $sSet );
01436                 $sUser = $aData[0];
01437                 $sPWD  = @$aData[1];
01438 
01439                 $sSelect =  'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDb->quote($sUser);
01440 
01441                 $rs = $oDb->select( $sSelect );
01442                 if ( $rs != false && $rs->recordCount() > 0 ) {
01443                     while (!$rs->EOF) {
01444                         $sTest = crypt( $rs->fields[1], $rs->fields[2] );
01445                         if ( $sTest == $sPWD ) {
01446                             // found
01447                             $sUserID = $rs->fields[0];
01448                             $blFoundInCookie = true;
01449                             break;
01450                         }
01451                         $rs->moveNext();
01452                     }
01453                 }
01454             }
01455         }
01456 
01457         // Checking if user is connected via Facebook connect.
01458         // If yes, trying to login user using user Facebook ID
01459         if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01460             $oFb = oxFb::getInstance();
01461             if ( $oFb->isConnected() && $oFb->getUser() ) {
01462                 $sUserSelect = "oxuser.oxfbid = " . $oDb->quote( $oFb->getUser() );
01463                 $sShopSelect = "";
01464 
01465 
01466                 $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01467                 $sUserID = $oDb->getOne( $sSelect );
01468             }
01469         }
01470 
01471         // checking user results
01472         if ( $sUserID ) {
01473             if ( $this->load( $sUserID ) ) {
01474                 // storing into session
01475                 if ($blAdmin) {
01476                     oxSession::setVar( 'auth', $sUserID );
01477                 } else {
01478                     oxSession::setVar( 'usr', $sUserID );
01479                 }
01480 
01481                 // marking the way user was loaded
01482                 $this->_blLoadedFromCookie = $blFoundInCookie;
01483                 return true;
01484             }
01485         } else {
01486             // no user
01487             if ($blAdmin) {
01488                 oxSession::deleteVar( 'auth' );
01489             } else {
01490                 oxSession::deleteVar( 'usr' );
01491             }
01492 
01493             return false;
01494         }
01495     }
01496 
01509     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01510     {
01511         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01512         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01513 
01514         // maybe this is LDAP user but supplied email Address instead of LDAP login
01515         $oDb = oxDb::getDb();
01516         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01517         if ( isset( $sLDAPKey) && $sLDAPKey) {
01518             $sUser = $sLDAPKey;
01519         }
01520 
01521         //$throws oxConnectionException
01522         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01523 
01524         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01525         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01526             // login successful
01527 
01528             // check if user is already in database
01529             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01530             $sOXID = $oDb->getOne( $sSelect );
01531 
01532             if ( !isset( $sOXID ) || !$sOXID ) {
01533                 // we need to create a new user
01534                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01535                 $this->setId();
01536 
01537                 // map all user data fields
01538                 foreach ( $aData as $fldname => $value) {
01539                     $sField = "oxuser__".strtolower( $fldname);
01540                     $this->$sField = new oxField( $aData[$fldname] );
01541                 }
01542 
01543                 $this->oxuser__oxactive  = new oxField( 1 );
01544                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01545                 $this->oxuser__oxldapkey = new oxField( $sUser );
01546                 $this->oxuser__oxrights  = new oxField( "user" );
01547                 $this->setPassword( "ldap user" );
01548 
01549                 $this->save();
01550             } else {
01551                 // LDAP user is already in OXID DB, load it
01552                 $this->load( $sOXID);
01553             }
01554 
01555         } else {
01556             $oEx = oxNew( 'oxUserException' );
01557             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01558             throw $oEx;
01559         }
01560     }
01561 
01568     protected function _getUserRights()
01569     {
01570         // previously user had no rights defined
01571         if ( !$this->oxuser__oxrights->value )
01572             return 'user';
01573 
01574         $oDb = oxDb::getDb();
01575         $myConfig    = $this->getConfig();
01576         $sAuthRights = null;
01577 
01578         // choosing possible user rights index
01579         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01580         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01581         if ( $sAuthUserID ) {
01582             $sAuthRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $sAuthUserID ) );
01583         }
01584 
01585         //preventing user rights edit for non admin
01586         $aRights = array();
01587 
01588         // selecting current users rights ...
01589         if ( $sCurrRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $this->getId() ) ) ) {
01590             $aRights[] = $sCurrRights;
01591         }
01592         $aRights[] = 'user';
01593 
01594         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01595             return current( $aRights );
01596         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01597             $aRights[] = $sAuthRights;
01598             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01599                 return current( $aRights );
01600             }
01601         }
01602 
01603         // leaving as it was set ...
01604         return $this->oxuser__oxrights->value;
01605     }
01606 
01612     protected function _insert()
01613     {
01614 
01615         // set oxcreate date
01616         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01617 
01618         if ( !isset( $this->oxuser__oxboni->value ) ) {
01619             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01620         }
01621 
01622         return parent::_insert();
01623     }
01624 
01630     protected function _update()
01631     {
01632         //V #M418: for not registered users, don't change boni during update
01633         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01634             $this->_aSkipSaveFields[] = 'oxboni';
01635         }
01636 
01637         // don't change this field
01638         $this->_aSkipSaveFields[] = 'oxcreate';
01639         if ( !$this->isAdmin() ) {
01640             $this->_aSkipSaveFields[] = 'oxcustnr';
01641             $this->_aSkipSaveFields[] = 'oxrights';
01642         }
01643 
01644         // updating subscription information
01645         if ( ( $blUpdate = parent::_update() ) ) {
01646             $this->getNewsSubscription()->updateSubscription( $this );
01647         }
01648 
01649         return $blUpdate;
01650     }
01651 
01667     protected function _checkLogin( $sLogin, $aInvAddress )
01668     {
01669         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01670         oxInputValidator::getInstance()->checkLogin( $this, $sLogin, $aInvAddress );
01671         return $sLogin;
01672     }
01673 
01681     public function checkIfEmailExists( $sEmail )
01682     {
01683         $myConfig = $this->getConfig();
01684         $oDb = oxDb::getDb();
01685         $iShopId = $myConfig->getShopId();
01686         $blExists = false;
01687 
01688         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDb->quote( $sEmail );
01689         if ( ( $sOxid = $this->getId() ) ) {
01690             $sQ .= " and oxid <> ".$oDb->quote( $sOxid );
01691         }
01692         $oRs = $oDb->select( $sQ );
01693         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01694 
01695             if ( $this->_blMallUsers ) {
01696 
01697                 $blExists = true;
01698                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01699 
01700                     // password is not set - allow to override
01701                     $blExists = false;
01702                 }
01703             } else {
01704 
01705                 $blExists = false;
01706                 while ( !$oRs->EOF ) {
01707                     if ( $oRs->fields[1] != 'user' ) {
01708 
01709                         // exists admin with same login - must not allow
01710                         $blExists = true;
01711                         break;
01712                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01713 
01714                         // exists same login (with password) in same shop
01715                         $blExists = true;
01716                         break;
01717                     }
01718 
01719                     $oRs->moveNext();
01720                 }
01721             }
01722         }
01723         return $blExists;
01724     }
01725 
01733     public function getUserRecommLists( $sOXID = null )
01734     {
01735         if ( !$sOXID )
01736             $sOXID = $this->getId();
01737 
01738         // sets active page
01739         $iActPage = (int) oxConfig::getParameter( '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         return $this->_iCntRecommLists;
01778     }
01779 
01790     protected function _checkEmail( $sEmail )
01791     {
01792         oxInputValidator::getInstance()->checkEmail( $this, $sEmail );
01793     }
01794 
01809     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01810     {
01811         return $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01812     }
01813 
01826     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01827     {
01828         return oxInputValidator::getInstance()->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01829     }
01830 
01841     protected function _checkCountries( $aInvAddress, $aDelAddress )
01842     {
01843         oxInputValidator::getInstance()->checkCountries( $this, $aInvAddress, $aDelAddress );
01844     }
01845 
01857     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01858     {
01859         oxInputValidator::getInstance()->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01860     }
01861 
01872     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01873     {
01874         oxInputValidator::getInstance()->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01875     }
01876 
01887     protected function _checkVatId( $aInvAddress )
01888     {
01889         oxInputValidator::getInstance()->checkVatId( $this, $aInvAddress );
01890     }
01891 
01900     protected function _setAutoGroups( $sCountryId )
01901     {
01902         // assigning automatically to specific groups
01903         $blForeigner = true;
01904         $blForeignGroupExists = false;
01905         $blInlandGroupExists = false;
01906 
01907         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01908         // foreigner ?
01909         if ( is_array($aHomeCountry)) {
01910             if (in_array($sCountryId, $aHomeCountry)) {
01911                 $blForeigner = false;
01912             }
01913         } elseif ($sCountryId == $aHomeCountry) {
01914             $blForeigner = false;
01915         }
01916 
01917         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01918             $blForeignGroupExists = true;
01919             if ( !$blForeigner ) {
01920                 $this->removeFromGroup( 'oxidforeigncustomer' );
01921             }
01922         }
01923 
01924         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01925             $blInlandGroupExists = true;
01926             if ( $blForeigner ) {
01927                 $this->removeFromGroup( 'oxidnewcustomer' );
01928             }
01929         }
01930 
01931         if ( !$this->oxuser__oxdisableautogrp->value ) {
01932             if ( !$blForeignGroupExists && $blForeigner ) {
01933                 $this->addToGroup( 'oxidforeigncustomer' );
01934             }
01935             if ( !$blInlandGroupExists && !$blForeigner ) {
01936                 $this->addToGroup( 'oxidnewcustomer' );
01937             }
01938         }
01939     }
01940 
01949     protected function _hasUserAddress( $sUserId )
01950     {
01951         $oAddresses = $this->getUserAddresses();
01952         if ( $oAddresses && count($oAddresses)>0 ) {
01953             $oAddresses->rewind() ;
01954             foreach ($oAddresses as $key => $oAddress) {
01955                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01956                     return true;
01957                 }
01958             }
01959         }
01960         return false;
01961     }
01962 
01976     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000, $sSalt = 'ox' )
01977     {
01978         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout, $sSalt );
01979     }
01980 
01990     protected function _deleteUserCookie( $sShopId = null )
01991     {
01992         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
01993     }
01994 
02004     protected static function _getUserCookie( $sShopId = null )
02005     {
02006         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
02007     }
02008 
02009 
02018     public function loadUserByUpdateId( $sUid )
02019     {
02020         $oDb = oxDb::getDb();
02021         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02022         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02023             return $this->load( $sUserId );
02024         }
02025     }
02026 
02034     public function setUpdateKey( $blReset = false )
02035     {
02036         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02037         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02038 
02039         // generating key
02040         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02041 
02042         // setting expiration time for 6 hours
02043         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02044 
02045         // saving
02046         $this->save();
02047     }
02048 
02054     public function getUpdateLinkTerm()
02055     {
02056         return 3600 * 6;
02057     }
02058 
02066     public function isExpiredUpdateId( $sKey )
02067     {
02068         $oDb = oxDb::getDb();
02069         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02070         return !( (bool) $oDb->getOne( $sQ ) );
02071     }
02072 
02078     public function getUpdateId()
02079     {
02080         if ( $this->_sUpdateKey === null ) {
02081             $this->setUpdateKey();
02082             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02083         }
02084         return $this->_sUpdateKey;
02085     }
02086 
02095     public function encodePassword( $sPassword, $sSalt )
02096     {
02097         $oDb = oxDb::getDb();
02098         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02099     }
02100 
02108     public function prepareSalt( $sSalt )
02109     {
02110         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02111     }
02112 
02120     public function decodeSalt( $sSaltHex )
02121     {
02122         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02123     }
02124 
02132     public function setPassword( $sPassword = null )
02133     {
02134         // setting salt if password is not empty
02135         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02136 
02137         // encoding only if password was not empty (e.g. user registration without pass)
02138         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02139 
02140         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02141         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02142     }
02143 
02151     public function isSamePassword( $sNewPass )
02152     {
02153         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02154     }
02155 
02161     public function isLoadedFromCookie()
02162     {
02163         return $this->_blLoadedFromCookie;
02164     }
02165 
02172     public function getPasswordHash()
02173     {
02174         $sHash = null;
02175         if ( $this->oxuser__oxpassword->value ) {
02176             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02177                 // decodable pass ?
02178                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02179             } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02180                 // plain pass ?
02181                 $this->setPassword( $this->oxuser__oxpassword->value );
02182             }
02183             $sHash = $this->oxuser__oxpassword->value;
02184         }
02185         return $sHash;
02186     }
02187 
02196     public static function getAdminUser()
02197     {
02198         return self::getActiveUser( true );
02199     }
02200 
02211     public static function getActiveUser( $blForceAdmin = false )
02212     {
02213         $oUser = oxNew( 'oxuser' );
02214         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02215             return $oUser;
02216         } else {
02217             return false;
02218         }
02219     }
02220 
02228     public function getReviewUserHash( $sUserId )
02229     {
02230         $oDb = oxDb::getDb();
02231         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02232         return $sReviewUserHash;
02233     }
02234 
02242     public function getReviewUserId( $sReviewUserHash )
02243     {
02244         $oDb = oxDb::getDb();
02245         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02246         return $sUserId;
02247     }
02248 
02254     public function getState()
02255     {
02256         return $this->oxuser__oxstateid->value;
02257     }
02258 
02264     public function isTermsAccepted()
02265     {
02266         $oDb = oxDb::getDb();
02267         $sShopId = $this->getConfig()->getShopId();
02268         $sUserId = $oDb->quote( $this->getId() );
02269         return (bool) $oDb->getOne( "select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'" );
02270     }
02271 
02277     public function acceptTerms()
02278     {
02279         $oDb = oxDb::getDb();
02280         $sUserId  = $oDb->quote( $this->getId() );
02281         $sShopId  = $this->getConfig()->getShopId();
02282         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02283 
02284         $oDb->execute( "replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02285     }
02286 
02296     public function setCreditPointsForRegistrant( $sUserId, $sRecEmail )
02297     {
02298         $blSet   = false;
02299         $oDb = oxDb::getDb();
02300         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02301         // check if this invitation is still not accepted
02302         $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);
02303         if ( $iPoints && $iPending ) {
02304             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02305             if ( $blSet = $this->save() ) {
02306                 // updating users statistics
02307                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail ) );
02308                 $oInvUser = oxNew( "oxuser" );
02309                 if ( $oInvUser->load( $sUserId ) ) {
02310                     $blSet = $oInvUser->setCreditPointsForInviter();
02311                 }
02312             }
02313         }
02314         oxSession::deleteVar( 'su' );
02315         oxSession::deleteVar( 're' );
02316 
02317         return $blSet;
02318     }
02319 
02325     public function setCreditPointsForInviter()
02326     {
02327         $blSet   = false;
02328         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02329         if ( $iPoints ) {
02330             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02331             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02332             $blSet = $this->save();
02333         }
02334 
02335         return $blSet;
02336     }
02337 
02343     public function updateFbId()
02344     {
02345         $oFb = oxFb::getInstance();
02346         $blRet = false;
02347 
02348         if ( $oFb->isConnected() && $oFb->getUser() ) {
02349              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02350              $blRet = $this->save();
02351         }
02352 
02353         return $blRet;
02354     }
02355 
02363     public function updateInvitationStatistics( $aRecEmail )
02364     {
02365         $oDb = oxDb::getDb();
02366         $sUserId = $this->getId();
02367 
02368         if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
02369             //iserting statistics about invitation
02370             $sDate = oxUtilsDate::getInstance()->formatDBDate( date("Y-m-d"), true );
02371             $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
02372             foreach ( $aRecEmail as $sRecEmail ) {
02373                 $sSql = "INSERT INTO oxinvitations SET oxuserid = ".$oDb->quote( $sUserId ).", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02374                 $oDb->execute( $sSql );
02375             }
02376         }
02377     }
02378 
02386     public function getIdByUserName( $sUserName )
02387     {
02388         $oDb = oxDb::getDb();
02389         $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = ". $oDb->quote( $sUserName );
02390         if ( !$this->getConfig()->getConfigParam( 'blMallUsers' ) ) {
02391             $sQ .= " AND `oxshopid` = ". $oDb->quote( $this->getConfig()->getShopId() );
02392         }
02393 
02394         return $oDb->getOne( $sQ );
02395 
02396     }
02397 
02403     public function hasAccount()
02404     {
02405 
02406         return (bool) $this->oxuser__oxpassword->value;
02407 
02408     }
02409 
02410 }