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 $_sCoreTbl = 'oxuser';
00028 
00033     protected $_sClassName = 'oxuser';
00034 
00040     protected $_aBaskets = array();
00041 
00047     protected $_oGroups;
00048 
00054     protected $_oAddresses;
00055 
00061     protected $_oPayments;
00062 
00068     protected $_oRecommList;
00069 
00075     protected $_blMallUsers = false;
00076 
00082     protected static $_aUserCookie = array();
00083 
00089     protected $_iCntNoticeListArticles = null;
00090 
00096     protected $_iCntWishListArticles = null;
00097 
00103     protected $_iCntRecommLists = null;
00104 
00110      protected $_sUpdateKey = null;
00111 
00117     public function __construct()
00118     {
00119         $myConfig = $this->getConfig();
00120         $this->setMallUsersStatus( $myConfig->getConfigParam( 'blMallUsers' ) );
00121 
00122         parent::__construct();
00123         $this->init( 'oxuser' );
00124     }
00125 
00133     public function setMallUsersStatus( $blOn = false )
00134     {
00135         $this->_blMallUsers = $blOn;
00136     }
00137 
00145     public function __get( $sParamName )
00146     {
00147         // it saves memory using - loads data only if it is used
00148         switch ( $sParamName ) {
00149             case 'oGroups':
00150                 return $this->_oGroups = $this->getUserGroups();
00151             case 'iCntNoticeListArticles':
00152                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00153             case 'iCntWishListArticles':
00154                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00155             case 'iCntRecommLists':
00156                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00157             case 'oAddresses':
00158                 return $this->_oAddresses = $this->getUserAddresses();
00159             case 'oPayments':
00160                 return $this->_oPayments = $this->getUserPayments();
00161             case 'oxuser__oxcountry':
00162                 return $this->oxuser__oxcountry = $this->getUserCountry();
00163             case 'sDBOptin':
00164                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00165             case 'sEmailFailed':
00166                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00167             }
00168     }
00169 
00175     public function getNewsSubscription()
00176     {
00177         if ( $this->_oNewsSubscription !== null ) {
00178             return $this->_oNewsSubscription;
00179         }
00180 
00181         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00182 
00183         // if subscription object is not set yet - we should create one
00184         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00185             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00186 
00187                 // no subscription defined yet - creating one
00188                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00189                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00190                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00191                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00192                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00193             }
00194         }
00195 
00196         return $this->_oNewsSubscription;
00197     }
00198 
00208     public function getUserCountry( $sCountryId = null, $iLang = null )
00209     {
00210 
00211         if ( !$sCountryId ) {
00212             $sCountryId = $this->oxuser__oxcountryid->value;
00213         }
00214 
00215         $sQ = 'select oxtitle'.oxLang::getInstance()->getLanguageTag( $iLang ).' from oxcountry where oxid = "'.$sCountryId.'" ';
00216         $this->oxuser__oxcountry = new oxField( oxDb::getDb()->GetOne( $sQ ), oxField::T_RAW);
00217 
00218         return $this->oxuser__oxcountry;
00219     }
00220 
00228     public function getUserGroups( $sOXID = null )
00229     {
00230 
00231         if ( isset( $this->_oGroups ) ) {
00232             return $this->_oGroups;
00233         }
00234 
00235         if ( !$sOXID ) {
00236             $sOXID = $this->getId();
00237         }
00238 
00239         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00240         //$this->oGroups->Init( 'oxbase' );
00241         //$this->oGroups->oLstoTpl->Init( array( 'oxgroups', 'oxobject2group' ) );
00242         $sSelect  = 'select oxgroups.* from oxgroups left join oxobject2group on oxobject2group.oxgroupsid = oxgroups.oxid ';
00243         $sSelect .= 'where oxobject2group.oxobjectid = "'.$sOXID.'" ';
00244         $this->_oGroups->selectString( $sSelect );
00245         return $this->_oGroups;
00246     }
00247 
00255     public function getUserAddresses( $sOXID = null )
00256     {
00257 
00258         if ( isset( $this->_oAddresses ) ) {
00259             return $this->_oAddresses;
00260         }
00261 
00262         if ( !$sOXID ) {
00263             $sOXID = $this->getId();
00264         }
00265 
00266         //P
00267         $this->_oAddresses = oxNew( 'oxlist' );
00268         $this->_oAddresses->init( "oxbase", "oxaddress" );
00269         $sSelect = 'select * from oxaddress where oxaddress.oxuserid = "'.$sOXID.'" ';
00270         $this->_oAddresses->selectString( $sSelect );
00271         return $this->_oAddresses;
00272     }
00273 
00282     public function getSelectedAddress( $sWishId = false)
00283     {
00284         $sAddressId = oxConfig::getParameter( "oxaddressid");
00285         if ( !$sAddressId ) {
00286             $sAddressId = oxSession::getVar( "deladrid" );
00287         }
00288         if ( $sAddressId ) {
00289             $sWishId = null;
00290         }
00291 
00292         if ( !isset( $this->_oAddresses ) ) {
00293             $this->getUserAddresses();
00294         }
00295 
00296         if ( $sWishId && $this->_oAddresses->count()) {
00297             foreach ( $this->_oAddresses as $oAddress ) {
00298                 $oAddress->selected = 0;
00299                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sWishId ) {
00300                     $oAddress->selected = 1;
00301                     return $oAddress->oxaddress__oxid->value;
00302                 }
00303             }
00304         }
00305 
00306         if ( !$sAddressId && $this->_oAddresses->count()) {
00307             $this->_oAddresses->rewind();
00308             $oCurAdress = $this->_oAddresses->current();
00309             if ( isset( $oCurAdress)) {
00310                 $sAddressId = $oCurAdress->oxaddress__oxid->value;
00311             }
00312         }
00313 
00314         // #597A
00315         if ( $sAddressId ) {
00316             foreach ( $this->_oAddresses as $oAddress ) {
00317                 $oAddress->selected = 0;
00318                 if ( $oAddress->oxaddress__oxid->value == $sAddressId ) {
00319                     $oAddress->selected = 1;
00320                     break;
00321                 }
00322             }
00323         }
00324         return $sAddressId;
00325     }
00326 
00334     public function getUserPayments( $sOXID = null )
00335     {
00336         if ( $this->_oPayments !== null ) {
00337             return $this->_oPayments;
00338         }
00339 
00340         if ( !$sOXID ) {
00341             $sOXID = $this->getId();
00342         }
00343 
00344         $sSelect = 'select * from oxuserpayments where oxuserid ="'. $sOXID .'"';
00345 
00346         $this->_oPayments = oxNew( 'oxlist' );
00347         $this->_oPayments->init( 'oxUserPayment' );
00348         $this->_oPayments->selectString( $sSelect );
00349 
00350         //while ( list( $key, $val ) = each( $oPayments ) ) {
00351         foreach ( $this->_oPayments as $oPayment ) {
00352             // add custom fields to this class
00353             $oPayment = oxUtils::getInstance()->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00354         }
00355 
00356         return $this->_oPayments;
00357     }
00358 
00364     public function save()
00365     {
00366 
00367         $blAddRemark = false;
00368         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00369             $blAddRemark = true;
00370             //save oxregister value
00371             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00372         }
00373 
00374         // setting user rights
00375         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00376 
00377         // processing birth date which came from output as array
00378         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00379             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00380         }
00381 
00382         // dodger  Task #1535 - editing user information in shop
00383         // isDerived seems to be wrong here
00384         $blStore = $this->_blIsDerived;
00385         $this->_blIsDerived = false;
00386         $blRet = parent::save();
00387         $this->_blIsDerived = $blStore;
00388 
00389         //add registered remark
00390         if ( $blAddRemark && $blRet ) {
00391             $oRemark = oxNew( 'oxremark' );
00392             $oRemark->oxremark__oxtext     = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00393             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00394             $oRemark->save();
00395         }
00396 
00397         return $blRet;
00398     }
00399 
00407     public function inGroup( $sGroupID )
00408     {
00409          if( !( $oGroups = $this->getUserGroups() ) ) {
00410             return false;
00411          }
00412 
00413          return isset( $oGroups[ $sGroupID ] );
00414     }
00415 
00424     public function delete( $sOXID = null )
00425     {
00426 
00427         if ( !$sOXID ) {
00428             $sOXID = $this->getId();
00429         }
00430         if ( !$sOXID ) {
00431             return false;
00432         }
00433 
00434         $blDeleted = parent::delete( $sOXID );
00435 
00436         if ( $blDeleted ) {
00437             $oDB = oxDb::getDb();
00438 
00439             // deleting stored payment, address, group dependencies, remarks info
00440             $rs = $oDB->execute( 'delete from oxuserpayments where oxuserpayments.oxuserid = "'.$sOXID.'" ' );
00441             $rs = $oDB->execute( 'delete from oxaddress where oxaddress.oxuserid = "'.$sOXID.'" ' );
00442             $rs = $oDB->execute( 'delete from oxobject2group where oxobject2group.oxobjectid = "'.$sOXID.'" ');
00443             $rs = $oDB->execute( 'delete from oxremark where oxparentid = "'.$sOXID.'" ' );
00444 
00445             // deleting notice/wish lists
00446             $rs = $oDB->execute( 'delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = "'.$sOXID.'" ' );
00447             $rs = $oDB->execute( 'delete from oxuserbaskets where oxuserid = "'.$sOXID.'" ' );
00448 
00449             // deleting Newsletter subscription
00450             $rs = $oDB->execute( 'delete from oxnewssubscribed where oxuserid = "'.$sOXID.'" ');
00451 
00452             $blDeleted = $rs->EOF;
00453         }
00454 
00455         return $blDeleted;
00456     }
00457 
00465     public function load( $oxID )
00466     {
00467 
00468         $blRet = parent::Load( $oxID );
00469 
00470         // convert date's to international format
00471         if ( isset( $this->oxuser__oxcreate->value ) ) {
00472             $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00473         }
00474 
00475         return $blRet;
00476     }
00477 
00485     public function exists( $sOXID = null )
00486     {
00487         if ( !$sOXID ) {
00488             $sOXID = $this->getId();
00489         }
00490 
00491         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00492                     WHERE ( oxusername = "'.$this->oxuser__oxusername->value.'"';
00493 
00494         if ( $sOXID ) {
00495             $sSelect.= " or oxid = '$sOXID' ) ";
00496         } else {
00497             $sSelect.= ' ) ';
00498         }
00499 
00500         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00501             $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00502         }
00503 
00504         $blExists = false;
00505         if ( ( $sOxid = oxDb::getDb()->getOne( $sSelect ) ) ) {
00506              // update - set oxid
00507             $this->setId( $sOxid );
00508             $blExists = true;
00509         }
00510         return $blExists;
00511     }
00512 
00518     public function getOrders()
00519     {
00520         $myConfig = $this->getConfig();
00521         $oOrders = oxNew( 'oxlist' );
00522         $oOrders->init( 'oxorder' );
00523 
00524         //P
00525         // Lists does not support loading from two tables, so orders
00526         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00527         // forcing to load product info which is used in templates
00528         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00529 
00530         //loading order for registered user
00531         if ( $this->oxuser__oxregister->value > 1 ) {
00532             $sQ = 'select * from oxorder where oxuserid = "'.$this->getId().'" and oxorderdate >= "'.$this->oxuser__oxregister->value.'" ';
00533 
00534             //#1546 - shopid check added, if it is not multishop
00535 
00536             $sQ .= ' order by oxorderdate desc ';
00537             $oOrders->selectString( $sQ );
00538         }
00539 
00540         return $oOrders;
00541     }
00542 
00548     public function getOrderCount()
00549     {
00550         $iCnt = 0;
00551         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00552             $sQ  = 'select count(*) from oxorder where oxuserid = "'.$this->getId().'" AND oxorderdate >= "'.$this->oxuser__oxregister->value.'" and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00553             $iCnt = (int) oxDb::getDb()->getOne( $sQ );
00554         }
00555 
00556         return $iCnt;
00557     }
00558 
00564     public function getNoticeListArtCnt()
00565     {
00566         if ( $this->_iCntNoticeListArticles === null ) {
00567             $this->_iCntNoticeListArticles = 0;
00568             if ( $this->getId() ) {
00569                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00570             }
00571         }
00572         return $this->_iCntNoticeListArticles;
00573     }
00574 
00580     public function getWishListArtCnt()
00581     {
00582         if ( $this->_iCntWishListArticles === null ) {
00583             $this->_iCntWishListArticles = false;
00584             if ( $this->getId() ) {
00585                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00586             }
00587         }
00588         return $this->_iCntWishListArticles;
00589     }
00590 
00597     public function getActiveCountry()
00598     {
00599         $sDeliveryCountry = '';
00600         if (!($soxAddressId = oxConfig::getParameter( 'deladrid' ))) {
00601             $soxAddressId = oxSession::getVar( 'deladrid' );
00602         }
00603         if ( $soxAddressId ) {
00604             $oDelAddress = oxNew( 'oxbase' );
00605             $oDelAddress->init( 'oxaddress' );
00606             $oDelAddress->load( $soxAddressId );
00607             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00608         } elseif ( $this->getId() ) {
00609             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00610         } elseif ( $oUser = self::getActiveUser() ) {
00611             $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00612         }
00613 
00614         return $sDeliveryCountry;
00615     }
00616 
00624     public function createUser()
00625     {
00626         $myConfig = $this->getConfig();
00627         $oDB = oxDb::getDb();
00628 
00629         $sShopSelect = '';
00630         $sShopID = $myConfig->getShopId();
00631         if ( !$this->_blMallUsers ) {
00632             $sShopSelect = ' and oxshopid = "'.$sShopID.'" ';
00633         }
00634 
00635         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00636         $sSelect = 'select oxid from oxuser where oxusername = "'.$this->oxuser__oxusername->value.'" and oxpassword = "" '.$sShopSelect;
00637         $sOXID   = $oDB->getOne( $sSelect );
00638 
00639         // user without password found - lets use
00640         $blOK = false;
00641 
00642         if ( isset( $sOXID ) && $sOXID ) {
00643 
00644             // try to update
00645             $this->setId( $sOXID );
00646         } else {
00647             // must be sure if there is no dublicate user
00648             if ( $this->_blMallUsers ) {
00649                 $sQ = 'select oxid from oxuser where oxusername = "'.$this->oxuser__oxusername->value.'" and oxusername != "" ';
00650                 if ( $oDB->getOne( $sQ ) ) {
00651                     $oEx = oxNew( 'oxUserException' );
00652                     $oEx->setMessage('EXCEPTION_USER_USEREXISTS');
00653                     throw $oEx;
00654                 }
00655             }
00656         }
00657 
00658         $this->oxuser__oxshopid = new oxField($sShopID, oxField::T_RAW);
00659         $blOK = $this->save();
00660 
00661         if ( $blOK ) { // dropping/cleaning old delivery address/payment info
00662             $oDB->execute( 'delete from oxaddress where oxaddress.oxuserid = "'.$this->oxuser__oxid->value.'" ' );
00663             $oDB->execute( 'update oxuserpayments set oxuserpayments.oxuserid = "'.$this->oxuser__oxusername->value.'" where oxuserpayments.oxuserid = "'.$this->oxuser__oxid->value.'" ' );
00664         } else {
00665             $oEx = oxNew( 'oxUserException' );
00666             $oEx->setMessage('EXCEPTION_USER_USERCREATIONFAILED');
00667             throw $oEx;
00668         }
00669 
00670         return $blOK;
00671     }
00672 
00680     public function addToGroup( $sGroupID )
00681     {
00682         if ( !$this->inGroup( $sGroupID ) ) {
00683             $oNewGroup = oxNew( 'oxobject2group' );
00684             $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00685             $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00686             if ( $oNewGroup->save() ) {
00687                 $this->_oGroups[$sGroupID] = $oNewGroup;
00688                 return true;
00689             }
00690         }
00691         return false;
00692     }
00693 
00701     public function removeFromGroup( $sGroupID = null )
00702     {
00703         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00704             $oGroups = oxNew( 'oxlist' );
00705             $oGroups->init( 'oxobject2group' );
00706             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00707             $oGroups->selectString( $sSelect );
00708             foreach ( $oGroups as $oRemgroup ) {
00709                 if ( $oRemgroup->delete() ) {
00710                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00711                 }
00712             }
00713         }
00714     }
00715 
00724     public function onOrderExecute( $oBasket, $iSuccess )
00725     {
00726 
00727         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00728             //adding user to particular customer groups
00729             if ( !$this->oxuser__oxdisableautogrp->value ) {
00730 
00731                 $myConfig = $this->getConfig();
00732                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00733                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00734 
00735                 $this->addToGroup( 'oxidcustomer' );
00736                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00737                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00738                     $this->addToGroup( 'oxidsmallcust' );
00739                 }
00740                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00741                     $this->addToGroup( 'oxidmiddlecust' );
00742                 }
00743                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00744                     $this->addToGroup( 'oxidgoodcust' );
00745                 }
00746             }
00747 
00748                 if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00749                     $this->removeFromGroup( 'oxidnotyetordered' );
00750                 }
00751         }
00752     }
00753 
00761     public function getBasket( $sName )
00762     {
00763         if ( !isset( $this->_aBaskets[$sName] ) ) {
00764             $oBasket = oxNew( 'oxuserbasket' );
00765             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00766 
00767             // creating if it does not exist
00768             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00769                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00770                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00771 
00772                 // marking basket as new (it will not be saved in DB yet)
00773                 $oBasket->setIsNewBasket();
00774             }
00775 
00776             $this->_aBaskets[$sName] = $oBasket;
00777         }
00778 
00779         return $this->_aBaskets[$sName];
00780     }
00781 
00790     public function convertBirthday( $aData )
00791     {
00792 
00793         // preparing data to process
00794         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00795         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00796         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00797 
00798         // leaving empty if not set
00799         if ( !$iYear && !$iMonth && !$iDay )
00800             return "";
00801 
00802         // year
00803         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00804             $iYear = date('Y');
00805 
00806         // month
00807         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00808             $iMonth = 1;
00809 
00810         // maximum nuber of days in month
00811         $iMaxDays = 31;
00812         switch( $iMonth) {
00813             case 2 :
00814                 if ($iMaxDays > 28)
00815                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00816                 break;
00817             case 4  :
00818             case 6  :
00819             case 9  :
00820             case 11 :
00821                 $iMaxDays = min(30, $iMaxDays);
00822                 break;
00823         }
00824 
00825         // day
00826         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00827             $iDay = 1;
00828         }
00829 
00830         // whole date
00831         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00832     }
00833 
00840     public function getBoni()
00841     {
00842         return 1000;
00843     }
00844 
00857     public function addDynGroup($sDynGoup, $aDeniedDynGroups)
00858     {
00859         // preparing input
00860         $sDynGoup = strtolower( trim( $sDynGoup ) );
00861 
00862         // user assignment to dyn group is not allowed
00863         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
00864             oxSession::deleteVar( 'dgr' );
00865             return false;
00866         }
00867 
00868         // setting denied groups from admin settings also
00869         $aDisabledDynGroups[] = 'oxidadmin';
00870         if ( is_array( $aDeniedDynGroups ) ) {
00871             $aDisabledDynGroups = array_merge( $aDisabledDynGroups, $aDeniedDynGroups );
00872         }
00873 
00874         // trying to add user to prohibited user group
00875         if ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
00876             oxSession::deleteVar( 'dgr' );
00877             return false;
00878         }
00879 
00880         if ( $this->addToGroup( $sDynGoup ) ) {
00881             oxSession::deleteVar( 'dgr' );
00882             return true;
00883         }
00884         return false;
00885     }
00886 
00902     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
00903     {
00904         // 1. checking user name
00905         $this->_checkLogin( $sLogin, $aInvAddress );
00906 
00907         // 2. cheking email
00908         $this->_checkEmail( $sLogin );
00909 
00910         // 3. password
00911         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
00912 
00913         // 4. required fields
00914         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
00915 
00916         // 5. vat id check.
00917             $this->_checkVatId( $aInvAddress );
00918     }
00919 
00928     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
00929     {
00930         // assigning to newsletter
00931         $blSuccess = false;
00932         $myConfig  = $this->getConfig();
00933 
00934         // user wants to get newsletter messages or no ?
00935         $oNewsSubscription = $this->getNewsSubscription();
00936         if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
00937             if ( !$blSendOptIn ) {
00938 
00939                 // double-opt-in check is disabled - assigning automatically
00940                 $this->addToGroup( 'oxidnewsletter' );
00941                 // and setting subscribed status
00942                 $oNewsSubscription->setOptInStatus( 1 );
00943                 $blSuccess = true;
00944             } else {
00945 
00946                 // double-opt-in check enabled - sending confirmation email and setting waiting status
00947                 $oNewsSubscription->setOptInStatus( 2 );
00948 
00949                 // sending double-opt-in mail
00950                 $oEmail = oxNew( 'oxemail' );
00951                 $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
00952             }
00953         } elseif ( !$blSubscribe ) { // removing user from newsletter subscribers
00954             $this->removeFromGroup( 'oxidnewsletter' );
00955             $oNewsSubscription->setOptInStatus( 0 );
00956             $blSuccess = true;
00957         }
00958 
00959         return $blSuccess;
00960     }
00961 
00979     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
00980     {
00981 
00982         // validating values before saving. If validation fails - exception is thrown
00983         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
00984 
00985         // input data is fine - lets save updated user info
00986         $this->assign( $aInvAddress );
00987 
00988 
00989         // update old or add new delivery address
00990         $this->_assignAddress( $aDelAddress );
00991 
00992         // saving new values
00993         if ( $this->save() ) {
00994 
00995             // assigning automatically to specific groups
00996             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
00997             $this->_setAutoGroups( $sCountryId );
00998         }
00999     }
01000 
01009     public function addUserAddress( $oUser )
01010     {
01011 
01012         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01013             return false;
01014         }
01015 
01016         $oAddress = oxNew( 'oxbase' );
01017         $oAddress->init( 'oxaddress' );
01018 
01019         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01020         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01021         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01022         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01023         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01024         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01025         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01026         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01027         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01028         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01029         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01030 
01031         // adding new address
01032         if ( $oAddress->save() ) {
01033             // resetting addresses
01034             $this->_oAddresses = null;
01035             return $oAddress->getId();
01036         }
01037     }
01038 
01046     protected function _assignAddress( $aDelAddress )
01047     {
01048         if ( ( isset( $aDelAddress['oxaddress__oxfname'] ) && $aDelAddress['oxaddress__oxfname'] ) ||
01049              ( isset( $aDelAddress['oxaddress__oxlname'] ) && $aDelAddress['oxaddress__oxlname'] ) ) {
01050 
01051             $aDelAddress['oxaddress__oxid'] = oxConfig::getParameter( 'oxaddressid' );
01052             if ( $aDelAddress['oxaddress__oxid'] == -1 || $aDelAddress['oxaddress__oxid'] == -2 ) {
01053                 $aDelAddress['oxaddress__oxid'] = null;
01054             }
01055 
01056             $oAddress = oxNew( 'oxbase' );
01057             $oAddress->init( 'oxaddress' );
01058             $oAddress->assign( $aDelAddress );
01059             $oAddress->oxaddress__oxuserid = new oxField($this->getId(), oxField::T_RAW);
01060             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value);
01061             $oAddress->save();
01062 
01063             // resetting addresses
01064             $this->_oAddresses = null;
01065 
01066             // saving delivery Address for later use
01067             oxSession::setVar( 'deladrid', $oAddress->getId() );
01068         } else {
01069             // resetting
01070             oxSession::setVar( 'deladrid', null );
01071         }
01072     }
01073 
01086     public function login( $sUser, $sPassword, $blCookie = false)
01087     {
01088         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01089             $oEx = oxNew( 'oxCookieException' );
01090             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01091             throw $oEx;
01092         }
01093 
01094         $myConfig = $this->getConfig();
01095         if ( $sPassword ) {
01096 
01097             $sShopID = $myConfig->getShopID();
01098             $sShopSelect = "";
01099 
01100             // admin view: can only login with higher than 'user' rights
01101             if ( $this->isAdmin() ) {
01102                 $sShopSelect = " and ( oxrights != 'user' ) ";
01103             }
01104 
01105             $sPasswdHash = oxUtils::getInstance()->strMan( $sPassword, $myConfig->getConfigParam( 'sConfigKey' ) );
01106 
01107             $sPasswdSelect  = "oxuser.oxpassword = '$sPasswdHash' or oxuser.oxpassword = '".$this->_encodePassword( $sPassword )."'";
01108 
01109             if ( strpos( $sPassword, "ox_" ) === false ) {
01110                 $sPasswdSelect .= " or oxuser.oxpassword = " . oxDb::getDb()->Quote( $sPassword );
01111             }
01112 
01113                 $sWhat =  "oxid";
01114 
01115             if ( is_numeric( $sUser ) ) {
01116                 $sSelect =  "select $sWhat from oxuser where oxuser.oxactive = 1 and ($sPasswdSelect) and oxuser.oxcustnr = $sUser $sShopSelect";
01117             } else {
01118                 $sSelect =  "select $sWhat from oxuser where oxuser.oxactive = 1 and ($sPasswdSelect) and oxuser.oxusername = " . oxDb::getDb()->Quote( $sUser ) . " $sShopSelect";
01119             }
01120 
01121             if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
01122                 if ( $sPassword == "admin" && $sUser == "admin" ) {
01123                     $sSelect =  "select $sWhat from oxuser where oxrights = 'malladmin' $sShopSelect ";
01124                 } else {
01125                     $oEx = oxNew( 'oxUserException' );
01126                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01127                     throw $oEx;
01128                 }
01129             }
01130 
01131             // load from DB
01132             $aData = oxDb::getDb()->GetAll( $sSelect );
01133             $sOXID = @$aData[0][0];
01134             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01135 
01136                 if ( !$this->load( $sOXID ) ) {
01137                     $oEx = oxNew( 'oxUserException' );
01138                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01139                     throw $oEx;
01140                 }
01141             }
01142         }
01143 
01144 
01145         //login successfull?
01146         if ( $this->oxuser__oxid->value ) {   // yes, successful login
01147             if ( $this->isAdmin() ) {
01148                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01149             } else {
01150                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01151             }
01152 
01153             // cookie must be set ?
01154             if ( $blCookie ) {
01155                 $this->_setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopID() );
01156             }
01157             return true;
01158         } else {
01159             $oEx = oxNew( 'oxUserException' );
01160             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01161             throw $oEx;
01162         }
01163     }
01164 
01170     public function logout()
01171     {
01172         // deleting session info
01173         oxSession::deleteVar( 'usr' );  // for front end
01174         oxSession::deleteVar( 'auth' ); // for back end
01175         oxSession::deleteVar( 'dgr' );
01176         oxSession::deleteVar( 'dynvalue' );
01177         oxSession::deleteVar( 'paymentid' );
01178         // oxSession::deleteVar( 'deladrid' );
01179 
01180         // delete cookie
01181         $this->_deleteUserCookie( $this->getConfig()->getShopID() );
01182 
01183         // unsetting global user
01184         $this->setUser( null );
01185 
01186         return true;
01187     }
01188 
01195     public static function getAdminUser()
01196     {
01197         return self::getActiveUser( true );
01198     }
01199 
01208     public static function getActiveUser( $blForceAdmin = false )
01209     {
01210         $myConfig = oxConfig::getInstance();
01211 
01212         $blAdmin = $myConfig->isAdmin() || $blForceAdmin;
01213         $oDB = oxDb::getDb();
01214 
01215         // first - checking session info
01216         if ( $blAdmin )
01217             $sUserID = oxSession::getVar( 'auth' );
01218         else
01219             $sUserID = oxSession::getVar( 'usr' );
01220 
01221         $blFoundInCookie = false;
01222 
01223         //trying automatic login (by 'remember me' cookie)
01224         if ( !$sUserID && !$blAdmin ) {
01225             $sShopID = $myConfig->getShopId();
01226             $sSet = self::_getUserCookie( $sShopID );
01227 
01228             if ( $sSet ) {
01229                 $aData = explode( '@@@', $sSet );
01230                 $sUser = $oDB->quote( $aData[0] );
01231                 $sPWD  = @$aData[1];
01232 
01233                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = '.$sUser;
01234 
01235 
01236                 $oDB = oxDb::getDb();
01237                 $rs = $oDB->execute( $sSelect );
01238                 if ( $rs != false && $rs->recordCount() > 0 ) {
01239                     while (!$rs->EOF) {
01240                         $sTest = crypt( $rs->fields[1], 'ox' );
01241                         if ( $sTest == $sPWD ) {
01242                             // found
01243                             $sUserID = $rs->fields[0];
01244                             $blFoundInCookie = true;
01245                             break;
01246                         }
01247                         $rs->moveNext();
01248                     }
01249                 }
01250             }
01251         }
01252 
01253         // checking user results
01254         if ( $sUserID ) {
01255             $oUser = new oxuser;
01256             if ( $oUser->load( $sUserID ) ) {
01257 
01258                 // storing into session
01259                 if ($blAdmin) {
01260                     oxSession::setVar( 'auth', $sUserID );
01261                 } else {
01262                     oxSession::setVar( 'usr', $sUserID );
01263                 }
01264 
01265                 // marking the way user was loaded
01266                 $oUser->blLoadedFromCookie = $blFoundInCookie;
01267 
01268                 return $oUser;
01269             }
01270         } else {
01271             // no user
01272             oxSession::deleteVar( 'usr' );
01273             oxSession::deleteVar( 'auth' );
01274 
01275             return false;
01276         }
01277     }
01278 
01291     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01292     {
01293         include "oxldap.php";
01294         $myConfig = $this->getConfig();
01295         //$throws oxConnectionException
01296         $aLDAPParams = $myConfig->getConfigParam( 'aLDAPParams' );
01297         $oLDAP = new oxLDAP( $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01298         // maybe this is LDAP user but supplied email Address instead of LDAP login
01299         $sLDAPKey = oxDb::getDb()->GetOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".oxDb::getDb()->Quote($sUser)." $sShopSelect");
01300         if( isset( $sLDAPKey) && $sLDAPKey) {
01301             $sUser = $sLDAPKey;
01302         }
01303 
01304         //$throws oxConnectionException
01305         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01306 
01307         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01308         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {   // login successful
01309 
01310             // check if user is already in database
01311             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".oxDb::getDb()->Quote($aData['OXUSERNAME'])." $sShopSelect";
01312             $sOXID = oxDb::getDb()->GetOne( $sSelect);
01313 
01314             if ( !isset( $sOXID) || !$sOXID) {   // we need to create a new user
01315                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01316                 $this->setId();
01317 
01318                 // map all user data fields
01319                 foreach ( $aData as $fldname => $value) {
01320                     $sField = "oxuser__".strtolower( $fldname);
01321                     $this->$sField->setValue($aData[$fldname]);
01322                 }
01323 
01324                 $this->oxuser__oxactive->setValue(1);
01325                 $this->oxuser__oxshopid->setValue($sShopID);
01326                 $this->oxuser__oxldapkey->setValue($sUser);
01327                 $this->oxuser__oxrights->setValue("user");
01328                 $this->setPassword( "ldap user" );
01329 
01330                 $this->save();
01331             } else {   // LDAP user is already in OXID DB, load it
01332                 $this->load( $sOXID);
01333             }
01334 
01335         } else {
01336             $oEx = oxNew( 'oxUserException' );
01337             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01338             throw $oEx;
01339         }
01340     }
01341 
01348     protected function _getUserRights()
01349     {
01350         // previously user had no rights defined
01351         if ( !$this->oxuser__oxrights->value )
01352             return 'user';
01353 
01354         $oDB = oxDb::getDb();
01355         $myConfig    = $this->getConfig();
01356         $sAuthRights = null;
01357 
01358         // choosing possible user rights index
01359         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01360         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01361         if ( $sAuthUserID ) {
01362             $sAuthUserID = $oDB->quote( $sAuthUserID );
01363             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$sAuthUserID );
01364         }
01365 
01366         //preventing user rights edit for non admin
01367         $aRights = array();
01368 
01369         // selecting current users rights ...
01370         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01371             $aRights[] = $sCurrRights;
01372         }
01373         $aRights[] = 'user';
01374 
01375         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01376             return current( $aRights );
01377         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01378             $aRights[] = $sAuthRights;
01379             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01380                 return current( $aRights );
01381             }
01382         }
01383 
01384         // leaving as it was set ...
01385         return $this->oxuser__oxrights->value;
01386     }
01387 
01397     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01398     {
01399 
01400         /*if ( !$myConfig->blMallUsers ) {
01401             $sShopID = $myConfig->getShopId();
01402             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01403         }*/
01404 
01405         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01406     }
01407 
01413     protected function _insert()
01414     {
01415 
01416         // set oxcreate date
01417         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01418 
01419         if ( !isset( $this->oxuser__oxboni->value ) ) {
01420             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01421         }
01422 
01423         if ( $blInsert = parent::_insert() ) {
01424             // setting customer number
01425             if ( !$this->oxuser__oxcustnr->value || !$this->isAdmin() ) {
01426                 $this->_setRecordNumber( 'oxcustnr' );
01427             }
01428         }
01429 
01430         return $blInsert;
01431     }
01432 
01438     protected function _update()
01439     {
01440         // don't change this field
01441         $this->_aSkipSaveFields[] = 'oxcreate';
01442         if ( !$this->isAdmin() ) {
01443             $this->_aSkipSaveFields[] = 'oxcustnr';
01444             $this->_aSkipSaveFields[] = 'oxrights';
01445         }
01446 
01447         // updating subscription information
01448         if ( ( $blUpdate = parent::_update() ) ) {
01449             $this->getNewsSubscription()->updateSubscription( $this );
01450         }
01451 
01452         return $blUpdate;
01453     }
01454 
01470     protected function _checkLogin( &$sLogin, $aInvAddress )
01471     {
01472         $myConfig = $this->getConfig();
01473 
01474         $sLogin   = ( isset( $aInvAddress['oxuser__oxusername'] ) && $aInvAddress['oxuser__oxusername'] )?$aInvAddress['oxuser__oxusername']:$sLogin;
01475 
01476         // check only for users with password during registration
01477         // if user wants to change user name - we must check if passwords are ok before changing
01478         if ( $this->oxuser__oxpassword->value && $sLogin != $this->oxuser__oxusername->value ) {
01479 
01480             // on this case password must be taken directly from request
01481             $sNewPass = isset( $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
01482             if ( !$sNewPass ) {
01483 
01484                 // 1. user forgot to enter password
01485                 $oEx = oxNew( 'oxInputException' );
01486                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01487                 throw $oEx;
01488             } else {
01489 
01490                 // 2. entered wrong password
01491                 if ( !$this->isSamePassword( $sNewPass ) ) {
01492                     $oEx = oxNew( 'oxUserException' );
01493                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01494                     throw $oEx;
01495                 }
01496             }
01497         }
01498 
01499         if ( $this->checkIfEmailExists( $sLogin)) {
01500             //if exists then we do now allow to do that
01501             $oEx = oxNew( 'oxUserException' );
01502             $oEx->setMessage('EXCEPTION_USER_USEREXISTS');
01503             throw $oEx;
01504         }
01505     }
01506 
01514     public function checkIfEmailExists( $sEmail )
01515     {
01516         $myConfig = $this->getConfig();
01517         $oDB = oxDb::getDb();
01518         $iShopId = $myConfig->getShopId();
01519         $blExists = false;
01520 
01521         $sPassHash = oxUtils::getInstance()->strMan( '', $myConfig->getConfigParam( 'sConfigKey' ) );
01522 
01523         $sQ = 'select oxshopid, oxrights, oxpassword, oxpassword like "ox_%", oxpassword like "'.$sPassHash.'"
01524                from oxuser where oxusername = '. $oDB->quote( $sEmail );
01525 
01526         if ( ( $sOxid = $this->getId() ) ) {
01527             $sQ .= " and oxid <> '$sOxid' ";
01528         }
01529 
01530         $oRs = $oDB->execute( $sQ );
01531         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01532 
01533             if ( $this->_blMallUsers ) {
01534 
01535                 $blExists = true;
01536                 if ( $oRs->fields[1] == 'user' && ( ( !$oRs->fields[3] && !$oRs->fields[2] ) || ( $oRs->fields[3] && $oRs->fields[4] ) ) ) {
01537 
01538                     // password is not set - allow to override
01539                     $blExists = false;
01540                 }
01541             } else {
01542 
01543                 $blExists = false;
01544                 while ( !$oRs->EOF ) {
01545                     if ( $oRs->fields[1] != 'user' ) {
01546 
01547                         // exists admin with same login - must not allow
01548                         $blExists = true;
01549                         break;
01550                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] && !$oRs->fields[4] ) {
01551 
01552                         // exists same login (with password) in same shop
01553                         $blExists = true;
01554                         break;
01555                     }
01556 
01557                     $oRs->moveNext();
01558                 }
01559             }
01560         }
01561         return $blExists;
01562     }
01563 
01571     public function getUserRecommLists( $sOXID = null )
01572     {
01573         if ( !$sOXID )
01574             $sOXID = $this->getId();
01575 
01576         // sets active page
01577         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01578         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01579 
01580         // load only lists which we show on screen
01581         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01582         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01583 
01584 
01585         $oRecommList = oxNew( 'oxlist' );
01586         $oRecommList->init( 'oxrecommlist' );
01587         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01588         $iShopId = $this->getConfig()->getShopId();
01589         $sSelect = 'select * from oxrecommlists where oxuserid ="'. $sOXID .'" and oxshopid ="'. $iShopId .'"';
01590         $oRecommList->selectString( $sSelect );
01591 
01592         return $oRecommList;
01593     }
01594 
01602     public function getRecommListsCount( $sOx = null )
01603     {
01604         if ( !$sOx ) {
01605             $sOXID = $this->getId();
01606         }
01607 
01608         if ( $this->_iCntRecommLists === null || $sOx ) {
01609             $this->_iCntRecommLists = 0;
01610             $iShopId = $this->getConfig()->getShopId();
01611             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid ="'. $sOXID .'" and oxshopid ="'. $iShopId .'"';
01612             $this->_iCntRecommLists = oxDb::getDb()->getOne( $sSelect );
01613         }
01614         return $this->_iCntRecommLists;
01615     }
01616 
01625     protected function _checkEmail( $sEmail )
01626     {
01627         // missing email address (user login name) ?
01628         if ( !$sEmail ) {
01629             $oEx = oxNew( 'oxInputException' );
01630             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01631             throw $oEx;
01632         }
01633 
01634         // invalid email address ?
01635         if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
01636             $oEx = oxNew( 'oxInputException' );
01637             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
01638             throw $oEx;
01639         }
01640     }
01641 
01654     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01655     {
01656 
01657         //  password is too short ?
01658         if ( $blCheckLenght &&  strlen( $sNewPass ) < 6 ) {
01659             $oEx = oxNew( 'oxInputException' );
01660             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
01661             throw $oEx;
01662         }
01663 
01664         //  passwords do not match ?
01665         if ( $sNewPass != $sConfPass ) {
01666             $oEx = oxNew( 'oxUserException' );
01667             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01668             throw $oEx;
01669         }
01670     }
01671 
01683     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01684     {
01685         // collecting info about required fields
01686         $aMustFields = array( 'oxuser__oxfname',
01687                               'oxuser__oxlname',
01688                               'oxuser__oxstreetnr',
01689                               'oxuser__oxstreet',
01690                               'oxuser__oxzip',
01691                               'oxuser__oxcity' );
01692 
01693         // config shoud override default fields
01694         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
01695         if ( is_array( $aMustFillFields ) ) {
01696             $aMustFields = $aMustFillFields;
01697         }
01698 
01699         // assuring data to check
01700         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
01701         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
01702 
01703         // collecting fields
01704         $aFields = array_merge( $aInvAddress, $aDelAddress );
01705 
01706         // check delivery address ?
01707         $blCheckDel = false;
01708         if ( count( $aDelAddress ) ) {
01709             $blCheckDel = true;
01710         }
01711 
01712         // checking
01713         foreach ( $aMustFields as $sMustField ) {
01714 
01715             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
01716             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
01717                 continue;
01718             }
01719 
01720             if ( !isset( $aFields[$sMustField] ) || ! trim( $aFields[$sMustField] ) ) {
01721                    $oEx = oxNew( 'oxInputException' );
01722                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01723                    throw $oEx;
01724             }
01725         }
01726     }
01727 
01738     protected function _checkVatId( $aInvAddress )
01739     {
01740         // vat ID must be checked only for business customers
01741         if ( $aInvAddress['oxuser__oxustid'] && $aInvAddress['oxuser__oxcompany'] ) {
01742 
01743             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
01744                 // no country
01745                 return;
01746             }
01747             $oCountry = oxNew('oxcountry');
01748             if (!$oCountry->load($sCountryId)) {
01749                 throw new oxObjectException();
01750             }
01751             if ($oCountry->isForeignCountry() && $oCountry->isInEU()) {
01752                     if (strncmp($aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2)) {
01753                         $oEx = oxNew( 'oxInputException' );
01754                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
01755                         throw $oEx;
01756                     }
01757             }
01758 
01759         }
01760     }
01761 
01770     protected function _setAutoGroups( $sCountryId )
01771     {
01772         // assigning automatically to specific groups
01773         $blForeigner = true;
01774         $blForeignGroupExists = false;
01775         $blInlandGroupExists = false;
01776 
01777         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01778         // foreigner ?
01779         if ( is_array($aHomeCountry)) {
01780             if (in_array($sCountryId, $aHomeCountry)) {
01781                 $blForeigner = false;
01782             }
01783         } elseif ($sCountryId == $aHomeCountry) {
01784             $blForeigner = false;
01785         }
01786 
01787         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01788             $blForeignGroupExists = true;
01789             if ( !$blForeigner ) {
01790                 $this->removeFromGroup( 'oxidforeigncustomer' );
01791             }
01792         }
01793 
01794         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01795             $blInlandGroupExists = true;
01796             if ( $blForeigner ) {
01797                 $this->removeFromGroup( 'oxidnewcustomer' );
01798             }
01799         }
01800 
01801         if ( !$this->oxuser__oxdisableautogrp->value ) {
01802             if ( !$blForeignGroupExists && $blForeigner ) {
01803                 $this->addToGroup( 'oxidforeigncustomer' );
01804             }
01805             if ( !$blInlandGroupExists && !$blForeigner ) {
01806                 $this->addToGroup( 'oxidnewcustomer' );
01807             }
01808         }
01809     }
01810 
01818     protected function _hasUserAddress( $sUserId )
01819     {
01820 
01821         $oAddresses = $this->getUserAddresses();
01822         if ( $oAddresses && count($oAddresses)>0 ) {
01823             $oAddresses->rewind() ;
01824             foreach ($oAddresses as $key => $oAddress) {
01825                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01826                     return true;
01827                 }
01828             }
01829         }
01830         return false;
01831     }
01832 
01843     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
01844     {
01845         if ( !$sShopId ) {
01846             $sShopId = $this->getConfig()->getShopID();
01847         }
01848 
01849         self::$_aUserCookie[$sShopId] = $sUser . '@@@' . crypt( $sPassword, 'ox' );
01850         oxUtilsServer::getInstance()->setOxCookie( 'oxid_'.$sShopId, self::$_aUserCookie[$sShopId], time() + $iTimeout, '/' );
01851         //oxUtils::getInstance()->setCookie( 'oxid_'.$sShopId, $sSet);
01852     }
01853 
01861     protected function _deleteUserCookie( $sShopId = null )
01862     {
01863         if ( !$sShopId ) {
01864             $sShopId = $this->getConfig()->getShopID();
01865         }
01866 
01867         self::$_aUserCookie[$sShopId] = '';
01868         oxUtilsServer::getInstance()->setOxCookie( 'oxid_'.$sShopId, '', time() - 3600, '/' );
01869     }
01870 
01878     protected static function _getUserCookie( $sShopId = null )
01879     {
01880         if (self::$_aUserCookie[$sShopId] !== null) {
01881             if (!self::$_aUserCookie[$sShopId]) {
01882                 // cookie has been deleted
01883                 return null;
01884             }
01885             return self::$_aUserCookie[$sShopId];
01886         }
01887 
01888         if ( !$sShopId ) {
01889             $sShopId = parent::getConfig()->getShopID();
01890         }
01891 
01892         self::$_aUserCookie[$sShopId] = oxUtilsServer::getInstance()->getOxCookie( 'oxid_'.$sShopId );
01893 
01894         return self::$_aUserCookie[$sShopId];
01895     }
01896 
01897 
01906     public function loadUserByUpdateId( $sUid )
01907     {
01908         $oDb = oxDb::getDb();
01909         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= UNIX_TIMESTAMP() and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
01910         if ( $sUserId = $oDb->getOne( $sQ ) ) {
01911             return $this->load( $sUserId );
01912         }
01913     }
01914 
01922     public function setUpdateKey( $blReset = false )
01923     {
01924         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
01925         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
01926 
01927         // generating key
01928         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
01929 
01930         // setting expiration time for 6 hours
01931         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
01932 
01933         // saving
01934         $this->save();
01935     }
01936 
01942     public function getUpdateLinkTerm()
01943     {
01944         return 3600 * 6;
01945     }
01946 
01954     public function isExpiredUpdateKey( $sKey )
01955     {
01956         $oDb = oxDb::getDb();
01957         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= UNIX_TIMESTAMP() and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
01958         return !( (bool) $oDb->getOne( $sQ ) );
01959     }
01960 
01966     public function getUpdateId()
01967     {
01968         if ( $this->_sUpdateKey === null ) {
01969             $this->setUpdateKey();
01970             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
01971         }
01972         return $this->_sUpdateKey;
01973     }
01974 
01982     protected function _encodePassword( $sPassword )
01983     {
01984         return md5( $sPassword . oxConfig::getInstance()->getConfigParam( 'sPasswdSalt' ) );
01985     }
01986 
01994     public function setPassword( $sPassword = null )
01995     {
01996         // encoding only if password was not empty (e.g. user registration without pass)
01997         $sPassword = $sPassword ? $this->_encodePassword( $sPassword ) : '';
01998         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
01999     }
02000 
02008     public function isSamePassword( $sNewPass )
02009     {
02010         $oMyUtils = oxUtils::getInstance();
02011         return ( $oMyUtils->strMan( $sNewPass, $this->getConfig()->getConfigParam( 'sConfigKey' ) ) == $this->oxuser__oxpassword->value ) ||
02012                ( $sNewPass == $this->oxuser__oxpassword->value && strpos( $sNewPass, 'ox_' ) === false ) ||
02013                ( $this->_encodePassword( $sNewPass ) == $this->oxuser__oxpassword->value );
02014     }
02015 }

Generated on Thu Dec 4 12:04:57 2008 for OXID eShop CE by  doxygen 1.5.5