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         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01435         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01436 
01437         // maybe this is LDAP user but supplied email Address instead of LDAP login
01438         $oDb = oxDb::getDb();
01439         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01440         if ( isset( $sLDAPKey) && $sLDAPKey) {
01441             $sUser = $sLDAPKey;
01442         }
01443 
01444         //$throws oxConnectionException
01445         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01446 
01447         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01448         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01449             // login successful
01450 
01451             // check if user is already in database
01452             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01453             $sOXID = $oDb->getOne( $sSelect );
01454 
01455             if ( !isset( $sOXID ) || !$sOXID ) {
01456                 // we need to create a new user
01457                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01458                 $this->setId();
01459 
01460                 // map all user data fields
01461                 foreach ( $aData as $fldname => $value) {
01462                     $sField = "oxuser__".strtolower( $fldname);
01463                     $this->$sField = new oxField( $aData[$fldname] );
01464                 }
01465 
01466                 $this->oxuser__oxactive  = new oxField( 1 );
01467                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01468                 $this->oxuser__oxldapkey = new oxField( $sUser );
01469                 $this->oxuser__oxrights  = new oxField( "user" );
01470                 $this->setPassword( "ldap user" );
01471 
01472                 $this->save();
01473             } else {
01474                 // LDAP user is already in OXID DB, load it
01475                 $this->load( $sOXID);
01476             }
01477 
01478         } else {
01479             $oEx = oxNew( 'oxUserException' );
01480             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01481             throw $oEx;
01482         }
01483     }
01484 
01491     protected function _getUserRights()
01492     {
01493         // previously user had no rights defined
01494         if ( !$this->oxuser__oxrights->value )
01495             return 'user';
01496 
01497         $oDB = oxDb::getDb();
01498         $myConfig    = $this->getConfig();
01499         $sAuthRights = null;
01500 
01501         // choosing possible user rights index
01502         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01503         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01504         if ( $sAuthUserID ) {
01505             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01506         }
01507 
01508         //preventing user rights edit for non admin
01509         $aRights = array();
01510 
01511         // selecting current users rights ...
01512         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01513             $aRights[] = $sCurrRights;
01514         }
01515         $aRights[] = 'user';
01516 
01517         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01518             return current( $aRights );
01519         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01520             $aRights[] = $sAuthRights;
01521             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01522                 return current( $aRights );
01523             }
01524         }
01525 
01526         // leaving as it was set ...
01527         return $this->oxuser__oxrights->value;
01528     }
01529 
01539     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01540     {
01541 
01542         /*if ( !$myConfig->blMallUsers ) {
01543             $sShopID = $myConfig->getShopId();
01544             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01545         }*/
01546 
01547         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01548     }
01549 
01555     protected function _insert()
01556     {
01557 
01558         // set oxcreate date
01559         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01560 
01561         if ( !isset( $this->oxuser__oxboni->value ) ) {
01562             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01563         }
01564 
01565         return parent::_insert();
01566     }
01567 
01573     protected function _update()
01574     {
01575         //V #M418: for not registered users, don't change boni during update
01576         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01577             $this->_aSkipSaveFields[] = 'oxboni';
01578         }
01579 
01580         // don't change this field
01581         $this->_aSkipSaveFields[] = 'oxcreate';
01582         if ( !$this->isAdmin() ) {
01583             $this->_aSkipSaveFields[] = 'oxcustnr';
01584             $this->_aSkipSaveFields[] = 'oxrights';
01585         }
01586 
01587         // updating subscription information
01588         if ( ( $blUpdate = parent::_update() ) ) {
01589             $this->getNewsSubscription()->updateSubscription( $this );
01590         }
01591 
01592         return $blUpdate;
01593     }
01594 
01610     protected function _checkLogin( $sLogin, $aInvAddress )
01611     {
01612         $myConfig = $this->getConfig();
01613 
01614         $sLogin   = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername']:$sLogin;
01615 
01616         // check only for users with password during registration
01617         // if user wants to change user name - we must check if passwords are ok before changing
01618         if ( $this->oxuser__oxpassword->value && $sLogin != $this->oxuser__oxusername->value ) {
01619 
01620             // on this case password must be taken directly from request
01621             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
01622             if ( !$sNewPass ) {
01623 
01624                 // 1. user forgot to enter password
01625                 $oEx = oxNew( 'oxInputException' );
01626                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01627                 throw $oEx;
01628             } else {
01629 
01630                 // 2. entered wrong password
01631                 if ( !$this->isSamePassword( $sNewPass ) ) {
01632                     $oEx = oxNew( 'oxUserException' );
01633                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01634                     throw $oEx;
01635                 }
01636             }
01637         }
01638 
01639         if ( $this->checkIfEmailExists( $sLogin ) ) {
01640             //if exists then we do now allow to do that
01641             $oEx = oxNew( 'oxUserException' );
01642             $oLang = oxLang::getInstance();
01643             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
01644             throw $oEx;
01645         }
01646 
01647         return $sLogin;
01648     }
01649 
01657     public function checkIfEmailExists( $sEmail )
01658     {
01659         $myConfig = $this->getConfig();
01660         $oDB = oxDb::getDb();
01661         $iShopId = $myConfig->getShopId();
01662         $blExists = false;
01663 
01664         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01665         if ( ( $sOxid = $this->getId() ) ) {
01666             $sQ .= " and oxid <> '$sOxid' ";
01667         }
01668 
01669         $oRs = $oDB->execute( $sQ );
01670         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01671 
01672             if ( $this->_blMallUsers ) {
01673 
01674                 $blExists = true;
01675                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01676 
01677                     // password is not set - allow to override
01678                     $blExists = false;
01679                 }
01680             } else {
01681 
01682                 $blExists = false;
01683                 while ( !$oRs->EOF ) {
01684                     if ( $oRs->fields[1] != 'user' ) {
01685 
01686                         // exists admin with same login - must not allow
01687                         $blExists = true;
01688                         break;
01689                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01690 
01691                         // exists same login (with password) in same shop
01692                         $blExists = true;
01693                         break;
01694                     }
01695 
01696                     $oRs->moveNext();
01697                 }
01698             }
01699         }
01700         return $blExists;
01701     }
01702 
01710     public function getUserRecommLists( $sOXID = null )
01711     {
01712         if ( !$sOXID )
01713             $sOXID = $this->getId();
01714 
01715         // sets active page
01716         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01717         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01718 
01719         // load only lists which we show on screen
01720         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01721         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01722 
01723 
01724         $oRecommList = oxNew( 'oxlist' );
01725         $oRecommList->init( 'oxrecommlist' );
01726         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01727         $iShopId = $this->getConfig()->getShopId();
01728         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01729         $oRecommList->selectString( $sSelect );
01730 
01731         return $oRecommList;
01732     }
01733 
01741     public function getRecommListsCount( $sOx = null )
01742     {
01743         if ( !$sOx ) {
01744             $sOx = $this->getId();
01745         }
01746 
01747         if ( $this->_iCntRecommLists === null || $sOx ) {
01748             $oDb = oxDb::getDb();
01749             $this->_iCntRecommLists = 0;
01750             $iShopId = $this->getConfig()->getShopId();
01751             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01752             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01753         }
01754         return $this->_iCntRecommLists;
01755     }
01756 
01765     protected function _checkEmail( $sEmail )
01766     {
01767         // missing email address (user login name) ?
01768         if ( !$sEmail ) {
01769             $oEx = oxNew( 'oxInputException' );
01770             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01771             throw $oEx;
01772         }
01773 
01774         // invalid email address ?
01775         if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
01776             $oEx = oxNew( 'oxInputException' );
01777             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
01778             throw $oEx;
01779         }
01780     }
01781 
01796     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01797     {
01798         $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01799     }
01800 
01813     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01814     {
01815         //  no password at all
01816         if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
01817             $oEx = oxNew( 'oxInputException' );
01818             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
01819             throw $oEx;
01820         }
01821 
01822         //  password is too short ?
01823         if ( $blCheckLenght &&  getStr()->strlen( $sNewPass ) < 6 ) {
01824             $oEx = oxNew( 'oxInputException' );
01825             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
01826             throw $oEx;
01827         }
01828 
01829         //  passwords do not match ?
01830         if ( $sNewPass != $sConfPass ) {
01831             $oEx = oxNew( 'oxUserException' );
01832             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01833             throw $oEx;
01834         }
01835     }
01836 
01847     protected function _checkCountries( $aInvAddress, $aDelAddress )
01848     {
01849         $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
01850         $sDelCtry  = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
01851 
01852         if ( $sBillCtry || $sDelCtry ) {
01853             $oDb = oxDb::getDb();
01854 
01855             if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
01856                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
01857                 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
01858             } else {
01859                 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
01860                               ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
01861             }
01862 
01863             if ( !$oDb->getOne( $sQ ) ) {
01864                 $oEx = oxNew( 'oxUserException' );
01865                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS' );
01866                 throw $oEx;
01867             }
01868         }
01869     }
01870 
01882     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01883     {
01884         // collecting info about required fields
01885         $aMustFields = array( 'oxuser__oxfname',
01886                               'oxuser__oxlname',
01887                               'oxuser__oxstreetnr',
01888                               'oxuser__oxstreet',
01889                               'oxuser__oxzip',
01890                               'oxuser__oxcity' );
01891 
01892         // config shoud override default fields
01893         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
01894         if ( is_array( $aMustFillFields ) ) {
01895             $aMustFields = $aMustFillFields;
01896         }
01897 
01898         // assuring data to check
01899         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
01900         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
01901 
01902         // collecting fields
01903         $aFields = array_merge( $aInvAddress, $aDelAddress );
01904 
01905 
01906         // check delivery address ?
01907         $blCheckDel = false;
01908         if ( count( $aDelAddress ) ) {
01909             $blCheckDel = true;
01910         }
01911 
01912         // checking
01913         foreach ( $aMustFields as $sMustField ) {
01914 
01915             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
01916             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
01917                 continue;
01918             }
01919 
01920             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
01921                 $this->_checkRequiredArrayFields( $sMustField, $aFields[$sMustField] );
01922             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
01923                    $oEx = oxNew( 'oxInputException' );
01924                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01925                    throw $oEx;
01926             }
01927         }
01928     }
01929 
01940     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01941     {
01942         foreach ( $aFieldValues as $sValue ) {
01943             if ( !trim( $sValue ) ) {
01944                 $oEx = oxNew( 'oxInputException' );
01945                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01946                 throw $oEx;
01947             }
01948         }
01949     }
01950 
01961     protected function _checkVatId( $aInvAddress )
01962     {
01963         if ( $aInvAddress['oxuser__oxustid'] ) {
01964 
01965             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
01966                 // no country
01967                 return;
01968             }
01969             $oCountry = oxNew('oxcountry');
01970             if (!$oCountry->load($sCountryId)) {
01971                 throw new oxObjectException();
01972             }
01973             if ($oCountry->isForeignCountry() && $oCountry->isInEU()) {
01974                     if (strncmp($aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2)) {
01975                         $oEx = oxNew( 'oxInputException' );
01976                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
01977                         throw $oEx;
01978                     }
01979             }
01980         }
01981     }
01982 
01991     protected function _setAutoGroups( $sCountryId )
01992     {
01993         // assigning automatically to specific groups
01994         $blForeigner = true;
01995         $blForeignGroupExists = false;
01996         $blInlandGroupExists = false;
01997 
01998         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01999         // foreigner ?
02000         if ( is_array($aHomeCountry)) {
02001             if (in_array($sCountryId, $aHomeCountry)) {
02002                 $blForeigner = false;
02003             }
02004         } elseif ($sCountryId == $aHomeCountry) {
02005             $blForeigner = false;
02006         }
02007 
02008         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
02009             $blForeignGroupExists = true;
02010             if ( !$blForeigner ) {
02011                 $this->removeFromGroup( 'oxidforeigncustomer' );
02012             }
02013         }
02014 
02015         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
02016             $blInlandGroupExists = true;
02017             if ( $blForeigner ) {
02018                 $this->removeFromGroup( 'oxidnewcustomer' );
02019             }
02020         }
02021 
02022         if ( !$this->oxuser__oxdisableautogrp->value ) {
02023             if ( !$blForeignGroupExists && $blForeigner ) {
02024                 $this->addToGroup( 'oxidforeigncustomer' );
02025             }
02026             if ( !$blInlandGroupExists && !$blForeigner ) {
02027                 $this->addToGroup( 'oxidnewcustomer' );
02028             }
02029         }
02030     }
02031 
02039     protected function _hasUserAddress( $sUserId )
02040     {
02041         $oAddresses = $this->getUserAddresses();
02042         if ( $oAddresses && count($oAddresses)>0 ) {
02043             $oAddresses->rewind() ;
02044             foreach ($oAddresses as $key => $oAddress) {
02045                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
02046                     return true;
02047                 }
02048             }
02049         }
02050         return false;
02051     }
02052 
02065     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
02066     {
02067         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
02068     }
02069 
02079     protected function _deleteUserCookie( $sShopId = null )
02080     {
02081         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
02082     }
02083 
02093     protected static function _getUserCookie( $sShopId = null )
02094     {
02095         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
02096     }
02097 
02098 
02107     public function loadUserByUpdateId( $sUid )
02108     {
02109         $oDb = oxDb::getDb();
02110         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02111         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02112             return $this->load( $sUserId );
02113         }
02114     }
02115 
02123     public function setUpdateKey( $blReset = false )
02124     {
02125         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02126         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02127 
02128         // generating key
02129         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02130 
02131         // setting expiration time for 6 hours
02132         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02133 
02134         // saving
02135         $this->save();
02136     }
02137 
02143     public function getUpdateLinkTerm()
02144     {
02145         return 3600 * 6;
02146     }
02147 
02155     public function isExpiredUpdateId( $sKey )
02156     {
02157         $oDb = oxDb::getDb();
02158         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02159         return !( (bool) $oDb->getOne( $sQ ) );
02160     }
02161 
02167     public function getUpdateId()
02168     {
02169         if ( $this->_sUpdateKey === null ) {
02170             $this->setUpdateKey();
02171             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02172         }
02173         return $this->_sUpdateKey;
02174     }
02175 
02184     public function encodePassword( $sPassword, $sSalt )
02185     {
02186         $oDb = oxDb::getDb();
02187         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02188     }
02189 
02197     public function prepareSalt( $sSalt )
02198     {
02199         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02200     }
02201 
02209     public function decodeSalt( $sSaltHex )
02210     {
02211         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02212     }
02213 
02221     public function setPassword( $sPassword = null )
02222     {
02223         // setting salt if password is not empty
02224         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02225 
02226         // encoding only if password was not empty (e.g. user registration without pass)
02227         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02228 
02229         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02230         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02231     }
02232 
02240     public function isSamePassword( $sNewPass )
02241     {
02242         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02243     }
02244 
02250     public function isLoadedFromCookie()
02251     {
02252         return $this->_blLoadedFromCookie;
02253     }
02254 
02261     public function getPasswordHash()
02262     {
02263         $sHash = null;
02264         if ( $this->oxuser__oxpassword->value ) {
02265             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02266                 // decodable pass ?
02267                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02268             } elseif ( ( strlen( $this->oxuser__oxpassword->value ) < 32 ) && ( strpos( $this->oxuser__oxpassword->value, 'openid_' ) !== 0 ) ) {
02269                 // plain pass ?
02270                 $this->setPassword( $this->oxuser__oxpassword->value );
02271             }
02272             $sHash = $this->oxuser__oxpassword->value;
02273         }
02274         return $sHash;
02275     }
02276 
02285     public static function getAdminUser()
02286     {
02287         return self::getActiveUser( true );
02288     }
02289 
02300     public static function getActiveUser( $blForceAdmin = false )
02301     {
02302         $oUser = oxNew( 'oxuser' );
02303         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02304             return $oUser;
02305         } else {
02306             return false;
02307         }
02308     }
02309 
02317     public function getOpenIdPassword( $iLength = 25 )
02318     {
02319         $sPassword= "openid_".substr( oxUtilsObject::getInstance()->generateUId(), 0, $iLength);
02320         return $sPassword;
02321     }
02322 
02330     public function getReviewUserHash( $sUserId )
02331     {
02332         $oDb = oxDb::getDb();
02333         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02334         return $sReviewUserHash;
02335     }
02336 
02344     public function getReviewUserId( $sReviewUserHash )
02345     {
02346         $oDb = oxDb::getDb();
02347         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02348         return $sUserId;
02349     }
02350 
02356     public function getState()
02357     {
02358         return $this->oxuser__oxstateid->value;
02359     }
02360 
02366     public function isTermsAccepted()
02367     {
02368         $sShopId = $this->getConfig()->getShopId();
02369         $sUserId = $this->getId();
02370         return (bool) oxDb::getDb()->getOne( "select 1 from oxacceptedterms where oxuserid='{$sUserId}' and oxshopid='{$sShopId}'" );
02371     }
02372 
02378     public function acceptTerms()
02379     {
02380         $sUserId  = $this->getId();
02381         $sShopId  = $this->getConfig()->getShopId();
02382         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02383 
02384         oxDb::getDb()->execute( "replace oxacceptedterms set oxuserid='{$sUserId}', oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02385     }
02386 
02395     public function setCreditPointsForRegistrant( $sUserId )
02396     {
02397         $blSet   = false;
02398         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02399         if ( $iPoints ) {
02400             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02401             if ( $blSet = $this->save() ) {
02402                 $oDb = oxDb::getDb();
02403 
02404                 // updating users statistics
02405                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ". $oDb->quote( $sUserId ) );
02406 
02407                 $oInvUser = oxNew( "oxuser" );
02408                 if ( $oInvUser->load( $sUserId ) ) {
02409                     $blSet = $oInvUser->setCreditPointsForInviter();
02410                 }
02411             }
02412 
02413             oxSession::deleteVar( 'su' );
02414         }
02415 
02416         return $blSet;
02417     }
02418 
02424     public function setCreditPointsForInviter()
02425     {
02426         $blSet   = false;
02427         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02428         if ( $iPoints ) {
02429             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02430             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02431             $blSet = $this->save();
02432         }
02433 
02434         return $blSet;
02435     }
02436 
02442     public function updateFbId()
02443     {
02444         $oFb = oxFb::getInstance();
02445         $blRet = false;
02446 
02447         if ( $oFb->isConnected() && $oFb->getUser() ) {
02448              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02449              $blRet = $this->save();
02450         }
02451 
02452         return $blRet;
02453     }
02454 }