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     public function __construct()
00124     {
00125         $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00126 
00127         parent::__construct();
00128         $this->init( 'oxuser' );
00129     }
00130 
00138     public function setMallUsersStatus( $blOn = false )
00139     {
00140         $this->_blMallUsers = $blOn;
00141     }
00142 
00150     public function __get( $sParamName )
00151     {
00152         // it saves memory using - loads data only if it is used
00153         switch ( $sParamName ) {
00154             case 'oGroups':
00155                 return $this->_oGroups = $this->getUserGroups();
00156                 break;
00157             case 'iCntNoticeListArticles':
00158                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00159                 break;
00160             case 'iCntWishListArticles':
00161                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00162                 break;
00163             case 'iCntRecommLists':
00164                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00165                 break;
00166             case 'oAddresses':
00167                 return $this->getUserAddresses();
00168                 break;
00169             case 'oPayments':
00170                 return $this->_oPayments = $this->getUserPayments();
00171                 break;
00172             case 'oxuser__oxcountry':
00173                 return $this->oxuser__oxcountry = $this->getUserCountry();
00174                 break;
00175             case 'sDBOptin':
00176                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00177                 break;
00178             case 'sEmailFailed':
00179                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00180                 break;
00181         }
00182     }
00183 
00189     public function getNewsSubscription()
00190     {
00191         if ( $this->_oNewsSubscription !== null ) {
00192             return $this->_oNewsSubscription;
00193         }
00194 
00195         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00196 
00197         // if subscription object is not set yet - we should create one
00198         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00199             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00200 
00201                 // no subscription defined yet - creating one
00202                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00203                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00204                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00205                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00206                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00207             }
00208         }
00209 
00210         return $this->_oNewsSubscription;
00211     }
00212 
00222     public function getUserCountry( $sCountryId = null, $iLang = null )
00223     {
00224         $oDb = oxDb::getDb();
00225         if ( !$sCountryId ) {
00226             $sCountryId = $this->oxuser__oxcountryid->value;
00227         }
00228 
00229         $sQ = "select oxtitle".oxLang::getInstance()->getLanguageTag( $iLang )." from oxcountry where oxid = " . $oDb->quote( $sCountryId ) . " ";
00230         $this->oxuser__oxcountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00231 
00232         return $this->oxuser__oxcountry;
00233     }
00234 
00242     public function getUserCountryId( $sCountry = null )
00243     {
00244         $oDb = oxDb::getDb();
00245         $sQ = "select oxid from oxcountry where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00246         $sCountryId = $oDb->getOne( $sQ );
00247 
00248         return $sCountryId;
00249     }
00250 
00258     public function getUserGroups( $sOXID = null )
00259     {
00260 
00261         if ( isset( $this->_oGroups ) ) {
00262             return $this->_oGroups;
00263         }
00264 
00265         if ( !$sOXID ) {
00266             $sOXID = $this->getId();
00267         }
00268 
00269         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00270         //$this->oGroups->Init( 'oxbase' );
00271         //$this->oGroups->oLstoTpl->Init( array( 'oxgroups', 'oxobject2group' ) );
00272         $sSelect  = 'select oxgroups.* from oxgroups left join oxobject2group on oxobject2group.oxgroupsid = oxgroups.oxid ';
00273         $sSelect .= 'where oxobject2group.oxobjectid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00274         $this->_oGroups->selectString( $sSelect );
00275         return $this->_oGroups;
00276     }
00277 
00285     public function getUserAddresses( $sUserId = null )
00286     {
00287         $sUserId = isset( $sUserId ) ? $sUserId : $this->getId();
00288         if ( !isset( $this->_aAddresses[$sUserId] ) ) {
00289             $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId ) . "";
00290 
00291             //P
00292             $this->_aAddresses[$sUserId] = oxNew( 'oxlist' );
00293             $this->_aAddresses[$sUserId]->init( "oxaddress" );
00294             $this->_aAddresses[$sUserId]->selectString( $sSelect );
00295 
00296             // marking selected
00297             if ( $sAddressId = $this->getSelectedAddressId() ) {
00298                 foreach ( $this->_aAddresses[$sUserId] as $oAddress ) {
00299                     $oAddress->selected = 0;
00300                     if ( $oAddress->getId() === $sAddressId ) {
00301                         $oAddress->selected = 1;
00302                         break;
00303                     }
00304                 }
00305             }
00306         }
00307         return $this->_aAddresses[$sUserId];
00308     }
00309 
00317     public function setSelectedAddressId( $sAddressId )
00318     {
00319         $this->_sSelAddressId = $sAddressId;
00320     }
00321 
00327     public function getSelectedAddressId()
00328     {
00329         if ( $this->_sSelAddressId !== null ) {
00330             return $this->_sSelAddressId;
00331         }
00332 
00333         $sAddressId = oxConfig::getParameter( "oxaddressid");
00334         if ( !$sAddressId && !oxConfig::getParameter( 'reloadaddress' ) ) {
00335             $sAddressId = oxSession::getVar( "deladrid" );
00336         }
00337         return $sAddressId;
00338     }
00339 
00348     public function getSelectedAddress( $sWishId = false )
00349     {
00350         $oAddresses = $this->getUserAddresses();
00351         if ( $oAddresses->count() ) {
00352             if ( $sAddressId = $this->getSelectedAddressId() ) {
00353                 foreach ( $oAddresses as $oAddress ) {
00354                     if ( $oAddress->selected == 1 ) {
00355                         $sAddressId = $oAddress->getId();
00356                         break;
00357                     }
00358                 }
00359             } elseif ( $sWishId ) {
00360                 foreach ( $oAddresses as $oAddress ) {
00361                     $oAddress->selected = 0;
00362                     if ( $oAddress->oxaddress__oxaddressuserid->value == $sWishId ) {
00363                         $oAddress->selected = 1;
00364                         $sAddressId = $oAddress->getId();
00365                     }
00366                 }
00367             }
00368 
00369             // in case none is set - setting first one
00370             if ( !$sAddressId ) {
00371                 $oAddresses->rewind();
00372                 $oAddress = $oAddresses->current();
00373                 $oAddress->selected = 1;
00374                 $sAddressId = $oAddress->getId();
00375             }
00376         }
00377 
00378         return $sAddressId;
00379     }
00380 
00388     public function getUserPayments( $sOXID = null )
00389     {
00390         if ( $this->_oPayments === null ) {
00391 
00392             if ( !$sOXID ) {
00393                 $sOXID = $this->getId();
00394             }
00395 
00396             $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00397 
00398             $this->_oPayments = oxNew( 'oxlist' );
00399             $this->_oPayments->init( 'oxUserPayment' );
00400             $this->_oPayments->selectString( $sSelect );
00401 
00402             $myUtils = oxUtils::getInstance();
00403             foreach ( $this->_oPayments as $oPayment ) {
00404                 // add custom fields to this class
00405                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00406             }
00407         }
00408 
00409         return $this->_oPayments;
00410     }
00411 
00417     public function save()
00418     {
00419         $myConfig  = oxConfig::getInstance();
00420 
00421         $blAddRemark = false;
00422         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00423             $blAddRemark = true;
00424             //save oxregister value
00425             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00426         }
00427 
00428         // setting user rights
00429         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00430 
00431         // processing birth date which came from output as array
00432         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00433             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00434         }
00435 
00436         // checking if user Facebook ID should be updated
00437         if ( $myConfig->getConfigParam( "bl_showFbConnect" ) ) {
00438             $oFb = oxFb::getInstance();
00439             if ( $oFb->isConnected() && $oFb->getUser() ) {
00440                  $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
00441             }
00442         }
00443 
00444         $blRet = parent::save();
00445 
00446         //add registered remark
00447         if ( $blAddRemark && $blRet ) {
00448             $oRemark = oxNew( 'oxremark' );
00449             $oRemark->oxremark__oxtext     = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00450             $oRemark->oxremark__oxtype     = new oxField('r', oxField::T_RAW);
00451             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00452             $oRemark->save();
00453         }
00454 
00455         return $blRet;
00456     }
00457 
00463     public function allowDerivedUpdate()
00464     {
00465         return true;
00466     }
00467 
00475     public function inGroup( $sGroupID )
00476     {
00477         $blIn = false;
00478         if ( ( $oGroups = $this->getUserGroups() ) ) {
00479             $blIn = isset( $oGroups[ $sGroupID ] );
00480         }
00481 
00482         return $blIn;
00483     }
00484 
00493     public function delete( $sOXID = null )
00494     {
00495 
00496         if ( !$sOXID ) {
00497             $sOXID = $this->getId();
00498         }
00499         if ( !$sOXID ) {
00500             return false;
00501         }
00502 
00503         $blDeleted = parent::delete( $sOXID );
00504 
00505         if ( $blDeleted ) {
00506             $oDB = oxDb::getDb();
00507             $sOXIDQuoted = $oDB->quote($sOXID);
00508 
00509             // deleting stored payment, address, group dependencies, remarks info
00510             $rs = $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00511             $rs = $oDB->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00512 
00513             // deleting notice/wish lists
00514             $rs = $oDB->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00515             $rs = $oDB->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00516 
00517             // deleting newsletter subscription
00518             $rs = $oDB->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00519 
00520             // delivery and delivery sets
00521             $rs = $oDB->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00522 
00523             // discounts
00524             $rs = $oDB->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00525 
00526 
00527             // and leaving all order related information
00528             $rs = $oDB->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00529 
00530             $blDeleted = $rs->EOF;
00531         }
00532 
00533         return $blDeleted;
00534     }
00535 
00543     public function load( $oxID )
00544     {
00545 
00546         $blRet = parent::Load( $oxID );
00547 
00548         // convert date's to international format
00549         if ( isset( $this->oxuser__oxcreate->value ) ) {
00550             $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00551         }
00552 
00553         return $blRet;
00554     }
00555 
00563     public function exists( $sOXID = null )
00564     {
00565         $oDb = oxDb::getDb();
00566         if ( !$sOXID ) {
00567             $sOXID = $this->getId();
00568         }
00569 
00570         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00571                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).'';
00572 
00573         if ( $sOXID ) {
00574             $sSelect.= " or oxid = ".$oDb->quote( $sOXID ) . " ) ";
00575         } else {
00576             $sSelect.= ' ) ';
00577         }
00578 
00579         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00580             $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00581         }
00582 
00583         $blExists = false;
00584         if ( ( $sOxid = oxDb::getDb()->getOne( $sSelect ) ) ) {
00585              // update - set oxid
00586             $this->setId( $sOxid );
00587             $blExists = true;
00588         }
00589         return $blExists;
00590     }
00591 
00600     public function getOrders( $iLimit = false, $iPage = 0 )
00601     {
00602         $myConfig = $this->getConfig();
00603         $oOrders = oxNew( 'oxlist' );
00604         $oOrders->init( 'oxorder' );
00605 
00606         if ( $iLimit !== false ) {
00607             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00608         }
00609 
00610         //P
00611         // Lists does not support loading from two tables, so orders
00612         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00613         // forcing to load product info which is used in templates
00614         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00615 
00616         //loading order for registered user
00617         if ( $this->oxuser__oxregister->value > 1 ) {
00618             $sQ = 'select * from oxorder where oxuserid = "'.$this->getId().'" and oxorderdate >= ' . oxDb::getDb()->quote( $this->oxuser__oxregister->value ) . ' ';
00619 
00620             //#1546 - shopid check added, if it is not multishop
00621 
00622             $sQ .= ' order by oxorderdate desc ';
00623             $oOrders->selectString( $sQ );
00624         }
00625 
00626         return $oOrders;
00627     }
00628 
00634     public function getOrderCount()
00635     {
00636         $iCnt = 0;
00637         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00638             $oDb = oxDb::getDb();
00639             $sQ  = 'select count(*) from oxorder where oxuserid = "'.$this->getId().'" AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00640             $iCnt = (int) $oDb->getOne( $sQ );
00641         }
00642 
00643         return $iCnt;
00644     }
00645 
00651     public function getNoticeListArtCnt()
00652     {
00653         if ( $this->_iCntNoticeListArticles === null ) {
00654             $this->_iCntNoticeListArticles = 0;
00655             if ( $this->getId() ) {
00656                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00657             }
00658         }
00659         return $this->_iCntNoticeListArticles;
00660     }
00661 
00667     public function getWishListArtCnt()
00668     {
00669         if ( $this->_iCntWishListArticles === null ) {
00670             $this->_iCntWishListArticles = false;
00671             if ( $this->getId() ) {
00672                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00673             }
00674         }
00675         return $this->_iCntWishListArticles;
00676     }
00677 
00684     public function getActiveCountry()
00685     {
00686         $sDeliveryCountry = '';
00687         if ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) {
00688             $oDelAddress = oxNew( 'oxaddress' );
00689             $oDelAddress->load( $soxAddressId );
00690             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00691         } elseif ( $this->getId() ) {
00692             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00693         } else {
00694             $oUser = oxNew( 'oxuser' );
00695             if ( $oUser->loadActiveUser() ) {
00696                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00697             }
00698         }
00699 
00700         return $sDeliveryCountry;
00701     }
00702 
00710     public function createUser()
00711     {
00712         $oDB = oxDb::getDb();
00713         $sShopID = $this->getConfig()->getShopId();
00714 
00715         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00716         $sSelect = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00717         if ( !$this->_blMallUsers ) {
00718             $sSelect .= " and oxshopid = '{$sShopID}' ";
00719         }
00720         $sOXID = $oDB->getOne( $sSelect );
00721 
00722         // user without password found - lets use
00723         if ( isset( $sOXID ) && $sOXID ) {
00724             // try to update
00725             $this->delete( $sOXID );
00726         } elseif ( $this->_blMallUsers ) {
00727             // must be sure if there is no dublicate user
00728             $sQ = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00729             if ( $oDB->getOne( $sQ ) ) {
00730                 $oEx = oxNew( 'oxUserException' );
00731                 $oLang = oxLang::getInstance();
00732                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00733                 throw $oEx;
00734             }
00735         }
00736 
00737         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00738         if ( ( $blOK = $this->save() ) ) {
00739             // dropping/cleaning old delivery address/payment info
00740             $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00741             $oDB->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00742         } else {
00743             $oEx = oxNew( 'oxUserException' );
00744             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00745             throw $oEx;
00746         }
00747 
00748         return $blOK;
00749     }
00750 
00758     public function addToGroup( $sGroupID )
00759     {
00760         if ( !$this->inGroup( $sGroupID ) ) {
00761             $oNewGroup = oxNew( 'oxobject2group' );
00762             $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00763             $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00764             if ( $oNewGroup->save() ) {
00765                 $this->_oGroups[$sGroupID] = $oNewGroup;
00766                 return true;
00767             }
00768         }
00769         return false;
00770     }
00771 
00779     public function removeFromGroup( $sGroupID = null )
00780     {
00781         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00782             $oGroups = oxNew( 'oxlist' );
00783             $oGroups->init( 'oxobject2group' );
00784             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00785             $oGroups->selectString( $sSelect );
00786             foreach ( $oGroups as $oRemgroup ) {
00787                 if ( $oRemgroup->delete() ) {
00788                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00789                 }
00790             }
00791         }
00792     }
00793 
00802     public function onOrderExecute( $oBasket, $iSuccess )
00803     {
00804 
00805         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00806             //adding user to particular customer groups
00807             if ( !$this->oxuser__oxdisableautogrp->value ) {
00808 
00809                 $myConfig = $this->getConfig();
00810                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00811                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00812 
00813                 $this->addToGroup( 'oxidcustomer' );
00814                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00815                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00816                     $this->addToGroup( 'oxidsmallcust' );
00817                 }
00818                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00819                     $this->addToGroup( 'oxidmiddlecust' );
00820                 }
00821                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00822                     $this->addToGroup( 'oxidgoodcust' );
00823                 }
00824             }
00825 
00826             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00827                 $this->removeFromGroup( 'oxidnotyetordered' );
00828             }
00829         }
00830     }
00831 
00839     public function getBasket( $sName )
00840     {
00841         if ( !isset( $this->_aBaskets[$sName] ) ) {
00842             $oBasket = oxNew( 'oxuserbasket' );
00843             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00844 
00845             // creating if it does not exist
00846             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00847                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00848                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00849 
00850                 // marking basket as new (it will not be saved in DB yet)
00851                 $oBasket->setIsNewBasket();
00852             }
00853 
00854             $this->_aBaskets[$sName] = $oBasket;
00855         }
00856 
00857         return $this->_aBaskets[$sName];
00858     }
00859 
00868     public function convertBirthday( $aData )
00869     {
00870 
00871         // preparing data to process
00872         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00873         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00874         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00875 
00876         // leaving empty if not set
00877         if ( !$iYear && !$iMonth && !$iDay )
00878             return "";
00879 
00880         // year
00881         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00882             $iYear = date('Y');
00883 
00884         // month
00885         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00886             $iMonth = 1;
00887 
00888         // maximum nuber of days in month
00889         $iMaxDays = 31;
00890         switch( $iMonth) {
00891             case 2 :
00892                 if ($iMaxDays > 28)
00893                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00894                 break;
00895             case 4  :
00896             case 6  :
00897             case 9  :
00898             case 11 :
00899                 $iMaxDays = min(30, $iMaxDays);
00900                 break;
00901         }
00902 
00903         // day
00904         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00905             $iDay = 1;
00906         }
00907 
00908         // whole date
00909         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00910     }
00911 
00918     public function getBoni()
00919     {
00920         return 1000;
00921     }
00922 
00935     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00936     {
00937         // preparing input
00938         $sDynGoup = strtolower( trim( $sDynGoup ) );
00939 
00940         // setting denied groups from admin settings also
00941         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
00942 
00943         // default state ..
00944         $blAdd = false;
00945 
00946         // user assignment to dyn group is not allowed
00947         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
00948             $blAdd = false;
00949         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
00950             // trying to add user to prohibited user group?
00951             $blAdd = false;
00952         } elseif ( $this->addToGroup( $sDynGoup ) ) {
00953             $blAdd = true;
00954         }
00955 
00956         // cleanup
00957         oxSession::deleteVar( 'dgr' );
00958 
00959         return $blAdd;
00960     }
00961 
00977     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
00978     {
00979         // 1. checking user name
00980         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
00981 
00982         // 2. cheking email
00983         $this->_checkEmail( $sLogin );
00984 
00985         // 3. password
00986         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
00987 
00988         // 4. required fields
00989         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
00990 
00991         // 5. country check
00992         $this->_checkCountries( $aInvAddress, $aDelAddress );
00993 
00994         // 6. vat id check.
00995             $this->_checkVatId( $aInvAddress );
00996     }
00997 
01006     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
01007     {
01008         // assigning to newsletter
01009         $blSuccess = false;
01010         $myConfig  = $this->getConfig();
01011 
01012         // user wants to get newsletter messages or no ?
01013         $oNewsSubscription = $this->getNewsSubscription();
01014         if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
01015             if ( !$blSendOptIn ) {
01016 
01017                 // double-opt-in check is disabled - assigning automatically
01018                 $this->addToGroup( 'oxidnewsletter' );
01019                 // and setting subscribed status
01020                 $oNewsSubscription->setOptInStatus( 1 );
01021                 $blSuccess = true;
01022             } else {
01023 
01024                 // double-opt-in check enabled - sending confirmation email and setting waiting status
01025                 $oNewsSubscription->setOptInStatus( 2 );
01026 
01027                 // sending double-opt-in mail
01028                 $oEmail = oxNew( 'oxemail' );
01029                 $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01030             }
01031         } elseif ( !$blSubscribe ) {
01032             // removing user from newsletter subscribers
01033             $this->removeFromGroup( 'oxidnewsletter' );
01034             $oNewsSubscription->setOptInStatus( 0 );
01035             $blSuccess = true;
01036         }
01037 
01038         return $blSuccess;
01039     }
01040 
01058     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01059     {
01060 
01061         // validating values before saving. If validation fails - exception is thrown
01062         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01063 
01064         // input data is fine - lets save updated user info
01065         $this->assign( $aInvAddress );
01066 
01067 
01068         // update old or add new delivery address
01069         $this->_assignAddress( $aDelAddress );
01070 
01071         // saving new values
01072         if ( $this->save() ) {
01073 
01074             // assigning automatically to specific groups
01075             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01076             $this->_setAutoGroups( $sCountryId );
01077         }
01078     }
01079 
01088     public function addUserAddress( $oUser )
01089     {
01090         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01091             return false;
01092         }
01093 
01094         $oAddress = oxNew( 'oxaddress' );
01095         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01096         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01097         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01098         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01099         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01100         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01101         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01102         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01103         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01104         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01105         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01106 
01107         // adding new address
01108         if ( $oAddress->save() ) {
01109             // resetting addresses
01110             $this->_aAddresses = null;
01111             return $oAddress->getId();
01112         }
01113     }
01114 
01122     protected function _assignAddress( $aDelAddress )
01123     {
01124         if (isset($aDelAddress) && count($aDelAddress)) {
01125             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01126             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01127             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01128             $oAddress = oxNew( 'oxaddress' );
01129             $oAddress->assign( $aDelAddress );
01130             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01131             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01132             $oAddress->save();
01133 
01134             // resetting addresses
01135             $this->_aAddresses = null;
01136 
01137             // saving delivery Address for later use
01138             oxSession::setVar( 'deladrid', $oAddress->getId() );
01139         } else {
01140             // resetting
01141             oxSession::setVar( 'deladrid', null );
01142         }
01143     }
01144 
01157     public function login( $sUser, $sPassword, $blCookie = false)
01158     {
01159         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01160             $oEx = oxNew( 'oxCookieException' );
01161             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01162             throw $oEx;
01163         }
01164 
01165         $myConfig = $this->getConfig();
01166         if ( $sPassword ) {
01167 
01168             $sShopID = $myConfig->getShopId();
01169             $oDb = oxDb::getDb();
01170 
01171             $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01172             $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01173             $sShopSelect = "";
01174 
01175 
01176             // admin view: can only login with higher than 'user' rights
01177             if ( $this->isAdmin() ) {
01178                 $sShopSelect = " and ( oxrights != 'user' ) ";
01179             }
01180 
01181             $sWhat = "oxid";
01182 
01183             $sSelect =  "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01184             if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
01185                 if ( $sPassword == "admin" && $sUser == "admin" ) {
01186                     $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01187                 } else {
01188                     $oEx = oxNew( 'oxUserException' );
01189                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01190                     throw $oEx;
01191                 }
01192             }
01193 
01194             // load from DB
01195             $aData = $oDb->getAll( $sSelect );
01196             $sOXID = @$aData[0][0];
01197             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01198 
01199                 if ( !$this->load( $sOXID ) ) {
01200                     $oEx = oxNew( 'oxUserException' );
01201                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01202                     throw $oEx;
01203                 }
01204             }
01205         }
01206 
01207 
01208         //login successfull?
01209         if ( $this->oxuser__oxid->value ) {
01210             // yes, successful login
01211             if ( $this->isAdmin() ) {
01212                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01213             } else {
01214                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01215             }
01216 
01217             // cookie must be set ?
01218             if ( $blCookie ) {
01219                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01220             }
01221             return true;
01222         } else {
01223             $oEx = oxNew( 'oxUserException' );
01224             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01225             throw $oEx;
01226         }
01227     }
01228 
01239     public function openIdLogin( $sUser )
01240     {
01241         $myConfig = $this->getConfig();
01242         $sShopID = $myConfig->getShopId();
01243         $oDb = oxDb::getDb();
01244 
01245         $sUserSelect = "oxuser.oxusername = " . $oDb->quote( $sUser );
01246         $sShopSelect = "";
01247 
01248 
01249         $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01250 
01251         // load from DB
01252         $aData = $oDb->getAll( $sSelect );
01253         $sOXID = @$aData[0][0];
01254         if ( isset( $sOXID ) && $sOXID ) {
01255 
01256             if ( !$this->load( $sOXID ) ) {
01257                 $oEx = oxNew( 'oxUserException' );
01258                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01259                 throw $oEx;
01260             }
01261         }
01262 
01263         //login successfull?
01264         if ( $this->oxuser__oxid->value ) {
01265             // yes, successful login
01266             oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01267             return true;
01268         } else {
01269             $oEx = oxNew( 'oxUserException' );
01270             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01271             throw $oEx;
01272         }
01273     }
01274 
01280     public function logout()
01281     {
01282         // deleting session info
01283         oxSession::deleteVar( 'usr' );  // for front end
01284         oxSession::deleteVar( 'auth' ); // for back end
01285         oxSession::deleteVar( 'dgr' );
01286         oxSession::deleteVar( 'dynvalue' );
01287         oxSession::deleteVar( 'paymentid' );
01288         // oxSession::deleteVar( 'deladrid' );
01289 
01290         // delete cookie
01291         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01292 
01293         // unsetting global user
01294         $this->setUser( null );
01295 
01296         return true;
01297     }
01298 
01305     public function loadAdminUser()
01306     {
01307         return $this->loadActiveUser( true );
01308     }
01309 
01318     public function loadActiveUser( $blForceAdmin = false )
01319     {
01320         $myConfig = $this->getConfig();
01321 
01322         $blAdmin = $myConfig->isAdmin() || $blForceAdmin;
01323         $oDB = oxDb::getDb();
01324 
01325         // first - checking session info
01326         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01327         $blFoundInCookie = false;
01328 
01329         //trying automatic login (by 'remember me' cookie)
01330         if ( !$sUserID && !$blAdmin ) {
01331             $sShopID = $myConfig->getShopId();
01332             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01333                 $aData = explode( '@@@', $sSet );
01334                 $sUser = $aData[0];
01335                 $sPWD  = @$aData[1];
01336 
01337                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDB->quote($sUser);
01338 
01339 
01340                 $oDB = oxDb::getDb();
01341                 $rs = $oDB->execute( $sSelect );
01342                 if ( $rs != false && $rs->recordCount() > 0 ) {
01343                     while (!$rs->EOF) {
01344                         $sTest = crypt( $rs->fields[1], 'ox' );
01345                         if ( $sTest == $sPWD ) {
01346                             // found
01347                             $sUserID = $rs->fields[0];
01348                             $blFoundInCookie = true;
01349                             break;
01350                         }
01351                         $rs->moveNext();
01352                     }
01353                 }
01354             }
01355         }
01356 
01357         // Checking if user is connected via Facebook connect.
01358         // If yes, trying to login user using user Facebook ID
01359         if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01360             $oFb = oxFb::getInstance();
01361             if ( $oFb->isConnected() && $oFb->getUser() ) {
01362                 $sUserSelect = "oxuser.oxfbid = " . $oDB->quote( $oFb->getUser() );
01363                 $sShopSelect = "";
01364 
01365 
01366                 $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01367                 $sUserID = $oDB->getOne( $sSelect );
01368             }
01369         }
01370 
01371         // checking user results
01372         if ( $sUserID ) {
01373             if ( $this->load( $sUserID ) ) {
01374                 // storing into session
01375                 if ($blAdmin) {
01376                     oxSession::setVar( 'auth', $sUserID );
01377                 } else {
01378                     oxSession::setVar( 'usr', $sUserID );
01379                 }
01380 
01381                 // marking the way user was loaded
01382                 $this->_blLoadedFromCookie = $blFoundInCookie;
01383                 return true;
01384             }
01385         } else {
01386             // no user
01387             oxSession::deleteVar( 'usr' );
01388             oxSession::deleteVar( 'auth' );
01389 
01390             return false;
01391         }
01392     }
01393 
01406     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01407     {
01408         include "oxldap.php";
01409         $myConfig = $this->getConfig();
01410         $oDb = oxDb::getDb();
01411         //$throws oxConnectionException
01412         $aLDAPParams = $myConfig->getConfigParam( 'aLDAPParams' );
01413         $oLDAP = new oxLDAP( $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01414         // maybe this is LDAP user but supplied email Address instead of LDAP login
01415         $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote($sUser)." $sShopSelect");
01416         if ( isset( $sLDAPKey) && $sLDAPKey) {
01417             $sUser = $sLDAPKey;
01418         }
01419 
01420         //$throws oxConnectionException
01421         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01422 
01423         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01424         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01425             // login successful
01426 
01427             // check if user is already in database
01428             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote($aData['OXUSERNAME'])." $sShopSelect";
01429             $sOXID = $oDb->getOne( $sSelect);
01430 
01431             if ( !isset( $sOXID) || !$sOXID) {
01432                 // we need to create a new user
01433                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01434                 $this->setId();
01435 
01436                 // map all user data fields
01437                 foreach ( $aData as $fldname => $value) {
01438                     $sField = "oxuser__".strtolower( $fldname);
01439                     $this->$sField->setValue($aData[$fldname]);
01440                 }
01441 
01442                 $this->oxuser__oxactive->setValue(1);
01443                 $this->oxuser__oxshopid->setValue($sShopID);
01444                 $this->oxuser__oxldapkey->setValue($sUser);
01445                 $this->oxuser__oxrights->setValue("user");
01446                 $this->setPassword( "ldap user" );
01447 
01448                 $this->save();
01449             } else {
01450                 // LDAP user is already in OXID DB, load it
01451                 $this->load( $sOXID);
01452             }
01453 
01454         } else {
01455             $oEx = oxNew( 'oxUserException' );
01456             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01457             throw $oEx;
01458         }
01459     }
01460 
01467     protected function _getUserRights()
01468     {
01469         // previously user had no rights defined
01470         if ( !$this->oxuser__oxrights->value )
01471             return 'user';
01472 
01473         $oDB = oxDb::getDb();
01474         $myConfig    = $this->getConfig();
01475         $sAuthRights = null;
01476 
01477         // choosing possible user rights index
01478         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01479         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01480         if ( $sAuthUserID ) {
01481             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01482         }
01483 
01484         //preventing user rights edit for non admin
01485         $aRights = array();
01486 
01487         // selecting current users rights ...
01488         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01489             $aRights[] = $sCurrRights;
01490         }
01491         $aRights[] = 'user';
01492 
01493         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01494             return current( $aRights );
01495         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01496             $aRights[] = $sAuthRights;
01497             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01498                 return current( $aRights );
01499             }
01500         }
01501 
01502         // leaving as it was set ...
01503         return $this->oxuser__oxrights->value;
01504     }
01505 
01515     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01516     {
01517 
01518         /*if ( !$myConfig->blMallUsers ) {
01519             $sShopID = $myConfig->getShopId();
01520             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01521         }*/
01522 
01523         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01524     }
01525 
01531     protected function _insert()
01532     {
01533 
01534         // set oxcreate date
01535         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01536 
01537         if ( !isset( $this->oxuser__oxboni->value ) ) {
01538             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01539         }
01540 
01541         return parent::_insert();
01542     }
01543 
01549     protected function _update()
01550     {
01551         //V #M418: for not registered users, don't change boni during update
01552         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01553             $this->_aSkipSaveFields[] = 'oxboni';
01554         }
01555 
01556         // don't change this field
01557         $this->_aSkipSaveFields[] = 'oxcreate';
01558         if ( !$this->isAdmin() ) {
01559             $this->_aSkipSaveFields[] = 'oxcustnr';
01560             $this->_aSkipSaveFields[] = 'oxrights';
01561         }
01562 
01563         // updating subscription information
01564         if ( ( $blUpdate = parent::_update() ) ) {
01565             $this->getNewsSubscription()->updateSubscription( $this );
01566         }
01567 
01568         return $blUpdate;
01569     }
01570 
01586     protected function _checkLogin( $sLogin, $aInvAddress )
01587     {
01588         $myConfig = $this->getConfig();
01589 
01590         $sLogin   = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername']:$sLogin;
01591 
01592         // check only for users with password during registration
01593         // if user wants to change user name - we must check if passwords are ok before changing
01594         if ( $this->oxuser__oxpassword->value && $sLogin != $this->oxuser__oxusername->value ) {
01595 
01596             // on this case password must be taken directly from request
01597             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
01598             if ( !$sNewPass ) {
01599 
01600                 // 1. user forgot to enter password
01601                 $oEx = oxNew( 'oxInputException' );
01602                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01603                 throw $oEx;
01604             } else {
01605 
01606                 // 2. entered wrong password
01607                 if ( !$this->isSamePassword( $sNewPass ) ) {
01608                     $oEx = oxNew( 'oxUserException' );
01609                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01610                     throw $oEx;
01611                 }
01612             }
01613         }
01614 
01615         if ( $this->checkIfEmailExists( $sLogin ) ) {
01616             //if exists then we do now allow to do that
01617             $oEx = oxNew( 'oxUserException' );
01618             $oLang = oxLang::getInstance();
01619             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
01620             throw $oEx;
01621         }
01622 
01623         return $sLogin;
01624     }
01625 
01633     public function checkIfEmailExists( $sEmail )
01634     {
01635         $myConfig = $this->getConfig();
01636         $oDB = oxDb::getDb();
01637         $iShopId = $myConfig->getShopId();
01638         $blExists = false;
01639 
01640         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01641         if ( ( $sOxid = $this->getId() ) ) {
01642             $sQ .= " and oxid <> '$sOxid' ";
01643         }
01644 
01645         $oRs = $oDB->execute( $sQ );
01646         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01647 
01648             if ( $this->_blMallUsers ) {
01649 
01650                 $blExists = true;
01651                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01652 
01653                     // password is not set - allow to override
01654                     $blExists = false;
01655                 }
01656             } else {
01657 
01658                 $blExists = false;
01659                 while ( !$oRs->EOF ) {
01660                     if ( $oRs->fields[1] != 'user' ) {
01661 
01662                         // exists admin with same login - must not allow
01663                         $blExists = true;
01664                         break;
01665                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01666 
01667                         // exists same login (with password) in same shop
01668                         $blExists = true;
01669                         break;
01670                     }
01671 
01672                     $oRs->moveNext();
01673                 }
01674             }
01675         }
01676         return $blExists;
01677     }
01678 
01686     public function getUserRecommLists( $sOXID = null )
01687     {
01688         if ( !$sOXID )
01689             $sOXID = $this->getId();
01690 
01691         // sets active page
01692         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01693         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01694 
01695         // load only lists which we show on screen
01696         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01697         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01698 
01699 
01700         $oRecommList = oxNew( 'oxlist' );
01701         $oRecommList->init( 'oxrecommlist' );
01702         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01703         $iShopId = $this->getConfig()->getShopId();
01704         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01705         $oRecommList->selectString( $sSelect );
01706 
01707         return $oRecommList;
01708     }
01709 
01717     public function getRecommListsCount( $sOx = null )
01718     {
01719         if ( !$sOx ) {
01720             $sOx = $this->getId();
01721         }
01722 
01723         if ( $this->_iCntRecommLists === null || $sOx ) {
01724             $oDb = oxDb::getDb();
01725             $this->_iCntRecommLists = 0;
01726             $iShopId = $this->getConfig()->getShopId();
01727             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01728             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01729         }
01730         return $this->_iCntRecommLists;
01731     }
01732 
01741     protected function _checkEmail( $sEmail )
01742     {
01743         // missing email address (user login name) ?
01744         if ( !$sEmail ) {
01745             $oEx = oxNew( 'oxInputException' );
01746             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01747             throw $oEx;
01748         }
01749 
01750         // invalid email address ?
01751         if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
01752             $oEx = oxNew( 'oxInputException' );
01753             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
01754             throw $oEx;
01755         }
01756     }
01757 
01772     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01773     {
01774         $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01775     }
01776 
01789     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01790     {
01791         //  no password at all
01792         if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
01793             $oEx = oxNew( 'oxInputException' );
01794             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
01795             throw $oEx;
01796         }
01797 
01798         //  password is too short ?
01799         if ( $blCheckLenght &&  getStr()->strlen( $sNewPass ) < 6 ) {
01800             $oEx = oxNew( 'oxInputException' );
01801             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
01802             throw $oEx;
01803         }
01804 
01805         //  passwords do not match ?
01806         if ( $sNewPass != $sConfPass ) {
01807             $oEx = oxNew( 'oxUserException' );
01808             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01809             throw $oEx;
01810         }
01811     }
01812 
01823     protected function _checkCountries( $aInvAddress, $aDelAddress )
01824     {
01825         $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
01826         $sDelCtry  = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
01827 
01828         if ( $sBillCtry || $sDelCtry ) {
01829             $oDb = oxDb::getDb();
01830 
01831             if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
01832                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
01833                 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
01834             } else {
01835                 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
01836                               ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
01837             }
01838 
01839             if ( !$oDb->getOne( $sQ ) ) {
01840                 $oEx = oxNew( 'oxUserException' );
01841                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS' );
01842                 throw $oEx;
01843             }
01844         }
01845     }
01846 
01858     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01859     {
01860         // collecting info about required fields
01861         $aMustFields = array( 'oxuser__oxfname',
01862                               'oxuser__oxlname',
01863                               'oxuser__oxstreetnr',
01864                               'oxuser__oxstreet',
01865                               'oxuser__oxzip',
01866                               'oxuser__oxcity' );
01867 
01868         // config shoud override default fields
01869         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
01870         if ( is_array( $aMustFillFields ) ) {
01871             $aMustFields = $aMustFillFields;
01872         }
01873 
01874         // assuring data to check
01875         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
01876         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
01877 
01878         // collecting fields
01879         $aFields = array_merge( $aInvAddress, $aDelAddress );
01880 
01881 
01882         // check delivery address ?
01883         $blCheckDel = false;
01884         if ( count( $aDelAddress ) ) {
01885             $blCheckDel = true;
01886         }
01887 
01888         // checking
01889         foreach ( $aMustFields as $sMustField ) {
01890 
01891             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
01892             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
01893                 continue;
01894             }
01895 
01896             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
01897                 $this->_checkRequiredArrayFields( $sMustField, $aFields[$sMustField] );
01898             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
01899                    $oEx = oxNew( 'oxInputException' );
01900                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01901                    throw $oEx;
01902             }
01903         }
01904     }
01905 
01916     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01917     {
01918         foreach ( $aFieldValues as $sValue ) {
01919             if ( !trim( $sValue ) ) {
01920                 $oEx = oxNew( 'oxInputException' );
01921                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01922                 throw $oEx;
01923             }
01924         }
01925     }
01926 
01937     protected function _checkVatId( $aInvAddress )
01938     {
01939         if ( $aInvAddress['oxuser__oxustid'] ) {
01940 
01941             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
01942                 // no country
01943                 return;
01944             }
01945             $oCountry = oxNew('oxcountry');
01946             if (!$oCountry->load($sCountryId)) {
01947                 throw new oxObjectException();
01948             }
01949             if ($oCountry->isForeignCountry() && $oCountry->isInEU()) {
01950                     if (strncmp($aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2)) {
01951                         $oEx = oxNew( 'oxInputException' );
01952                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
01953                         throw $oEx;
01954                     }
01955             }
01956         }
01957     }
01958 
01967     protected function _setAutoGroups( $sCountryId )
01968     {
01969         // assigning automatically to specific groups
01970         $blForeigner = true;
01971         $blForeignGroupExists = false;
01972         $blInlandGroupExists = false;
01973 
01974         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01975         // foreigner ?
01976         if ( is_array($aHomeCountry)) {
01977             if (in_array($sCountryId, $aHomeCountry)) {
01978                 $blForeigner = false;
01979             }
01980         } elseif ($sCountryId == $aHomeCountry) {
01981             $blForeigner = false;
01982         }
01983 
01984         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01985             $blForeignGroupExists = true;
01986             if ( !$blForeigner ) {
01987                 $this->removeFromGroup( 'oxidforeigncustomer' );
01988             }
01989         }
01990 
01991         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01992             $blInlandGroupExists = true;
01993             if ( $blForeigner ) {
01994                 $this->removeFromGroup( 'oxidnewcustomer' );
01995             }
01996         }
01997 
01998         if ( !$this->oxuser__oxdisableautogrp->value ) {
01999             if ( !$blForeignGroupExists && $blForeigner ) {
02000                 $this->addToGroup( 'oxidforeigncustomer' );
02001             }
02002             if ( !$blInlandGroupExists && !$blForeigner ) {
02003                 $this->addToGroup( 'oxidnewcustomer' );
02004             }
02005         }
02006     }
02007 
02015     protected function _hasUserAddress( $sUserId )
02016     {
02017         $oAddresses = $this->getUserAddresses();
02018         if ( $oAddresses && count($oAddresses)>0 ) {
02019             $oAddresses->rewind() ;
02020             foreach ($oAddresses as $key => $oAddress) {
02021                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
02022                     return true;
02023                 }
02024             }
02025         }
02026         return false;
02027     }
02028 
02041     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
02042     {
02043         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
02044     }
02045 
02055     protected function _deleteUserCookie( $sShopId = null )
02056     {
02057         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
02058     }
02059 
02069     protected static function _getUserCookie( $sShopId = null )
02070     {
02071         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
02072     }
02073 
02074 
02083     public function loadUserByUpdateId( $sUid )
02084     {
02085         $oDb = oxDb::getDb();
02086         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02087         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02088             return $this->load( $sUserId );
02089         }
02090     }
02091 
02099     public function setUpdateKey( $blReset = false )
02100     {
02101         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02102         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02103 
02104         // generating key
02105         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02106 
02107         // setting expiration time for 6 hours
02108         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02109 
02110         // saving
02111         $this->save();
02112     }
02113 
02119     public function getUpdateLinkTerm()
02120     {
02121         return 3600 * 6;
02122     }
02123 
02131     public function isExpiredUpdateId( $sKey )
02132     {
02133         $oDb = oxDb::getDb();
02134         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02135         return !( (bool) $oDb->getOne( $sQ ) );
02136     }
02137 
02143     public function getUpdateId()
02144     {
02145         if ( $this->_sUpdateKey === null ) {
02146             $this->setUpdateKey();
02147             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02148         }
02149         return $this->_sUpdateKey;
02150     }
02151 
02160     public function encodePassword( $sPassword, $sSalt )
02161     {
02162         $oDb = oxDb::getDb();
02163         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02164     }
02165 
02173     public function prepareSalt( $sSalt )
02174     {
02175         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02176     }
02177 
02185     public function decodeSalt( $sSaltHex )
02186     {
02187         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02188     }
02189 
02197     public function setPassword( $sPassword = null )
02198     {
02199         // setting salt if password is not empty
02200         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02201 
02202         // encoding only if password was not empty (e.g. user registration without pass)
02203         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02204 
02205         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02206         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02207     }
02208 
02216     public function isSamePassword( $sNewPass )
02217     {
02218         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02219     }
02220 
02226     public function isLoadedFromCookie()
02227     {
02228         return $this->_blLoadedFromCookie;
02229     }
02230 
02237     public function getPasswordHash()
02238     {
02239         $sHash = null;
02240         if ( $this->oxuser__oxpassword->value ) {
02241             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02242                 // decodable pass ?
02243                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02244             } elseif ( ( strlen( $this->oxuser__oxpassword->value ) < 32 ) && ( strpos( $this->oxuser__oxpassword->value, 'openid_' ) !== 0 ) ) {
02245                 // plain pass ?
02246                 $this->setPassword( $this->oxuser__oxpassword->value );
02247             }
02248             $sHash = $this->oxuser__oxpassword->value;
02249         }
02250         return $sHash;
02251     }
02252 
02261     public static function getAdminUser()
02262     {
02263         return self::getActiveUser( true );
02264     }
02265 
02276     public static function getActiveUser( $blForceAdmin = false )
02277     {
02278         $oUser = oxNew( 'oxuser' );
02279         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02280             return $oUser;
02281         } else {
02282             return false;
02283         }
02284     }
02285 
02293     public function getOpenIdPassword( $iLength = 25 )
02294     {
02295         $sPassword= "openid_".substr( oxUtilsObject::getInstance()->generateUId(), 0, $iLength);
02296         return $sPassword;
02297     }
02298 
02306     public function getReviewUserHash( $sUserId )
02307     {
02308         $oDb = oxDb::getDb();
02309         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02310         return $sReviewUserHash;
02311     }
02312 
02320     public function getReviewUserId( $sReviewUserHash )
02321     {
02322         $oDb = oxDb::getDb();
02323         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02324         return $sUserId;
02325     }
02326 
02332     public function getState()
02333     {
02334         return $this->oxuser__oxstateid->value;
02335     }
02336 
02342     public function isTermsAccepted()
02343     {
02344         $sShopId = $this->getConfig()->getShopId();
02345         $sUserId = $this->getId();
02346         return (bool) oxDb::getDb()->getOne( "select 1 from oxacceptedterms where oxuserid='{$sUserId}' and oxshopid='{$sShopId}'" );
02347     }
02348 
02354     public function acceptTerms()
02355     {
02356         $sUserId  = $this->getId();
02357         $sShopId  = $this->getConfig()->getShopId();
02358         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02359 
02360         oxDb::getDb()->execute( "replace oxacceptedterms set oxuserid='{$sUserId}', oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02361     }
02362 
02371     public function setCreditPointsForRegistrant( $sUserId )
02372     {
02373         $blSet   = false;
02374         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02375         if ( $iPoints ) {
02376             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02377             if ( $blSet = $this->save() ) {
02378                 $oDb = oxDb::getDb();
02379 
02380                 // updating users statistics
02381                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ". $oDb->quote( $sUserId ) );
02382 
02383                 $oInvUser = oxNew( "oxuser" );
02384                 if ( $oInvUser->load( $sUserId ) ) {
02385                     $blSet = $oInvUser->setCreditPointsForInviter();
02386                 }
02387             }
02388 
02389             oxSession::deleteVar( 'su' );
02390         }
02391 
02392         return $blSet;
02393     }
02394 
02400     public function setCreditPointsForInviter()
02401     {
02402         $blSet   = false;
02403         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02404         if ( $iPoints ) {
02405             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02406             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02407             $blSet = $this->save();
02408         }
02409 
02410         return $blSet;
02411     }
02412 
02418     public function updateFbId()
02419     {
02420         $oFb = oxFb::getInstance();
02421         $blRet = false;
02422 
02423         if ( $oFb->isConnected() && $oFb->getUser() ) {
02424              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02425              $blRet = $this->save();
02426         }
02427 
02428         return $blRet;
02429     }
02430 }