oxuser.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class oxUser extends oxBase
00010 {
00015     protected $_blDisableShopCheck = true;
00016 
00021     protected $_oNewsSubscription = null;
00022 
00027     protected $_sClassName = 'oxuser';
00028 
00034     protected $_aBaskets = array();
00035 
00041     protected $_oGroups;
00042 
00048     protected $_aAddresses = array();
00049 
00055     protected $_oPayments;
00056 
00062     protected $_oRecommList;
00063 
00069     protected $_blMallUsers = false;
00070 
00076     protected static $_aUserCookie = array();
00077 
00083     protected $_iCntNoticeListArticles = null;
00084 
00090     protected $_iCntWishListArticles = null;
00091 
00097     protected $_iCntRecommLists = null;
00098 
00104     protected $_sUpdateKey = null;
00105 
00111     protected $_blLoadedFromCookie  = null;
00112 
00118     protected $_sSelAddressId = null;
00119 
00125     protected $_oSelAddress = null;
00126 
00132     protected $_sWishId = null;
00133 
00139     protected $_oUserCountryTitle = null;
00140 
00146     public function __construct()
00147     {
00148         $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00149 
00150         parent::__construct();
00151         $this->init( 'oxuser' );
00152     }
00153 
00161     public function setMallUsersStatus( $blOn = false )
00162     {
00163         $this->_blMallUsers = $blOn;
00164     }
00165 
00173     public function __get( $sParamName )
00174     {
00175         // it saves memory using - loads data only if it is used
00176         switch ( $sParamName ) {
00177             case 'oGroups':
00178                 return $this->_oGroups = $this->getUserGroups();
00179                 break;
00180             case 'iCntNoticeListArticles':
00181                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00182                 break;
00183             case 'iCntWishListArticles':
00184                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00185                 break;
00186             case 'iCntRecommLists':
00187                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00188                 break;
00189             case 'oAddresses':
00190                 return $this->getUserAddresses();
00191                 break;
00192             case 'oPayments':
00193                 return $this->_oPayments = $this->getUserPayments();
00194                 break;
00195             case 'oxuser__oxcountry':
00196                 return $this->oxuser__oxcountry = $this->getUserCountry();
00197                 break;
00198             case 'sDBOptin':
00199                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00200                 break;
00201             case 'sEmailFailed':
00202                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00203                 break;
00204         }
00205     }
00206 
00212     public function getNewsSubscription()
00213     {
00214         if ( $this->_oNewsSubscription !== null ) {
00215             return $this->_oNewsSubscription;
00216         }
00217 
00218         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00219 
00220         // if subscription object is not set yet - we should create one
00221         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00222             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00223 
00224                 // no subscription defined yet - creating one
00225                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00226                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00227                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00228                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00229                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00230             }
00231         }
00232 
00233         return $this->_oNewsSubscription;
00234     }
00235 
00245     public function getUserCountry( $sCountryId = null, $iLang = null )
00246     {
00247         if ( $this->_oUserCountryTitle == null || $sCountryId ) {
00248             $sId = $sCountryId ? $sCountryId : $this->oxuser__oxcountryid->value;
00249             $oDb = oxDb::getDb();
00250             $sViewName = getViewName( 'oxcountry', $iLang );
00251             $sQ = "select oxtitle from {$sViewName} where oxid = " . $oDb->quote( $sId ) . " ";
00252             $oCountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00253             if ( !$sCountryId ) {
00254                 $this->_oUserCountryTitle = $oCountry;
00255             }
00256         } else {
00257             return $this->_oUserCountryTitle;
00258         }
00259 
00260         return $oCountry;
00261     }
00262 
00270     public function getUserCountryId( $sCountry = null )
00271     {
00272         $oDb = oxDb::getDb();
00273         $sQ = "select oxid from ".getviewName( "oxcountry" )." where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00274         $sCountryId = $oDb->getOne( $sQ );
00275 
00276         return $sCountryId;
00277     }
00278 
00286     public function getUserGroups( $sOXID = null )
00287     {
00288 
00289         if ( isset( $this->_oGroups ) ) {
00290             return $this->_oGroups;
00291         }
00292 
00293         if ( !$sOXID ) {
00294             $sOXID = $this->getId();
00295         }
00296 
00297         $sViewName = getViewName( "oxgroups" );
00298         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00299         $sSelect  = "select {$sViewName}.* from {$sViewName} left join oxobject2group on oxobject2group.oxgroupsid = {$sViewName}.oxid
00300                      where oxobject2group.oxobjectid = " . oxDb::getDb()->quote( $sOXID );
00301         $this->_oGroups->selectString( $sSelect );
00302         return $this->_oGroups;
00303     }
00304 
00312     public function getUserAddresses( $sUserId = null )
00313     {
00314         $sUserId = isset( $sUserId ) ? $sUserId : $this->getId();
00315         if ( !isset( $this->_aAddresses[$sUserId] ) ) {
00316             $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId );
00317 
00318             //P
00319             $this->_aAddresses[$sUserId] = oxNew( "oxlist" );
00320             $this->_aAddresses[$sUserId]->init( "oxaddress" );
00321             $this->_aAddresses[$sUserId]->selectString( $sSelect );
00322 
00323             // marking selected
00324             if ( $sAddressId = $this->getSelectedAddressId() ) {
00325                 foreach ( $this->_aAddresses[$sUserId] as $oAddress ) {
00326                     if ( $oAddress->getId() === $sAddressId ) {
00327                         $oAddress->setSelected();
00328                         break;
00329                     }
00330                 }
00331             }
00332         }
00333         return $this->_aAddresses[$sUserId];
00334     }
00335 
00343     public function setSelectedAddressId( $sAddressId )
00344     {
00345         $this->_sSelAddressId = $sAddressId;
00346     }
00347 
00353     public function getSelectedAddressId()
00354     {
00355         if ( $this->_sSelAddressId !== null ) {
00356             return $this->_sSelAddressId;
00357         }
00358 
00359         $sAddressId = oxConfig::getParameter( "oxaddressid");
00360         if ( !$sAddressId && !oxConfig::getParameter( 'reloadaddress' ) ) {
00361             $sAddressId = oxSession::getVar( "deladrid" );
00362         }
00363         return $sAddressId;
00364     }
00365 
00371     protected function _getWishListId()
00372     {
00373         $this->_sWishId = null;
00374         // check if we have to set it here
00375         $oBasket = $this->getSession()->getBasket();
00376         foreach ( $oBasket->getContents() as $oBasketItem ) {
00377             if ( $this->_sWishId = $oBasketItem->getWishId() ) {
00378                 // stop on first found
00379                 break;
00380             }
00381         }
00382         return $this->_sWishId;
00383     }
00384 
00393     public function getSelectedAddress( $sWishId = false )
00394     {
00395         if ( $this->_oSelAddress !== null ) {
00396             return $this->_oSelAddress;
00397         }
00398 
00399         $oSelectedAddress = null;
00400         $oAddresses = $this->getUserAddresses();
00401         if ( $oAddresses->count() ) {
00402             if ( $sAddressId = $this->getSelectedAddressId() ) {
00403                 foreach ( $oAddresses as $oAddress ) {
00404                     if ( $oAddress->getId() == $sAddressId ) {
00405                         $oAddress->selected = 1;
00406                         $oAddress->setSelected();
00407                         $oSelectedAddress = $oAddress;
00408                         break;
00409                     }
00410                 }
00411             }
00412 
00413             // in case none is set - setting first one
00414             if ( !$oSelectedAddress ) {
00415                 if ( !$sAddressId || $sAddressId >= 0 ) {
00416                     $oAddresses->rewind();
00417                     $oAddress = $oAddresses->current();
00418                 } else {
00419                     $aAddresses = $oAddresses->getArray();
00420                     $oAddress   = array_pop( $aAddresses );
00421                 }
00422                 $oAddress->selected = 1;
00423                 $oAddress->setSelected();
00424                 $oSelectedAddress = $oAddress;
00425             }
00426         }
00427         $this->_oSelAddress = $oSelectedAddress;
00428         return $oSelectedAddress;
00429     }
00430 
00438     public function getUserPayments( $sOXID = null )
00439     {
00440         if ( $this->_oPayments === null ) {
00441 
00442             if ( !$sOXID ) {
00443                 $sOXID = $this->getId();
00444             }
00445 
00446             $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00447 
00448             $this->_oPayments = oxNew( 'oxlist' );
00449             $this->_oPayments->init( 'oxUserPayment' );
00450             $this->_oPayments->selectString( $sSelect );
00451 
00452             $myUtils = oxRegistry::getUtils();
00453             foreach ( $this->_oPayments as $oPayment ) {
00454                 // add custom fields to this class
00455                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00456             }
00457         }
00458 
00459         return $this->_oPayments;
00460     }
00461 
00467     public function save()
00468     {
00469         $myConfig  = oxRegistry::getConfig();
00470 
00471         $blAddRemark = false;
00472         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00473             $blAddRemark = true;
00474             //save oxregister value
00475             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00476         }
00477 
00478         // setting user rights
00479         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00480 
00481         // processing birth date which came from output as array
00482         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00483             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00484         }
00485 
00486         // checking if user Facebook ID should be updated
00487         if ( $myConfig->getConfigParam( "bl_showFbConnect" ) ) {
00488             $oFb = oxRegistry::get("oxFb");
00489             if ( $oFb->isConnected() && $oFb->getUser() ) {
00490                  $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
00491             }
00492         }
00493 
00494         $blRet = parent::save();
00495 
00496         //add registered remark
00497         if ( $blAddRemark && $blRet ) {
00498             $oRemark = oxNew( 'oxremark' );
00499             $oRemark->oxremark__oxtext     = new oxField(oxRegistry::getLang()->translateString( 'usrRegistered' ), oxField::T_RAW);
00500             $oRemark->oxremark__oxtype     = new oxField('r', oxField::T_RAW);
00501             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00502             $oRemark->save();
00503         }
00504 
00505         return $blRet;
00506     }
00507 
00513     public function allowDerivedUpdate()
00514     {
00515         return true;
00516     }
00517 
00525     public function inGroup( $sGroupID )
00526     {
00527         $blIn = false;
00528         if ( ( $oGroups = $this->getUserGroups() ) ) {
00529             $blIn = isset( $oGroups[ $sGroupID ] );
00530         }
00531 
00532         return $blIn;
00533     }
00534 
00543     public function delete( $sOXID = null )
00544     {
00545 
00546         if ( !$sOXID ) {
00547             $sOXID = $this->getId();
00548         }
00549         if ( !$sOXID ) {
00550             return false;
00551         }
00552 
00553         $blDeleted = parent::delete( $sOXID );
00554 
00555         if ( $blDeleted ) {
00556             $oDb = oxDb::getDb();
00557             $sOXIDQuoted = $oDb->quote($sOXID);
00558 
00559             // deleting stored payment, address, group dependencies, remarks info
00560             $rs = $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00561             $rs = $oDb->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00562 
00563             // deleting notice/wish lists
00564             $rs = $oDb->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00565             $rs = $oDb->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00566 
00567             // deleting newsletter subscription
00568             $rs = $oDb->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00569 
00570             // delivery and delivery sets
00571             $rs = $oDb->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00572 
00573             // discounts
00574             $rs = $oDb->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00575 
00576 
00577             // and leaving all order related information
00578             $rs = $oDb->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00579 
00580             $blDeleted = $rs->EOF;
00581         }
00582 
00583         return $blDeleted;
00584     }
00585 
00593     public function load( $oxID )
00594     {
00595 
00596         $blRet = parent::load( $oxID );
00597 
00598         // convert date's to international format
00599         if ( isset( $this->oxuser__oxcreate->value ) ) {
00600             $this->oxuser__oxcreate->setValue(oxRegistry::get("oxUtilsDate")->formatDBDate( $this->oxuser__oxcreate->value ));
00601         }
00602 
00603         // change newsSubcription user id
00604         if ( isset($this->_oNewsSubscription) ) {
00605             $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField( $oxID, oxField::T_RAW);
00606         }
00607 
00608         return $blRet;
00609     }
00610 
00618     public function exists( $sOXID = null )
00619     {
00620         $oDb = oxDb::getDb();
00621         if ( !$sOXID ) {
00622             $sOXID = $this->getId();
00623         }
00624 
00625         $sShopSelect = '';
00626         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00627             $sShopSelect = ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00628         }
00629 
00630         //#4543 Query optimisation by splitting it into two, might need an logics optimisation as well
00631         if ( $sOXID ) {
00632             $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00633                     WHERE ( oxid = '.$oDb->quote( $sOXID ).' ) ';
00634             $sSelect .= $sShopSelect;
00635 
00636             if ( ( $sOxid = $oDb->getOne( $sSelect, false, false ) ) ) {
00637                 // update - set oxid
00638                 $this->setId( $sOxid );
00639                 return true;
00640             }
00641         }
00642 
00643         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00644                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).' ) ';
00645         $sSelect .= $sShopSelect;
00646 
00647         if ( ( $sOxid = $oDb->getOne( $sSelect, false, false ) ) ) {
00648              // update - set oxid
00649             $this->setId( $sOxid );
00650             return true;
00651         }
00652         return false;
00653     }
00654 
00663     public function getOrders( $iLimit = false, $iPage = 0 )
00664     {
00665         $oOrders = oxNew( 'oxlist' );
00666         $oOrders->init( 'oxorder' );
00667 
00668         if ( $iLimit !== false ) {
00669             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00670         }
00671 
00672         //P
00673         // Lists does not support loading from two tables, so orders
00674         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00675         // forcing to load product info which is used in templates
00676         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00677 
00678         //loading order for registered user
00679         if ( $this->oxuser__oxregister->value > 1 ) {
00680             $oDb = oxDb::getDb();
00681             $sQ = 'select * from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' and oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value ) . ' ';
00682 
00683             //#1546 - shopid check added, if it is not multishop
00684 
00685             $sQ .= ' order by oxorderdate desc ';
00686             $oOrders->selectString( $sQ );
00687         }
00688 
00689         return $oOrders;
00690     }
00691 
00697     public function getOrderCount()
00698     {
00699         $iCnt = 0;
00700         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00701             $oDb = oxDb::getDb();
00702             $sQ  = 'select count(*) from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00703             $iCnt = (int) $oDb->getOne( $sQ );
00704         }
00705 
00706         return $iCnt;
00707     }
00708 
00714     public function getNoticeListArtCnt()
00715     {
00716         if ( $this->_iCntNoticeListArticles === null ) {
00717             $this->_iCntNoticeListArticles = 0;
00718             if ( $this->getId() ) {
00719                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00720             }
00721         }
00722         return $this->_iCntNoticeListArticles;
00723     }
00724 
00730     public function getWishListArtCnt()
00731     {
00732         if ( $this->_iCntWishListArticles === null ) {
00733             $this->_iCntWishListArticles = false;
00734             if ( $this->getId() ) {
00735                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00736             }
00737         }
00738         return $this->_iCntWishListArticles;
00739     }
00740 
00746     public function getEncodedDeliveryAddress()
00747     {
00748         return md5($this->_getMergedAddressFields());
00749     }
00750 
00757     public function getActiveCountry()
00758     {
00759         $sDeliveryCountry = '';
00760         $soxAddressId = oxSession::getVar( 'deladrid' );
00761         if ( $soxAddressId ) {
00762             $oDelAddress = oxNew( 'oxaddress' );
00763             $oDelAddress->load( $soxAddressId );
00764             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00765         } elseif ( $this->getId() ) {
00766             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00767         } else {
00768             $oUser = oxNew( 'oxuser' );
00769             if ( $oUser->loadActiveUser() ) {
00770                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00771             }
00772         }
00773 
00774         return $sDeliveryCountry;
00775     }
00776 
00784     public function createUser()
00785     {
00786         $oDb = oxDb::getDb();
00787         $sShopID = $this->getConfig()->getShopId();
00788 
00789         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00790         $sSelect = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00791         if ( !$this->_blMallUsers ) {
00792             $sSelect .= " and oxshopid = '{$sShopID}' ";
00793         }
00794         $sOXID = $oDb->getOne( $sSelect, false, false );
00795 
00796         // user without password found - lets use
00797         if ( isset( $sOXID ) && $sOXID ) {
00798             // try to update
00799             $this->delete( $sOXID );
00800         } elseif ( $this->_blMallUsers ) {
00801             // must be sure if there is no dublicate user
00802             $sQ = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00803             if ( $oDb->getOne( $sQ, false, false ) ) {
00804                 $oEx = oxNew( 'oxUserException' );
00805                 $oLang = oxRegistry::getLang();
00806                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00807                 throw $oEx;
00808             }
00809         }
00810 
00811         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00812         if ( ( $blOK = $this->save() ) ) {
00813             // dropping/cleaning old delivery address/payment info
00814             $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00815             $oDb->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00816         } else {
00817             $oEx = oxNew( 'oxUserException' );
00818             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00819             throw $oEx;
00820         }
00821 
00822         return $blOK;
00823     }
00824 
00832     public function addToGroup( $sGroupID )
00833     {
00834         if ( !$this->inGroup( $sGroupID ) ) {
00835             // create oxgroup object
00836             $oGroup = oxNew('oxGroups');
00837             if ( $oGroup->load($sGroupID) ) {
00838                 $oNewGroup = oxNew( 'oxobject2group' );
00839                 $oNewGroup->oxobject2group__oxobjectid = new oxField( $this->getId(), oxField::T_RAW );
00840                 $oNewGroup->oxobject2group__oxgroupsid = new oxField( $sGroupID, oxField::T_RAW );
00841                 if ( $oNewGroup->save() ) {
00842                     $this->_oGroups[$sGroupID] = $oGroup;
00843                     return true;
00844                 }
00845             }
00846         }
00847         return false;
00848     }
00849 
00857     public function removeFromGroup( $sGroupID = null )
00858     {
00859         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00860             $oGroups = oxNew( 'oxlist' );
00861             $oGroups->init( 'oxobject2group' );
00862             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00863             $oGroups->selectString( $sSelect );
00864             foreach ( $oGroups as $oRemgroup ) {
00865                 if ( $oRemgroup->delete() ) {
00866                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00867                 }
00868             }
00869         }
00870     }
00871 
00880     public function onOrderExecute( $oBasket, $iSuccess )
00881     {
00882 
00883         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00884             //adding user to particular customer groups
00885             if ( !$this->oxuser__oxdisableautogrp->value ) {
00886 
00887                 $myConfig = $this->getConfig();
00888                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00889                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00890 
00891                 $this->addToGroup( 'oxidcustomer' );
00892                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00893                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00894                     $this->addToGroup( 'oxidsmallcust' );
00895                 }
00896                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00897                     $this->addToGroup( 'oxidmiddlecust' );
00898                 }
00899                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00900                     $this->addToGroup( 'oxidgoodcust' );
00901                 }
00902             }
00903 
00904             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00905                 $this->removeFromGroup( 'oxidnotyetordered' );
00906             }
00907         }
00908     }
00909 
00917     public function getBasket( $sName )
00918     {
00919         if ( !isset( $this->_aBaskets[$sName] ) ) {
00920             $oBasket = oxNew( 'oxuserbasket' );
00921             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00922 
00923             // creating if it does not exist
00924             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00925                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00926                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00927 
00928                 // marking basket as new (it will not be saved in DB yet)
00929                 $oBasket->setIsNewBasket();
00930             }
00931 
00932             $this->_aBaskets[$sName] = $oBasket;
00933         }
00934 
00935         return $this->_aBaskets[$sName];
00936     }
00937 
00946     public function convertBirthday( $aData )
00947     {
00948 
00949         // preparing data to process
00950         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00951         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00952         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00953 
00954         // leaving empty if not set
00955         if ( !$iYear && !$iMonth && !$iDay )
00956             return "";
00957 
00958         // year
00959         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00960             $iYear = date('Y');
00961 
00962         // month
00963         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00964             $iMonth = 1;
00965 
00966         // maximum nuber of days in month
00967         $iMaxDays = 31;
00968         switch( $iMonth) {
00969             case 2 :
00970                 if ($iMaxDays > 28)
00971                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00972                 break;
00973             case 4  :
00974             case 6  :
00975             case 9  :
00976             case 11 :
00977                 $iMaxDays = min(30, $iMaxDays);
00978                 break;
00979         }
00980 
00981         // day
00982         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00983             $iDay = 1;
00984         }
00985 
00986         // whole date
00987         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00988     }
00989 
00995     public function getBoni()
00996     {
00997         if ( ! $iBoni = $this->getConfig()->getConfigParam( 'iCreditRating' ) ) {
00998             $iBoni = 1000;
00999         }
01000 
01001         return $iBoni;
01002     }
01003 
01016     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
01017     {
01018         // preparing input
01019         $sDynGoup = strtolower( trim( $sDynGoup ) );
01020 
01021         // setting denied groups from admin settings also
01022         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
01023 
01024         // default state ..
01025         $blAdd = false;
01026 
01027         // user assignment to dyn group is not allowed
01028         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
01029             $blAdd = false;
01030         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
01031             // trying to add user to prohibited user group?
01032             $blAdd = false;
01033         } elseif ( $this->addToGroup( $sDynGoup ) ) {
01034             $blAdd = true;
01035         }
01036 
01037         // cleanup
01038         oxSession::deleteVar( 'dgr' );
01039 
01040         return $blAdd;
01041     }
01042 
01061     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01062     {
01063         // 1. checking user name
01064         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01065 
01066         // 2. cheking email
01067         $this->_checkEmail( $sLogin );
01068 
01069         // 3. password
01070         $this->checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01071 
01072         // 4. required fields
01073         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01074 
01075         // 5. country check
01076         $this->_checkCountries( $aInvAddress, $aDelAddress );
01077 
01078         // 6. vat id check.
01079             $this->_checkVatId( $aInvAddress );
01080 
01081 
01082         // throwing first validation error
01083         if ( $oError = oxRegistry::get("oxInputValidator")->getFirstValidationError() ) {
01084             throw $oError;
01085         }
01086     }
01087 
01097     public function setNewsSubscription( $blSubscribe, $blSendOptIn, $blForceCheckOptIn = false )
01098     {
01099         // assigning to newsletter
01100         $blSuccess = false;
01101         $myConfig  = $this->getConfig();
01102         $mySession = $this->getSession();
01103 
01104         // user wants to get newsletter messages or no ?
01105         $oNewsSubscription = $this->getNewsSubscription();
01106         if ( $oNewsSubscription ) {
01107             if ( $blSubscribe && ($blForceCheckOptIn || ( $iOptInStatus = $oNewsSubscription->getOptInStatus() ) != 1) ) {
01108                 if ( !$blSendOptIn ) {
01109 
01110                     // double-opt-in check is disabled - assigning automatically
01111                     $this->addToGroup( 'oxidnewsletter' );
01112                     // and setting subscribed status
01113                     $oNewsSubscription->setOptInStatus( 1 );
01114                     $blSuccess = true;
01115                 } else {
01116 
01117                     // double-opt-in check enabled - sending confirmation email and setting waiting status
01118                     if ( $iOptInStatus != 2 ) {
01119                         // sending double-opt-in mail
01120                         $oEmail = oxNew( 'oxemail' );
01121                         $blSuccess = $oEmail->sendNewsletterDbOptInMail( $this );
01122                     } else {
01123                         // mail already was sent, so just confirming that
01124                         $blSuccess = true;
01125                     }
01126 
01127                     $oNewsSubscription->setOptInStatus( 2 );
01128                 }
01129             } elseif ( !$blSubscribe ) {
01130                 // removing user from newsletter subscribers
01131                 $this->removeFromGroup( 'oxidnewsletter' );
01132                 $oNewsSubscription->setOptInStatus( 0 );
01133                 $blSuccess = true;
01134             }
01135         }
01136 
01137         return $blSuccess;
01138     }
01139 
01157     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01158     {
01159 
01160         // validating values before saving. If validation fails - exception is thrown
01161         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01162         // input data is fine - lets save updated user info
01163 
01164 
01165 
01166         $this->assign( $aInvAddress );
01167 
01168 
01169 
01170 
01171 
01172         // update old or add new delivery address
01173         $this->_assignAddress( $aDelAddress );
01174 
01175 
01176 
01177 
01178         // saving new values
01179         if ( $this->save() ) {
01180 
01181             // assigning automatically to specific groups
01182             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01183             $this->_setAutoGroups( $sCountryId );
01184         }
01185     }
01186 
01192     protected function _getMergedAddressFields()
01193     {
01194         $sDelAddress = '';
01195         $sDelAddress .= $this->oxuser__oxcompany;
01196         $sDelAddress .= $this->oxuser__oxusername;
01197         $sDelAddress .= $this->oxuser__oxfname;
01198         $sDelAddress .= $this->oxuser__oxlname;
01199         $sDelAddress .= $this->oxuser__oxstreet;
01200         $sDelAddress .= $this->oxuser__oxstreetnr;
01201         $sDelAddress .= $this->oxuser__oxaddinfo;
01202         $sDelAddress .= $this->oxuser__oxustid;
01203         $sDelAddress .= $this->oxuser__oxcity;
01204         $sDelAddress .= $this->oxuser__oxcountryid;
01205         $sDelAddress .= $this->oxuser__oxstateid;
01206         $sDelAddress .= $this->oxuser__oxzip;
01207         $sDelAddress .= $this->oxuser__oxfon;
01208         $sDelAddress .= $this->oxuser__oxfax;
01209         $sDelAddress .= $this->oxuser__oxsal;
01210 
01211         return $sDelAddress;
01212     }
01213 
01221     protected function _assignAddress( $aDelAddress )
01222     {
01223         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01224 
01225             $sAddressId = $this->getConfig()->getRequestParameter( 'oxaddressid' );
01226             $sAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01227 
01228             $oAddress = oxNew( 'oxaddress' );
01229             $oAddress->setId( $sAddressId );
01230             $oAddress->load( $sAddressId );
01231             $oAddress->assign( $aDelAddress );
01232             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01233             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01234             $oAddress->save();
01235 
01236             // resetting addresses
01237             $this->_aAddresses = null;
01238 
01239             // saving delivery Address for later use
01240             oxSession::setVar( 'deladrid', $oAddress->getId() );
01241         } else {
01242             // resetting
01243             oxSession::setVar( 'deladrid', null );
01244         }
01245     }
01246 
01257     protected function _getLoginQuery( $sUser, $sPassword, $sShopID, $blAdmin )
01258     {
01259         $myConfig = $this->getConfig();
01260         $oDb = oxDb::getDb();
01261 
01262         $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01263         $sPassSelect = " oxuser.oxpassword = BINARY MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01264         $sShopSelect = "";
01265 
01266 
01267         // admin view: can only login with higher than 'user' rights
01268         if ( $blAdmin ) {
01269             $sShopSelect = " and ( oxrights != 'user' ) ";
01270         }
01271 
01272         $blStagingMode = false;
01273         $blDemoMode = false;
01274         $sWhat = "oxid";
01275             if ($myConfig->isDemoShop()) {
01276                 $blDemoMode = true;
01277             }
01278 
01279         $sSelect = "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01280         if ( ( $blDemoMode || $blStagingMode ) && $blAdmin ) {
01281             if ( $sPassword == "admin" && $sUser == "admin" ) {
01282                 $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' ";
01283             } elseif ( $blDemoMode ) {
01284                 $oEx = oxNew( 'oxUserException' );
01285                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01286                 throw $oEx;
01287             }
01288         }
01289 
01290         return $sSelect;
01291     }
01292 
01302     protected function _getShopSelect( $myConfig, $sShopID, $blAdmin )
01303     {
01304         $sShopSelect = "";
01305         // admin view: can only login with higher than 'user' rights
01306         if ( $blAdmin ) {
01307             $sShopSelect = " and ( oxrights != 'user' ) ";
01308         }
01309 
01310         return $sShopSelect;
01311     }
01319     protected function _loadSavedUserBasketAfterLogin()
01320     {
01321         if ( !$this->isAdmin() && !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' )) {
01322             //load basket from the database
01323             try {
01324                 if ( $oBasket = $this->getSession()->getBasket() ) {
01325                     $oBasket->load();
01326                 }
01327             } catch ( Exception $oE ) {
01328                 //just ignore it
01329             }
01330         }
01331     }
01332 
01333 
01346     public function login( $sUser, $sPassword, $blCookie = false)
01347     {
01348         if ( $this->isAdmin() && !count( oxRegistry::get("oxUtilsServer")->getOxCookie() ) ) {
01349             $oEx = oxNew( 'oxCookieException' );
01350             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01351             throw $oEx;
01352         }
01353 
01354         $myConfig = $this->getConfig();
01355         if ( $sPassword ) {
01356 
01357             $sShopID = $myConfig->getShopId();
01358             $sSelect = $this->_getLoginQuery( $sUser, $sPassword, $sShopID, $this->isAdmin() );
01359 
01360             // load from DB
01361             $aData = oxDb::getDb()->getAll( $sSelect );
01362             $sOXID = @$aData[0][0];
01363             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01364 
01365                 if ( !$this->load( $sOXID ) ) {
01366                     $oEx = oxNew( 'oxUserException' );
01367                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01368                     throw $oEx;
01369                 }
01370             }
01371         }
01372 
01373 
01374         //login successfull?
01375         if ( $this->oxuser__oxid->value ) {
01376             // yes, successful login
01377 
01378             //reseting active user
01379             $this->setUser( null );
01380 
01381             if ( $this->isAdmin() ) {
01382                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01383             } else {
01384                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01385             }
01386 
01387             // cookie must be set ?
01388             if ( $blCookie && $myConfig->getConfigParam( 'blShowRememberMe' ) ) {
01389                 oxRegistry::get("oxUtilsServer")->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value );
01390             }
01391 
01392             return true;
01393         } else {
01394             $oEx = oxNew( 'oxUserException' );
01395             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01396             throw $oEx;
01397         }
01398     }
01399 
01405     public function logout()
01406     {
01407         // deleting session info
01408         oxSession::deleteVar( 'usr' );  // for front end
01409         oxSession::deleteVar( 'auth' ); // for back end
01410         oxSession::deleteVar( 'dgr' );
01411         oxSession::deleteVar( 'dynvalue' );
01412         oxSession::deleteVar( 'paymentid' );
01413         // oxSession::deleteVar( 'deladrid' );
01414 
01415         // delete cookie
01416         oxRegistry::get("oxUtilsServer")->deleteUserCookie( $this->getConfig()->getShopID() );
01417 
01418         // unsetting global user
01419         $this->setUser( null );
01420 
01421         return true;
01422     }
01423 
01430     public function loadAdminUser()
01431     {
01432         return $this->loadActiveUser( true );
01433     }
01434 
01443     public function loadActiveUser( $blForceAdmin = false )
01444     {
01445         $oConfig = $this->getConfig();
01446 
01447         $blAdmin = $this->isAdmin() || $blForceAdmin;
01448 
01449         // first - checking session info
01450         $sUserID = $blAdmin ? oxRegistry::getSession()->getVariable( 'auth' ) : oxRegistry::getSession()->getVariable( 'usr' );
01451 
01452         // trying automatic login (by 'remember me' cookie)
01453         $blFoundInCookie = false;
01454         if ( !$sUserID && !$blAdmin && $oConfig->getConfigParam('blShowRememberMe') ) {
01455             $sUserID = $this->_getCookieUserId();
01456             $blFoundInCookie = $sUserID? true : false;
01457         }
01458 
01459         // If facebook connection is enabled, trying to login user using Facebook ID
01460         if ( !$sUserID && !$blAdmin && $oConfig->getConfigParam( "bl_showFbConnect") ) {
01461             $sUserID = $this->_getFacebookUserId();
01462         }
01463 
01464         // checking user results
01465         if ( $sUserID ) {
01466             if ( $this->load( $sUserID ) ) {
01467                 // storing into session
01468                 if ($blAdmin) {
01469                     oxRegistry::getSession()->setVariable( 'auth', $sUserID );
01470                 } else {
01471                     oxRegistry::getSession()->setVariable( 'usr', $sUserID );
01472                 }
01473 
01474                 // marking the way user was loaded
01475                 $this->_blLoadedFromCookie = $blFoundInCookie;
01476                 return true;
01477             }
01478         } else {
01479             // no user
01480             if ($blAdmin) {
01481                 oxRegistry::getSession()->deleteVariable( 'auth' );
01482             } else {
01483                 oxRegistry::getSession()->deleteVariable( 'usr' );
01484             }
01485 
01486             return false;
01487         }
01488     }
01489 
01495     protected function _getFacebookUserId()
01496     {
01497         $oDb = oxDb::getDb();
01498         $oFb = oxRegistry::get("oxFb");
01499         $oConfig = $this->getConfig();
01500         if ( $oFb->isConnected() && $oFb->getUser() ) {
01501             $sUserSelect = "oxuser.oxfbid = " . $oDb->quote( $oFb->getUser() );
01502             $sShopSelect = "";
01503 
01504 
01505             $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01506             $sUserID = $oDb->getOne( $sSelect );
01507         }
01508         return $sUserID;
01509     }
01510 
01516     protected function _getCookieUserId()
01517     {
01518         $oConfig = $this->getConfig();
01519         $sShopID = $oConfig->getShopId();
01520         if ( ( $sSet = oxRegistry::get("oxUtilsServer")->getUserCookie( $sShopID ) ) ) {
01521             $oDb = oxDb::getDb();
01522             $aData = explode( '@@@', $sSet );
01523             $sUser = $aData[0];
01524             $sPWD  = @$aData[1];
01525 
01526             $sSelect =  'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDb->quote($sUser);
01527 
01528             $rs = $oDb->select( $sSelect );
01529             if ( $rs != false && $rs->recordCount() > 0 ) {
01530                 while (!$rs->EOF) {
01531                     $sTest = crypt( $rs->fields[1], $rs->fields[2] );
01532                     if ( $sTest == $sPWD ) {
01533                         // found
01534                         $sUserID = $rs->fields[0];
01535                         break;
01536                     }
01537                     $rs->moveNext();
01538                 }
01539             }
01540             // if cookie info is not valid, remove it.
01541             if ( !$sUserID ) {
01542                 oxRegistry::get('oxUtilsServer')->deleteUserCookie( $sShopID );
01543             }
01544         }
01545         return $sUserID;
01546     }
01547 
01560     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01561     {
01562         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01563         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01564 
01565         // maybe this is LDAP user but supplied email Address instead of LDAP login
01566         $oDb = oxDb::getDb();
01567         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01568         if ( isset( $sLDAPKey) && $sLDAPKey) {
01569             $sUser = $sLDAPKey;
01570         }
01571 
01572         //$throws oxConnectionException
01573         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01574 
01575         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01576         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01577             // login successful
01578 
01579             // check if user is already in database
01580             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01581             $sOXID = $oDb->getOne( $sSelect );
01582 
01583             if ( !isset( $sOXID ) || !$sOXID ) {
01584                 // we need to create a new user
01585                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01586                 $this->setId();
01587 
01588                 // map all user data fields
01589                 foreach ( $aData as $fldname => $value) {
01590                     $sField = "oxuser__".strtolower( $fldname);
01591                     $this->$sField = new oxField( $aData[$fldname] );
01592                 }
01593 
01594                 $this->oxuser__oxactive  = new oxField( 1 );
01595                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01596                 $this->oxuser__oxldapkey = new oxField( $sUser );
01597                 $this->oxuser__oxrights  = new oxField( "user" );
01598                 $this->setPassword( "ldap user" );
01599 
01600                 $this->save();
01601             } else {
01602                 // LDAP user is already in OXID DB, load it
01603                 $this->load( $sOXID);
01604             }
01605 
01606         } else {
01607             $oEx = oxNew( 'oxUserException' );
01608             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01609             throw $oEx;
01610         }
01611     }
01612 
01619     protected function _getUserRights()
01620     {
01621         // previously user had no rights defined
01622         if ( !$this->oxuser__oxrights->value )
01623             return 'user';
01624 
01625         $oDb = oxDb::getDb();
01626         $myConfig    = $this->getConfig();
01627         $sAuthRights = null;
01628 
01629         // choosing possible user rights index
01630         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01631         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01632         if ( $sAuthUserID ) {
01633             $sAuthRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $sAuthUserID ) );
01634         }
01635 
01636         //preventing user rights edit for non admin
01637         $aRights = array();
01638 
01639         // selecting current users rights ...
01640         if ( $sCurrRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $this->getId() ) ) ) {
01641             $aRights[] = $sCurrRights;
01642         }
01643         $aRights[] = 'user';
01644 
01645         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01646             return current( $aRights );
01647         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01648             $aRights[] = $sAuthRights;
01649             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01650                 return current( $aRights );
01651             }
01652         }
01653 
01654         // leaving as it was set ...
01655         return $this->oxuser__oxrights->value;
01656     }
01657 
01663     protected function _insert()
01664     {
01665 
01666         // set oxcreate date
01667         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01668 
01669         if ( !isset( $this->oxuser__oxboni->value ) ) {
01670             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01671         }
01672 
01673         return parent::_insert();
01674     }
01675 
01681     protected function _update()
01682     {
01683         //V #M418: for not registered users, don't change boni during update
01684         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01685             $this->_aSkipSaveFields[] = 'oxboni';
01686         }
01687 
01688         // don't change this field
01689         $this->_aSkipSaveFields[] = 'oxcreate';
01690         if ( !$this->isAdmin() ) {
01691             $this->_aSkipSaveFields[] = 'oxcustnr';
01692             $this->_aSkipSaveFields[] = 'oxrights';
01693         }
01694 
01695         // updating subscription information
01696         if ( ( $blUpdate = parent::_update() ) ) {
01697             $this->getNewsSubscription()->updateSubscription( $this );
01698         }
01699 
01700         return $blUpdate;
01701     }
01702 
01718     protected function _checkLogin( $sLogin, $aInvAddress )
01719     {
01720         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01721         oxRegistry::get("oxInputValidator")->checkLogin( $this, $sLogin, $aInvAddress );
01722         return $sLogin;
01723     }
01724 
01732     public function checkIfEmailExists( $sEmail )
01733     {
01734         $myConfig = $this->getConfig();
01735         $oDb = oxDb::getDb();
01736         $iShopId = $myConfig->getShopId();
01737         $blExists = false;
01738 
01739         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDb->quote( $sEmail );
01740         if ( ( $sOxid = $this->getId() ) ) {
01741             $sQ .= " and oxid <> ".$oDb->quote( $sOxid );
01742         }
01743         $oRs = $oDb->select( $sQ, false, false );
01744         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01745 
01746             if ( $this->_blMallUsers ) {
01747 
01748                 $blExists = true;
01749                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01750 
01751                     // password is not set - allow to override
01752                     $blExists = false;
01753                 }
01754             } else {
01755 
01756                 $blExists = false;
01757                 while ( !$oRs->EOF ) {
01758                     if ( $oRs->fields[1] != 'user' ) {
01759 
01760                         // exists admin with same login - must not allow
01761                         $blExists = true;
01762                         break;
01763                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01764 
01765                         // exists same login (with password) in same shop
01766                         $blExists = true;
01767                         break;
01768                     }
01769 
01770                     $oRs->moveNext();
01771                 }
01772             }
01773         }
01774         return $blExists;
01775     }
01776 
01784     public function getUserRecommLists( $sOXID = null )
01785     {
01786         if ( !$sOXID )
01787             $sOXID = $this->getId();
01788 
01789         // sets active page
01790         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01791         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01792 
01793         // load only lists which we show on screen
01794         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01795         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01796 
01797 
01798         $oRecommList = oxNew( 'oxlist' );
01799         $oRecommList->init( 'oxrecommlist' );
01800         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01801         $iShopId = $this->getConfig()->getShopId();
01802         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01803         $oRecommList->selectString( $sSelect );
01804 
01805         return $oRecommList;
01806     }
01807 
01815     public function getRecommListsCount( $sOx = null )
01816     {
01817         if ( !$sOx ) {
01818             $sOx = $this->getId();
01819         }
01820 
01821         if ( $this->_iCntRecommLists === null || $sOx ) {
01822             $oDb = oxDb::getDb();
01823             $this->_iCntRecommLists = 0;
01824             $iShopId = $this->getConfig()->getShopId();
01825             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01826             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01827         }
01828         return $this->_iCntRecommLists;
01829     }
01830 
01841     protected function _checkEmail( $sEmail )
01842     {
01843         oxRegistry::get("oxInputValidator")->checkEmail( $this, $sEmail );
01844     }
01845 
01858     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01859     {
01860         return oxRegistry::get("oxInputValidator")->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01861     }
01862 
01873     protected function _checkCountries( $aInvAddress, $aDelAddress )
01874     {
01875         oxRegistry::get("oxInputValidator")->checkCountries( $this, $aInvAddress, $aDelAddress );
01876     }
01877 
01889     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01890     {
01891         oxRegistry::get("oxInputValidator")->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01892     }
01893 
01904     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01905     {
01906         oxRegistry::get("oxInputValidator")->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01907     }
01908 
01919     protected function _checkVatId( $aInvAddress )
01920     {
01921         oxRegistry::get("oxInputValidator")->checkVatId( $this, $aInvAddress );
01922     }
01923 
01932     protected function _setAutoGroups( $sCountryId )
01933     {
01934         // assigning automatically to specific groups
01935         $blForeigner = true;
01936         $blForeignGroupExists = false;
01937         $blInlandGroupExists = false;
01938 
01939         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01940         // foreigner ?
01941         if ( is_array($aHomeCountry)) {
01942             if (in_array($sCountryId, $aHomeCountry)) {
01943                 $blForeigner = false;
01944             }
01945         } elseif ($sCountryId == $aHomeCountry) {
01946             $blForeigner = false;
01947         }
01948 
01949         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01950             $blForeignGroupExists = true;
01951             if ( !$blForeigner ) {
01952                 $this->removeFromGroup( 'oxidforeigncustomer' );
01953             }
01954         }
01955 
01956         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01957             $blInlandGroupExists = true;
01958             if ( $blForeigner ) {
01959                 $this->removeFromGroup( 'oxidnewcustomer' );
01960             }
01961         }
01962 
01963         if ( !$this->oxuser__oxdisableautogrp->value ) {
01964             if ( !$blForeignGroupExists && $blForeigner ) {
01965                 $this->addToGroup( 'oxidforeigncustomer' );
01966             }
01967             if ( !$blInlandGroupExists && !$blForeigner ) {
01968                 $this->addToGroup( 'oxidnewcustomer' );
01969             }
01970         }
01971     }
01972 
01973 
01982     public function loadUserByUpdateId( $sUid )
01983     {
01984         $oDb = oxDb::getDb();
01985         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
01986         if ( $sUserId = $oDb->getOne( $sQ ) ) {
01987             return $this->load( $sUserId );
01988         }
01989     }
01990 
01998     public function setUpdateKey( $blReset = false )
01999     {
02000         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02001         $iUpTime = $blReset ? 0 : oxRegistry::get("oxUtilsDate")->getTime() + $this->getUpdateLinkTerm();
02002 
02003         // generating key
02004         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02005 
02006         // setting expiration time for 6 hours
02007         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02008 
02009         // saving
02010         $this->save();
02011     }
02012 
02018     public function getUpdateLinkTerm()
02019     {
02020         return 3600 * 6;
02021     }
02022 
02030     public function isExpiredUpdateId( $sKey )
02031     {
02032         $oDb = oxDb::getDb();
02033         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02034         return !( (bool) $oDb->getOne( $sQ ) );
02035     }
02036 
02042     public function getUpdateId()
02043     {
02044         if ( $this->_sUpdateKey === null ) {
02045             $this->setUpdateKey();
02046             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02047         }
02048         return $this->_sUpdateKey;
02049     }
02050 
02059     public function encodePassword( $sPassword, $sSalt )
02060     {
02061         $oDb = oxDb::getDb();
02062         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02063     }
02064 
02072     public function prepareSalt( $sSalt )
02073     {
02074         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02075     }
02076 
02084     public function decodeSalt( $sSaltHex )
02085     {
02086         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02087     }
02088 
02096     public function setPassword( $sPassword = null )
02097     {
02098         // setting salt if password is not empty
02099         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02100 
02101         // encoding only if password was not empty (e.g. user registration without pass)
02102         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02103 
02104         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02105         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02106     }
02107 
02115     public function isSamePassword( $sNewPass )
02116     {
02117         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02118     }
02119 
02125     public function isLoadedFromCookie()
02126     {
02127         return $this->_blLoadedFromCookie;
02128     }
02129 
02136     public function getPasswordHash()
02137     {
02138         $sHash = null;
02139         if ( $this->oxuser__oxpassword->value ) {
02140             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02141                 // decodable pass ?
02142                 $this->setPassword( oxRegistry::getUtils()->strRem( $this->oxuser__oxpassword->value ) );
02143             } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02144                 // plain pass ?
02145                 $this->setPassword( $this->oxuser__oxpassword->value );
02146             }
02147             $sHash = $this->oxuser__oxpassword->value;
02148         }
02149         return $sHash;
02150     }
02151 
02159     public function getReviewUserHash( $sUserId )
02160     {
02161         $oDb = oxDb::getDb();
02162         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02163         return $sReviewUserHash;
02164     }
02165 
02173     public function getReviewUserId( $sReviewUserHash )
02174     {
02175         $oDb = oxDb::getDb();
02176         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02177         return $sUserId;
02178     }
02179 
02185     public function getState()
02186     {
02187         return $this->oxuser__oxstateid->value;
02188     }
02189 
02195     public function isTermsAccepted()
02196     {
02197         $oDb = oxDb::getDb();
02198         $sShopId = $this->getConfig()->getShopId();
02199         $sUserId = $oDb->quote( $this->getId() );
02200         return (bool) $oDb->getOne( "select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'" );
02201     }
02202 
02208     public function acceptTerms()
02209     {
02210         $oDb = oxDb::getDb();
02211         $sUserId  = $oDb->quote( $this->getId() );
02212         $sShopId  = $this->getConfig()->getShopId();
02213         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02214 
02215         $oDb->execute( "replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02216     }
02217 
02227     public function setCreditPointsForRegistrant( $sUserId, $sRecEmail )
02228     {
02229         $blSet   = false;
02230         $oDb = oxDb::getDb();
02231         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02232         // check if this invitation is still not accepted
02233         $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);
02234         if ( $iPoints && $iPending ) {
02235             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02236             if ( $blSet = $this->save() ) {
02237                 // updating users statistics
02238                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail ) );
02239                 $oInvUser = oxNew( "oxuser" );
02240                 if ( $oInvUser->load( $sUserId ) ) {
02241                     $blSet = $oInvUser->setCreditPointsForInviter();
02242                 }
02243             }
02244         }
02245         oxSession::deleteVar( 'su' );
02246         oxSession::deleteVar( 're' );
02247 
02248         return $blSet;
02249     }
02250 
02256     public function setCreditPointsForInviter()
02257     {
02258         $blSet   = false;
02259         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02260         if ( $iPoints ) {
02261             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02262             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02263             $blSet = $this->save();
02264         }
02265 
02266         return $blSet;
02267     }
02268 
02274     public function updateFbId()
02275     {
02276         $oFb = oxRegistry::get("oxFb");
02277         $blRet = false;
02278 
02279         if ( $oFb->isConnected() && $oFb->getUser() ) {
02280              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02281              $blRet = $this->save();
02282         }
02283 
02284         return $blRet;
02285     }
02286 
02294     public function updateInvitationStatistics( $aRecEmail )
02295     {
02296         $oDb = oxDb::getDb();
02297         $sUserId = $this->getId();
02298 
02299         if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
02300             //iserting statistics about invitation
02301             $sDate = oxRegistry::get("oxUtilsDate")->formatDBDate( date("Y-m-d"), true );
02302             $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
02303             foreach ( $aRecEmail as $sRecEmail ) {
02304                 $sSql = "INSERT INTO oxinvitations SET oxuserid = ".$oDb->quote( $sUserId ).", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02305                 $oDb->execute( $sSql );
02306             }
02307         }
02308     }
02309 
02317     public function getIdByUserName( $sUserName )
02318     {
02319         $oDb = oxDb::getDb();
02320         $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = ". $oDb->quote( $sUserName );
02321         if ( !$this->getConfig()->getConfigParam( 'blMallUsers' ) ) {
02322             $sQ .= " AND `oxshopid` = ". $oDb->quote( $this->getConfig()->getShopId() );
02323         }
02324 
02325         return $oDb->getOne( $sQ );
02326 
02327     }
02328 
02334     public function hasAccount()
02335     {
02336 
02337         return (bool) $this->oxuser__oxpassword->value;
02338 
02339     }
02340 
02346     public function isPriceViewModeNetto()
02347     {
02348         return (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
02349     }
02350 
02351 }