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         if ( isset($this->_oNewsSubscription) ) {
00603             $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField( $oxID, oxField::T_RAW);
00604         }
00605 
00606         return $blRet;
00607     }
00608 
00616     public function exists( $sOXID = null )
00617     {
00618         $oDb = oxDb::getDb();
00619         if ( !$sOXID ) {
00620             $sOXID = $this->getId();
00621         }
00622 
00623         $sShopSelect = '';
00624         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00625             $sShopSelect = ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00626         }
00627 
00628         //#4543 Query optimisation by splitting it into two, might need an logics optimisation as well
00629         if ( $sOXID ) {
00630             $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00631                     WHERE ( oxid = '.$oDb->quote( $sOXID ).' ) ';
00632             $sSelect .= $sShopSelect;
00633 
00634             if ( ( $sOxid = $oDb->getOne( $sSelect ) ) ) {
00635                 // update - set oxid
00636                 $this->setId( $sOxid );
00637                 return true;
00638             }
00639         }
00640 
00641         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00642                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).' ) ';
00643         $sSelect .= $sShopSelect;
00644 
00645         if ( ( $sOxid = $oDb->getOne( $sSelect ) ) ) {
00646              // update - set oxid
00647             $this->setId( $sOxid );
00648             return true;
00649         }
00650         return false;
00651     }
00652 
00661     public function getOrders( $iLimit = false, $iPage = 0 )
00662     {
00663         $myConfig = $this->getConfig();
00664         $oOrders = oxNew( 'oxlist' );
00665         $oOrders->init( 'oxorder' );
00666 
00667         if ( $iLimit !== false ) {
00668             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00669         }
00670 
00671         //P
00672         // Lists does not support loading from two tables, so orders
00673         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00674         // forcing to load product info which is used in templates
00675         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00676 
00677         //loading order for registered user
00678         if ( $this->oxuser__oxregister->value > 1 ) {
00679             $oDb = oxDb::getDb();
00680             $sQ = 'select * from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' and oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value ) . ' ';
00681 
00682             //#1546 - shopid check added, if it is not multishop
00683 
00684             $sQ .= ' order by oxorderdate desc ';
00685             $oOrders->selectString( $sQ );
00686         }
00687 
00688         return $oOrders;
00689     }
00690 
00696     public function getOrderCount()
00697     {
00698         $iCnt = 0;
00699         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00700             $oDb = oxDb::getDb();
00701             $sQ  = 'select count(*) from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00702             $iCnt = (int) $oDb->getOne( $sQ );
00703         }
00704 
00705         return $iCnt;
00706     }
00707 
00713     public function getNoticeListArtCnt()
00714     {
00715         if ( $this->_iCntNoticeListArticles === null ) {
00716             $this->_iCntNoticeListArticles = 0;
00717             if ( $this->getId() ) {
00718                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00719             }
00720         }
00721         return $this->_iCntNoticeListArticles;
00722     }
00723 
00729     public function getWishListArtCnt()
00730     {
00731         if ( $this->_iCntWishListArticles === null ) {
00732             $this->_iCntWishListArticles = false;
00733             if ( $this->getId() ) {
00734                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00735             }
00736         }
00737         return $this->_iCntWishListArticles;
00738     }
00739 
00746     public function getActiveCountry()
00747     {
00748         $sDeliveryCountry = '';
00749         $soxAddressId = oxSession::getVar( 'deladrid' );
00750         if ( $soxAddressId ) {
00751             $oDelAddress = oxNew( 'oxaddress' );
00752             $oDelAddress->load( $soxAddressId );
00753             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00754         } elseif ( $this->getId() ) {
00755             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00756         } else {
00757             $oUser = oxNew( 'oxuser' );
00758             if ( $oUser->loadActiveUser() ) {
00759                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00760             }
00761         }
00762 
00763         return $sDeliveryCountry;
00764     }
00765 
00773     public function createUser()
00774     {
00775         $oDb = oxDb::getDb();
00776         $sShopID = $this->getConfig()->getShopId();
00777 
00778         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00779         $sSelect = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00780         if ( !$this->_blMallUsers ) {
00781             $sSelect .= " and oxshopid = '{$sShopID}' ";
00782         }
00783         $sOXID = $oDb->getOne( $sSelect, false, false );
00784 
00785         // user without password found - lets use
00786         if ( isset( $sOXID ) && $sOXID ) {
00787             // try to update
00788             $this->delete( $sOXID );
00789         } elseif ( $this->_blMallUsers ) {
00790             // must be sure if there is no dublicate user
00791             $sQ = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00792             if ( $oDb->getOne( $sQ, false, false ) ) {
00793                 $oEx = oxNew( 'oxUserException' );
00794                 $oLang = oxLang::getInstance();
00795                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00796                 throw $oEx;
00797             }
00798         }
00799 
00800         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00801         if ( ( $blOK = $this->save() ) ) {
00802             // dropping/cleaning old delivery address/payment info
00803             $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00804             $oDb->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00805         } else {
00806             $oEx = oxNew( 'oxUserException' );
00807             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00808             throw $oEx;
00809         }
00810 
00811         return $blOK;
00812     }
00813 
00821     public function addToGroup( $sGroupID )
00822     {
00823         if ( !$this->inGroup( $sGroupID ) ) {
00824             // create oxgroup object
00825             $oGroup = oxNew('oxGroups');
00826             if ( $oGroup->load($sGroupID) ) {
00827                 $oNewGroup = oxNew( 'oxobject2group' );
00828                 $oNewGroup->oxobject2group__oxobjectid = new oxField( $this->getId(), oxField::T_RAW );
00829                 $oNewGroup->oxobject2group__oxgroupsid = new oxField( $sGroupID, oxField::T_RAW );
00830                 if ( $oNewGroup->save() ) {
00831                     $this->_oGroups[$sGroupID] = $oGroup;
00832                     return true;
00833                 }
00834             }
00835         }
00836         return false;
00837     }
00838 
00846     public function removeFromGroup( $sGroupID = null )
00847     {
00848         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00849             $oGroups = oxNew( 'oxlist' );
00850             $oGroups->init( 'oxobject2group' );
00851             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00852             $oGroups->selectString( $sSelect );
00853             foreach ( $oGroups as $oRemgroup ) {
00854                 if ( $oRemgroup->delete() ) {
00855                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00856                 }
00857             }
00858         }
00859     }
00860 
00869     public function onOrderExecute( $oBasket, $iSuccess )
00870     {
00871 
00872         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00873             //adding user to particular customer groups
00874             if ( !$this->oxuser__oxdisableautogrp->value ) {
00875 
00876                 $myConfig = $this->getConfig();
00877                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00878                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00879 
00880                 $this->addToGroup( 'oxidcustomer' );
00881                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00882                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00883                     $this->addToGroup( 'oxidsmallcust' );
00884                 }
00885                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00886                     $this->addToGroup( 'oxidmiddlecust' );
00887                 }
00888                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00889                     $this->addToGroup( 'oxidgoodcust' );
00890                 }
00891             }
00892 
00893             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00894                 $this->removeFromGroup( 'oxidnotyetordered' );
00895             }
00896         }
00897     }
00898 
00906     public function getBasket( $sName )
00907     {
00908         if ( !isset( $this->_aBaskets[$sName] ) ) {
00909             $oBasket = oxNew( 'oxuserbasket' );
00910             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00911 
00912             // creating if it does not exist
00913             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00914                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00915                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00916 
00917                 // marking basket as new (it will not be saved in DB yet)
00918                 $oBasket->setIsNewBasket();
00919             }
00920 
00921             $this->_aBaskets[$sName] = $oBasket;
00922         }
00923 
00924         return $this->_aBaskets[$sName];
00925     }
00926 
00935     public function convertBirthday( $aData )
00936     {
00937 
00938         // preparing data to process
00939         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00940         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00941         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00942 
00943         // leaving empty if not set
00944         if ( !$iYear && !$iMonth && !$iDay )
00945             return "";
00946 
00947         // year
00948         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00949             $iYear = date('Y');
00950 
00951         // month
00952         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00953             $iMonth = 1;
00954 
00955         // maximum nuber of days in month
00956         $iMaxDays = 31;
00957         switch( $iMonth) {
00958             case 2 :
00959                 if ($iMaxDays > 28)
00960                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00961                 break;
00962             case 4  :
00963             case 6  :
00964             case 9  :
00965             case 11 :
00966                 $iMaxDays = min(30, $iMaxDays);
00967                 break;
00968         }
00969 
00970         // day
00971         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00972             $iDay = 1;
00973         }
00974 
00975         // whole date
00976         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00977     }
00978 
00985     public function getBoni()
00986     {
00987         return 1000;
00988     }
00989 
01002     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
01003     {
01004         // preparing input
01005         $sDynGoup = strtolower( trim( $sDynGoup ) );
01006 
01007         // setting denied groups from admin settings also
01008         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
01009 
01010         // default state ..
01011         $blAdd = false;
01012 
01013         // user assignment to dyn group is not allowed
01014         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
01015             $blAdd = false;
01016         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
01017             // trying to add user to prohibited user group?
01018             $blAdd = false;
01019         } elseif ( $this->addToGroup( $sDynGoup ) ) {
01020             $blAdd = true;
01021         }
01022 
01023         // cleanup
01024         oxSession::deleteVar( 'dgr' );
01025 
01026         return $blAdd;
01027     }
01028 
01047     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01048     {
01049         // 1. checking user name
01050         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01051 
01052         // 2. cheking email
01053         $this->_checkEmail( $sLogin );
01054 
01055         // 3. password
01056         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01057 
01058         // 4. required fields
01059         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01060 
01061         // 5. country check
01062         $this->_checkCountries( $aInvAddress, $aDelAddress );
01063 
01064         // 6. vat id check.
01065             $this->_checkVatId( $aInvAddress );
01066 
01067 
01068         // throwing first validation error
01069         if ( $oError = oxInputValidator::getInstance()->getFirstValidationError() ) {
01070             throw $oError;
01071         }
01072     }
01073 
01083     public function setNewsSubscription( $blSubscribe, $blSendOptIn, $blForceCheckOptIn = false  )
01084     {
01085         // assigning to newsletter
01086         $blSuccess = false;
01087         $myConfig  = $this->getConfig();
01088         $mySession = $this->getSession();
01089 
01090         // user wants to get newsletter messages or no ?
01091         $oNewsSubscription = $this->getNewsSubscription();
01092         if ( $oNewsSubscription ) {
01093             if ( $blSubscribe && ($blForceCheckOptIn || ( $iOptInStatus = $oNewsSubscription->getOptInStatus() ) != 1) ) {
01094                 if ( !$blSendOptIn ) {
01095 
01096                     // double-opt-in check is disabled - assigning automatically
01097                     $this->addToGroup( 'oxidnewsletter' );
01098                     // and setting subscribed status
01099                     $oNewsSubscription->setOptInStatus( 1 );
01100                     $blSuccess = true;
01101                 } else {
01102 
01103                     // double-opt-in check enabled - sending confirmation email and setting waiting status
01104                     if ( $iOptInStatus != 2 ) {
01105                         // sending double-opt-in mail
01106                         $oEmail = oxNew( 'oxemail' );
01107                         $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01108                     } else {
01109                         // mail already was sent, so just confirming that
01110                         $blSuccess = true;
01111                     }
01112 
01113                     $oNewsSubscription->setOptInStatus( 2 );
01114                 }
01115             } elseif ( !$blSubscribe ) {
01116                 // removing user from newsletter subscribers
01117                 $this->removeFromGroup( 'oxidnewsletter' );
01118                 $oNewsSubscription->setOptInStatus( 0 );
01119                 $blSuccess = true;
01120             }
01121         }
01122 
01123         return $blSuccess;
01124     }
01125 
01143     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01144     {
01145         // validating values before saving. If validation fails - exception is thrown
01146         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01147 
01148         // input data is fine - lets save updated user info
01149         $this->assign( $aInvAddress );
01150 
01151 
01152         // update old or add new delivery address
01153         $this->_assignAddress( $aDelAddress );
01154 
01155         // saving new values
01156         if ( $this->save() ) {
01157 
01158             // assigning automatically to specific groups
01159             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01160             $this->_setAutoGroups( $sCountryId );
01161         }
01162     }
01163 
01174     public function addUserAddress( $oUser )
01175     {
01176         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01177             return false;
01178         }
01179 
01180         $oAddress = oxNew( 'oxaddress' );
01181         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01182         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01183         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01184         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01185         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01186         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01187         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01188         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01189         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01190         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01191         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01192 
01193         // adding new address
01194         if ( $oAddress->save() ) {
01195             // resetting addresses
01196             $this->_aAddresses = null;
01197             return $oAddress->getId();
01198         }
01199     }
01200 
01208     protected function _assignAddress( $aDelAddress )
01209     {
01210         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01211             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01212             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01213             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01214             $oAddress = oxNew( 'oxaddress' );
01215             $oAddress->assign( $aDelAddress );
01216             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01217             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01218             $oAddress->save();
01219 
01220             // resetting addresses
01221             $this->_aAddresses = null;
01222 
01223             // saving delivery Address for later use
01224             oxSession::setVar( 'deladrid', $oAddress->getId() );
01225         } else {
01226             // resetting
01227             oxSession::setVar( 'deladrid', null );
01228         }
01229     }
01230 
01241     protected function _getLoginQuery( $sUser, $sPassword, $sShopID, $blAdmin )
01242     {
01243         $myConfig = $this->getConfig();
01244         $oDb = oxDb::getDb();
01245 
01246         $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01247         $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01248         
01249         $sShopSelect = $this->_getShopSelect( $myConfig, $sShopID, $blAdmin );
01250 
01251         $blStagingMode = false;
01252         $blDemoMode = false;
01253         $sWhat = "oxid";
01254             if ($myConfig->isDemoShop()) {
01255                 $blDemoMode = true;
01256             }
01257 
01258         $sSelect = "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01259         if ( ( $blDemoMode || $blStagingMode ) && $blAdmin ) {
01260             if ( $sPassword == "admin" && $sUser == "admin" ) {
01261                 $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' ";
01262             } elseif ( $blDemoMode ) {
01263                 $oEx = oxNew( 'oxUserException' );
01264                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01265                 throw $oEx;
01266             }
01267         }
01268 
01269         return $sSelect;
01270     }
01271 
01281     protected function _getShopSelect( $myConfig, $sShopID, $blAdmin )
01282     {
01283         $sShopSelect = "";
01284         // admin view: can only login with higher than 'user' rights
01285         if ( $blAdmin ) {
01286             $sShopSelect = " and ( oxrights != 'user' ) ";
01287         }
01288         
01289         return $sShopSelect;
01290     }
01298     protected function _loadSavedUserBasketAfterLogin()
01299     {
01300         if ( !$this->isAdmin() && !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' )) {
01301             //load basket from the database
01302             try {
01303                 if ( $oBasket = $this->getSession()->getBasket() ) {
01304                     $oBasket->load();
01305                 }
01306             } catch ( Exception $oE ) {
01307                 //just ignore it
01308             }
01309         }
01310     }
01311     
01312 
01325     public function login( $sUser, $sPassword, $blCookie = false)
01326     {
01327         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01328             $oEx = oxNew( 'oxCookieException' );
01329             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01330             throw $oEx;
01331         }
01332 
01333         $myConfig = $this->getConfig();
01334         if ( $sPassword ) {
01335 
01336             $sShopID = $myConfig->getShopId();
01337             $sSelect = $this->_getLoginQuery( $sUser, $sPassword, $sShopID, $this->isAdmin() );
01338 
01339             // load from DB
01340             $aData = oxDb::getDb()->getAll( $sSelect );
01341             $sOXID = @$aData[0][0];
01342             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01343 
01344                 if ( !$this->load( $sOXID ) ) {
01345                     $oEx = oxNew( 'oxUserException' );
01346                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01347                     throw $oEx;
01348                 }
01349             }
01350         }
01351 
01352 
01353         //login successfull?
01354         if ( $this->oxuser__oxid->value ) {
01355             // yes, successful login
01356 
01357             //reseting active user
01358             $this->setUser( null );
01359 
01360             if ( $this->isAdmin() ) {
01361                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01362             } else {
01363                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01364             }
01365 
01366             // cookie must be set ?
01367             if ( $blCookie && $myConfig->getConfigParam( 'blShowRememberMe' ) ) {
01368                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value );
01369             }
01370 
01371             //load basket from the database
01372             $this->_loadSavedUserBasketAfterLogin();
01373 
01374             return true;
01375         } else {
01376             $oEx = oxNew( 'oxUserException' );
01377             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01378             throw $oEx;
01379         }
01380     }
01381 
01387     public function logout()
01388     {
01389         // deleting session info
01390         oxSession::deleteVar( 'usr' );  // for front end
01391         oxSession::deleteVar( 'auth' ); // for back end
01392         oxSession::deleteVar( 'dgr' );
01393         oxSession::deleteVar( 'dynvalue' );
01394         oxSession::deleteVar( 'paymentid' );
01395         // oxSession::deleteVar( 'deladrid' );
01396 
01397         // delete cookie
01398         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01399 
01400         // unsetting global user
01401         $this->setUser( null );
01402 
01403         return true;
01404     }
01405 
01412     public function loadAdminUser()
01413     {
01414         return $this->loadActiveUser( true );
01415     }
01416 
01425     public function loadActiveUser( $blForceAdmin = false )
01426     {
01427         $myConfig = $this->getConfig();
01428 
01429         $blAdmin = $this->isAdmin() || $blForceAdmin;
01430 
01431         // first - checking session info
01432         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01433 
01434         // trying automatic login (by 'remember me' cookie)
01435         $blFoundInCookie = false;
01436         if ( !$sUserID && !$blAdmin && $myConfig->getConfigParam('blShowRememberMe') ) {
01437             $sUserID = $this->_getCookieUserId( $blAdmin );
01438             $blFoundInCookie = $sUserID? true : false;
01439         }
01440 
01441         // If facebook connection is enabled, trying to login user using Facebook ID
01442         if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01443             $sUserID = $this->_getFacebookUserId();
01444         }
01445 
01446         // checking user results
01447         if ( $sUserID ) {
01448             if ( $this->load( $sUserID ) ) {
01449                 // storing into session
01450                 if ($blAdmin) {
01451                     oxSession::setVar( 'auth', $sUserID );
01452                 } else {
01453                     oxSession::setVar( 'usr', $sUserID );
01454                 }
01455 
01456                 // marking the way user was loaded
01457                 $this->_blLoadedFromCookie = $blFoundInCookie;
01458                 return true;
01459             }
01460         } else {
01461             // no user
01462             if ($blAdmin) {
01463                 oxSession::deleteVar( 'auth' );
01464             } else {
01465                 oxSession::deleteVar( 'usr' );
01466             }
01467 
01468             return false;
01469         }
01470     }
01471 
01477     protected function _getFacebookUserId()
01478     {
01479         $oDb = oxDb::getDb();
01480         $oFb = oxFb::getInstance();
01481         $oConfig = $this->getConfig();
01482         if ( $oFb->isConnected() && $oFb->getUser() ) {
01483             $sUserSelect = "oxuser.oxfbid = " . $oDb->quote( $oFb->getUser() );
01484             $sShopSelect = "";
01485 
01486 
01487             $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01488             $sUserID = $oDb->getOne( $sSelect );
01489         }
01490         return $sUserID;
01491     }
01492 
01500     protected function _getCookieUserId( $blAdmin = false )
01501     {
01502         $oConfig = $this->getConfig();
01503         $sShopID = $oConfig->getShopId();
01504         if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01505             $oDb = oxDb::getDb();
01506             $aData = explode( '@@@', $sSet );
01507             $sUser = $aData[0];
01508             $sPWD  = @$aData[1];
01509 
01510             $sSelect =  'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDb->quote($sUser);
01511 
01512             $rs = $oDb->select( $sSelect );
01513             if ( $rs != false && $rs->recordCount() > 0 ) {
01514                 while (!$rs->EOF) {
01515                     $sTest = crypt( $rs->fields[1], $rs->fields[2] );
01516                     if ( $sTest == $sPWD ) {
01517                         // found
01518                         $sUserID = $rs->fields[0];
01519                         break;
01520                     }
01521                     $rs->moveNext();
01522                 }
01523             }
01524             // if cookie info is not valid, remove it.
01525             if ( !$sUserID ) {
01526                 oxUtilsServer::getInstance()->deleteUserCookie( $sShopID );
01527             }
01528         }
01529         return $sUserID;
01530     }
01531 
01544     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01545     {
01546         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01547         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01548 
01549         // maybe this is LDAP user but supplied email Address instead of LDAP login
01550         $oDb = oxDb::getDb();
01551         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01552         if ( isset( $sLDAPKey) && $sLDAPKey) {
01553             $sUser = $sLDAPKey;
01554         }
01555 
01556         //$throws oxConnectionException
01557         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01558 
01559         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01560         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01561             // login successful
01562 
01563             // check if user is already in database
01564             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01565             $sOXID = $oDb->getOne( $sSelect );
01566 
01567             if ( !isset( $sOXID ) || !$sOXID ) {
01568                 // we need to create a new user
01569                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01570                 $this->setId();
01571 
01572                 // map all user data fields
01573                 foreach ( $aData as $fldname => $value) {
01574                     $sField = "oxuser__".strtolower( $fldname);
01575                     $this->$sField = new oxField( $aData[$fldname] );
01576                 }
01577 
01578                 $this->oxuser__oxactive  = new oxField( 1 );
01579                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01580                 $this->oxuser__oxldapkey = new oxField( $sUser );
01581                 $this->oxuser__oxrights  = new oxField( "user" );
01582                 $this->setPassword( "ldap user" );
01583 
01584                 $this->save();
01585             } else {
01586                 // LDAP user is already in OXID DB, load it
01587                 $this->load( $sOXID);
01588             }
01589 
01590         } else {
01591             $oEx = oxNew( 'oxUserException' );
01592             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01593             throw $oEx;
01594         }
01595     }
01596 
01603     protected function _getUserRights()
01604     {
01605         // previously user had no rights defined
01606         if ( !$this->oxuser__oxrights->value )
01607             return 'user';
01608 
01609         $oDb = oxDb::getDb();
01610         $myConfig    = $this->getConfig();
01611         $sAuthRights = null;
01612 
01613         // choosing possible user rights index
01614         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01615         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01616         if ( $sAuthUserID ) {
01617             $sAuthRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $sAuthUserID ) );
01618         }
01619 
01620         //preventing user rights edit for non admin
01621         $aRights = array();
01622 
01623         // selecting current users rights ...
01624         if ( $sCurrRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $this->getId() ) ) ) {
01625             $aRights[] = $sCurrRights;
01626         }
01627         $aRights[] = 'user';
01628 
01629         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01630             return current( $aRights );
01631         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01632             $aRights[] = $sAuthRights;
01633             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01634                 return current( $aRights );
01635             }
01636         }
01637 
01638         // leaving as it was set ...
01639         return $this->oxuser__oxrights->value;
01640     }
01641 
01647     protected function _insert()
01648     {
01649 
01650         // set oxcreate date
01651         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01652 
01653         if ( !isset( $this->oxuser__oxboni->value ) ) {
01654             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01655         }
01656 
01657         return parent::_insert();
01658     }
01659 
01665     protected function _update()
01666     {
01667         //V #M418: for not registered users, don't change boni during update
01668         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01669             $this->_aSkipSaveFields[] = 'oxboni';
01670         }
01671 
01672         // don't change this field
01673         $this->_aSkipSaveFields[] = 'oxcreate';
01674         if ( !$this->isAdmin() ) {
01675             $this->_aSkipSaveFields[] = 'oxcustnr';
01676             $this->_aSkipSaveFields[] = 'oxrights';
01677         }
01678 
01679         // updating subscription information
01680         if ( ( $blUpdate = parent::_update() ) ) {
01681             $this->getNewsSubscription()->updateSubscription( $this );
01682         }
01683 
01684         return $blUpdate;
01685     }
01686 
01702     protected function _checkLogin( $sLogin, $aInvAddress )
01703     {
01704         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01705         oxInputValidator::getInstance()->checkLogin( $this, $sLogin, $aInvAddress );
01706         return $sLogin;
01707     }
01708 
01716     public function checkIfEmailExists( $sEmail )
01717     {
01718         $myConfig = $this->getConfig();
01719         $oDb = oxDb::getDb();
01720         $iShopId = $myConfig->getShopId();
01721         $blExists = false;
01722 
01723         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDb->quote( $sEmail );
01724         if ( ( $sOxid = $this->getId() ) ) {
01725             $sQ .= " and oxid <> ".$oDb->quote( $sOxid );
01726         }
01727         $oRs = $oDb->select( $sQ );
01728         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01729 
01730             if ( $this->_blMallUsers ) {
01731 
01732                 $blExists = true;
01733                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01734 
01735                     // password is not set - allow to override
01736                     $blExists = false;
01737                 }
01738             } else {
01739 
01740                 $blExists = false;
01741                 while ( !$oRs->EOF ) {
01742                     if ( $oRs->fields[1] != 'user' ) {
01743 
01744                         // exists admin with same login - must not allow
01745                         $blExists = true;
01746                         break;
01747                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01748 
01749                         // exists same login (with password) in same shop
01750                         $blExists = true;
01751                         break;
01752                     }
01753 
01754                     $oRs->moveNext();
01755                 }
01756             }
01757         }
01758         return $blExists;
01759     }
01760 
01768     public function getUserRecommLists( $sOXID = null )
01769     {
01770         if ( !$sOXID )
01771             $sOXID = $this->getId();
01772 
01773         // sets active page
01774         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01775         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01776 
01777         // load only lists which we show on screen
01778         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01779         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01780 
01781 
01782         $oRecommList = oxNew( 'oxlist' );
01783         $oRecommList->init( 'oxrecommlist' );
01784         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01785         $iShopId = $this->getConfig()->getShopId();
01786         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01787         $oRecommList->selectString( $sSelect );
01788 
01789         return $oRecommList;
01790     }
01791 
01799     public function getRecommListsCount( $sOx = null )
01800     {
01801         if ( !$sOx ) {
01802             $sOx = $this->getId();
01803         }
01804 
01805         if ( $this->_iCntRecommLists === null || $sOx ) {
01806             $oDb = oxDb::getDb();
01807             $this->_iCntRecommLists = 0;
01808             $iShopId = $this->getConfig()->getShopId();
01809             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01810             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01811         }
01812         return $this->_iCntRecommLists;
01813     }
01814 
01825     protected function _checkEmail( $sEmail )
01826     {
01827         oxInputValidator::getInstance()->checkEmail( $this, $sEmail );
01828     }
01829 
01844     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01845     {
01846         return $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01847     }
01848 
01861     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01862     {
01863         return oxInputValidator::getInstance()->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01864     }
01865 
01876     protected function _checkCountries( $aInvAddress, $aDelAddress )
01877     {
01878         oxInputValidator::getInstance()->checkCountries( $this, $aInvAddress, $aDelAddress );
01879     }
01880 
01892     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01893     {
01894         oxInputValidator::getInstance()->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01895     }
01896 
01907     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01908     {
01909         oxInputValidator::getInstance()->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01910     }
01911 
01922     protected function _checkVatId( $aInvAddress )
01923     {
01924         oxInputValidator::getInstance()->checkVatId( $this, $aInvAddress );
01925     }
01926 
01935     protected function _setAutoGroups( $sCountryId )
01936     {
01937         // assigning automatically to specific groups
01938         $blForeigner = true;
01939         $blForeignGroupExists = false;
01940         $blInlandGroupExists = false;
01941 
01942         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01943         // foreigner ?
01944         if ( is_array($aHomeCountry)) {
01945             if (in_array($sCountryId, $aHomeCountry)) {
01946                 $blForeigner = false;
01947             }
01948         } elseif ($sCountryId == $aHomeCountry) {
01949             $blForeigner = false;
01950         }
01951 
01952         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01953             $blForeignGroupExists = true;
01954             if ( !$blForeigner ) {
01955                 $this->removeFromGroup( 'oxidforeigncustomer' );
01956             }
01957         }
01958 
01959         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01960             $blInlandGroupExists = true;
01961             if ( $blForeigner ) {
01962                 $this->removeFromGroup( 'oxidnewcustomer' );
01963             }
01964         }
01965 
01966         if ( !$this->oxuser__oxdisableautogrp->value ) {
01967             if ( !$blForeignGroupExists && $blForeigner ) {
01968                 $this->addToGroup( 'oxidforeigncustomer' );
01969             }
01970             if ( !$blInlandGroupExists && !$blForeigner ) {
01971                 $this->addToGroup( 'oxidnewcustomer' );
01972             }
01973         }
01974     }
01975 
01984     protected function _hasUserAddress( $sUserId )
01985     {
01986         $oAddresses = $this->getUserAddresses();
01987         if ( $oAddresses && count($oAddresses)>0 ) {
01988             $oAddresses->rewind() ;
01989             foreach ($oAddresses as $key => $oAddress) {
01990                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01991                     return true;
01992                 }
01993             }
01994         }
01995         return false;
01996     }
01997 
02011     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000, $sSalt = 'ox' )
02012     {
02013         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout, $sSalt );
02014     }
02015 
02025     protected function _deleteUserCookie( $sShopId = null )
02026     {
02027         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
02028     }
02029 
02039     protected static function _getUserCookie( $sShopId = null )
02040     {
02041         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
02042     }
02043 
02044 
02053     public function loadUserByUpdateId( $sUid )
02054     {
02055         $oDb = oxDb::getDb();
02056         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02057         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02058             return $this->load( $sUserId );
02059         }
02060     }
02061 
02069     public function setUpdateKey( $blReset = false )
02070     {
02071         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02072         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02073 
02074         // generating key
02075         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02076 
02077         // setting expiration time for 6 hours
02078         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02079 
02080         // saving
02081         $this->save();
02082     }
02083 
02089     public function getUpdateLinkTerm()
02090     {
02091         return 3600 * 6;
02092     }
02093 
02101     public function isExpiredUpdateId( $sKey )
02102     {
02103         $oDb = oxDb::getDb();
02104         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02105         return !( (bool) $oDb->getOne( $sQ ) );
02106     }
02107 
02113     public function getUpdateId()
02114     {
02115         if ( $this->_sUpdateKey === null ) {
02116             $this->setUpdateKey();
02117             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02118         }
02119         return $this->_sUpdateKey;
02120     }
02121 
02130     public function encodePassword( $sPassword, $sSalt )
02131     {
02132         $oDb = oxDb::getDb();
02133         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02134     }
02135 
02143     public function prepareSalt( $sSalt )
02144     {
02145         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02146     }
02147 
02155     public function decodeSalt( $sSaltHex )
02156     {
02157         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02158     }
02159 
02167     public function setPassword( $sPassword = null )
02168     {
02169         // setting salt if password is not empty
02170         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02171 
02172         // encoding only if password was not empty (e.g. user registration without pass)
02173         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02174 
02175         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02176         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02177     }
02178 
02186     public function isSamePassword( $sNewPass )
02187     {
02188         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02189     }
02190 
02196     public function isLoadedFromCookie()
02197     {
02198         return $this->_blLoadedFromCookie;
02199     }
02200 
02207     public function getPasswordHash()
02208     {
02209         $sHash = null;
02210         if ( $this->oxuser__oxpassword->value ) {
02211             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02212                 // decodable pass ?
02213                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02214             } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02215                 // plain pass ?
02216                 $this->setPassword( $this->oxuser__oxpassword->value );
02217             }
02218             $sHash = $this->oxuser__oxpassword->value;
02219         }
02220         return $sHash;
02221     }
02222 
02231     public static function getAdminUser()
02232     {
02233         return self::getActiveUser( true );
02234     }
02235 
02246     public static function getActiveUser( $blForceAdmin = false )
02247     {
02248         $oUser = oxNew( 'oxuser' );
02249         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02250             return $oUser;
02251         } else {
02252             return false;
02253         }
02254     }
02255 
02263     public function getReviewUserHash( $sUserId )
02264     {
02265         $oDb = oxDb::getDb();
02266         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02267         return $sReviewUserHash;
02268     }
02269 
02277     public function getReviewUserId( $sReviewUserHash )
02278     {
02279         $oDb = oxDb::getDb();
02280         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02281         return $sUserId;
02282     }
02283 
02289     public function getState()
02290     {
02291         return $this->oxuser__oxstateid->value;
02292     }
02293 
02299     public function isTermsAccepted()
02300     {
02301         $oDb = oxDb::getDb();
02302         $sShopId = $this->getConfig()->getShopId();
02303         $sUserId = $oDb->quote( $this->getId() );
02304         return (bool) $oDb->getOne( "select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'" );
02305     }
02306 
02312     public function acceptTerms()
02313     {
02314         $oDb = oxDb::getDb();
02315         $sUserId  = $oDb->quote( $this->getId() );
02316         $sShopId  = $this->getConfig()->getShopId();
02317         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02318 
02319         $oDb->execute( "replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02320     }
02321 
02331     public function setCreditPointsForRegistrant( $sUserId, $sRecEmail )
02332     {
02333         $blSet   = false;
02334         $oDb = oxDb::getDb();
02335         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02336         // check if this invitation is still not accepted
02337         $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);
02338         if ( $iPoints && $iPending ) {
02339             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02340             if ( $blSet = $this->save() ) {
02341                 // updating users statistics
02342                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail ) );
02343                 $oInvUser = oxNew( "oxuser" );
02344                 if ( $oInvUser->load( $sUserId ) ) {
02345                     $blSet = $oInvUser->setCreditPointsForInviter();
02346                 }
02347             }
02348         }
02349         oxSession::deleteVar( 'su' );
02350         oxSession::deleteVar( 're' );
02351 
02352         return $blSet;
02353     }
02354 
02360     public function setCreditPointsForInviter()
02361     {
02362         $blSet   = false;
02363         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02364         if ( $iPoints ) {
02365             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02366             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02367             $blSet = $this->save();
02368         }
02369 
02370         return $blSet;
02371     }
02372 
02378     public function updateFbId()
02379     {
02380         $oFb = oxFb::getInstance();
02381         $blRet = false;
02382 
02383         if ( $oFb->isConnected() && $oFb->getUser() ) {
02384              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02385              $blRet = $this->save();
02386         }
02387 
02388         return $blRet;
02389     }
02390 
02398     public function updateInvitationStatistics( $aRecEmail )
02399     {
02400         $oDb = oxDb::getDb();
02401         $sUserId = $this->getId();
02402 
02403         if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
02404             //iserting statistics about invitation
02405             $sDate = oxUtilsDate::getInstance()->formatDBDate( date("Y-m-d"), true );
02406             $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
02407             foreach ( $aRecEmail as $sRecEmail ) {
02408                 $sSql = "INSERT INTO oxinvitations SET oxuserid = ".$oDb->quote( $sUserId ).", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02409                 $oDb->execute( $sSql );
02410             }
02411         }
02412     }
02413 
02421     public function getIdByUserName( $sUserName )
02422     {
02423         $oDb = oxDb::getDb();
02424         $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = ". $oDb->quote( $sUserName );
02425         if ( !$this->getConfig()->getConfigParam( 'blMallUsers' ) ) {
02426             $sQ .= " AND `oxshopid` = ". $oDb->quote( $this->getConfig()->getShopId() );
02427         }
02428 
02429         return $oDb->getOne( $sQ );
02430 
02431     }
02432 
02438     public function hasAccount()
02439     {
02440 
02441         return (bool) $this->oxuser__oxpassword->value;
02442 
02443     }
02444 
02445 }