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', null, true ), 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         if (!$sOXID) {
00621             $sOXID = $this->getId();
00622         }
00623         //#5901 if physical record exists return true unconditionally
00624         if (parent::exists($sOXID)) {
00625             $this->setId($sOXID);
00626             return true;
00627         }
00628 
00629         //additional username check
00630         //This part is used by not yet saved user object, to detect the case when such username exists in db.
00631         //Basically it is called when anonymous visitor enters existing username for newsletter subscription
00632         //see Newsletter::send()
00633         //TODO: transfer this validation to newsletter part
00634         $sShopSelect = '';
00635         if (!$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00636             $sShopSelect = ' AND oxshopid = "' . $this->getConfig()->getShopId() . '" ';
00637         }
00638         $oDb = oxDb::getDb();
00639         $sSelect = 'SELECT oxid FROM ' . $this->getViewName() . '
00640                     WHERE ( oxusername = ' . $oDb->quote($this->oxuser__oxusername->value) . ' ) ';
00641         $sSelect .= $sShopSelect;
00642 
00643         if (($sOxid = $oDb->getOne($sSelect, false, false))) {
00644             // update - set oxid
00645             $this->setId($sOxid);
00646 
00647             return true;
00648         }
00649         return false;
00650     }
00651 
00660     public function getOrders( $iLimit = false, $iPage = 0 )
00661     {
00662         $oOrders = oxNew( 'oxlist' );
00663         $oOrders->init( 'oxorder' );
00664 
00665         if ( $iLimit !== false ) {
00666             $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00667         }
00668 
00669         //P
00670         // Lists does not support loading from two tables, so orders
00671         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00672         // forcing to load product info which is used in templates
00673         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00674 
00675         //loading order for registered user
00676         if ( $this->oxuser__oxregister->value > 1 ) {
00677             $oDb = oxDb::getDb();
00678             $sQ = 'select * from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' and oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value ) . ' ';
00679 
00680             //#1546 - shopid check added, if it is not multishop
00681 
00682             $sQ .= ' order by oxorderdate desc ';
00683             $oOrders->selectString( $sQ );
00684         }
00685 
00686         return $oOrders;
00687     }
00688 
00694     public function getOrderCount()
00695     {
00696         $iCnt = 0;
00697         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00698             $oDb = oxDb::getDb();
00699             $sQ  = 'select count(*) from oxorder where oxuserid = '.$oDb->quote( $this->getId() ).' AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00700             $iCnt = (int) $oDb->getOne( $sQ );
00701         }
00702 
00703         return $iCnt;
00704     }
00705 
00711     public function getNoticeListArtCnt()
00712     {
00713         if ( $this->_iCntNoticeListArticles === null ) {
00714             $this->_iCntNoticeListArticles = 0;
00715             if ( $this->getId() ) {
00716                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00717             }
00718         }
00719         return $this->_iCntNoticeListArticles;
00720     }
00721 
00727     public function getWishListArtCnt()
00728     {
00729         if ( $this->_iCntWishListArticles === null ) {
00730             $this->_iCntWishListArticles = false;
00731             if ( $this->getId() ) {
00732                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00733             }
00734         }
00735         return $this->_iCntWishListArticles;
00736     }
00737 
00743     public function getEncodedDeliveryAddress()
00744     {
00745         return md5($this->_getMergedAddressFields());
00746     }
00747 
00754     public function getActiveCountry()
00755     {
00756         $sDeliveryCountry = '';
00757         $soxAddressId = oxSession::getVar( 'deladrid' );
00758         if ( $soxAddressId ) {
00759             $oDelAddress = oxNew( 'oxaddress' );
00760             $oDelAddress->load( $soxAddressId );
00761             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00762         } elseif ( $this->getId() ) {
00763             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00764         } else {
00765             $oUser = oxNew( 'oxuser' );
00766             if ( $oUser->loadActiveUser() ) {
00767                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00768             }
00769         }
00770 
00771         return $sDeliveryCountry;
00772     }
00773 
00781     public function createUser()
00782     {
00783         $oDb = oxDb::getDb();
00784         $sShopID = $this->getConfig()->getShopId();
00785 
00786         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00787         $sSelect = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00788         if ( !$this->_blMallUsers ) {
00789             $sSelect .= " and oxshopid = '{$sShopID}' ";
00790         }
00791         $sOXID = $oDb->getOne( $sSelect, false, false );
00792 
00793         // user without password found - lets use
00794         if ( isset( $sOXID ) && $sOXID ) {
00795             // try to update
00796             $this->delete( $sOXID );
00797         } elseif ( $this->_blMallUsers ) {
00798             // must be sure if there is no dublicate user
00799             $sQ = "select oxid from oxuser where oxusername = " . $oDb->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00800             if ( $oDb->getOne( $sQ, false, false ) ) {
00801                 $oEx = oxNew( 'oxUserException' );
00802                 $oLang = oxRegistry::getLang();
00803                 $oEx->setMessage( sprintf( $oLang->translateString( 'ERROR_MESSAGE_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00804                 throw $oEx;
00805             }
00806         }
00807 
00808         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00809         if ( ( $blOK = $this->save() ) ) {
00810             // dropping/cleaning old delivery address/payment info
00811             $oDb->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00812             $oDb->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDb->quote( $this->oxuser__oxid->value ) . " " );
00813         } else {
00814             $oEx = oxNew( 'oxUserException' );
00815             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00816             throw $oEx;
00817         }
00818 
00819         return $blOK;
00820     }
00821 
00829     public function addToGroup( $sGroupID )
00830     {
00831         if ( !$this->inGroup( $sGroupID ) ) {
00832             // create oxgroup object
00833             $oGroup = oxNew('oxGroups');
00834             if ( $oGroup->load($sGroupID) ) {
00835                 $oNewGroup = oxNew( 'oxobject2group' );
00836                 $oNewGroup->oxobject2group__oxobjectid = new oxField( $this->getId(), oxField::T_RAW );
00837                 $oNewGroup->oxobject2group__oxgroupsid = new oxField( $sGroupID, oxField::T_RAW );
00838                 if ( $oNewGroup->save() ) {
00839                     $this->_oGroups[$sGroupID] = $oGroup;
00840                     return true;
00841                 }
00842             }
00843         }
00844         return false;
00845     }
00846 
00854     public function removeFromGroup( $sGroupID = null )
00855     {
00856         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00857             $oGroups = oxNew( 'oxlist' );
00858             $oGroups->init( 'oxobject2group' );
00859             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00860             $oGroups->selectString( $sSelect );
00861             foreach ( $oGroups as $oRemgroup ) {
00862                 if ( $oRemgroup->delete() ) {
00863                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00864                 }
00865             }
00866         }
00867     }
00868 
00877     public function onOrderExecute( $oBasket, $iSuccess )
00878     {
00879 
00880         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00881             //adding user to particular customer groups
00882             if ( !$this->oxuser__oxdisableautogrp->value ) {
00883 
00884                 $myConfig = $this->getConfig();
00885                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00886                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00887 
00888                 $this->addToGroup( 'oxidcustomer' );
00889                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00890                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00891                     $this->addToGroup( 'oxidsmallcust' );
00892                 }
00893                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00894                     $this->addToGroup( 'oxidmiddlecust' );
00895                 }
00896                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00897                     $this->addToGroup( 'oxidgoodcust' );
00898                 }
00899             }
00900 
00901             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00902                 $this->removeFromGroup( 'oxidnotyetordered' );
00903             }
00904         }
00905     }
00906 
00914     public function getBasket( $sName )
00915     {
00916         if ( !isset( $this->_aBaskets[$sName] ) ) {
00917             $oBasket = oxNew( 'oxuserbasket' );
00918             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00919 
00920             // creating if it does not exist
00921             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00922                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00923                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00924 
00925                 // marking basket as new (it will not be saved in DB yet)
00926                 $oBasket->setIsNewBasket();
00927             }
00928 
00929             $this->_aBaskets[$sName] = $oBasket;
00930         }
00931 
00932         return $this->_aBaskets[$sName];
00933     }
00934 
00943     public function convertBirthday( $aData )
00944     {
00945 
00946         // preparing data to process
00947         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00948         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00949         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00950 
00951         // leaving empty if not set
00952         if ( !$iYear && !$iMonth && !$iDay )
00953             return "";
00954 
00955         // year
00956         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00957             $iYear = date('Y');
00958 
00959         // month
00960         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00961             $iMonth = 1;
00962 
00963         // maximum nuber of days in month
00964         $iMaxDays = 31;
00965         switch( $iMonth) {
00966             case 2 :
00967                 if ($iMaxDays > 28)
00968                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00969                 break;
00970             case 4  :
00971             case 6  :
00972             case 9  :
00973             case 11 :
00974                 $iMaxDays = min(30, $iMaxDays);
00975                 break;
00976         }
00977 
00978         // day
00979         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00980             $iDay = 1;
00981         }
00982 
00983         // whole date
00984         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00985     }
00986 
00992     public function getBoni()
00993     {
00994         if ( ! $iBoni = $this->getConfig()->getConfigParam( 'iCreditRating' ) ) {
00995             $iBoni = 1000;
00996         }
00997 
00998         return $iBoni;
00999     }
01000 
01013     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
01014     {
01015         // preparing input
01016         $sDynGoup = strtolower( trim( $sDynGoup ) );
01017 
01018         // setting denied groups from admin settings also
01019         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
01020 
01021         // default state ..
01022         $blAdd = false;
01023 
01024         // user assignment to dyn group is not allowed
01025         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
01026             $blAdd = false;
01027         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
01028             // trying to add user to prohibited user group?
01029             $blAdd = false;
01030         } elseif ( $this->addToGroup( $sDynGoup ) ) {
01031             $blAdd = true;
01032         }
01033 
01034         // cleanup
01035         oxSession::deleteVar( 'dgr' );
01036 
01037         return $blAdd;
01038     }
01039 
01058     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01059     {
01060         // 1. checking user name
01061         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01062 
01063         // 2. cheking email
01064         $this->_checkEmail( $sLogin );
01065 
01066         // 3. password
01067         $this->checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01068 
01069         // 4. required fields
01070         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01071 
01072         // 5. country check
01073         $this->_checkCountries( $aInvAddress, $aDelAddress );
01074 
01075         // 6. vat id check.
01076             $this->_checkVatId( $aInvAddress );
01077 
01078 
01079         // throwing first validation error
01080         if ( $oError = oxRegistry::get("oxInputValidator")->getFirstValidationError() ) {
01081             throw $oError;
01082         }
01083     }
01084 
01094     public function setNewsSubscription( $blSubscribe, $blSendOptIn, $blForceCheckOptIn = false )
01095     {
01096         // assigning to newsletter
01097         $blSuccess = false;
01098         $myConfig  = $this->getConfig();
01099         $mySession = $this->getSession();
01100 
01101         // user wants to get newsletter messages or no ?
01102         $oNewsSubscription = $this->getNewsSubscription();
01103         if ( $oNewsSubscription ) {
01104             if ( $blSubscribe && ($blForceCheckOptIn || ( $iOptInStatus = $oNewsSubscription->getOptInStatus() ) != 1) ) {
01105                 if ( !$blSendOptIn ) {
01106 
01107                     // double-opt-in check is disabled - assigning automatically
01108                     $this->addToGroup( 'oxidnewsletter' );
01109                     // and setting subscribed status
01110                     $oNewsSubscription->setOptInStatus( 1 );
01111                     $blSuccess = true;
01112                 } else {
01113 
01114                     // double-opt-in check enabled - sending confirmation email and setting waiting status
01115                     if ( $iOptInStatus != 2 ) {
01116                         // sending double-opt-in mail
01117                         $oEmail = oxNew( 'oxemail' );
01118                         $blSuccess = $oEmail->sendNewsletterDbOptInMail( $this );
01119                     } else {
01120                         // mail already was sent, so just confirming that
01121                         $blSuccess = true;
01122                     }
01123 
01124                     $oNewsSubscription->setOptInStatus( 2 );
01125                 }
01126             } elseif ( !$blSubscribe ) {
01127                 // removing user from newsletter subscribers
01128                 $this->removeFromGroup( 'oxidnewsletter' );
01129                 $oNewsSubscription->setOptInStatus( 0 );
01130                 $blSuccess = true;
01131             }
01132         }
01133 
01134         return $blSuccess;
01135     }
01136 
01154     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01155     {
01156 
01157         // validating values before saving. If validation fails - exception is thrown
01158         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01159         // input data is fine - lets save updated user info
01160 
01161 
01162 
01163         $this->assign( $aInvAddress );
01164 
01165 
01166 
01167 
01168 
01169         // update old or add new delivery address
01170         $this->_assignAddress( $aDelAddress );
01171 
01172 
01173 
01174 
01175         // saving new values
01176         if ( $this->save() ) {
01177 
01178             // assigning automatically to specific groups
01179             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01180             $this->_setAutoGroups( $sCountryId );
01181         }
01182     }
01183 
01189     protected function _getMergedAddressFields()
01190     {
01191         $sDelAddress = '';
01192         $sDelAddress .= $this->oxuser__oxcompany;
01193         $sDelAddress .= $this->oxuser__oxusername;
01194         $sDelAddress .= $this->oxuser__oxfname;
01195         $sDelAddress .= $this->oxuser__oxlname;
01196         $sDelAddress .= $this->oxuser__oxstreet;
01197         $sDelAddress .= $this->oxuser__oxstreetnr;
01198         $sDelAddress .= $this->oxuser__oxaddinfo;
01199         $sDelAddress .= $this->oxuser__oxustid;
01200         $sDelAddress .= $this->oxuser__oxcity;
01201         $sDelAddress .= $this->oxuser__oxcountryid;
01202         $sDelAddress .= $this->oxuser__oxstateid;
01203         $sDelAddress .= $this->oxuser__oxzip;
01204         $sDelAddress .= $this->oxuser__oxfon;
01205         $sDelAddress .= $this->oxuser__oxfax;
01206         $sDelAddress .= $this->oxuser__oxsal;
01207 
01208         return $sDelAddress;
01209     }
01210 
01218     protected function _assignAddress( $aDelAddress )
01219     {
01220         if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01221 
01222             $sAddressId = $this->getConfig()->getRequestParameter( 'oxaddressid' );
01223             $sAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01224 
01225             $oAddress = oxNew( 'oxaddress' );
01226             $oAddress->setId( $sAddressId );
01227             $oAddress->load( $sAddressId );
01228             $oAddress->assign( $aDelAddress );
01229             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01230             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01231             $oAddress->save();
01232 
01233             // resetting addresses
01234             $this->_aAddresses = null;
01235 
01236             // saving delivery Address for later use
01237             oxSession::setVar( 'deladrid', $oAddress->getId() );
01238         } else {
01239             // resetting
01240             oxSession::setVar( 'deladrid', null );
01241         }
01242     }
01243 
01254     protected function _getLegacyLoginQuery( $sUser, $sPassword, $sShopID, $blAdmin )
01255     {
01256         $myConfig = $this->getConfig();
01257         $oDb = oxDb::getDb();
01258 
01259         $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01260         $sPassSelect = " oxuser.oxpassword = BINARY MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01261         $sShopSelect = "";
01262 
01263 
01264         // admin view: can only login with higher than 'user' rights
01265         if ( $blAdmin ) {
01266             $sShopSelect = " and ( oxrights != 'user' ) ";
01267         }
01268 
01269         $sSelect = "select `oxid` from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01270 
01271 
01272         return $sSelect;
01273     }
01274 
01287     protected function _getLoginQuery( $sUser, $sPassword, $sShopID, $blAdmin )
01288     {
01289         $myConfig = $this->getConfig();
01290         $oDb = oxDb::getDb();
01291 
01292         $sSalt = $oDb->getOne("SELECT `oxpasssalt` FROM `oxuser` WHERE `oxusername` = " . $oDb->quote($sUser));
01293 
01294         $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01295         $sPassSelect = " oxuser.oxpassword = " . $oDb->quote($this->encodePassword($sPassword, $sSalt) );
01296         $sShopSelect = "";
01297 
01298 
01299         // admin view: can only login with higher than 'user' rights
01300         if ( $blAdmin ) {
01301             $sShopSelect = " and ( oxrights != 'user' ) ";
01302         }
01303 
01304         $sSelect = "select `oxid` from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01305 
01306 
01307         return $sSelect;
01308     }
01309 
01319     protected function _getShopSelect( $myConfig, $sShopID, $blAdmin )
01320     {
01321         $sShopSelect = "";
01322         // admin view: can only login with higher than 'user' rights
01323         if ( $blAdmin ) {
01324             $sShopSelect = " and ( oxrights != 'user' ) ";
01325         }
01326 
01327         return $sShopSelect;
01328     }
01336     protected function _loadSavedUserBasketAfterLogin()
01337     {
01338         if ( !$this->isAdmin() && !$this->getConfig()->getConfigParam( 'blPerfNoBasketSaving' )) {
01339             //load basket from the database
01340             try {
01341                 if ( $oBasket = $this->getSession()->getBasket() ) {
01342                     $oBasket->load();
01343                 }
01344             } catch ( Exception $oE ) {
01345                 //just ignore it
01346             }
01347         }
01348     }
01349 
01364     public function login( $sUser, $sPassword, $blCookie = false)
01365     {
01366         if ( $this->isAdmin() && !count( oxRegistry::get("oxUtilsServer")->getOxCookie() ) ) {
01368             $oEx = oxNew( 'oxCookieException' );
01369             $oEx->setMessage( 'ERROR_MESSAGE_COOKIE_NOCOOKIE' );
01370             throw $oEx;
01371         }
01372 
01373         $oConfig = $this->getConfig();
01374 
01375 
01376         if ( $sPassword ) {
01377 
01378             $sShopID = $oConfig->getShopId();
01379             $this->_dbLogin( $sUser, $sPassword, $sShopID );
01380 
01381         }
01382 
01383 
01384 
01385 
01386 
01387 
01388 
01389         //login successful?
01390         if ( $this->oxuser__oxid->value ) {
01391 
01392             // yes, successful login
01393 
01394             //resetting active user
01395             $this->setUser( null );
01396 
01397             if ( $this->isAdmin() ) {
01398                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01399             } else {
01400                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01401             }
01402 
01403             // cookie must be set ?
01404             if ( $blCookie && $oConfig->getConfigParam( 'blShowRememberMe' ) ) {
01405                 oxRegistry::get("oxUtilsServer")->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $oConfig->getShopId(), 31536000, $this->oxuser__oxpasssalt->value );
01406             }
01407 
01408             return true;
01409         } else {
01411             $oEx = oxNew( 'oxUserException' );
01412             $oEx->setMessage( 'ERROR_MESSAGE_USER_NOVALIDLOGIN' );
01413             throw $oEx;
01414         }
01415     }
01416 
01422     public function logout()
01423     {
01424         // deleting session info
01425         oxSession::deleteVar( 'usr' );  // for front end
01426         oxSession::deleteVar( 'auth' ); // for back end
01427         oxSession::deleteVar( 'dgr' );
01428         oxSession::deleteVar( 'dynvalue' );
01429         oxSession::deleteVar( 'paymentid' );
01430         // oxSession::deleteVar( 'deladrid' );
01431 
01432         // delete cookie
01433         oxRegistry::get("oxUtilsServer")->deleteUserCookie( $this->getConfig()->getShopID() );
01434 
01435         // unsetting global user
01436         $this->setUser( null );
01437 
01438         return true;
01439     }
01440 
01447     public function loadAdminUser()
01448     {
01449         return $this->loadActiveUser( true );
01450     }
01451 
01460     public function loadActiveUser( $blForceAdmin = false )
01461     {
01462         $oConfig = $this->getConfig();
01463 
01464         $blAdmin = $this->isAdmin() || $blForceAdmin;
01465 
01466         // first - checking session info
01467         $sUserID = $blAdmin ? oxRegistry::getSession()->getVariable( 'auth' ) : oxRegistry::getSession()->getVariable( 'usr' );
01468 
01469         // trying automatic login (by 'remember me' cookie)
01470         $blFoundInCookie = false;
01471         if ( !$sUserID && !$blAdmin && $oConfig->getConfigParam('blShowRememberMe') ) {
01472             $sUserID = $this->_getCookieUserId();
01473             $blFoundInCookie = $sUserID? true : false;
01474         }
01475 
01476         // If facebook connection is enabled, trying to login user using Facebook ID
01477         if ( !$sUserID && !$blAdmin && $oConfig->getConfigParam( "bl_showFbConnect") ) {
01478             $sUserID = $this->_getFacebookUserId();
01479         }
01480 
01481         // checking user results
01482         if ( $sUserID ) {
01483             if ( $this->load( $sUserID ) ) {
01484                 // storing into session
01485                 if ($blAdmin) {
01486                     oxRegistry::getSession()->setVariable( 'auth', $sUserID );
01487                 } else {
01488                     oxRegistry::getSession()->setVariable( 'usr', $sUserID );
01489                 }
01490 
01491                 // marking the way user was loaded
01492                 $this->_blLoadedFromCookie = $blFoundInCookie;
01493                 return true;
01494             }
01495         } else {
01496             // no user
01497             if ($blAdmin) {
01498                 oxRegistry::getSession()->deleteVariable( 'auth' );
01499             } else {
01500                 oxRegistry::getSession()->deleteVariable( 'usr' );
01501             }
01502 
01503             return false;
01504         }
01505     }
01506 
01512     protected function _getFacebookUserId()
01513     {
01514         $oDb = oxDb::getDb();
01515         $oFb = oxRegistry::get("oxFb");
01516         $oConfig = $this->getConfig();
01517         if ( $oFb->isConnected() && $oFb->getUser() ) {
01518             $sUserSelect = "oxuser.oxfbid = " . $oDb->quote( $oFb->getUser() );
01519             $sShopSelect = "";
01520 
01521 
01522             $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01523             $sUserID = $oDb->getOne( $sSelect );
01524         }
01525         return $sUserID;
01526     }
01527 
01533     protected function _getCookieUserId()
01534     {
01535         $sUserID = null;
01536         $oConfig = $this->getConfig();
01537         $sShopID = $oConfig->getShopId();
01538         if ( ( $sSet = oxRegistry::get("oxUtilsServer")->getUserCookie( $sShopID ) ) ) {
01539             $oDb = oxDb::getDb();
01540             $aData = explode( '@@@', $sSet );
01541             $sUser = $aData[0];
01542             $sPWD  = @$aData[1];
01543 
01544             $sSelect =  'select oxid, oxpassword, oxpasssalt from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDb->quote($sUser);
01545 
01546             $rs = $oDb->select( $sSelect );
01547             if ( $rs != false && $rs->recordCount() > 0 ) {
01548                 while (!$rs->EOF) {
01549                     $sTest = crypt( $rs->fields[1], $rs->fields[2] );
01550                     if ( $sTest == $sPWD ) {
01551                         // found
01552                         $sUserID = $rs->fields[0];
01553                         break;
01554                     }
01555                     $rs->moveNext();
01556                 }
01557             }
01558             // if cookie info is not valid, remove it.
01559             if ( !$sUserID ) {
01560                 oxRegistry::get('oxUtilsServer')->deleteUserCookie( $sShopID );
01561             }
01562         }
01563         return $sUserID;
01564     }
01565 
01578     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01579     {
01580         $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01581         $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01582 
01583         // maybe this is LDAP user but supplied email Address instead of LDAP login
01584         $oDb = oxDb::getDb();
01585         $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01586         if ( isset( $sLDAPKey) && $sLDAPKey) {
01587             $sUser = $sLDAPKey;
01588         }
01589 
01590         //$throws oxConnectionException
01591         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01592 
01593         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01594         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01595             // login successful
01596 
01597             // check if user is already in database
01598             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01599             $sOXID = $oDb->getOne( $sSelect );
01600 
01601             if ( !isset( $sOXID ) || !$sOXID ) {
01602                 // we need to create a new user
01603                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01604                 $this->setId();
01605 
01606                 // map all user data fields
01607                 foreach ( $aData as $fldname => $value) {
01608                     $sField = "oxuser__".strtolower( $fldname);
01609                     $this->$sField = new oxField( $aData[$fldname] );
01610                 }
01611 
01612                 $this->oxuser__oxactive  = new oxField( 1 );
01613                 $this->oxuser__oxshopid  = new oxField( $sShopID );
01614                 $this->oxuser__oxldapkey = new oxField( $sUser );
01615                 $this->oxuser__oxrights  = new oxField( "user" );
01616                 $this->setPassword( "ldap user" );
01617 
01618                 $this->save();
01619             } else {
01620                 // LDAP user is already in OXID DB, load it
01621                 $this->load( $sOXID);
01622             }
01623 
01624         } else {
01625             $oEx = oxNew( 'oxUserException' );
01626             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01627             throw $oEx;
01628         }
01629     }
01630 
01637     protected function _getUserRights()
01638     {
01639         // previously user had no rights defined
01640         if ( !$this->oxuser__oxrights->value )
01641             return 'user';
01642 
01643         $oDb = oxDb::getDb();
01644         $myConfig    = $this->getConfig();
01645         $sAuthRights = null;
01646 
01647         // choosing possible user rights index
01648         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01649         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01650         if ( $sAuthUserID ) {
01651             $sAuthRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $sAuthUserID ) );
01652         }
01653 
01654         //preventing user rights edit for non admin
01655         $aRights = array();
01656 
01657         // selecting current users rights ...
01658         if ( $sCurrRights = $oDb->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDb->quote( $this->getId() ) ) ) {
01659             $aRights[] = $sCurrRights;
01660         }
01661         $aRights[] = 'user';
01662 
01663         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01664             return current( $aRights );
01665         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01666             $aRights[] = $sAuthRights;
01667             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01668                 return current( $aRights );
01669             }
01670         }
01671 
01672         // leaving as it was set ...
01673         return $this->oxuser__oxrights->value;
01674     }
01675 
01681     protected function _insert()
01682     {
01683 
01684         // set oxcreate date
01685         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01686 
01687         if ( !isset( $this->oxuser__oxboni->value ) ) {
01688             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01689         }
01690 
01691         return parent::_insert();
01692     }
01693 
01699     protected function _update()
01700     {
01701         //V #M418: for not registered users, don't change boni during update
01702         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01703             $this->_aSkipSaveFields[] = 'oxboni';
01704         }
01705 
01706         // don't change this field
01707         $this->_aSkipSaveFields[] = 'oxcreate';
01708         if ( !$this->isAdmin() ) {
01709             $this->_aSkipSaveFields[] = 'oxcustnr';
01710             $this->_aSkipSaveFields[] = 'oxrights';
01711         }
01712 
01713         // updating subscription information
01714         if ( ( $blUpdate = parent::_update() ) ) {
01715             $this->getNewsSubscription()->updateSubscription( $this );
01716         }
01717 
01718         return $blUpdate;
01719     }
01720 
01736     protected function _checkLogin( $sLogin, $aInvAddress )
01737     {
01738         $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01739         oxRegistry::get("oxInputValidator")->checkLogin( $this, $sLogin, $aInvAddress );
01740         return $sLogin;
01741     }
01742 
01750     public function checkIfEmailExists( $sEmail )
01751     {
01752         $myConfig = $this->getConfig();
01753         $oDb = oxDb::getDb();
01754         $iShopId = $myConfig->getShopId();
01755         $blExists = false;
01756 
01757         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDb->quote( $sEmail );
01758         if ( ( $sOxid = $this->getId() ) ) {
01759             $sQ .= " and oxid <> ".$oDb->quote( $sOxid );
01760         }
01761         $oRs = $oDb->select( $sQ, false, false );
01762         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01763 
01764             if ( $this->_blMallUsers ) {
01765 
01766                 $blExists = true;
01767                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01768 
01769                     // password is not set - allow to override
01770                     $blExists = false;
01771                 }
01772             } else {
01773 
01774                 $blExists = false;
01775                 while ( !$oRs->EOF ) {
01776                     if ( $oRs->fields[1] != 'user' ) {
01777 
01778                         // exists admin with same login - must not allow
01779                         $blExists = true;
01780                         break;
01781                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01782 
01783                         // exists same login (with password) in same shop
01784                         $blExists = true;
01785                         break;
01786                     }
01787 
01788                     $oRs->moveNext();
01789                 }
01790             }
01791         }
01792         return $blExists;
01793     }
01794 
01802     public function getUserRecommLists( $sOXID = null )
01803     {
01804         if ( !$sOXID )
01805             $sOXID = $this->getId();
01806 
01807         // sets active page
01808         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01809         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01810 
01811         // load only lists which we show on screen
01812         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01813         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01814 
01815 
01816         $oRecommList = oxNew( 'oxlist' );
01817         $oRecommList->init( 'oxrecommlist' );
01818         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01819         $iShopId = $this->getConfig()->getShopId();
01820         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01821         $oRecommList->selectString( $sSelect );
01822 
01823         return $oRecommList;
01824     }
01825 
01833     public function getRecommListsCount( $sOx = null )
01834     {
01835         if ( !$sOx ) {
01836             $sOx = $this->getId();
01837         }
01838 
01839         if ( $this->_iCntRecommLists === null || $sOx ) {
01840             $oDb = oxDb::getDb();
01841             $this->_iCntRecommLists = 0;
01842             $iShopId = $this->getConfig()->getShopId();
01843             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01844             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01845         }
01846         return $this->_iCntRecommLists;
01847     }
01848 
01859     protected function _checkEmail( $sEmail )
01860     {
01861         oxRegistry::get("oxInputValidator")->checkEmail( $this, $sEmail );
01862     }
01863 
01876     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01877     {
01878         return oxRegistry::get("oxInputValidator")->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01879     }
01880 
01891     protected function _checkCountries( $aInvAddress, $aDelAddress )
01892     {
01893         oxRegistry::get("oxInputValidator")->checkCountries( $this, $aInvAddress, $aDelAddress );
01894     }
01895 
01907     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01908     {
01909         oxRegistry::get("oxInputValidator")->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01910     }
01911 
01922     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01923     {
01924         oxRegistry::get("oxInputValidator")->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01925     }
01926 
01937     protected function _checkVatId( $aInvAddress )
01938     {
01939         oxRegistry::get("oxInputValidator")->checkVatId( $this, $aInvAddress );
01940     }
01941 
01950     protected function _setAutoGroups( $sCountryId )
01951     {
01952         // assigning automatically to specific groups
01953         $blForeigner = true;
01954         $blForeignGroupExists = false;
01955         $blInlandGroupExists = false;
01956 
01957         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01958         // foreigner ?
01959         if ( is_array($aHomeCountry)) {
01960             if (in_array($sCountryId, $aHomeCountry)) {
01961                 $blForeigner = false;
01962             }
01963         } elseif ($sCountryId == $aHomeCountry) {
01964             $blForeigner = false;
01965         }
01966 
01967         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01968             $blForeignGroupExists = true;
01969             if ( !$blForeigner ) {
01970                 $this->removeFromGroup( 'oxidforeigncustomer' );
01971             }
01972         }
01973 
01974         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01975             $blInlandGroupExists = true;
01976             if ( $blForeigner ) {
01977                 $this->removeFromGroup( 'oxidnewcustomer' );
01978             }
01979         }
01980 
01981         if ( !$this->oxuser__oxdisableautogrp->value ) {
01982             if ( !$blForeignGroupExists && $blForeigner ) {
01983                 $this->addToGroup( 'oxidforeigncustomer' );
01984             }
01985             if ( !$blInlandGroupExists && !$blForeigner ) {
01986                 $this->addToGroup( 'oxidnewcustomer' );
01987             }
01988         }
01989     }
01990 
01991 
02000     public function loadUserByUpdateId( $sUid )
02001     {
02002         $oDb = oxDb::getDb();
02003         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02004         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02005             return $this->load( $sUserId );
02006         }
02007     }
02008 
02016     public function setUpdateKey( $blReset = false )
02017     {
02018         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02019         $iUpTime = $blReset ? 0 : oxRegistry::get("oxUtilsDate")->getTime() + $this->getUpdateLinkTerm();
02020 
02021         // generating key
02022         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02023 
02024         // setting expiration time for 6 hours
02025         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02026 
02027         // saving
02028         $this->save();
02029     }
02030 
02036     public function getUpdateLinkTerm()
02037     {
02038         return 3600 * 6;
02039     }
02040 
02048     public function isExpiredUpdateId( $sKey )
02049     {
02050         $oDb = oxDb::getDb();
02051         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02052         return !( (bool) $oDb->getOne( $sQ ) );
02053     }
02054 
02060     public function getUpdateId()
02061     {
02062         if ( $this->_sUpdateKey === null ) {
02063             $this->setUpdateKey();
02064             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02065         }
02066         return $this->_sUpdateKey;
02067     }
02068 
02079     public function encodePassword( $sPassword, $sSalt )
02080     {
02082         $oSha512Hasher  = oxNew('oxSha512Hasher');
02084         $oHasher = oxNew('oxPasswordHasher', $oSha512Hasher );
02085 
02086         return $oHasher->hash($sPassword, $sSalt);
02087     }
02088 
02098     public function prepareSalt( $sSalt )
02099     {
02101         $oOpenSSLFunctionalityChecker = oxNew('oxOpenSSLFunctionalityChecker');
02103         $oGenerator = oxNew('oxPasswordSaltGenerator', $oOpenSSLFunctionalityChecker);
02104 
02105         return $oGenerator->generate();
02106     }
02107 
02117     public function decodeSalt( $sSaltHex )
02118     {
02119         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02120     }
02121 
02129     public function setPassword( $sPassword = null )
02130     {
02132         $oOpenSSLFunctionalityChecker = oxNew('oxOpenSSLFunctionalityChecker');
02133         // setting salt if password is not empty
02135         $oSaltGenerator = oxNew('oxPasswordSaltGenerator', $oOpenSSLFunctionalityChecker);
02136 
02137         $sSalt = $sPassword ? $oSaltGenerator->generate() : '';
02138 
02139         // encoding only if password was not empty (e.g. user registration without pass)
02140         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02141 
02142         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02143         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02144     }
02145 
02153     public function isSamePassword( $sNewPass )
02154     {
02155         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02156     }
02157 
02163     public function isLoadedFromCookie()
02164     {
02165         return $this->_blLoadedFromCookie;
02166     }
02167 
02174     public function getPasswordHash()
02175     {
02176         $sHash = null;
02177         if ( $this->oxuser__oxpassword->value ) {
02178             if ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02179                 $this->setPassword( $this->oxuser__oxpassword->value );
02180             }
02181             $sHash = $this->oxuser__oxpassword->value;
02182         }
02183         return $sHash;
02184     }
02185 
02193     public function getReviewUserHash( $sUserId )
02194     {
02195         $oDb = oxDb::getDb();
02196         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02197         return $sReviewUserHash;
02198     }
02199 
02207     public function getReviewUserId( $sReviewUserHash )
02208     {
02209         $oDb = oxDb::getDb();
02210         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02211         return $sUserId;
02212     }
02213 
02219     public function getState()
02220     {
02221         return $this->oxuser__oxstateid->value;
02222     }
02223 
02229     public function isTermsAccepted()
02230     {
02231         $oDb = oxDb::getDb();
02232         $sShopId = $this->getConfig()->getShopId();
02233         $sUserId = $oDb->quote( $this->getId() );
02234         return (bool) $oDb->getOne( "select 1 from oxacceptedterms where oxuserid={$sUserId} and oxshopid='{$sShopId}'" );
02235     }
02236 
02242     public function acceptTerms()
02243     {
02244         $oDb = oxDb::getDb();
02245         $sUserId  = $oDb->quote( $this->getId() );
02246         $sShopId  = $this->getConfig()->getShopId();
02247         $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02248 
02249         $oDb->execute( "replace oxacceptedterms set oxuserid={$sUserId}, oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02250     }
02251 
02261     public function setCreditPointsForRegistrant( $sUserId, $sRecEmail )
02262     {
02263         $blSet   = false;
02264         $oDb = oxDb::getDb();
02265         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02266         // check if this invitation is still not accepted
02267         $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);
02268         if ( $iPoints && $iPending ) {
02269             $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02270             if ( $blSet = $this->save() ) {
02271                 // updating users statistics
02272                 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail ) );
02273                 $oInvUser = oxNew( "oxuser" );
02274                 if ( $oInvUser->load( $sUserId ) ) {
02275                     $blSet = $oInvUser->setCreditPointsForInviter();
02276                 }
02277             }
02278         }
02279         oxSession::deleteVar( 'su' );
02280         oxSession::deleteVar( 're' );
02281 
02282         return $blSet;
02283     }
02284 
02290     public function setCreditPointsForInviter()
02291     {
02292         $blSet   = false;
02293         $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02294         if ( $iPoints ) {
02295             $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02296             $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02297             $blSet = $this->save();
02298         }
02299 
02300         return $blSet;
02301     }
02302 
02308     public function updateFbId()
02309     {
02310         $oFb = oxRegistry::get("oxFb");
02311         $blRet = false;
02312 
02313         if ( $oFb->isConnected() && $oFb->getUser() ) {
02314              $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02315              $blRet = $this->save();
02316         }
02317 
02318         return $blRet;
02319     }
02320 
02328     public function updateInvitationStatistics( $aRecEmail )
02329     {
02330         $oDb = oxDb::getDb();
02331         $sUserId = $this->getId();
02332 
02333         if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
02334             //iserting statistics about invitation
02335             $sDate = oxRegistry::get("oxUtilsDate")->formatDBDate( date("Y-m-d"), true );
02336             $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
02337             foreach ( $aRecEmail as $sRecEmail ) {
02338                 $sSql = "INSERT INTO oxinvitations SET oxuserid = ".$oDb->quote( $sUserId ).", oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02339                 $oDb->execute( $sSql );
02340             }
02341         }
02342     }
02343 
02351     public function getIdByUserName( $sUserName )
02352     {
02353         $oDb = oxDb::getDb();
02354         $sQ = "SELECT `oxid` FROM `oxuser` WHERE `oxusername` = ". $oDb->quote( $sUserName );
02355         if ( !$this->getConfig()->getConfigParam( 'blMallUsers' ) ) {
02356             $sQ .= " AND `oxshopid` = ". $oDb->quote( $this->getConfig()->getShopId() );
02357         }
02358 
02359         return $oDb->getOne( $sQ );
02360 
02361     }
02362 
02368     public function hasAccount()
02369     {
02370 
02371         return (bool) $this->oxuser__oxpassword->value;
02372 
02373     }
02374 
02380     public function isPriceViewModeNetto()
02381     {
02382         return (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
02383     }
02384 
02392     protected function _dbLogin( $sUser, $sPassword, $sShopID )
02393     {
02394         $blOldHash = false;
02395         $oDb = oxDb::getDb();
02396 
02397         if ($this->_isDemoShop() && $this->isAdmin()) {
02398             $sUserOxId = $oDb->getOne( $this->_getDemoShopLoginQuery( $sUser, $sPassword ));
02399         } else {
02400             $sUserOxId = $oDb->getOne( $this->_getLoginQuery( $sUser, $sPassword, $sShopID, $this->isAdmin() ) );
02401             if( !$sUserOxId ){
02402                 $sUserOxId = $oDb->getOne( $this->_getLegacyLoginQuery( $sUser, $sPassword, $sShopID, $this->isAdmin() ) );
02403                 $blOldHash = true;
02404             }
02405         }
02406 
02407         if ( $sUserOxId ) {
02408             if ( !$this->load( $sUserOxId ) ) {
02410                 $oEx = oxNew( 'oxUserException' );
02411                 $oEx->setMessage( 'ERROR_MESSAGE_USER_NOVALIDLOGIN' );
02412                 throw $oEx;
02413             }elseif($blOldHash && $this->getId()){
02414                 $this->setPassword($sPassword);
02415                 $this->save();
02416             }
02417         }
02418 
02419     }
02420 
02421     protected function _isDemoShop()
02422     {
02423         $blDemoMode = false;
02424 
02425             if ($this->getConfig()->isDemoShop()) {
02426                 $blDemoMode = true;
02427             }
02428 
02429         return $blDemoMode;
02430     }
02431 
02432     protected function _getDemoShopLoginQuery( $sUser, $sPassword )
02433     {
02434         if ( $sPassword == "admin" && $sUser == "admin" ) {
02435             $sSelect = "SELECT `oxid` FROM `oxuser` WHERE `oxrights` = 'malladmin' ";
02436         } else {
02438             $oEx = oxNew( 'oxUserException' );
02439             $oEx->setMessage( 'ERROR_MESSAGE_USER_NOVALIDLOGIN' );
02440             throw $oEx;
02441         }
02442 
02443         return $sSelect;
02444     }
02445 }