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         $mySession = $this->getSession();
01012 
01013         // user wants to get newsletter messages or no ?
01014         $oNewsSubscription = $this->getNewsSubscription();
01015         if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
01016             if ( !$blSendOptIn ) {
01017 
01018                 // double-opt-in check is disabled - assigning automatically
01019                 $this->addToGroup( 'oxidnewsletter' );
01020                 // and setting subscribed status
01021                 $oNewsSubscription->setOptInStatus( 1 );
01022                 $blSuccess = true;
01023             } else {
01024 
01025                 $oNewsSubscription->setOptInStatus( 2 );
01026 
01027                 // double-opt-in check enabled - sending confirmation email and setting waiting status
01028                 if ( !$mySession->getVar( "blDBOptInMailAlreadyDone" ) ) {
01029 
01030                     // sending double-opt-in mail
01031                     $oEmail = oxNew( 'oxemail' );
01032                     $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01033 
01034                     if ( $blSuccess ) {
01035                         //setting in seesion parameter to force sending email only once (#2033)
01036                         $mySession->setVar( "blDBOptInMailAlreadyDone", true );
01037                     }
01038                 } else {
01039                     // mail already was sent, so just confirming that
01040                     $blSuccess = true;
01041                 }
01042             }
01043         } elseif ( !$blSubscribe ) {
01044             // removing user from newsletter subscribers
01045             $this->removeFromGroup( 'oxidnewsletter' );
01046             $oNewsSubscription->setOptInStatus( 0 );
01047             $blSuccess = true;
01048         }
01049 
01050         return $blSuccess;
01051     }
01052 
01070     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01071     {
01072 
01073         // validating values before saving. If validation fails - exception is thrown
01074         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01075 
01076         // input data is fine - lets save updated user info
01077         $this->assign( $aInvAddress );
01078 
01079 
01080         // update old or add new delivery address
01081         $this->_assignAddress( $aDelAddress );
01082 
01083         // saving new values
01084         if ( $this->save() ) {
01085 
01086             // assigning automatically to specific groups
01087             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01088             $this->_setAutoGroups( $sCountryId );
01089         }
01090     }
01091 
01100     public function addUserAddress( $oUser )
01101     {
01102         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01103             return false;
01104         }
01105 
01106         $oAddress = oxNew( 'oxaddress' );
01107         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01108         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01109         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01110         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01111         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01112         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01113         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01114         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01115         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01116         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01117         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01118 
01119         // adding new address
01120         if ( $oAddress->save() ) {
01121             // resetting addresses
01122             $this->_aAddresses = null;
01123             return $oAddress->getId();
01124         }
01125     }
01126 
01134     protected function _assignAddress( $aDelAddress )
01135     {
01136         if (isset($aDelAddress) && count($aDelAddress)) {
01137             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01138             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01139             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01140             $oAddress = oxNew( 'oxaddress' );
01141             $oAddress->assign( $aDelAddress );
01142             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01143             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01144             $oAddress->save();
01145 
01146             // resetting addresses
01147             $this->_aAddresses = null;
01148 
01149             // saving delivery Address for later use
01150             oxSession::setVar( 'deladrid', $oAddress->getId() );
01151         } else {
01152             // resetting
01153             oxSession::setVar( 'deladrid', null );
01154         }
01155     }
01156 
01169     public function login( $sUser, $sPassword, $blCookie = false)
01170     {
01171         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01172             $oEx = oxNew( 'oxCookieException' );
01173             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01174             throw $oEx;
01175         }
01176 
01177         $myConfig = $this->getConfig();
01178         if ( $sPassword ) {
01179 
01180             $sShopID = $myConfig->getShopId();
01181             $oDb = oxDb::getDb();
01182 
01183             $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01184             $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01185             $sShopSelect = "";
01186 
01187 
01188             // admin view: can only login with higher than 'user' rights
01189             if ( $this->isAdmin() ) {
01190                 $sShopSelect = " and ( oxrights != 'user' ) ";
01191             }
01192 
01193             $sWhat = "oxid";
01194 
01195             $sSelect =  "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01196             if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
01197                 if ( $sPassword == "admin" && $sUser == "admin" ) {
01198                     $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01199                 } else {
01200                     $oEx = oxNew( 'oxUserException' );
01201                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01202                     throw $oEx;
01203                 }
01204             }
01205 
01206             // load from DB
01207             $aData = $oDb->getAll( $sSelect );
01208             $sOXID = @$aData[0][0];
01209             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01210 
01211                 if ( !$this->load( $sOXID ) ) {
01212                     $oEx = oxNew( 'oxUserException' );
01213                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01214                     throw $oEx;
01215                 }
01216             }
01217         }
01218 
01219 
01220         //login successfull?
01221         if ( $this->oxuser__oxid->value ) {
01222             // yes, successful login
01223             if ( $this->isAdmin() ) {
01224                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01225             } else {
01226                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01227             }
01228 
01229             // cookie must be set ?
01230             if ( $blCookie ) {
01231                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01232             }
01233 
01234             //load basket from the database
01235             try {
01236                 if ($oBasket = $this->getSession()->getBasket()) {
01237                     $oBasket->load();
01238                 }
01239 
01240             } catch (Exception $oE) {
01241                 //just ignore it
01242             }
01243 
01244             return true;
01245         } else {
01246             $oEx = oxNew( 'oxUserException' );
01247             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01248             throw $oEx;
01249         }
01250     }
01251 
01262     public function openIdLogin( $sUser )
01263     {
01264         $myConfig = $this->getConfig();
01265         $sShopID = $myConfig->getShopId();
01266         $oDb = oxDb::getDb();
01267 
01268         $sUserSelect = "oxuser.oxusername = " . $oDb->quote( $sUser );
01269         $sShopSelect = "";
01270 
01271 
01272         $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01273 
01274         // load from DB
01275         $aData = $oDb->getAll( $sSelect );
01276         $sOXID = @$aData[0][0];
01277         if ( isset( $sOXID ) && $sOXID ) {
01278 
01279             if ( !$this->load( $sOXID ) ) {
01280                 $oEx = oxNew( 'oxUserException' );
01281                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01282                 throw $oEx;
01283             }
01284         }
01285 
01286         //login successfull?
01287         if ( $this->oxuser__oxid->value ) {
01288             // yes, successful login
01289             oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01290             return true;
01291         } else {
01292             $oEx = oxNew( 'oxUserException' );
01293             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01294             throw $oEx;
01295         }
01296     }
01297 
01303     public function logout()
01304     {
01305         // deleting session info
01306         oxSession::deleteVar( 'usr' );  // for front end
01307         oxSession::deleteVar( 'auth' ); // for back end
01308         oxSession::deleteVar( 'dgr' );
01309         oxSession::deleteVar( 'dynvalue' );
01310         oxSession::deleteVar( 'paymentid' );
01311         // oxSession::deleteVar( 'deladrid' );
01312 
01313         // delete cookie
01314         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01315 
01316         // unsetting global user
01317         $this->setUser( null );
01318 
01319         return true;
01320     }
01321 
01328     public function loadAdminUser()
01329     {
01330         return $this->loadActiveUser( true );
01331     }
01332 
01341     public function loadActiveUser( $blForceAdmin = false )
01342     {
01343         $myConfig = $this->getConfig();
01344 
01345         $blAdmin = $this->isAdmin() || $blForceAdmin;
01346         $oDB = oxDb::getDb();
01347 
01348         // first - checking session info
01349         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01350         $blFoundInCookie = false;
01351 
01352         //trying automatic login (by 'remember me' cookie)
01353         if ( !$sUserID && !$blAdmin ) {
01354             $sShopID = $myConfig->getShopId();
01355             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01356                 $aData = explode( '@@@', $sSet );
01357                 $sUser = $aData[0];
01358                 $sPWD  = @$aData[1];
01359 
01360                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDB->quote($sUser);
01361 
01362 
01363                 $oDB = oxDb::getDb();
01364                 $rs = $oDB->execute( $sSelect );
01365                 if ( $rs != false && $rs->recordCount() > 0 ) {
01366                     while (!$rs->EOF) {
01367                         $sTest = crypt( $rs->fields[1], 'ox' );
01368                         if ( $sTest == $sPWD ) {
01369                             // found
01370                             $sUserID = $rs->fields[0];
01371                             $blFoundInCookie = true;
01372                             break;
01373                         }
01374                         $rs->moveNext();
01375                     }
01376                 }
01377             }
01378         }
01379 
01380         // Checking if user is connected via Facebook connect.
01381         // If yes, trying to login user using user Facebook ID
01382         if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01383             $oFb = oxFb::getInstance();
01384             if ( $oFb->isConnected() && $oFb->getUser() ) {
01385                 $sUserSelect = "oxuser.oxfbid = " . $oDB->quote( $oFb->getUser() );
01386                 $sShopSelect = "";
01387 
01388 
01389                 $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01390                 $sUserID = $oDB->getOne( $sSelect );
01391             }
01392         }
01393 
01394         // checking user results
01395         if ( $sUserID ) {
01396             if ( $this->load( $sUserID ) ) {
01397                 // storing into session
01398                 if ($blAdmin) {
01399                     oxSession::setVar( 'auth', $sUserID );
01400                 } else {
01401                     oxSession::setVar( 'usr', $sUserID );
01402                 }
01403 
01404                 // marking the way user was loaded
01405                 $this->_blLoadedFromCookie = $blFoundInCookie;
01406                 return true;
01407             }
01408         } else {
01409             // no user
01410             if ($blAdmin) {
01411                 oxSession::deleteVar( 'auth' );
01412             } else {
01413                 oxSession::deleteVar( 'usr' );
01414             }
01415 
01416             return false;
01417         }
01418     }
01419 
01432     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01433     {
01434         include "oxldap.php";
01435         $myConfig = $this->getConfig();
01436         $oDb = oxDb::getDb();
01437         //$throws oxConnectionException
01438         $aLDAPParams = $myConfig->getConfigParam( 'aLDAPParams' );
01439         $oLDAP = new oxLDAP( $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01440         // maybe this is LDAP user but supplied email Address instead of LDAP login
01441         $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote($sUser)." $sShopSelect");
01442         if ( isset( $sLDAPKey) && $sLDAPKey) {
01443             $sUser = $sLDAPKey;
01444         }
01445 
01446         //$throws oxConnectionException
01447         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01448 
01449         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01450         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01451             // login successful
01452 
01453             // check if user is already in database
01454             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote($aData['OXUSERNAME'])." $sShopSelect";
01455             $sOXID = $oDb->getOne( $sSelect);
01456 
01457             if ( !isset( $sOXID) || !$sOXID) {
01458                 // we need to create a new user
01459                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01460                 $this->setId();
01461 
01462                 // map all user data fields
01463                 foreach ( $aData as $fldname => $value) {
01464                     $sField = "oxuser__".strtolower( $fldname);
01465                     $this->$sField->setValue($aData[$fldname]);
01466                 }
01467 
01468                 $this->oxuser__oxactive->setValue(1);
01469                 $this->oxuser__oxshopid->setValue($sShopID);
01470                 $this->oxuser__oxldapkey->setValue($sUser);
01471                 $this->oxuser__oxrights->setValue("user");
01472                 $this->setPassword( "ldap user" );
01473 
01474                 $this->save();
01475             } else {
01476                 // LDAP user is already in OXID DB, load it
01477                 $this->load( $sOXID);
01478             }
01479 
01480         } else {
01481             $oEx = oxNew( 'oxUserException' );
01482             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01483             throw $oEx;
01484         }
01485     }
01486 
01493     protected function _getUserRights()
01494     {
01495         // previously user had no rights defined
01496         if ( !$this->oxuser__oxrights->value )
01497             return 'user';
01498 
01499         $oDB = oxDb::getDb();
01500         $myConfig    = $this->getConfig();
01501         $sAuthRights = null;
01502 
01503         // choosing possible user rights index
01504         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01505         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01506         if ( $sAuthUserID ) {
01507             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01508         }
01509 
01510         //preventing user rights edit for non admin
01511         $aRights = array();
01512 
01513         // selecting current users rights ...
01514         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01515             $aRights[] = $sCurrRights;
01516         }
01517         $aRights[] = 'user';
01518 
01519         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01520             return current( $aRights );
01521         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01522             $aRights[] = $sAuthRights;
01523             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01524                 return current( $aRights );
01525             }
01526         }
01527 
01528         // leaving as it was set ...
01529         return $this->oxuser__oxrights->value;
01530     }
01531 
01541     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01542     {
01543 
01544         /*if ( !$myConfig->blMallUsers ) {
01545             $sShopID = $myConfig->getShopId();
01546             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01547         }*/
01548 
01549         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01550     }
01551 
01557     protected function _insert()
01558     {
01559 
01560         // set oxcreate date
01561         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01562 
01563         if ( !isset( $this->oxuser__oxboni->value ) ) {
01564             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01565         }
01566 
01567         return parent::_insert();
01568     }
01569 
01575     protected function _update()
01576     {
01577         //V #M418: for not registered users, don't change boni during update
01578         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01579             $this->_aSkipSaveFields[] = 'oxboni';
01580         }
01581 
01582         // don't change this field
01583         $this->_aSkipSaveFields[] = 'oxcreate';
01584         if ( !$this->isAdmin() ) {
01585             $this->_aSkipSaveFields[] = 'oxcustnr';
01586             $this->_aSkipSaveFields[] = 'oxrights';
01587         }
01588 
01589         // updating subscription information
01590         if ( ( $blUpdate = parent::_update() ) ) {
01591             $this->getNewsSubscription()->updateSubscription( $this );
01592         }
01593 
01594         return $blUpdate;
01595     }
01596 
01612     protected function _checkLogin( $sLogin, $aInvAddress )
01613     {
01614         $myConfig = $this->getConfig();
01615 
01616         $sLogin   = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername']:$sLogin;
01617 
01618         // check only for users with password during registration
01619         // if user wants to change user name - we must check if passwords are ok before changing
01620         if ( $this->oxuser__oxpassword->value && $sLogin != $this->oxuser__oxusername->value ) {
01621 
01622             // on this case password must be taken directly from request
01623             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
01624             if ( !$sNewPass ) {
01625 
01626                 // 1. user forgot to enter password
01627                 $oEx = oxNew( 'oxInputException' );
01628                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01629                 throw $oEx;
01630             } else {
01631 
01632                 // 2. entered wrong password
01633                 if ( !$this->isSamePassword( $sNewPass ) ) {
01634                     $oEx = oxNew( 'oxUserException' );
01635                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01636                     throw $oEx;
01637                 }
01638             }
01639         }
01640 
01641         if ( $this->checkIfEmailExists( $sLogin ) ) {
01642             //if exists then we do now allow to do that
01643             $oEx = oxNew( 'oxUserException' );
01644             $oLang = oxLang::getInstance();
01645             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
01646             throw $oEx;
01647         }
01648 
01649         return $sLogin;
01650     }
01651 
01659     public function checkIfEmailExists( $sEmail )
01660     {
01661         $myConfig = $this->getConfig();
01662         $oDB = oxDb::getDb();
01663         $iShopId = $myConfig->getShopId();
01664         $blExists = false;
01665 
01666         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01667         if ( ( $sOxid = $this->getId() ) ) {
01668             $sQ .= " and oxid <> '$sOxid' ";
01669         }
01670 
01671         $oRs = $oDB->execute( $sQ );
01672         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01673 
01674             if ( $this->_blMallUsers ) {
01675 
01676                 $blExists = true;
01677                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01678 
01679                     // password is not set - allow to override
01680                     $blExists = false;
01681                 }
01682             } else {
01683 
01684                 $blExists = false;
01685                 while ( !$oRs->EOF ) {
01686                     if ( $oRs->fields[1] != 'user' ) {
01687 
01688                         // exists admin with same login - must not allow
01689                         $blExists = true;
01690                         break;
01691                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01692 
01693                         // exists same login (with password) in same shop
01694                         $blExists = true;
01695                         break;
01696                     }
01697 
01698                     $oRs->moveNext();
01699                 }
01700             }
01701         }
01702         return $blExists;
01703     }
01704 
01712     public function getUserRecommLists( $sOXID = null )
01713     {
01714         if ( !$sOXID )
01715             $sOXID = $this->getId();
01716 
01717         // sets active page
01718         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01719         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01720 
01721         // load only lists which we show on screen
01722         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01723         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01724 
01725 
01726         $oRecommList = oxNew( 'oxlist' );
01727         $oRecommList->init( 'oxrecommlist' );
01728         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01729         $iShopId = $this->getConfig()->getShopId();
01730         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01731         $oRecommList->selectString( $sSelect );
01732 
01733         return $oRecommList;
01734     }
01735 
01743     public function getRecommListsCount( $sOx = null )
01744     {
01745         if ( !$sOx ) {
01746             $sOx = $this->getId();
01747         }
01748 
01749         if ( $this->_iCntRecommLists === null || $sOx ) {
01750             $oDb = oxDb::getDb();
01751             $this->_iCntRecommLists = 0;
01752             $iShopId = $this->getConfig()->getShopId();
01753             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01754             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01755         }
01756         return $this->_iCntRecommLists;
01757     }
01758 
01767     protected function _checkEmail( $sEmail )
01768     {
01769         // missing email address (user login name) ?
01770         if ( !$sEmail ) {
01771             $oEx = oxNew( 'oxInputException' );
01772             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01773             throw $oEx;
01774         }
01775 
01776         // invalid email address ?
01777         if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
01778             $oEx = oxNew( 'oxInputException' );
01779             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
01780             throw $oEx;
01781         }
01782     }
01783 
01798     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01799     {
01800         $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01801     }
01802 
01815     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01816     {
01817         //  no password at all
01818         if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
01819             $oEx = oxNew( 'oxInputException' );
01820             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
01821             throw $oEx;
01822         }
01823 
01824         //  password is too short ?
01825         if ( $blCheckLenght &&  getStr()->strlen( $sNewPass ) < 6 ) {
01826             $oEx = oxNew( 'oxInputException' );
01827             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
01828             throw $oEx;
01829         }
01830 
01831         //  passwords do not match ?
01832         if ( $sNewPass != $sConfPass ) {
01833             $oEx = oxNew( 'oxUserException' );
01834             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01835             throw $oEx;
01836         }
01837     }
01838 
01849     protected function _checkCountries( $aInvAddress, $aDelAddress )
01850     {
01851         $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
01852         $sDelCtry  = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
01853 
01854         if ( $sBillCtry || $sDelCtry ) {
01855             $oDb = oxDb::getDb();
01856 
01857             if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
01858                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
01859                 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
01860             } else {
01861                 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
01862                               ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
01863             }
01864 
01865             if ( !$oDb->getOne( $sQ ) ) {
01866                 $oEx = oxNew( 'oxUserException' );
01867                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS' );
01868                 throw $oEx;
01869             }
01870         }
01871     }
01872 
01884     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01885     {
01886         // collecting info about required fields
01887         $aMustFields = array( 'oxuser__oxfname',
01888                               'oxuser__oxlname',
01889                               'oxuser__oxstreetnr',
01890                               'oxuser__oxstreet',
01891                               'oxuser__oxzip',
01892                               'oxuser__oxcity' );
01893 
01894         // config shoud override default fields
01895         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
01896         if ( is_array( $aMustFillFields ) ) {
01897             $aMustFields = $aMustFillFields;
01898         }
01899 
01900         // assuring data to check
01901         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
01902         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
01903 
01904         // collecting fields
01905         $aFields = array_merge( $aInvAddress, $aDelAddress );
01906 
01907 
01908         // check delivery address ?
01909         $blCheckDel = false;
01910         if ( count( $aDelAddress ) ) {
01911             $blCheckDel = true;
01912         }
01913 
01914         // checking
01915         foreach ( $aMustFields as $sMustField ) {
01916 
01917             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
01918             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
01919                 continue;
01920             }
01921 
01922             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
01923                 $this->_checkRequiredArrayFields( $sMustField, $aFields[$sMustField] );
01924             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
01925                    $oEx = oxNew( 'oxInputException' );
01926                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01927                    throw $oEx;
01928             }
01929         }
01930     }
01931 
01942     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01943     {
01944         foreach ( $aFieldValues as $sValue ) {
01945             if ( !trim( $sValue ) ) {
01946                 $oEx = oxNew( 'oxInputException' );
01947                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01948                 throw $oEx;
01949             }
01950         }
01951     }
01952 
01963     protected function _checkVatId( $aInvAddress )
01964     {
01965         if ( $aInvAddress['oxuser__oxustid'] ) {
01966 
01967             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
01968                 // no country
01969                 return;
01970             }
01971             $oCountry = oxNew('oxcountry');
01972             if (!$oCountry->load($sCountryId)) {
01973                 throw new oxObjectException();
01974             }
01975             if ($oCountry->isForeignCountry() && $oCountry->isInEU()) {
01976                     if (strncmp($aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2)) {
01977                         $oEx = oxNew( 'oxInputException' );
01978                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
01979                         throw $oEx;
01980                     }
01981             }
01982         }
01983     }
01984 
01993     protected function _setAutoGroups( $sCountryId )
01994     {
01995         // assigning automatically to specific groups
01996         $blForeigner = true;
01997         $blForeignGroupExists = false;
01998         $blInlandGroupExists = false;
01999 
02000         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
02001         // foreigner ?
02002         if ( is_array($aHomeCountry)) {
02003             if (in_array($sCountryId, $aHomeCountry)) {
02004                 $blForeigner = false;
02005             }
02006         } elseif ($sCountryId == $aHomeCountry) {
02007             $blForeigner = false;
02008         }
02009 
02010         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
02011             $blForeignGroupExists = true;
02012             if ( !$blForeigner ) {
02013                 $this->removeFromGroup( 'oxidforeigncustomer' );
02014             }
02015         }
02016 
02017         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
02018             $blInlandGroupExists = true;
02019             if ( $blForeigner ) {
02020                 $this->removeFromGroup( 'oxidnewcustomer' );
02021             }
02022         }
02023 
02024         if ( !$this->oxuser__oxdisableautogrp->value ) {
02025             if ( !$blForeignGroupExists && $blForeigner ) {
02026                 $this->addToGroup( 'oxidforeigncustomer' );
02027             }
02028             if ( !$blInlandGroupExists && !$blForeigner ) {
02029                 $this->addToGroup( 'oxidnewcustomer' );
02030             }
02031         }
02032     }
02033 
02041     protected function _hasUserAddress( $sUserId )
02042     {
02043         $oAddresses = $this->getUserAddresses();
02044         if ( $oAddresses && count($oAddresses)>0 ) {
02045             $oAddresses->rewind() ;
02046             foreach ($oAddresses as $key => $oAddress) {
02047                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
02048                     return true;
02049                 }
02050             }
02051         }
02052         return false;
02053     }
02054 
02067     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
02068     {
02069         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
02070     }
02071 
02081     protected function _deleteUserCookie( $sShopId = null )
02082     {
02083         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
02084     }
02085 
02095     protected static function _getUserCookie( $sShopId = null )
02096     {
02097         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
02098     }
02099 
02100 
02109     public function loadUserByUpdateId( $sUid )
02110     {
02111         $oDb = oxDb::getDb();
02112         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02113         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02114             return $this->load( $sUserId );
02115         }
02116     }
02117 
02125     public function setUpdateKey( $blReset = false )
02126     {
02127         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02128         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02129 
02130         // generating key
02131         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02132 
02133         // setting expiration time for 6 hours
02134         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02135 
02136         // saving
02137         $this->save();
02138     }
02139 
02145     public function getUpdateLinkTerm()
02146     {
02147         return 3600 * 6;
02148     }
02149 
02157     public function isExpiredUpdateId( $sKey )
02158     {
02159         $oDb = oxDb::getDb();
02160         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02161         return !( (bool) $oDb->getOne( $sQ ) );
02162     }
02163 
02169     public function getUpdateId()
02170     {
02171         if ( $this->_sUpdateKey === null ) {
02172             $this->setUpdateKey();
02173             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02174         }
02175         return $this->_sUpdateKey;
02176     }
02177 
02186     public function encodePassword( $sPassword, $sSalt )
02187     {
02188         $oDb = oxDb::getDb();
02189         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02190     }
02191 
02199     public function prepareSalt( $sSalt )
02200     {
02201         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02202     }
02203 
02211     public function decodeSalt( $sSaltHex )
02212     {
02213         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02214     }
02215 
02223     public function setPassword( $sPassword = null )
02224     {
02225         // setting salt if password is not empty
02226         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02227 
02228         // encoding only if password was not empty (e.g. user registration without pass)
02229         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02230 
02231         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02232         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02233     }
02234 
02242     public function isSamePassword( $sNewPass )
02243     {
02244         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02245     }
02246 
02252     public function isLoadedFromCookie()
02253     {
02254         return $this->_blLoadedFromCookie;
02255     }
02256 
02263     public function getPasswordHash()
02264     {
02265         $sHash = null;
02266         if ( $this->oxuser__oxpassword->value ) {
02267             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02268                 // decodable pass ?
02269                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02270             } elseif ( ( strlen( $this->oxuser__oxpassword->value ) < 32 ) && ( strpos( $this->oxuser__oxpassword->value, 'openid_' ) !== 0 ) ) {
02271                 // plain pass ?
02272                 $this->setPassword( $this->oxuser__oxpassword->value );
02273             }
02274             $sHash = $this->oxuser__oxpassword->value;
02275         }
02276         return $sHash;
02277     }
02278 
02287     public static function getAdminUser()
02288     {
02289         return self::getActiveUser( true );
02290     }
02291 
02302     public static function getActiveUser( $blForceAdmin = false )
02303     {
02304         $oUser = oxNew( 'oxuser' );
02305         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02306             return $oUser;
02307         } else {
02308             return false;
02309         }
02310     }
02311 
02319     public function getOpenIdPassword( $iLength = 25 )
02320     {
02321         $sPassword= "openid_".substr( oxUtilsObject::getInstance()->generateUId(), 0, $iLength);
02322         return $sPassword;
02323     }
02324 
02332     public function getReviewUserHash( $sUserId )
02333     {
02334         $oDb = oxDb::getDb();
02335         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02336         return $sReviewUserHash;
02337     }
02338 
02346     public function getReviewUserId( $sReviewUserHash )
02347     {
02348         $oDb = oxDb::getDb();
02349         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02350         return $sUserId;
02351     }
02352 
02358     public function getState()
02359     {
02360         return $this->oxuser__oxstateid->value;
02361     }
02362 
02368     public function isTermsAccepted()
02369     {
02370         $sShopId = $this->getConfig()->getShopId();
02371         $sUserId = $this->getId();
02372         return (bool) oxDb::getDb()->getOne( "select 1 from oxacceptedterms where oxuserid='{$sUserId}' and oxshopid='{$sShopId}'" );
02373     }
02374 
02380     public function acceptTerms()
02381     {
02382         $sUserId  = $this->getId();
02383         $sShopId  = $this->getConfig()->getShopId();
02384         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02385 
02386         oxDb::getDb()->execute( "replace oxacceptedterms set oxuserid='{$sUserId}', oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02387     }
02388 
02397     public function setCreditPointsForRegistrant( $sUserId )
02398     {
02399         $blSet   = false;
02400         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02401         if ( $iPoints ) {
02402             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02403             if ( $blSet = $this->save() ) {
02404                 $oDb = oxDb::getDb();
02405 
02406                 // updating users statistics
02407                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ". $oDb->quote( $sUserId ) );
02408 
02409                 $oInvUser = oxNew( "oxuser" );
02410                 if ( $oInvUser->load( $sUserId ) ) {
02411                     $blSet = $oInvUser->setCreditPointsForInviter();
02412                 }
02413             }
02414 
02415             oxSession::deleteVar( 'su' );
02416         }
02417 
02418         return $blSet;
02419     }
02420 
02426     public function setCreditPointsForInviter()
02427     {
02428         $blSet   = false;
02429         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02430         if ( $iPoints ) {
02431             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02432             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02433             $blSet = $this->save();
02434         }
02435 
02436         return $blSet;
02437     }
02438 
02444     public function updateFbId()
02445     {
02446         $oFb = oxFb::getInstance();
02447         $blRet = false;
02448 
02449         if ( $oFb->isConnected() && $oFb->getUser() ) {
02450              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02451              $blRet = $this->save();
02452         }
02453 
02454         return $blRet;
02455     }
02456 }