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     public function __construct()
00145     {
00146         $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00147 
00148         parent::__construct();
00149         $this->init( 'oxuser' );
00150     }
00151 
00159     public function setMallUsersStatus( $blOn = false )
00160     {
00161         $this->_blMallUsers = $blOn;
00162     }
00163 
00171     public function __get( $sParamName )
00172     {
00173         // it saves memory using - loads data only if it is used
00174         switch ( $sParamName ) {
00175             case 'oGroups':
00176                 return $this->_oGroups = $this->getUserGroups();
00177                 break;
00178             case 'iCntNoticeListArticles':
00179                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00180                 break;
00181             case 'iCntWishListArticles':
00182                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00183                 break;
00184             case 'iCntRecommLists':
00185                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00186                 break;
00187             case 'oAddresses':
00188                 return $this->getUserAddresses();
00189                 break;
00190             case 'oPayments':
00191                 return $this->_oPayments = $this->getUserPayments();
00192                 break;
00193             case 'oxuser__oxcountry':
00194                 return $this->oxuser__oxcountry = $this->getUserCountry();
00195                 break;
00196             case 'sDBOptin':
00197                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00198                 break;
00199             case 'sEmailFailed':
00200                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00201                 break;
00202         }
00203     }
00204 
00210     public function getNewsSubscription()
00211     {
00212         if ( $this->_oNewsSubscription !== null ) {
00213             return $this->_oNewsSubscription;
00214         }
00215 
00216         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00217 
00218         // if subscription object is not set yet - we should create one
00219         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00220             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00221 
00222                 // no subscription defined yet - creating one
00223                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00224                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00225                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00226                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00227                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00228             }
00229         }
00230 
00231         return $this->_oNewsSubscription;
00232     }
00233 
00243     public function getUserCountry( $sCountryId = null, $iLang = null )
00244     {
00245         $oDb = oxDb::getDb();
00246         if ( !$sCountryId ) {
00247             $sCountryId = $this->oxuser__oxcountryid->value;
00248         }
00249 
00250         $sViewName = getViewName( 'oxcountry', $iLang );
00251         $sQ = "select oxtitle from {$sViewName} where oxid = " . $oDb->quote( $sCountryId ) . " ";
00252         $this->oxuser__oxcountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00253 
00254         return $this->oxuser__oxcountry;
00255     }
00256 
00264     public function getUserCountryId( $sCountry = null )
00265     {
00266         $oDb = oxDb::getDb();
00267         $sQ = "select oxid from ".getviewName( "oxcountry" )." where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00268         $sCountryId = $oDb->getOne( $sQ );
00269 
00270         return $sCountryId;
00271     }
00272 
00280     public function getUserGroups( $sOXID = null )
00281     {
00282 
00283         if ( isset( $this->_oGroups ) ) {
00284             return $this->_oGroups;
00285         }
00286 
00287         if ( !$sOXID ) {
00288             $sOXID = $this->getId();
00289         }
00290 
00291         $sViewName = getViewName( "oxgroups" );
00292         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00293         $sSelect  = "select {$sViewName}.* from {$sViewName} left join oxobject2group on oxobject2group.oxgroupsid = {$sViewName}.oxid
00294                      where oxobject2group.oxobjectid = " . oxDb::getDb()->quote( $sOXID );
00295         $this->_oGroups->selectString( $sSelect );
00296         return $this->_oGroups;
00297     }
00298 
00306     public function getUserAddresses( $sUserId = null )
00307     {
00308         $sUserId = isset( $sUserId ) ? $sUserId : $this->getId();
00309         if ( !isset( $this->_aAddresses[$sUserId] ) ) {
00310             $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId );
00311 
00312             //P
00313             $this->_aAddresses[$sUserId] = oxNew( "oxlist" );
00314             $this->_aAddresses[$sUserId]->init( "oxaddress" );
00315             $this->_aAddresses[$sUserId]->selectString( $sSelect );
00316 
00317             // marking selected
00318             if ( $sAddressId = $this->getSelectedAddressId() ) {
00319                 foreach ( $this->_aAddresses[$sUserId] as $oAddress ) {
00320                     if ( $oAddress->getId() === $sAddressId ) {
00321                         $oAddress->setSelected();
00322                         break;
00323                     }
00324                 }
00325             }
00326         }
00327         return $this->_aAddresses[$sUserId];
00328     }
00329 
00337     public function setSelectedAddressId( $sAddressId )
00338     {
00339         $this->_sSelAddressId = $sAddressId;
00340     }
00341 
00347     public function getSelectedAddressId()
00348     {
00349         if ( $this->_sSelAddressId !== null ) {
00350             return $this->_sSelAddressId;
00351         }
00352 
00353         $sAddressId = oxConfig::getParameter( "oxaddressid");
00354         if ( !$sAddressId && !oxConfig::getParameter( 'reloadaddress' ) ) {
00355             $sAddressId = oxSession::getVar( "deladrid" );
00356         }
00357         return $sAddressId;
00358     }
00359 
00365     protected function _getWishListId()
00366     {
00367         $this->_sWishId = null;
00368         // check if we have to set it here
00369         $oBasket = $this->getSession()->getBasket();
00370         foreach ( $oBasket->getContents() as $oBasketItem ) {
00371             if ( $this->_sWishId = $oBasketItem->getWishId() ) {
00372                 // stop on first found
00373                 break;
00374             }
00375         }
00376         return $this->_sWishId;
00377     }
00378 
00387     public function getSelectedAddress( $sWishId = false )
00388     {
00389         if ( $this->_oSelAddress !== null ) {
00390             return $this->_oSelAddress;
00391         }
00392 
00393         $oSelectedAddress = null;
00394         $oAddresses = $this->getUserAddresses();
00395         if ( $oAddresses->count() ) {
00396             if ( $sAddressId = $this->getSelectedAddressId() ) {
00397                 foreach ( $oAddresses as $oAddress ) {
00398                     if ( $oAddress->getId() == $sAddressId ) {
00399                         $oAddress->selected = 1;
00400                         $oSelectedAddress = $oAddress;
00401                         break;
00402                     }
00403                 }
00404             } elseif ( $sWishId = $this->_getWishListId() ) {
00405                 foreach ( $oAddresses as $oAddress ) {
00406                     $oAddress->selected = 0;
00407                     if ( $oAddress->oxaddress__oxaddressuserid->value == $sWishId ) {
00408                         $oAddress->selected = 1;
00409                         $sAddressId = $oAddress->getId();
00410                         $oSelectedAddress = $oAddress;
00411                     }
00412                 }
00413             }
00414 
00415             // in case none is set - setting first one
00416             if ( !$oSelectedAddress ) {
00417                 $oAddresses->rewind();
00418                 $oAddress = $oAddresses->current();
00419                 $oAddress->selected = 1;
00420                 $oSelectedAddress = $oAddress;
00421             }
00422         }
00423         $this->_oSelAddress = $oSelectedAddress;
00424         return $oSelectedAddress;
00425     }
00426 
00434     public function getUserPayments( $sOXID = null )
00435     {
00436         if ( $this->_oPayments === null ) {
00437 
00438             if ( !$sOXID ) {
00439                 $sOXID = $this->getId();
00440             }
00441 
00442             $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00443 
00444             $this->_oPayments = oxNew( 'oxlist' );
00445             $this->_oPayments->init( 'oxUserPayment' );
00446             $this->_oPayments->selectString( $sSelect );
00447 
00448             $myUtils = oxUtils::getInstance();
00449             foreach ( $this->_oPayments as $oPayment ) {
00450                 // add custom fields to this class
00451                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00452             }
00453         }
00454 
00455         return $this->_oPayments;
00456     }
00457 
00463     public function save()
00464     {
00465         $myConfig  = oxConfig::getInstance();
00466 
00467         $blAddRemark = false;
00468         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00469             $blAddRemark = true;
00470             //save oxregister value
00471             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00472         }
00473 
00474         // setting user rights
00475         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00476 
00477         // processing birth date which came from output as array
00478         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00479             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00480         }
00481 
00482         // checking if user Facebook ID should be updated
00483         if ( $myConfig->getConfigParam( "bl_showFbConnect" ) ) {
00484             $oFb = oxFb::getInstance();
00485             if ( $oFb->isConnected() && $oFb->getUser() ) {
00486                  $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
00487             }
00488         }
00489 
00490         $blRet = parent::save();
00491 
00492         //add registered remark
00493         if ( $blAddRemark && $blRet ) {
00494             $oRemark = oxNew( 'oxremark' );
00495             $oRemark->oxremark__oxtext     = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00496             $oRemark->oxremark__oxtype     = new oxField('r', oxField::T_RAW);
00497             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00498             $oRemark->save();
00499         }
00500 
00501         return $blRet;
00502     }
00503 
00509     public function allowDerivedUpdate()
00510     {
00511         return true;
00512     }
00513 
00521     public function inGroup( $sGroupID )
00522     {
00523         $blIn = false;
00524         if ( ( $oGroups = $this->getUserGroups() ) ) {
00525             $blIn = isset( $oGroups[ $sGroupID ] );
00526         }
00527 
00528         return $blIn;
00529     }
00530 
00539     public function delete( $sOXID = null )
00540     {
00541 
00542         if ( !$sOXID ) {
00543             $sOXID = $this->getId();
00544         }
00545         if ( !$sOXID ) {
00546             return false;
00547         }
00548 
00549         $blDeleted = parent::delete( $sOXID );
00550 
00551         if ( $blDeleted ) {
00552             $oDB = oxDb::getDb();
00553             $sOXIDQuoted = $oDB->quote($sOXID);
00554 
00555             // deleting stored payment, address, group dependencies, remarks info
00556             $rs = $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00557             $rs = $oDB->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00558 
00559             // deleting notice/wish lists
00560             $rs = $oDB->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00561             $rs = $oDB->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00562 
00563             // deleting newsletter subscription
00564             $rs = $oDB->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00565 
00566             // delivery and delivery sets
00567             $rs = $oDB->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00568 
00569             // discounts
00570             $rs = $oDB->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00571 
00572 
00573             // and leaving all order related information
00574             $rs = $oDB->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00575 
00576             $blDeleted = $rs->EOF;
00577         }
00578 
00579         return $blDeleted;
00580     }
00581 
00589     public function load( $oxID )
00590     {
00591 
00592         $blRet = parent::Load( $oxID );
00593 
00594         // convert date's to international format
00595         if ( isset( $this->oxuser__oxcreate->value ) ) {
00596             $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00597         }
00598 
00599         return $blRet;
00600     }
00601 
00609     public function exists( $sOXID = null )
00610     {
00611         $oDb = oxDb::getDb();
00612         if ( !$sOXID ) {
00613             $sOXID = $this->getId();
00614         }
00615 
00616         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00617                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).'';
00618 
00619         if ( $sOXID ) {
00620             $sSelect.= " or oxid = ".$oDb->quote( $sOXID ) . " ) ";
00621         } else {
00622             $sSelect.= ' ) ';
00623         }
00624 
00625         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00626             $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00627         }
00628 
00629         $blExists = false;
00630         if ( ( $sOxid = oxDb::getDb()->getOne( $sSelect ) ) ) {
00631              // update - set oxid
00632             $this->setId( $sOxid );
00633             $blExists = true;
00634         }
00635         return $blExists;
00636     }
00637 
00646     public function getOrders( $iLimit = false, $iPage = 0 )
00647     {
00648         $myConfig = $this->getConfig();
00649         $oOrders = oxNew( 'oxlist' );
00650         $oOrders->init( 'oxorder' );
00651 
00652         if ( $iLimit !== false ) {
00653             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00654         }
00655 
00656         //P
00657         // Lists does not support loading from two tables, so orders
00658         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00659         // forcing to load product info which is used in templates
00660         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00661 
00662         //loading order for registered user
00663         if ( $this->oxuser__oxregister->value > 1 ) {
00664             $sQ = 'select * from oxorder where oxuserid = "'.$this->getId().'" and oxorderdate >= ' . oxDb::getDb()->quote( $this->oxuser__oxregister->value ) . ' ';
00665 
00666             //#1546 - shopid check added, if it is not multishop
00667 
00668             $sQ .= ' order by oxorderdate desc ';
00669             $oOrders->selectString( $sQ );
00670         }
00671 
00672         return $oOrders;
00673     }
00674 
00680     public function getOrderCount()
00681     {
00682         $iCnt = 0;
00683         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00684             $oDb = oxDb::getDb();
00685             $sQ  = 'select count(*) from oxorder where oxuserid = "'.$this->getId().'" AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00686             $iCnt = (int) $oDb->getOne( $sQ );
00687         }
00688 
00689         return $iCnt;
00690     }
00691 
00697     public function getNoticeListArtCnt()
00698     {
00699         if ( $this->_iCntNoticeListArticles === null ) {
00700             $this->_iCntNoticeListArticles = 0;
00701             if ( $this->getId() ) {
00702                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00703             }
00704         }
00705         return $this->_iCntNoticeListArticles;
00706     }
00707 
00713     public function getWishListArtCnt()
00714     {
00715         if ( $this->_iCntWishListArticles === null ) {
00716             $this->_iCntWishListArticles = false;
00717             if ( $this->getId() ) {
00718                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00719             }
00720         }
00721         return $this->_iCntWishListArticles;
00722     }
00723 
00730     public function getActiveCountry()
00731     {
00732         $sDeliveryCountry = '';
00733         $soxAddressId = oxSession::getVar( 'deladrid' );
00734         if ( $soxAddressId ) {
00735             $oDelAddress = oxNew( 'oxaddress' );
00736             $oDelAddress->load( $soxAddressId );
00737             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00738         } elseif ( $this->getId() ) {
00739             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00740         } else {
00741             $oUser = oxNew( 'oxuser' );
00742             if ( $oUser->loadActiveUser() ) {
00743                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00744             }
00745         }
00746 
00747         return $sDeliveryCountry;
00748     }
00749 
00757     public function createUser()
00758     {
00759         $oDB = oxDb::getDb();
00760         $sShopID = $this->getConfig()->getShopId();
00761 
00762         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00763         $sSelect = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00764         if ( !$this->_blMallUsers ) {
00765             $sSelect .= " and oxshopid = '{$sShopID}' ";
00766         }
00767         $sOXID = $oDB->getOne( $sSelect );
00768 
00769         // user without password found - lets use
00770         if ( isset( $sOXID ) && $sOXID ) {
00771             // try to update
00772             $this->delete( $sOXID );
00773         } elseif ( $this->_blMallUsers ) {
00774             // must be sure if there is no dublicate user
00775             $sQ = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00776             if ( $oDB->getOne( $sQ ) ) {
00777                 $oEx = oxNew( 'oxUserException' );
00778                 $oLang = oxLang::getInstance();
00779                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00780                 throw $oEx;
00781             }
00782         }
00783 
00784         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00785         if ( ( $blOK = $this->save() ) ) {
00786             // dropping/cleaning old delivery address/payment info
00787             $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00788             $oDB->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00789         } else {
00790             $oEx = oxNew( 'oxUserException' );
00791             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00792             throw $oEx;
00793         }
00794 
00795         return $blOK;
00796     }
00797 
00805     public function addToGroup( $sGroupID )
00806     {
00807         if ( !$this->inGroup( $sGroupID ) ) {
00808             $oNewGroup = oxNew( 'oxobject2group' );
00809             $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00810             $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00811             if ( $oNewGroup->save() ) {
00812                 $this->_oGroups[$sGroupID] = $oNewGroup;
00813                 return true;
00814             }
00815         }
00816         return false;
00817     }
00818 
00826     public function removeFromGroup( $sGroupID = null )
00827     {
00828         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00829             $oGroups = oxNew( 'oxlist' );
00830             $oGroups->init( 'oxobject2group' );
00831             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00832             $oGroups->selectString( $sSelect );
00833             foreach ( $oGroups as $oRemgroup ) {
00834                 if ( $oRemgroup->delete() ) {
00835                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00836                 }
00837             }
00838         }
00839     }
00840 
00849     public function onOrderExecute( $oBasket, $iSuccess )
00850     {
00851 
00852         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00853             //adding user to particular customer groups
00854             if ( !$this->oxuser__oxdisableautogrp->value ) {
00855 
00856                 $myConfig = $this->getConfig();
00857                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00858                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00859 
00860                 $this->addToGroup( 'oxidcustomer' );
00861                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00862                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00863                     $this->addToGroup( 'oxidsmallcust' );
00864                 }
00865                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00866                     $this->addToGroup( 'oxidmiddlecust' );
00867                 }
00868                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00869                     $this->addToGroup( 'oxidgoodcust' );
00870                 }
00871             }
00872 
00873             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00874                 $this->removeFromGroup( 'oxidnotyetordered' );
00875             }
00876         }
00877     }
00878 
00886     public function getBasket( $sName )
00887     {
00888         if ( !isset( $this->_aBaskets[$sName] ) ) {
00889             $oBasket = oxNew( 'oxuserbasket' );
00890             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00891 
00892             // creating if it does not exist
00893             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00894                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00895                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00896 
00897                 // marking basket as new (it will not be saved in DB yet)
00898                 $oBasket->setIsNewBasket();
00899             }
00900 
00901             $this->_aBaskets[$sName] = $oBasket;
00902         }
00903 
00904         return $this->_aBaskets[$sName];
00905     }
00906 
00915     public function convertBirthday( $aData )
00916     {
00917 
00918         // preparing data to process
00919         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00920         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00921         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00922 
00923         // leaving empty if not set
00924         if ( !$iYear && !$iMonth && !$iDay )
00925             return "";
00926 
00927         // year
00928         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00929             $iYear = date('Y');
00930 
00931         // month
00932         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00933             $iMonth = 1;
00934 
00935         // maximum nuber of days in month
00936         $iMaxDays = 31;
00937         switch( $iMonth) {
00938             case 2 :
00939                 if ($iMaxDays > 28)
00940                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00941                 break;
00942             case 4  :
00943             case 6  :
00944             case 9  :
00945             case 11 :
00946                 $iMaxDays = min(30, $iMaxDays);
00947                 break;
00948         }
00949 
00950         // day
00951         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00952             $iDay = 1;
00953         }
00954 
00955         // whole date
00956         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00957     }
00958 
00965     public function getBoni()
00966     {
00967         return 1000;
00968     }
00969 
00982     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00983     {
00984         // preparing input
00985         $sDynGoup = strtolower( trim( $sDynGoup ) );
00986 
00987         // setting denied groups from admin settings also
00988         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
00989 
00990         // default state ..
00991         $blAdd = false;
00992 
00993         // user assignment to dyn group is not allowed
00994         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
00995             $blAdd = false;
00996         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
00997             // trying to add user to prohibited user group?
00998             $blAdd = false;
00999         } elseif ( $this->addToGroup( $sDynGoup ) ) {
01000             $blAdd = true;
01001         }
01002 
01003         // cleanup
01004         oxSession::deleteVar( 'dgr' );
01005 
01006         return $blAdd;
01007     }
01008 
01027     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01028     {
01029         // 1. checking user name
01030         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01031 
01032         // 2. cheking email
01033         $this->_checkEmail( $sLogin );
01034 
01035         // 3. password
01036         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01037 
01038         // 4. required fields
01039         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01040 
01041         // 5. country check
01042         $this->_checkCountries( $aInvAddress, $aDelAddress );
01043 
01044         // 6. vat id check.
01045             $this->_checkVatId( $aInvAddress );
01046 
01047 
01048         // throwing first validation error
01049         if ( $oError = oxInputValidator::getInstance()->getFirstValidationError( $this->getId() ) ) {
01050             throw $oError;
01051         }
01052     }
01053 
01062     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
01063     {
01064         // assigning to newsletter
01065         $blSuccess = false;
01066         $myConfig  = $this->getConfig();
01067         $mySession = $this->getSession();
01068 
01069         // user wants to get newsletter messages or no ?
01070         $oNewsSubscription = $this->getNewsSubscription();
01071         if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
01072             if ( !$blSendOptIn ) {
01073 
01074                 // double-opt-in check is disabled - assigning automatically
01075                 $this->addToGroup( 'oxidnewsletter' );
01076                 // and setting subscribed status
01077                 $oNewsSubscription->setOptInStatus( 1 );
01078                 $blSuccess = true;
01079             } else {
01080 
01081                 $oNewsSubscription->setOptInStatus( 2 );
01082 
01083                 // double-opt-in check enabled - sending confirmation email and setting waiting status
01084                 if ( !$mySession->getVar( "blDBOptInMailAlreadyDone" ) ) {
01085 
01086                     // sending double-opt-in mail
01087                     $oEmail = oxNew( 'oxemail' );
01088                     $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01089 
01090                     if ( $blSuccess ) {
01091                         //setting in seesion parameter to force sending email only once (#2033)
01092                         $mySession->setVar( "blDBOptInMailAlreadyDone", true );
01093                     }
01094                 } else {
01095                     // mail already was sent, so just confirming that
01096                     $blSuccess = true;
01097                 }
01098             }
01099         } elseif ( !$blSubscribe ) {
01100             // removing user from newsletter subscribers
01101             $this->removeFromGroup( 'oxidnewsletter' );
01102             $oNewsSubscription->setOptInStatus( 0 );
01103             $blSuccess = true;
01104         }
01105 
01106         return $blSuccess;
01107     }
01108 
01126     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01127     {
01128         // validating values before saving. If validation fails - exception is thrown
01129         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01130 
01131         // input data is fine - lets save updated user info
01132         $this->assign( $aInvAddress );
01133 
01134 
01135         // update old or add new delivery address
01136         $this->_assignAddress( $aDelAddress );
01137 
01138         // saving new values
01139         if ( $this->save() ) {
01140 
01141             // assigning automatically to specific groups
01142             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01143             $this->_setAutoGroups( $sCountryId );
01144         }
01145     }
01146 
01155     public function addUserAddress( $oUser )
01156     {
01157         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01158             return false;
01159         }
01160 
01161         $oAddress = oxNew( 'oxaddress' );
01162         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01163         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01164         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01165         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01166         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01167         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01168         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01169         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01170         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01171         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01172         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01173 
01174         // adding new address
01175         if ( $oAddress->save() ) {
01176             // resetting addresses
01177             $this->_aAddresses = null;
01178             return $oAddress->getId();
01179         }
01180     }
01181 
01189     protected function _assignAddress( $aDelAddress )
01190     {
01191         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01192             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01193             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01194             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01195             $oAddress = oxNew( 'oxaddress' );
01196             $oAddress->assign( $aDelAddress );
01197             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01198             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01199             $oAddress->save();
01200 
01201             // resetting addresses
01202             $this->_aAddresses = null;
01203 
01204             // saving delivery Address for later use
01205             oxSession::setVar( 'deladrid', $oAddress->getId() );
01206         } else {
01207             // resetting
01208             oxSession::setVar( 'deladrid', null );
01209         }
01210     }
01211 
01224     public function login( $sUser, $sPassword, $blCookie = false)
01225     {
01226         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01227             $oEx = oxNew( 'oxCookieException' );
01228             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01229             throw $oEx;
01230         }
01231 
01232         $myConfig = $this->getConfig();
01233         if ( $sPassword ) {
01234 
01235             $sShopID = $myConfig->getShopId();
01236             $oDb = oxDb::getDb();
01237 
01238             $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01239             $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01240             $sShopSelect = "";
01241 
01242 
01243             // admin view: can only login with higher than 'user' rights
01244             if ( $this->isAdmin() ) {
01245                 $sShopSelect = " and ( oxrights != 'user' ) ";
01246             }
01247 
01248             $sWhat = "oxid";
01249 
01250             $sSelect =  "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01251             if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
01252                 if ( $sPassword == "admin" && $sUser == "admin" ) {
01253                     $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01254                 } else {
01255                     $oEx = oxNew( 'oxUserException' );
01256                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01257                     throw $oEx;
01258                 }
01259             }
01260 
01261             // load from DB
01262             $aData = $oDb->getAll( $sSelect );
01263             $sOXID = @$aData[0][0];
01264             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01265 
01266                 if ( !$this->load( $sOXID ) ) {
01267                     $oEx = oxNew( 'oxUserException' );
01268                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01269                     throw $oEx;
01270                 }
01271             }
01272         }
01273 
01274 
01275         //login successfull?
01276         if ( $this->oxuser__oxid->value ) {
01277             // yes, successful login
01278 
01279             //reseting active user
01280             $this->setUser( null );
01281 
01282             if ( $this->isAdmin() ) {
01283                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01284             } else {
01285                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01286             }
01287 
01288             // cookie must be set ?
01289             if ( $blCookie ) {
01290                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01291             }
01292 
01293             //load basket from the database
01294             try {
01295                 if ($oBasket = $this->getSession()->getBasket()) {
01296                     $oBasket->load();
01297                 }
01298 
01299             } catch (Exception $oE) {
01300                 //just ignore it
01301             }
01302 
01303             return true;
01304         } else {
01305             $oEx = oxNew( 'oxUserException' );
01306             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01307             throw $oEx;
01308         }
01309     }
01310 
01316     public function logout()
01317     {
01318         // deleting session info
01319         oxSession::deleteVar( 'usr' );  // for front end
01320         oxSession::deleteVar( 'auth' ); // for back end
01321         oxSession::deleteVar( 'dgr' );
01322         oxSession::deleteVar( 'dynvalue' );
01323         oxSession::deleteVar( 'paymentid' );
01324         // oxSession::deleteVar( 'deladrid' );
01325 
01326         // delete cookie
01327         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01328 
01329         // unsetting global user
01330         $this->setUser( null );
01331 
01332         return true;
01333     }
01334 
01341     public function loadAdminUser()
01342     {
01343         return $this->loadActiveUser( true );
01344     }
01345 
01354     public function loadActiveUser( $blForceAdmin = false )
01355     {
01356         $myConfig = $this->getConfig();
01357 
01358         $blAdmin = $this->isAdmin() || $blForceAdmin;
01359         $oDB = oxDb::getDb();
01360 
01361         // first - checking session info
01362         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01363         $blFoundInCookie = false;
01364 
01365         //trying automatic login (by 'remember me' cookie)
01366         if ( !$sUserID && !$blAdmin ) {
01367             $sShopID = $myConfig->getShopId();
01368             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01369                 $aData = explode( '@@@', $sSet );
01370                 $sUser = $aData[0];
01371                 $sPWD  = @$aData[1];
01372 
01373                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDB->quote($sUser);
01374 
01375 
01376                 $oDB = oxDb::getDb();
01377                 $rs = $oDB->execute( $sSelect );
01378                 if ( $rs != false && $rs->recordCount() > 0 ) {
01379                     while (!$rs->EOF) {
01380                         $sTest = crypt( $rs->fields[1], 'ox' );
01381                         if ( $sTest == $sPWD ) {
01382                             // found
01383                             $sUserID = $rs->fields[0];
01384                             $blFoundInCookie = true;
01385                             break;
01386                         }
01387                         $rs->moveNext();
01388                     }
01389                 }
01390             }
01391         }
01392 
01393         // Checking if user is connected via Facebook connect.
01394         // If yes, trying to login user using user Facebook ID
01395         if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01396             $oFb = oxFb::getInstance();
01397             if ( $oFb->isConnected() && $oFb->getUser() ) {
01398                 $sUserSelect = "oxuser.oxfbid = " . $oDB->quote( $oFb->getUser() );
01399                 $sShopSelect = "";
01400 
01401 
01402                 $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01403                 $sUserID = $oDB->getOne( $sSelect );
01404             }
01405         }
01406 
01407         // checking user results
01408         if ( $sUserID ) {
01409             if ( $this->load( $sUserID ) ) {
01410                 // storing into session
01411                 if ($blAdmin) {
01412                     oxSession::setVar( 'auth', $sUserID );
01413                 } else {
01414                     oxSession::setVar( 'usr', $sUserID );
01415                 }
01416 
01417                 // marking the way user was loaded
01418                 $this->_blLoadedFromCookie = $blFoundInCookie;
01419                 return true;
01420             }
01421         } else {
01422             // no user
01423             if ($blAdmin) {
01424                 oxSession::deleteVar( 'auth' );
01425             } else {
01426                 oxSession::deleteVar( 'usr' );
01427             }
01428 
01429             return false;
01430         }
01431     }
01432 
01445     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01446     {
01447         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01448         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01449 
01450         // maybe this is LDAP user but supplied email Address instead of LDAP login
01451         $oDb = oxDb::getDb();
01452         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01453         if ( isset( $sLDAPKey) && $sLDAPKey) {
01454             $sUser = $sLDAPKey;
01455         }
01456 
01457         //$throws oxConnectionException
01458         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01459 
01460         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01461         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01462             // login successful
01463 
01464             // check if user is already in database
01465             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01466             $sOXID = $oDb->getOne( $sSelect );
01467 
01468             if ( !isset( $sOXID ) || !$sOXID ) {
01469                 // we need to create a new user
01470                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01471                 $this->setId();
01472 
01473                 // map all user data fields
01474                 foreach ( $aData as $fldname => $value) {
01475                     $sField = "oxuser__".strtolower( $fldname);
01476                     $this->$sField = new oxField( $aData[$fldname] );
01477                 }
01478 
01479                 $this->oxuser__oxactive  = new oxField( 1 );
01480                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01481                 $this->oxuser__oxldapkey = new oxField( $sUser );
01482                 $this->oxuser__oxrights  = new oxField( "user" );
01483                 $this->setPassword( "ldap user" );
01484 
01485                 $this->save();
01486             } else {
01487                 // LDAP user is already in OXID DB, load it
01488                 $this->load( $sOXID);
01489             }
01490 
01491         } else {
01492             $oEx = oxNew( 'oxUserException' );
01493             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01494             throw $oEx;
01495         }
01496     }
01497 
01504     protected function _getUserRights()
01505     {
01506         // previously user had no rights defined
01507         if ( !$this->oxuser__oxrights->value )
01508             return 'user';
01509 
01510         $oDB = oxDb::getDb();
01511         $myConfig    = $this->getConfig();
01512         $sAuthRights = null;
01513 
01514         // choosing possible user rights index
01515         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01516         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01517         if ( $sAuthUserID ) {
01518             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01519         }
01520 
01521         //preventing user rights edit for non admin
01522         $aRights = array();
01523 
01524         // selecting current users rights ...
01525         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01526             $aRights[] = $sCurrRights;
01527         }
01528         $aRights[] = 'user';
01529 
01530         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01531             return current( $aRights );
01532         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01533             $aRights[] = $sAuthRights;
01534             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01535                 return current( $aRights );
01536             }
01537         }
01538 
01539         // leaving as it was set ...
01540         return $this->oxuser__oxrights->value;
01541     }
01542 
01552     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01553     {
01554 
01555         /*if ( !$myConfig->blMallUsers ) {
01556             $sShopID = $myConfig->getShopId();
01557             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01558         }*/
01559 
01560         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01561     }
01562 
01568     protected function _insert()
01569     {
01570 
01571         // set oxcreate date
01572         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01573 
01574         if ( !isset( $this->oxuser__oxboni->value ) ) {
01575             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01576         }
01577 
01578         return parent::_insert();
01579     }
01580 
01586     protected function _update()
01587     {
01588         //V #M418: for not registered users, don't change boni during update
01589         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01590             $this->_aSkipSaveFields[] = 'oxboni';
01591         }
01592 
01593         // don't change this field
01594         $this->_aSkipSaveFields[] = 'oxcreate';
01595         if ( !$this->isAdmin() ) {
01596             $this->_aSkipSaveFields[] = 'oxcustnr';
01597             $this->_aSkipSaveFields[] = 'oxrights';
01598         }
01599 
01600         // updating subscription information
01601         if ( ( $blUpdate = parent::_update() ) ) {
01602             $this->getNewsSubscription()->updateSubscription( $this );
01603         }
01604 
01605         return $blUpdate;
01606     }
01607 
01623     protected function _checkLogin( $sLogin, $aInvAddress )
01624     {
01625         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01626         oxInputValidator::getInstance()->checkLogin( $this, $sLogin, $aInvAddress );
01627         return $sLogin;
01628     }
01629 
01637     public function checkIfEmailExists( $sEmail )
01638     {
01639         $myConfig = $this->getConfig();
01640         $oDB = oxDb::getDb();
01641         $iShopId = $myConfig->getShopId();
01642         $blExists = false;
01643 
01644         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01645         if ( ( $sOxid = $this->getId() ) ) {
01646             $sQ .= " and oxid <> '$sOxid' ";
01647         }
01648 
01649         $oRs = $oDB->execute( $sQ );
01650         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01651 
01652             if ( $this->_blMallUsers ) {
01653 
01654                 $blExists = true;
01655                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01656 
01657                     // password is not set - allow to override
01658                     $blExists = false;
01659                 }
01660             } else {
01661 
01662                 $blExists = false;
01663                 while ( !$oRs->EOF ) {
01664                     if ( $oRs->fields[1] != 'user' ) {
01665 
01666                         // exists admin with same login - must not allow
01667                         $blExists = true;
01668                         break;
01669                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01670 
01671                         // exists same login (with password) in same shop
01672                         $blExists = true;
01673                         break;
01674                     }
01675 
01676                     $oRs->moveNext();
01677                 }
01678             }
01679         }
01680         return $blExists;
01681     }
01682 
01690     public function getUserRecommLists( $sOXID = null )
01691     {
01692         if ( !$sOXID )
01693             $sOXID = $this->getId();
01694 
01695         // sets active page
01696         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01697         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01698 
01699         // load only lists which we show on screen
01700         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01701         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01702 
01703 
01704         $oRecommList = oxNew( 'oxlist' );
01705         $oRecommList->init( 'oxrecommlist' );
01706         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01707         $iShopId = $this->getConfig()->getShopId();
01708         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01709         $oRecommList->selectString( $sSelect );
01710 
01711         return $oRecommList;
01712     }
01713 
01721     public function getRecommListsCount( $sOx = null )
01722     {
01723         if ( !$sOx ) {
01724             $sOx = $this->getId();
01725         }
01726 
01727         if ( $this->_iCntRecommLists === null || $sOx ) {
01728             $oDb = oxDb::getDb();
01729             $this->_iCntRecommLists = 0;
01730             $iShopId = $this->getConfig()->getShopId();
01731             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01732             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01733         }
01734         return $this->_iCntRecommLists;
01735     }
01736 
01747     protected function _checkEmail( $sEmail )
01748     {
01749         oxInputValidator::getInstance()->checkEmail( $this, $sEmail );
01750     }
01751 
01766     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01767     {
01768         return $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01769     }
01770 
01783     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01784     {
01785         return oxInputValidator::getInstance()->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01786     }
01787 
01798     protected function _checkCountries( $aInvAddress, $aDelAddress )
01799     {
01800         oxInputValidator::getInstance()->checkCountries( $this, $aInvAddress, $aDelAddress );
01801     }
01802 
01814     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01815     {
01816         oxInputValidator::getInstance()->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01817     }
01818 
01829     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01830     {
01831         oxInputValidator::getInstance()->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01832     }
01833 
01844     protected function _checkVatId( $aInvAddress )
01845     {
01846         oxInputValidator::getInstance()->checkVatId( $this, $aInvAddress );
01847     }
01848 
01857     protected function _setAutoGroups( $sCountryId )
01858     {
01859         // assigning automatically to specific groups
01860         $blForeigner = true;
01861         $blForeignGroupExists = false;
01862         $blInlandGroupExists = false;
01863 
01864         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01865         // foreigner ?
01866         if ( is_array($aHomeCountry)) {
01867             if (in_array($sCountryId, $aHomeCountry)) {
01868                 $blForeigner = false;
01869             }
01870         } elseif ($sCountryId == $aHomeCountry) {
01871             $blForeigner = false;
01872         }
01873 
01874         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01875             $blForeignGroupExists = true;
01876             if ( !$blForeigner ) {
01877                 $this->removeFromGroup( 'oxidforeigncustomer' );
01878             }
01879         }
01880 
01881         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01882             $blInlandGroupExists = true;
01883             if ( $blForeigner ) {
01884                 $this->removeFromGroup( 'oxidnewcustomer' );
01885             }
01886         }
01887 
01888         if ( !$this->oxuser__oxdisableautogrp->value ) {
01889             if ( !$blForeignGroupExists && $blForeigner ) {
01890                 $this->addToGroup( 'oxidforeigncustomer' );
01891             }
01892             if ( !$blInlandGroupExists && !$blForeigner ) {
01893                 $this->addToGroup( 'oxidnewcustomer' );
01894             }
01895         }
01896     }
01897 
01905     protected function _hasUserAddress( $sUserId )
01906     {
01907         $oAddresses = $this->getUserAddresses();
01908         if ( $oAddresses && count($oAddresses)>0 ) {
01909             $oAddresses->rewind() ;
01910             foreach ($oAddresses as $key => $oAddress) {
01911                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01912                     return true;
01913                 }
01914             }
01915         }
01916         return false;
01917     }
01918 
01931     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
01932     {
01933         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
01934     }
01935 
01945     protected function _deleteUserCookie( $sShopId = null )
01946     {
01947         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
01948     }
01949 
01959     protected static function _getUserCookie( $sShopId = null )
01960     {
01961         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
01962     }
01963 
01964 
01973     public function loadUserByUpdateId( $sUid )
01974     {
01975         $oDb = oxDb::getDb();
01976         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
01977         if ( $sUserId = $oDb->getOne( $sQ ) ) {
01978             return $this->load( $sUserId );
01979         }
01980     }
01981 
01989     public function setUpdateKey( $blReset = false )
01990     {
01991         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
01992         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
01993 
01994         // generating key
01995         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
01996 
01997         // setting expiration time for 6 hours
01998         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
01999 
02000         // saving
02001         $this->save();
02002     }
02003 
02009     public function getUpdateLinkTerm()
02010     {
02011         return 3600 * 6;
02012     }
02013 
02021     public function isExpiredUpdateId( $sKey )
02022     {
02023         $oDb = oxDb::getDb();
02024         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02025         return !( (bool) $oDb->getOne( $sQ ) );
02026     }
02027 
02033     public function getUpdateId()
02034     {
02035         if ( $this->_sUpdateKey === null ) {
02036             $this->setUpdateKey();
02037             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02038         }
02039         return $this->_sUpdateKey;
02040     }
02041 
02050     public function encodePassword( $sPassword, $sSalt )
02051     {
02052         $oDb = oxDb::getDb();
02053         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02054     }
02055 
02063     public function prepareSalt( $sSalt )
02064     {
02065         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02066     }
02067 
02075     public function decodeSalt( $sSaltHex )
02076     {
02077         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02078     }
02079 
02087     public function setPassword( $sPassword = null )
02088     {
02089         // setting salt if password is not empty
02090         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02091 
02092         // encoding only if password was not empty (e.g. user registration without pass)
02093         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02094 
02095         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02096         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02097     }
02098 
02106     public function isSamePassword( $sNewPass )
02107     {
02108         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02109     }
02110 
02116     public function isLoadedFromCookie()
02117     {
02118         return $this->_blLoadedFromCookie;
02119     }
02120 
02127     public function getPasswordHash()
02128     {
02129         $sHash = null;
02130         if ( $this->oxuser__oxpassword->value ) {
02131             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02132                 // decodable pass ?
02133                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02134             } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02135                 // plain pass ?
02136                 $this->setPassword( $this->oxuser__oxpassword->value );
02137             }
02138             $sHash = $this->oxuser__oxpassword->value;
02139         }
02140         return $sHash;
02141     }
02142 
02151     public static function getAdminUser()
02152     {
02153         return self::getActiveUser( true );
02154     }
02155 
02166     public static function getActiveUser( $blForceAdmin = false )
02167     {
02168         $oUser = oxNew( 'oxuser' );
02169         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02170             return $oUser;
02171         } else {
02172             return false;
02173         }
02174     }
02175 
02183     public function getReviewUserHash( $sUserId )
02184     {
02185         $oDb = oxDb::getDb();
02186         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02187         return $sReviewUserHash;
02188     }
02189 
02197     public function getReviewUserId( $sReviewUserHash )
02198     {
02199         $oDb = oxDb::getDb();
02200         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02201         return $sUserId;
02202     }
02203 
02209     public function getState()
02210     {
02211         return $this->oxuser__oxstateid->value;
02212     }
02213 
02219     public function isTermsAccepted()
02220     {
02221         $sShopId = $this->getConfig()->getShopId();
02222         $sUserId = $this->getId();
02223         return (bool) oxDb::getDb()->getOne( "select 1 from oxacceptedterms where oxuserid='{$sUserId}' and oxshopid='{$sShopId}'" );
02224     }
02225 
02231     public function acceptTerms()
02232     {
02233         $sUserId  = $this->getId();
02234         $sShopId  = $this->getConfig()->getShopId();
02235         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02236 
02237         oxDb::getDb()->execute( "replace oxacceptedterms set oxuserid='{$sUserId}', oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02238     }
02239 
02248     public function setCreditPointsForRegistrant( $sUserId )
02249     {
02250         $blSet   = false;
02251         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02252         if ( $iPoints ) {
02253             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02254             if ( $blSet = $this->save() ) {
02255                 $oDb = oxDb::getDb();
02256 
02257                 // updating users statistics
02258                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ". $oDb->quote( $sUserId ) );
02259 
02260                 $oInvUser = oxNew( "oxuser" );
02261                 if ( $oInvUser->load( $sUserId ) ) {
02262                     $blSet = $oInvUser->setCreditPointsForInviter();
02263                 }
02264             }
02265 
02266             oxSession::deleteVar( 'su' );
02267         }
02268 
02269         return $blSet;
02270     }
02271 
02277     public function setCreditPointsForInviter()
02278     {
02279         $blSet   = false;
02280         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02281         if ( $iPoints ) {
02282             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02283             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02284             $blSet = $this->save();
02285         }
02286 
02287         return $blSet;
02288     }
02289 
02295     public function updateFbId()
02296     {
02297         $oFb = oxFb::getInstance();
02298         $blRet = false;
02299 
02300         if ( $oFb->isConnected() && $oFb->getUser() ) {
02301              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02302              $blRet = $this->save();
02303         }
02304 
02305         return $blRet;
02306     }
02307 }