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             $oDb = oxDb::getDb();
00819             $sQ = "select 1 from oxgroups where oxid=" . $oDb->quote( $sGroupID );
00820             if ( $oDb->getOne( $sQ ) ) {
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] = $oNewGroup;
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 ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
01086             if ( !$blSendOptIn ) {
01087 
01088                 // double-opt-in check is disabled - assigning automatically
01089                 $this->addToGroup( 'oxidnewsletter' );
01090                 // and setting subscribed status
01091                 $oNewsSubscription->setOptInStatus( 1 );
01092                 $blSuccess = true;
01093             } else {
01094 
01095                 $oNewsSubscription->setOptInStatus( 2 );
01096 
01097                 // double-opt-in check enabled - sending confirmation email and setting waiting status
01098                 if ( !$mySession->getVar( "blDBOptInMailAlreadyDone" ) ) {
01099 
01100                     // sending double-opt-in mail
01101                     $oEmail = oxNew( 'oxemail' );
01102                     $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01103 
01104                     if ( $blSuccess ) {
01105                         //setting in seesion parameter to force sending email only once (#2033)
01106                         oxSession::setVar( "blDBOptInMailAlreadyDone", true );
01107                     }
01108                 } else {
01109                     // mail already was sent, so just confirming that
01110                     $blSuccess = true;
01111                 }
01112             }
01113         } elseif ( !$blSubscribe ) {
01114             // removing user from newsletter subscribers
01115             $this->removeFromGroup( 'oxidnewsletter' );
01116             $oNewsSubscription->setOptInStatus( 0 );
01117             $blSuccess = true;
01118         }
01119 
01120         return $blSuccess;
01121     }
01122 
01140     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01141     {
01142         // validating values before saving. If validation fails - exception is thrown
01143         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01144 
01145         // input data is fine - lets save updated user info
01146         $this->assign( $aInvAddress );
01147 
01148 
01149         // update old or add new delivery address
01150         $this->_assignAddress( $aDelAddress );
01151 
01152         // saving new values
01153         if ( $this->save() ) {
01154 
01155             // assigning automatically to specific groups
01156             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01157             $this->_setAutoGroups( $sCountryId );
01158         }
01159     }
01160 
01171     public function addUserAddress( $oUser )
01172     {
01173         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01174             return false;
01175         }
01176 
01177         $oAddress = oxNew( 'oxaddress' );
01178         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01179         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01180         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01181         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01182         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01183         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01184         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01185         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01186         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01187         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01188         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01189 
01190         // adding new address
01191         if ( $oAddress->save() ) {
01192             // resetting addresses
01193             $this->_aAddresses = null;
01194             return $oAddress->getId();
01195         }
01196     }
01197 
01205     protected function _assignAddress( $aDelAddress )
01206     {
01207         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01208             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01209             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01210             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01211             $oAddress = oxNew( 'oxaddress' );
01212             $oAddress->assign( $aDelAddress );
01213             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01214             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01215             $oAddress->save();
01216 
01217             // resetting addresses
01218             $this->_aAddresses = null;
01219 
01220             // saving delivery Address for later use
01221             oxSession::setVar( 'deladrid', $oAddress->getId() );
01222         } else {
01223             // resetting
01224             oxSession::setVar( 'deladrid', null );
01225         }
01226     }
01227 
01238     protected function _getLoginQuery( $sUser, $sPassword, $sShopID, $blAdmin )
01239     {
01240         $myConfig = $this->getConfig();
01241         $oDb = oxDb::getDb();
01242 
01243         $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01244         $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01245         $sShopSelect = "";
01246 
01247 
01248         // admin view: can only login with higher than 'user' rights
01249         if ( $blAdmin ) {
01250             $sShopSelect = " and ( oxrights != 'user' ) ";
01251         }
01252 
01253         $sWhat = "oxid";
01254 
01255         $sSelect = "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01256         if ( $myConfig->isDemoShop() && $blAdmin ) {
01257             if ( $sPassword == "admin" && $sUser == "admin" ) {
01258                 $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01259             } else {
01260                 $oEx = oxNew( 'oxUserException' );
01261                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01262                 throw $oEx;
01263             }
01264         }
01265 
01266         return $sSelect;
01267     }
01268 
01276     protected function _loadSavedUserBasketAfterLogin()
01277     {
01278         if ( !$this->isAdmin() ) {
01279             //load basket from the database
01280             try {
01281                 if ( $oBasket = $this->getSession()->getBasket() ) {
01282                     $oBasket->load();
01283                 }
01284             } catch ( Exception $oE ) {
01285                 //just ignore it
01286             }
01287         }
01288     }
01289 
01302     public function login( $sUser, $sPassword, $blCookie = false)
01303     {
01304         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01305             $oEx = oxNew( 'oxCookieException' );
01306             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01307             throw $oEx;
01308         }
01309 
01310         $myConfig = $this->getConfig();
01311         if ( $sPassword ) {
01312 
01313             $sShopID = $myConfig->getShopId();
01314             $sSelect = $this->_getLoginQuery( $sUser, $sPassword, $sShopID, $this->isAdmin() );
01315 
01316             // load from DB
01317             $aData = oxDb::getDb()->getAll( $sSelect );
01318             $sOXID = @$aData[0][0];
01319             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01320 
01321                 if ( !$this->load( $sOXID ) ) {
01322                     $oEx = oxNew( 'oxUserException' );
01323                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01324                     throw $oEx;
01325                 }
01326             }
01327         }
01328 
01329 
01330         //login successfull?
01331         if ( $this->oxuser__oxid->value ) {
01332             // yes, successful login
01333 
01334             //reseting active user
01335             $this->setUser( null );
01336 
01337             if ( $this->isAdmin() ) {
01338                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01339             } else {
01340                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01341             }
01342 
01343             // cookie must be set ?
01344             if ( $blCookie ) {
01345                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01346             }
01347 
01348             //load basket from the database
01349             $this->_loadSavedUserBasketAfterLogin();
01350 
01351             return true;
01352         } else {
01353             $oEx = oxNew( 'oxUserException' );
01354             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01355             throw $oEx;
01356         }
01357     }
01358 
01364     public function logout()
01365     {
01366         // deleting session info
01367         oxSession::deleteVar( 'usr' );  // for front end
01368         oxSession::deleteVar( 'auth' ); // for back end
01369         oxSession::deleteVar( 'dgr' );
01370         oxSession::deleteVar( 'dynvalue' );
01371         oxSession::deleteVar( 'paymentid' );
01372         // oxSession::deleteVar( 'deladrid' );
01373 
01374         // delete cookie
01375         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01376 
01377         // unsetting global user
01378         $this->setUser( null );
01379 
01380         return true;
01381     }
01382 
01389     public function loadAdminUser()
01390     {
01391         return $this->loadActiveUser( true );
01392     }
01393 
01402     public function loadActiveUser( $blForceAdmin = false )
01403     {
01404         $myConfig = $this->getConfig();
01405 
01406         $blAdmin = $this->isAdmin() || $blForceAdmin;
01407         $oDB = oxDb::getDb();
01408 
01409         // first - checking session info
01410         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01411         $blFoundInCookie = false;
01412 
01413         //trying automatic login (by 'remember me' cookie)
01414         if ( !$sUserID && !$blAdmin ) {
01415             $sShopID = $myConfig->getShopId();
01416             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01417                 $aData = explode( '@@@', $sSet );
01418                 $sUser = $aData[0];
01419                 $sPWD  = @$aData[1];
01420 
01421                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDB->quote($sUser);
01422 
01423 
01424                 $rs = $oDB->execute( $sSelect );
01425                 if ( $rs != false && $rs->recordCount() > 0 ) {
01426                     while (!$rs->EOF) {
01427                         $sTest = crypt( $rs->fields[1], 'ox' );
01428                         if ( $sTest == $sPWD ) {
01429                             // found
01430                             $sUserID = $rs->fields[0];
01431                             $blFoundInCookie = true;
01432                             break;
01433                         }
01434                         $rs->moveNext();
01435                     }
01436                 }
01437             }
01438         }
01439 
01440         // Checking if user is connected via Facebook connect.
01441         // If yes, trying to login user using user Facebook ID
01442         if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01443             $oFb = oxFb::getInstance();
01444             if ( $oFb->isConnected() && $oFb->getUser() ) {
01445                 $sUserSelect = "oxuser.oxfbid = " . $oDB->quote( $oFb->getUser() );
01446                 $sShopSelect = "";
01447 
01448 
01449                 $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01450                 $sUserID = $oDB->getOne( $sSelect );
01451             }
01452         }
01453 
01454         // checking user results
01455         if ( $sUserID ) {
01456             if ( $this->load( $sUserID ) ) {
01457                 // storing into session
01458                 if ($blAdmin) {
01459                     oxSession::setVar( 'auth', $sUserID );
01460                 } else {
01461                     oxSession::setVar( 'usr', $sUserID );
01462                 }
01463 
01464                 // marking the way user was loaded
01465                 $this->_blLoadedFromCookie = $blFoundInCookie;
01466                 return true;
01467             }
01468         } else {
01469             // no user
01470             if ($blAdmin) {
01471                 oxSession::deleteVar( 'auth' );
01472             } else {
01473                 oxSession::deleteVar( 'usr' );
01474             }
01475 
01476             return false;
01477         }
01478     }
01479 
01492     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01493     {
01494         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01495         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01496 
01497         // maybe this is LDAP user but supplied email Address instead of LDAP login
01498         $oDb = oxDb::getDb();
01499         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01500         if ( isset( $sLDAPKey) && $sLDAPKey) {
01501             $sUser = $sLDAPKey;
01502         }
01503 
01504         //$throws oxConnectionException
01505         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01506 
01507         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01508         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01509             // login successful
01510 
01511             // check if user is already in database
01512             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01513             $sOXID = $oDb->getOne( $sSelect );
01514 
01515             if ( !isset( $sOXID ) || !$sOXID ) {
01516                 // we need to create a new user
01517                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01518                 $this->setId();
01519 
01520                 // map all user data fields
01521                 foreach ( $aData as $fldname => $value) {
01522                     $sField = "oxuser__".strtolower( $fldname);
01523                     $this->$sField = new oxField( $aData[$fldname] );
01524                 }
01525 
01526                 $this->oxuser__oxactive  = new oxField( 1 );
01527                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01528                 $this->oxuser__oxldapkey = new oxField( $sUser );
01529                 $this->oxuser__oxrights  = new oxField( "user" );
01530                 $this->setPassword( "ldap user" );
01531 
01532                 $this->save();
01533             } else {
01534                 // LDAP user is already in OXID DB, load it
01535                 $this->load( $sOXID);
01536             }
01537 
01538         } else {
01539             $oEx = oxNew( 'oxUserException' );
01540             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01541             throw $oEx;
01542         }
01543     }
01544 
01551     protected function _getUserRights()
01552     {
01553         // previously user had no rights defined
01554         if ( !$this->oxuser__oxrights->value )
01555             return 'user';
01556 
01557         $oDB = oxDb::getDb();
01558         $myConfig    = $this->getConfig();
01559         $sAuthRights = null;
01560 
01561         // choosing possible user rights index
01562         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01563         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01564         if ( $sAuthUserID ) {
01565             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01566         }
01567 
01568         //preventing user rights edit for non admin
01569         $aRights = array();
01570 
01571         // selecting current users rights ...
01572         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $this->getId() ) ) ) {
01573             $aRights[] = $sCurrRights;
01574         }
01575         $aRights[] = 'user';
01576 
01577         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01578             return current( $aRights );
01579         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01580             $aRights[] = $sAuthRights;
01581             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01582                 return current( $aRights );
01583             }
01584         }
01585 
01586         // leaving as it was set ...
01587         return $this->oxuser__oxrights->value;
01588     }
01589 
01599     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01600     {
01601 
01602         /*if ( !$myConfig->blMallUsers ) {
01603             $sShopID = $myConfig->getShopId();
01604             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01605         }*/
01606 
01607         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01608     }
01609 
01615     protected function _insert()
01616     {
01617 
01618         // set oxcreate date
01619         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01620 
01621         if ( !isset( $this->oxuser__oxboni->value ) ) {
01622             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01623         }
01624 
01625         return parent::_insert();
01626     }
01627 
01633     protected function _update()
01634     {
01635         //V #M418: for not registered users, don't change boni during update
01636         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01637             $this->_aSkipSaveFields[] = 'oxboni';
01638         }
01639 
01640         // don't change this field
01641         $this->_aSkipSaveFields[] = 'oxcreate';
01642         if ( !$this->isAdmin() ) {
01643             $this->_aSkipSaveFields[] = 'oxcustnr';
01644             $this->_aSkipSaveFields[] = 'oxrights';
01645         }
01646 
01647         // updating subscription information
01648         if ( ( $blUpdate = parent::_update() ) ) {
01649             $this->getNewsSubscription()->updateSubscription( $this );
01650         }
01651 
01652         return $blUpdate;
01653     }
01654 
01670     protected function _checkLogin( $sLogin, $aInvAddress )
01671     {
01672         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01673         oxInputValidator::getInstance()->checkLogin( $this, $sLogin, $aInvAddress );
01674         return $sLogin;
01675     }
01676 
01684     public function checkIfEmailExists( $sEmail )
01685     {
01686         $myConfig = $this->getConfig();
01687         $oDB = oxDb::getDb();
01688         $iShopId = $myConfig->getShopId();
01689         $blExists = false;
01690 
01691         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01692         if ( ( $sOxid = $this->getId() ) ) {
01693             $sQ .= " and oxid <> ".$oDB->quote( $sOxid );
01694         }
01695 
01696         $oRs = $oDB->execute( $sQ );
01697         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01698 
01699             if ( $this->_blMallUsers ) {
01700 
01701                 $blExists = true;
01702                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01703 
01704                     // password is not set - allow to override
01705                     $blExists = false;
01706                 }
01707             } else {
01708 
01709                 $blExists = false;
01710                 while ( !$oRs->EOF ) {
01711                     if ( $oRs->fields[1] != 'user' ) {
01712 
01713                         // exists admin with same login - must not allow
01714                         $blExists = true;
01715                         break;
01716                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01717 
01718                         // exists same login (with password) in same shop
01719                         $blExists = true;
01720                         break;
01721                     }
01722 
01723                     $oRs->moveNext();
01724                 }
01725             }
01726         }
01727         return $blExists;
01728     }
01729 
01737     public function getUserRecommLists( $sOXID = null )
01738     {
01739         if ( !$sOXID )
01740             $sOXID = $this->getId();
01741 
01742         // sets active page
01743         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01744         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01745 
01746         // load only lists which we show on screen
01747         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01748         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01749 
01750 
01751         $oRecommList = oxNew( 'oxlist' );
01752         $oRecommList->init( 'oxrecommlist' );
01753         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01754         $iShopId = $this->getConfig()->getShopId();
01755         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01756         $oRecommList->selectString( $sSelect );
01757 
01758         return $oRecommList;
01759     }
01760 
01768     public function getRecommListsCount( $sOx = null )
01769     {
01770         if ( !$sOx ) {
01771             $sOx = $this->getId();
01772         }
01773 
01774         if ( $this->_iCntRecommLists === null || $sOx ) {
01775             $oDb = oxDb::getDb();
01776             $this->_iCntRecommLists = 0;
01777             $iShopId = $this->getConfig()->getShopId();
01778             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01779             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01780         }
01781         return $this->_iCntRecommLists;
01782     }
01783 
01794     protected function _checkEmail( $sEmail )
01795     {
01796         oxInputValidator::getInstance()->checkEmail( $this, $sEmail );
01797     }
01798 
01813     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01814     {
01815         return $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01816     }
01817 
01830     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01831     {
01832         return oxInputValidator::getInstance()->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01833     }
01834 
01845     protected function _checkCountries( $aInvAddress, $aDelAddress )
01846     {
01847         oxInputValidator::getInstance()->checkCountries( $this, $aInvAddress, $aDelAddress );
01848     }
01849 
01861     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01862     {
01863         oxInputValidator::getInstance()->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01864     }
01865 
01876     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01877     {
01878         oxInputValidator::getInstance()->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01879     }
01880 
01891     protected function _checkVatId( $aInvAddress )
01892     {
01893         oxInputValidator::getInstance()->checkVatId( $this, $aInvAddress );
01894     }
01895 
01904     protected function _setAutoGroups( $sCountryId )
01905     {
01906         // assigning automatically to specific groups
01907         $blForeigner = true;
01908         $blForeignGroupExists = false;
01909         $blInlandGroupExists = false;
01910 
01911         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01912         // foreigner ?
01913         if ( is_array($aHomeCountry)) {
01914             if (in_array($sCountryId, $aHomeCountry)) {
01915                 $blForeigner = false;
01916             }
01917         } elseif ($sCountryId == $aHomeCountry) {
01918             $blForeigner = false;
01919         }
01920 
01921         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01922             $blForeignGroupExists = true;
01923             if ( !$blForeigner ) {
01924                 $this->removeFromGroup( 'oxidforeigncustomer' );
01925             }
01926         }
01927 
01928         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01929             $blInlandGroupExists = true;
01930             if ( $blForeigner ) {
01931                 $this->removeFromGroup( 'oxidnewcustomer' );
01932             }
01933         }
01934 
01935         if ( !$this->oxuser__oxdisableautogrp->value ) {
01936             if ( !$blForeignGroupExists && $blForeigner ) {
01937                 $this->addToGroup( 'oxidforeigncustomer' );
01938             }
01939             if ( !$blInlandGroupExists && !$blForeigner ) {
01940                 $this->addToGroup( 'oxidnewcustomer' );
01941             }
01942         }
01943     }
01944 
01953     protected function _hasUserAddress( $sUserId )
01954     {
01955         $oAddresses = $this->getUserAddresses();
01956         if ( $oAddresses && count($oAddresses)>0 ) {
01957             $oAddresses->rewind() ;
01958             foreach ($oAddresses as $key => $oAddress) {
01959                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01960                     return true;
01961                 }
01962             }
01963         }
01964         return false;
01965     }
01966 
01979     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
01980     {
01981         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
01982     }
01983 
01993     protected function _deleteUserCookie( $sShopId = null )
01994     {
01995         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
01996     }
01997 
02007     protected static function _getUserCookie( $sShopId = null )
02008     {
02009         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
02010     }
02011 
02012 
02021     public function loadUserByUpdateId( $sUid )
02022     {
02023         $oDb = oxDb::getDb();
02024         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02025         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02026             return $this->load( $sUserId );
02027         }
02028     }
02029 
02037     public function setUpdateKey( $blReset = false )
02038     {
02039         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02040         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02041 
02042         // generating key
02043         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02044 
02045         // setting expiration time for 6 hours
02046         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02047 
02048         // saving
02049         $this->save();
02050     }
02051 
02057     public function getUpdateLinkTerm()
02058     {
02059         return 3600 * 6;
02060     }
02061 
02069     public function isExpiredUpdateId( $sKey )
02070     {
02071         $oDb = oxDb::getDb();
02072         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02073         return !( (bool) $oDb->getOne( $sQ ) );
02074     }
02075 
02081     public function getUpdateId()
02082     {
02083         if ( $this->_sUpdateKey === null ) {
02084             $this->setUpdateKey();
02085             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02086         }
02087         return $this->_sUpdateKey;
02088     }
02089 
02098     public function encodePassword( $sPassword, $sSalt )
02099     {
02100         $oDb = oxDb::getDb();
02101         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02102     }
02103 
02111     public function prepareSalt( $sSalt )
02112     {
02113         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02114     }
02115 
02123     public function decodeSalt( $sSaltHex )
02124     {
02125         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02126     }
02127 
02135     public function setPassword( $sPassword = null )
02136     {
02137         // setting salt if password is not empty
02138         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02139 
02140         // encoding only if password was not empty (e.g. user registration without pass)
02141         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02142 
02143         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02144         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02145     }
02146 
02154     public function isSamePassword( $sNewPass )
02155     {
02156         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02157     }
02158 
02164     public function isLoadedFromCookie()
02165     {
02166         return $this->_blLoadedFromCookie;
02167     }
02168 
02175     public function getPasswordHash()
02176     {
02177         $sHash = null;
02178         if ( $this->oxuser__oxpassword->value ) {
02179             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02180                 // decodable pass ?
02181                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02182             } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02183                 // plain pass ?
02184                 $this->setPassword( $this->oxuser__oxpassword->value );
02185             }
02186             $sHash = $this->oxuser__oxpassword->value;
02187         }
02188         return $sHash;
02189     }
02190 
02199     public static function getAdminUser()
02200     {
02201         return self::getActiveUser( true );
02202     }
02203 
02214     public static function getActiveUser( $blForceAdmin = false )
02215     {
02216         $oUser = oxNew( 'oxuser' );
02217         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02218             return $oUser;
02219         } else {
02220             return false;
02221         }
02222     }
02223 
02231     public function getReviewUserHash( $sUserId )
02232     {
02233         $oDb = oxDb::getDb();
02234         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02235         return $sReviewUserHash;
02236     }
02237 
02245     public function getReviewUserId( $sReviewUserHash )
02246     {
02247         $oDb = oxDb::getDb();
02248         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02249         return $sUserId;
02250     }
02251 
02257     public function getState()
02258     {
02259         return $this->oxuser__oxstateid->value;
02260     }
02261 
02267     public function isTermsAccepted()
02268     {
02269         $oDb = oxDb::getDb();
02270         $sShopId = $this->getConfig()->getShopId();
02271         $sUserId = $oDb->quote( $this->getId() );
02272         return (bool) $oDb->getOne( "select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'" );
02273     }
02274 
02280     public function acceptTerms()
02281     {
02282         $oDb = oxDb::getDb();
02283         $sUserId  = $oDb->quote( $this->getId() );
02284         $sShopId  = $this->getConfig()->getShopId();
02285         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02286 
02287         $oDb->execute( "replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02288     }
02289 
02299     public function setCreditPointsForRegistrant( $sUserId, $sRecEmail )
02300     {
02301         $blSet   = false;
02302         $oDb = oxDb::getDb();
02303         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02304         // check if this invitation is still not accepted
02305         $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" );
02306         if ( $iPoints && $iPending ) {
02307             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02308             if ( $blSet = $this->save() ) {
02309                 // updating users statistics
02310                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail ) );
02311                 $oInvUser = oxNew( "oxuser" );
02312                 if ( $oInvUser->load( $sUserId ) ) {
02313                     $blSet = $oInvUser->setCreditPointsForInviter();
02314                 }
02315             }
02316         }
02317         oxSession::deleteVar( 'su' );
02318         oxSession::deleteVar( 're' );
02319 
02320         return $blSet;
02321     }
02322 
02328     public function setCreditPointsForInviter()
02329     {
02330         $blSet   = false;
02331         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02332         if ( $iPoints ) {
02333             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02334             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02335             $blSet = $this->save();
02336         }
02337 
02338         return $blSet;
02339     }
02340 
02346     public function updateFbId()
02347     {
02348         $oFb = oxFb::getInstance();
02349         $blRet = false;
02350 
02351         if ( $oFb->isConnected() && $oFb->getUser() ) {
02352              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02353              $blRet = $this->save();
02354         }
02355 
02356         return $blRet;
02357     }
02358 
02366     public function updateInvitationStatistics( $aRecEmail )
02367     {
02368         $oDb = oxDb::getDb( true );
02369         $sUserId = $this->getId();
02370 
02371         if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
02372             //iserting statistics about invitation
02373             $sDate = oxUtilsDate::getInstance()->formatDBDate( date("Y-m-d"), true );
02374             $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
02375             foreach ( $aRecEmail as $sRecEmail ) {
02376                 $sSql = "INSERT INTO oxinvitations SET oxuserid = ".$oDb->quote( $sUserId ).", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02377                 $oDb->execute( $sSql );
02378             }
02379         }
02380     }
02381 
02382 }