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         $oDb = oxDb::getDb();
00225         if ( !$sCountryId ) {
00226             $sCountryId = $this->oxuser__oxcountryid->value;
00227         }
00228 
00229         $sQ = "select oxtitle".oxLang::getInstance()->getLanguageTag( $iLang )." from oxcountry where oxid = " . $oDb->quote( $sCountryId ) . " ";
00230         $this->oxuser__oxcountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00231 
00232         return $this->oxuser__oxcountry;
00233     }
00234 
00242     public function getUserCountryId( $sCountry = null )
00243     {
00244         $oDb = oxDb::getDb();
00245         $sQ = "select oxid from oxcountry where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00246         $sCountryId = $oDb->getOne( $sQ );
00247 
00248         return $sCountryId;
00249     }
00250 
00258     public function getUserGroups( $sOXID = null )
00259     {
00260 
00261         if ( isset( $this->_oGroups ) ) {
00262             return $this->_oGroups;
00263         }
00264 
00265         if ( !$sOXID ) {
00266             $sOXID = $this->getId();
00267         }
00268 
00269         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00270         //$this->oGroups->Init( 'oxbase' );
00271         //$this->oGroups->oLstoTpl->Init( array( 'oxgroups', 'oxobject2group' ) );
00272         $sSelect  = 'select oxgroups.* from oxgroups left join oxobject2group on oxobject2group.oxgroupsid = oxgroups.oxid ';
00273         $sSelect .= 'where oxobject2group.oxobjectid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00274         $this->_oGroups->selectString( $sSelect );
00275         return $this->_oGroups;
00276     }
00277 
00285     public function getUserAddresses( $sUserId = null )
00286     {
00287 
00288         if ( $this->_oAddresses == null ) {
00289 
00290             $sUserId = ( $sUserId ) ? $sUserId : $this->getId();
00291             $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId ) . "";
00292 
00293             //P
00294             $this->_oAddresses = oxNew( 'oxlist' );
00295             $this->_oAddresses->init( "oxaddress" );
00296             $this->_oAddresses->selectString( $sSelect );
00297 
00298             // marking selected
00299             if ( $sAddressId = $this->getSelectedAddressId() ) {
00300                 foreach ( $this->_oAddresses as $oAddress ) {
00301                     $oAddress->selected = 0;
00302                     if ( $oAddress->getId() == $sAddressId ) {
00303                         $oAddress->selected = 1;
00304                         break;
00305                     }
00306                 }
00307             }
00308         }
00309         return $this->_oAddresses;
00310     }
00311 
00317     public function getSelectedAddressId()
00318     {
00319         if ( !( $sAddressId = oxConfig::getParameter( "oxaddressid") ) ) {
00320             $sAddressId = oxSession::getVar( "deladrid" );
00321         }
00322         return $sAddressId;
00323     }
00324 
00333     public function getSelectedAddress( $sWishId = false )
00334     {
00335         $oAddresses = $this->getUserAddresses();
00336         if ( $oAddresses->count() ) {
00337             if ( $sAddressId = $this->getSelectedAddressId() ) {
00338                 foreach ( $oAddresses as $oAddress ) {
00339                     if ( $oAddress->selected == 1 ) {
00340                         $sAddressId = $oAddress->getId();
00341                         break;
00342                     }
00343                 }
00344             } elseif ( $sWishId ) {
00345                 foreach ( $oAddresses as $oAddress ) {
00346                     $oAddress->selected = 0;
00347                     if ( $oAddress->oxaddress__oxaddressuserid->value == $sWishId ) {
00348                         $oAddress->selected = 1;
00349                         $sAddressId = $oAddress->getId();
00350                     }
00351                 }
00352             }
00353 
00354             // in case none is set - setting first one
00355             if ( !$sAddressId ) {
00356                 $oAddresses->rewind();
00357                 $oAddress = $oAddresses->current();
00358                 $oAddress->selected = 1;
00359                 $sAddressId = $oAddress->getId();
00360             }
00361         }
00362 
00363         return $sAddressId;
00364     }
00365 
00373     public function getUserPayments( $sOXID = null )
00374     {
00375         if ( $this->_oPayments === null ) {
00376 
00377             if ( !$sOXID ) {
00378                 $sOXID = $this->getId();
00379             }
00380 
00381             $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00382 
00383             $this->_oPayments = oxNew( 'oxlist' );
00384             $this->_oPayments->init( 'oxUserPayment' );
00385             $this->_oPayments->selectString( $sSelect );
00386 
00387             $myUtils = oxUtils::getInstance();
00388             foreach ( $this->_oPayments as $oPayment ) {
00389                 // add custom fields to this class
00390                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00391             }
00392         }
00393 
00394         return $this->_oPayments;
00395     }
00396 
00402     public function save()
00403     {
00404 
00405         $blAddRemark = false;
00406         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00407             $blAddRemark = true;
00408             //save oxregister value
00409             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00410         }
00411 
00412         // setting user rights
00413         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00414 
00415         // processing birth date which came from output as array
00416         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00417             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00418         }
00419 
00420         $blRet = parent::save();
00421 
00422         //add registered remark
00423         if ( $blAddRemark && $blRet ) {
00424             $oRemark = oxNew( 'oxremark' );
00425             $oRemark->oxremark__oxtext     = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00426             $oRemark->oxremark__oxtype     = new oxField('r', oxField::T_RAW);
00427             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00428             $oRemark->save();
00429         }
00430 
00431         return $blRet;
00432     }
00433 
00439     public function allowDerivedUpdate()
00440     {
00441         return true;
00442     }
00443 
00451     public function inGroup( $sGroupID )
00452     {
00453         $blIn = false;
00454         if ( ( $oGroups = $this->getUserGroups() ) ) {
00455             $blIn = isset( $oGroups[ $sGroupID ] );
00456         }
00457 
00458         return $blIn;
00459     }
00460 
00469     public function delete( $sOXID = null )
00470     {
00471 
00472         if ( !$sOXID ) {
00473             $sOXID = $this->getId();
00474         }
00475         if ( !$sOXID ) {
00476             return false;
00477         }
00478 
00479         $blDeleted = parent::delete( $sOXID );
00480 
00481         if ( $blDeleted ) {
00482             $oDB = oxDb::getDb();
00483             $sOXIDQuoted = $oDB->quote($sOXID);
00484 
00485             // deleting stored payment, address, group dependencies, remarks info
00486             $rs = $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00487             $rs = $oDB->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00488 
00489             // deleting notice/wish lists
00490             $rs = $oDB->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00491             $rs = $oDB->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00492 
00493             // deleting newsletter subscription
00494             $rs = $oDB->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00495 
00496             // delivery and delivery sets
00497             $rs = $oDB->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00498 
00499             // discounts
00500             $rs = $oDB->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00501 
00502 
00503             // and leaving all order related information
00504             $rs = $oDB->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00505 
00506             $blDeleted = $rs->EOF;
00507         }
00508 
00509         return $blDeleted;
00510     }
00511 
00519     public function load( $oxID )
00520     {
00521 
00522         $blRet = parent::Load( $oxID );
00523 
00524         // convert date's to international format
00525         if ( isset( $this->oxuser__oxcreate->value ) ) {
00526             $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00527         }
00528 
00529         return $blRet;
00530     }
00531 
00539     public function exists( $sOXID = null )
00540     {
00541         $oDb = oxDb::getDb();
00542         if ( !$sOXID ) {
00543             $sOXID = $this->getId();
00544         }
00545 
00546         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00547                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).'';
00548 
00549         if ( $sOXID ) {
00550             $sSelect.= " or oxid = ".$oDb->quote( $sOXID ) . " ) ";
00551         } else {
00552             $sSelect.= ' ) ';
00553         }
00554 
00555         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00556             $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00557         }
00558 
00559         $blExists = false;
00560         if ( ( $sOxid = oxDb::getDb()->getOne( $sSelect ) ) ) {
00561              // update - set oxid
00562             $this->setId( $sOxid );
00563             $blExists = true;
00564         }
00565         return $blExists;
00566     }
00567 
00573     public function getOrders()
00574     {
00575         $myConfig = $this->getConfig();
00576         $oOrders = oxNew( 'oxlist' );
00577         $oOrders->init( 'oxorder' );
00578 
00579         //P
00580         // Lists does not support loading from two tables, so orders
00581         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00582         // forcing to load product info which is used in templates
00583         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00584 
00585         //loading order for registered user
00586         if ( $this->oxuser__oxregister->value > 1 ) {
00587             $sQ = 'select * from oxorder where oxuserid = "'.$this->getId().'" and oxorderdate >= ' . oxDb::getDb()->quote( $this->oxuser__oxregister->value ) . ' ';
00588 
00589             //#1546 - shopid check added, if it is not multishop
00590 
00591             $sQ .= ' order by oxorderdate desc ';
00592             $oOrders->selectString( $sQ );
00593         }
00594 
00595         return $oOrders;
00596     }
00597 
00603     public function getOrderCount()
00604     {
00605         $iCnt = 0;
00606         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00607             $oDb = oxDb::getDb();
00608             $sQ  = 'select count(*) from oxorder where oxuserid = "'.$this->getId().'" AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00609             $iCnt = (int) $oDb->getOne( $sQ );
00610         }
00611 
00612         return $iCnt;
00613     }
00614 
00620     public function getNoticeListArtCnt()
00621     {
00622         if ( $this->_iCntNoticeListArticles === null ) {
00623             $this->_iCntNoticeListArticles = 0;
00624             if ( $this->getId() ) {
00625                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00626             }
00627         }
00628         return $this->_iCntNoticeListArticles;
00629     }
00630 
00636     public function getWishListArtCnt()
00637     {
00638         if ( $this->_iCntWishListArticles === null ) {
00639             $this->_iCntWishListArticles = false;
00640             if ( $this->getId() ) {
00641                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00642             }
00643         }
00644         return $this->_iCntWishListArticles;
00645     }
00646 
00653     public function getActiveCountry()
00654     {
00655         $sDeliveryCountry = '';
00656         if ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) {
00657             $oDelAddress = oxNew( 'oxaddress' );
00658             $oDelAddress->load( $soxAddressId );
00659             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00660         } elseif ( $this->getId() ) {
00661             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00662         } else {
00663             $oUser = oxNew( 'oxuser' );
00664             if ( $oUser->loadActiveUser() ) {
00665                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00666             }
00667         }
00668 
00669         return $sDeliveryCountry;
00670     }
00671 
00679     public function createUser()
00680     {
00681         $oDB = oxDb::getDb();
00682         $sShopID = $this->getConfig()->getShopId();
00683 
00684         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00685         $sSelect = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00686         if ( !$this->_blMallUsers ) {
00687             $sSelect .= " and oxshopid = '{$sShopID}' ";
00688         }
00689         $sOXID = $oDB->getOne( $sSelect );
00690 
00691         // user without password found - lets use
00692         if ( isset( $sOXID ) && $sOXID ) {
00693             // try to update
00694             $this->delete( $sOXID );
00695         } elseif ( $this->_blMallUsers ) { // must be sure if there is no dublicate user
00696             $sQ = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00697             if ( $oDB->getOne( $sQ ) ) {
00698                 $oEx = oxNew( 'oxUserException' );
00699                 $oLang = oxLang::getInstance();
00700                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00701                 throw $oEx;
00702             }
00703         }
00704 
00705         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00706         if ( ( $blOK = $this->save() ) ) {
00707             // dropping/cleaning old delivery address/payment info
00708             $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00709             $oDB->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00710         } else {
00711             $oEx = oxNew( 'oxUserException' );
00712             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00713             throw $oEx;
00714         }
00715 
00716         return $blOK;
00717     }
00718 
00726     public function addToGroup( $sGroupID )
00727     {
00728         if ( !$this->inGroup( $sGroupID ) ) {
00729             $oNewGroup = oxNew( 'oxobject2group' );
00730             $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00731             $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00732             if ( $oNewGroup->save() ) {
00733                 $this->_oGroups[$sGroupID] = $oNewGroup;
00734                 return true;
00735             }
00736         }
00737         return false;
00738     }
00739 
00747     public function removeFromGroup( $sGroupID = null )
00748     {
00749         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00750             $oGroups = oxNew( 'oxlist' );
00751             $oGroups->init( 'oxobject2group' );
00752             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00753             $oGroups->selectString( $sSelect );
00754             foreach ( $oGroups as $oRemgroup ) {
00755                 if ( $oRemgroup->delete() ) {
00756                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00757                 }
00758             }
00759         }
00760     }
00761 
00770     public function onOrderExecute( $oBasket, $iSuccess )
00771     {
00772 
00773         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00774             //adding user to particular customer groups
00775             if ( !$this->oxuser__oxdisableautogrp->value ) {
00776 
00777                 $myConfig = $this->getConfig();
00778                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00779                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00780 
00781                 $this->addToGroup( 'oxidcustomer' );
00782                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00783                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00784                     $this->addToGroup( 'oxidsmallcust' );
00785                 }
00786                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00787                     $this->addToGroup( 'oxidmiddlecust' );
00788                 }
00789                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00790                     $this->addToGroup( 'oxidgoodcust' );
00791                 }
00792             }
00793 
00794             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00795                 $this->removeFromGroup( 'oxidnotyetordered' );
00796             }
00797         }
00798     }
00799 
00807     public function getBasket( $sName )
00808     {
00809         if ( !isset( $this->_aBaskets[$sName] ) ) {
00810             $oBasket = oxNew( 'oxuserbasket' );
00811             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00812 
00813             // creating if it does not exist
00814             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00815                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00816                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00817 
00818                 // marking basket as new (it will not be saved in DB yet)
00819                 $oBasket->setIsNewBasket();
00820             }
00821 
00822             $this->_aBaskets[$sName] = $oBasket;
00823         }
00824 
00825         return $this->_aBaskets[$sName];
00826     }
00827 
00836     public function convertBirthday( $aData )
00837     {
00838 
00839         // preparing data to process
00840         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00841         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00842         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00843 
00844         // leaving empty if not set
00845         if ( !$iYear && !$iMonth && !$iDay )
00846             return "";
00847 
00848         // year
00849         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00850             $iYear = date('Y');
00851 
00852         // month
00853         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00854             $iMonth = 1;
00855 
00856         // maximum nuber of days in month
00857         $iMaxDays = 31;
00858         switch( $iMonth) {
00859             case 2 :
00860                 if ($iMaxDays > 28)
00861                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00862                 break;
00863             case 4  :
00864             case 6  :
00865             case 9  :
00866             case 11 :
00867                 $iMaxDays = min(30, $iMaxDays);
00868                 break;
00869         }
00870 
00871         // day
00872         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00873             $iDay = 1;
00874         }
00875 
00876         // whole date
00877         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00878     }
00879 
00886     public function getBoni()
00887     {
00888         return 1000;
00889     }
00890 
00903     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00904     {
00905         // preparing input
00906         $sDynGoup = strtolower( trim( $sDynGoup ) );
00907 
00908         // setting denied groups from admin settings also
00909         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
00910 
00911         // default state ..
00912         $blAdd = false;
00913 
00914         // user assignment to dyn group is not allowed
00915         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
00916             $blAdd = false;
00917         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
00918             // trying to add user to prohibited user group?
00919             $blAdd = false;
00920         } elseif ( $this->addToGroup( $sDynGoup ) ) {
00921             $blAdd = true;
00922         }
00923 
00924         // cleanup
00925         oxSession::deleteVar( 'dgr' );
00926 
00927         return $blAdd;
00928     }
00929 
00945     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
00946     {
00947         // 1. checking user name
00948         $this->_checkLogin( $sLogin, $aInvAddress );
00949 
00950         // 2. cheking email
00951         $this->_checkEmail( $sLogin );
00952 
00953         // 3. password
00954         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
00955 
00956         // 4. required fields
00957         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
00958 
00959         // 5. country check
00960         $this->_checkCountries( $aInvAddress, $aDelAddress );
00961 
00962         // 6. vat id check.
00963             $this->_checkVatId( $aInvAddress );
00964     }
00965 
00974     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
00975     {
00976         // assigning to newsletter
00977         $blSuccess = false;
00978         $myConfig  = $this->getConfig();
00979 
00980         // user wants to get newsletter messages or no ?
00981         $oNewsSubscription = $this->getNewsSubscription();
00982         if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
00983             if ( !$blSendOptIn ) {
00984 
00985                 // double-opt-in check is disabled - assigning automatically
00986                 $this->addToGroup( 'oxidnewsletter' );
00987                 // and setting subscribed status
00988                 $oNewsSubscription->setOptInStatus( 1 );
00989                 $blSuccess = true;
00990             } else {
00991 
00992                 // double-opt-in check enabled - sending confirmation email and setting waiting status
00993                 $oNewsSubscription->setOptInStatus( 2 );
00994 
00995                 // sending double-opt-in mail
00996                 $oEmail = oxNew( 'oxemail' );
00997                 $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
00998             }
00999         } elseif ( !$blSubscribe ) { // removing user from newsletter subscribers
01000             $this->removeFromGroup( 'oxidnewsletter' );
01001             $oNewsSubscription->setOptInStatus( 0 );
01002             $blSuccess = true;
01003         }
01004 
01005         return $blSuccess;
01006     }
01007 
01025     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01026     {
01027 
01028         // validating values before saving. If validation fails - exception is thrown
01029         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01030 
01031         // input data is fine - lets save updated user info
01032         $this->assign( $aInvAddress );
01033 
01034 
01035         // update old or add new delivery address
01036         $this->_assignAddress( $aDelAddress );
01037 
01038         // saving new values
01039         if ( $this->save() ) {
01040 
01041             // assigning automatically to specific groups
01042             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01043             $this->_setAutoGroups( $sCountryId );
01044         }
01045     }
01046 
01055     public function addUserAddress( $oUser )
01056     {
01057 
01058         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01059             return false;
01060         }
01061 
01062         $oAddress = oxNew( 'oxaddress' );
01063 
01064         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01065         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01066         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01067         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01068         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01069         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01070         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01071         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01072         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01073         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01074         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01075 
01076         // adding new address
01077         if ( $oAddress->save() ) {
01078             // resetting addresses
01079             $this->_oAddresses = null;
01080             return $oAddress->getId();
01081         }
01082     }
01083 
01091     protected function _assignAddress( $aDelAddress )
01092     {
01093         if (isset($aDelAddress) && count($aDelAddress)) {
01094             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01095             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01096             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01097             $oAddress = oxNew( 'oxaddress' );
01098             $oAddress->assign( $aDelAddress );
01099             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01100             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01101             $oAddress->save();
01102 
01103             // resetting addresses
01104             $this->_oAddresses = null;
01105 
01106             // saving delivery Address for later use
01107             oxSession::setVar( 'deladrid', $oAddress->getId() );
01108         } else {
01109             // resetting
01110             oxSession::setVar( 'deladrid', null );
01111         }
01112 
01113     }
01114 
01127     public function login( $sUser, $sPassword, $blCookie = false)
01128     {
01129         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01130             $oEx = oxNew( 'oxCookieException' );
01131             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01132             throw $oEx;
01133         }
01134 
01135         $myConfig = $this->getConfig();
01136         if ( $sPassword ) {
01137 
01138             $sShopID = $myConfig->getShopId();
01139             $oDb = oxDb::getDb();
01140 
01141             $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01142             $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01143             $sShopSelect = "";
01144 
01145 
01146             // admin view: can only login with higher than 'user' rights
01147             if ( $this->isAdmin() ) {
01148                 $sShopSelect = " and ( oxrights != 'user' ) ";
01149             }
01150 
01151             $sWhat = "oxid";
01152 
01153             $sSelect =  "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01154             if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
01155                 if ( $sPassword == "admin" && $sUser == "admin" ) {
01156                     $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01157                 } else {
01158                     $oEx = oxNew( 'oxUserException' );
01159                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01160                     throw $oEx;
01161                 }
01162             }
01163 
01164             // load from DB
01165             $aData = $oDb->getAll( $sSelect );
01166             $sOXID = @$aData[0][0];
01167             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01168 
01169                 if ( !$this->load( $sOXID ) ) {
01170                     $oEx = oxNew( 'oxUserException' );
01171                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01172                     throw $oEx;
01173                 }
01174             }
01175         }
01176 
01177 
01178         //login successfull?
01179         if ( $this->oxuser__oxid->value ) {   // yes, successful login
01180             if ( $this->isAdmin() ) {
01181                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01182             } else {
01183                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01184             }
01185 
01186             // cookie must be set ?
01187             if ( $blCookie ) {
01188                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01189             }
01190             return true;
01191         } else {
01192             $oEx = oxNew( 'oxUserException' );
01193             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01194             throw $oEx;
01195         }
01196     }
01197 
01208     public function openIdLogin( $sUser )
01209     {
01210         $myConfig = $this->getConfig();
01211         $sShopID = $myConfig->getShopId();
01212         $oDb = oxDb::getDb();
01213 
01214         $sUserSelect = "oxuser.oxusername = " . $oDb->quote( $sUser );
01215         $sShopSelect = "";
01216 
01217 
01218         $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01219 
01220         // load from DB
01221         $aData = $oDb->getAll( $sSelect );
01222         $sOXID = @$aData[0][0];
01223         if ( isset( $sOXID ) && $sOXID ) {
01224 
01225             if ( !$this->load( $sOXID ) ) {
01226                 $oEx = oxNew( 'oxUserException' );
01227                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01228                 throw $oEx;
01229             }
01230         }
01231 
01232         //login successfull?
01233         if ( $this->oxuser__oxid->value ) {   // yes, successful login
01234             oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01235             return true;
01236         } else {
01237             $oEx = oxNew( 'oxUserException' );
01238             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01239             throw $oEx;
01240         }
01241     }
01242 
01248     public function logout()
01249     {
01250         // deleting session info
01251         oxSession::deleteVar( 'usr' );  // for front end
01252         oxSession::deleteVar( 'auth' ); // for back end
01253         oxSession::deleteVar( 'dgr' );
01254         oxSession::deleteVar( 'dynvalue' );
01255         oxSession::deleteVar( 'paymentid' );
01256         // oxSession::deleteVar( 'deladrid' );
01257 
01258         // delete cookie
01259         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01260 
01261         // unsetting global user
01262         $this->setUser( null );
01263 
01264         return true;
01265     }
01266 
01273     public function loadAdminUser()
01274     {
01275         return $this->loadActiveUser( true );
01276     }
01277 
01286     public function loadActiveUser( $blForceAdmin = false )
01287     {
01288         $myConfig = $this->getConfig();
01289 
01290         $blAdmin = $myConfig->isAdmin() || $blForceAdmin;
01291         $oDB = oxDb::getDb();
01292 
01293         // first - checking session info
01294         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01295         $blFoundInCookie = false;
01296 
01297         //trying automatic login (by 'remember me' cookie)
01298         if ( !$sUserID && !$blAdmin ) {
01299             $sShopID = $myConfig->getShopId();
01300             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01301                 $aData = explode( '@@@', $sSet );
01302                 $sUser = $aData[0];
01303                 $sPWD  = @$aData[1];
01304 
01305                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDB->quote($sUser);
01306 
01307 
01308                 $oDB = oxDb::getDb();
01309                 $rs = $oDB->execute( $sSelect );
01310                 if ( $rs != false && $rs->recordCount() > 0 ) {
01311                     while (!$rs->EOF) {
01312                         $sTest = crypt( $rs->fields[1], 'ox' );
01313                         if ( $sTest == $sPWD ) {
01314                             // found
01315                             $sUserID = $rs->fields[0];
01316                             $blFoundInCookie = true;
01317                             break;
01318                         }
01319                         $rs->moveNext();
01320                     }
01321                 }
01322             }
01323         }
01324 
01325         // checking user results
01326         if ( $sUserID ) {
01327             if ( $this->load( $sUserID ) ) {
01328                 // storing into session
01329                 if ($blAdmin) {
01330                     oxSession::setVar( 'auth', $sUserID );
01331                 } else {
01332                     oxSession::setVar( 'usr', $sUserID );
01333                 }
01334 
01335                 // marking the way user was loaded
01336                 $this->_blLoadedFromCookie = $blFoundInCookie;
01337                 return true;
01338             }
01339         } else {
01340             // no user
01341             oxSession::deleteVar( 'usr' );
01342             oxSession::deleteVar( 'auth' );
01343 
01344             return false;
01345         }
01346     }
01347 
01360     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01361     {
01362         include "oxldap.php";
01363         $myConfig = $this->getConfig();
01364         $oDb = oxDb::getDb();
01365         //$throws oxConnectionException
01366         $aLDAPParams = $myConfig->getConfigParam( 'aLDAPParams' );
01367         $oLDAP = new oxLDAP( $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01368         // maybe this is LDAP user but supplied email Address instead of LDAP login
01369         $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote($sUser)." $sShopSelect");
01370         if ( isset( $sLDAPKey) && $sLDAPKey) {
01371             $sUser = $sLDAPKey;
01372         }
01373 
01374         //$throws oxConnectionException
01375         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01376 
01377         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01378         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {   // login successful
01379 
01380             // check if user is already in database
01381             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote($aData['OXUSERNAME'])." $sShopSelect";
01382             $sOXID = $oDb->getOne( $sSelect);
01383 
01384             if ( !isset( $sOXID) || !$sOXID) {   // we need to create a new user
01385                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01386                 $this->setId();
01387 
01388                 // map all user data fields
01389                 foreach ( $aData as $fldname => $value) {
01390                     $sField = "oxuser__".strtolower( $fldname);
01391                     $this->$sField->setValue($aData[$fldname]);
01392                 }
01393 
01394                 $this->oxuser__oxactive->setValue(1);
01395                 $this->oxuser__oxshopid->setValue($sShopID);
01396                 $this->oxuser__oxldapkey->setValue($sUser);
01397                 $this->oxuser__oxrights->setValue("user");
01398                 $this->setPassword( "ldap user" );
01399 
01400                 $this->save();
01401             } else {   // LDAP user is already in OXID DB, load it
01402                 $this->load( $sOXID);
01403             }
01404 
01405         } else {
01406             $oEx = oxNew( 'oxUserException' );
01407             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01408             throw $oEx;
01409         }
01410     }
01411 
01418     protected function _getUserRights()
01419     {
01420         // previously user had no rights defined
01421         if ( !$this->oxuser__oxrights->value )
01422             return 'user';
01423 
01424         $oDB = oxDb::getDb();
01425         $myConfig    = $this->getConfig();
01426         $sAuthRights = null;
01427 
01428         // choosing possible user rights index
01429         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01430         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01431         if ( $sAuthUserID ) {
01432             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01433         }
01434 
01435         //preventing user rights edit for non admin
01436         $aRights = array();
01437 
01438         // selecting current users rights ...
01439         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01440             $aRights[] = $sCurrRights;
01441         }
01442         $aRights[] = 'user';
01443 
01444         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01445             return current( $aRights );
01446         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01447             $aRights[] = $sAuthRights;
01448             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01449                 return current( $aRights );
01450             }
01451         }
01452 
01453         // leaving as it was set ...
01454         return $this->oxuser__oxrights->value;
01455     }
01456 
01466     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01467     {
01468 
01469         /*if ( !$myConfig->blMallUsers ) {
01470             $sShopID = $myConfig->getShopId();
01471             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01472         }*/
01473 
01474         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01475     }
01476 
01482     protected function _insert()
01483     {
01484 
01485         // set oxcreate date
01486         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01487 
01488         if ( !isset( $this->oxuser__oxboni->value ) ) {
01489             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01490         }
01491 
01492         if ( $blInsert = parent::_insert() ) {
01493             // setting customer number
01494             if ( !$this->oxuser__oxcustnr->value || !$this->isAdmin() ) {
01495                 $this->_setRecordNumber( 'oxcustnr' );
01496             }
01497         }
01498 
01499         return $blInsert;
01500     }
01501 
01507     protected function _update()
01508     {
01509         //V #M418: for not registered users, don't change boni during update
01510         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01511             $this->_aSkipSaveFields[] = 'oxboni';
01512         }
01513 
01514         // don't change this field
01515         $this->_aSkipSaveFields[] = 'oxcreate';
01516         if ( !$this->isAdmin() ) {
01517             $this->_aSkipSaveFields[] = 'oxcustnr';
01518             $this->_aSkipSaveFields[] = 'oxrights';
01519         }
01520 
01521         // updating subscription information
01522         if ( ( $blUpdate = parent::_update() ) ) {
01523             $this->getNewsSubscription()->updateSubscription( $this );
01524         }
01525 
01526         return $blUpdate;
01527     }
01528 
01544     protected function _checkLogin( &$sLogin, $aInvAddress )
01545     {
01546         $myConfig = $this->getConfig();
01547 
01548         $sLogin   = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername']:$sLogin;
01549 
01550         // check only for users with password during registration
01551         // if user wants to change user name - we must check if passwords are ok before changing
01552         if ( $this->oxuser__oxpassword->value && $sLogin != $this->oxuser__oxusername->value ) {
01553 
01554             // on this case password must be taken directly from request
01555             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
01556             if ( !$sNewPass ) {
01557 
01558                 // 1. user forgot to enter password
01559                 $oEx = oxNew( 'oxInputException' );
01560                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01561                 throw $oEx;
01562             } else {
01563 
01564                 // 2. entered wrong password
01565                 if ( !$this->isSamePassword( $sNewPass ) ) {
01566                     $oEx = oxNew( 'oxUserException' );
01567                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01568                     throw $oEx;
01569                 }
01570             }
01571         }
01572 
01573         if ( $this->checkIfEmailExists( $sLogin ) ) {
01574             //if exists then we do now allow to do that
01575             $oEx = oxNew( 'oxUserException' );
01576             $oLang = oxLang::getInstance();
01577             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
01578             throw $oEx;
01579         }
01580     }
01581 
01589     public function checkIfEmailExists( $sEmail )
01590     {
01591         $myConfig = $this->getConfig();
01592         $oDB = oxDb::getDb();
01593         $iShopId = $myConfig->getShopId();
01594         $blExists = false;
01595 
01596         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01597         if ( ( $sOxid = $this->getId() ) ) {
01598             $sQ .= " and oxid <> '$sOxid' ";
01599         }
01600 
01601         $oRs = $oDB->execute( $sQ );
01602         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01603 
01604             if ( $this->_blMallUsers ) {
01605 
01606                 $blExists = true;
01607                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01608 
01609                     // password is not set - allow to override
01610                     $blExists = false;
01611                 }
01612             } else {
01613 
01614                 $blExists = false;
01615                 while ( !$oRs->EOF ) {
01616                     if ( $oRs->fields[1] != 'user' ) {
01617 
01618                         // exists admin with same login - must not allow
01619                         $blExists = true;
01620                         break;
01621                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01622 
01623                         // exists same login (with password) in same shop
01624                         $blExists = true;
01625                         break;
01626                     }
01627 
01628                     $oRs->moveNext();
01629                 }
01630             }
01631         }
01632         return $blExists;
01633     }
01634 
01642     public function getUserRecommLists( $sOXID = null )
01643     {
01644         if ( !$sOXID )
01645             $sOXID = $this->getId();
01646 
01647         // sets active page
01648         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01649         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01650 
01651         // load only lists which we show on screen
01652         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01653         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01654 
01655 
01656         $oRecommList = oxNew( 'oxlist' );
01657         $oRecommList->init( 'oxrecommlist' );
01658         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01659         $iShopId = $this->getConfig()->getShopId();
01660         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01661         $oRecommList->selectString( $sSelect );
01662 
01663         return $oRecommList;
01664     }
01665 
01673     public function getRecommListsCount( $sOx = null )
01674     {
01675         if ( !$sOx ) {
01676             $sOx = $this->getId();
01677         }
01678 
01679         if ( $this->_iCntRecommLists === null || $sOx ) {
01680             $oDb = oxDb::getDb();
01681             $this->_iCntRecommLists = 0;
01682             $iShopId = $this->getConfig()->getShopId();
01683             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01684             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01685         }
01686         return $this->_iCntRecommLists;
01687     }
01688 
01697     protected function _checkEmail( $sEmail )
01698     {
01699         // missing email address (user login name) ?
01700         if ( !$sEmail ) {
01701             $oEx = oxNew( 'oxInputException' );
01702             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01703             throw $oEx;
01704         }
01705 
01706         // invalid email address ?
01707         if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
01708             $oEx = oxNew( 'oxInputException' );
01709             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
01710             throw $oEx;
01711         }
01712     }
01713 
01728     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01729     {
01730         $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01731     }
01732 
01745     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01746     {
01747         //  no password at all
01748         if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
01749             $oEx = oxNew( 'oxInputException' );
01750             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
01751             throw $oEx;
01752         }
01753 
01754         //  password is too short ?
01755         if ( $blCheckLenght &&  getStr()->strlen( $sNewPass ) < 6 ) {
01756             $oEx = oxNew( 'oxInputException' );
01757             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
01758             throw $oEx;
01759         }
01760 
01761         //  passwords do not match ?
01762         if ( $sNewPass != $sConfPass ) {
01763             $oEx = oxNew( 'oxUserException' );
01764             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01765             throw $oEx;
01766         }
01767     }
01768 
01777     protected function _checkCountries( $aInvAddress, $aDelAddress )
01778     {
01779         $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
01780         $sDelCtry  = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
01781 
01782         if ( $sBillCtry || $sDelCtry ) {
01783             $oDb = oxDb::getDb();
01784 
01785             if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
01786                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
01787                 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
01788             } else {
01789                 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
01790                               ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
01791             }
01792 
01793             if ( !$oDb->getOne( $sQ ) ) {
01794                 $oEx = oxNew( 'oxUserException' );
01795                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS' );
01796                 throw $oEx;
01797             }
01798         }
01799     }
01800 
01812     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01813     {
01814         // collecting info about required fields
01815         $aMustFields = array( 'oxuser__oxfname',
01816                               'oxuser__oxlname',
01817                               'oxuser__oxstreetnr',
01818                               'oxuser__oxstreet',
01819                               'oxuser__oxzip',
01820                               'oxuser__oxcity' );
01821 
01822         // config shoud override default fields
01823         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
01824         if ( is_array( $aMustFillFields ) ) {
01825             $aMustFields = $aMustFillFields;
01826         }
01827 
01828         // assuring data to check
01829         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
01830         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
01831 
01832         // collecting fields
01833         $aFields = array_merge( $aInvAddress, $aDelAddress );
01834 
01835 
01836         // check delivery address ?
01837         $blCheckDel = false;
01838         if ( count( $aDelAddress ) ) {
01839             $blCheckDel = true;
01840         }
01841 
01842         // checking
01843         foreach ( $aMustFields as $sMustField ) {
01844 
01845             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
01846             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
01847                 continue;
01848             }
01849 
01850             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
01851                 $this->_checkRequiredArrayFields( $sMustField, $aFields[$sMustField] );
01852             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
01853                    $oEx = oxNew( 'oxInputException' );
01854                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01855                    throw $oEx;
01856             }
01857         }
01858     }
01859 
01870     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01871     {
01872         foreach ( $aFieldValues as $sValue ) {
01873             if ( !trim( $sValue ) ) {
01874                 $oEx = oxNew( 'oxInputException' );
01875                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01876                 throw $oEx;
01877             }
01878         }
01879     }
01880 
01891     protected function _checkVatId( $aInvAddress )
01892     {
01893         // vat ID must be checked only for business customers
01894         if ( $aInvAddress['oxuser__oxustid'] && $aInvAddress['oxuser__oxcompany'] ) {
01895 
01896             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
01897                 // no country
01898                 return;
01899             }
01900             $oCountry = oxNew('oxcountry');
01901             if (!$oCountry->load($sCountryId)) {
01902                 throw new oxObjectException();
01903             }
01904             if ($oCountry->isForeignCountry() && $oCountry->isInEU()) {
01905                     if (strncmp($aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2)) {
01906                         $oEx = oxNew( 'oxInputException' );
01907                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
01908                         throw $oEx;
01909                     }
01910             }
01911 
01912         }
01913     }
01914 
01923     protected function _setAutoGroups( $sCountryId )
01924     {
01925         // assigning automatically to specific groups
01926         $blForeigner = true;
01927         $blForeignGroupExists = false;
01928         $blInlandGroupExists = false;
01929 
01930         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01931         // foreigner ?
01932         if ( is_array($aHomeCountry)) {
01933             if (in_array($sCountryId, $aHomeCountry)) {
01934                 $blForeigner = false;
01935             }
01936         } elseif ($sCountryId == $aHomeCountry) {
01937             $blForeigner = false;
01938         }
01939 
01940         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01941             $blForeignGroupExists = true;
01942             if ( !$blForeigner ) {
01943                 $this->removeFromGroup( 'oxidforeigncustomer' );
01944             }
01945         }
01946 
01947         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01948             $blInlandGroupExists = true;
01949             if ( $blForeigner ) {
01950                 $this->removeFromGroup( 'oxidnewcustomer' );
01951             }
01952         }
01953 
01954         if ( !$this->oxuser__oxdisableautogrp->value ) {
01955             if ( !$blForeignGroupExists && $blForeigner ) {
01956                 $this->addToGroup( 'oxidforeigncustomer' );
01957             }
01958             if ( !$blInlandGroupExists && !$blForeigner ) {
01959                 $this->addToGroup( 'oxidnewcustomer' );
01960             }
01961         }
01962     }
01963 
01971     protected function _hasUserAddress( $sUserId )
01972     {
01973         $oAddresses = $this->getUserAddresses();
01974         if ( $oAddresses && count($oAddresses)>0 ) {
01975             $oAddresses->rewind() ;
01976             foreach ($oAddresses as $key => $oAddress) {
01977                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01978                     return true;
01979                 }
01980             }
01981         }
01982         return false;
01983     }
01984 
01997     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
01998     {
01999         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
02000     }
02001 
02011     protected function _deleteUserCookie( $sShopId = null )
02012     {
02013         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
02014     }
02015 
02025     protected static function _getUserCookie( $sShopId = null )
02026     {
02027         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
02028     }
02029 
02030 
02039     public function loadUserByUpdateId( $sUid )
02040     {
02041         $oDb = oxDb::getDb();
02042         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02043         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02044             return $this->load( $sUserId );
02045         }
02046     }
02047 
02055     public function setUpdateKey( $blReset = false )
02056     {
02057         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02058         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02059 
02060         // generating key
02061         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02062 
02063         // setting expiration time for 6 hours
02064         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02065 
02066         // saving
02067         $this->save();
02068     }
02069 
02075     public function getUpdateLinkTerm()
02076     {
02077         return 3600 * 6;
02078     }
02079 
02087     public function isExpiredUpdateId( $sKey )
02088     {
02089         $oDb = oxDb::getDb();
02090         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02091         return !( (bool) $oDb->getOne( $sQ ) );
02092     }
02093 
02099     public function getUpdateId()
02100     {
02101         if ( $this->_sUpdateKey === null ) {
02102             $this->setUpdateKey();
02103             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02104         }
02105         return $this->_sUpdateKey;
02106     }
02107 
02116     public function encodePassword( $sPassword, $sSalt )
02117     {
02118         $oDb = oxDb::getDb();
02119         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02120     }
02121 
02129     public function prepareSalt( $sSalt )
02130     {
02131         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02132     }
02133 
02141     public function decodeSalt( $sSaltHex )
02142     {
02143         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02144     }
02145 
02153     public function setPassword( $sPassword = null )
02154     {
02155         // setting salt if password is not empty
02156         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02157 
02158         // encoding only if password was not empty (e.g. user registration without pass)
02159         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02160 
02161         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02162         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02163     }
02164 
02172     public function isSamePassword( $sNewPass )
02173     {
02174         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02175     }
02176 
02182     public function isLoadedFromCookie()
02183     {
02184         return $this->_blLoadedFromCookie;
02185     }
02186 
02193     public function getPasswordHash()
02194     {
02195         $sHash = null;
02196         if ( $this->oxuser__oxpassword->value ) {
02197             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02198                 // decodable pass ?
02199                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02200             } elseif ( ( strlen( $this->oxuser__oxpassword->value ) < 32 ) && ( strpos( $this->oxuser__oxpassword->value, 'openid_' ) !== 0 ) ) {
02201                 // plain pass ?
02202                 $this->setPassword( $this->oxuser__oxpassword->value );
02203             }
02204             $sHash = $this->oxuser__oxpassword->value;
02205         }
02206         return $sHash;
02207     }
02208 
02217     public static function getAdminUser()
02218     {
02219         return self::getActiveUser( true );
02220     }
02221 
02232     public static function getActiveUser( $blForceAdmin = false )
02233     {
02234         $oUser = oxNew( 'oxuser' );
02235         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02236             return $oUser;
02237         } else {
02238             return false;
02239         }
02240     }
02241 
02249     public function getOpenIdPassword( $iLength = 25 )
02250     {
02251         $sPassword= "openid_".substr( oxUtilsObject::getInstance()->generateUId(), 0, $iLength);
02252         return $sPassword;
02253     }
02254 
02262     public function getReviewUserHash( $sUserId )
02263     {
02264         $oDb = oxDb::getDb();
02265         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02266         return $sReviewUserHash;
02267     }
02268 
02276     public function getReviewUserId( $sReviewUserHash )
02277     {
02278         $oDb = oxDb::getDb();
02279         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02280         return $sUserId;
02281     }
02282 
02283 }

Generated on Tue Sep 29 16:45:13 2009 for OXID eShop CE by  doxygen 1.5.5