oxuser.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxUser extends oxBase
00009 {
00014     protected $_blDisableShopCheck = true;
00015 
00020     protected $_oNewsSubscription = null;
00021 
00026     protected $_sCoreTbl = 'oxuser';
00027 
00032     protected $_sClassName = 'oxuser';
00033 
00039     protected $_aBaskets = array();
00040 
00046     protected $_oGroups;
00047 
00053     protected $_aAddresses = array();
00054 
00060     protected $_oPayments;
00061 
00067     protected $_oRecommList;
00068 
00074     protected $_blMallUsers = false;
00075 
00081     protected static $_aUserCookie = array();
00082 
00088     protected $_iCntNoticeListArticles = null;
00089 
00095     protected $_iCntWishListArticles = null;
00096 
00102     protected $_iCntRecommLists = null;
00103 
00109     protected $_sUpdateKey = null;
00110 
00116     protected $_blLoadedFromCookie  = null;
00117 
00123     protected $_sSelAddressId = null;
00124 
00130     protected $_oSelAddress = null;
00131 
00137     protected $_sWishId = null;
00138 
00144     protected $_oUserCountryTitle = null;
00145 
00151     public function __construct()
00152     {
00153         $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00154 
00155         parent::__construct();
00156         $this->init( 'oxuser' );
00157     }
00158 
00166     public function setMallUsersStatus( $blOn = false )
00167     {
00168         $this->_blMallUsers = $blOn;
00169     }
00170 
00178     public function __get( $sParamName )
00179     {
00180         // it saves memory using - loads data only if it is used
00181         switch ( $sParamName ) {
00182             case 'oGroups':
00183                 return $this->_oGroups = $this->getUserGroups();
00184                 break;
00185             case 'iCntNoticeListArticles':
00186                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00187                 break;
00188             case 'iCntWishListArticles':
00189                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00190                 break;
00191             case 'iCntRecommLists':
00192                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00193                 break;
00194             case 'oAddresses':
00195                 return $this->getUserAddresses();
00196                 break;
00197             case 'oPayments':
00198                 return $this->_oPayments = $this->getUserPayments();
00199                 break;
00200             case 'oxuser__oxcountry':
00201                 return $this->oxuser__oxcountry = $this->getUserCountry();
00202                 break;
00203             case 'sDBOptin':
00204                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00205                 break;
00206             case 'sEmailFailed':
00207                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00208                 break;
00209         }
00210     }
00211 
00217     public function getNewsSubscription()
00218     {
00219         if ( $this->_oNewsSubscription !== null ) {
00220             return $this->_oNewsSubscription;
00221         }
00222 
00223         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00224 
00225         // if subscription object is not set yet - we should create one
00226         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00227             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00228 
00229                 // no subscription defined yet - creating one
00230                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00231                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00232                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00233                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00234                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00235             }
00236         }
00237 
00238         return $this->_oNewsSubscription;
00239     }
00240 
00250     public function getUserCountry( $sCountryId = null, $iLang = null )
00251     {
00252         if ( $this->_oUserCountryTitle == null || $sCountryId ) {
00253             $sId = $sCountryId ? $sCountryId : $this->oxuser__oxcountryid->value;
00254             $oDb = oxDb::getDb();
00255             $sViewName = getViewName( 'oxcountry', $iLang );
00256             $sQ = "select oxtitle from {$sViewName} where oxid = " . $oDb->quote( $sId ) . " ";
00257             $oCountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00258             if ( !$sCountryId ) {
00259                 $this->_oUserCountryTitle = $oCountry;
00260             }
00261         } else {
00262             return $this->_oUserCountryTitle;
00263         }
00264 
00265         return $oCountry;
00266     }
00267 
00275     public function getUserCountryId( $sCountry = null )
00276     {
00277         $oDb = oxDb::getDb();
00278         $sQ = "select oxid from ".getviewName( "oxcountry" )." where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00279         $sCountryId = $oDb->getOne( $sQ );
00280 
00281         return $sCountryId;
00282     }
00283 
00291     public function getUserGroups( $sOXID = null )
00292     {
00293 
00294         if ( isset( $this->_oGroups ) ) {
00295             return $this->_oGroups;
00296         }
00297 
00298         if ( !$sOXID ) {
00299             $sOXID = $this->getId();
00300         }
00301 
00302         $sViewName = getViewName( "oxgroups" );
00303         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00304         $sSelect  = "select {$sViewName}.* from {$sViewName} left join oxobject2group on oxobject2group.oxgroupsid = {$sViewName}.oxid
00305                      where oxobject2group.oxobjectid = " . oxDb::getDb()->quote( $sOXID );
00306         $this->_oGroups->selectString( $sSelect );
00307         return $this->_oGroups;
00308     }
00309 
00317     public function getUserAddresses( $sUserId = null )
00318     {
00319         $sUserId = isset( $sUserId ) ? $sUserId : $this->getId();
00320         if ( !isset( $this->_aAddresses[$sUserId] ) ) {
00321             $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId );
00322 
00323             //P
00324             $this->_aAddresses[$sUserId] = oxNew( "oxlist" );
00325             $this->_aAddresses[$sUserId]->init( "oxaddress" );
00326             $this->_aAddresses[$sUserId]->selectString( $sSelect );
00327 
00328             // marking selected
00329             if ( $sAddressId = $this->getSelectedAddressId() ) {
00330                 foreach ( $this->_aAddresses[$sUserId] as $oAddress ) {
00331                     if ( $oAddress->getId() === $sAddressId ) {
00332                         $oAddress->setSelected();
00333                         break;
00334                     }
00335                 }
00336             }
00337         }
00338         return $this->_aAddresses[$sUserId];
00339     }
00340 
00348     public function setSelectedAddressId( $sAddressId )
00349     {
00350         $this->_sSelAddressId = $sAddressId;
00351     }
00352 
00358     public function getSelectedAddressId()
00359     {
00360         if ( $this->_sSelAddressId !== null ) {
00361             return $this->_sSelAddressId;
00362         }
00363 
00364         $sAddressId = oxConfig::getParameter( "oxaddressid");
00365         if ( !$sAddressId && !oxConfig::getParameter( 'reloadaddress' ) ) {
00366             $sAddressId = oxSession::getVar( "deladrid" );
00367         }
00368         return $sAddressId;
00369     }
00370 
00376     protected function _getWishListId()
00377     {
00378         $this->_sWishId = null;
00379         // check if we have to set it here
00380         $oBasket = $this->getSession()->getBasket();
00381         foreach ( $oBasket->getContents() as $oBasketItem ) {
00382             if ( $this->_sWishId = $oBasketItem->getWishId() ) {
00383                 // stop on first found
00384                 break;
00385             }
00386         }
00387         return $this->_sWishId;
00388     }
00389 
00398     public function getSelectedAddress( $sWishId = false )
00399     {
00400         if ( $this->_oSelAddress !== null ) {
00401             return $this->_oSelAddress;
00402         }
00403 
00404         $oSelectedAddress = null;
00405         $oAddresses = $this->getUserAddresses();
00406         if ( $oAddresses->count() ) {
00407             if ( $sAddressId = $this->getSelectedAddressId() ) {
00408                 foreach ( $oAddresses as $oAddress ) {
00409                     if ( $oAddress->getId() == $sAddressId ) {
00410                         $oAddress->selected = 1;
00411                         $oAddress->setSelected();
00412                         $oSelectedAddress = $oAddress;
00413                         break;
00414                     }
00415                 }
00416             }
00417 
00418             // in case none is set - setting first one
00419             if ( !$oSelectedAddress ) {
00420                 if ( !$sAddressId || $sAddressId >= 0 ) {
00421                     $oAddresses->rewind();
00422                     $oAddress = $oAddresses->current();
00423                 } else {
00424                     $aAddresses = $oAddresses->getArray();
00425                     $oAddress   = array_pop( $aAddresses );
00426                 }
00427                 $oAddress->selected = 1;
00428                 $oAddress->setSelected();
00429                 $oSelectedAddress = $oAddress;
00430             }
00431         }
00432         $this->_oSelAddress = $oSelectedAddress;
00433         return $oSelectedAddress;
00434     }
00435 
00443     public function getUserPayments( $sOXID = null )
00444     {
00445         if ( $this->_oPayments === null ) {
00446 
00447             if ( !$sOXID ) {
00448                 $sOXID = $this->getId();
00449             }
00450 
00451             $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00452 
00453             $this->_oPayments = oxNew( 'oxlist' );
00454             $this->_oPayments->init( 'oxUserPayment' );
00455             $this->_oPayments->selectString( $sSelect );
00456 
00457             $myUtils = oxUtils::getInstance();
00458             foreach ( $this->_oPayments as $oPayment ) {
00459                 // add custom fields to this class
00460                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00461             }
00462         }
00463 
00464         return $this->_oPayments;
00465     }
00466 
00472     public function save()
00473     {
00474         $myConfig  = oxConfig::getInstance();
00475 
00476         $blAddRemark = false;
00477         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00478             $blAddRemark = true;
00479             //save oxregister value
00480             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00481         }
00482 
00483         // setting user rights
00484         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00485 
00486         // processing birth date which came from output as array
00487         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00488             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00489         }
00490 
00491         // checking if user Facebook ID should be updated
00492         if ( $myConfig->getConfigParam( "bl_showFbConnect" ) ) {
00493             $oFb = oxFb::getInstance();
00494             if ( $oFb->isConnected() && $oFb->getUser() ) {
00495                  $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
00496             }
00497         }
00498 
00499         $blRet = parent::save();
00500 
00501         //add registered remark
00502         if ( $blAddRemark && $blRet ) {
00503             $oRemark = oxNew( 'oxremark' );
00504             $oRemark->oxremark__oxtext     = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00505             $oRemark->oxremark__oxtype     = new oxField('r', oxField::T_RAW);
00506             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00507             $oRemark->save();
00508         }
00509 
00510         return $blRet;
00511     }
00512 
00518     public function allowDerivedUpdate()
00519     {
00520         return true;
00521     }
00522 
00530     public function inGroup( $sGroupID )
00531     {
00532         $blIn = false;
00533         if ( ( $oGroups = $this->getUserGroups() ) ) {
00534             $blIn = isset( $oGroups[ $sGroupID ] );
00535         }
00536 
00537         return $blIn;
00538     }
00539 
00548     public function delete( $sOXID = null )
00549     {
00550 
00551         if ( !$sOXID ) {
00552             $sOXID = $this->getId();
00553         }
00554         if ( !$sOXID ) {
00555             return false;
00556         }
00557 
00558         $blDeleted = parent::delete( $sOXID );
00559 
00560         if ( $blDeleted ) {
00561             $oDB = oxDb::getDb();
00562             $sOXIDQuoted = $oDB->quote($sOXID);
00563 
00564             // deleting stored payment, address, group dependencies, remarks info
00565             $rs = $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00566             $rs = $oDB->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00567 
00568             // deleting notice/wish lists
00569             $rs = $oDB->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00570             $rs = $oDB->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00571 
00572             // deleting newsletter subscription
00573             $rs = $oDB->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00574 
00575             // delivery and delivery sets
00576             $rs = $oDB->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00577 
00578             // discounts
00579             $rs = $oDB->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00580 
00581 
00582             // and leaving all order related information
00583             $rs = $oDB->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00584 
00585             $blDeleted = $rs->EOF;
00586         }
00587 
00588         return $blDeleted;
00589     }
00590 
00598     public function load( $oxID )
00599     {
00600 
00601         $blRet = parent::Load( $oxID );
00602 
00603         // convert date's to international format
00604         if ( isset( $this->oxuser__oxcreate->value ) ) {
00605             $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
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         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00626                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).'';
00627 
00628         if ( $sOXID ) {
00629             $sSelect.= " or oxid = ".$oDb->quote( $sOXID ) . " ) ";
00630         } else {
00631             $sSelect.= ' ) ';
00632         }
00633 
00634         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00635             $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00636         }
00637 
00638         $blExists = false;
00639         if ( ( $sOxid = oxDb::getDb()->getOne( $sSelect ) ) ) {
00640              // update - set oxid
00641             $this->setId( $sOxid );
00642             $blExists = true;
00643         }
00644         return $blExists;
00645     }
00646 
00655     public function getOrders( $iLimit = false, $iPage = 0 )
00656     {
00657         $myConfig = $this->getConfig();
00658         $oOrders = oxNew( 'oxlist' );
00659         $oOrders->init( 'oxorder' );
00660 
00661         if ( $iLimit !== false ) {
00662             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00663         }
00664 
00665         //P
00666         // Lists does not support loading from two tables, so orders
00667         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00668         // forcing to load product info which is used in templates
00669         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00670 
00671         //loading order for registered user
00672         if ( $this->oxuser__oxregister->value > 1 ) {
00673             $sQ = 'select * from oxorder where oxuserid = "'.$this->getId().'" and oxorderdate >= ' . oxDb::getDb()->quote( $this->oxuser__oxregister->value ) . ' ';
00674 
00675             //#1546 - shopid check added, if it is not multishop
00676 
00677             $sQ .= ' order by oxorderdate desc ';
00678             $oOrders->selectString( $sQ );
00679         }
00680 
00681         return $oOrders;
00682     }
00683 
00689     public function getOrderCount()
00690     {
00691         $iCnt = 0;
00692         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00693             $oDb = oxDb::getDb();
00694             $sQ  = 'select count(*) from oxorder where oxuserid = "'.$this->getId().'" AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00695             $iCnt = (int) $oDb->getOne( $sQ );
00696         }
00697 
00698         return $iCnt;
00699     }
00700 
00706     public function getNoticeListArtCnt()
00707     {
00708         if ( $this->_iCntNoticeListArticles === null ) {
00709             $this->_iCntNoticeListArticles = 0;
00710             if ( $this->getId() ) {
00711                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00712             }
00713         }
00714         return $this->_iCntNoticeListArticles;
00715     }
00716 
00722     public function getWishListArtCnt()
00723     {
00724         if ( $this->_iCntWishListArticles === null ) {
00725             $this->_iCntWishListArticles = false;
00726             if ( $this->getId() ) {
00727                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00728             }
00729         }
00730         return $this->_iCntWishListArticles;
00731     }
00732 
00739     public function getActiveCountry()
00740     {
00741         $sDeliveryCountry = '';
00742         $soxAddressId = oxSession::getVar( 'deladrid' );
00743         if ( $soxAddressId ) {
00744             $oDelAddress = oxNew( 'oxaddress' );
00745             $oDelAddress->load( $soxAddressId );
00746             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00747         } elseif ( $this->getId() ) {
00748             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00749         } else {
00750             $oUser = oxNew( 'oxuser' );
00751             if ( $oUser->loadActiveUser() ) {
00752                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00753             }
00754         }
00755 
00756         return $sDeliveryCountry;
00757     }
00758 
00766     public function createUser()
00767     {
00768         $oDB = oxDb::getDb();
00769         $sShopID = $this->getConfig()->getShopId();
00770 
00771         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00772         $sSelect = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00773         if ( !$this->_blMallUsers ) {
00774             $sSelect .= " and oxshopid = '{$sShopID}' ";
00775         }
00776         $sOXID = $oDB->getOne( $sSelect );
00777 
00778         // user without password found - lets use
00779         if ( isset( $sOXID ) && $sOXID ) {
00780             // try to update
00781             $this->delete( $sOXID );
00782         } elseif ( $this->_blMallUsers ) {
00783             // must be sure if there is no dublicate user
00784             $sQ = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00785             if ( $oDB->getOne( $sQ ) ) {
00786                 $oEx = oxNew( 'oxUserException' );
00787                 $oLang = oxLang::getInstance();
00788                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00789                 throw $oEx;
00790             }
00791         }
00792 
00793         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00794         if ( ( $blOK = $this->save() ) ) {
00795             // dropping/cleaning old delivery address/payment info
00796             $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00797             $oDB->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00798         } else {
00799             $oEx = oxNew( 'oxUserException' );
00800             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00801             throw $oEx;
00802         }
00803 
00804         return $blOK;
00805     }
00806 
00814     public function addToGroup( $sGroupID )
00815     {
00816         if ( !$this->inGroup( $sGroupID ) ) {
00817             $oDb = oxDb::getDb();
00818             $sQ = "select 1 from oxgroups where oxid=" . $oDb->quote( $sGroupID );
00819             if ( $oDb->getOne( $sQ ) ) {
00820                 $oNewGroup = oxNew( 'oxobject2group' );
00821                 $oNewGroup->oxobject2group__oxobjectid = new oxField( $this->getId(), oxField::T_RAW );
00822                 $oNewGroup->oxobject2group__oxgroupsid = new oxField( $sGroupID, oxField::T_RAW );
00823                 if ( $oNewGroup->save() ) {
00824                     $this->_oGroups[$sGroupID] = $oNewGroup;
00825                     return true;
00826                 }
00827             }
00828         }
00829         return false;
00830     }
00831 
00839     public function removeFromGroup( $sGroupID = null )
00840     {
00841         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00842             $oGroups = oxNew( 'oxlist' );
00843             $oGroups->init( 'oxobject2group' );
00844             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00845             $oGroups->selectString( $sSelect );
00846             foreach ( $oGroups as $oRemgroup ) {
00847                 if ( $oRemgroup->delete() ) {
00848                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00849                 }
00850             }
00851         }
00852     }
00853 
00862     public function onOrderExecute( $oBasket, $iSuccess )
00863     {
00864 
00865         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00866             //adding user to particular customer groups
00867             if ( !$this->oxuser__oxdisableautogrp->value ) {
00868 
00869                 $myConfig = $this->getConfig();
00870                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00871                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00872 
00873                 $this->addToGroup( 'oxidcustomer' );
00874                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00875                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00876                     $this->addToGroup( 'oxidsmallcust' );
00877                 }
00878                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00879                     $this->addToGroup( 'oxidmiddlecust' );
00880                 }
00881                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00882                     $this->addToGroup( 'oxidgoodcust' );
00883                 }
00884             }
00885 
00886             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00887                 $this->removeFromGroup( 'oxidnotyetordered' );
00888             }
00889         }
00890     }
00891 
00899     public function getBasket( $sName )
00900     {
00901         if ( !isset( $this->_aBaskets[$sName] ) ) {
00902             $oBasket = oxNew( 'oxuserbasket' );
00903             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00904 
00905             // creating if it does not exist
00906             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00907                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00908                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00909 
00910                 // marking basket as new (it will not be saved in DB yet)
00911                 $oBasket->setIsNewBasket();
00912             }
00913 
00914             $this->_aBaskets[$sName] = $oBasket;
00915         }
00916 
00917         return $this->_aBaskets[$sName];
00918     }
00919 
00928     public function convertBirthday( $aData )
00929     {
00930 
00931         // preparing data to process
00932         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00933         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00934         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00935 
00936         // leaving empty if not set
00937         if ( !$iYear && !$iMonth && !$iDay )
00938             return "";
00939 
00940         // year
00941         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00942             $iYear = date('Y');
00943 
00944         // month
00945         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00946             $iMonth = 1;
00947 
00948         // maximum nuber of days in month
00949         $iMaxDays = 31;
00950         switch( $iMonth) {
00951             case 2 :
00952                 if ($iMaxDays > 28)
00953                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00954                 break;
00955             case 4  :
00956             case 6  :
00957             case 9  :
00958             case 11 :
00959                 $iMaxDays = min(30, $iMaxDays);
00960                 break;
00961         }
00962 
00963         // day
00964         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00965             $iDay = 1;
00966         }
00967 
00968         // whole date
00969         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00970     }
00971 
00978     public function getBoni()
00979     {
00980         return 1000;
00981     }
00982 
00995     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00996     {
00997         // preparing input
00998         $sDynGoup = strtolower( trim( $sDynGoup ) );
00999 
01000         // setting denied groups from admin settings also
01001         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
01002 
01003         // default state ..
01004         $blAdd = false;
01005 
01006         // user assignment to dyn group is not allowed
01007         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
01008             $blAdd = false;
01009         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
01010             // trying to add user to prohibited user group?
01011             $blAdd = false;
01012         } elseif ( $this->addToGroup( $sDynGoup ) ) {
01013             $blAdd = true;
01014         }
01015 
01016         // cleanup
01017         oxSession::deleteVar( 'dgr' );
01018 
01019         return $blAdd;
01020     }
01021 
01040     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01041     {
01042         // 1. checking user name
01043         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01044 
01045         // 2. cheking email
01046         $this->_checkEmail( $sLogin );
01047 
01048         // 3. password
01049         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01050 
01051         // 4. required fields
01052         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01053 
01054         // 5. country check
01055         $this->_checkCountries( $aInvAddress, $aDelAddress );
01056 
01057         // 6. vat id check.
01058             $this->_checkVatId( $aInvAddress );
01059 
01060 
01061         // throwing first validation error
01062         if ( $oError = oxInputValidator::getInstance()->getFirstValidationError( $this->getId() ) ) {
01063             throw $oError;
01064         }
01065     }
01066 
01075     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
01076     {
01077         // assigning to newsletter
01078         $blSuccess = false;
01079         $myConfig  = $this->getConfig();
01080         $mySession = $this->getSession();
01081 
01082         // user wants to get newsletter messages or no ?
01083         $oNewsSubscription = $this->getNewsSubscription();
01084         if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
01085             if ( !$blSendOptIn ) {
01086 
01087                 // double-opt-in check is disabled - assigning automatically
01088                 $this->addToGroup( 'oxidnewsletter' );
01089                 // and setting subscribed status
01090                 $oNewsSubscription->setOptInStatus( 1 );
01091                 $blSuccess = true;
01092             } else {
01093 
01094                 $oNewsSubscription->setOptInStatus( 2 );
01095 
01096                 // double-opt-in check enabled - sending confirmation email and setting waiting status
01097                 if ( !$mySession->getVar( "blDBOptInMailAlreadyDone" ) ) {
01098 
01099                     // sending double-opt-in mail
01100                     $oEmail = oxNew( 'oxemail' );
01101                     $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01102 
01103                     if ( $blSuccess ) {
01104                         //setting in seesion parameter to force sending email only once (#2033)
01105                         oxSession::setVar( "blDBOptInMailAlreadyDone", true );
01106                     }
01107                 } else {
01108                     // mail already was sent, so just confirming that
01109                     $blSuccess = true;
01110                 }
01111             }
01112         } elseif ( !$blSubscribe ) {
01113             // removing user from newsletter subscribers
01114             $this->removeFromGroup( 'oxidnewsletter' );
01115             $oNewsSubscription->setOptInStatus( 0 );
01116             $blSuccess = true;
01117         }
01118 
01119         return $blSuccess;
01120     }
01121 
01139     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01140     {
01141         // validating values before saving. If validation fails - exception is thrown
01142         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01143 
01144         // input data is fine - lets save updated user info
01145         $this->assign( $aInvAddress );
01146 
01147 
01148         // update old or add new delivery address
01149         $this->_assignAddress( $aDelAddress );
01150 
01151         // saving new values
01152         if ( $this->save() ) {
01153 
01154             // assigning automatically to specific groups
01155             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01156             $this->_setAutoGroups( $sCountryId );
01157         }
01158     }
01159 
01170     public function addUserAddress( $oUser )
01171     {
01172         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01173             return false;
01174         }
01175 
01176         $oAddress = oxNew( 'oxaddress' );
01177         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01178         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01179         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01180         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01181         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01182         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01183         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01184         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01185         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01186         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01187         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01188 
01189         // adding new address
01190         if ( $oAddress->save() ) {
01191             // resetting addresses
01192             $this->_aAddresses = null;
01193             return $oAddress->getId();
01194         }
01195     }
01196 
01204     protected function _assignAddress( $aDelAddress )
01205     {
01206         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01207             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01208             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01209             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01210             $oAddress = oxNew( 'oxaddress' );
01211             $oAddress->assign( $aDelAddress );
01212             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01213             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01214             $oAddress->save();
01215 
01216             // resetting addresses
01217             $this->_aAddresses = null;
01218 
01219             // saving delivery Address for later use
01220             oxSession::setVar( 'deladrid', $oAddress->getId() );
01221         } else {
01222             // resetting
01223             oxSession::setVar( 'deladrid', null );
01224         }
01225     }
01226 
01237     protected function _getLoginQuery( $sUser, $sPassword, $sShopID, $blAdmin )
01238     {
01239         $myConfig = $this->getConfig();
01240         $oDb = oxDb::getDb();
01241 
01242         $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01243         $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01244         $sShopSelect = "";
01245 
01246 
01247         // admin view: can only login with higher than 'user' rights
01248         if ( $blAdmin ) {
01249             $sShopSelect = " and ( oxrights != 'user' ) ";
01250         }
01251 
01252         $sWhat = "oxid";
01253 
01254         $sSelect = "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01255         if ( $myConfig->isDemoShop() && $blAdmin ) {
01256             if ( $sPassword == "admin" && $sUser == "admin" ) {
01257                 $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01258             } else {
01259                 $oEx = oxNew( 'oxUserException' );
01260                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01261                 throw $oEx;
01262             }
01263         }
01264 
01265         return $sSelect;
01266     }
01267 
01275     protected function _loadSavedUserBasketAfterLogin()
01276     {
01277         if ( !$this->isAdmin() ) {
01278             //load basket from the database
01279             try {
01280                 if ( $oBasket = $this->getSession()->getBasket() ) {
01281                     $oBasket->load();
01282                 }
01283             } catch ( Exception $oE ) {
01284                 //just ignore it
01285             }
01286         }
01287     }
01288 
01301     public function login( $sUser, $sPassword, $blCookie = false)
01302     {
01303         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01304             $oEx = oxNew( 'oxCookieException' );
01305             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01306             throw $oEx;
01307         }
01308 
01309         $myConfig = $this->getConfig();
01310         if ( $sPassword ) {
01311 
01312             $sShopID = $myConfig->getShopId();
01313             $sSelect = $this->_getLoginQuery( $sUser, $sPassword, $sShopID, $this->isAdmin() );
01314 
01315             // load from DB
01316             $aData = oxDb::getDb()->getAll( $sSelect );
01317             $sOXID = @$aData[0][0];
01318             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01319 
01320                 if ( !$this->load( $sOXID ) ) {
01321                     $oEx = oxNew( 'oxUserException' );
01322                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01323                     throw $oEx;
01324                 }
01325             }
01326         }
01327 
01328 
01329         //login successfull?
01330         if ( $this->oxuser__oxid->value ) {
01331             // yes, successful login
01332 
01333             //reseting active user
01334             $this->setUser( null );
01335 
01336             if ( $this->isAdmin() ) {
01337                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01338             } else {
01339                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01340             }
01341 
01342             // cookie must be set ?
01343             if ( $blCookie ) {
01344                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01345             }
01346 
01347             //load basket from the database
01348             $this->_loadSavedUserBasketAfterLogin();
01349 
01350             return true;
01351         } else {
01352             $oEx = oxNew( 'oxUserException' );
01353             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01354             throw $oEx;
01355         }
01356     }
01357 
01363     public function logout()
01364     {
01365         // deleting session info
01366         oxSession::deleteVar( 'usr' );  // for front end
01367         oxSession::deleteVar( 'auth' ); // for back end
01368         oxSession::deleteVar( 'dgr' );
01369         oxSession::deleteVar( 'dynvalue' );
01370         oxSession::deleteVar( 'paymentid' );
01371         // oxSession::deleteVar( 'deladrid' );
01372 
01373         // delete cookie
01374         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01375 
01376         // unsetting global user
01377         $this->setUser( null );
01378 
01379         return true;
01380     }
01381 
01388     public function loadAdminUser()
01389     {
01390         return $this->loadActiveUser( true );
01391     }
01392 
01401     public function loadActiveUser( $blForceAdmin = false )
01402     {
01403         $myConfig = $this->getConfig();
01404 
01405         $blAdmin = $this->isAdmin() || $blForceAdmin;
01406         $oDB = oxDb::getDb();
01407 
01408         // first - checking session info
01409         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01410         $blFoundInCookie = false;
01411 
01412         //trying automatic login (by 'remember me' cookie)
01413         if ( !$sUserID && !$blAdmin ) {
01414             $sShopID = $myConfig->getShopId();
01415             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01416                 $aData = explode( '@@@', $sSet );
01417                 $sUser = $aData[0];
01418                 $sPWD  = @$aData[1];
01419 
01420                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDB->quote($sUser);
01421 
01422 
01423                 $oDB = oxDb::getDb();
01424                 $rs = $oDB->execute( $sSelect );
01425                 if ( $rs != false && $rs->recordCount() > 0 ) {
01426                     while (!$rs->EOF) {
01427                         $sTest = crypt( $rs->fields[1], 'ox' );
01428                         if ( $sTest == $sPWD ) {
01429                             // found
01430                             $sUserID = $rs->fields[0];
01431                             $blFoundInCookie = true;
01432                             break;
01433                         }
01434                         $rs->moveNext();
01435                     }
01436                 }
01437             }
01438         }
01439 
01440         // Checking if user is connected via Facebook connect.
01441         // If yes, trying to login user using user Facebook ID
01442         if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01443             $oFb = oxFb::getInstance();
01444             if ( $oFb->isConnected() && $oFb->getUser() ) {
01445                 $sUserSelect = "oxuser.oxfbid = " . $oDB->quote( $oFb->getUser() );
01446                 $sShopSelect = "";
01447 
01448 
01449                 $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01450                 $sUserID = $oDB->getOne( $sSelect );
01451             }
01452         }
01453 
01454         // checking user results
01455         if ( $sUserID ) {
01456             if ( $this->load( $sUserID ) ) {
01457                 // storing into session
01458                 if ($blAdmin) {
01459                     oxSession::setVar( 'auth', $sUserID );
01460                 } else {
01461                     oxSession::setVar( 'usr', $sUserID );
01462                 }
01463 
01464                 // marking the way user was loaded
01465                 $this->_blLoadedFromCookie = $blFoundInCookie;
01466                 return true;
01467             }
01468         } else {
01469             // no user
01470             if ($blAdmin) {
01471                 oxSession::deleteVar( 'auth' );
01472             } else {
01473                 oxSession::deleteVar( 'usr' );
01474             }
01475 
01476             return false;
01477         }
01478     }
01479 
01492     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01493     {
01494         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01495         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01496 
01497         // maybe this is LDAP user but supplied email Address instead of LDAP login
01498         $oDb = oxDb::getDb();
01499         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01500         if ( isset( $sLDAPKey) && $sLDAPKey) {
01501             $sUser = $sLDAPKey;
01502         }
01503 
01504         //$throws oxConnectionException
01505         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01506 
01507         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01508         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01509             // login successful
01510 
01511             // check if user is already in database
01512             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01513             $sOXID = $oDb->getOne( $sSelect );
01514 
01515             if ( !isset( $sOXID ) || !$sOXID ) {
01516                 // we need to create a new user
01517                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01518                 $this->setId();
01519 
01520                 // map all user data fields
01521                 foreach ( $aData as $fldname => $value) {
01522                     $sField = "oxuser__".strtolower( $fldname);
01523                     $this->$sField = new oxField( $aData[$fldname] );
01524                 }
01525 
01526                 $this->oxuser__oxactive  = new oxField( 1 );
01527                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01528                 $this->oxuser__oxldapkey = new oxField( $sUser );
01529                 $this->oxuser__oxrights  = new oxField( "user" );
01530                 $this->setPassword( "ldap user" );
01531 
01532                 $this->save();
01533             } else {
01534                 // LDAP user is already in OXID DB, load it
01535                 $this->load( $sOXID);
01536             }
01537 
01538         } else {
01539             $oEx = oxNew( 'oxUserException' );
01540             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01541             throw $oEx;
01542         }
01543     }
01544 
01551     protected function _getUserRights()
01552     {
01553         // previously user had no rights defined
01554         if ( !$this->oxuser__oxrights->value )
01555             return 'user';
01556 
01557         $oDB = oxDb::getDb();
01558         $myConfig    = $this->getConfig();
01559         $sAuthRights = null;
01560 
01561         // choosing possible user rights index
01562         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01563         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01564         if ( $sAuthUserID ) {
01565             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01566         }
01567 
01568         //preventing user rights edit for non admin
01569         $aRights = array();
01570 
01571         // selecting current users rights ...
01572         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01573             $aRights[] = $sCurrRights;
01574         }
01575         $aRights[] = 'user';
01576 
01577         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01578             return current( $aRights );
01579         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01580             $aRights[] = $sAuthRights;
01581             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01582                 return current( $aRights );
01583             }
01584         }
01585 
01586         // leaving as it was set ...
01587         return $this->oxuser__oxrights->value;
01588     }
01589 
01599     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01600     {
01601 
01602         /*if ( !$myConfig->blMallUsers ) {
01603             $sShopID = $myConfig->getShopId();
01604             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01605         }*/
01606 
01607         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01608     }
01609 
01615     protected function _insert()
01616     {
01617 
01618         // set oxcreate date
01619         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01620 
01621         if ( !isset( $this->oxuser__oxboni->value ) ) {
01622             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01623         }
01624 
01625         return parent::_insert();
01626     }
01627 
01633     protected function _update()
01634     {
01635         //V #M418: for not registered users, don't change boni during update
01636         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01637             $this->_aSkipSaveFields[] = 'oxboni';
01638         }
01639 
01640         // don't change this field
01641         $this->_aSkipSaveFields[] = 'oxcreate';
01642         if ( !$this->isAdmin() ) {
01643             $this->_aSkipSaveFields[] = 'oxcustnr';
01644             $this->_aSkipSaveFields[] = 'oxrights';
01645         }
01646 
01647         // updating subscription information
01648         if ( ( $blUpdate = parent::_update() ) ) {
01649             $this->getNewsSubscription()->updateSubscription( $this );
01650         }
01651 
01652         return $blUpdate;
01653     }
01654 
01670     protected function _checkLogin( $sLogin, $aInvAddress )
01671     {
01672         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01673         oxInputValidator::getInstance()->checkLogin( $this, $sLogin, $aInvAddress );
01674         return $sLogin;
01675     }
01676 
01684     public function checkIfEmailExists( $sEmail )
01685     {
01686         $myConfig = $this->getConfig();
01687         $oDB = oxDb::getDb();
01688         $iShopId = $myConfig->getShopId();
01689         $blExists = false;
01690 
01691         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01692         if ( ( $sOxid = $this->getId() ) ) {
01693             $sQ .= " and oxid <> '$sOxid' ";
01694         }
01695 
01696         $oRs = $oDB->execute( $sQ );
01697         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01698 
01699             if ( $this->_blMallUsers ) {
01700 
01701                 $blExists = true;
01702                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01703 
01704                     // password is not set - allow to override
01705                     $blExists = false;
01706                 }
01707             } else {
01708 
01709                 $blExists = false;
01710                 while ( !$oRs->EOF ) {
01711                     if ( $oRs->fields[1] != 'user' ) {
01712 
01713                         // exists admin with same login - must not allow
01714                         $blExists = true;
01715                         break;
01716                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01717 
01718                         // exists same login (with password) in same shop
01719                         $blExists = true;
01720                         break;
01721                     }
01722 
01723                     $oRs->moveNext();
01724                 }
01725             }
01726         }
01727         return $blExists;
01728     }
01729 
01737     public function getUserRecommLists( $sOXID = null )
01738     {
01739         if ( !$sOXID )
01740             $sOXID = $this->getId();
01741 
01742         // sets active page
01743         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01744         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01745 
01746         // load only lists which we show on screen
01747         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01748         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01749 
01750 
01751         $oRecommList = oxNew( 'oxlist' );
01752         $oRecommList->init( 'oxrecommlist' );
01753         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01754         $iShopId = $this->getConfig()->getShopId();
01755         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01756         $oRecommList->selectString( $sSelect );
01757 
01758         return $oRecommList;
01759     }
01760 
01768     public function getRecommListsCount( $sOx = null )
01769     {
01770         if ( !$sOx ) {
01771             $sOx = $this->getId();
01772         }
01773 
01774         if ( $this->_iCntRecommLists === null || $sOx ) {
01775             $oDb = oxDb::getDb();
01776             $this->_iCntRecommLists = 0;
01777             $iShopId = $this->getConfig()->getShopId();
01778             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01779             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01780         }
01781         return $this->_iCntRecommLists;
01782     }
01783 
01794     protected function _checkEmail( $sEmail )
01795     {
01796         oxInputValidator::getInstance()->checkEmail( $this, $sEmail );
01797     }
01798 
01813     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01814     {
01815         return $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01816     }
01817 
01830     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01831     {
01832         return oxInputValidator::getInstance()->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01833     }
01834 
01845     protected function _checkCountries( $aInvAddress, $aDelAddress )
01846     {
01847         oxInputValidator::getInstance()->checkCountries( $this, $aInvAddress, $aDelAddress );
01848     }
01849 
01861     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01862     {
01863         oxInputValidator::getInstance()->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01864     }
01865 
01876     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01877     {
01878         oxInputValidator::getInstance()->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01879     }
01880 
01891     protected function _checkVatId( $aInvAddress )
01892     {
01893         oxInputValidator::getInstance()->checkVatId( $this, $aInvAddress );
01894     }
01895 
01904     protected function _setAutoGroups( $sCountryId )
01905     {
01906         // assigning automatically to specific groups
01907         $blForeigner = true;
01908         $blForeignGroupExists = false;
01909         $blInlandGroupExists = false;
01910 
01911         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01912         // foreigner ?
01913         if ( is_array($aHomeCountry)) {
01914             if (in_array($sCountryId, $aHomeCountry)) {
01915                 $blForeigner = false;
01916             }
01917         } elseif ($sCountryId == $aHomeCountry) {
01918             $blForeigner = false;
01919         }
01920 
01921         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01922             $blForeignGroupExists = true;
01923             if ( !$blForeigner ) {
01924                 $this->removeFromGroup( 'oxidforeigncustomer' );
01925             }
01926         }
01927 
01928         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01929             $blInlandGroupExists = true;
01930             if ( $blForeigner ) {
01931                 $this->removeFromGroup( 'oxidnewcustomer' );
01932             }
01933         }
01934 
01935         if ( !$this->oxuser__oxdisableautogrp->value ) {
01936             if ( !$blForeignGroupExists && $blForeigner ) {
01937                 $this->addToGroup( 'oxidforeigncustomer' );
01938             }
01939             if ( !$blInlandGroupExists && !$blForeigner ) {
01940                 $this->addToGroup( 'oxidnewcustomer' );
01941             }
01942         }
01943     }
01944 
01953     protected function _hasUserAddress( $sUserId )
01954     {
01955         $oAddresses = $this->getUserAddresses();
01956         if ( $oAddresses && count($oAddresses)>0 ) {
01957             $oAddresses->rewind() ;
01958             foreach ($oAddresses as $key => $oAddress) {
01959                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01960                     return true;
01961                 }
01962             }
01963         }
01964         return false;
01965     }
01966 
01979     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
01980     {
01981         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
01982     }
01983 
01993     protected function _deleteUserCookie( $sShopId = null )
01994     {
01995         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
01996     }
01997 
02007     protected static function _getUserCookie( $sShopId = null )
02008     {
02009         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
02010     }
02011 
02012 
02021     public function loadUserByUpdateId( $sUid )
02022     {
02023         $oDb = oxDb::getDb();
02024         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02025         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02026             return $this->load( $sUserId );
02027         }
02028     }
02029 
02037     public function setUpdateKey( $blReset = false )
02038     {
02039         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02040         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02041 
02042         // generating key
02043         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02044 
02045         // setting expiration time for 6 hours
02046         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02047 
02048         // saving
02049         $this->save();
02050     }
02051 
02057     public function getUpdateLinkTerm()
02058     {
02059         return 3600 * 6;
02060     }
02061 
02069     public function isExpiredUpdateId( $sKey )
02070     {
02071         $oDb = oxDb::getDb();
02072         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02073         return !( (bool) $oDb->getOne( $sQ ) );
02074     }
02075 
02081     public function getUpdateId()
02082     {
02083         if ( $this->_sUpdateKey === null ) {
02084             $this->setUpdateKey();
02085             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02086         }
02087         return $this->_sUpdateKey;
02088     }
02089 
02098     public function encodePassword( $sPassword, $sSalt )
02099     {
02100         $oDb = oxDb::getDb();
02101         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02102     }
02103 
02111     public function prepareSalt( $sSalt )
02112     {
02113         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02114     }
02115 
02123     public function decodeSalt( $sSaltHex )
02124     {
02125         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02126     }
02127 
02135     public function setPassword( $sPassword = null )
02136     {
02137         // setting salt if password is not empty
02138         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02139 
02140         // encoding only if password was not empty (e.g. user registration without pass)
02141         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02142 
02143         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02144         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02145     }
02146 
02154     public function isSamePassword( $sNewPass )
02155     {
02156         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02157     }
02158 
02164     public function isLoadedFromCookie()
02165     {
02166         return $this->_blLoadedFromCookie;
02167     }
02168 
02175     public function getPasswordHash()
02176     {
02177         $sHash = null;
02178         if ( $this->oxuser__oxpassword->value ) {
02179             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02180                 // decodable pass ?
02181                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02182             } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02183                 // plain pass ?
02184                 $this->setPassword( $this->oxuser__oxpassword->value );
02185             }
02186             $sHash = $this->oxuser__oxpassword->value;
02187         }
02188         return $sHash;
02189     }
02190 
02199     public static function getAdminUser()
02200     {
02201         return self::getActiveUser( true );
02202     }
02203 
02214     public static function getActiveUser( $blForceAdmin = false )
02215     {
02216         $oUser = oxNew( 'oxuser' );
02217         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02218             return $oUser;
02219         } else {
02220             return false;
02221         }
02222     }
02223 
02231     public function getReviewUserHash( $sUserId )
02232     {
02233         $oDb = oxDb::getDb();
02234         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02235         return $sReviewUserHash;
02236     }
02237 
02245     public function getReviewUserId( $sReviewUserHash )
02246     {
02247         $oDb = oxDb::getDb();
02248         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02249         return $sUserId;
02250     }
02251 
02257     public function getState()
02258     {
02259         return $this->oxuser__oxstateid->value;
02260     }
02261 
02267     public function isTermsAccepted()
02268     {
02269         $sShopId = $this->getConfig()->getShopId();
02270         $sUserId = $this->getId();
02271         return (bool) oxDb::getDb()->getOne( "select 1 from oxacceptedterms where oxuserid='{$sUserId}' and oxshopid='{$sShopId}'" );
02272     }
02273 
02279     public function acceptTerms()
02280     {
02281         $sUserId  = $this->getId();
02282         $sShopId  = $this->getConfig()->getShopId();
02283         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02284 
02285         oxDb::getDb()->execute( "replace oxacceptedterms set oxuserid='{$sUserId}', oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02286     }
02287 
02297     public function setCreditPointsForRegistrant( $sUserId, $sRecEmail )
02298     {
02299         $blSet   = false;
02300         $oDb = oxDb::getDb();
02301         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02302         // check if this invitation is still not accepted
02303         $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" );
02304         if ( $iPoints && $iPending ) {
02305             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02306             if ( $blSet = $this->save() ) {
02307                 // updating users statistics
02308                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail ) );
02309                 $oInvUser = oxNew( "oxuser" );
02310                 if ( $oInvUser->load( $sUserId ) ) {
02311                     $blSet = $oInvUser->setCreditPointsForInviter();
02312                 }
02313             }
02314         }
02315         oxSession::deleteVar( 'su' );
02316         oxSession::deleteVar( 're' );
02317 
02318         return $blSet;
02319     }
02320 
02326     public function setCreditPointsForInviter()
02327     {
02328         $blSet   = false;
02329         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02330         if ( $iPoints ) {
02331             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02332             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02333             $blSet = $this->save();
02334         }
02335 
02336         return $blSet;
02337     }
02338 
02344     public function updateFbId()
02345     {
02346         $oFb = oxFb::getInstance();
02347         $blRet = false;
02348 
02349         if ( $oFb->isConnected() && $oFb->getUser() ) {
02350              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02351              $blRet = $this->save();
02352         }
02353 
02354         return $blRet;
02355     }
02356 
02364     public function updateInvitationStatistics( $aRecEmail )
02365     {
02366         $oDb = oxDb::getDb( true );
02367         $sUserId = $this->getId();
02368 
02369         if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
02370             //iserting statistics about invitation
02371             $sDate = oxUtilsDate::getInstance()->formatDBDate( date("Y-m-d"), true );
02372             $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
02373             foreach ( $aRecEmail as $sRecEmail ) {
02374                 $sSql = "INSERT INTO oxinvitations SET oxuserid = '$sUserId', oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02375                 $oDb->execute( $sSql );
02376             }
02377         }
02378     }
02379 
02380 }