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                 break;
00157             case 'iCntNoticeListArticles':
00158                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00159                 break;
00160             case 'iCntWishListArticles':
00161                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00162                 break;
00163             case 'iCntRecommLists':
00164                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00165                 break;
00166             case 'oAddresses':
00167                 return $this->_oAddresses = $this->getUserAddresses();
00168                 break;
00169             case 'oPayments':
00170                 return $this->_oPayments = $this->getUserPayments();
00171                 break;
00172             case 'oxuser__oxcountry':
00173                 return $this->oxuser__oxcountry = $this->getUserCountry();
00174                 break;
00175             case 'sDBOptin':
00176                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00177                 break;
00178             case 'sEmailFailed':
00179                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00180                 break;
00181         }
00182     }
00183 
00189     public function getNewsSubscription()
00190     {
00191         if ( $this->_oNewsSubscription !== null ) {
00192             return $this->_oNewsSubscription;
00193         }
00194 
00195         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00196 
00197         // if subscription object is not set yet - we should create one
00198         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00199             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00200 
00201                 // no subscription defined yet - creating one
00202                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00203                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00204                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00205                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00206                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00207             }
00208         }
00209 
00210         return $this->_oNewsSubscription;
00211     }
00212 
00222     public function getUserCountry( $sCountryId = null, $iLang = null )
00223     {
00224 
00225         if ( !$sCountryId ) {
00226             $sCountryId = $this->oxuser__oxcountryid->value;
00227         }
00228 
00229         $sQ = 'select oxtitle'.oxLang::getInstance()->getLanguageTag( $iLang ).' from oxcountry where oxid = "'.$sCountryId.'" ';
00230         $this->oxuser__oxcountry = new oxField( oxDb::getDb()->getOne( $sQ ), oxField::T_RAW);
00231 
00232         return $this->oxuser__oxcountry;
00233     }
00234 
00242     public function getUserCountryId( $sCountry = null )
00243     {
00244         $sQ = 'select oxid from oxcountry where oxactive = "1" and oxisoalpha2 = "' . $sCountry . '"';
00245         $sCountryId = oxDb::getDb()->getOne( $sQ );
00246 
00247         return $sCountryId;
00248     }
00249 
00257     public function getUserGroups( $sOXID = null )
00258     {
00259 
00260         if ( isset( $this->_oGroups ) ) {
00261             return $this->_oGroups;
00262         }
00263 
00264         if ( !$sOXID ) {
00265             $sOXID = $this->getId();
00266         }
00267 
00268         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00269         //$this->oGroups->Init( 'oxbase' );
00270         //$this->oGroups->oLstoTpl->Init( array( 'oxgroups', 'oxobject2group' ) );
00271         $sSelect  = 'select oxgroups.* from oxgroups left join oxobject2group on oxobject2group.oxgroupsid = oxgroups.oxid ';
00272         $sSelect .= 'where oxobject2group.oxobjectid = "'.$sOXID.'" ';
00273         $this->_oGroups->selectString( $sSelect );
00274         return $this->_oGroups;
00275     }
00276 
00284     public function getUserAddresses( $sUserId = null )
00285     {
00286 
00287         if ( $this->_oAddresses == null ) {
00288 
00289             $sSelect = "select * from oxaddress where oxaddress.oxuserid = '".( $sUserId ? $sUserId : $this->getId() )."'";
00290 
00291             //P
00292             $this->_oAddresses = oxNew( 'oxlist' );
00293             $this->_oAddresses->init( "oxbase", "oxaddress" );
00294             $this->_oAddresses->selectString( $sSelect );
00295 
00296             // marking selected
00297             if ( $sAddressId = $this->getSelectedAddressId() ) {
00298                 foreach ( $this->_oAddresses as $oAddress ) {
00299                     $oAddress->selected = 0;
00300                     if ( $oAddress->getId() == $sAddressId ) {
00301                         $oAddress->selected = 1;
00302                         break;
00303                     }
00304                 }
00305             }
00306         }
00307         return $this->_oAddresses;
00308     }
00309 
00315     public function getSelectedAddressId()
00316     {
00317         if ( !( $sAddressId = oxConfig::getParameter( "oxaddressid") ) ) {
00318             $sAddressId = oxSession::getVar( "deladrid" );
00319         }
00320         return $sAddressId;
00321     }
00322 
00331     public function getSelectedAddress( $sWishId = false )
00332     {
00333         $oAddresses = $this->getUserAddresses();
00334         if ( $oAddresses->count() ) {
00335             if ( $sAddressId = $this->getSelectedAddressId() ) {
00336                 foreach ( $oAddresses as $oAddress ) {
00337                     if ( $oAddress->selected == 1 ) {
00338                         $sAddressId = $oAddress->getId();
00339                         break;
00340                     }
00341                 }
00342             } elseif ( $sWishId ) {
00343                 foreach ( $oAddresses as $oAddress ) {
00344                     $oAddress->selected = 0;
00345                     if ( $oAddress->oxaddress__oxaddressuserid->value == $sWishId ) {
00346                         $oAddress->selected = 1;
00347                         $sAddressId = $oAddress->getId();
00348                     }
00349                 }
00350             }
00351 
00352             // in case none is set - setting first one
00353             if ( !$sAddressId ) {
00354                 $oAddresses->rewind();
00355                 $oAddress = $oAddresses->current();
00356                 $oAddress->selected = 1;
00357                 $sAddressId = $oAddress->getId();
00358             }
00359         }
00360 
00361         return $sAddressId;
00362     }
00363 
00371     public function getUserPayments( $sOXID = null )
00372     {
00373         if ( $this->_oPayments === null ) {
00374 
00375             if ( !$sOXID ) {
00376                 $sOXID = $this->getId();
00377             }
00378 
00379             $sSelect = 'select * from oxuserpayments where oxuserid ="'. $sOXID .'"';
00380 
00381             $this->_oPayments = oxNew( 'oxlist' );
00382             $this->_oPayments->init( 'oxUserPayment' );
00383             $this->_oPayments->selectString( $sSelect );
00384 
00385             $myUtils = oxUtils::getInstance();
00386             foreach ( $this->_oPayments as $oPayment ) {
00387                 // add custom fields to this class
00388                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00389             }
00390         }
00391 
00392         return $this->_oPayments;
00393     }
00394 
00400     public function save()
00401     {
00402 
00403         $blAddRemark = false;
00404         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00405             $blAddRemark = true;
00406             //save oxregister value
00407             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00408         }
00409 
00410         // setting user rights
00411         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00412 
00413         // processing birth date which came from output as array
00414         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00415             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00416         }
00417 
00418         $blRet = parent::save();
00419 
00420         //add registered remark
00421         if ( $blAddRemark && $blRet ) {
00422             $oRemark = oxNew( 'oxremark' );
00423             $oRemark->oxremark__oxtext     = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00424             $oRemark->oxremark__oxtype     = new oxField('r', oxField::T_RAW);
00425             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00426             $oRemark->save();
00427         }
00428 
00429         return $blRet;
00430     }
00431 
00437     public function allowDerivedUpdate()
00438     {
00439         return true;
00440     }
00441 
00449     public function inGroup( $sGroupID )
00450     {
00451         $blIn = false;
00452         if ( ( $oGroups = $this->getUserGroups() ) ) {
00453             $blIn = isset( $oGroups[ $sGroupID ] );
00454         }
00455 
00456         return $blIn;
00457     }
00458 
00467     public function delete( $sOXID = null )
00468     {
00469 
00470         if ( !$sOXID ) {
00471             $sOXID = $this->getId();
00472         }
00473         if ( !$sOXID ) {
00474             return false;
00475         }
00476 
00477         $blDeleted = parent::delete( $sOXID );
00478 
00479         if ( $blDeleted ) {
00480             $oDB = oxDb::getDb();
00481 
00482             // deleting stored payment, address, group dependencies, remarks info
00483             $rs = $oDB->execute( 'delete from oxaddress where oxaddress.oxuserid = "'.$sOXID.'" ' );
00484             $rs = $oDB->execute( 'delete from oxobject2group where oxobject2group.oxobjectid = "'.$sOXID.'" ');
00485 
00486             // deleting notice/wish lists
00487             $rs = $oDB->execute( 'delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = "'.$sOXID.'" ' );
00488             $rs = $oDB->execute( 'delete from oxuserbaskets where oxuserid = "'.$sOXID.'" ' );
00489 
00490             // deleting Newsletter subscription
00491             $rs = $oDB->execute( 'delete from oxnewssubscribed where oxuserid = "'.$sOXID.'" ');
00492 
00493             // and leaving all order related information
00494             $rs = $oDB->execute( 'delete from oxremark where oxparentid = "'.$sOXID.'" and oxtype !="o"' );
00495 
00496             $blDeleted = $rs->EOF;
00497         }
00498 
00499         return $blDeleted;
00500     }
00501 
00509     public function load( $oxID )
00510     {
00511 
00512         $blRet = parent::Load( $oxID );
00513 
00514         // convert date's to international format
00515         if ( isset( $this->oxuser__oxcreate->value ) ) {
00516             $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00517         }
00518 
00519         return $blRet;
00520     }
00521 
00529     public function exists( $sOXID = null )
00530     {
00531         if ( !$sOXID ) {
00532             $sOXID = $this->getId();
00533         }
00534 
00535         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00536                     WHERE ( oxusername = "'.$this->oxuser__oxusername->value.'"';
00537 
00538         if ( $sOXID ) {
00539             $sSelect.= " or oxid = '$sOXID' ) ";
00540         } else {
00541             $sSelect.= ' ) ';
00542         }
00543 
00544         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00545             $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00546         }
00547 
00548         $blExists = false;
00549         if ( ( $sOxid = oxDb::getDb()->getOne( $sSelect ) ) ) {
00550              // update - set oxid
00551             $this->setId( $sOxid );
00552             $blExists = true;
00553         }
00554         return $blExists;
00555     }
00556 
00562     public function getOrders()
00563     {
00564         $myConfig = $this->getConfig();
00565         $oOrders = oxNew( 'oxlist' );
00566         $oOrders->init( 'oxorder' );
00567 
00568         //P
00569         // Lists does not support loading from two tables, so orders
00570         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00571         // forcing to load product info which is used in templates
00572         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00573 
00574         //loading order for registered user
00575         if ( $this->oxuser__oxregister->value > 1 ) {
00576             $sQ = 'select * from oxorder where oxuserid = "'.$this->getId().'" and oxorderdate >= "'.$this->oxuser__oxregister->value.'" ';
00577 
00578             //#1546 - shopid check added, if it is not multishop
00579 
00580             $sQ .= ' order by oxorderdate desc ';
00581             $oOrders->selectString( $sQ );
00582         }
00583 
00584         return $oOrders;
00585     }
00586 
00592     public function getOrderCount()
00593     {
00594         $iCnt = 0;
00595         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00596             $sQ  = 'select count(*) from oxorder where oxuserid = "'.$this->getId().'" AND oxorderdate >= "'.$this->oxuser__oxregister->value.'" and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00597             $iCnt = (int) oxDb::getDb()->getOne( $sQ );
00598         }
00599 
00600         return $iCnt;
00601     }
00602 
00608     public function getNoticeListArtCnt()
00609     {
00610         if ( $this->_iCntNoticeListArticles === null ) {
00611             $this->_iCntNoticeListArticles = 0;
00612             if ( $this->getId() ) {
00613                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00614             }
00615         }
00616         return $this->_iCntNoticeListArticles;
00617     }
00618 
00624     public function getWishListArtCnt()
00625     {
00626         if ( $this->_iCntWishListArticles === null ) {
00627             $this->_iCntWishListArticles = false;
00628             if ( $this->getId() ) {
00629                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00630             }
00631         }
00632         return $this->_iCntWishListArticles;
00633     }
00634 
00641     public function getActiveCountry()
00642     {
00643         $sDeliveryCountry = '';
00644         if ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) {
00645             $oDelAddress = oxNew( 'oxbase' );
00646             $oDelAddress->init( 'oxaddress' );
00647             $oDelAddress->load( $soxAddressId );
00648             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00649         } elseif ( $this->getId() ) {
00650             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00651         } else {
00652             $oUser = oxNew( 'oxuser' );
00653             if ( $oUser->loadActiveUser() ) {
00654                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00655             }
00656         }
00657 
00658         return $sDeliveryCountry;
00659     }
00660 
00668     public function createUser()
00669     {
00670         $oDB = oxDb::getDb();
00671         $sShopID = $this->getConfig()->getShopId();
00672 
00673         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00674         $sSelect = "select oxid from oxuser where oxusername = '{$this->oxuser__oxusername->value}' and oxpassword = '' ";
00675         if ( !$this->_blMallUsers ) {
00676             $sSelect .= " and oxshopid = '{$sShopID}' ";
00677         }
00678         $sOXID = $oDB->getOne( $sSelect );
00679 
00680         // user without password found - lets use
00681         if ( isset( $sOXID ) && $sOXID ) {
00682             // try to update
00683             $this->delete( $sOXID );
00684         } elseif ( $this->_blMallUsers ) { // must be sure if there is no dublicate user
00685             $sQ = "select oxid from oxuser where oxusername = '{$this->oxuser__oxusername->value}' and oxusername != '' ";
00686             if ( $oDB->getOne( $sQ ) ) {
00687                 $oEx = oxNew( 'oxUserException' );
00688                 $oLang = oxLang::getInstance();
00689                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00690                 throw $oEx;
00691             }
00692         }
00693 
00694         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00695         if ( ( $blOK = $this->save() ) ) {
00696             // dropping/cleaning old delivery address/payment info
00697             $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = '{$this->oxuser__oxid->value}' " );
00698             $oDB->execute( "update oxuserpayments set oxuserpayments.oxuserid = '{$this->oxuser__oxusername->value}' where oxuserpayments.oxuserid = '{$this->oxuser__oxid->value}' " );
00699         } else {
00700             $oEx = oxNew( 'oxUserException' );
00701             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00702             throw $oEx;
00703         }
00704 
00705         return $blOK;
00706     }
00707 
00715     public function addToGroup( $sGroupID )
00716     {
00717         if ( !$this->inGroup( $sGroupID ) ) {
00718             $oNewGroup = oxNew( 'oxobject2group' );
00719             $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00720             $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00721             if ( $oNewGroup->save() ) {
00722                 $this->_oGroups[$sGroupID] = $oNewGroup;
00723                 return true;
00724             }
00725         }
00726         return false;
00727     }
00728 
00736     public function removeFromGroup( $sGroupID = null )
00737     {
00738         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00739             $oGroups = oxNew( 'oxlist' );
00740             $oGroups->init( 'oxobject2group' );
00741             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00742             $oGroups->selectString( $sSelect );
00743             foreach ( $oGroups as $oRemgroup ) {
00744                 if ( $oRemgroup->delete() ) {
00745                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00746                 }
00747             }
00748         }
00749     }
00750 
00759     public function onOrderExecute( $oBasket, $iSuccess )
00760     {
00761 
00762         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00763             //adding user to particular customer groups
00764             if ( !$this->oxuser__oxdisableautogrp->value ) {
00765 
00766                 $myConfig = $this->getConfig();
00767                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00768                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00769 
00770                 $this->addToGroup( 'oxidcustomer' );
00771                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00772                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00773                     $this->addToGroup( 'oxidsmallcust' );
00774                 }
00775                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00776                     $this->addToGroup( 'oxidmiddlecust' );
00777                 }
00778                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00779                     $this->addToGroup( 'oxidgoodcust' );
00780                 }
00781             }
00782 
00783                 if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00784                     $this->removeFromGroup( 'oxidnotyetordered' );
00785                 }
00786         }
00787     }
00788 
00796     public function getBasket( $sName )
00797     {
00798         if ( !isset( $this->_aBaskets[$sName] ) ) {
00799             $oBasket = oxNew( 'oxuserbasket' );
00800             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00801 
00802             // creating if it does not exist
00803             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00804                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00805                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00806 
00807                 // marking basket as new (it will not be saved in DB yet)
00808                 $oBasket->setIsNewBasket();
00809             }
00810 
00811             $this->_aBaskets[$sName] = $oBasket;
00812         }
00813 
00814         return $this->_aBaskets[$sName];
00815     }
00816 
00825     public function convertBirthday( $aData )
00826     {
00827 
00828         // preparing data to process
00829         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00830         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00831         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00832 
00833         // leaving empty if not set
00834         if ( !$iYear && !$iMonth && !$iDay )
00835             return "";
00836 
00837         // year
00838         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00839             $iYear = date('Y');
00840 
00841         // month
00842         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00843             $iMonth = 1;
00844 
00845         // maximum nuber of days in month
00846         $iMaxDays = 31;
00847         switch( $iMonth) {
00848             case 2 :
00849                 if ($iMaxDays > 28)
00850                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00851                 break;
00852             case 4  :
00853             case 6  :
00854             case 9  :
00855             case 11 :
00856                 $iMaxDays = min(30, $iMaxDays);
00857                 break;
00858         }
00859 
00860         // day
00861         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00862             $iDay = 1;
00863         }
00864 
00865         // whole date
00866         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00867     }
00868 
00875     public function getBoni()
00876     {
00877         return 1000;
00878     }
00879 
00892     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00893     {
00894         // preparing input
00895         $sDynGoup = strtolower( trim( $sDynGoup ) );
00896 
00897         // setting denied groups from admin settings also
00898         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
00899 
00900         // default state ..
00901         $blAdd = false;
00902 
00903         // user assignment to dyn group is not allowed
00904         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
00905             $blAdd = false;
00906         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
00907             // trying to add user to prohibited user group?
00908             $blAdd = false;
00909         } elseif ( $this->addToGroup( $sDynGoup ) ) {
00910             $blAdd = true;
00911         }
00912 
00913         // cleanup
00914         oxSession::deleteVar( 'dgr' );
00915 
00916         return $blAdd;
00917     }
00918 
00934     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
00935     {
00936         // 1. checking user name
00937         $this->_checkLogin( $sLogin, $aInvAddress );
00938 
00939         // 2. cheking email
00940         $this->_checkEmail( $sLogin );
00941 
00942         // 3. password
00943         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
00944 
00945         // 4. required fields
00946         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
00947 
00948         // 5. vat id check.
00949             $this->_checkVatId( $aInvAddress );
00950     }
00951 
00960     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
00961     {
00962         // assigning to newsletter
00963         $blSuccess = false;
00964         $myConfig  = $this->getConfig();
00965 
00966         // user wants to get newsletter messages or no ?
00967         $oNewsSubscription = $this->getNewsSubscription();
00968         if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
00969             if ( !$blSendOptIn ) {
00970 
00971                 // double-opt-in check is disabled - assigning automatically
00972                 $this->addToGroup( 'oxidnewsletter' );
00973                 // and setting subscribed status
00974                 $oNewsSubscription->setOptInStatus( 1 );
00975                 $blSuccess = true;
00976             } else {
00977 
00978                 // double-opt-in check enabled - sending confirmation email and setting waiting status
00979                 $oNewsSubscription->setOptInStatus( 2 );
00980 
00981                 // sending double-opt-in mail
00982                 $oEmail = oxNew( 'oxemail' );
00983                 $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
00984             }
00985         } elseif ( !$blSubscribe ) { // removing user from newsletter subscribers
00986             $this->removeFromGroup( 'oxidnewsletter' );
00987             $oNewsSubscription->setOptInStatus( 0 );
00988             $blSuccess = true;
00989         }
00990 
00991         return $blSuccess;
00992     }
00993 
01011     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01012     {
01013 
01014         // validating values before saving. If validation fails - exception is thrown
01015         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01016 
01017         // input data is fine - lets save updated user info
01018         $this->assign( $aInvAddress );
01019 
01020 
01021         // update old or add new delivery address
01022         $this->_assignAddress( $aDelAddress );
01023 
01024         // saving new values
01025         if ( $this->save() ) {
01026 
01027             // assigning automatically to specific groups
01028             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01029             $this->_setAutoGroups( $sCountryId );
01030         }
01031     }
01032 
01041     public function addUserAddress( $oUser )
01042     {
01043 
01044         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01045             return false;
01046         }
01047 
01048         $oAddress = oxNew( 'oxbase' );
01049         $oAddress->init( 'oxaddress' );
01050 
01051         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01052         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01053         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01054         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01055         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01056         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01057         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01058         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01059         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01060         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01061         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01062 
01063         // adding new address
01064         if ( $oAddress->save() ) {
01065             // resetting addresses
01066             $this->_oAddresses = null;
01067             return $oAddress->getId();
01068         }
01069     }
01070 
01078     protected function _assignAddress( $aDelAddress )
01079     {
01080         if ( ( isset( $aDelAddress['oxaddress__oxfname'] ) && $aDelAddress['oxaddress__oxfname'] ) ||
01081              ( isset( $aDelAddress['oxaddress__oxlname'] ) && $aDelAddress['oxaddress__oxlname'] ) ) {
01082 
01083             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01084             $aDelAddress['oxaddress__oxid'] = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01085 
01086             $oAddress = oxNew( 'oxbase' );
01087             $oAddress->init( 'oxaddress' );
01088             $oAddress->assign( $aDelAddress );
01089             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01090             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01091             $oAddress->save();
01092 
01093             // resetting addresses
01094             $this->_oAddresses = null;
01095 
01096             // saving delivery Address for later use
01097             oxSession::setVar( 'deladrid', $oAddress->getId() );
01098         } else {
01099             // resetting
01100             oxSession::setVar( 'deladrid', null );
01101         }
01102     }
01103 
01116     public function login( $sUser, $sPassword, $blCookie = false)
01117     {
01118         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01119             $oEx = oxNew( 'oxCookieException' );
01120             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01121             throw $oEx;
01122         }
01123 
01124         $myConfig = $this->getConfig();
01125         if ( $sPassword ) {
01126 
01127             $sShopID = $myConfig->getShopId();
01128             $oDb = oxDb::getDb();
01129 
01130             $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01131             $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01132             $sShopSelect = "";
01133 
01134 
01135             // admin view: can only login with higher than 'user' rights
01136             if ( $this->isAdmin() ) {
01137                 $sShopSelect = " and ( oxrights != 'user' ) ";
01138             }
01139 
01140             $sWhat = "oxid";
01141 
01142             $sSelect =  "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01143             if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
01144                 if ( $sPassword == "admin" && $sUser == "admin" ) {
01145                     $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01146                 } else {
01147                     $oEx = oxNew( 'oxUserException' );
01148                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01149                     throw $oEx;
01150                 }
01151             }
01152 
01153             // load from DB
01154             $aData = $oDb->getAll( $sSelect );
01155             $sOXID = @$aData[0][0];
01156             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01157 
01158                 if ( !$this->load( $sOXID ) ) {
01159                     $oEx = oxNew( 'oxUserException' );
01160                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01161                     throw $oEx;
01162                 }
01163             }
01164         }
01165 
01166 
01167         //login successfull?
01168         if ( $this->oxuser__oxid->value ) {   // yes, successful login
01169             if ( $this->isAdmin() ) {
01170                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01171             } else {
01172                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01173             }
01174 
01175             // cookie must be set ?
01176             if ( $blCookie ) {
01177                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01178             }
01179             return true;
01180         } else {
01181             $oEx = oxNew( 'oxUserException' );
01182             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01183             throw $oEx;
01184         }
01185     }
01186 
01197     public function openIdLogin( $sUser )
01198     {
01199         $myConfig = $this->getConfig();
01200         $sShopID = $myConfig->getShopId();
01201         $oDb = oxDb::getDb();
01202 
01203         $sUserSelect = "oxuser.oxusername = " . $oDb->quote( $sUser );
01204         $sShopSelect = "";
01205 
01206 
01207         $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01208 
01209         // load from DB
01210         $aData = $oDb->getAll( $sSelect );
01211         $sOXID = @$aData[0][0];
01212         if ( isset( $sOXID ) && $sOXID ) {
01213 
01214             if ( !$this->load( $sOXID ) ) {
01215                 $oEx = oxNew( 'oxUserException' );
01216                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01217                 throw $oEx;
01218             }
01219         }
01220 
01221         //login successfull?
01222         if ( $this->oxuser__oxid->value ) {   // yes, successful login
01223             oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01224             return true;
01225         } else {
01226             $oEx = oxNew( 'oxUserException' );
01227             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01228             throw $oEx;
01229         }
01230     }
01231 
01237     public function logout()
01238     {
01239         // deleting session info
01240         oxSession::deleteVar( 'usr' );  // for front end
01241         oxSession::deleteVar( 'auth' ); // for back end
01242         oxSession::deleteVar( 'dgr' );
01243         oxSession::deleteVar( 'dynvalue' );
01244         oxSession::deleteVar( 'paymentid' );
01245         // oxSession::deleteVar( 'deladrid' );
01246 
01247         // delete cookie
01248         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01249 
01250         // unsetting global user
01251         $this->setUser( null );
01252 
01253         return true;
01254     }
01255 
01262     public function loadAdminUser()
01263     {
01264         return $this->loadActiveUser( true );
01265     }
01266 
01275     public function loadActiveUser( $blForceAdmin = false )
01276     {
01277         $myConfig = $this->getConfig();
01278 
01279         $blAdmin = $myConfig->isAdmin() || $blForceAdmin;
01280         $oDB = oxDb::getDb();
01281 
01282         // first - checking session info
01283         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01284         $blFoundInCookie = false;
01285 
01286         //trying automatic login (by 'remember me' cookie)
01287         if ( !$sUserID && !$blAdmin ) {
01288             $sShopID = $myConfig->getShopId();
01289             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01290                 $aData = explode( '@@@', $sSet );
01291                 $sUser = $oDB->quote( $aData[0] );
01292                 $sPWD  = @$aData[1];
01293 
01294                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$sUser;
01295 
01296 
01297                 $oDB = oxDb::getDb();
01298                 $rs = $oDB->execute( $sSelect );
01299                 if ( $rs != false && $rs->recordCount() > 0 ) {
01300                     while (!$rs->EOF) {
01301                         $sTest = crypt( $rs->fields[1], 'ox' );
01302                         if ( $sTest == $sPWD ) {
01303                             // found
01304                             $sUserID = $rs->fields[0];
01305                             $blFoundInCookie = true;
01306                             break;
01307                         }
01308                         $rs->moveNext();
01309                     }
01310                 }
01311             }
01312         }
01313 
01314         // checking user results
01315         if ( $sUserID ) {
01316             if ( $this->load( $sUserID ) ) {
01317                 // storing into session
01318                 if ($blAdmin) {
01319                     oxSession::setVar( 'auth', $sUserID );
01320                 } else {
01321                     oxSession::setVar( 'usr', $sUserID );
01322                 }
01323 
01324                 // marking the way user was loaded
01325                 $this->_blLoadedFromCookie = $blFoundInCookie;
01326                 return true;
01327             }
01328         } else {
01329             // no user
01330             oxSession::deleteVar( 'usr' );
01331             oxSession::deleteVar( 'auth' );
01332 
01333             return false;
01334         }
01335     }
01336 
01349     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01350     {
01351         include "oxldap.php";
01352         $myConfig = $this->getConfig();
01353         $oDb = oxDb::getDb();
01354         //$throws oxConnectionException
01355         $aLDAPParams = $myConfig->getConfigParam( 'aLDAPParams' );
01356         $oLDAP = new oxLDAP( $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01357         // maybe this is LDAP user but supplied email Address instead of LDAP login
01358         $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote($sUser)." $sShopSelect");
01359         if ( isset( $sLDAPKey) && $sLDAPKey) {
01360             $sUser = $sLDAPKey;
01361         }
01362 
01363         //$throws oxConnectionException
01364         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01365 
01366         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01367         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {   // login successful
01368 
01369             // check if user is already in database
01370             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote($aData['OXUSERNAME'])." $sShopSelect";
01371             $sOXID = $oDb->getOne( $sSelect);
01372 
01373             if ( !isset( $sOXID) || !$sOXID) {   // we need to create a new user
01374                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01375                 $this->setId();
01376 
01377                 // map all user data fields
01378                 foreach ( $aData as $fldname => $value) {
01379                     $sField = "oxuser__".strtolower( $fldname);
01380                     $this->$sField->setValue($aData[$fldname]);
01381                 }
01382 
01383                 $this->oxuser__oxactive->setValue(1);
01384                 $this->oxuser__oxshopid->setValue($sShopID);
01385                 $this->oxuser__oxldapkey->setValue($sUser);
01386                 $this->oxuser__oxrights->setValue("user");
01387                 $this->setPassword( "ldap user" );
01388 
01389                 $this->save();
01390             } else {   // LDAP user is already in OXID DB, load it
01391                 $this->load( $sOXID);
01392             }
01393 
01394         } else {
01395             $oEx = oxNew( 'oxUserException' );
01396             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01397             throw $oEx;
01398         }
01399     }
01400 
01407     protected function _getUserRights()
01408     {
01409         // previously user had no rights defined
01410         if ( !$this->oxuser__oxrights->value )
01411             return 'user';
01412 
01413         $oDB = oxDb::getDb();
01414         $myConfig    = $this->getConfig();
01415         $sAuthRights = null;
01416 
01417         // choosing possible user rights index
01418         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01419         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01420         if ( $sAuthUserID ) {
01421             $sAuthUserID = $oDB->quote( $sAuthUserID );
01422             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$sAuthUserID );
01423         }
01424 
01425         //preventing user rights edit for non admin
01426         $aRights = array();
01427 
01428         // selecting current users rights ...
01429         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01430             $aRights[] = $sCurrRights;
01431         }
01432         $aRights[] = 'user';
01433 
01434         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01435             return current( $aRights );
01436         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01437             $aRights[] = $sAuthRights;
01438             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01439                 return current( $aRights );
01440             }
01441         }
01442 
01443         // leaving as it was set ...
01444         return $this->oxuser__oxrights->value;
01445     }
01446 
01456     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01457     {
01458 
01459         /*if ( !$myConfig->blMallUsers ) {
01460             $sShopID = $myConfig->getShopId();
01461             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01462         }*/
01463 
01464         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01465     }
01466 
01472     protected function _insert()
01473     {
01474 
01475         // set oxcreate date
01476         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01477 
01478         if ( !isset( $this->oxuser__oxboni->value ) ) {
01479             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01480         }
01481 
01482         if ( $blInsert = parent::_insert() ) {
01483             // setting customer number
01484             if ( !$this->oxuser__oxcustnr->value || !$this->isAdmin() ) {
01485                 $this->_setRecordNumber( 'oxcustnr' );
01486             }
01487         }
01488 
01489         return $blInsert;
01490     }
01491 
01497     protected function _update()
01498     {
01499         //V #M418: for not registered users, don't change boni during update
01500         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01501             $this->_aSkipSaveFields[] = 'oxboni';
01502         }
01503 
01504         // don't change this field
01505         $this->_aSkipSaveFields[] = 'oxcreate';
01506         if ( !$this->isAdmin() ) {
01507             $this->_aSkipSaveFields[] = 'oxcustnr';
01508             $this->_aSkipSaveFields[] = 'oxrights';
01509         }
01510 
01511         // updating subscription information
01512         if ( ( $blUpdate = parent::_update() ) ) {
01513             $this->getNewsSubscription()->updateSubscription( $this );
01514         }
01515 
01516         return $blUpdate;
01517     }
01518 
01534     protected function _checkLogin( &$sLogin, $aInvAddress )
01535     {
01536         $myConfig = $this->getConfig();
01537 
01538         $sLogin   = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername']:$sLogin;
01539 
01540         // check only for users with password during registration
01541         // if user wants to change user name - we must check if passwords are ok before changing
01542         if ( $this->oxuser__oxpassword->value && $sLogin != $this->oxuser__oxusername->value ) {
01543 
01544             // on this case password must be taken directly from request
01545             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
01546             if ( !$sNewPass ) {
01547 
01548                 // 1. user forgot to enter password
01549                 $oEx = oxNew( 'oxInputException' );
01550                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01551                 throw $oEx;
01552             } else {
01553 
01554                 // 2. entered wrong password
01555                 if ( !$this->isSamePassword( $sNewPass ) ) {
01556                     $oEx = oxNew( 'oxUserException' );
01557                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01558                     throw $oEx;
01559                 }
01560             }
01561         }
01562 
01563         if ( $this->checkIfEmailExists( $sLogin ) ) {
01564             //if exists then we do now allow to do that
01565             $oEx = oxNew( 'oxUserException' );
01566             $oLang = oxLang::getInstance();
01567             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
01568             throw $oEx;
01569         }
01570     }
01571 
01579     public function checkIfEmailExists( $sEmail )
01580     {
01581         $myConfig = $this->getConfig();
01582         $oDB = oxDb::getDb();
01583         $iShopId = $myConfig->getShopId();
01584         $blExists = false;
01585 
01586         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01587         if ( ( $sOxid = $this->getId() ) ) {
01588             $sQ .= " and oxid <> '$sOxid' ";
01589         }
01590 
01591         $oRs = $oDB->execute( $sQ );
01592         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01593 
01594             if ( $this->_blMallUsers ) {
01595 
01596                 $blExists = true;
01597                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01598 
01599                     // password is not set - allow to override
01600                     $blExists = false;
01601                 }
01602             } else {
01603 
01604                 $blExists = false;
01605                 while ( !$oRs->EOF ) {
01606                     if ( $oRs->fields[1] != 'user' ) {
01607 
01608                         // exists admin with same login - must not allow
01609                         $blExists = true;
01610                         break;
01611                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01612 
01613                         // exists same login (with password) in same shop
01614                         $blExists = true;
01615                         break;
01616                     }
01617 
01618                     $oRs->moveNext();
01619                 }
01620             }
01621         }
01622         return $blExists;
01623     }
01624 
01632     public function getUserRecommLists( $sOXID = null )
01633     {
01634         if ( !$sOXID )
01635             $sOXID = $this->getId();
01636 
01637         // sets active page
01638         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01639         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01640 
01641         // load only lists which we show on screen
01642         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01643         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01644 
01645 
01646         $oRecommList = oxNew( 'oxlist' );
01647         $oRecommList->init( 'oxrecommlist' );
01648         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01649         $iShopId = $this->getConfig()->getShopId();
01650         $sSelect = 'select * from oxrecommlists where oxuserid ="'. $sOXID .'" and oxshopid ="'. $iShopId .'"';
01651         $oRecommList->selectString( $sSelect );
01652 
01653         return $oRecommList;
01654     }
01655 
01663     public function getRecommListsCount( $sOx = null )
01664     {
01665         if ( !$sOx ) {
01666             $sOXID = $this->getId();
01667         }
01668 
01669         if ( $this->_iCntRecommLists === null || $sOx ) {
01670             $this->_iCntRecommLists = 0;
01671             $iShopId = $this->getConfig()->getShopId();
01672             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid ="'. $sOXID .'" and oxshopid ="'. $iShopId .'"';
01673             $this->_iCntRecommLists = oxDb::getDb()->getOne( $sSelect );
01674         }
01675         return $this->_iCntRecommLists;
01676     }
01677 
01686     protected function _checkEmail( $sEmail )
01687     {
01688         // missing email address (user login name) ?
01689         if ( !$sEmail ) {
01690             $oEx = oxNew( 'oxInputException' );
01691             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01692             throw $oEx;
01693         }
01694 
01695         // invalid email address ?
01696         if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
01697             $oEx = oxNew( 'oxInputException' );
01698             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
01699             throw $oEx;
01700         }
01701     }
01702 
01717     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01718     {
01719         $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01720     }
01721 
01734     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01735     {
01736         //  no password at all
01737         if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
01738             $oEx = oxNew( 'oxInputException' );
01739             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
01740             throw $oEx;
01741         }
01742 
01743         //  password is too short ?
01744         if ( $blCheckLenght &&  getStr()->strlen( $sNewPass ) < 6 ) {
01745             $oEx = oxNew( 'oxInputException' );
01746             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
01747             throw $oEx;
01748         }
01749 
01750         //  passwords do not match ?
01751         if ( $sNewPass != $sConfPass ) {
01752             $oEx = oxNew( 'oxUserException' );
01753             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01754             throw $oEx;
01755         }
01756     }
01757 
01769     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01770     {
01771         // collecting info about required fields
01772         $aMustFields = array( 'oxuser__oxfname',
01773                               'oxuser__oxlname',
01774                               'oxuser__oxstreetnr',
01775                               'oxuser__oxstreet',
01776                               'oxuser__oxzip',
01777                               'oxuser__oxcity' );
01778 
01779         // config shoud override default fields
01780         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
01781         if ( is_array( $aMustFillFields ) ) {
01782             $aMustFields = $aMustFillFields;
01783         }
01784 
01785         // assuring data to check
01786         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
01787         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
01788 
01789         // collecting fields
01790         $aFields = array_merge( $aInvAddress, $aDelAddress );
01791 
01792         // check delivery address ?
01793         $blCheckDel = false;
01794         if ( count( $aDelAddress ) ) {
01795             $blCheckDel = true;
01796         }
01797 
01798         // checking
01799         foreach ( $aMustFields as $sMustField ) {
01800 
01801             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
01802             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
01803                 continue;
01804             }
01805 
01806             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
01807                 $this->_checkRequiredArrayFields( $sMustField, $aFields[$sMustField] );
01808             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
01809                    $oEx = oxNew( 'oxInputException' );
01810                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01811                    throw $oEx;
01812             }
01813         }
01814     }
01815 
01826     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01827     {
01828         foreach ( $aFieldValues as $sValue ) {
01829             if ( !trim( $sValue ) ) {
01830                 $oEx = oxNew( 'oxInputException' );
01831                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01832                 throw $oEx;
01833             }
01834         }
01835     }
01836 
01847     protected function _checkVatId( $aInvAddress )
01848     {
01849         // vat ID must be checked only for business customers
01850         if ( $aInvAddress['oxuser__oxustid'] && $aInvAddress['oxuser__oxcompany'] ) {
01851 
01852             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
01853                 // no country
01854                 return;
01855             }
01856             $oCountry = oxNew('oxcountry');
01857             if (!$oCountry->load($sCountryId)) {
01858                 throw new oxObjectException();
01859             }
01860             if ($oCountry->isForeignCountry() && $oCountry->isInEU()) {
01861                     if (strncmp($aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2)) {
01862                         $oEx = oxNew( 'oxInputException' );
01863                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
01864                         throw $oEx;
01865                     }
01866             }
01867 
01868         }
01869     }
01870 
01879     protected function _setAutoGroups( $sCountryId )
01880     {
01881         // assigning automatically to specific groups
01882         $blForeigner = true;
01883         $blForeignGroupExists = false;
01884         $blInlandGroupExists = false;
01885 
01886         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01887         // foreigner ?
01888         if ( is_array($aHomeCountry)) {
01889             if (in_array($sCountryId, $aHomeCountry)) {
01890                 $blForeigner = false;
01891             }
01892         } elseif ($sCountryId == $aHomeCountry) {
01893             $blForeigner = false;
01894         }
01895 
01896         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01897             $blForeignGroupExists = true;
01898             if ( !$blForeigner ) {
01899                 $this->removeFromGroup( 'oxidforeigncustomer' );
01900             }
01901         }
01902 
01903         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01904             $blInlandGroupExists = true;
01905             if ( $blForeigner ) {
01906                 $this->removeFromGroup( 'oxidnewcustomer' );
01907             }
01908         }
01909 
01910         if ( !$this->oxuser__oxdisableautogrp->value ) {
01911             if ( !$blForeignGroupExists && $blForeigner ) {
01912                 $this->addToGroup( 'oxidforeigncustomer' );
01913             }
01914             if ( !$blInlandGroupExists && !$blForeigner ) {
01915                 $this->addToGroup( 'oxidnewcustomer' );
01916             }
01917         }
01918     }
01919 
01927     protected function _hasUserAddress( $sUserId )
01928     {
01929 
01930         $oAddresses = $this->getUserAddresses();
01931         if ( $oAddresses && count($oAddresses)>0 ) {
01932             $oAddresses->rewind() ;
01933             foreach ($oAddresses as $key => $oAddress) {
01934                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01935                     return true;
01936                 }
01937             }
01938         }
01939         return false;
01940     }
01941 
01954     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
01955     {
01956         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
01957     }
01958 
01968     protected function _deleteUserCookie( $sShopId = null )
01969     {
01970         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
01971     }
01972 
01982     protected static function _getUserCookie( $sShopId = null )
01983     {
01984         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
01985     }
01986 
01987 
01996     public function loadUserByUpdateId( $sUid )
01997     {
01998         $oDb = oxDb::getDb();
01999         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02000         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02001             return $this->load( $sUserId );
02002         }
02003     }
02004 
02012     public function setUpdateKey( $blReset = false )
02013     {
02014         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02015         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02016 
02017         // generating key
02018         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02019 
02020         // setting expiration time for 6 hours
02021         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02022 
02023         // saving
02024         $this->save();
02025     }
02026 
02032     public function getUpdateLinkTerm()
02033     {
02034         return 3600 * 6;
02035     }
02036 
02044     public function isExpiredUpdateId( $sKey )
02045     {
02046         $oDb = oxDb::getDb();
02047         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02048         return !( (bool) $oDb->getOne( $sQ ) );
02049     }
02050 
02056     public function getUpdateId()
02057     {
02058         if ( $this->_sUpdateKey === null ) {
02059             $this->setUpdateKey();
02060             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02061         }
02062         return $this->_sUpdateKey;
02063     }
02064 
02073     public function encodePassword( $sPassword, $sSalt )
02074     {
02075         $oDb = oxDb::getDb();
02076         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02077     }
02078 
02086     public function prepareSalt( $sSalt )
02087     {
02088         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02089     }
02090 
02098     public function decodeSalt( $sSaltHex )
02099     {
02100         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02101     }
02102 
02110     public function setPassword( $sPassword = null )
02111     {
02112         // setting salt if password is not empty
02113         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02114 
02115         // encoding only if password was not empty (e.g. user registration without pass)
02116         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02117 
02118         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02119         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02120     }
02121 
02129     public function isSamePassword( $sNewPass )
02130     {
02131         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02132     }
02133 
02139     public function isLoadedFromCookie()
02140     {
02141         return $this->_blLoadedFromCookie;
02142     }
02143 
02150     public function getPasswordHash()
02151     {
02152         $sHash = null;
02153         if ( $this->oxuser__oxpassword->value ) {
02154             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02155                 // decodable pass ?
02156                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02157             } elseif ( ( strlen( $this->oxuser__oxpassword->value ) < 32 ) && ( strpos( $this->oxuser__oxpassword->value, 'openid_' ) !== 0 ) ) {
02158                 // plain pass ?
02159                 $this->setPassword( $this->oxuser__oxpassword->value );
02160             }
02161             $sHash = $this->oxuser__oxpassword->value;
02162         }
02163         return $sHash;
02164     }
02165 
02174     public static function getAdminUser()
02175     {
02176         return self::getActiveUser( true );
02177     }
02178 
02189     public static function getActiveUser( $blForceAdmin = false )
02190     {
02191         $oUser = oxNew( 'oxuser' );
02192         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02193             return $oUser;
02194         } else {
02195             return false;
02196         }
02197     }
02198 
02206     public function getOpenIdPassword( $iLength = 25 )
02207     {
02208         $sPassword= "openid_".substr( oxUtilsObject::getInstance()->generateUId(), 0, $iLength);
02209         return $sPassword;
02210     }
02211 
02219     public function getReviewUserHash( $sUserId )
02220     {
02221         $sReviewUserHash = oxDb::getDb()->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = "'.$sUserId.'"');
02222         return $sReviewUserHash;
02223     }
02224 
02232     public function getReviewUserId( $sReviewUserHash )
02233     {
02234         $sUserId = oxDb::getDb()->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = "'.$sReviewUserHash.'"');
02235         return $sUserId;
02236     }
02237 
02238 }

Generated on Tue Aug 4 09:09:57 2009 for OXID eShop CE by  doxygen 1.5.5