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         // change newsSubcription user id
00604         if ( isset($this->_oNewsSubscription) ) {
00605             $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField( $oxID, oxField::T_RAW);
00606         }
00607 
00608         return $blRet;
00609     }
00610 
00618     public function exists( $sOXID = null )
00619     {
00620         $oDb = oxDb::getDb();
00621         if ( !$sOXID ) {
00622             $sOXID = $this->getId();
00623         }
00624 
00625         $sShopSelect = '';
00626         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00627             $sShopSelect = ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00628         }
00629 
00630         //#4543 Query optimisation by splitting it into two, might need an logics optimisation as well
00631         if ( $sOXID ) {
00632             $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00633                     WHERE ( oxid = '.$oDb->quote( $sOXID ).' ) ';
00634             $sSelect .= $sShopSelect;
00635 
00636             if ( ( $sOxid = $oDb->getOne( $sSelect ) ) ) {
00637                 // update - set oxid
00638                 $this->setId( $sOxid );
00639                 return true;
00640             }
00641         }
00642 
00643         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00644                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).' ) ';
00645         $sSelect .= $sShopSelect;
00646 
00647         if ( ( $sOxid = $oDb->getOne( $sSelect ) ) ) {
00648              // update - set oxid
00649             $this->setId( $sOxid );
00650             return true;
00651         }
00652         return false;
00653     }
00654 
00663     public function getOrders( $iLimit = false, $iPage = 0 )
00664     {
00665         $myConfig = $this->getConfig();
00666         $oOrders = oxNew( 'oxlist' );
00667         $oOrders->init( 'oxorder' );
00668 
00669         if ( $iLimit !== false ) {
00670             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00671         }
00672 
00673         //P
00674         // Lists does not support loading from two tables, so orders
00675         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00676         // forcing to load product info which is used in templates
00677         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00678 
00679         //loading order for registered user
00680         if ( $this->oxuser__oxregister->value > 1 ) {
00681             $oDb = oxDb::getDb();
00682             $sQ = 'select * from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' and oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value ) . ' ';
00683 
00684             //#1546 - shopid check added, if it is not multishop
00685 
00686             $sQ .= ' order by oxorderdate desc ';
00687             $oOrders->selectString( $sQ );
00688         }
00689 
00690         return $oOrders;
00691     }
00692 
00698     public function getOrderCount()
00699     {
00700         $iCnt = 0;
00701         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00702             $oDb = oxDb::getDb();
00703             $sQ  = 'select count(*) from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00704             $iCnt = (int) $oDb->getOne( $sQ );
00705         }
00706 
00707         return $iCnt;
00708     }
00709 
00715     public function getNoticeListArtCnt()
00716     {
00717         if ( $this->_iCntNoticeListArticles === null ) {
00718             $this->_iCntNoticeListArticles = 0;
00719             if ( $this->getId() ) {
00720                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00721             }
00722         }
00723         return $this->_iCntNoticeListArticles;
00724     }
00725 
00731     public function getWishListArtCnt()
00732     {
00733         if ( $this->_iCntWishListArticles === null ) {
00734             $this->_iCntWishListArticles = false;
00735             if ( $this->getId() ) {
00736                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00737             }
00738         }
00739         return $this->_iCntWishListArticles;
00740     }
00741 
00748     public function getActiveCountry()
00749     {
00750         $sDeliveryCountry = '';
00751         $soxAddressId = oxSession::getVar( 'deladrid' );
00752         if ( $soxAddressId ) {
00753             $oDelAddress = oxNew( 'oxaddress' );
00754             $oDelAddress->load( $soxAddressId );
00755             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00756         } elseif ( $this->getId() ) {
00757             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00758         } else {
00759             $oUser = oxNew( 'oxuser' );
00760             if ( $oUser->loadActiveUser() ) {
00761                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00762             }
00763         }
00764 
00765         return $sDeliveryCountry;
00766     }
00767 
00775     public function createUser()
00776     {
00777         $oDb = oxDb::getDb();
00778         $sShopID = $this->getConfig()->getShopId();
00779 
00780         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00781         $sSelect = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00782         if ( !$this->_blMallUsers ) {
00783             $sSelect .= " and oxshopid = '{$sShopID}' ";
00784         }
00785         $sOXID = $oDb->getOne( $sSelect, false, false );
00786 
00787         // user without password found - lets use
00788         if ( isset( $sOXID ) && $sOXID ) {
00789             // try to update
00790             $this->delete( $sOXID );
00791         } elseif ( $this->_blMallUsers ) {
00792             // must be sure if there is no dublicate user
00793             $sQ = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00794             if ( $oDb->getOne( $sQ, false, false ) ) {
00795                 $oEx = oxNew( 'oxUserException' );
00796                 $oLang = oxRegistry::getLang();
00797                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00798                 throw $oEx;
00799             }
00800         }
00801 
00802         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00803         if ( ( $blOK = $this->save() ) ) {
00804             // dropping/cleaning old delivery address/payment info
00805             $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00806             $oDb->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00807         } else {
00808             $oEx = oxNew( 'oxUserException' );
00809             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00810             throw $oEx;
00811         }
00812 
00813         return $blOK;
00814     }
00815 
00823     public function addToGroup( $sGroupID )
00824     {
00825         if ( !$this->inGroup( $sGroupID ) ) {
00826             // create oxgroup object
00827             $oGroup = oxNew('oxGroups');
00828             if ( $oGroup->load($sGroupID) ) {
00829                 $oNewGroup = oxNew( 'oxobject2group' );
00830                 $oNewGroup->oxobject2group__oxobjectid = new oxField( $this->getId(), oxField::T_RAW );
00831                 $oNewGroup->oxobject2group__oxgroupsid = new oxField( $sGroupID, oxField::T_RAW );
00832                 if ( $oNewGroup->save() ) {
00833                     $this->_oGroups[$sGroupID] = $oGroup;
00834                     return true;
00835                 }
00836             }
00837         }
00838         return false;
00839     }
00840 
00848     public function removeFromGroup( $sGroupID = null )
00849     {
00850         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00851             $oGroups = oxNew( 'oxlist' );
00852             $oGroups->init( 'oxobject2group' );
00853             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00854             $oGroups->selectString( $sSelect );
00855             foreach ( $oGroups as $oRemgroup ) {
00856                 if ( $oRemgroup->delete() ) {
00857                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00858                 }
00859             }
00860         }
00861     }
00862 
00871     public function onOrderExecute( $oBasket, $iSuccess )
00872     {
00873 
00874         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00875             //adding user to particular customer groups
00876             if ( !$this->oxuser__oxdisableautogrp->value ) {
00877 
00878                 $myConfig = $this->getConfig();
00879                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00880                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00881 
00882                 $this->addToGroup( 'oxidcustomer' );
00883                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00884                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00885                     $this->addToGroup( 'oxidsmallcust' );
00886                 }
00887                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00888                     $this->addToGroup( 'oxidmiddlecust' );
00889                 }
00890                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00891                     $this->addToGroup( 'oxidgoodcust' );
00892                 }
00893             }
00894 
00895             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00896                 $this->removeFromGroup( 'oxidnotyetordered' );
00897             }
00898         }
00899     }
00900 
00908     public function getBasket( $sName )
00909     {
00910         if ( !isset( $this->_aBaskets[$sName] ) ) {
00911             $oBasket = oxNew( 'oxuserbasket' );
00912             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00913 
00914             // creating if it does not exist
00915             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00916                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00917                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00918 
00919                 // marking basket as new (it will not be saved in DB yet)
00920                 $oBasket->setIsNewBasket();
00921             }
00922 
00923             $this->_aBaskets[$sName] = $oBasket;
00924         }
00925 
00926         return $this->_aBaskets[$sName];
00927     }
00928 
00937     public function convertBirthday( $aData )
00938     {
00939 
00940         // preparing data to process
00941         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00942         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00943         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00944 
00945         // leaving empty if not set
00946         if ( !$iYear && !$iMonth && !$iDay )
00947             return "";
00948 
00949         // year
00950         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00951             $iYear = date('Y');
00952 
00953         // month
00954         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00955             $iMonth = 1;
00956 
00957         // maximum nuber of days in month
00958         $iMaxDays = 31;
00959         switch( $iMonth) {
00960             case 2 :
00961                 if ($iMaxDays > 28)
00962                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00963                 break;
00964             case 4  :
00965             case 6  :
00966             case 9  :
00967             case 11 :
00968                 $iMaxDays = min(30, $iMaxDays);
00969                 break;
00970         }
00971 
00972         // day
00973         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00974             $iDay = 1;
00975         }
00976 
00977         // whole date
00978         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00979     }
00980 
00987     public function getBoni()
00988     {
00989         return 1000;
00990     }
00991 
01004     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
01005     {
01006         // preparing input
01007         $sDynGoup = strtolower( trim( $sDynGoup ) );
01008 
01009         // setting denied groups from admin settings also
01010         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
01011 
01012         // default state ..
01013         $blAdd = false;
01014 
01015         // user assignment to dyn group is not allowed
01016         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
01017             $blAdd = false;
01018         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
01019             // trying to add user to prohibited user group?
01020             $blAdd = false;
01021         } elseif ( $this->addToGroup( $sDynGoup ) ) {
01022             $blAdd = true;
01023         }
01024 
01025         // cleanup
01026         oxSession::deleteVar( 'dgr' );
01027 
01028         return $blAdd;
01029     }
01030 
01049     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01050     {
01051         // 1. checking user name
01052         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01053 
01054         // 2. cheking email
01055         $this->_checkEmail( $sLogin );
01056 
01057         // 3. password
01058         $this->checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01059 
01060         // 4. required fields
01061         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01062 
01063         // 5. country check
01064         $this->_checkCountries( $aInvAddress, $aDelAddress );
01065 
01066         // 6. vat id check.
01067             $this->_checkVatId( $aInvAddress );
01068 
01069 
01070         // throwing first validation error
01071         if ( $oError = oxRegistry::get("oxInputValidator")->getFirstValidationError() ) {
01072             throw $oError;
01073         }
01074     }
01075 
01085     public function setNewsSubscription( $blSubscribe, $blSendOptIn, $blForceCheckOptIn = false )
01086     {
01087         // assigning to newsletter
01088         $blSuccess = false;
01089         $myConfig  = $this->getConfig();
01090         $mySession = $this->getSession();
01091 
01092         // user wants to get newsletter messages or no ?
01093         $oNewsSubscription = $this->getNewsSubscription();
01094         if ( $oNewsSubscription ) {
01095             if ( $blSubscribe && ($blForceCheckOptIn || ( $iOptInStatus = $oNewsSubscription->getOptInStatus() ) != 1) ) {
01096                 if ( !$blSendOptIn ) {
01097 
01098                     // double-opt-in check is disabled - assigning automatically
01099                     $this->addToGroup( 'oxidnewsletter' );
01100                     // and setting subscribed status
01101                     $oNewsSubscription->setOptInStatus( 1 );
01102                     $blSuccess = true;
01103                 } else {
01104 
01105                     // double-opt-in check enabled - sending confirmation email and setting waiting status
01106                     if ( $iOptInStatus != 2 ) {
01107                         // sending double-opt-in mail
01108                         $oEmail = oxNew( 'oxemail' );
01109                         $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01110                     } else {
01111                         // mail already was sent, so just confirming that
01112                         $blSuccess = true;
01113                     }
01114 
01115                     $oNewsSubscription->setOptInStatus( 2 );
01116                 }
01117             } elseif ( !$blSubscribe ) {
01118                 // removing user from newsletter subscribers
01119                 $this->removeFromGroup( 'oxidnewsletter' );
01120                 $oNewsSubscription->setOptInStatus( 0 );
01121                 $blSuccess = true;
01122             }
01123         }
01124 
01125         return $blSuccess;
01126     }
01127 
01145     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01146     {
01147         // validating values before saving. If validation fails - exception is thrown
01148         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01149         // input data is fine - lets save updated user info
01150         $this->assign( $aInvAddress );
01151 
01152 
01153         // update old or add new delivery address
01154         $this->_assignAddress( $aDelAddress );
01155 
01156         // saving new values
01157         if ( $this->save() ) {
01158 
01159             // assigning automatically to specific groups
01160             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01161             $this->_setAutoGroups( $sCountryId );
01162         }
01163     }
01164 
01172     protected function _assignAddress( $aDelAddress )
01173     {
01174         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01175             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01176             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01177             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01178             $oAddress = oxNew( 'oxaddress' );
01179             $oAddress->assign( $aDelAddress );
01180             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01181             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01182             $oAddress->save();
01183 
01184             // resetting addresses
01185             $this->_aAddresses = null;
01186 
01187             // saving delivery Address for later use
01188             oxSession::setVar( 'deladrid', $oAddress->getId() );
01189         } else {
01190             // resetting
01191             oxSession::setVar( 'deladrid', null );
01192         }
01193     }
01194 
01205     protected function _getLoginQuery( $sUser, $sPassword, $sShopID, $blAdmin )
01206     {
01207         $myConfig = $this->getConfig();
01208         $oDb = oxDb::getDb();
01209 
01210         $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01211         $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01212         $sShopSelect = "";
01213 
01214 
01215         // admin view: can only login with higher than 'user' rights
01216         if ( $blAdmin ) {
01217             $sShopSelect = " and ( oxrights != 'user' ) ";
01218         }
01219 
01220         $blStagingMode = false;
01221         $blDemoMode = false;
01222         $sWhat = "oxid";
01223             if ($myConfig->isDemoShop()) {
01224                 $blDemoMode = true;
01225             }
01226 
01227         $sSelect = "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01228         if ( ( $blDemoMode || $blStagingMode ) && $blAdmin ) {
01229             if ( $sPassword == "admin" && $sUser == "admin" ) {
01230                 $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' ";
01231             } elseif ( $blDemoMode ) {
01232                 $oEx = oxNew( 'oxUserException' );
01233                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01234                 throw $oEx;
01235             }
01236         }
01237 
01238         return $sSelect;
01239     }
01240 
01250     protected function _getShopSelect( $myConfig, $sShopID, $blAdmin )
01251     {
01252         $sShopSelect = "";
01253         // admin view: can only login with higher than 'user' rights
01254         if ( $blAdmin ) {
01255             $sShopSelect = " and ( oxrights != 'user' ) ";
01256         }
01257 
01258         return $sShopSelect;
01259     }
01267     protected function _loadSavedUserBasketAfterLogin()
01268     {
01269         if ( !$this->isAdmin() && !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' )) {
01270             //load basket from the database
01271             try {
01272                 if ( $oBasket = $this->getSession()->getBasket() ) {
01273                     $oBasket->load();
01274                 }
01275             } catch ( Exception $oE ) {
01276                 //just ignore it
01277             }
01278         }
01279     }
01280 
01281 
01294     public function login( $sUser, $sPassword, $blCookie = false)
01295     {
01296         if ( $this->isAdmin() && !count( oxRegistry::get("oxUtilsServer")->getOxCookie() ) ) {
01297             $oEx = oxNew( 'oxCookieException' );
01298             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01299             throw $oEx;
01300         }
01301 
01302         $myConfig = $this->getConfig();
01303         if ( $sPassword ) {
01304 
01305             $sShopID = $myConfig->getShopId();
01306             $sSelect = $this->_getLoginQuery( $sUser, $sPassword, $sShopID, $this->isAdmin() );
01307 
01308             // load from DB
01309             $aData = oxDb::getDb()->getAll( $sSelect );
01310             $sOXID = @$aData[0][0];
01311             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01312 
01313                 if ( !$this->load( $sOXID ) ) {
01314                     $oEx = oxNew( 'oxUserException' );
01315                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01316                     throw $oEx;
01317                 }
01318             }
01319         }
01320 
01321 
01322         //login successfull?
01323         if ( $this->oxuser__oxid->value ) {
01324             // yes, successful login
01325 
01326             //reseting active user
01327             $this->setUser( null );
01328 
01329             if ( $this->isAdmin() ) {
01330                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01331             } else {
01332                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01333             }
01334 
01335             // cookie must be set ?
01336             if ( $blCookie && $myConfig->getConfigParam( 'blShowRememberMe' ) ) {
01337                 oxRegistry::get("oxUtilsServer")->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value );
01338             }
01339 
01340             return true;
01341         } else {
01342             $oEx = oxNew( 'oxUserException' );
01343             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01344             throw $oEx;
01345         }
01346     }
01347 
01353     public function logout()
01354     {
01355         // deleting session info
01356         oxSession::deleteVar( 'usr' );  // for front end
01357         oxSession::deleteVar( 'auth' ); // for back end
01358         oxSession::deleteVar( 'dgr' );
01359         oxSession::deleteVar( 'dynvalue' );
01360         oxSession::deleteVar( 'paymentid' );
01361         // oxSession::deleteVar( 'deladrid' );
01362 
01363         // delete cookie
01364         oxRegistry::get("oxUtilsServer")->deleteUserCookie( $this->getConfig()->getShopID() );
01365 
01366         // unsetting global user
01367         $this->setUser( null );
01368 
01369         return true;
01370     }
01371 
01378     public function loadAdminUser()
01379     {
01380         return $this->loadActiveUser( true );
01381     }
01382 
01391     public function loadActiveUser( $blForceAdmin = false )
01392     {
01393         $myConfig = $this->getConfig();
01394 
01395         $blAdmin = $this->isAdmin() || $blForceAdmin;
01396         $oDb = oxDb::getDb();
01397 
01398         // first - checking session info
01399         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01400         $blFoundInCookie = false;
01401 
01402         //trying automatic login (by 'remember me' cookie)
01403         if ( !$sUserID && !$blAdmin && $myConfig->getConfigParam('blShowRememberMe') ) {
01404             $sShopID = $myConfig->getShopId();
01405             if ( ( $sSet = oxRegistry::get("oxUtilsServer")->getUserCookie( $sShopID ) ) ) {
01406                 $aData = explode( '@@@', $sSet );
01407                 $sUser = $aData[0];
01408                 $sPWD  = @$aData[1];
01409 
01410                 $sSelect =  'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDb->quote($sUser);
01411 
01412                 $rs = $oDb->select( $sSelect );
01413                 if ( $rs != false && $rs->recordCount() > 0 ) {
01414                     while (!$rs->EOF) {
01415                         $sTest = crypt( $rs->fields[1], $rs->fields[2] );
01416                         if ( $sTest == $sPWD ) {
01417                             // found
01418                             $sUserID = $rs->fields[0];
01419                             $blFoundInCookie = true;
01420                             break;
01421                         }
01422                         $rs->moveNext();
01423                     }
01424                 }
01425             }
01426         }
01427 
01428         // Checking if user is connected via Facebook connect.
01429         // If yes, trying to login user using user Facebook ID
01430         if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01431             $oFb = oxRegistry::get("oxFb");
01432             if ( $oFb->isConnected() && $oFb->getUser() ) {
01433                 $sUserSelect = "oxuser.oxfbid = " . $oDb->quote( $oFb->getUser() );
01434                 $sShopSelect = "";
01435 
01436 
01437                 $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01438                 $sUserID = $oDb->getOne( $sSelect );
01439             }
01440         }
01441 
01442         // checking user results
01443         if ( $sUserID ) {
01444             if ( $this->load( $sUserID ) ) {
01445                 // storing into session
01446                 if ($blAdmin) {
01447                     oxSession::setVar( 'auth', $sUserID );
01448                 } else {
01449                     oxSession::setVar( 'usr', $sUserID );
01450                 }
01451 
01452                 // marking the way user was loaded
01453                 $this->_blLoadedFromCookie = $blFoundInCookie;
01454                 return true;
01455             }
01456         } else {
01457             // no user
01458             if ($blAdmin) {
01459                 oxSession::deleteVar( 'auth' );
01460             } else {
01461                 oxSession::deleteVar( 'usr' );
01462             }
01463 
01464             return false;
01465         }
01466     }
01467 
01480     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01481     {
01482         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01483         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01484 
01485         // maybe this is LDAP user but supplied email Address instead of LDAP login
01486         $oDb = oxDb::getDb();
01487         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01488         if ( isset( $sLDAPKey) && $sLDAPKey) {
01489             $sUser = $sLDAPKey;
01490         }
01491 
01492         //$throws oxConnectionException
01493         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01494 
01495         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01496         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01497             // login successful
01498 
01499             // check if user is already in database
01500             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01501             $sOXID = $oDb->getOne( $sSelect );
01502 
01503             if ( !isset( $sOXID ) || !$sOXID ) {
01504                 // we need to create a new user
01505                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01506                 $this->setId();
01507 
01508                 // map all user data fields
01509                 foreach ( $aData as $fldname => $value) {
01510                     $sField = "oxuser__".strtolower( $fldname);
01511                     $this->$sField = new oxField( $aData[$fldname] );
01512                 }
01513 
01514                 $this->oxuser__oxactive  = new oxField( 1 );
01515                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01516                 $this->oxuser__oxldapkey = new oxField( $sUser );
01517                 $this->oxuser__oxrights  = new oxField( "user" );
01518                 $this->setPassword( "ldap user" );
01519 
01520                 $this->save();
01521             } else {
01522                 // LDAP user is already in OXID DB, load it
01523                 $this->load( $sOXID);
01524             }
01525 
01526         } else {
01527             $oEx = oxNew( 'oxUserException' );
01528             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01529             throw $oEx;
01530         }
01531     }
01532 
01539     protected function _getUserRights()
01540     {
01541         // previously user had no rights defined
01542         if ( !$this->oxuser__oxrights->value )
01543             return 'user';
01544 
01545         $oDb = oxDb::getDb();
01546         $myConfig    = $this->getConfig();
01547         $sAuthRights = null;
01548 
01549         // choosing possible user rights index
01550         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01551         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01552         if ( $sAuthUserID ) {
01553             $sAuthRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $sAuthUserID ) );
01554         }
01555 
01556         //preventing user rights edit for non admin
01557         $aRights = array();
01558 
01559         // selecting current users rights ...
01560         if ( $sCurrRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $this->getId() ) ) ) {
01561             $aRights[] = $sCurrRights;
01562         }
01563         $aRights[] = 'user';
01564 
01565         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01566             return current( $aRights );
01567         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01568             $aRights[] = $sAuthRights;
01569             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01570                 return current( $aRights );
01571             }
01572         }
01573 
01574         // leaving as it was set ...
01575         return $this->oxuser__oxrights->value;
01576     }
01577 
01583     protected function _insert()
01584     {
01585 
01586         // set oxcreate date
01587         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01588 
01589         if ( !isset( $this->oxuser__oxboni->value ) ) {
01590             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01591         }
01592 
01593         return parent::_insert();
01594     }
01595 
01601     protected function _update()
01602     {
01603         //V #M418: for not registered users, don't change boni during update
01604         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01605             $this->_aSkipSaveFields[] = 'oxboni';
01606         }
01607 
01608         // don't change this field
01609         $this->_aSkipSaveFields[] = 'oxcreate';
01610         if ( !$this->isAdmin() ) {
01611             $this->_aSkipSaveFields[] = 'oxcustnr';
01612             $this->_aSkipSaveFields[] = 'oxrights';
01613         }
01614 
01615         // updating subscription information
01616         if ( ( $blUpdate = parent::_update() ) ) {
01617             $this->getNewsSubscription()->updateSubscription( $this );
01618         }
01619 
01620         return $blUpdate;
01621     }
01622 
01638     protected function _checkLogin( $sLogin, $aInvAddress )
01639     {
01640         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01641         oxRegistry::get("oxInputValidator")->checkLogin( $this, $sLogin, $aInvAddress );
01642         return $sLogin;
01643     }
01644 
01652     public function checkIfEmailExists( $sEmail )
01653     {
01654         $myConfig = $this->getConfig();
01655         $oDb = oxDb::getDb();
01656         $iShopId = $myConfig->getShopId();
01657         $blExists = false;
01658 
01659         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDb->quote( $sEmail );
01660         if ( ( $sOxid = $this->getId() ) ) {
01661             $sQ .= " and oxid <> ".$oDb->quote( $sOxid );
01662         }
01663         $oRs = $oDb->select( $sQ );
01664         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01665 
01666             if ( $this->_blMallUsers ) {
01667 
01668                 $blExists = true;
01669                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01670 
01671                     // password is not set - allow to override
01672                     $blExists = false;
01673                 }
01674             } else {
01675 
01676                 $blExists = false;
01677                 while ( !$oRs->EOF ) {
01678                     if ( $oRs->fields[1] != 'user' ) {
01679 
01680                         // exists admin with same login - must not allow
01681                         $blExists = true;
01682                         break;
01683                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01684 
01685                         // exists same login (with password) in same shop
01686                         $blExists = true;
01687                         break;
01688                     }
01689 
01690                     $oRs->moveNext();
01691                 }
01692             }
01693         }
01694         return $blExists;
01695     }
01696 
01704     public function getUserRecommLists( $sOXID = null )
01705     {
01706         if ( !$sOXID )
01707             $sOXID = $this->getId();
01708 
01709         // sets active page
01710         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01711         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01712 
01713         // load only lists which we show on screen
01714         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01715         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01716 
01717 
01718         $oRecommList = oxNew( 'oxlist' );
01719         $oRecommList->init( 'oxrecommlist' );
01720         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01721         $iShopId = $this->getConfig()->getShopId();
01722         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01723         $oRecommList->selectString( $sSelect );
01724 
01725         return $oRecommList;
01726     }
01727 
01735     public function getRecommListsCount( $sOx = null )
01736     {
01737         if ( !$sOx ) {
01738             $sOx = $this->getId();
01739         }
01740 
01741         if ( $this->_iCntRecommLists === null || $sOx ) {
01742             $oDb = oxDb::getDb();
01743             $this->_iCntRecommLists = 0;
01744             $iShopId = $this->getConfig()->getShopId();
01745             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01746             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01747         }
01748         return $this->_iCntRecommLists;
01749     }
01750 
01761     protected function _checkEmail( $sEmail )
01762     {
01763         oxRegistry::get("oxInputValidator")->checkEmail( $this, $sEmail );
01764     }
01765 
01778     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01779     {
01780         return oxRegistry::get("oxInputValidator")->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01781     }
01782 
01793     protected function _checkCountries( $aInvAddress, $aDelAddress )
01794     {
01795         oxRegistry::get("oxInputValidator")->checkCountries( $this, $aInvAddress, $aDelAddress );
01796     }
01797 
01809     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01810     {
01811         oxRegistry::get("oxInputValidator")->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01812     }
01813 
01824     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01825     {
01826         oxRegistry::get("oxInputValidator")->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01827     }
01828 
01839     protected function _checkVatId( $aInvAddress )
01840     {
01841         oxRegistry::get("oxInputValidator")->checkVatId( $this, $aInvAddress );
01842     }
01843 
01852     protected function _setAutoGroups( $sCountryId )
01853     {
01854         // assigning automatically to specific groups
01855         $blForeigner = true;
01856         $blForeignGroupExists = false;
01857         $blInlandGroupExists = false;
01858 
01859         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01860         // foreigner ?
01861         if ( is_array($aHomeCountry)) {
01862             if (in_array($sCountryId, $aHomeCountry)) {
01863                 $blForeigner = false;
01864             }
01865         } elseif ($sCountryId == $aHomeCountry) {
01866             $blForeigner = false;
01867         }
01868 
01869         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01870             $blForeignGroupExists = true;
01871             if ( !$blForeigner ) {
01872                 $this->removeFromGroup( 'oxidforeigncustomer' );
01873             }
01874         }
01875 
01876         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01877             $blInlandGroupExists = true;
01878             if ( $blForeigner ) {
01879                 $this->removeFromGroup( 'oxidnewcustomer' );
01880             }
01881         }
01882 
01883         if ( !$this->oxuser__oxdisableautogrp->value ) {
01884             if ( !$blForeignGroupExists && $blForeigner ) {
01885                 $this->addToGroup( 'oxidforeigncustomer' );
01886             }
01887             if ( !$blInlandGroupExists && !$blForeigner ) {
01888                 $this->addToGroup( 'oxidnewcustomer' );
01889             }
01890         }
01891     }
01892 
01893 
01902     public function loadUserByUpdateId( $sUid )
01903     {
01904         $oDb = oxDb::getDb();
01905         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
01906         if ( $sUserId = $oDb->getOne( $sQ ) ) {
01907             return $this->load( $sUserId );
01908         }
01909     }
01910 
01918     public function setUpdateKey( $blReset = false )
01919     {
01920         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
01921         $iUpTime = $blReset ? 0 : oxRegistry::get("oxUtilsDate")->getTime() + $this->getUpdateLinkTerm();
01922 
01923         // generating key
01924         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
01925 
01926         // setting expiration time for 6 hours
01927         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
01928 
01929         // saving
01930         $this->save();
01931     }
01932 
01938     public function getUpdateLinkTerm()
01939     {
01940         return 3600 * 6;
01941     }
01942 
01950     public function isExpiredUpdateId( $sKey )
01951     {
01952         $oDb = oxDb::getDb();
01953         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
01954         return !( (bool) $oDb->getOne( $sQ ) );
01955     }
01956 
01962     public function getUpdateId()
01963     {
01964         if ( $this->_sUpdateKey === null ) {
01965             $this->setUpdateKey();
01966             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
01967         }
01968         return $this->_sUpdateKey;
01969     }
01970 
01979     public function encodePassword( $sPassword, $sSalt )
01980     {
01981         $oDb = oxDb::getDb();
01982         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
01983     }
01984 
01992     public function prepareSalt( $sSalt )
01993     {
01994         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
01995     }
01996 
02004     public function decodeSalt( $sSaltHex )
02005     {
02006         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02007     }
02008 
02016     public function setPassword( $sPassword = null )
02017     {
02018         // setting salt if password is not empty
02019         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02020 
02021         // encoding only if password was not empty (e.g. user registration without pass)
02022         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02023 
02024         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02025         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02026     }
02027 
02035     public function isSamePassword( $sNewPass )
02036     {
02037         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02038     }
02039 
02045     public function isLoadedFromCookie()
02046     {
02047         return $this->_blLoadedFromCookie;
02048     }
02049 
02056     public function getPasswordHash()
02057     {
02058         $sHash = null;
02059         if ( $this->oxuser__oxpassword->value ) {
02060             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02061                 // decodable pass ?
02062                 $this->setPassword( oxRegistry::getUtils()->strRem( $this->oxuser__oxpassword->value ) );
02063             } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02064                 // plain pass ?
02065                 $this->setPassword( $this->oxuser__oxpassword->value );
02066             }
02067             $sHash = $this->oxuser__oxpassword->value;
02068         }
02069         return $sHash;
02070     }
02071 
02079     public function getReviewUserHash( $sUserId )
02080     {
02081         $oDb = oxDb::getDb();
02082         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02083         return $sReviewUserHash;
02084     }
02085 
02093     public function getReviewUserId( $sReviewUserHash )
02094     {
02095         $oDb = oxDb::getDb();
02096         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02097         return $sUserId;
02098     }
02099 
02105     public function getState()
02106     {
02107         return $this->oxuser__oxstateid->value;
02108     }
02109 
02115     public function isTermsAccepted()
02116     {
02117         $oDb = oxDb::getDb();
02118         $sShopId = $this->getConfig()->getShopId();
02119         $sUserId = $oDb->quote( $this->getId() );
02120         return (bool) $oDb->getOne( "select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'" );
02121     }
02122 
02128     public function acceptTerms()
02129     {
02130         $oDb = oxDb::getDb();
02131         $sUserId  = $oDb->quote( $this->getId() );
02132         $sShopId  = $this->getConfig()->getShopId();
02133         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02134 
02135         $oDb->execute( "replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02136     }
02137 
02147     public function setCreditPointsForRegistrant( $sUserId, $sRecEmail )
02148     {
02149         $blSet   = false;
02150         $oDb = oxDb::getDb();
02151         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02152         // check if this invitation is still not accepted
02153         $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);
02154         if ( $iPoints && $iPending ) {
02155             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02156             if ( $blSet = $this->save() ) {
02157                 // updating users statistics
02158                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail ) );
02159                 $oInvUser = oxNew( "oxuser" );
02160                 if ( $oInvUser->load( $sUserId ) ) {
02161                     $blSet = $oInvUser->setCreditPointsForInviter();
02162                 }
02163             }
02164         }
02165         oxSession::deleteVar( 'su' );
02166         oxSession::deleteVar( 're' );
02167 
02168         return $blSet;
02169     }
02170 
02176     public function setCreditPointsForInviter()
02177     {
02178         $blSet   = false;
02179         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02180         if ( $iPoints ) {
02181             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02182             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02183             $blSet = $this->save();
02184         }
02185 
02186         return $blSet;
02187     }
02188 
02194     public function updateFbId()
02195     {
02196         $oFb = oxRegistry::get("oxFb");
02197         $blRet = false;
02198 
02199         if ( $oFb->isConnected() && $oFb->getUser() ) {
02200              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02201              $blRet = $this->save();
02202         }
02203 
02204         return $blRet;
02205     }
02206 
02214     public function updateInvitationStatistics( $aRecEmail )
02215     {
02216         $oDb = oxDb::getDb();
02217         $sUserId = $this->getId();
02218 
02219         if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
02220             //iserting statistics about invitation
02221             $sDate = oxRegistry::get("oxUtilsDate")->formatDBDate( date("Y-m-d"), true );
02222             $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
02223             foreach ( $aRecEmail as $sRecEmail ) {
02224                 $sSql = "INSERT INTO oxinvitations SET oxuserid = ".$oDb->quote( $sUserId ).", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02225                 $oDb->execute( $sSql );
02226             }
02227         }
02228     }
02229 
02237     public function getIdByUserName( $sUserName )
02238     {
02239         $oDb = oxDb::getDb();
02240         $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = ". $oDb->quote( $sUserName );
02241         if ( !$this->getConfig()->getConfigParam( 'blMallUsers' ) ) {
02242             $sQ .= " AND `oxshopid` = ". $oDb->quote( $this->getConfig()->getShopId() );
02243         }
02244 
02245         return $oDb->getOne( $sQ );
02246 
02247     }
02248 
02254     public function hasAccount()
02255     {
02256 
02257         return (bool) $this->oxuser__oxpassword->value;
02258 
02259     }
02260 
02266     public function isPriceViewModeNetto()
02267     {
02268         return (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
02269     }
02270 
02271 }