oxuser.php

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