oxuser.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class oxUser extends oxBase
00010 {
00015     protected $_blDisableShopCheck = true;
00016 
00021     protected $_oNewsSubscription = null;
00022 
00027     protected $_sClassName = 'oxuser';
00028 
00034     protected $_aBaskets = array();
00035 
00041     protected $_oGroups;
00042 
00048     protected $_aAddresses = array();
00049 
00055     protected $_oPayments;
00056 
00062     protected $_oRecommList;
00063 
00069     protected $_blMallUsers = false;
00070 
00076     protected static $_aUserCookie = array();
00077 
00083     protected $_iCntNoticeListArticles = null;
00084 
00090     protected $_iCntWishListArticles = null;
00091 
00097     protected $_iCntRecommLists = null;
00098 
00104     protected $_sUpdateKey = null;
00105 
00111     protected $_blLoadedFromCookie  = null;
00112 
00118     protected $_sSelAddressId = null;
00119 
00125     protected $_oSelAddress = null;
00126 
00132     protected $_sWishId = null;
00133 
00139     protected $_oUserCountryTitle = null;
00140 
00146     public function __construct()
00147     {
00148         $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00149 
00150         parent::__construct();
00151         $this->init( 'oxuser' );
00152     }
00153 
00161     public function setMallUsersStatus( $blOn = false )
00162     {
00163         $this->_blMallUsers = $blOn;
00164     }
00165 
00173     public function __get( $sParamName )
00174     {
00175         // it saves memory using - loads data only if it is used
00176         switch ( $sParamName ) {
00177             case 'oGroups':
00178                 return $this->_oGroups = $this->getUserGroups();
00179                 break;
00180             case 'iCntNoticeListArticles':
00181                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00182                 break;
00183             case 'iCntWishListArticles':
00184                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00185                 break;
00186             case 'iCntRecommLists':
00187                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00188                 break;
00189             case 'oAddresses':
00190                 return $this->getUserAddresses();
00191                 break;
00192             case 'oPayments':
00193                 return $this->_oPayments = $this->getUserPayments();
00194                 break;
00195             case 'oxuser__oxcountry':
00196                 return $this->oxuser__oxcountry = $this->getUserCountry();
00197                 break;
00198             case 'sDBOptin':
00199                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00200                 break;
00201             case 'sEmailFailed':
00202                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00203                 break;
00204         }
00205     }
00206 
00212     public function getNewsSubscription()
00213     {
00214         if ( $this->_oNewsSubscription !== null ) {
00215             return $this->_oNewsSubscription;
00216         }
00217 
00218         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00219 
00220         // if subscription object is not set yet - we should create one
00221         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00222             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00223 
00224                 // no subscription defined yet - creating one
00225                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00226                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00227                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00228                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00229                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00230             }
00231         }
00232 
00233         return $this->_oNewsSubscription;
00234     }
00235 
00245     public function getUserCountry( $sCountryId = null, $iLang = null )
00246     {
00247         if ( $this->_oUserCountryTitle == null || $sCountryId ) {
00248             $sId = $sCountryId ? $sCountryId : $this->oxuser__oxcountryid->value;
00249             $oDb = oxDb::getDb();
00250             $sViewName = getViewName( 'oxcountry', $iLang );
00251             $sQ = "select oxtitle from {$sViewName} where oxid = " . $oDb->quote( $sId ) . " ";
00252             $oCountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00253             if ( !$sCountryId ) {
00254                 $this->_oUserCountryTitle = $oCountry;
00255             }
00256         } else {
00257             return $this->_oUserCountryTitle;
00258         }
00259 
00260         return $oCountry;
00261     }
00262 
00270     public function getUserCountryId( $sCountry = null )
00271     {
00272         $oDb = oxDb::getDb();
00273         $sQ = "select oxid from ".getviewName( "oxcountry" )." where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00274         $sCountryId = $oDb->getOne( $sQ );
00275 
00276         return $sCountryId;
00277     }
00278 
00286     public function getUserGroups( $sOXID = null )
00287     {
00288 
00289         if ( isset( $this->_oGroups ) ) {
00290             return $this->_oGroups;
00291         }
00292 
00293         if ( !$sOXID ) {
00294             $sOXID = $this->getId();
00295         }
00296 
00297         $sViewName = getViewName( "oxgroups" );
00298         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00299         $sSelect  = "select {$sViewName}.* from {$sViewName} left join oxobject2group on oxobject2group.oxgroupsid = {$sViewName}.oxid
00300                      where oxobject2group.oxobjectid = " . oxDb::getDb()->quote( $sOXID );
00301         $this->_oGroups->selectString( $sSelect );
00302         return $this->_oGroups;
00303     }
00304 
00312     public function getUserAddresses( $sUserId = null )
00313     {
00314         $sUserId = isset( $sUserId ) ? $sUserId : $this->getId();
00315         if ( !isset( $this->_aAddresses[$sUserId] ) ) {
00316             $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId );
00317 
00318             //P
00319             $this->_aAddresses[$sUserId] = oxNew( "oxlist" );
00320             $this->_aAddresses[$sUserId]->init( "oxaddress" );
00321             $this->_aAddresses[$sUserId]->selectString( $sSelect );
00322 
00323             // marking selected
00324             if ( $sAddressId = $this->getSelectedAddressId() ) {
00325                 foreach ( $this->_aAddresses[$sUserId] as $oAddress ) {
00326                     if ( $oAddress->getId() === $sAddressId ) {
00327                         $oAddress->setSelected();
00328                         break;
00329                     }
00330                 }
00331             }
00332         }
00333         return $this->_aAddresses[$sUserId];
00334     }
00335 
00343     public function setSelectedAddressId( $sAddressId )
00344     {
00345         $this->_sSelAddressId = $sAddressId;
00346     }
00347 
00353     public function getSelectedAddressId()
00354     {
00355         if ( $this->_sSelAddressId !== null ) {
00356             return $this->_sSelAddressId;
00357         }
00358 
00359         $sAddressId = oxConfig::getParameter( "oxaddressid");
00360         if ( !$sAddressId && !oxConfig::getParameter( 'reloadaddress' ) ) {
00361             $sAddressId = oxSession::getVar( "deladrid" );
00362         }
00363         return $sAddressId;
00364     }
00365 
00371     protected function _getWishListId()
00372     {
00373         $this->_sWishId = null;
00374         // check if we have to set it here
00375         $oBasket = $this->getSession()->getBasket();
00376         foreach ( $oBasket->getContents() as $oBasketItem ) {
00377             if ( $this->_sWishId = $oBasketItem->getWishId() ) {
00378                 // stop on first found
00379                 break;
00380             }
00381         }
00382         return $this->_sWishId;
00383     }
00384 
00393     public function getSelectedAddress( $sWishId = false )
00394     {
00395         if ( $this->_oSelAddress !== null ) {
00396             return $this->_oSelAddress;
00397         }
00398 
00399         $oSelectedAddress = null;
00400         $oAddresses = $this->getUserAddresses();
00401         if ( $oAddresses->count() ) {
00402             if ( $sAddressId = $this->getSelectedAddressId() ) {
00403                 foreach ( $oAddresses as $oAddress ) {
00404                     if ( $oAddress->getId() == $sAddressId ) {
00405                         $oAddress->selected = 1;
00406                         $oAddress->setSelected();
00407                         $oSelectedAddress = $oAddress;
00408                         break;
00409                     }
00410                 }
00411             }
00412 
00413             // in case none is set - setting first one
00414             if ( !$oSelectedAddress ) {
00415                 if ( !$sAddressId || $sAddressId >= 0 ) {
00416                     $oAddresses->rewind();
00417                     $oAddress = $oAddresses->current();
00418                 } else {
00419                     $aAddresses = $oAddresses->getArray();
00420                     $oAddress   = array_pop( $aAddresses );
00421                 }
00422                 $oAddress->selected = 1;
00423                 $oAddress->setSelected();
00424                 $oSelectedAddress = $oAddress;
00425             }
00426         }
00427         $this->_oSelAddress = $oSelectedAddress;
00428         return $oSelectedAddress;
00429     }
00430 
00438     public function getUserPayments( $sOXID = null )
00439     {
00440         if ( $this->_oPayments === null ) {
00441 
00442             if ( !$sOXID ) {
00443                 $sOXID = $this->getId();
00444             }
00445 
00446             $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00447 
00448             $this->_oPayments = oxNew( 'oxlist' );
00449             $this->_oPayments->init( 'oxUserPayment' );
00450             $this->_oPayments->selectString( $sSelect );
00451 
00452             $myUtils = oxRegistry::getUtils();
00453             foreach ( $this->_oPayments as $oPayment ) {
00454                 // add custom fields to this class
00455                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00456             }
00457         }
00458 
00459         return $this->_oPayments;
00460     }
00461 
00467     public function save()
00468     {
00469         $myConfig  = oxRegistry::getConfig();
00470 
00471         $blAddRemark = false;
00472         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00473             $blAddRemark = true;
00474             //save oxregister value
00475             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00476         }
00477 
00478         // setting user rights
00479         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00480 
00481         // processing birth date which came from output as array
00482         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00483             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00484         }
00485 
00486         // checking if user Facebook ID should be updated
00487         if ( $myConfig->getConfigParam( "bl_showFbConnect" ) ) {
00488             $oFb = oxRegistry::get("oxFb");
00489             if ( $oFb->isConnected() && $oFb->getUser() ) {
00490                  $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
00491             }
00492         }
00493 
00494         $blRet = parent::save();
00495 
00496         //add registered remark
00497         if ( $blAddRemark && $blRet ) {
00498             $oRemark = oxNew( 'oxremark' );
00499             $oRemark->oxremark__oxtext     = new oxField(oxRegistry::getLang()->translateString( 'usrRegistered', null, true ), oxField::T_RAW);
00500             $oRemark->oxremark__oxtype     = new oxField('r', oxField::T_RAW);
00501             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00502             $oRemark->save();
00503         }
00504 
00505         return $blRet;
00506     }
00507 
00513     public function allowDerivedUpdate()
00514     {
00515         return true;
00516     }
00517 
00525     public function inGroup( $sGroupID )
00526     {
00527         $blIn = false;
00528         if ( ( $oGroups = $this->getUserGroups() ) ) {
00529             $blIn = isset( $oGroups[ $sGroupID ] );
00530         }
00531 
00532         return $blIn;
00533     }
00534 
00543     public function delete( $sOXID = null )
00544     {
00545 
00546         if ( !$sOXID ) {
00547             $sOXID = $this->getId();
00548         }
00549         if ( !$sOXID ) {
00550             return false;
00551         }
00552 
00553         $blDeleted = parent::delete( $sOXID );
00554 
00555         if ( $blDeleted ) {
00556             $oDb = oxDb::getDb();
00557             $sOXIDQuoted = $oDb->quote($sOXID);
00558 
00559             // deleting stored payment, address, group dependencies, remarks info
00560             $rs = $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00561             $rs = $oDb->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00562 
00563             // deleting notice/wish lists
00564             $rs = $oDb->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00565             $rs = $oDb->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00566 
00567             // deleting newsletter subscription
00568             $rs = $oDb->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00569 
00570             // delivery and delivery sets
00571             $rs = $oDb->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00572 
00573             // discounts
00574             $rs = $oDb->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00575 
00576 
00577             // and leaving all order related information
00578             $rs = $oDb->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00579 
00580             $blDeleted = $rs->EOF;
00581         }
00582 
00583         return $blDeleted;
00584     }
00585 
00593     public function load( $oxID )
00594     {
00595 
00596         $blRet = parent::load( $oxID );
00597 
00598         // convert date's to international format
00599         if ( isset( $this->oxuser__oxcreate->value ) ) {
00600             $this->oxuser__oxcreate->setValue(oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxuser__oxcreate->value ));
00601         }
00602 
00603         // change newsSubcription user id
00604         if ( isset($this->_oNewsSubscription) ) {
00605             $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField( $oxID, oxField::T_RAW);
00606         }
00607 
00608         return $blRet;
00609     }
00610 
00618     public function exists( $sOXID = null )
00619     {
00620         $oDb = oxDb::getDb();
00621         if ( !$sOXID ) {
00622             $sOXID = $this->getId();
00623         }
00624 
00625         $sShopSelect = '';
00626         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00627             $sShopSelect = ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00628         }
00629 
00630         //#4543 Query optimisation by splitting it into two, might need an logics optimisation as well
00631         if ( $sOXID ) {
00632             $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00633                     WHERE ( oxid = '.$oDb->quote( $sOXID ).' ) ';
00634             $sSelect .= $sShopSelect;
00635 
00636             if ( ( $sOxid = $oDb->getOne( $sSelect, false, false ) ) ) {
00637                 // update - set oxid
00638                 $this->setId( $sOxid );
00639                 return true;
00640             }
00641         }
00642 
00643         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00644                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).' ) ';
00645         $sSelect .= $sShopSelect;
00646 
00647         if ( ( $sOxid = $oDb->getOne( $sSelect, false, false ) ) ) {
00648              // update - set oxid
00649             $this->setId( $sOxid );
00650             return true;
00651         }
00652         return false;
00653     }
00654 
00663     public function getOrders( $iLimit = false, $iPage = 0 )
00664     {
00665         $oOrders = oxNew( 'oxlist' );
00666         $oOrders->init( 'oxorder' );
00667 
00668         if ( $iLimit !== false ) {
00669             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00670         }
00671 
00672         //P
00673         // Lists does not support loading from two tables, so orders
00674         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00675         // forcing to load product info which is used in templates
00676         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00677 
00678         //loading order for registered user
00679         if ( $this->oxuser__oxregister->value > 1 ) {
00680             $oDb = oxDb::getDb();
00681             $sQ = 'select * from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' and oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value ) . ' ';
00682 
00683             //#1546 - shopid check added, if it is not multishop
00684 
00685             $sQ .= ' order by oxorderdate desc ';
00686             $oOrders->selectString( $sQ );
00687         }
00688 
00689         return $oOrders;
00690     }
00691 
00697     public function getOrderCount()
00698     {
00699         $iCnt = 0;
00700         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00701             $oDb = oxDb::getDb();
00702             $sQ  = 'select count(*) from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00703             $iCnt = (int) $oDb->getOne( $sQ );
00704         }
00705 
00706         return $iCnt;
00707     }
00708 
00714     public function getNoticeListArtCnt()
00715     {
00716         if ( $this->_iCntNoticeListArticles === null ) {
00717             $this->_iCntNoticeListArticles = 0;
00718             if ( $this->getId() ) {
00719                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00720             }
00721         }
00722         return $this->_iCntNoticeListArticles;
00723     }
00724 
00730     public function getWishListArtCnt()
00731     {
00732         if ( $this->_iCntWishListArticles === null ) {
00733             $this->_iCntWishListArticles = false;
00734             if ( $this->getId() ) {
00735                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00736             }
00737         }
00738         return $this->_iCntWishListArticles;
00739     }
00740 
00746     public function getEncodedDeliveryAddress()
00747     {
00748         return md5($this->_getMergedAddressFields());
00749     }
00750 
00757     public function getActiveCountry()
00758     {
00759         $sDeliveryCountry = '';
00760         $soxAddressId = oxSession::getVar( 'deladrid' );
00761         if ( $soxAddressId ) {
00762             $oDelAddress = oxNew( 'oxaddress' );
00763             $oDelAddress->load( $soxAddressId );
00764             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00765         } elseif ( $this->getId() ) {
00766             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00767         } else {
00768             $oUser = oxNew( 'oxuser' );
00769             if ( $oUser->loadActiveUser() ) {
00770                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00771             }
00772         }
00773 
00774         return $sDeliveryCountry;
00775     }
00776 
00784     public function createUser()
00785     {
00786         $oDb = oxDb::getDb();
00787         $sShopID = $this->getConfig()->getShopId();
00788 
00789         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00790         $sSelect = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00791         if ( !$this->_blMallUsers ) {
00792             $sSelect .= " and oxshopid = '{$sShopID}' ";
00793         }
00794         $sOXID = $oDb->getOne( $sSelect, false, false );
00795 
00796         // user without password found - lets use
00797         if ( isset( $sOXID ) && $sOXID ) {
00798             // try to update
00799             $this->delete( $sOXID );
00800         } elseif ( $this->_blMallUsers ) {
00801             // must be sure if there is no dublicate user
00802             $sQ = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00803             if ( $oDb->getOne( $sQ, false, false ) ) {
00804                 $oEx = oxNew( 'oxUserException' );
00805                 $oLang = oxRegistry::getLang();
00806                 $oEx->setMessage( sprintf( $oLang->translateString( 'ERROR_MESSAGE_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00807                 throw $oEx;
00808             }
00809         }
00810 
00811         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00812         if ( ( $blOK = $this->save() ) ) {
00813             // dropping/cleaning old delivery address/payment info
00814             $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00815             $oDb->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00816         } else {
00817             $oEx = oxNew( 'oxUserException' );
00818             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00819             throw $oEx;
00820         }
00821 
00822         return $blOK;
00823     }
00824 
00832     public function addToGroup( $sGroupID )
00833     {
00834         if ( !$this->inGroup( $sGroupID ) ) {
00835             // create oxgroup object
00836             $oGroup = oxNew('oxGroups');
00837             if ( $oGroup->load($sGroupID) ) {
00838                 $oNewGroup = oxNew( 'oxobject2group' );
00839                 $oNewGroup->oxobject2group__oxobjectid = new oxField( $this->getId(), oxField::T_RAW );
00840                 $oNewGroup->oxobject2group__oxgroupsid = new oxField( $sGroupID, oxField::T_RAW );
00841                 if ( $oNewGroup->save() ) {
00842                     $this->_oGroups[$sGroupID] = $oGroup;
00843                     return true;
00844                 }
00845             }
00846         }
00847         return false;
00848     }
00849 
00857     public function removeFromGroup( $sGroupID = null )
00858     {
00859         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00860             $oGroups = oxNew( 'oxlist' );
00861             $oGroups->init( 'oxobject2group' );
00862             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00863             $oGroups->selectString( $sSelect );
00864             foreach ( $oGroups as $oRemgroup ) {
00865                 if ( $oRemgroup->delete() ) {
00866                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00867                 }
00868             }
00869         }
00870     }
00871 
00880     public function onOrderExecute( $oBasket, $iSuccess )
00881     {
00882 
00883         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00884             //adding user to particular customer groups
00885             if ( !$this->oxuser__oxdisableautogrp->value ) {
00886 
00887                 $myConfig = $this->getConfig();
00888                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00889                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00890 
00891                 $this->addToGroup( 'oxidcustomer' );
00892                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00893                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00894                     $this->addToGroup( 'oxidsmallcust' );
00895                 }
00896                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00897                     $this->addToGroup( 'oxidmiddlecust' );
00898                 }
00899                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00900                     $this->addToGroup( 'oxidgoodcust' );
00901                 }
00902             }
00903 
00904             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00905                 $this->removeFromGroup( 'oxidnotyetordered' );
00906             }
00907         }
00908     }
00909 
00917     public function getBasket( $sName )
00918     {
00919         if ( !isset( $this->_aBaskets[$sName] ) ) {
00920             $oBasket = oxNew( 'oxuserbasket' );
00921             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00922 
00923             // creating if it does not exist
00924             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00925                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00926                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00927 
00928                 // marking basket as new (it will not be saved in DB yet)
00929                 $oBasket->setIsNewBasket();
00930             }
00931 
00932             $this->_aBaskets[$sName] = $oBasket;
00933         }
00934 
00935         return $this->_aBaskets[$sName];
00936     }
00937 
00946     public function convertBirthday( $aData )
00947     {
00948 
00949         // preparing data to process
00950         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00951         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00952         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00953 
00954         // leaving empty if not set
00955         if ( !$iYear && !$iMonth && !$iDay )
00956             return "";
00957 
00958         // year
00959         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00960             $iYear = date('Y');
00961 
00962         // month
00963         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00964             $iMonth = 1;
00965 
00966         // maximum nuber of days in month
00967         $iMaxDays = 31;
00968         switch( $iMonth) {
00969             case 2 :
00970                 if ($iMaxDays > 28)
00971                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00972                 break;
00973             case 4  :
00974             case 6  :
00975             case 9  :
00976             case 11 :
00977                 $iMaxDays = min(30, $iMaxDays);
00978                 break;
00979         }
00980 
00981         // day
00982         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00983             $iDay = 1;
00984         }
00985 
00986         // whole date
00987         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00988     }
00989 
00995     public function getBoni()
00996     {
00997         if ( ! $iBoni = $this->getConfig()->getConfigParam( 'iCreditRating' ) ) {
00998             $iBoni = 1000;
00999         }
01000 
01001         return $iBoni;
01002     }
01003 
01016     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
01017     {
01018         // preparing input
01019         $sDynGoup = strtolower( trim( $sDynGoup ) );
01020 
01021         // setting denied groups from admin settings also
01022         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
01023 
01024         // default state ..
01025         $blAdd = false;
01026 
01027         // user assignment to dyn group is not allowed
01028         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
01029             $blAdd = false;
01030         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
01031             // trying to add user to prohibited user group?
01032             $blAdd = false;
01033         } elseif ( $this->addToGroup( $sDynGoup ) ) {
01034             $blAdd = true;
01035         }
01036 
01037         // cleanup
01038         oxSession::deleteVar( 'dgr' );
01039 
01040         return $blAdd;
01041     }
01042 
01061     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01062     {
01063         // 1. checking user name
01064         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01065 
01066         // 2. cheking email
01067         $this->_checkEmail( $sLogin );
01068 
01069         // 3. password
01070         $this->checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01071 
01072         // 4. required fields
01073         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01074 
01075         // 5. country check
01076         $this->_checkCountries( $aInvAddress, $aDelAddress );
01077 
01078         // 6. vat id check.
01079             $this->_checkVatId( $aInvAddress );
01080 
01081 
01082         // throwing first validation error
01083         if ( $oError = oxRegistry::get("oxInputValidator")->getFirstValidationError() ) {
01084             throw $oError;
01085         }
01086     }
01087 
01097     public function setNewsSubscription( $blSubscribe, $blSendOptIn, $blForceCheckOptIn = false )
01098     {
01099         // assigning to newsletter
01100         $blSuccess = false;
01101         $myConfig  = $this->getConfig();
01102         $mySession = $this->getSession();
01103 
01104         // user wants to get newsletter messages or no ?
01105         $oNewsSubscription = $this->getNewsSubscription();
01106         if ( $oNewsSubscription ) {
01107             if ( $blSubscribe && ($blForceCheckOptIn || ( $iOptInStatus = $oNewsSubscription->getOptInStatus() ) != 1) ) {
01108                 if ( !$blSendOptIn ) {
01109 
01110                     // double-opt-in check is disabled - assigning automatically
01111                     $this->addToGroup( 'oxidnewsletter' );
01112                     // and setting subscribed status
01113                     $oNewsSubscription->setOptInStatus( 1 );
01114                     $blSuccess = true;
01115                 } else {
01116 
01117                     // double-opt-in check enabled - sending confirmation email and setting waiting status
01118                     if ( $iOptInStatus != 2 ) {
01119                         // sending double-opt-in mail
01120                         $oEmail = oxNew( 'oxemail' );
01121                         $blSuccess = $oEmail->sendNewsletterDbOptInMail( $this );
01122                     } else {
01123                         // mail already was sent, so just confirming that
01124                         $blSuccess = true;
01125                     }
01126 
01127                     $oNewsSubscription->setOptInStatus( 2 );
01128                 }
01129             } elseif ( !$blSubscribe ) {
01130                 // removing user from newsletter subscribers
01131                 $this->removeFromGroup( 'oxidnewsletter' );
01132                 $oNewsSubscription->setOptInStatus( 0 );
01133                 $blSuccess = true;
01134             }
01135         }
01136 
01137         return $blSuccess;
01138     }
01139 
01157     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01158     {
01159 
01160         // validating values before saving. If validation fails - exception is thrown
01161         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01162         // input data is fine - lets save updated user info
01163 
01164 
01165 
01166         $this->assign( $aInvAddress );
01167 
01168 
01169 
01170 
01171 
01172         // update old or add new delivery address
01173         $this->_assignAddress( $aDelAddress );
01174 
01175 
01176 
01177 
01178         // saving new values
01179         if ( $this->save() ) {
01180 
01181             // assigning automatically to specific groups
01182             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01183             $this->_setAutoGroups( $sCountryId );
01184         }
01185     }
01186 
01192     protected function _getMergedAddressFields()
01193     {
01194         $sDelAddress = '';
01195         $sDelAddress .= $this->oxuser__oxcompany;
01196         $sDelAddress .= $this->oxuser__oxusername;
01197         $sDelAddress .= $this->oxuser__oxfname;
01198         $sDelAddress .= $this->oxuser__oxlname;
01199         $sDelAddress .= $this->oxuser__oxstreet;
01200         $sDelAddress .= $this->oxuser__oxstreetnr;
01201         $sDelAddress .= $this->oxuser__oxaddinfo;
01202         $sDelAddress .= $this->oxuser__oxustid;
01203         $sDelAddress .= $this->oxuser__oxcity;
01204         $sDelAddress .= $this->oxuser__oxcountryid;
01205         $sDelAddress .= $this->oxuser__oxstateid;
01206         $sDelAddress .= $this->oxuser__oxzip;
01207         $sDelAddress .= $this->oxuser__oxfon;
01208         $sDelAddress .= $this->oxuser__oxfax;
01209         $sDelAddress .= $this->oxuser__oxsal;
01210 
01211         return $sDelAddress;
01212     }
01213 
01221     protected function _assignAddress( $aDelAddress )
01222     {
01223         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01224 
01225             $sAddressId = $this->getConfig()->getRequestParameter( 'oxaddressid' );
01226             $sAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01227 
01228             $oAddress = oxNew( 'oxaddress' );
01229             $oAddress->setId( $sAddressId );
01230             $oAddress->load( $sAddressId );
01231             $oAddress->assign( $aDelAddress );
01232             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01233             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01234             $oAddress->save();
01235 
01236             // resetting addresses
01237             $this->_aAddresses = null;
01238 
01239             // saving delivery Address for later use
01240             oxSession::setVar( 'deladrid', $oAddress->getId() );
01241         } else {
01242             // resetting
01243             oxSession::setVar( 'deladrid', null );
01244         }
01245     }
01246 
01257     protected function _getLoginQuery( $sUser, $sPassword, $sShopID, $blAdmin )
01258     {
01259         $myConfig = $this->getConfig();
01260         $oDb = oxDb::getDb();
01261 
01262         $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01263         $sPassSelect = " oxuser.oxpassword = BINARY MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01264         $sShopSelect = "";
01265 
01266 
01267         // admin view: can only login with higher than 'user' rights
01268         if ( $blAdmin ) {
01269             $sShopSelect = " and ( oxrights != 'user' ) ";
01270         }
01271 
01272         $blStagingMode = false;
01273         $blDemoMode = false;
01274         $sWhat = "oxid";
01275             if ($myConfig->isDemoShop()) {
01276                 $blDemoMode = true;
01277             }
01278 
01279         $sSelect = "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01280         if ( ( $blDemoMode || $blStagingMode ) && $blAdmin ) {
01281             if ( $sPassword == "admin" && $sUser == "admin" ) {
01282                 $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' ";
01283             } elseif ( $blDemoMode ) {
01284                 $oEx = oxNew( 'oxUserException' );
01285                 $oEx->setMessage( 'ERROR_MESSAGE_USER_NOVALIDLOGIN' );
01286                 throw $oEx;
01287             }
01288         }
01289 
01290         return $sSelect;
01291     }
01292 
01302     protected function _getShopSelect( $myConfig, $sShopID, $blAdmin )
01303     {
01304         $sShopSelect = "";
01305         // admin view: can only login with higher than 'user' rights
01306         if ( $blAdmin ) {
01307             $sShopSelect = " and ( oxrights != 'user' ) ";
01308         }
01309 
01310         return $sShopSelect;
01311     }
01319     protected function _loadSavedUserBasketAfterLogin()
01320     {
01321         if ( !$this->isAdmin() && !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' )) {
01322             //load basket from the database
01323             try {
01324                 if ( $oBasket = $this->getSession()->getBasket() ) {
01325                     $oBasket->load();
01326                 }
01327             } catch ( Exception $oE ) {
01328                 //just ignore it
01329             }
01330         }
01331     }
01332 
01333 
01346     public function login( $sUser, $sPassword, $blCookie = false)
01347     {
01348         if ( $this->isAdmin() && !count( oxRegistry::get("oxUtilsServer")->getOxCookie() ) ) {
01349             $oEx = oxNew( 'oxCookieException' );
01350             $oEx->setMessage( 'ERROR_MESSAGE_COOKIE_NOCOOKIE' );
01351             throw $oEx;
01352         }
01353 
01354         $oConfig = $this->getConfig();
01355         if ( $sPassword ) {
01356 
01357             $sShopID = $oConfig->getShopId();
01358             $sSelect = $this->_getLoginQuery( $sUser, $sPassword, $sShopID, $this->isAdmin() );
01359 
01360             // load from DB
01361             $aData = oxDb::getDb()->getAll( $sSelect );
01362             $sOXID = @$aData[0][0];
01363             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01364 
01365                 if ( !$this->load( $sOXID ) ) {
01366                     $oEx = oxNew( 'oxUserException' );
01367                     $oEx->setMessage( 'ERROR_MESSAGE_USER_NOVALIDLOGIN' );
01368                     throw $oEx;
01369                 }
01370             }
01371         }
01372 
01373 
01374         //login successful?
01375         if ( $this->oxuser__oxid->value ) {
01376             // yes, successful login
01377 
01378             //resetting active user
01379             $this->setUser( null );
01380 
01381             if ( $this->isAdmin() ) {
01382                 oxRegistry::getSession()->setVariable( 'auth', $this->oxuser__oxid->value );
01383             } else {
01384                 oxRegistry::getSession()->setVariable( 'usr', $this->oxuser__oxid->value );
01385             }
01386 
01387             // cookie must be set ?
01388             if ( $blCookie && $oConfig->getConfigParam( 'blShowRememberMe' ) ) {
01389                 oxRegistry::get("oxUtilsServer")->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $oConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value );
01390             }
01391 
01392             return true;
01393         } else {
01394             $oEx = oxNew( 'oxUserException' );
01395             $oEx->setMessage( 'ERROR_MESSAGE_USER_NOVALIDLOGIN' );
01396             throw $oEx;
01397         }
01398     }
01399 
01405     public function logout()
01406     {
01407         // deleting session info
01408         oxSession::deleteVar( 'usr' );  // for front end
01409         oxSession::deleteVar( 'auth' ); // for back end
01410         oxSession::deleteVar( 'dgr' );
01411         oxSession::deleteVar( 'dynvalue' );
01412         oxSession::deleteVar( 'paymentid' );
01413         // oxSession::deleteVar( 'deladrid' );
01414 
01415         // delete cookie
01416         oxRegistry::get("oxUtilsServer")->deleteUserCookie( $this->getConfig()->getShopID() );
01417 
01418         // unsetting global user
01419         $this->setUser( null );
01420 
01421         return true;
01422     }
01423 
01430     public function loadAdminUser()
01431     {
01432         return $this->loadActiveUser( true );
01433     }
01434 
01443     public function loadActiveUser( $blForceAdmin = false )
01444     {
01445         $oConfig = $this->getConfig();
01446 
01447         $blAdmin = $this->isAdmin() || $blForceAdmin;
01448 
01449         // first - checking session info
01450         $sUserID = $blAdmin ? oxRegistry::getSession()->getVariable( 'auth' ) : oxRegistry::getSession()->getVariable( 'usr' );
01451 
01452         // trying automatic login (by 'remember me' cookie)
01453         $blFoundInCookie = false;
01454         if ( !$sUserID && !$blAdmin && $oConfig->getConfigParam('blShowRememberMe') ) {
01455             $sUserID = $this->_getCookieUserId();
01456             $blFoundInCookie = $sUserID? true : false;
01457         }
01458 
01459         // If facebook connection is enabled, trying to login user using Facebook ID
01460         if ( !$sUserID && !$blAdmin && $oConfig->getConfigParam( "bl_showFbConnect") ) {
01461             $sUserID = $this->_getFacebookUserId();
01462         }
01463 
01464         // checking user results
01465         if ( $sUserID ) {
01466             if ( $this->load( $sUserID ) ) {
01467                 // storing into session
01468                 if ($blAdmin) {
01469                     oxRegistry::getSession()->setVariable( 'auth', $sUserID );
01470                 } else {
01471                     oxRegistry::getSession()->setVariable( 'usr', $sUserID );
01472                 }
01473 
01474                 // marking the way user was loaded
01475                 $this->_blLoadedFromCookie = $blFoundInCookie;
01476                 return true;
01477             }
01478         } else {
01479             // no user
01480             if ($blAdmin) {
01481                 oxRegistry::getSession()->deleteVariable( 'auth' );
01482             } else {
01483                 oxRegistry::getSession()->deleteVariable( 'usr' );
01484             }
01485 
01486             return false;
01487         }
01488     }
01489 
01495     protected function _getFacebookUserId()
01496     {
01497         $oDb = oxDb::getDb();
01498         $oFb = oxRegistry::get("oxFb");
01499         $oConfig = $this->getConfig();
01500         if ( $oFb->isConnected() && $oFb->getUser() ) {
01501             $sUserSelect = "oxuser.oxfbid = " . $oDb->quote( $oFb->getUser() );
01502             $sShopSelect = "";
01503 
01504 
01505             $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01506             $sUserID = $oDb->getOne( $sSelect );
01507         }
01508         return $sUserID;
01509     }
01510 
01516     protected function _getCookieUserId()
01517     {
01518         $sUserID = null;
01519         $oConfig = $this->getConfig();
01520         $sShopID = $oConfig->getShopId();
01521         if ( ( $sSet = oxRegistry::get("oxUtilsServer")->getUserCookie( $sShopID ) ) ) {
01522             $oDb = oxDb::getDb();
01523             $aData = explode( '@@@', $sSet );
01524             $sUser = $aData[0];
01525             $sPWD  = @$aData[1];
01526 
01527             $sSelect =  'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDb->quote($sUser);
01528 
01529             $rs = $oDb->select( $sSelect );
01530             if ( $rs != false && $rs->recordCount() > 0 ) {
01531                 while (!$rs->EOF) {
01532                     $sTest = crypt( $rs->fields[1], $rs->fields[2] );
01533                     if ( $sTest == $sPWD ) {
01534                         // found
01535                         $sUserID = $rs->fields[0];
01536                         break;
01537                     }
01538                     $rs->moveNext();
01539                 }
01540             }
01541             // if cookie info is not valid, remove it.
01542             if ( !$sUserID ) {
01543                 oxRegistry::get('oxUtilsServer')->deleteUserCookie( $sShopID );
01544             }
01545         }
01546         return $sUserID;
01547     }
01548 
01561     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01562     {
01563         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01564         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01565 
01566         // maybe this is LDAP user but supplied email Address instead of LDAP login
01567         $oDb = oxDb::getDb();
01568         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01569         if ( isset( $sLDAPKey) && $sLDAPKey) {
01570             $sUser = $sLDAPKey;
01571         }
01572 
01573         //$throws oxConnectionException
01574         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01575 
01576         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01577         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01578             // login successful
01579 
01580             // check if user is already in database
01581             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01582             $sOXID = $oDb->getOne( $sSelect );
01583 
01584             if ( !isset( $sOXID ) || !$sOXID ) {
01585                 // we need to create a new user
01586                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01587                 $this->setId();
01588 
01589                 // map all user data fields
01590                 foreach ( $aData as $fldname => $value) {
01591                     $sField = "oxuser__".strtolower( $fldname);
01592                     $this->$sField = new oxField( $aData[$fldname] );
01593                 }
01594 
01595                 $this->oxuser__oxactive  = new oxField( 1 );
01596                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01597                 $this->oxuser__oxldapkey = new oxField( $sUser );
01598                 $this->oxuser__oxrights  = new oxField( "user" );
01599                 $this->setPassword( "ldap user" );
01600 
01601                 $this->save();
01602             } else {
01603                 // LDAP user is already in OXID DB, load it
01604                 $this->load( $sOXID);
01605             }
01606 
01607         } else {
01608             $oEx = oxNew( 'oxUserException' );
01609             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01610             throw $oEx;
01611         }
01612     }
01613 
01620     protected function _getUserRights()
01621     {
01622         // previously user had no rights defined
01623         if ( !$this->oxuser__oxrights->value )
01624             return 'user';
01625 
01626         $oDb = oxDb::getDb();
01627         $myConfig    = $this->getConfig();
01628         $sAuthRights = null;
01629 
01630         // choosing possible user rights index
01631         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01632         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01633         if ( $sAuthUserID ) {
01634             $sAuthRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $sAuthUserID ) );
01635         }
01636 
01637         //preventing user rights edit for non admin
01638         $aRights = array();
01639 
01640         // selecting current users rights ...
01641         if ( $sCurrRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $this->getId() ) ) ) {
01642             $aRights[] = $sCurrRights;
01643         }
01644         $aRights[] = 'user';
01645 
01646         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01647             return current( $aRights );
01648         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01649             $aRights[] = $sAuthRights;
01650             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01651                 return current( $aRights );
01652             }
01653         }
01654 
01655         // leaving as it was set ...
01656         return $this->oxuser__oxrights->value;
01657     }
01658 
01664     protected function _insert()
01665     {
01666 
01667         // set oxcreate date
01668         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01669 
01670         if ( !isset( $this->oxuser__oxboni->value ) ) {
01671             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01672         }
01673 
01674         return parent::_insert();
01675     }
01676 
01682     protected function _update()
01683     {
01684         //V #M418: for not registered users, don't change boni during update
01685         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01686             $this->_aSkipSaveFields[] = 'oxboni';
01687         }
01688 
01689         // don't change this field
01690         $this->_aSkipSaveFields[] = 'oxcreate';
01691         if ( !$this->isAdmin() ) {
01692             $this->_aSkipSaveFields[] = 'oxcustnr';
01693             $this->_aSkipSaveFields[] = 'oxrights';
01694         }
01695 
01696         // updating subscription information
01697         if ( ( $blUpdate = parent::_update() ) ) {
01698             $this->getNewsSubscription()->updateSubscription( $this );
01699         }
01700 
01701         return $blUpdate;
01702     }
01703 
01719     protected function _checkLogin( $sLogin, $aInvAddress )
01720     {
01721         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01722         oxRegistry::get("oxInputValidator")->checkLogin( $this, $sLogin, $aInvAddress );
01723         return $sLogin;
01724     }
01725 
01733     public function checkIfEmailExists( $sEmail )
01734     {
01735         $myConfig = $this->getConfig();
01736         $oDb = oxDb::getDb();
01737         $iShopId = $myConfig->getShopId();
01738         $blExists = false;
01739 
01740         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDb->quote( $sEmail );
01741         if ( ( $sOxid = $this->getId() ) ) {
01742             $sQ .= " and oxid <> ".$oDb->quote( $sOxid );
01743         }
01744         $oRs = $oDb->select( $sQ, false, false );
01745         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01746 
01747             if ( $this->_blMallUsers ) {
01748 
01749                 $blExists = true;
01750                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01751 
01752                     // password is not set - allow to override
01753                     $blExists = false;
01754                 }
01755             } else {
01756 
01757                 $blExists = false;
01758                 while ( !$oRs->EOF ) {
01759                     if ( $oRs->fields[1] != 'user' ) {
01760 
01761                         // exists admin with same login - must not allow
01762                         $blExists = true;
01763                         break;
01764                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01765 
01766                         // exists same login (with password) in same shop
01767                         $blExists = true;
01768                         break;
01769                     }
01770 
01771                     $oRs->moveNext();
01772                 }
01773             }
01774         }
01775         return $blExists;
01776     }
01777 
01785     public function getUserRecommLists( $sOXID = null )
01786     {
01787         if ( !$sOXID )
01788             $sOXID = $this->getId();
01789 
01790         // sets active page
01791         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01792         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01793 
01794         // load only lists which we show on screen
01795         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01796         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01797 
01798 
01799         $oRecommList = oxNew( 'oxlist' );
01800         $oRecommList->init( 'oxrecommlist' );
01801         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01802         $iShopId = $this->getConfig()->getShopId();
01803         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01804         $oRecommList->selectString( $sSelect );
01805 
01806         return $oRecommList;
01807     }
01808 
01816     public function getRecommListsCount( $sOx = null )
01817     {
01818         if ( !$sOx ) {
01819             $sOx = $this->getId();
01820         }
01821 
01822         if ( $this->_iCntRecommLists === null || $sOx ) {
01823             $oDb = oxDb::getDb();
01824             $this->_iCntRecommLists = 0;
01825             $iShopId = $this->getConfig()->getShopId();
01826             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01827             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01828         }
01829         return $this->_iCntRecommLists;
01830     }
01831 
01842     protected function _checkEmail( $sEmail )
01843     {
01844         oxRegistry::get("oxInputValidator")->checkEmail( $this, $sEmail );
01845     }
01846 
01859     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01860     {
01861         return oxRegistry::get("oxInputValidator")->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01862     }
01863 
01874     protected function _checkCountries( $aInvAddress, $aDelAddress )
01875     {
01876         oxRegistry::get("oxInputValidator")->checkCountries( $this, $aInvAddress, $aDelAddress );
01877     }
01878 
01890     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01891     {
01892         oxRegistry::get("oxInputValidator")->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01893     }
01894 
01905     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01906     {
01907         oxRegistry::get("oxInputValidator")->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01908     }
01909 
01920     protected function _checkVatId( $aInvAddress )
01921     {
01922         oxRegistry::get("oxInputValidator")->checkVatId( $this, $aInvAddress );
01923     }
01924 
01933     protected function _setAutoGroups( $sCountryId )
01934     {
01935         // assigning automatically to specific groups
01936         $blForeigner = true;
01937         $blForeignGroupExists = false;
01938         $blInlandGroupExists = false;
01939 
01940         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01941         // foreigner ?
01942         if ( is_array($aHomeCountry)) {
01943             if (in_array($sCountryId, $aHomeCountry)) {
01944                 $blForeigner = false;
01945             }
01946         } elseif ($sCountryId == $aHomeCountry) {
01947             $blForeigner = false;
01948         }
01949 
01950         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01951             $blForeignGroupExists = true;
01952             if ( !$blForeigner ) {
01953                 $this->removeFromGroup( 'oxidforeigncustomer' );
01954             }
01955         }
01956 
01957         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01958             $blInlandGroupExists = true;
01959             if ( $blForeigner ) {
01960                 $this->removeFromGroup( 'oxidnewcustomer' );
01961             }
01962         }
01963 
01964         if ( !$this->oxuser__oxdisableautogrp->value ) {
01965             if ( !$blForeignGroupExists && $blForeigner ) {
01966                 $this->addToGroup( 'oxidforeigncustomer' );
01967             }
01968             if ( !$blInlandGroupExists && !$blForeigner ) {
01969                 $this->addToGroup( 'oxidnewcustomer' );
01970             }
01971         }
01972     }
01973 
01974 
01983     public function loadUserByUpdateId( $sUid )
01984     {
01985         $oDb = oxDb::getDb();
01986         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
01987         if ( $sUserId = $oDb->getOne( $sQ ) ) {
01988             return $this->load( $sUserId );
01989         }
01990     }
01991 
01999     public function setUpdateKey( $blReset = false )
02000     {
02001         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02002         $iUpTime = $blReset ? 0 : oxRegistry::get("oxUtilsDate")->getTime() + $this->getUpdateLinkTerm();
02003 
02004         // generating key
02005         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02006 
02007         // setting expiration time for 6 hours
02008         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02009 
02010         // saving
02011         $this->save();
02012     }
02013 
02019     public function getUpdateLinkTerm()
02020     {
02021         return 3600 * 6;
02022     }
02023 
02031     public function isExpiredUpdateId( $sKey )
02032     {
02033         $oDb = oxDb::getDb();
02034         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02035         return !( (bool) $oDb->getOne( $sQ ) );
02036     }
02037 
02043     public function getUpdateId()
02044     {
02045         if ( $this->_sUpdateKey === null ) {
02046             $this->setUpdateKey();
02047             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02048         }
02049         return $this->_sUpdateKey;
02050     }
02051 
02060     public function encodePassword( $sPassword, $sSalt )
02061     {
02062         $oDb = oxDb::getDb();
02063         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02064     }
02065 
02073     public function prepareSalt( $sSalt )
02074     {
02075         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02076     }
02077 
02085     public function decodeSalt( $sSaltHex )
02086     {
02087         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02088     }
02089 
02097     public function setPassword( $sPassword = null )
02098     {
02099         // setting salt if password is not empty
02100         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02101 
02102         // encoding only if password was not empty (e.g. user registration without pass)
02103         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02104 
02105         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02106         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02107     }
02108 
02116     public function isSamePassword( $sNewPass )
02117     {
02118         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02119     }
02120 
02126     public function isLoadedFromCookie()
02127     {
02128         return $this->_blLoadedFromCookie;
02129     }
02130 
02137     public function getPasswordHash()
02138     {
02139         $sHash = null;
02140         if ( $this->oxuser__oxpassword->value ) {
02141             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02142                 // decodable pass ?
02143                 $this->setPassword( oxRegistry::getUtils()->strRem( $this->oxuser__oxpassword->value ) );
02144             } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02145                 // plain pass ?
02146                 $this->setPassword( $this->oxuser__oxpassword->value );
02147             }
02148             $sHash = $this->oxuser__oxpassword->value;
02149         }
02150         return $sHash;
02151     }
02152 
02160     public function getReviewUserHash( $sUserId )
02161     {
02162         $oDb = oxDb::getDb();
02163         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02164         return $sReviewUserHash;
02165     }
02166 
02174     public function getReviewUserId( $sReviewUserHash )
02175     {
02176         $oDb = oxDb::getDb();
02177         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02178         return $sUserId;
02179     }
02180 
02186     public function getState()
02187     {
02188         return $this->oxuser__oxstateid->value;
02189     }
02190 
02196     public function isTermsAccepted()
02197     {
02198         $oDb = oxDb::getDb();
02199         $sShopId = $this->getConfig()->getShopId();
02200         $sUserId = $oDb->quote( $this->getId() );
02201         return (bool) $oDb->getOne( "select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'" );
02202     }
02203 
02209     public function acceptTerms()
02210     {
02211         $oDb = oxDb::getDb();
02212         $sUserId  = $oDb->quote( $this->getId() );
02213         $sShopId  = $this->getConfig()->getShopId();
02214         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02215 
02216         $oDb->execute( "replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02217     }
02218 
02228     public function setCreditPointsForRegistrant( $sUserId, $sRecEmail )
02229     {
02230         $blSet   = false;
02231         $oDb = oxDb::getDb();
02232         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02233         // check if this invitation is still not accepted
02234         $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);
02235         if ( $iPoints && $iPending ) {
02236             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02237             if ( $blSet = $this->save() ) {
02238                 // updating users statistics
02239                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail ) );
02240                 $oInvUser = oxNew( "oxuser" );
02241                 if ( $oInvUser->load( $sUserId ) ) {
02242                     $blSet = $oInvUser->setCreditPointsForInviter();
02243                 }
02244             }
02245         }
02246         oxSession::deleteVar( 'su' );
02247         oxSession::deleteVar( 're' );
02248 
02249         return $blSet;
02250     }
02251 
02257     public function setCreditPointsForInviter()
02258     {
02259         $blSet   = false;
02260         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02261         if ( $iPoints ) {
02262             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02263             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02264             $blSet = $this->save();
02265         }
02266 
02267         return $blSet;
02268     }
02269 
02275     public function updateFbId()
02276     {
02277         $oFb = oxRegistry::get("oxFb");
02278         $blRet = false;
02279 
02280         if ( $oFb->isConnected() && $oFb->getUser() ) {
02281              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02282              $blRet = $this->save();
02283         }
02284 
02285         return $blRet;
02286     }
02287 
02295     public function updateInvitationStatistics( $aRecEmail )
02296     {
02297         $oDb = oxDb::getDb();
02298         $sUserId = $this->getId();
02299 
02300         if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
02301             //iserting statistics about invitation
02302             $sDate = oxRegistry::get("oxUtilsDate")->formatDBDate( date("Y-m-d"), true );
02303             $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
02304             foreach ( $aRecEmail as $sRecEmail ) {
02305                 $sSql = "INSERT INTO oxinvitations SET oxuserid = ".$oDb->quote( $sUserId ).", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02306                 $oDb->execute( $sSql );
02307             }
02308         }
02309     }
02310 
02318     public function getIdByUserName( $sUserName )
02319     {
02320         $oDb = oxDb::getDb();
02321         $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = ". $oDb->quote( $sUserName );
02322         if ( !$this->getConfig()->getConfigParam( 'blMallUsers' ) ) {
02323             $sQ .= " AND `oxshopid` = ". $oDb->quote( $this->getConfig()->getShopId() );
02324         }
02325 
02326         return $oDb->getOne( $sQ );
02327 
02328     }
02329 
02335     public function hasAccount()
02336     {
02337 
02338         return (bool) $this->oxuser__oxpassword->value;
02339 
02340     }
02341 
02347     public function isPriceViewModeNetto()
02348     {
02349         return (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
02350     }
02351 
02352 }