oxuser.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxUser extends oxBase
00009 {
00014     protected $_blDisableShopCheck = true;
00015 
00020     protected $_oNewsSubscription = null;
00021 
00026     protected $_sCoreTbl = 'oxuser';
00027 
00032     protected $_sClassName = 'oxuser';
00033 
00039     protected $_aBaskets = array();
00040 
00046     protected $_oGroups;
00047 
00053     protected $_oAddresses;
00054 
00060     protected $_oPayments;
00061 
00067     protected $_oRecommList;
00068 
00074     protected $_blMallUsers = false;
00075 
00081     protected static $_aUserCookie = array();
00082 
00088     protected $_iCntNoticeListArticles = null;
00089 
00095     protected $_iCntWishListArticles = null;
00096 
00102     protected $_iCntRecommLists = null;
00103 
00109      protected $_sUpdateKey = null;
00110 
00116      protected $_blLoadedFromCookie  = null;
00117 
00123     public function __construct()
00124     {
00125         $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00126 
00127         parent::__construct();
00128         $this->init( 'oxuser' );
00129     }
00130 
00138     public function setMallUsersStatus( $blOn = false )
00139     {
00140         $this->_blMallUsers = $blOn;
00141     }
00142 
00150     public function __get( $sParamName )
00151     {
00152         // it saves memory using - loads data only if it is used
00153         switch ( $sParamName ) {
00154             case 'oGroups':
00155                 return $this->_oGroups = $this->getUserGroups();
00156             case 'iCntNoticeListArticles':
00157                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00158             case 'iCntWishListArticles':
00159                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00160             case 'iCntRecommLists':
00161                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00162             case 'oAddresses':
00163                 return $this->_oAddresses = $this->getUserAddresses();
00164             case 'oPayments':
00165                 return $this->_oPayments = $this->getUserPayments();
00166             case 'oxuser__oxcountry':
00167                 return $this->oxuser__oxcountry = $this->getUserCountry();
00168             case 'sDBOptin':
00169                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00170             case 'sEmailFailed':
00171                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00172         }
00173     }
00174 
00180     public function getNewsSubscription()
00181     {
00182         if ( $this->_oNewsSubscription !== null ) {
00183             return $this->_oNewsSubscription;
00184         }
00185 
00186         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00187 
00188         // if subscription object is not set yet - we should create one
00189         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00190             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00191 
00192                 // no subscription defined yet - creating one
00193                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00194                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00195                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00196                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00197                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00198             }
00199         }
00200 
00201         return $this->_oNewsSubscription;
00202     }
00203 
00213     public function getUserCountry( $sCountryId = null, $iLang = null )
00214     {
00215 
00216         if ( !$sCountryId ) {
00217             $sCountryId = $this->oxuser__oxcountryid->value;
00218         }
00219 
00220         $sQ = 'select oxtitle'.oxLang::getInstance()->getLanguageTag( $iLang ).' from oxcountry where oxid = "'.$sCountryId.'" ';
00221         $this->oxuser__oxcountry = new oxField( oxDb::getDb()->getOne( $sQ ), oxField::T_RAW);
00222 
00223         return $this->oxuser__oxcountry;
00224     }
00225 
00233     public function getUserCountryId( $sCountry = null )
00234     {
00235         $sQ = 'select oxid from oxcountry where oxactive = "1" and oxisoalpha2 = "' . $sCountry . '"';
00236         $sCountryId = oxDb::getDb()->getOne( $sQ );
00237 
00238         return $sCountryId;
00239     }
00240 
00248     public function getUserGroups( $sOXID = null )
00249     {
00250 
00251         if ( isset( $this->_oGroups ) ) {
00252             return $this->_oGroups;
00253         }
00254 
00255         if ( !$sOXID ) {
00256             $sOXID = $this->getId();
00257         }
00258 
00259         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00260         //$this->oGroups->Init( 'oxbase' );
00261         //$this->oGroups->oLstoTpl->Init( array( 'oxgroups', 'oxobject2group' ) );
00262         $sSelect  = 'select oxgroups.* from oxgroups left join oxobject2group on oxobject2group.oxgroupsid = oxgroups.oxid ';
00263         $sSelect .= 'where oxobject2group.oxobjectid = "'.$sOXID.'" ';
00264         $this->_oGroups->selectString( $sSelect );
00265         return $this->_oGroups;
00266     }
00267 
00275     public function getUserAddresses( $sOXID = null )
00276     {
00277 
00278         if ( isset( $this->_oAddresses ) ) {
00279             return $this->_oAddresses;
00280         }
00281 
00282         if ( !$sOXID ) {
00283             $sOXID = $this->getId();
00284         }
00285 
00286         //P
00287         $this->_oAddresses = oxNew( 'oxlist' );
00288         $this->_oAddresses->init( "oxbase", "oxaddress" );
00289         $sSelect = 'select * from oxaddress where oxaddress.oxuserid = "'.$sOXID.'" ';
00290         $this->_oAddresses->selectString( $sSelect );
00291         return $this->_oAddresses;
00292     }
00293 
00302     public function getSelectedAddress( $sWishId = false )
00303     {
00304         $sAddressId = oxConfig::getParameter( "oxaddressid");
00305         if ( !$sAddressId ) {
00306             $sAddressId = oxSession::getVar( "deladrid" );
00307         }
00308 
00309         if ( $sAddressId ) {
00310             $sWishId = null;
00311         }
00312 
00313         $oAddresses = $this->getUserAddresses();
00314         if ( $sWishId && $oAddresses->count()) {
00315             foreach ( $oAddresses as $oAddress ) {
00316                 $oAddress->selected = 0;
00317                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sWishId ) {
00318                     $oAddress->selected = 1;
00319                     return $oAddress->getId();
00320                 }
00321             }
00322         }
00323 
00324         if ( !$sAddressId && $oAddresses->count() ) {
00325             $oAddresses->rewind();
00326             if ( ( $oCurAdress = $oAddresses->current() ) ) {
00327                 $sAddressId = $oCurAdress->getId();
00328             }
00329         }
00330 
00331         // #597A
00332         if ( $sAddressId ) {
00333             foreach ( $oAddresses as $oAddress ) {
00334                 $oAddress->selected = 0;
00335                 if ( $oAddress->getId() == $sAddressId ) {
00336                     $oAddress->selected = 1;
00337                     break;
00338                 }
00339             }
00340         }
00341 
00342         return $sAddressId;
00343     }
00344 
00352     public function getUserPayments( $sOXID = null )
00353     {
00354         if ( $this->_oPayments === null ) {
00355 
00356             if ( !$sOXID ) {
00357                 $sOXID = $this->getId();
00358             }
00359 
00360             $sSelect = 'select * from oxuserpayments where oxuserid ="'. $sOXID .'"';
00361 
00362             $this->_oPayments = oxNew( 'oxlist' );
00363             $this->_oPayments->init( 'oxUserPayment' );
00364             $this->_oPayments->selectString( $sSelect );
00365 
00366             $myUtils = oxUtils::getInstance();
00367             foreach ( $this->_oPayments as $oPayment ) {
00368                 // add custom fields to this class
00369                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00370             }
00371         }
00372 
00373         return $this->_oPayments;
00374     }
00375 
00381     public function save()
00382     {
00383 
00384         $blAddRemark = false;
00385         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00386             $blAddRemark = true;
00387             //save oxregister value
00388             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00389         }
00390 
00391         // setting user rights
00392         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00393 
00394         // processing birth date which came from output as array
00395         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00396             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00397         }
00398 
00399         // dodger  Task #1535 - editing user information in shop
00400         // isDerived seems to be wrong here
00401         $blStore = $this->_blIsDerived;
00402         $this->_blIsDerived = false;
00403         $blRet = parent::save();
00404         $this->_blIsDerived = $blStore;
00405 
00406         //add registered remark
00407         if ( $blAddRemark && $blRet ) {
00408             $oRemark = oxNew( 'oxremark' );
00409             $oRemark->oxremark__oxtext     = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00410             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00411             $oRemark->save();
00412         }
00413 
00414         return $blRet;
00415     }
00416 
00424     public function inGroup( $sGroupID )
00425     {
00426         $blIn = false;
00427         if ( ( $oGroups = $this->getUserGroups() ) ) {
00428             $blIn = isset( $oGroups[ $sGroupID ] );
00429         }
00430 
00431         return $blIn;
00432     }
00433 
00442     public function delete( $sOXID = null )
00443     {
00444 
00445         if ( !$sOXID ) {
00446             $sOXID = $this->getId();
00447         }
00448         if ( !$sOXID ) {
00449             return false;
00450         }
00451 
00452         $blDeleted = parent::delete( $sOXID );
00453 
00454         if ( $blDeleted ) {
00455             $oDB = oxDb::getDb();
00456 
00457             // deleting stored payment, address, group dependencies, remarks info
00458             $rs = $oDB->execute( 'delete from oxuserpayments where oxuserpayments.oxuserid = "'.$sOXID.'" ' );
00459             $rs = $oDB->execute( 'delete from oxaddress where oxaddress.oxuserid = "'.$sOXID.'" ' );
00460             $rs = $oDB->execute( 'delete from oxobject2group where oxobject2group.oxobjectid = "'.$sOXID.'" ');
00461             $rs = $oDB->execute( 'delete from oxremark where oxparentid = "'.$sOXID.'" ' );
00462 
00463             // deleting notice/wish lists
00464             $rs = $oDB->execute( 'delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = "'.$sOXID.'" ' );
00465             $rs = $oDB->execute( 'delete from oxuserbaskets where oxuserid = "'.$sOXID.'" ' );
00466 
00467             // deleting Newsletter subscription
00468             $rs = $oDB->execute( 'delete from oxnewssubscribed where oxuserid = "'.$sOXID.'" ');
00469 
00470             $blDeleted = $rs->EOF;
00471         }
00472 
00473         return $blDeleted;
00474     }
00475 
00483     public function load( $oxID )
00484     {
00485 
00486         $blRet = parent::Load( $oxID );
00487 
00488         // convert date's to international format
00489         if ( isset( $this->oxuser__oxcreate->value ) ) {
00490             $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00491         }
00492 
00493         return $blRet;
00494     }
00495 
00503     public function exists( $sOXID = null )
00504     {
00505         if ( !$sOXID ) {
00506             $sOXID = $this->getId();
00507         }
00508 
00509         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00510                     WHERE ( oxusername = "'.$this->oxuser__oxusername->value.'"';
00511 
00512         if ( $sOXID ) {
00513             $sSelect.= " or oxid = '$sOXID' ) ";
00514         } else {
00515             $sSelect.= ' ) ';
00516         }
00517 
00518         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00519             $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00520         }
00521 
00522         $blExists = false;
00523         if ( ( $sOxid = oxDb::getDb()->getOne( $sSelect ) ) ) {
00524              // update - set oxid
00525             $this->setId( $sOxid );
00526             $blExists = true;
00527         }
00528         return $blExists;
00529     }
00530 
00536     public function getOrders()
00537     {
00538         $myConfig = $this->getConfig();
00539         $oOrders = oxNew( 'oxlist' );
00540         $oOrders->init( 'oxorder' );
00541 
00542         //P
00543         // Lists does not support loading from two tables, so orders
00544         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00545         // forcing to load product info which is used in templates
00546         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00547 
00548         //loading order for registered user
00549         if ( $this->oxuser__oxregister->value > 1 ) {
00550             $sQ = 'select * from oxorder where oxuserid = "'.$this->getId().'" and oxorderdate >= "'.$this->oxuser__oxregister->value.'" ';
00551 
00552             //#1546 - shopid check added, if it is not multishop
00553 
00554             $sQ .= ' order by oxorderdate desc ';
00555             $oOrders->selectString( $sQ );
00556         }
00557 
00558         return $oOrders;
00559     }
00560 
00566     public function getOrderCount()
00567     {
00568         $iCnt = 0;
00569         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00570             $sQ  = 'select count(*) from oxorder where oxuserid = "'.$this->getId().'" AND oxorderdate >= "'.$this->oxuser__oxregister->value.'" and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00571             $iCnt = (int) oxDb::getDb()->getOne( $sQ );
00572         }
00573 
00574         return $iCnt;
00575     }
00576 
00582     public function getNoticeListArtCnt()
00583     {
00584         if ( $this->_iCntNoticeListArticles === null ) {
00585             $this->_iCntNoticeListArticles = 0;
00586             if ( $this->getId() ) {
00587                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00588             }
00589         }
00590         return $this->_iCntNoticeListArticles;
00591     }
00592 
00598     public function getWishListArtCnt()
00599     {
00600         if ( $this->_iCntWishListArticles === null ) {
00601             $this->_iCntWishListArticles = false;
00602             if ( $this->getId() ) {
00603                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00604             }
00605         }
00606         return $this->_iCntWishListArticles;
00607     }
00608 
00615     public function getActiveCountry()
00616     {
00617         $sDeliveryCountry = '';
00618         if (!($soxAddressId = oxConfig::getParameter( 'deladrid' ))) {
00619             $soxAddressId = oxSession::getVar( 'deladrid' );
00620         }
00621         if ( $soxAddressId ) {
00622             $oDelAddress = oxNew( 'oxbase' );
00623             $oDelAddress->init( 'oxaddress' );
00624             $oDelAddress->load( $soxAddressId );
00625             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00626         } elseif ( $this->getId() ) {
00627             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00628         } else {
00629             $oUser = oxNew( 'oxuser' );
00630             if ( $oUser->loadActiveUser() ) {
00631                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00632             }
00633         }
00634 
00635         return $sDeliveryCountry;
00636     }
00637 
00645     public function createUser()
00646     {
00647         $oDB = oxDb::getDb();
00648         $sShopID = $this->getConfig()->getShopId();
00649 
00650         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00651         $sSelect = "select oxid from oxuser where oxusername = '{$this->oxuser__oxusername->value}' and oxpassword = '' ";
00652         if ( !$this->_blMallUsers ) {
00653             $sSelect .= " and oxshopid = '{$sShopID}' ";
00654         }
00655         $sOXID = $oDB->getOne( $sSelect );
00656 
00657         // user without password found - lets use
00658         if ( isset( $sOXID ) && $sOXID ) {
00659             // try to update
00660             $this->setId( $sOXID );
00661         } elseif ( $this->_blMallUsers ) { // must be sure if there is no dublicate user
00662             $sQ = "select oxid from oxuser where oxusername = '{$this->oxuser__oxusername->value}' and oxusername != '' ";
00663             if ( $oDB->getOne( $sQ ) ) {
00664                 $oEx = oxNew( 'oxUserException' );
00665                 $oLang = oxLang::getInstance();
00666                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00667                 throw $oEx;
00668             }
00669         }
00670 
00671         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00672         if ( ( $blOK = $this->save() ) ) {
00673             // dropping/cleaning old delivery address/payment info
00674             $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = '{$this->oxuser__oxid->value}' " );
00675             $oDB->execute( "update oxuserpayments set oxuserpayments.oxuserid = '{$this->oxuser__oxusername->value}' where oxuserpayments.oxuserid = '{$this->oxuser__oxid->value}' " );
00676         } else {
00677             $oEx = oxNew( 'oxUserException' );
00678             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00679             throw $oEx;
00680         }
00681 
00682         return $blOK;
00683     }
00684 
00692     public function addToGroup( $sGroupID )
00693     {
00694         if ( !$this->inGroup( $sGroupID ) ) {
00695             $oNewGroup = oxNew( 'oxobject2group' );
00696             $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00697             $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00698             if ( $oNewGroup->save() ) {
00699                 $this->_oGroups[$sGroupID] = $oNewGroup;
00700                 return true;
00701             }
00702         }
00703         return false;
00704     }
00705 
00713     public function removeFromGroup( $sGroupID = null )
00714     {
00715         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00716             $oGroups = oxNew( 'oxlist' );
00717             $oGroups->init( 'oxobject2group' );
00718             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00719             $oGroups->selectString( $sSelect );
00720             foreach ( $oGroups as $oRemgroup ) {
00721                 if ( $oRemgroup->delete() ) {
00722                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00723                 }
00724             }
00725         }
00726     }
00727 
00736     public function onOrderExecute( $oBasket, $iSuccess )
00737     {
00738 
00739         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00740             //adding user to particular customer groups
00741             if ( !$this->oxuser__oxdisableautogrp->value ) {
00742 
00743                 $myConfig = $this->getConfig();
00744                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00745                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00746 
00747                 $this->addToGroup( 'oxidcustomer' );
00748                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00749                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00750                     $this->addToGroup( 'oxidsmallcust' );
00751                 }
00752                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00753                     $this->addToGroup( 'oxidmiddlecust' );
00754                 }
00755                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00756                     $this->addToGroup( 'oxidgoodcust' );
00757                 }
00758             }
00759 
00760                 if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00761                     $this->removeFromGroup( 'oxidnotyetordered' );
00762                 }
00763         }
00764     }
00765 
00773     public function getBasket( $sName )
00774     {
00775         if ( !isset( $this->_aBaskets[$sName] ) ) {
00776             $oBasket = oxNew( 'oxuserbasket' );
00777             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00778 
00779             // creating if it does not exist
00780             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00781                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00782                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00783 
00784                 // marking basket as new (it will not be saved in DB yet)
00785                 $oBasket->setIsNewBasket();
00786             }
00787 
00788             $this->_aBaskets[$sName] = $oBasket;
00789         }
00790 
00791         return $this->_aBaskets[$sName];
00792     }
00793 
00802     public function convertBirthday( $aData )
00803     {
00804 
00805         // preparing data to process
00806         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00807         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00808         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00809 
00810         // leaving empty if not set
00811         if ( !$iYear && !$iMonth && !$iDay )
00812             return "";
00813 
00814         // year
00815         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00816             $iYear = date('Y');
00817 
00818         // month
00819         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00820             $iMonth = 1;
00821 
00822         // maximum nuber of days in month
00823         $iMaxDays = 31;
00824         switch( $iMonth) {
00825             case 2 :
00826                 if ($iMaxDays > 28)
00827                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00828                 break;
00829             case 4  :
00830             case 6  :
00831             case 9  :
00832             case 11 :
00833                 $iMaxDays = min(30, $iMaxDays);
00834                 break;
00835         }
00836 
00837         // day
00838         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00839             $iDay = 1;
00840         }
00841 
00842         // whole date
00843         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00844     }
00845 
00852     public function getBoni()
00853     {
00854         return 1000;
00855     }
00856 
00869     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00870     {
00871         // preparing input
00872         $sDynGoup = strtolower( trim( $sDynGoup ) );
00873 
00874         // setting denied groups from admin settings also
00875         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
00876 
00877         // default state ..
00878         $blAdd = false;
00879 
00880         // user assignment to dyn group is not allowed
00881         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
00882             $blAdd = false;
00883         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
00884             // trying to add user to prohibited user group?
00885             $blAdd = false;
00886         } elseif ( $this->addToGroup( $sDynGoup ) ) {
00887             $blAdd = true;
00888         }
00889 
00890         // cleanup
00891         oxSession::deleteVar( 'dgr' );
00892 
00893         return $blAdd;
00894     }
00895 
00911     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
00912     {
00913         // 1. checking user name
00914         $this->_checkLogin( $sLogin, $aInvAddress );
00915 
00916         // 2. cheking email
00917         $this->_checkEmail( $sLogin );
00918 
00919         // 3. password
00920         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
00921 
00922         // 4. required fields
00923         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
00924 
00925         // 5. vat id check.
00926             $this->_checkVatId( $aInvAddress );
00927     }
00928 
00937     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
00938     {
00939         // assigning to newsletter
00940         $blSuccess = false;
00941         $myConfig  = $this->getConfig();
00942 
00943         // user wants to get newsletter messages or no ?
00944         $oNewsSubscription = $this->getNewsSubscription();
00945         if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
00946             if ( !$blSendOptIn ) {
00947 
00948                 // double-opt-in check is disabled - assigning automatically
00949                 $this->addToGroup( 'oxidnewsletter' );
00950                 // and setting subscribed status
00951                 $oNewsSubscription->setOptInStatus( 1 );
00952                 $blSuccess = true;
00953             } else {
00954 
00955                 // double-opt-in check enabled - sending confirmation email and setting waiting status
00956                 $oNewsSubscription->setOptInStatus( 2 );
00957 
00958                 // sending double-opt-in mail
00959                 $oEmail = oxNew( 'oxemail' );
00960                 $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
00961             }
00962         } elseif ( !$blSubscribe ) { // removing user from newsletter subscribers
00963             $this->removeFromGroup( 'oxidnewsletter' );
00964             $oNewsSubscription->setOptInStatus( 0 );
00965             $blSuccess = true;
00966         }
00967 
00968         return $blSuccess;
00969     }
00970 
00988     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
00989     {
00990 
00991         // validating values before saving. If validation fails - exception is thrown
00992         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
00993 
00994         // input data is fine - lets save updated user info
00995         $this->assign( $aInvAddress );
00996 
00997 
00998         // update old or add new delivery address
00999         $this->_assignAddress( $aDelAddress );
01000 
01001         // saving new values
01002         if ( $this->save() ) {
01003 
01004             // assigning automatically to specific groups
01005             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01006             $this->_setAutoGroups( $sCountryId );
01007         }
01008     }
01009 
01018     public function addUserAddress( $oUser )
01019     {
01020 
01021         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01022             return false;
01023         }
01024 
01025         $oAddress = oxNew( 'oxbase' );
01026         $oAddress->init( 'oxaddress' );
01027 
01028         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01029         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01030         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01031         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01032         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01033         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01034         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01035         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01036         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01037         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01038         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01039 
01040         // adding new address
01041         if ( $oAddress->save() ) {
01042             // resetting addresses
01043             $this->_oAddresses = null;
01044             return $oAddress->getId();
01045         }
01046     }
01047 
01055     protected function _assignAddress( $aDelAddress )
01056     {
01057         if ( ( isset( $aDelAddress['oxaddress__oxfname'] ) && $aDelAddress['oxaddress__oxfname'] ) ||
01058              ( isset( $aDelAddress['oxaddress__oxlname'] ) && $aDelAddress['oxaddress__oxlname'] ) ) {
01059 
01060             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01061             $aDelAddress['oxaddress__oxid'] = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01062 
01063             $oAddress = oxNew( 'oxbase' );
01064             $oAddress->init( 'oxaddress' );
01065             $oAddress->assign( $aDelAddress );
01066             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01067             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01068             $oAddress->save();
01069 
01070             // resetting addresses
01071             $this->_oAddresses = null;
01072 
01073             // saving delivery Address for later use
01074             oxSession::setVar( 'deladrid', $oAddress->getId() );
01075         } else {
01076             // resetting
01077             oxSession::setVar( 'deladrid', null );
01078         }
01079     }
01080 
01093     public function login( $sUser, $sPassword, $blCookie = false)
01094     {
01095         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01096             $oEx = oxNew( 'oxCookieException' );
01097             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01098             throw $oEx;
01099         }
01100 
01101         $myConfig = $this->getConfig();
01102         if ( $sPassword ) {
01103 
01104             $sShopID = $myConfig->getShopId();
01105             $oDb = oxDb::getDb();
01106 
01107             $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01108             $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01109             $sShopSelect = "";
01110 
01111 
01112             // admin view: can only login with higher than 'user' rights
01113             if ( $this->isAdmin() ) {
01114                 $sShopSelect = " and ( oxrights != 'user' ) ";
01115             }
01116 
01117             $sWhat = "oxid";
01118 
01119             $sSelect =  "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01120             if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
01121                 if ( $sPassword == "admin" && $sUser == "admin" ) {
01122                     $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01123                 } else {
01124                     $oEx = oxNew( 'oxUserException' );
01125                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01126                     throw $oEx;
01127                 }
01128             }
01129 
01130             // load from DB
01131             $aData = $oDb->getAll( $sSelect );
01132             $sOXID = @$aData[0][0];
01133             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01134 
01135                 if ( !$this->load( $sOXID ) ) {
01136                     $oEx = oxNew( 'oxUserException' );
01137                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01138                     throw $oEx;
01139                 }
01140             }
01141         }
01142 
01143 
01144         //login successfull?
01145         if ( $this->oxuser__oxid->value ) {   // yes, successful login
01146             if ( $this->isAdmin() ) {
01147                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01148             } else {
01149                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01150             }
01151 
01152             // cookie must be set ?
01153             if ( $blCookie ) {
01154                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01155             }
01156             return true;
01157         } else {
01158             $oEx = oxNew( 'oxUserException' );
01159             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01160             throw $oEx;
01161         }
01162     }
01163 
01174     public function openIdLogin( $sUser )
01175     {
01176         $myConfig = $this->getConfig();
01177         $sShopID = $myConfig->getShopId();
01178         $oDb = oxDb::getDb();
01179 
01180         $sUserSelect = "oxuser.oxusername = " . $oDb->quote( $sUser );
01181         $sShopSelect = "";
01182 
01183 
01184         $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01185 
01186         // load from DB
01187         $aData = $oDb->getAll( $sSelect );
01188         $sOXID = @$aData[0][0];
01189         if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01190 
01191             if ( !$this->load( $sOXID ) ) {
01192                 $oEx = oxNew( 'oxUserException' );
01193                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01194                 throw $oEx;
01195             }
01196         }
01197 
01198         //login successfull?
01199         if ( $this->oxuser__oxid->value ) {   // yes, successful login
01200             oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01201             return true;
01202         } else {
01203             $oEx = oxNew( 'oxUserException' );
01204             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01205             throw $oEx;
01206         }
01207     }
01208 
01214     public function logout()
01215     {
01216         // deleting session info
01217         oxSession::deleteVar( 'usr' );  // for front end
01218         oxSession::deleteVar( 'auth' ); // for back end
01219         oxSession::deleteVar( 'dgr' );
01220         oxSession::deleteVar( 'dynvalue' );
01221         oxSession::deleteVar( 'paymentid' );
01222         // oxSession::deleteVar( 'deladrid' );
01223 
01224         // delete cookie
01225         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01226 
01227         // unsetting global user
01228         $this->setUser( null );
01229 
01230         return true;
01231     }
01232 
01239     public function loadAdminUser()
01240     {
01241         return $this->loadActiveUser( true );
01242     }
01243 
01252     public function loadActiveUser( $blForceAdmin = false )
01253     {
01254         $myConfig = $this->getConfig();
01255 
01256         $blAdmin = $myConfig->isAdmin() || $blForceAdmin;
01257         $oDB = oxDb::getDb();
01258 
01259         // first - checking session info
01260         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01261         $blFoundInCookie = false;
01262 
01263         //trying automatic login (by 'remember me' cookie)
01264         if ( !$sUserID && !$blAdmin ) {
01265             $sShopID = $myConfig->getShopId();
01266             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01267                 $aData = explode( '@@@', $sSet );
01268                 $sUser = $oDB->quote( $aData[0] );
01269                 $sPWD  = @$aData[1];
01270 
01271                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = '.$sUser;
01272 
01273 
01274                 $oDB = oxDb::getDb();
01275                 $rs = $oDB->execute( $sSelect );
01276                 if ( $rs != false && $rs->recordCount() > 0 ) {
01277                     while (!$rs->EOF) {
01278                         $sTest = crypt( $rs->fields[1], 'ox' );
01279                         if ( $sTest == $sPWD ) {
01280                             // found
01281                             $sUserID = $rs->fields[0];
01282                             $blFoundInCookie = true;
01283                             break;
01284                         }
01285                         $rs->moveNext();
01286                     }
01287                 }
01288             }
01289         }
01290 
01291         // checking user results
01292         if ( $sUserID ) {
01293             if ( $this->load( $sUserID ) ) {
01294                 // storing into session
01295                 if ($blAdmin) {
01296                     oxSession::setVar( 'auth', $sUserID );
01297                 } else {
01298                     oxSession::setVar( 'usr', $sUserID );
01299                 }
01300 
01301                 // marking the way user was loaded
01302                 $this->_blLoadedFromCookie = $blFoundInCookie;
01303                 return true;
01304             }
01305         } else {
01306             // no user
01307             oxSession::deleteVar( 'usr' );
01308             oxSession::deleteVar( 'auth' );
01309 
01310             return false;
01311         }
01312     }
01313 
01326     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01327     {
01328         include "oxldap.php";
01329         $myConfig = $this->getConfig();
01330         $oDb = oxDb::getDb();
01331         //$throws oxConnectionException
01332         $aLDAPParams = $myConfig->getConfigParam( 'aLDAPParams' );
01333         $oLDAP = new oxLDAP( $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01334         // maybe this is LDAP user but supplied email Address instead of LDAP login
01335         $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote($sUser)." $sShopSelect");
01336         if ( isset( $sLDAPKey) && $sLDAPKey) {
01337             $sUser = $sLDAPKey;
01338         }
01339 
01340         //$throws oxConnectionException
01341         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01342 
01343         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01344         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {   // login successful
01345 
01346             // check if user is already in database
01347             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote($aData['OXUSERNAME'])." $sShopSelect";
01348             $sOXID = $oDb->getOne( $sSelect);
01349 
01350             if ( !isset( $sOXID) || !$sOXID) {   // we need to create a new user
01351                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01352                 $this->setId();
01353 
01354                 // map all user data fields
01355                 foreach ( $aData as $fldname => $value) {
01356                     $sField = "oxuser__".strtolower( $fldname);
01357                     $this->$sField->setValue($aData[$fldname]);
01358                 }
01359 
01360                 $this->oxuser__oxactive->setValue(1);
01361                 $this->oxuser__oxshopid->setValue($sShopID);
01362                 $this->oxuser__oxldapkey->setValue($sUser);
01363                 $this->oxuser__oxrights->setValue("user");
01364                 $this->setPassword( "ldap user" );
01365 
01366                 $this->save();
01367             } else {   // LDAP user is already in OXID DB, load it
01368                 $this->load( $sOXID);
01369             }
01370 
01371         } else {
01372             $oEx = oxNew( 'oxUserException' );
01373             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01374             throw $oEx;
01375         }
01376     }
01377 
01384     protected function _getUserRights()
01385     {
01386         // previously user had no rights defined
01387         if ( !$this->oxuser__oxrights->value )
01388             return 'user';
01389 
01390         $oDB = oxDb::getDb();
01391         $myConfig    = $this->getConfig();
01392         $sAuthRights = null;
01393 
01394         // choosing possible user rights index
01395         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01396         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01397         if ( $sAuthUserID ) {
01398             $sAuthUserID = $oDB->quote( $sAuthUserID );
01399             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$sAuthUserID );
01400         }
01401 
01402         //preventing user rights edit for non admin
01403         $aRights = array();
01404 
01405         // selecting current users rights ...
01406         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01407             $aRights[] = $sCurrRights;
01408         }
01409         $aRights[] = 'user';
01410 
01411         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01412             return current( $aRights );
01413         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01414             $aRights[] = $sAuthRights;
01415             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01416                 return current( $aRights );
01417             }
01418         }
01419 
01420         // leaving as it was set ...
01421         return $this->oxuser__oxrights->value;
01422     }
01423 
01433     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01434     {
01435 
01436         /*if ( !$myConfig->blMallUsers ) {
01437             $sShopID = $myConfig->getShopId();
01438             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01439         }*/
01440 
01441         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01442     }
01443 
01449     protected function _insert()
01450     {
01451 
01452         // set oxcreate date
01453         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01454 
01455         if ( !isset( $this->oxuser__oxboni->value ) ) {
01456             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01457         }
01458 
01459         if ( $blInsert = parent::_insert() ) {
01460             // setting customer number
01461             if ( !$this->oxuser__oxcustnr->value || !$this->isAdmin() ) {
01462                 $this->_setRecordNumber( 'oxcustnr' );
01463             }
01464         }
01465 
01466         return $blInsert;
01467     }
01468 
01474     protected function _update()
01475     {
01476         //V #M418: for not registered users, don't change boni during update
01477         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01478             $this->_aSkipSaveFields[] = 'oxboni';
01479         }
01480 
01481         // don't change this field
01482         $this->_aSkipSaveFields[] = 'oxcreate';
01483         if ( !$this->isAdmin() ) {
01484             $this->_aSkipSaveFields[] = 'oxcustnr';
01485             $this->_aSkipSaveFields[] = 'oxrights';
01486         }
01487 
01488         // updating subscription information
01489         if ( ( $blUpdate = parent::_update() ) ) {
01490             $this->getNewsSubscription()->updateSubscription( $this );
01491         }
01492 
01493         return $blUpdate;
01494     }
01495 
01511     protected function _checkLogin( &$sLogin, $aInvAddress )
01512     {
01513         $myConfig = $this->getConfig();
01514 
01515         $sLogin   = ( isset( $aInvAddress['oxuser__oxusername'] ) && $aInvAddress['oxuser__oxusername'] )?$aInvAddress['oxuser__oxusername']:$sLogin;
01516 
01517         // check only for users with password during registration
01518         // if user wants to change user name - we must check if passwords are ok before changing
01519         if ( $this->oxuser__oxpassword->value && $sLogin != $this->oxuser__oxusername->value ) {
01520 
01521             // on this case password must be taken directly from request
01522             $sNewPass = isset( $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
01523             if ( !$sNewPass ) {
01524 
01525                 // 1. user forgot to enter password
01526                 $oEx = oxNew( 'oxInputException' );
01527                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01528                 throw $oEx;
01529             } else {
01530 
01531                 // 2. entered wrong password
01532                 if ( !$this->isSamePassword( $sNewPass ) ) {
01533                     $oEx = oxNew( 'oxUserException' );
01534                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01535                     throw $oEx;
01536                 }
01537             }
01538         }
01539 
01540         if ( $this->checkIfEmailExists( $sLogin ) ) {
01541             //if exists then we do now allow to do that
01542             $oEx = oxNew( 'oxUserException' );
01543             $oLang = oxLang::getInstance();
01544             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
01545             throw $oEx;
01546         }
01547     }
01548 
01556     public function checkIfEmailExists( $sEmail )
01557     {
01558         $myConfig = $this->getConfig();
01559         $oDB = oxDb::getDb();
01560         $iShopId = $myConfig->getShopId();
01561         $blExists = false;
01562 
01563         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01564         if ( ( $sOxid = $this->getId() ) ) {
01565             $sQ .= " and oxid <> '$sOxid' ";
01566         }
01567 
01568         $oRs = $oDB->execute( $sQ );
01569         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01570 
01571             if ( $this->_blMallUsers ) {
01572 
01573                 $blExists = true;
01574                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01575 
01576                     // password is not set - allow to override
01577                     $blExists = false;
01578                 }
01579             } else {
01580 
01581                 $blExists = false;
01582                 while ( !$oRs->EOF ) {
01583                     if ( $oRs->fields[1] != 'user' ) {
01584 
01585                         // exists admin with same login - must not allow
01586                         $blExists = true;
01587                         break;
01588                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01589 
01590                         // exists same login (with password) in same shop
01591                         $blExists = true;
01592                         break;
01593                     }
01594 
01595                     $oRs->moveNext();
01596                 }
01597             }
01598         }
01599         return $blExists;
01600     }
01601 
01609     public function getUserRecommLists( $sOXID = null )
01610     {
01611         if ( !$sOXID )
01612             $sOXID = $this->getId();
01613 
01614         // sets active page
01615         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01616         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01617 
01618         // load only lists which we show on screen
01619         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01620         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01621 
01622 
01623         $oRecommList = oxNew( 'oxlist' );
01624         $oRecommList->init( 'oxrecommlist' );
01625         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01626         $iShopId = $this->getConfig()->getShopId();
01627         $sSelect = 'select * from oxrecommlists where oxuserid ="'. $sOXID .'" and oxshopid ="'. $iShopId .'"';
01628         $oRecommList->selectString( $sSelect );
01629 
01630         return $oRecommList;
01631     }
01632 
01640     public function getRecommListsCount( $sOx = null )
01641     {
01642         if ( !$sOx ) {
01643             $sOXID = $this->getId();
01644         }
01645 
01646         if ( $this->_iCntRecommLists === null || $sOx ) {
01647             $this->_iCntRecommLists = 0;
01648             $iShopId = $this->getConfig()->getShopId();
01649             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid ="'. $sOXID .'" and oxshopid ="'. $iShopId .'"';
01650             $this->_iCntRecommLists = oxDb::getDb()->getOne( $sSelect );
01651         }
01652         return $this->_iCntRecommLists;
01653     }
01654 
01663     protected function _checkEmail( $sEmail )
01664     {
01665         // missing email address (user login name) ?
01666         if ( !$sEmail ) {
01667             $oEx = oxNew( 'oxInputException' );
01668             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01669             throw $oEx;
01670         }
01671 
01672         // invalid email address ?
01673         if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
01674             $oEx = oxNew( 'oxInputException' );
01675             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
01676             throw $oEx;
01677         }
01678     }
01679 
01694     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01695     {
01696         $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01697     }
01698 
01711     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01712     {
01713         //  no password at all
01714         if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
01715             $oEx = oxNew( 'oxInputException' );
01716             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
01717             throw $oEx;
01718         }
01719 
01720         //  password is too short ?
01721         if ( $blCheckLenght &&  getStr()->strlen( $sNewPass ) < 6 ) {
01722             $oEx = oxNew( 'oxInputException' );
01723             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
01724             throw $oEx;
01725         }
01726 
01727         //  passwords do not match ?
01728         if ( $sNewPass != $sConfPass ) {
01729             $oEx = oxNew( 'oxUserException' );
01730             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01731             throw $oEx;
01732         }
01733     }
01734 
01746     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01747     {
01748         // collecting info about required fields
01749         $aMustFields = array( 'oxuser__oxfname',
01750                               'oxuser__oxlname',
01751                               'oxuser__oxstreetnr',
01752                               'oxuser__oxstreet',
01753                               'oxuser__oxzip',
01754                               'oxuser__oxcity' );
01755 
01756         // config shoud override default fields
01757         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
01758         if ( is_array( $aMustFillFields ) ) {
01759             $aMustFields = $aMustFillFields;
01760         }
01761 
01762         // assuring data to check
01763         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
01764         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
01765 
01766         // collecting fields
01767         $aFields = array_merge( $aInvAddress, $aDelAddress );
01768 
01769         // check delivery address ?
01770         $blCheckDel = false;
01771         if ( count( $aDelAddress ) ) {
01772             $blCheckDel = true;
01773         }
01774 
01775         // checking
01776         foreach ( $aMustFields as $sMustField ) {
01777 
01778             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
01779             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
01780                 continue;
01781             }
01782 
01783             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
01784                 $this->_checkRequiredArrayFields( $sMustField, $aFields[$sMustField] );
01785             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
01786                    $oEx = oxNew( 'oxInputException' );
01787                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01788                    throw $oEx;
01789             }
01790         }
01791     }
01792 
01803     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01804     {
01805         foreach ( $aFieldValues as $sValue ) {
01806             if ( !trim( $sValue ) ) {
01807                 $oEx = oxNew( 'oxInputException' );
01808                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01809                 throw $oEx;
01810             }
01811         }
01812     }
01813 
01824     protected function _checkVatId( $aInvAddress )
01825     {
01826         // vat ID must be checked only for business customers
01827         if ( $aInvAddress['oxuser__oxustid'] && $aInvAddress['oxuser__oxcompany'] ) {
01828 
01829             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
01830                 // no country
01831                 return;
01832             }
01833             $oCountry = oxNew('oxcountry');
01834             if (!$oCountry->load($sCountryId)) {
01835                 throw new oxObjectException();
01836             }
01837             if ($oCountry->isForeignCountry() && $oCountry->isInEU()) {
01838                     if (strncmp($aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2)) {
01839                         $oEx = oxNew( 'oxInputException' );
01840                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
01841                         throw $oEx;
01842                     }
01843             }
01844 
01845         }
01846     }
01847 
01856     protected function _setAutoGroups( $sCountryId )
01857     {
01858         // assigning automatically to specific groups
01859         $blForeigner = true;
01860         $blForeignGroupExists = false;
01861         $blInlandGroupExists = false;
01862 
01863         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01864         // foreigner ?
01865         if ( is_array($aHomeCountry)) {
01866             if (in_array($sCountryId, $aHomeCountry)) {
01867                 $blForeigner = false;
01868             }
01869         } elseif ($sCountryId == $aHomeCountry) {
01870             $blForeigner = false;
01871         }
01872 
01873         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01874             $blForeignGroupExists = true;
01875             if ( !$blForeigner ) {
01876                 $this->removeFromGroup( 'oxidforeigncustomer' );
01877             }
01878         }
01879 
01880         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01881             $blInlandGroupExists = true;
01882             if ( $blForeigner ) {
01883                 $this->removeFromGroup( 'oxidnewcustomer' );
01884             }
01885         }
01886 
01887         if ( !$this->oxuser__oxdisableautogrp->value ) {
01888             if ( !$blForeignGroupExists && $blForeigner ) {
01889                 $this->addToGroup( 'oxidforeigncustomer' );
01890             }
01891             if ( !$blInlandGroupExists && !$blForeigner ) {
01892                 $this->addToGroup( 'oxidnewcustomer' );
01893             }
01894         }
01895     }
01896 
01904     protected function _hasUserAddress( $sUserId )
01905     {
01906 
01907         $oAddresses = $this->getUserAddresses();
01908         if ( $oAddresses && count($oAddresses)>0 ) {
01909             $oAddresses->rewind() ;
01910             foreach ($oAddresses as $key => $oAddress) {
01911                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01912                     return true;
01913                 }
01914             }
01915         }
01916         return false;
01917     }
01918 
01931     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
01932     {
01933         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
01934     }
01935 
01945     protected function _deleteUserCookie( $sShopId = null )
01946     {
01947         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
01948     }
01949 
01959     protected static function _getUserCookie( $sShopId = null )
01960     {
01961         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
01962     }
01963 
01964 
01973     public function loadUserByUpdateId( $sUid )
01974     {
01975         $oDb = oxDb::getDb();
01976         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
01977         if ( $sUserId = $oDb->getOne( $sQ ) ) {
01978             return $this->load( $sUserId );
01979         }
01980     }
01981 
01989     public function setUpdateKey( $blReset = false )
01990     {
01991         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
01992         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
01993 
01994         // generating key
01995         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
01996 
01997         // setting expiration time for 6 hours
01998         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
01999 
02000         // saving
02001         $this->save();
02002     }
02003 
02009     public function getUpdateLinkTerm()
02010     {
02011         return 3600 * 6;
02012     }
02013 
02021     public function isExpiredUpdateId( $sKey )
02022     {
02023         $oDb = oxDb::getDb();
02024         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02025         return !( (bool) $oDb->getOne( $sQ ) );
02026     }
02027 
02033     public function getUpdateId()
02034     {
02035         if ( $this->_sUpdateKey === null ) {
02036             $this->setUpdateKey();
02037             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02038         }
02039         return $this->_sUpdateKey;
02040     }
02041 
02050     public function encodePassword( $sPassword, $sSalt )
02051     {
02052         $oDb = oxDb::getDb();
02053         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02054     }
02055 
02063     public function prepareSalt( $sSalt )
02064     {
02065         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02066     }
02067 
02075     public function decodeSalt( $sSaltHex )
02076     {
02077         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02078     }
02079 
02087     public function setPassword( $sPassword = null )
02088     {
02089         // setting salt if password is not empty
02090         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02091 
02092         // encoding only if password was not empty (e.g. user registration without pass)
02093         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02094 
02095         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02096         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02097     }
02098 
02106     public function isSamePassword( $sNewPass )
02107     {
02108         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02109     }
02110 
02116     public function isLoadedFromCookie()
02117     {
02118         return $this->_blLoadedFromCookie;
02119     }
02120 
02127     public function getPasswordHash()
02128     {
02129         $sHash = null;
02130         if ( $this->oxuser__oxpassword->value ) {
02131             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02132                 // decodable pass ?
02133                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02134             } elseif ( ( strlen( $this->oxuser__oxpassword->value ) < 32 ) && ( strpos( $this->oxuser__oxpassword->value, 'openid_' ) !== 0 ) ) {
02135                 // plain pass ?
02136                 $this->setPassword( $this->oxuser__oxpassword->value );
02137             }
02138             $sHash = $this->oxuser__oxpassword->value;
02139         }
02140         return $sHash;
02141     }
02142 
02151     public static function getAdminUser()
02152     {
02153         return self::getActiveUser( true );
02154     }
02155 
02166     public static function getActiveUser( $blForceAdmin = false )
02167     {
02168         $oUser = oxNew( 'oxuser' );
02169         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02170             return $oUser;
02171         } else {
02172             return false;
02173         }
02174     }
02175 
02183     public function getOpenIdPassword( $iLength = 25 )
02184     {
02185         $sPassword= "openid_".substr( oxUtilsObject::getInstance()->generateUId(), 0, $iLength);
02186         return $sPassword;
02187     }
02188 
02196     public function getReviewUserHash( $sUserId )
02197     {
02198         $sReviewUserHash = oxDb::getDb()->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = "'.$sUserId.'"');
02199         return $sReviewUserHash;
02200     }
02201 
02209     public function getReviewUserId( $sReviewUserHash )
02210     {
02211         $sUserId = oxDb::getDb()->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = "'.$sReviewUserHash.'"');
02212         return $sUserId;
02213     }
02214 }

Generated on Wed May 13 13:25:51 2009 for OXID eShop CE by  doxygen 1.5.5