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' ), 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         return $blRet;
00604     }
00605 
00613     public function exists( $sOXID = null )
00614     {
00615         $oDb = oxDb::getDb();
00616         if ( !$sOXID ) {
00617             $sOXID = $this->getId();
00618         }
00619 
00620         $sShopSelect = '';
00621         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00622             $sShopSelect = ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00623         }
00624 
00625         //#4543 Query optimisation by splitting it into two, might need an logics optimisation as well
00626         if ( $sOXID ) {
00627             $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00628                     WHERE ( oxid = '.$oDb->quote( $sOXID ).' ) ';
00629             $sSelect .= $sShopSelect;
00630 
00631             if ( ( $sOxid = $oDb->getOne( $sSelect ) ) ) {
00632                 // update - set oxid
00633                 $this->setId( $sOxid );
00634                 return true;
00635             }
00636         }
00637 
00638         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00639                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).' ) ';
00640         $sSelect .= $sShopSelect;
00641 
00642         if ( ( $sOxid = $oDb->getOne( $sSelect ) ) ) {
00643              // update - set oxid
00644             $this->setId( $sOxid );
00645             return true;
00646         }
00647         return false;
00648     }
00649 
00658     public function getOrders( $iLimit = false, $iPage = 0 )
00659     {
00660         $myConfig = $this->getConfig();
00661         $oOrders = oxNew( 'oxlist' );
00662         $oOrders->init( 'oxorder' );
00663 
00664         if ( $iLimit !== false ) {
00665             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00666         }
00667 
00668         //P
00669         // Lists does not support loading from two tables, so orders
00670         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00671         // forcing to load product info which is used in templates
00672         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00673 
00674         //loading order for registered user
00675         if ( $this->oxuser__oxregister->value > 1 ) {
00676             $oDb = oxDb::getDb();
00677             $sQ = 'select * from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' and oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value ) . ' ';
00678 
00679             //#1546 - shopid check added, if it is not multishop
00680 
00681             $sQ .= ' order by oxorderdate desc ';
00682             $oOrders->selectString( $sQ );
00683         }
00684 
00685         return $oOrders;
00686     }
00687 
00693     public function getOrderCount()
00694     {
00695         $iCnt = 0;
00696         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00697             $oDb = oxDb::getDb();
00698             $sQ  = 'select count(*) from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00699             $iCnt = (int) $oDb->getOne( $sQ );
00700         }
00701 
00702         return $iCnt;
00703     }
00704 
00710     public function getNoticeListArtCnt()
00711     {
00712         if ( $this->_iCntNoticeListArticles === null ) {
00713             $this->_iCntNoticeListArticles = 0;
00714             if ( $this->getId() ) {
00715                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00716             }
00717         }
00718         return $this->_iCntNoticeListArticles;
00719     }
00720 
00726     public function getWishListArtCnt()
00727     {
00728         if ( $this->_iCntWishListArticles === null ) {
00729             $this->_iCntWishListArticles = false;
00730             if ( $this->getId() ) {
00731                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00732             }
00733         }
00734         return $this->_iCntWishListArticles;
00735     }
00736 
00743     public function getActiveCountry()
00744     {
00745         $sDeliveryCountry = '';
00746         $soxAddressId = oxSession::getVar( 'deladrid' );
00747         if ( $soxAddressId ) {
00748             $oDelAddress = oxNew( 'oxaddress' );
00749             $oDelAddress->load( $soxAddressId );
00750             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00751         } elseif ( $this->getId() ) {
00752             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00753         } else {
00754             $oUser = oxNew( 'oxuser' );
00755             if ( $oUser->loadActiveUser() ) {
00756                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00757             }
00758         }
00759 
00760         return $sDeliveryCountry;
00761     }
00762 
00770     public function createUser()
00771     {
00772         $oDb = oxDb::getDb();
00773         $sShopID = $this->getConfig()->getShopId();
00774 
00775         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00776         $sSelect = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00777         if ( !$this->_blMallUsers ) {
00778             $sSelect .= " and oxshopid = '{$sShopID}' ";
00779         }
00780         $sOXID = $oDb->getOne( $sSelect, false, false );
00781 
00782         // user without password found - lets use
00783         if ( isset( $sOXID ) && $sOXID ) {
00784             // try to update
00785             $this->delete( $sOXID );
00786         } elseif ( $this->_blMallUsers ) {
00787             // must be sure if there is no dublicate user
00788             $sQ = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00789             if ( $oDb->getOne( $sQ, false, false ) ) {
00790                 $oEx = oxNew( 'oxUserException' );
00791                 $oLang = oxRegistry::getLang();
00792                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00793                 throw $oEx;
00794             }
00795         }
00796 
00797         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00798         if ( ( $blOK = $this->save() ) ) {
00799             // dropping/cleaning old delivery address/payment info
00800             $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00801             $oDb->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00802         } else {
00803             $oEx = oxNew( 'oxUserException' );
00804             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00805             throw $oEx;
00806         }
00807 
00808         return $blOK;
00809     }
00810 
00818     public function addToGroup( $sGroupID )
00819     {
00820         if ( !$this->inGroup( $sGroupID ) ) {
00821             // create oxgroup object
00822             $oGroup = oxNew('oxGroups');
00823             if ( $oGroup->load($sGroupID) ) {
00824                 $oNewGroup = oxNew( 'oxobject2group' );
00825                 $oNewGroup->oxobject2group__oxobjectid = new oxField( $this->getId(), oxField::T_RAW );
00826                 $oNewGroup->oxobject2group__oxgroupsid = new oxField( $sGroupID, oxField::T_RAW );
00827                 if ( $oNewGroup->save() ) {
00828                     $this->_oGroups[$sGroupID] = $oGroup;
00829                     return true;
00830                 }
00831             }
00832         }
00833         return false;
00834     }
00835 
00843     public function removeFromGroup( $sGroupID = null )
00844     {
00845         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00846             $oGroups = oxNew( 'oxlist' );
00847             $oGroups->init( 'oxobject2group' );
00848             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00849             $oGroups->selectString( $sSelect );
00850             foreach ( $oGroups as $oRemgroup ) {
00851                 if ( $oRemgroup->delete() ) {
00852                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00853                 }
00854             }
00855         }
00856     }
00857 
00866     public function onOrderExecute( $oBasket, $iSuccess )
00867     {
00868 
00869         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00870             //adding user to particular customer groups
00871             if ( !$this->oxuser__oxdisableautogrp->value ) {
00872 
00873                 $myConfig = $this->getConfig();
00874                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00875                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00876 
00877                 $this->addToGroup( 'oxidcustomer' );
00878                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00879                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00880                     $this->addToGroup( 'oxidsmallcust' );
00881                 }
00882                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00883                     $this->addToGroup( 'oxidmiddlecust' );
00884                 }
00885                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00886                     $this->addToGroup( 'oxidgoodcust' );
00887                 }
00888             }
00889 
00890             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00891                 $this->removeFromGroup( 'oxidnotyetordered' );
00892             }
00893         }
00894     }
00895 
00903     public function getBasket( $sName )
00904     {
00905         if ( !isset( $this->_aBaskets[$sName] ) ) {
00906             $oBasket = oxNew( 'oxuserbasket' );
00907             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00908 
00909             // creating if it does not exist
00910             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00911                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00912                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00913 
00914                 // marking basket as new (it will not be saved in DB yet)
00915                 $oBasket->setIsNewBasket();
00916             }
00917 
00918             $this->_aBaskets[$sName] = $oBasket;
00919         }
00920 
00921         return $this->_aBaskets[$sName];
00922     }
00923 
00932     public function convertBirthday( $aData )
00933     {
00934 
00935         // preparing data to process
00936         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00937         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00938         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00939 
00940         // leaving empty if not set
00941         if ( !$iYear && !$iMonth && !$iDay )
00942             return "";
00943 
00944         // year
00945         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00946             $iYear = date('Y');
00947 
00948         // month
00949         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00950             $iMonth = 1;
00951 
00952         // maximum nuber of days in month
00953         $iMaxDays = 31;
00954         switch( $iMonth) {
00955             case 2 :
00956                 if ($iMaxDays > 28)
00957                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00958                 break;
00959             case 4  :
00960             case 6  :
00961             case 9  :
00962             case 11 :
00963                 $iMaxDays = min(30, $iMaxDays);
00964                 break;
00965         }
00966 
00967         // day
00968         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00969             $iDay = 1;
00970         }
00971 
00972         // whole date
00973         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00974     }
00975 
00982     public function getBoni()
00983     {
00984         return 1000;
00985     }
00986 
00999     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
01000     {
01001         // preparing input
01002         $sDynGoup = strtolower( trim( $sDynGoup ) );
01003 
01004         // setting denied groups from admin settings also
01005         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
01006 
01007         // default state ..
01008         $blAdd = false;
01009 
01010         // user assignment to dyn group is not allowed
01011         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
01012             $blAdd = false;
01013         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
01014             // trying to add user to prohibited user group?
01015             $blAdd = false;
01016         } elseif ( $this->addToGroup( $sDynGoup ) ) {
01017             $blAdd = true;
01018         }
01019 
01020         // cleanup
01021         oxSession::deleteVar( 'dgr' );
01022 
01023         return $blAdd;
01024     }
01025 
01044     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01045     {
01046         // 1. checking user name
01047         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01048 
01049         // 2. cheking email
01050         $this->_checkEmail( $sLogin );
01051 
01052         // 3. password
01053         $this->checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01054 
01055         // 4. required fields
01056         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01057 
01058         // 5. country check
01059         $this->_checkCountries( $aInvAddress, $aDelAddress );
01060 
01061         // 6. vat id check.
01062             $this->_checkVatId( $aInvAddress );
01063 
01064 
01065         // throwing first validation error
01066         if ( $oError = oxRegistry::get("oxInputValidator")->getFirstValidationError() ) {
01067             throw $oError;
01068         }
01069     }
01070 
01079     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
01080     {
01081         // assigning to newsletter
01082         $blSuccess = false;
01083         $myConfig  = $this->getConfig();
01084         $mySession = $this->getSession();
01085 
01086         // user wants to get newsletter messages or no ?
01087         $oNewsSubscription = $this->getNewsSubscription();
01088         if ( $oNewsSubscription ) {
01089             if ( $blSubscribe && ( $iOptInStatus = $oNewsSubscription->getOptInStatus() ) != 1 ) {
01090                 if ( !$blSendOptIn ) {
01091 
01092                     // double-opt-in check is disabled - assigning automatically
01093                     $this->addToGroup( 'oxidnewsletter' );
01094                     // and setting subscribed status
01095                     $oNewsSubscription->setOptInStatus( 1 );
01096                     $blSuccess = true;
01097                 } else {
01098 
01099                     // double-opt-in check enabled - sending confirmation email and setting waiting status
01100                     if ( $iOptInStatus != 2 ) {
01101                         // sending double-opt-in mail
01102                         $oEmail = oxNew( 'oxemail' );
01103                         $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01104                     } else {
01105                         // mail already was sent, so just confirming that
01106                         $blSuccess = true;
01107                     }
01108 
01109                     $oNewsSubscription->setOptInStatus( 2 );
01110                 }
01111             } elseif ( !$blSubscribe ) {
01112                 // removing user from newsletter subscribers
01113                 $this->removeFromGroup( 'oxidnewsletter' );
01114                 $oNewsSubscription->setOptInStatus( 0 );
01115                 $blSuccess = true;
01116             }
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 
01167     protected function _assignAddress( $aDelAddress )
01168     {
01169         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01170             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01171             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01172             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01173             $oAddress = oxNew( 'oxaddress' );
01174             $oAddress->assign( $aDelAddress );
01175             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01176             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01177             $oAddress->save();
01178 
01179             // resetting addresses
01180             $this->_aAddresses = null;
01181 
01182             // saving delivery Address for later use
01183             oxSession::setVar( 'deladrid', $oAddress->getId() );
01184         } else {
01185             // resetting
01186             oxSession::setVar( 'deladrid', null );
01187         }
01188     }
01189 
01200     protected function _getLoginQuery( $sUser, $sPassword, $sShopID, $blAdmin )
01201     {
01202         $myConfig = $this->getConfig();
01203         $oDb = oxDb::getDb();
01204 
01205         $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01206         $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01207         $sShopSelect = "";
01208 
01209 
01210         // admin view: can only login with higher than 'user' rights
01211         if ( $blAdmin ) {
01212             $sShopSelect = " and ( oxrights != 'user' ) ";
01213         }
01214 
01215         $blStagingMode = false;
01216         $blDemoMode = false;
01217         $sWhat = "oxid";
01218             if ($myConfig->isDemoShop()) {
01219                 $blDemoMode = true;
01220             }
01221 
01222         $sSelect = "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01223         if ( ( $blDemoMode || $blStagingMode ) && $blAdmin ) {
01224             if ( $sPassword == "admin" && $sUser == "admin" ) {
01225                 $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' ";
01226             } elseif ( $blDemoMode ) {
01227                 $oEx = oxNew( 'oxUserException' );
01228                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01229                 throw $oEx;
01230             }
01231         }
01232 
01233         return $sSelect;
01234     }
01235 
01245     protected function _getShopSelect( $myConfig, $sShopID, $blAdmin )
01246     {
01247         $sShopSelect = "";
01248         // admin view: can only login with higher than 'user' rights
01249         if ( $blAdmin ) {
01250             $sShopSelect = " and ( oxrights != 'user' ) ";
01251         }
01252 
01253         return $sShopSelect;
01254     }
01262     protected function _loadSavedUserBasketAfterLogin()
01263     {
01264         if ( !$this->isAdmin() && !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' )) {
01265             //load basket from the database
01266             try {
01267                 if ( $oBasket = $this->getSession()->getBasket() ) {
01268                     $oBasket->load();
01269                 }
01270             } catch ( Exception $oE ) {
01271                 //just ignore it
01272             }
01273         }
01274     }
01275 
01276 
01289     public function login( $sUser, $sPassword, $blCookie = false)
01290     {
01291         if ( $this->isAdmin() && !count( oxRegistry::get("oxUtilsServer")->getOxCookie() ) ) {
01292             $oEx = oxNew( 'oxCookieException' );
01293             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01294             throw $oEx;
01295         }
01296 
01297         $myConfig = $this->getConfig();
01298         if ( $sPassword ) {
01299 
01300             $sShopID = $myConfig->getShopId();
01301             $sSelect = $this->_getLoginQuery( $sUser, $sPassword, $sShopID, $this->isAdmin() );
01302 
01303             // load from DB
01304             $aData = oxDb::getDb()->getAll( $sSelect );
01305             $sOXID = @$aData[0][0];
01306             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01307 
01308                 if ( !$this->load( $sOXID ) ) {
01309                     $oEx = oxNew( 'oxUserException' );
01310                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01311                     throw $oEx;
01312                 }
01313             }
01314         }
01315 
01316 
01317         //login successfull?
01318         if ( $this->oxuser__oxid->value ) {
01319             // yes, successful login
01320 
01321             //reseting active user
01322             $this->setUser( null );
01323 
01324             if ( $this->isAdmin() ) {
01325                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01326             } else {
01327                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01328             }
01329 
01330             // cookie must be set ?
01331             if ( $blCookie && $myConfig->getConfigParam( 'blShowRememberMe' ) ) {
01332                 oxRegistry::get("oxUtilsServer")->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value );
01333             }
01334 
01335             return true;
01336         } else {
01337             $oEx = oxNew( 'oxUserException' );
01338             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01339             throw $oEx;
01340         }
01341     }
01342 
01348     public function logout()
01349     {
01350         // deleting session info
01351         oxSession::deleteVar( 'usr' );  // for front end
01352         oxSession::deleteVar( 'auth' ); // for back end
01353         oxSession::deleteVar( 'dgr' );
01354         oxSession::deleteVar( 'dynvalue' );
01355         oxSession::deleteVar( 'paymentid' );
01356         // oxSession::deleteVar( 'deladrid' );
01357 
01358         // delete cookie
01359         oxRegistry::get("oxUtilsServer")->deleteUserCookie( $this->getConfig()->getShopID() );
01360 
01361         // unsetting global user
01362         $this->setUser( null );
01363 
01364         return true;
01365     }
01366 
01373     public function loadAdminUser()
01374     {
01375         return $this->loadActiveUser( true );
01376     }
01377 
01386     public function loadActiveUser( $blForceAdmin = false )
01387     {
01388         $myConfig = $this->getConfig();
01389 
01390         $blAdmin = $this->isAdmin() || $blForceAdmin;
01391         $oDb = oxDb::getDb();
01392 
01393         // first - checking session info
01394         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01395         $blFoundInCookie = false;
01396 
01397         //trying automatic login (by 'remember me' cookie)
01398         if ( !$sUserID && !$blAdmin && $myConfig->getConfigParam('blShowRememberMe') ) {
01399             $sShopID = $myConfig->getShopId();
01400             if ( ( $sSet = oxRegistry::get("oxUtilsServer")->getUserCookie( $sShopID ) ) ) {
01401                 $aData = explode( '@@@', $sSet );
01402                 $sUser = $aData[0];
01403                 $sPWD  = @$aData[1];
01404 
01405                 $sSelect =  'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDb->quote($sUser);
01406 
01407                 $rs = $oDb->select( $sSelect );
01408                 if ( $rs != false && $rs->recordCount() > 0 ) {
01409                     while (!$rs->EOF) {
01410                         $sTest = crypt( $rs->fields[1], $rs->fields[2] );
01411                         if ( $sTest == $sPWD ) {
01412                             // found
01413                             $sUserID = $rs->fields[0];
01414                             $blFoundInCookie = true;
01415                             break;
01416                         }
01417                         $rs->moveNext();
01418                     }
01419                 }
01420             }
01421         }
01422 
01423         // Checking if user is connected via Facebook connect.
01424         // If yes, trying to login user using user Facebook ID
01425         if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01426             $oFb = oxRegistry::get("oxFb");
01427             if ( $oFb->isConnected() && $oFb->getUser() ) {
01428                 $sUserSelect = "oxuser.oxfbid = " . $oDb->quote( $oFb->getUser() );
01429                 $sShopSelect = "";
01430 
01431 
01432                 $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01433                 $sUserID = $oDb->getOne( $sSelect );
01434             }
01435         }
01436 
01437         // checking user results
01438         if ( $sUserID ) {
01439             if ( $this->load( $sUserID ) ) {
01440                 // storing into session
01441                 if ($blAdmin) {
01442                     oxSession::setVar( 'auth', $sUserID );
01443                 } else {
01444                     oxSession::setVar( 'usr', $sUserID );
01445                 }
01446 
01447                 // marking the way user was loaded
01448                 $this->_blLoadedFromCookie = $blFoundInCookie;
01449                 return true;
01450             }
01451         } else {
01452             // no user
01453             if ($blAdmin) {
01454                 oxSession::deleteVar( 'auth' );
01455             } else {
01456                 oxSession::deleteVar( 'usr' );
01457             }
01458 
01459             return false;
01460         }
01461     }
01462 
01475     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01476     {
01477         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01478         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01479 
01480         // maybe this is LDAP user but supplied email Address instead of LDAP login
01481         $oDb = oxDb::getDb();
01482         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01483         if ( isset( $sLDAPKey) && $sLDAPKey) {
01484             $sUser = $sLDAPKey;
01485         }
01486 
01487         //$throws oxConnectionException
01488         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01489 
01490         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01491         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01492             // login successful
01493 
01494             // check if user is already in database
01495             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01496             $sOXID = $oDb->getOne( $sSelect );
01497 
01498             if ( !isset( $sOXID ) || !$sOXID ) {
01499                 // we need to create a new user
01500                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01501                 $this->setId();
01502 
01503                 // map all user data fields
01504                 foreach ( $aData as $fldname => $value) {
01505                     $sField = "oxuser__".strtolower( $fldname);
01506                     $this->$sField = new oxField( $aData[$fldname] );
01507                 }
01508 
01509                 $this->oxuser__oxactive  = new oxField( 1 );
01510                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01511                 $this->oxuser__oxldapkey = new oxField( $sUser );
01512                 $this->oxuser__oxrights  = new oxField( "user" );
01513                 $this->setPassword( "ldap user" );
01514 
01515                 $this->save();
01516             } else {
01517                 // LDAP user is already in OXID DB, load it
01518                 $this->load( $sOXID);
01519             }
01520 
01521         } else {
01522             $oEx = oxNew( 'oxUserException' );
01523             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01524             throw $oEx;
01525         }
01526     }
01527 
01534     protected function _getUserRights()
01535     {
01536         // previously user had no rights defined
01537         if ( !$this->oxuser__oxrights->value )
01538             return 'user';
01539 
01540         $oDb = oxDb::getDb();
01541         $myConfig    = $this->getConfig();
01542         $sAuthRights = null;
01543 
01544         // choosing possible user rights index
01545         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01546         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01547         if ( $sAuthUserID ) {
01548             $sAuthRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $sAuthUserID ) );
01549         }
01550 
01551         //preventing user rights edit for non admin
01552         $aRights = array();
01553 
01554         // selecting current users rights ...
01555         if ( $sCurrRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $this->getId() ) ) ) {
01556             $aRights[] = $sCurrRights;
01557         }
01558         $aRights[] = 'user';
01559 
01560         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01561             return current( $aRights );
01562         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01563             $aRights[] = $sAuthRights;
01564             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01565                 return current( $aRights );
01566             }
01567         }
01568 
01569         // leaving as it was set ...
01570         return $this->oxuser__oxrights->value;
01571     }
01572 
01578     protected function _insert()
01579     {
01580 
01581         // set oxcreate date
01582         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01583 
01584         if ( !isset( $this->oxuser__oxboni->value ) ) {
01585             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01586         }
01587 
01588         return parent::_insert();
01589     }
01590 
01596     protected function _update()
01597     {
01598         //V #M418: for not registered users, don't change boni during update
01599         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01600             $this->_aSkipSaveFields[] = 'oxboni';
01601         }
01602 
01603         // don't change this field
01604         $this->_aSkipSaveFields[] = 'oxcreate';
01605         if ( !$this->isAdmin() ) {
01606             $this->_aSkipSaveFields[] = 'oxcustnr';
01607             $this->_aSkipSaveFields[] = 'oxrights';
01608         }
01609 
01610         // updating subscription information
01611         if ( ( $blUpdate = parent::_update() ) ) {
01612             $this->getNewsSubscription()->updateSubscription( $this );
01613         }
01614 
01615         return $blUpdate;
01616     }
01617 
01633     protected function _checkLogin( $sLogin, $aInvAddress )
01634     {
01635         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01636         oxRegistry::get("oxInputValidator")->checkLogin( $this, $sLogin, $aInvAddress );
01637         return $sLogin;
01638     }
01639 
01647     public function checkIfEmailExists( $sEmail )
01648     {
01649         $myConfig = $this->getConfig();
01650         $oDb = oxDb::getDb();
01651         $iShopId = $myConfig->getShopId();
01652         $blExists = false;
01653 
01654         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDb->quote( $sEmail );
01655         if ( ( $sOxid = $this->getId() ) ) {
01656             $sQ .= " and oxid <> ".$oDb->quote( $sOxid );
01657         }
01658         $oRs = $oDb->select( $sQ );
01659         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01660 
01661             if ( $this->_blMallUsers ) {
01662 
01663                 $blExists = true;
01664                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01665 
01666                     // password is not set - allow to override
01667                     $blExists = false;
01668                 }
01669             } else {
01670 
01671                 $blExists = false;
01672                 while ( !$oRs->EOF ) {
01673                     if ( $oRs->fields[1] != 'user' ) {
01674 
01675                         // exists admin with same login - must not allow
01676                         $blExists = true;
01677                         break;
01678                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01679 
01680                         // exists same login (with password) in same shop
01681                         $blExists = true;
01682                         break;
01683                     }
01684 
01685                     $oRs->moveNext();
01686                 }
01687             }
01688         }
01689         return $blExists;
01690     }
01691 
01699     public function getUserRecommLists( $sOXID = null )
01700     {
01701         if ( !$sOXID )
01702             $sOXID = $this->getId();
01703 
01704         // sets active page
01705         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01706         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01707 
01708         // load only lists which we show on screen
01709         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01710         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01711 
01712 
01713         $oRecommList = oxNew( 'oxlist' );
01714         $oRecommList->init( 'oxrecommlist' );
01715         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01716         $iShopId = $this->getConfig()->getShopId();
01717         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01718         $oRecommList->selectString( $sSelect );
01719 
01720         return $oRecommList;
01721     }
01722 
01730     public function getRecommListsCount( $sOx = null )
01731     {
01732         if ( !$sOx ) {
01733             $sOx = $this->getId();
01734         }
01735 
01736         if ( $this->_iCntRecommLists === null || $sOx ) {
01737             $oDb = oxDb::getDb();
01738             $this->_iCntRecommLists = 0;
01739             $iShopId = $this->getConfig()->getShopId();
01740             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01741             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01742         }
01743         return $this->_iCntRecommLists;
01744     }
01745 
01756     protected function _checkEmail( $sEmail )
01757     {
01758         oxRegistry::get("oxInputValidator")->checkEmail( $this, $sEmail );
01759     }
01760 
01773     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01774     {
01775         return oxRegistry::get("oxInputValidator")->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01776     }
01777 
01788     protected function _checkCountries( $aInvAddress, $aDelAddress )
01789     {
01790         oxRegistry::get("oxInputValidator")->checkCountries( $this, $aInvAddress, $aDelAddress );
01791     }
01792 
01804     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01805     {
01806         oxRegistry::get("oxInputValidator")->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01807     }
01808 
01819     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01820     {
01821         oxRegistry::get("oxInputValidator")->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01822     }
01823 
01834     protected function _checkVatId( $aInvAddress )
01835     {
01836         oxRegistry::get("oxInputValidator")->checkVatId( $this, $aInvAddress );
01837     }
01838 
01847     protected function _setAutoGroups( $sCountryId )
01848     {
01849         // assigning automatically to specific groups
01850         $blForeigner = true;
01851         $blForeignGroupExists = false;
01852         $blInlandGroupExists = false;
01853 
01854         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01855         // foreigner ?
01856         if ( is_array($aHomeCountry)) {
01857             if (in_array($sCountryId, $aHomeCountry)) {
01858                 $blForeigner = false;
01859             }
01860         } elseif ($sCountryId == $aHomeCountry) {
01861             $blForeigner = false;
01862         }
01863 
01864         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01865             $blForeignGroupExists = true;
01866             if ( !$blForeigner ) {
01867                 $this->removeFromGroup( 'oxidforeigncustomer' );
01868             }
01869         }
01870 
01871         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01872             $blInlandGroupExists = true;
01873             if ( $blForeigner ) {
01874                 $this->removeFromGroup( 'oxidnewcustomer' );
01875             }
01876         }
01877 
01878         if ( !$this->oxuser__oxdisableautogrp->value ) {
01879             if ( !$blForeignGroupExists && $blForeigner ) {
01880                 $this->addToGroup( 'oxidforeigncustomer' );
01881             }
01882             if ( !$blInlandGroupExists && !$blForeigner ) {
01883                 $this->addToGroup( 'oxidnewcustomer' );
01884             }
01885         }
01886     }
01887 
01888 
01897     public function loadUserByUpdateId( $sUid )
01898     {
01899         $oDb = oxDb::getDb();
01900         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
01901         if ( $sUserId = $oDb->getOne( $sQ ) ) {
01902             return $this->load( $sUserId );
01903         }
01904     }
01905 
01913     public function setUpdateKey( $blReset = false )
01914     {
01915         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
01916         $iUpTime = $blReset ? 0 : oxRegistry::get("oxUtilsDate")->getTime() + $this->getUpdateLinkTerm();
01917 
01918         // generating key
01919         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
01920 
01921         // setting expiration time for 6 hours
01922         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
01923 
01924         // saving
01925         $this->save();
01926     }
01927 
01933     public function getUpdateLinkTerm()
01934     {
01935         return 3600 * 6;
01936     }
01937 
01945     public function isExpiredUpdateId( $sKey )
01946     {
01947         $oDb = oxDb::getDb();
01948         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
01949         return !( (bool) $oDb->getOne( $sQ ) );
01950     }
01951 
01957     public function getUpdateId()
01958     {
01959         if ( $this->_sUpdateKey === null ) {
01960             $this->setUpdateKey();
01961             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
01962         }
01963         return $this->_sUpdateKey;
01964     }
01965 
01974     public function encodePassword( $sPassword, $sSalt )
01975     {
01976         $oDb = oxDb::getDb();
01977         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
01978     }
01979 
01987     public function prepareSalt( $sSalt )
01988     {
01989         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
01990     }
01991 
01999     public function decodeSalt( $sSaltHex )
02000     {
02001         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02002     }
02003 
02011     public function setPassword( $sPassword = null )
02012     {
02013         // setting salt if password is not empty
02014         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02015 
02016         // encoding only if password was not empty (e.g. user registration without pass)
02017         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02018 
02019         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02020         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02021     }
02022 
02030     public function isSamePassword( $sNewPass )
02031     {
02032         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02033     }
02034 
02040     public function isLoadedFromCookie()
02041     {
02042         return $this->_blLoadedFromCookie;
02043     }
02044 
02051     public function getPasswordHash()
02052     {
02053         $sHash = null;
02054         if ( $this->oxuser__oxpassword->value ) {
02055             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02056                 // decodable pass ?
02057                 $this->setPassword( oxRegistry::getUtils()->strRem( $this->oxuser__oxpassword->value ) );
02058             } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02059                 // plain pass ?
02060                 $this->setPassword( $this->oxuser__oxpassword->value );
02061             }
02062             $sHash = $this->oxuser__oxpassword->value;
02063         }
02064         return $sHash;
02065     }
02066 
02074     public function getReviewUserHash( $sUserId )
02075     {
02076         $oDb = oxDb::getDb();
02077         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02078         return $sReviewUserHash;
02079     }
02080 
02088     public function getReviewUserId( $sReviewUserHash )
02089     {
02090         $oDb = oxDb::getDb();
02091         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02092         return $sUserId;
02093     }
02094 
02100     public function getState()
02101     {
02102         return $this->oxuser__oxstateid->value;
02103     }
02104 
02110     public function isTermsAccepted()
02111     {
02112         $oDb = oxDb::getDb();
02113         $sShopId = $this->getConfig()->getShopId();
02114         $sUserId = $oDb->quote( $this->getId() );
02115         return (bool) $oDb->getOne( "select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'" );
02116     }
02117 
02123     public function acceptTerms()
02124     {
02125         $oDb = oxDb::getDb();
02126         $sUserId  = $oDb->quote( $this->getId() );
02127         $sShopId  = $this->getConfig()->getShopId();
02128         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02129 
02130         $oDb->execute( "replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02131     }
02132 
02142     public function setCreditPointsForRegistrant( $sUserId, $sRecEmail )
02143     {
02144         $blSet   = false;
02145         $oDb = oxDb::getDb();
02146         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02147         // check if this invitation is still not accepted
02148         $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);
02149         if ( $iPoints && $iPending ) {
02150             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02151             if ( $blSet = $this->save() ) {
02152                 // updating users statistics
02153                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail ) );
02154                 $oInvUser = oxNew( "oxuser" );
02155                 if ( $oInvUser->load( $sUserId ) ) {
02156                     $blSet = $oInvUser->setCreditPointsForInviter();
02157                 }
02158             }
02159         }
02160         oxSession::deleteVar( 'su' );
02161         oxSession::deleteVar( 're' );
02162 
02163         return $blSet;
02164     }
02165 
02171     public function setCreditPointsForInviter()
02172     {
02173         $blSet   = false;
02174         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02175         if ( $iPoints ) {
02176             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02177             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02178             $blSet = $this->save();
02179         }
02180 
02181         return $blSet;
02182     }
02183 
02189     public function updateFbId()
02190     {
02191         $oFb = oxRegistry::get("oxFb");
02192         $blRet = false;
02193 
02194         if ( $oFb->isConnected() && $oFb->getUser() ) {
02195              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02196              $blRet = $this->save();
02197         }
02198 
02199         return $blRet;
02200     }
02201 
02209     public function updateInvitationStatistics( $aRecEmail )
02210     {
02211         $oDb = oxDb::getDb();
02212         $sUserId = $this->getId();
02213 
02214         if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
02215             //iserting statistics about invitation
02216             $sDate = oxRegistry::get("oxUtilsDate")->formatDBDate( date("Y-m-d"), true );
02217             $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
02218             foreach ( $aRecEmail as $sRecEmail ) {
02219                 $sSql = "INSERT INTO oxinvitations SET oxuserid = ".$oDb->quote( $sUserId ).", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02220                 $oDb->execute( $sSql );
02221             }
02222         }
02223     }
02224 
02232     public function getIdByUserName( $sUserName )
02233     {
02234         $oDb = oxDb::getDb();
02235         $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = ". $oDb->quote( $sUserName );
02236         if ( !$this->getConfig()->getConfigParam( 'blMallUsers' ) ) {
02237             $sQ .= " AND `oxshopid` = ". $oDb->quote( $this->getConfig()->getShopId() );
02238         }
02239 
02240         return $oDb->getOne( $sQ );
02241 
02242     }
02243 
02249     public function hasAccount()
02250     {
02251 
02252         return (bool) $this->oxuser__oxpassword->value;
02253 
02254     }
02255 
02261     public function isPriceViewModeNetto()
02262     {
02263         return (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
02264     }
02265 
02266 }