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 $_sClassName = 'oxuser';
00027 
00033     protected $_aBaskets = array();
00034 
00040     protected $_oGroups;
00041 
00047     protected $_aAddresses = array();
00048 
00054     protected $_oPayments;
00055 
00061     protected $_oRecommList;
00062 
00068     protected $_blMallUsers = false;
00069 
00075     protected static $_aUserCookie = array();
00076 
00082     protected $_iCntNoticeListArticles = null;
00083 
00089     protected $_iCntWishListArticles = null;
00090 
00096     protected $_iCntRecommLists = null;
00097 
00103     protected $_sUpdateKey = null;
00104 
00110     protected $_blLoadedFromCookie  = null;
00111 
00117     protected $_sSelAddressId = null;
00118 
00124     protected $_oSelAddress = null;
00125 
00131     protected $_sWishId = null;
00132 
00138     protected $_oUserCountryTitle = null;
00139 
00145     public function __construct()
00146     {
00147         $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00148 
00149         parent::__construct();
00150         $this->init( 'oxuser' );
00151     }
00152 
00160     public function setMallUsersStatus( $blOn = false )
00161     {
00162         $this->_blMallUsers = $blOn;
00163     }
00164 
00172     public function __get( $sParamName )
00173     {
00174         // it saves memory using - loads data only if it is used
00175         switch ( $sParamName ) {
00176             case 'oGroups':
00177                 return $this->_oGroups = $this->getUserGroups();
00178                 break;
00179             case 'iCntNoticeListArticles':
00180                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00181                 break;
00182             case 'iCntWishListArticles':
00183                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00184                 break;
00185             case 'iCntRecommLists':
00186                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00187                 break;
00188             case 'oAddresses':
00189                 return $this->getUserAddresses();
00190                 break;
00191             case 'oPayments':
00192                 return $this->_oPayments = $this->getUserPayments();
00193                 break;
00194             case 'oxuser__oxcountry':
00195                 return $this->oxuser__oxcountry = $this->getUserCountry();
00196                 break;
00197             case 'sDBOptin':
00198                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00199                 break;
00200             case 'sEmailFailed':
00201                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00202                 break;
00203         }
00204     }
00205 
00211     public function getNewsSubscription()
00212     {
00213         if ( $this->_oNewsSubscription !== null ) {
00214             return $this->_oNewsSubscription;
00215         }
00216 
00217         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00218 
00219         // if subscription object is not set yet - we should create one
00220         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00221             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00222 
00223                 // no subscription defined yet - creating one
00224                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00225                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00226                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00227                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00228                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00229             }
00230         }
00231 
00232         return $this->_oNewsSubscription;
00233     }
00234 
00244     public function getUserCountry( $sCountryId = null, $iLang = null )
00245     {
00246         if ( $this->_oUserCountryTitle == null || $sCountryId ) {
00247             $sId = $sCountryId ? $sCountryId : $this->oxuser__oxcountryid->value;
00248             $oDb = oxDb::getDb();
00249             $sViewName = getViewName( 'oxcountry', $iLang );
00250             $sQ = "select oxtitle from {$sViewName} where oxid = " . $oDb->quote( $sId ) . " ";
00251             $oCountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00252             if ( !$sCountryId ) {
00253                 $this->_oUserCountryTitle = $oCountry;
00254             }
00255         } else {
00256             return $this->_oUserCountryTitle;
00257         }
00258 
00259         return $oCountry;
00260     }
00261 
00269     public function getUserCountryId( $sCountry = null )
00270     {
00271         $oDb = oxDb::getDb();
00272         $sQ = "select oxid from ".getviewName( "oxcountry" )." where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00273         $sCountryId = $oDb->getOne( $sQ );
00274 
00275         return $sCountryId;
00276     }
00277 
00285     public function getUserGroups( $sOXID = null )
00286     {
00287 
00288         if ( isset( $this->_oGroups ) ) {
00289             return $this->_oGroups;
00290         }
00291 
00292         if ( !$sOXID ) {
00293             $sOXID = $this->getId();
00294         }
00295 
00296         $sViewName = getViewName( "oxgroups" );
00297         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00298         $sSelect  = "select {$sViewName}.* from {$sViewName} left join oxobject2group on oxobject2group.oxgroupsid = {$sViewName}.oxid
00299                      where oxobject2group.oxobjectid = " . oxDb::getDb()->quote( $sOXID );
00300         $this->_oGroups->selectString( $sSelect );
00301         return $this->_oGroups;
00302     }
00303 
00311     public function getUserAddresses( $sUserId = null )
00312     {
00313         $sUserId = isset( $sUserId ) ? $sUserId : $this->getId();
00314         if ( !isset( $this->_aAddresses[$sUserId] ) ) {
00315             $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId );
00316 
00317             //P
00318             $this->_aAddresses[$sUserId] = oxNew( "oxlist" );
00319             $this->_aAddresses[$sUserId]->init( "oxaddress" );
00320             $this->_aAddresses[$sUserId]->selectString( $sSelect );
00321 
00322             // marking selected
00323             if ( $sAddressId = $this->getSelectedAddressId() ) {
00324                 foreach ( $this->_aAddresses[$sUserId] as $oAddress ) {
00325                     if ( $oAddress->getId() === $sAddressId ) {
00326                         $oAddress->setSelected();
00327                         break;
00328                     }
00329                 }
00330             }
00331         }
00332         return $this->_aAddresses[$sUserId];
00333     }
00334 
00342     public function setSelectedAddressId( $sAddressId )
00343     {
00344         $this->_sSelAddressId = $sAddressId;
00345     }
00346 
00352     public function getSelectedAddressId()
00353     {
00354         if ( $this->_sSelAddressId !== null ) {
00355             return $this->_sSelAddressId;
00356         }
00357 
00358         $sAddressId = oxConfig::getParameter( "oxaddressid");
00359         if ( !$sAddressId && !oxConfig::getParameter( 'reloadaddress' ) ) {
00360             $sAddressId = oxSession::getVar( "deladrid" );
00361         }
00362         return $sAddressId;
00363     }
00364 
00370     protected function _getWishListId()
00371     {
00372         $this->_sWishId = null;
00373         // check if we have to set it here
00374         $oBasket = $this->getSession()->getBasket();
00375         foreach ( $oBasket->getContents() as $oBasketItem ) {
00376             if ( $this->_sWishId = $oBasketItem->getWishId() ) {
00377                 // stop on first found
00378                 break;
00379             }
00380         }
00381         return $this->_sWishId;
00382     }
00383 
00392     public function getSelectedAddress( $sWishId = false )
00393     {
00394         if ( $this->_oSelAddress !== null ) {
00395             return $this->_oSelAddress;
00396         }
00397 
00398         $oSelectedAddress = null;
00399         $oAddresses = $this->getUserAddresses();
00400         if ( $oAddresses->count() ) {
00401             if ( $sAddressId = $this->getSelectedAddressId() ) {
00402                 foreach ( $oAddresses as $oAddress ) {
00403                     if ( $oAddress->getId() == $sAddressId ) {
00404                         $oAddress->selected = 1;
00405                         $oAddress->setSelected();
00406                         $oSelectedAddress = $oAddress;
00407                         break;
00408                     }
00409                 }
00410             }
00411 
00412             // in case none is set - setting first one
00413             if ( !$oSelectedAddress ) {
00414                 if ( !$sAddressId || $sAddressId >= 0 ) {
00415                     $oAddresses->rewind();
00416                     $oAddress = $oAddresses->current();
00417                 } else {
00418                     $aAddresses = $oAddresses->getArray();
00419                     $oAddress   = array_pop( $aAddresses );
00420                 }
00421                 $oAddress->selected = 1;
00422                 $oAddress->setSelected();
00423                 $oSelectedAddress = $oAddress;
00424             }
00425         }
00426         $this->_oSelAddress = $oSelectedAddress;
00427         return $oSelectedAddress;
00428     }
00429 
00437     public function getUserPayments( $sOXID = null )
00438     {
00439         if ( $this->_oPayments === null ) {
00440 
00441             if ( !$sOXID ) {
00442                 $sOXID = $this->getId();
00443             }
00444 
00445             $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00446 
00447             $this->_oPayments = oxNew( 'oxlist' );
00448             $this->_oPayments->init( 'oxUserPayment' );
00449             $this->_oPayments->selectString( $sSelect );
00450 
00451             $myUtils = oxUtils::getInstance();
00452             foreach ( $this->_oPayments as $oPayment ) {
00453                 // add custom fields to this class
00454                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00455             }
00456         }
00457 
00458         return $this->_oPayments;
00459     }
00460 
00466     public function save()
00467     {
00468         $myConfig  = oxConfig::getInstance();
00469 
00470         $blAddRemark = false;
00471         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00472             $blAddRemark = true;
00473             //save oxregister value
00474             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00475         }
00476 
00477         // setting user rights
00478         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00479 
00480         // processing birth date which came from output as array
00481         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00482             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00483         }
00484 
00485         // checking if user Facebook ID should be updated
00486         if ( $myConfig->getConfigParam( "bl_showFbConnect" ) ) {
00487             $oFb = oxFb::getInstance();
00488             if ( $oFb->isConnected() && $oFb->getUser() ) {
00489                  $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
00490             }
00491         }
00492 
00493         $blRet = parent::save();
00494 
00495         //add registered remark
00496         if ( $blAddRemark && $blRet ) {
00497             $oRemark = oxNew( 'oxremark' );
00498             $oRemark->oxremark__oxtext     = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00499             $oRemark->oxremark__oxtype     = new oxField('r', oxField::T_RAW);
00500             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00501             $oRemark->save();
00502         }
00503 
00504         return $blRet;
00505     }
00506 
00512     public function allowDerivedUpdate()
00513     {
00514         return true;
00515     }
00516 
00524     public function inGroup( $sGroupID )
00525     {
00526         $blIn = false;
00527         if ( ( $oGroups = $this->getUserGroups() ) ) {
00528             $blIn = isset( $oGroups[ $sGroupID ] );
00529         }
00530 
00531         return $blIn;
00532     }
00533 
00542     public function delete( $sOXID = null )
00543     {
00544 
00545         if ( !$sOXID ) {
00546             $sOXID = $this->getId();
00547         }
00548         if ( !$sOXID ) {
00549             return false;
00550         }
00551 
00552         $blDeleted = parent::delete( $sOXID );
00553 
00554         if ( $blDeleted ) {
00555             $oDb = oxDb::getDb();
00556             $sOXIDQuoted = $oDb->quote($sOXID);
00557 
00558             // deleting stored payment, address, group dependencies, remarks info
00559             $rs = $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00560             $rs = $oDb->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00561 
00562             // deleting notice/wish lists
00563             $rs = $oDb->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00564             $rs = $oDb->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00565 
00566             // deleting newsletter subscription
00567             $rs = $oDb->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00568 
00569             // delivery and delivery sets
00570             $rs = $oDb->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00571 
00572             // discounts
00573             $rs = $oDb->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00574 
00575 
00576             // and leaving all order related information
00577             $rs = $oDb->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00578 
00579             $blDeleted = $rs->EOF;
00580         }
00581 
00582         return $blDeleted;
00583     }
00584 
00592     public function load( $oxID )
00593     {
00594 
00595         $blRet = parent::Load( $oxID );
00596 
00597         // convert date's to international format
00598         if ( isset( $this->oxuser__oxcreate->value ) ) {
00599             $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00600         }
00601 
00602         return $blRet;
00603     }
00604 
00612     public function exists( $sOXID = null )
00613     {
00614         $oDb = oxDb::getDb();
00615         if ( !$sOXID ) {
00616             $sOXID = $this->getId();
00617         }
00618 
00619         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00620                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).'';
00621 
00622         if ( $sOXID ) {
00623             $sSelect.= " or oxid = ".$oDb->quote( $sOXID ) . " ) ";
00624         } else {
00625             $sSelect.= ' ) ';
00626         }
00627 
00628         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00629             $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00630         }
00631 
00632         $blExists = false;
00633         if ( ( $sOxid = $oDb->getOne( $sSelect ) ) ) {
00634              // update - set oxid
00635             $this->setId( $sOxid );
00636             $blExists = true;
00637         }
00638         return $blExists;
00639     }
00640 
00649     public function getOrders( $iLimit = false, $iPage = 0 )
00650     {
00651         $myConfig = $this->getConfig();
00652         $oOrders = oxNew( 'oxlist' );
00653         $oOrders->init( 'oxorder' );
00654 
00655         if ( $iLimit !== false ) {
00656             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00657         }
00658 
00659         //P
00660         // Lists does not support loading from two tables, so orders
00661         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00662         // forcing to load product info which is used in templates
00663         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00664 
00665         //loading order for registered user
00666         if ( $this->oxuser__oxregister->value > 1 ) {
00667             $oDb = oxDb::getDb();
00668             $sQ = 'select * from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' and oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value ) . ' ';
00669 
00670             //#1546 - shopid check added, if it is not multishop
00671 
00672             $sQ .= ' order by oxorderdate desc ';
00673             $oOrders->selectString( $sQ );
00674         }
00675 
00676         return $oOrders;
00677     }
00678 
00684     public function getOrderCount()
00685     {
00686         $iCnt = 0;
00687         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00688             $oDb = oxDb::getDb();
00689             $sQ  = 'select count(*) from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00690             $iCnt = (int) $oDb->getOne( $sQ );
00691         }
00692 
00693         return $iCnt;
00694     }
00695 
00701     public function getNoticeListArtCnt()
00702     {
00703         if ( $this->_iCntNoticeListArticles === null ) {
00704             $this->_iCntNoticeListArticles = 0;
00705             if ( $this->getId() ) {
00706                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00707             }
00708         }
00709         return $this->_iCntNoticeListArticles;
00710     }
00711 
00717     public function getWishListArtCnt()
00718     {
00719         if ( $this->_iCntWishListArticles === null ) {
00720             $this->_iCntWishListArticles = false;
00721             if ( $this->getId() ) {
00722                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00723             }
00724         }
00725         return $this->_iCntWishListArticles;
00726     }
00727 
00734     public function getActiveCountry()
00735     {
00736         $sDeliveryCountry = '';
00737         $soxAddressId = oxSession::getVar( 'deladrid' );
00738         if ( $soxAddressId ) {
00739             $oDelAddress = oxNew( 'oxaddress' );
00740             $oDelAddress->load( $soxAddressId );
00741             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00742         } elseif ( $this->getId() ) {
00743             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00744         } else {
00745             $oUser = oxNew( 'oxuser' );
00746             if ( $oUser->loadActiveUser() ) {
00747                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00748             }
00749         }
00750 
00751         return $sDeliveryCountry;
00752     }
00753 
00761     public function createUser()
00762     {
00763         $oDb = oxDb::getDb();
00764         $sShopID = $this->getConfig()->getShopId();
00765 
00766         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00767         $sSelect = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00768         if ( !$this->_blMallUsers ) {
00769             $sSelect .= " and oxshopid = '{$sShopID}' ";
00770         }
00771         $sOXID = $oDb->getOne( $sSelect, false, false );
00772 
00773         // user without password found - lets use
00774         if ( isset( $sOXID ) && $sOXID ) {
00775             // try to update
00776             $this->delete( $sOXID );
00777         } elseif ( $this->_blMallUsers ) {
00778             // must be sure if there is no dublicate user
00779             $sQ = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00780             if ( $oDb->getOne( $sQ, false, false ) ) {
00781                 $oEx = oxNew( 'oxUserException' );
00782                 $oLang = oxLang::getInstance();
00783                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00784                 throw $oEx;
00785             }
00786         }
00787 
00788         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00789         if ( ( $blOK = $this->save() ) ) {
00790             // dropping/cleaning old delivery address/payment info
00791             $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00792             $oDb->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00793         } else {
00794             $oEx = oxNew( 'oxUserException' );
00795             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00796             throw $oEx;
00797         }
00798 
00799         return $blOK;
00800     }
00801 
00809     public function addToGroup( $sGroupID )
00810     {
00811         if ( !$this->inGroup( $sGroupID ) ) {
00812             // create oxgroup object
00813             $oGroup = oxNew('oxGroups');
00814             if ( $oGroup->load($sGroupID) ) {
00815                 $oNewGroup = oxNew( 'oxobject2group' );
00816                 $oNewGroup->oxobject2group__oxobjectid = new oxField( $this->getId(), oxField::T_RAW );
00817                 $oNewGroup->oxobject2group__oxgroupsid = new oxField( $sGroupID, oxField::T_RAW );
00818                 if ( $oNewGroup->save() ) {
00819                     $this->_oGroups[$sGroupID] = $oGroup;
00820                     return true;
00821                 }
00822             }
00823         }
00824         return false;
00825     }
00826 
00834     public function removeFromGroup( $sGroupID = null )
00835     {
00836         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00837             $oGroups = oxNew( 'oxlist' );
00838             $oGroups->init( 'oxobject2group' );
00839             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00840             $oGroups->selectString( $sSelect );
00841             foreach ( $oGroups as $oRemgroup ) {
00842                 if ( $oRemgroup->delete() ) {
00843                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00844                 }
00845             }
00846         }
00847     }
00848 
00857     public function onOrderExecute( $oBasket, $iSuccess )
00858     {
00859 
00860         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00861             //adding user to particular customer groups
00862             if ( !$this->oxuser__oxdisableautogrp->value ) {
00863 
00864                 $myConfig = $this->getConfig();
00865                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00866                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00867 
00868                 $this->addToGroup( 'oxidcustomer' );
00869                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00870                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00871                     $this->addToGroup( 'oxidsmallcust' );
00872                 }
00873                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00874                     $this->addToGroup( 'oxidmiddlecust' );
00875                 }
00876                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00877                     $this->addToGroup( 'oxidgoodcust' );
00878                 }
00879             }
00880 
00881             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00882                 $this->removeFromGroup( 'oxidnotyetordered' );
00883             }
00884         }
00885     }
00886 
00894     public function getBasket( $sName )
00895     {
00896         if ( !isset( $this->_aBaskets[$sName] ) ) {
00897             $oBasket = oxNew( 'oxuserbasket' );
00898             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00899 
00900             // creating if it does not exist
00901             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00902                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00903                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00904 
00905                 // marking basket as new (it will not be saved in DB yet)
00906                 $oBasket->setIsNewBasket();
00907             }
00908 
00909             $this->_aBaskets[$sName] = $oBasket;
00910         }
00911 
00912         return $this->_aBaskets[$sName];
00913     }
00914 
00923     public function convertBirthday( $aData )
00924     {
00925 
00926         // preparing data to process
00927         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00928         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00929         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00930 
00931         // leaving empty if not set
00932         if ( !$iYear && !$iMonth && !$iDay )
00933             return "";
00934 
00935         // year
00936         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00937             $iYear = date('Y');
00938 
00939         // month
00940         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00941             $iMonth = 1;
00942 
00943         // maximum nuber of days in month
00944         $iMaxDays = 31;
00945         switch( $iMonth) {
00946             case 2 :
00947                 if ($iMaxDays > 28)
00948                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00949                 break;
00950             case 4  :
00951             case 6  :
00952             case 9  :
00953             case 11 :
00954                 $iMaxDays = min(30, $iMaxDays);
00955                 break;
00956         }
00957 
00958         // day
00959         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00960             $iDay = 1;
00961         }
00962 
00963         // whole date
00964         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00965     }
00966 
00973     public function getBoni()
00974     {
00975         return 1000;
00976     }
00977 
00990     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00991     {
00992         // preparing input
00993         $sDynGoup = strtolower( trim( $sDynGoup ) );
00994 
00995         // setting denied groups from admin settings also
00996         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
00997 
00998         // default state ..
00999         $blAdd = false;
01000 
01001         // user assignment to dyn group is not allowed
01002         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
01003             $blAdd = false;
01004         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
01005             // trying to add user to prohibited user group?
01006             $blAdd = false;
01007         } elseif ( $this->addToGroup( $sDynGoup ) ) {
01008             $blAdd = true;
01009         }
01010 
01011         // cleanup
01012         oxSession::deleteVar( 'dgr' );
01013 
01014         return $blAdd;
01015     }
01016 
01035     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01036     {
01037         // 1. checking user name
01038         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01039 
01040         // 2. cheking email
01041         $this->_checkEmail( $sLogin );
01042 
01043         // 3. password
01044         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01045 
01046         // 4. required fields
01047         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01048 
01049         // 5. country check
01050         $this->_checkCountries( $aInvAddress, $aDelAddress );
01051 
01052         // 6. vat id check.
01053             $this->_checkVatId( $aInvAddress );
01054 
01055 
01056         // throwing first validation error
01057         if ( $oError = oxInputValidator::getInstance()->getFirstValidationError( $this->getId() ) ) {
01058             throw $oError;
01059         }
01060     }
01061 
01070     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
01071     {
01072         // assigning to newsletter
01073         $blSuccess = false;
01074         $myConfig  = $this->getConfig();
01075         $mySession = $this->getSession();
01076 
01077         // user wants to get newsletter messages or no ?
01078         $oNewsSubscription = $this->getNewsSubscription();
01079         if ( $oNewsSubscription ) {
01080             if ( $blSubscribe && ( $iOptInStatus = $oNewsSubscription->getOptInStatus() ) != 1 ) {
01081                 if ( !$blSendOptIn ) {
01082 
01083                     // double-opt-in check is disabled - assigning automatically
01084                     $this->addToGroup( 'oxidnewsletter' );
01085                     // and setting subscribed status
01086                     $oNewsSubscription->setOptInStatus( 1 );
01087                     $blSuccess = true;
01088                 } else {
01089 
01090                     // double-opt-in check enabled - sending confirmation email and setting waiting status
01091                     if ( $iOptInStatus != 2 ) {
01092                         // sending double-opt-in mail
01093                         $oEmail = oxNew( 'oxemail' );
01094                         $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01095                     } else {
01096                         // mail already was sent, so just confirming that
01097                         $blSuccess = true;
01098                     }
01099 
01100                     $oNewsSubscription->setOptInStatus( 2 );
01101                 }
01102             } elseif ( !$blSubscribe ) {
01103                 // removing user from newsletter subscribers
01104                 $this->removeFromGroup( 'oxidnewsletter' );
01105                 $oNewsSubscription->setOptInStatus( 0 );
01106                 $blSuccess = true;
01107             }
01108         }
01109 
01110         return $blSuccess;
01111     }
01112 
01130     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01131     {
01132         // validating values before saving. If validation fails - exception is thrown
01133         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01134 
01135         // input data is fine - lets save updated user info
01136         $this->assign( $aInvAddress );
01137 
01138 
01139         // update old or add new delivery address
01140         $this->_assignAddress( $aDelAddress );
01141 
01142         // saving new values
01143         if ( $this->save() ) {
01144 
01145             // assigning automatically to specific groups
01146             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01147             $this->_setAutoGroups( $sCountryId );
01148         }
01149     }
01150 
01161     public function addUserAddress( $oUser )
01162     {
01163         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01164             return false;
01165         }
01166 
01167         $oAddress = oxNew( 'oxaddress' );
01168         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01169         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01170         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01171         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01172         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01173         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01174         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01175         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01176         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01177         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01178         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01179 
01180         // adding new address
01181         if ( $oAddress->save() ) {
01182             // resetting addresses
01183             $this->_aAddresses = null;
01184             return $oAddress->getId();
01185         }
01186     }
01187 
01195     protected function _assignAddress( $aDelAddress )
01196     {
01197         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01198             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01199             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01200             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01201             $oAddress = oxNew( 'oxaddress' );
01202             $oAddress->assign( $aDelAddress );
01203             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01204             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01205             $oAddress->save();
01206 
01207             // resetting addresses
01208             $this->_aAddresses = null;
01209 
01210             // saving delivery Address for later use
01211             oxSession::setVar( 'deladrid', $oAddress->getId() );
01212         } else {
01213             // resetting
01214             oxSession::setVar( 'deladrid', null );
01215         }
01216     }
01217 
01228     protected function _getLoginQuery( $sUser, $sPassword, $sShopID, $blAdmin )
01229     {
01230         $myConfig = $this->getConfig();
01231         $oDb = oxDb::getDb();
01232 
01233         $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01234         $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01235         $sShopSelect = "";
01236 
01237 
01238         // admin view: can only login with higher than 'user' rights
01239         if ( $blAdmin ) {
01240             $sShopSelect = " and ( oxrights != 'user' ) ";
01241         }
01242 
01243         $blStagingMode = false;
01244         $blDemoMode = false;
01245         $sWhat = "oxid";
01246             if ($myConfig->isDemoShop()) {
01247                 $blDemoMode = true;
01248             }
01249 
01250         $sSelect = "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01251         if ( ( $blDemoMode || $blStagingMode ) && $blAdmin ) {
01252             if ( $sPassword == "admin" && $sUser == "admin" ) {
01253                 $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' ";
01254             } elseif ( $blDemoMode ) {
01255                 $oEx = oxNew( 'oxUserException' );
01256                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01257                 throw $oEx;
01258             }
01259         }
01260 
01261         return $sSelect;
01262     }
01263 
01271     protected function _loadSavedUserBasketAfterLogin()
01272     {
01273         if ( !$this->isAdmin() && !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' )) {
01274             //load basket from the database
01275             try {
01276                 if ( $oBasket = $this->getSession()->getBasket() ) {
01277                     $oBasket->load();
01278                 }
01279             } catch ( Exception $oE ) {
01280                 //just ignore it
01281             }
01282         }
01283     }
01284 
01297     public function login( $sUser, $sPassword, $blCookie = false)
01298     {
01299         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01300             $oEx = oxNew( 'oxCookieException' );
01301             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01302             throw $oEx;
01303         }
01304 
01305         $myConfig = $this->getConfig();
01306         if ( $sPassword ) {
01307 
01308             $sShopID = $myConfig->getShopId();
01309             $sSelect = $this->_getLoginQuery( $sUser, $sPassword, $sShopID, $this->isAdmin() );
01310 
01311             // load from DB
01312             $aData = oxDb::getDb()->getAll( $sSelect );
01313             $sOXID = @$aData[0][0];
01314             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01315 
01316                 if ( !$this->load( $sOXID ) ) {
01317                     $oEx = oxNew( 'oxUserException' );
01318                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01319                     throw $oEx;
01320                 }
01321             }
01322         }
01323 
01324 
01325         //login successfull?
01326         if ( $this->oxuser__oxid->value ) {
01327             // yes, successful login
01328 
01329             //reseting active user
01330             $this->setUser( null );
01331 
01332             if ( $this->isAdmin() ) {
01333                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01334             } else {
01335                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01336             }
01337 
01338             // cookie must be set ?
01339             if ( $blCookie && $myConfig->getConfigParam( 'blShowRememberMe' ) ) {
01340                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value );
01341             }
01342 
01343             //load basket from the database
01344             $this->_loadSavedUserBasketAfterLogin();
01345 
01346             return true;
01347         } else {
01348             $oEx = oxNew( 'oxUserException' );
01349             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01350             throw $oEx;
01351         }
01352     }
01353 
01359     public function logout()
01360     {
01361         // deleting session info
01362         oxSession::deleteVar( 'usr' );  // for front end
01363         oxSession::deleteVar( 'auth' ); // for back end
01364         oxSession::deleteVar( 'dgr' );
01365         oxSession::deleteVar( 'dynvalue' );
01366         oxSession::deleteVar( 'paymentid' );
01367         // oxSession::deleteVar( 'deladrid' );
01368 
01369         // delete cookie
01370         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01371 
01372         // unsetting global user
01373         $this->setUser( null );
01374 
01375         return true;
01376     }
01377 
01384     public function loadAdminUser()
01385     {
01386         return $this->loadActiveUser( true );
01387     }
01388 
01397     public function loadActiveUser( $blForceAdmin = false )
01398     {
01399         $myConfig = $this->getConfig();
01400 
01401         $blAdmin = $this->isAdmin() || $blForceAdmin;
01402         $oDb = oxDb::getDb();
01403 
01404         // first - checking session info
01405         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01406         $blFoundInCookie = false;
01407 
01408         //trying automatic login (by 'remember me' cookie)
01409         if ( !$sUserID && !$blAdmin && $myConfig->getConfigParam('blShowRememberMe') ) {
01410             $sShopID = $myConfig->getShopId();
01411             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01412                 $aData = explode( '@@@', $sSet );
01413                 $sUser = $aData[0];
01414                 $sPWD  = @$aData[1];
01415 
01416                 $sSelect =  'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDb->quote($sUser);
01417 
01418                 $rs = $oDb->select( $sSelect );
01419                 if ( $rs != false && $rs->recordCount() > 0 ) {
01420                     while (!$rs->EOF) {
01421                         $sTest = crypt( $rs->fields[1], $rs->fields[2] );
01422                         if ( $sTest == $sPWD ) {
01423                             // found
01424                             $sUserID = $rs->fields[0];
01425                             $blFoundInCookie = true;
01426                             break;
01427                         }
01428                         $rs->moveNext();
01429                     }
01430                 }
01431             }
01432         }
01433 
01434         // Checking if user is connected via Facebook connect.
01435         // If yes, trying to login user using user Facebook ID
01436         if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01437             $oFb = oxFb::getInstance();
01438             if ( $oFb->isConnected() && $oFb->getUser() ) {
01439                 $sUserSelect = "oxuser.oxfbid = " . $oDb->quote( $oFb->getUser() );
01440                 $sShopSelect = "";
01441 
01442 
01443                 $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01444                 $sUserID = $oDb->getOne( $sSelect );
01445             }
01446         }
01447 
01448         // checking user results
01449         if ( $sUserID ) {
01450             if ( $this->load( $sUserID ) ) {
01451                 // storing into session
01452                 if ($blAdmin) {
01453                     oxSession::setVar( 'auth', $sUserID );
01454                 } else {
01455                     oxSession::setVar( 'usr', $sUserID );
01456                 }
01457 
01458                 // marking the way user was loaded
01459                 $this->_blLoadedFromCookie = $blFoundInCookie;
01460                 return true;
01461             }
01462         } else {
01463             // no user
01464             if ($blAdmin) {
01465                 oxSession::deleteVar( 'auth' );
01466             } else {
01467                 oxSession::deleteVar( 'usr' );
01468             }
01469 
01470             return false;
01471         }
01472     }
01473 
01486     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01487     {
01488         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01489         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01490 
01491         // maybe this is LDAP user but supplied email Address instead of LDAP login
01492         $oDb = oxDb::getDb();
01493         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01494         if ( isset( $sLDAPKey) && $sLDAPKey) {
01495             $sUser = $sLDAPKey;
01496         }
01497 
01498         //$throws oxConnectionException
01499         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01500 
01501         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01502         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01503             // login successful
01504 
01505             // check if user is already in database
01506             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01507             $sOXID = $oDb->getOne( $sSelect );
01508 
01509             if ( !isset( $sOXID ) || !$sOXID ) {
01510                 // we need to create a new user
01511                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01512                 $this->setId();
01513 
01514                 // map all user data fields
01515                 foreach ( $aData as $fldname => $value) {
01516                     $sField = "oxuser__".strtolower( $fldname);
01517                     $this->$sField = new oxField( $aData[$fldname] );
01518                 }
01519 
01520                 $this->oxuser__oxactive  = new oxField( 1 );
01521                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01522                 $this->oxuser__oxldapkey = new oxField( $sUser );
01523                 $this->oxuser__oxrights  = new oxField( "user" );
01524                 $this->setPassword( "ldap user" );
01525 
01526                 $this->save();
01527             } else {
01528                 // LDAP user is already in OXID DB, load it
01529                 $this->load( $sOXID);
01530             }
01531 
01532         } else {
01533             $oEx = oxNew( 'oxUserException' );
01534             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01535             throw $oEx;
01536         }
01537     }
01538 
01545     protected function _getUserRights()
01546     {
01547         // previously user had no rights defined
01548         if ( !$this->oxuser__oxrights->value )
01549             return 'user';
01550 
01551         $oDb = oxDb::getDb();
01552         $myConfig    = $this->getConfig();
01553         $sAuthRights = null;
01554 
01555         // choosing possible user rights index
01556         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01557         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01558         if ( $sAuthUserID ) {
01559             $sAuthRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $sAuthUserID ) );
01560         }
01561 
01562         //preventing user rights edit for non admin
01563         $aRights = array();
01564 
01565         // selecting current users rights ...
01566         if ( $sCurrRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $this->getId() ) ) ) {
01567             $aRights[] = $sCurrRights;
01568         }
01569         $aRights[] = 'user';
01570 
01571         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01572             return current( $aRights );
01573         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01574             $aRights[] = $sAuthRights;
01575             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01576                 return current( $aRights );
01577             }
01578         }
01579 
01580         // leaving as it was set ...
01581         return $this->oxuser__oxrights->value;
01582     }
01583 
01589     protected function _insert()
01590     {
01591 
01592         // set oxcreate date
01593         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01594 
01595         if ( !isset( $this->oxuser__oxboni->value ) ) {
01596             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01597         }
01598 
01599         return parent::_insert();
01600     }
01601 
01607     protected function _update()
01608     {
01609         //V #M418: for not registered users, don't change boni during update
01610         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01611             $this->_aSkipSaveFields[] = 'oxboni';
01612         }
01613 
01614         // don't change this field
01615         $this->_aSkipSaveFields[] = 'oxcreate';
01616         if ( !$this->isAdmin() ) {
01617             $this->_aSkipSaveFields[] = 'oxcustnr';
01618             $this->_aSkipSaveFields[] = 'oxrights';
01619         }
01620 
01621         // updating subscription information
01622         if ( ( $blUpdate = parent::_update() ) ) {
01623             $this->getNewsSubscription()->updateSubscription( $this );
01624         }
01625 
01626         return $blUpdate;
01627     }
01628 
01644     protected function _checkLogin( $sLogin, $aInvAddress )
01645     {
01646         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01647         oxInputValidator::getInstance()->checkLogin( $this, $sLogin, $aInvAddress );
01648         return $sLogin;
01649     }
01650 
01658     public function checkIfEmailExists( $sEmail )
01659     {
01660         $myConfig = $this->getConfig();
01661         $oDb = oxDb::getDb();
01662         $iShopId = $myConfig->getShopId();
01663         $blExists = false;
01664 
01665         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDb->quote( $sEmail );
01666         if ( ( $sOxid = $this->getId() ) ) {
01667             $sQ .= " and oxid <> ".$oDb->quote( $sOxid );
01668         }
01669         $oRs = $oDb->select( $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 
01767     protected function _checkEmail( $sEmail )
01768     {
01769         oxInputValidator::getInstance()->checkEmail( $this, $sEmail );
01770     }
01771 
01786     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01787     {
01788         return $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01789     }
01790 
01803     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01804     {
01805         return oxInputValidator::getInstance()->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01806     }
01807 
01818     protected function _checkCountries( $aInvAddress, $aDelAddress )
01819     {
01820         oxInputValidator::getInstance()->checkCountries( $this, $aInvAddress, $aDelAddress );
01821     }
01822 
01834     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01835     {
01836         oxInputValidator::getInstance()->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01837     }
01838 
01849     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01850     {
01851         oxInputValidator::getInstance()->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01852     }
01853 
01864     protected function _checkVatId( $aInvAddress )
01865     {
01866         oxInputValidator::getInstance()->checkVatId( $this, $aInvAddress );
01867     }
01868 
01877     protected function _setAutoGroups( $sCountryId )
01878     {
01879         // assigning automatically to specific groups
01880         $blForeigner = true;
01881         $blForeignGroupExists = false;
01882         $blInlandGroupExists = false;
01883 
01884         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01885         // foreigner ?
01886         if ( is_array($aHomeCountry)) {
01887             if (in_array($sCountryId, $aHomeCountry)) {
01888                 $blForeigner = false;
01889             }
01890         } elseif ($sCountryId == $aHomeCountry) {
01891             $blForeigner = false;
01892         }
01893 
01894         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01895             $blForeignGroupExists = true;
01896             if ( !$blForeigner ) {
01897                 $this->removeFromGroup( 'oxidforeigncustomer' );
01898             }
01899         }
01900 
01901         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01902             $blInlandGroupExists = true;
01903             if ( $blForeigner ) {
01904                 $this->removeFromGroup( 'oxidnewcustomer' );
01905             }
01906         }
01907 
01908         if ( !$this->oxuser__oxdisableautogrp->value ) {
01909             if ( !$blForeignGroupExists && $blForeigner ) {
01910                 $this->addToGroup( 'oxidforeigncustomer' );
01911             }
01912             if ( !$blInlandGroupExists && !$blForeigner ) {
01913                 $this->addToGroup( 'oxidnewcustomer' );
01914             }
01915         }
01916     }
01917 
01926     protected function _hasUserAddress( $sUserId )
01927     {
01928         $oAddresses = $this->getUserAddresses();
01929         if ( $oAddresses && count($oAddresses)>0 ) {
01930             $oAddresses->rewind() ;
01931             foreach ($oAddresses as $key => $oAddress) {
01932                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01933                     return true;
01934                 }
01935             }
01936         }
01937         return false;
01938     }
01939 
01953     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000, $sSalt = 'ox' )
01954     {
01955         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout, $sSalt );
01956     }
01957 
01967     protected function _deleteUserCookie( $sShopId = null )
01968     {
01969         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
01970     }
01971 
01981     protected static function _getUserCookie( $sShopId = null )
01982     {
01983         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
01984     }
01985 
01986 
01995     public function loadUserByUpdateId( $sUid )
01996     {
01997         $oDb = oxDb::getDb();
01998         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
01999         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02000             return $this->load( $sUserId );
02001         }
02002     }
02003 
02011     public function setUpdateKey( $blReset = false )
02012     {
02013         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02014         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02015 
02016         // generating key
02017         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02018 
02019         // setting expiration time for 6 hours
02020         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02021 
02022         // saving
02023         $this->save();
02024     }
02025 
02031     public function getUpdateLinkTerm()
02032     {
02033         return 3600 * 6;
02034     }
02035 
02043     public function isExpiredUpdateId( $sKey )
02044     {
02045         $oDb = oxDb::getDb();
02046         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02047         return !( (bool) $oDb->getOne( $sQ ) );
02048     }
02049 
02055     public function getUpdateId()
02056     {
02057         if ( $this->_sUpdateKey === null ) {
02058             $this->setUpdateKey();
02059             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02060         }
02061         return $this->_sUpdateKey;
02062     }
02063 
02072     public function encodePassword( $sPassword, $sSalt )
02073     {
02074         $oDb = oxDb::getDb();
02075         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02076     }
02077 
02085     public function prepareSalt( $sSalt )
02086     {
02087         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02088     }
02089 
02097     public function decodeSalt( $sSaltHex )
02098     {
02099         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02100     }
02101 
02109     public function setPassword( $sPassword = null )
02110     {
02111         // setting salt if password is not empty
02112         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02113 
02114         // encoding only if password was not empty (e.g. user registration without pass)
02115         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02116 
02117         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02118         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02119     }
02120 
02128     public function isSamePassword( $sNewPass )
02129     {
02130         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02131     }
02132 
02138     public function isLoadedFromCookie()
02139     {
02140         return $this->_blLoadedFromCookie;
02141     }
02142 
02149     public function getPasswordHash()
02150     {
02151         $sHash = null;
02152         if ( $this->oxuser__oxpassword->value ) {
02153             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02154                 // decodable pass ?
02155                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02156             } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02157                 // plain pass ?
02158                 $this->setPassword( $this->oxuser__oxpassword->value );
02159             }
02160             $sHash = $this->oxuser__oxpassword->value;
02161         }
02162         return $sHash;
02163     }
02164 
02173     public static function getAdminUser()
02174     {
02175         return self::getActiveUser( true );
02176     }
02177 
02188     public static function getActiveUser( $blForceAdmin = false )
02189     {
02190         $oUser = oxNew( 'oxuser' );
02191         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02192             return $oUser;
02193         } else {
02194             return false;
02195         }
02196     }
02197 
02205     public function getReviewUserHash( $sUserId )
02206     {
02207         $oDb = oxDb::getDb();
02208         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02209         return $sReviewUserHash;
02210     }
02211 
02219     public function getReviewUserId( $sReviewUserHash )
02220     {
02221         $oDb = oxDb::getDb();
02222         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02223         return $sUserId;
02224     }
02225 
02231     public function getState()
02232     {
02233         return $this->oxuser__oxstateid->value;
02234     }
02235 
02241     public function isTermsAccepted()
02242     {
02243         $oDb = oxDb::getDb();
02244         $sShopId = $this->getConfig()->getShopId();
02245         $sUserId = $oDb->quote( $this->getId() );
02246         return (bool) $oDb->getOne( "select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'" );
02247     }
02248 
02254     public function acceptTerms()
02255     {
02256         $oDb = oxDb::getDb();
02257         $sUserId  = $oDb->quote( $this->getId() );
02258         $sShopId  = $this->getConfig()->getShopId();
02259         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02260 
02261         $oDb->execute( "replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02262     }
02263 
02273     public function setCreditPointsForRegistrant( $sUserId, $sRecEmail )
02274     {
02275         $blSet   = false;
02276         $oDb = oxDb::getDb();
02277         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02278         // check if this invitation is still not accepted
02279         $iPending = $oDb->getOne( "select count(oxuserid) from oxinvitations where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail )." and oxpending = 1 and oxaccepted = 0", false, false);
02280         if ( $iPoints && $iPending ) {
02281             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02282             if ( $blSet = $this->save() ) {
02283                 // updating users statistics
02284                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail ) );
02285                 $oInvUser = oxNew( "oxuser" );
02286                 if ( $oInvUser->load( $sUserId ) ) {
02287                     $blSet = $oInvUser->setCreditPointsForInviter();
02288                 }
02289             }
02290         }
02291         oxSession::deleteVar( 'su' );
02292         oxSession::deleteVar( 're' );
02293 
02294         return $blSet;
02295     }
02296 
02302     public function setCreditPointsForInviter()
02303     {
02304         $blSet   = false;
02305         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02306         if ( $iPoints ) {
02307             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02308             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02309             $blSet = $this->save();
02310         }
02311 
02312         return $blSet;
02313     }
02314 
02320     public function updateFbId()
02321     {
02322         $oFb = oxFb::getInstance();
02323         $blRet = false;
02324 
02325         if ( $oFb->isConnected() && $oFb->getUser() ) {
02326              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02327              $blRet = $this->save();
02328         }
02329 
02330         return $blRet;
02331     }
02332 
02340     public function updateInvitationStatistics( $aRecEmail )
02341     {
02342         $oDb = oxDb::getDb();
02343         $sUserId = $this->getId();
02344 
02345         if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
02346             //iserting statistics about invitation
02347             $sDate = oxUtilsDate::getInstance()->formatDBDate( date("Y-m-d"), true );
02348             $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
02349             foreach ( $aRecEmail as $sRecEmail ) {
02350                 $sSql = "INSERT INTO oxinvitations SET oxuserid = ".$oDb->quote( $sUserId ).", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02351                 $oDb->execute( $sSql );
02352             }
02353         }
02354     }
02355 
02363     public function getIdByUserName( $sUserName )
02364     {
02365         $oDb = oxDb::getDb();
02366         $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = ". $oDb->quote( $sUserName );
02367         if ( !$this->getConfig()->getConfigParam( 'blMallUsers' ) ) {
02368             $sQ .= " AND `oxshopid` = ". $oDb->quote( $this->getConfig()->getShopId() );
02369         }
02370 
02371         return $oDb->getOne( $sQ );
02372 
02373     }
02374 
02380     public function hasAccount()
02381     {
02382 
02383         return (bool) $this->oxuser__oxpassword->value;
02384 
02385     }
02386 
02387 }