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 $_aAddresses = array();
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 protected $_sSelAddressId = null;
00124
00130 protected $_oSelAddress = null;
00131
00137 protected $_sWishId = null;
00138
00144 protected $_oUserCountryTitle = null;
00145
00151 public function __construct()
00152 {
00153 $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00154
00155 parent::__construct();
00156 $this->init( 'oxuser' );
00157 }
00158
00166 public function setMallUsersStatus( $blOn = false )
00167 {
00168 $this->_blMallUsers = $blOn;
00169 }
00170
00178 public function __get( $sParamName )
00179 {
00180
00181 switch ( $sParamName ) {
00182 case 'oGroups':
00183 return $this->_oGroups = $this->getUserGroups();
00184 break;
00185 case 'iCntNoticeListArticles':
00186 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00187 break;
00188 case 'iCntWishListArticles':
00189 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00190 break;
00191 case 'iCntRecommLists':
00192 return $this->_iCntRecommLists = $this->getRecommListsCount();
00193 break;
00194 case 'oAddresses':
00195 return $this->getUserAddresses();
00196 break;
00197 case 'oPayments':
00198 return $this->_oPayments = $this->getUserPayments();
00199 break;
00200 case 'oxuser__oxcountry':
00201 return $this->oxuser__oxcountry = $this->getUserCountry();
00202 break;
00203 case 'sDBOptin':
00204 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00205 break;
00206 case 'sEmailFailed':
00207 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00208 break;
00209 }
00210 }
00211
00217 public function getNewsSubscription()
00218 {
00219 if ( $this->_oNewsSubscription !== null ) {
00220 return $this->_oNewsSubscription;
00221 }
00222
00223 $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00224
00225
00226 if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00227 if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00228
00229
00230 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00231 $this->_oNewsSubscription->oxnewssubscribed__oxemail = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00232 $this->_oNewsSubscription->oxnewssubscribed__oxsal = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00233 $this->_oNewsSubscription->oxnewssubscribed__oxfname = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00234 $this->_oNewsSubscription->oxnewssubscribed__oxlname = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00235 }
00236 }
00237
00238 return $this->_oNewsSubscription;
00239 }
00240
00250 public function getUserCountry( $sCountryId = null, $iLang = null )
00251 {
00252 if ( $this->_oUserCountryTitle == null || $sCountryId ) {
00253 $sId = $sCountryId ? $sCountryId : $this->oxuser__oxcountryid->value;
00254 $oDb = oxDb::getDb();
00255 $sViewName = getViewName( 'oxcountry', $iLang );
00256 $sQ = "select oxtitle from {$sViewName} where oxid = " . $oDb->quote( $sId ) . " ";
00257 $oCountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00258 if ( !$sCountryId ) {
00259 $this->_oUserCountryTitle = $oCountry;
00260 }
00261 } else {
00262 return $this->_oUserCountryTitle;
00263 }
00264
00265 return $oCountry;
00266 }
00267
00275 public function getUserCountryId( $sCountry = null )
00276 {
00277 $oDb = oxDb::getDb();
00278 $sQ = "select oxid from ".getviewName( "oxcountry" )." where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00279 $sCountryId = $oDb->getOne( $sQ );
00280
00281 return $sCountryId;
00282 }
00283
00291 public function getUserGroups( $sOXID = null )
00292 {
00293
00294 if ( isset( $this->_oGroups ) ) {
00295 return $this->_oGroups;
00296 }
00297
00298 if ( !$sOXID ) {
00299 $sOXID = $this->getId();
00300 }
00301
00302 $sViewName = getViewName( "oxgroups" );
00303 $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00304 $sSelect = "select {$sViewName}.* from {$sViewName} left join oxobject2group on oxobject2group.oxgroupsid = {$sViewName}.oxid
00305 where oxobject2group.oxobjectid = " . oxDb::getDb()->quote( $sOXID );
00306 $this->_oGroups->selectString( $sSelect );
00307 return $this->_oGroups;
00308 }
00309
00317 public function getUserAddresses( $sUserId = null )
00318 {
00319 $sUserId = isset( $sUserId ) ? $sUserId : $this->getId();
00320 if ( !isset( $this->_aAddresses[$sUserId] ) ) {
00321 $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId );
00322
00323
00324 $this->_aAddresses[$sUserId] = oxNew( "oxlist" );
00325 $this->_aAddresses[$sUserId]->init( "oxaddress" );
00326 $this->_aAddresses[$sUserId]->selectString( $sSelect );
00327
00328
00329 if ( $sAddressId = $this->getSelectedAddressId() ) {
00330 foreach ( $this->_aAddresses[$sUserId] as $oAddress ) {
00331 if ( $oAddress->getId() === $sAddressId ) {
00332 $oAddress->setSelected();
00333 break;
00334 }
00335 }
00336 }
00337 }
00338 return $this->_aAddresses[$sUserId];
00339 }
00340
00348 public function setSelectedAddressId( $sAddressId )
00349 {
00350 $this->_sSelAddressId = $sAddressId;
00351 }
00352
00358 public function getSelectedAddressId()
00359 {
00360 if ( $this->_sSelAddressId !== null ) {
00361 return $this->_sSelAddressId;
00362 }
00363
00364 $sAddressId = oxConfig::getParameter( "oxaddressid");
00365 if ( !$sAddressId && !oxConfig::getParameter( 'reloadaddress' ) ) {
00366 $sAddressId = oxSession::getVar( "deladrid" );
00367 }
00368 return $sAddressId;
00369 }
00370
00376 protected function _getWishListId()
00377 {
00378 $this->_sWishId = null;
00379
00380 $oBasket = $this->getSession()->getBasket();
00381 foreach ( $oBasket->getContents() as $oBasketItem ) {
00382 if ( $this->_sWishId = $oBasketItem->getWishId() ) {
00383
00384 break;
00385 }
00386 }
00387 return $this->_sWishId;
00388 }
00389
00398 public function getSelectedAddress( $sWishId = false )
00399 {
00400 if ( $this->_oSelAddress !== null ) {
00401 return $this->_oSelAddress;
00402 }
00403
00404 $oSelectedAddress = null;
00405 $oAddresses = $this->getUserAddresses();
00406 if ( $oAddresses->count() ) {
00407 if ( $sAddressId = $this->getSelectedAddressId() ) {
00408 foreach ( $oAddresses as $oAddress ) {
00409 if ( $oAddress->getId() == $sAddressId ) {
00410 $oAddress->selected = 1;
00411 $oAddress->setSelected();
00412 $oSelectedAddress = $oAddress;
00413 break;
00414 }
00415 }
00416 }
00417
00418
00419 if ( !$oSelectedAddress ) {
00420 if ( !$sAddressId || $sAddressId >= 0 ) {
00421 $oAddresses->rewind();
00422 $oAddress = $oAddresses->current();
00423 } else {
00424 $aAddresses = $oAddresses->getArray();
00425 $oAddress = array_pop( $aAddresses );
00426 }
00427 $oAddress->selected = 1;
00428 $oAddress->setSelected();
00429 $oSelectedAddress = $oAddress;
00430 }
00431 }
00432 $this->_oSelAddress = $oSelectedAddress;
00433 return $oSelectedAddress;
00434 }
00435
00443 public function getUserPayments( $sOXID = null )
00444 {
00445 if ( $this->_oPayments === null ) {
00446
00447 if ( !$sOXID ) {
00448 $sOXID = $this->getId();
00449 }
00450
00451 $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00452
00453 $this->_oPayments = oxNew( 'oxlist' );
00454 $this->_oPayments->init( 'oxUserPayment' );
00455 $this->_oPayments->selectString( $sSelect );
00456
00457 $myUtils = oxUtils::getInstance();
00458 foreach ( $this->_oPayments as $oPayment ) {
00459
00460 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00461 }
00462 }
00463
00464 return $this->_oPayments;
00465 }
00466
00472 public function save()
00473 {
00474 $myConfig = oxConfig::getInstance();
00475
00476 $blAddRemark = false;
00477 if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00478 $blAddRemark = true;
00479
00480 $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00481 }
00482
00483
00484 $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00485
00486
00487 if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00488 $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00489 }
00490
00491
00492 if ( $myConfig->getConfigParam( "bl_showFbConnect" ) ) {
00493 $oFb = oxFb::getInstance();
00494 if ( $oFb->isConnected() && $oFb->getUser() ) {
00495 $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
00496 }
00497 }
00498
00499 $blRet = parent::save();
00500
00501
00502 if ( $blAddRemark && $blRet ) {
00503 $oRemark = oxNew( 'oxremark' );
00504 $oRemark->oxremark__oxtext = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00505 $oRemark->oxremark__oxtype = new oxField('r', oxField::T_RAW);
00506 $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00507 $oRemark->save();
00508 }
00509
00510 return $blRet;
00511 }
00512
00518 public function allowDerivedUpdate()
00519 {
00520 return true;
00521 }
00522
00530 public function inGroup( $sGroupID )
00531 {
00532 $blIn = false;
00533 if ( ( $oGroups = $this->getUserGroups() ) ) {
00534 $blIn = isset( $oGroups[ $sGroupID ] );
00535 }
00536
00537 return $blIn;
00538 }
00539
00548 public function delete( $sOXID = null )
00549 {
00550
00551 if ( !$sOXID ) {
00552 $sOXID = $this->getId();
00553 }
00554 if ( !$sOXID ) {
00555 return false;
00556 }
00557
00558 $blDeleted = parent::delete( $sOXID );
00559
00560 if ( $blDeleted ) {
00561 $oDB = oxDb::getDb();
00562 $sOXIDQuoted = $oDB->quote($sOXID);
00563
00564
00565 $rs = $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00566 $rs = $oDB->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00567
00568
00569 $rs = $oDB->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00570 $rs = $oDB->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00571
00572
00573 $rs = $oDB->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00574
00575
00576 $rs = $oDB->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00577
00578
00579 $rs = $oDB->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00580
00581
00582
00583 $rs = $oDB->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00584
00585 $blDeleted = $rs->EOF;
00586 }
00587
00588 return $blDeleted;
00589 }
00590
00598 public function load( $oxID )
00599 {
00600
00601 $blRet = parent::Load( $oxID );
00602
00603
00604 if ( isset( $this->oxuser__oxcreate->value ) ) {
00605 $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00606 }
00607
00608 return $blRet;
00609 }
00610
00618 public function exists( $sOXID = null )
00619 {
00620 $oDb = oxDb::getDb();
00621 if ( !$sOXID ) {
00622 $sOXID = $this->getId();
00623 }
00624
00625 $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00626 WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).'';
00627
00628 if ( $sOXID ) {
00629 $sSelect.= " or oxid = ".$oDb->quote( $sOXID ) . " ) ";
00630 } else {
00631 $sSelect.= ' ) ';
00632 }
00633
00634 if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00635 $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00636 }
00637
00638 $blExists = false;
00639 if ( ( $sOxid = oxDb::getDb()->getOne( $sSelect ) ) ) {
00640
00641 $this->setId( $sOxid );
00642 $blExists = true;
00643 }
00644 return $blExists;
00645 }
00646
00655 public function getOrders( $iLimit = false, $iPage = 0 )
00656 {
00657 $myConfig = $this->getConfig();
00658 $oOrders = oxNew( 'oxlist' );
00659 $oOrders->init( 'oxorder' );
00660
00661 if ( $iLimit !== false ) {
00662 $oOrders->setSqlLimit( $iLimit * $iPage, $iLimit );
00663 }
00664
00665
00666
00667
00668
00669
00670
00671
00672 if ( $this->oxuser__oxregister->value > 1 ) {
00673 $sQ = 'select * from oxorder where oxuserid = "'.$this->getId().'" and oxorderdate >= ' . oxDb::getDb()->quote( $this->oxuser__oxregister->value ) . ' ';
00674
00675
00676
00677 $sQ .= ' order by oxorderdate desc ';
00678 $oOrders->selectString( $sQ );
00679 }
00680
00681 return $oOrders;
00682 }
00683
00689 public function getOrderCount()
00690 {
00691 $iCnt = 0;
00692 if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00693 $oDb = oxDb::getDb();
00694 $sQ = 'select count(*) from oxorder where oxuserid = "'.$this->getId().'" AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00695 $iCnt = (int) $oDb->getOne( $sQ );
00696 }
00697
00698 return $iCnt;
00699 }
00700
00706 public function getNoticeListArtCnt()
00707 {
00708 if ( $this->_iCntNoticeListArticles === null ) {
00709 $this->_iCntNoticeListArticles = 0;
00710 if ( $this->getId() ) {
00711 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00712 }
00713 }
00714 return $this->_iCntNoticeListArticles;
00715 }
00716
00722 public function getWishListArtCnt()
00723 {
00724 if ( $this->_iCntWishListArticles === null ) {
00725 $this->_iCntWishListArticles = false;
00726 if ( $this->getId() ) {
00727 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00728 }
00729 }
00730 return $this->_iCntWishListArticles;
00731 }
00732
00739 public function getActiveCountry()
00740 {
00741 $sDeliveryCountry = '';
00742 $soxAddressId = oxSession::getVar( 'deladrid' );
00743 if ( $soxAddressId ) {
00744 $oDelAddress = oxNew( 'oxaddress' );
00745 $oDelAddress->load( $soxAddressId );
00746 $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00747 } elseif ( $this->getId() ) {
00748 $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00749 } else {
00750 $oUser = oxNew( 'oxuser' );
00751 if ( $oUser->loadActiveUser() ) {
00752 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00753 }
00754 }
00755
00756 return $sDeliveryCountry;
00757 }
00758
00766 public function createUser()
00767 {
00768 $oDB = oxDb::getDb();
00769 $sShopID = $this->getConfig()->getShopId();
00770
00771
00772 $sSelect = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00773 if ( !$this->_blMallUsers ) {
00774 $sSelect .= " and oxshopid = '{$sShopID}' ";
00775 }
00776 $sOXID = $oDB->getOne( $sSelect );
00777
00778
00779 if ( isset( $sOXID ) && $sOXID ) {
00780
00781 $this->delete( $sOXID );
00782 } elseif ( $this->_blMallUsers ) {
00783
00784 $sQ = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00785 if ( $oDB->getOne( $sQ ) ) {
00786 $oEx = oxNew( 'oxUserException' );
00787 $oLang = oxLang::getInstance();
00788 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00789 throw $oEx;
00790 }
00791 }
00792
00793 $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00794 if ( ( $blOK = $this->save() ) ) {
00795
00796 $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00797 $oDB->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00798 } else {
00799 $oEx = oxNew( 'oxUserException' );
00800 $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00801 throw $oEx;
00802 }
00803
00804 return $blOK;
00805 }
00806
00814 public function addToGroup( $sGroupID )
00815 {
00816 if ( !$this->inGroup( $sGroupID ) ) {
00817 $oNewGroup = oxNew( 'oxobject2group' );
00818 $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00819 $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00820 if ( $oNewGroup->save() ) {
00821 $this->_oGroups[$sGroupID] = $oNewGroup;
00822 return true;
00823 }
00824 }
00825 return false;
00826 }
00827
00835 public function removeFromGroup( $sGroupID = null )
00836 {
00837 if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00838 $oGroups = oxNew( 'oxlist' );
00839 $oGroups->init( 'oxobject2group' );
00840 $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00841 $oGroups->selectString( $sSelect );
00842 foreach ( $oGroups as $oRemgroup ) {
00843 if ( $oRemgroup->delete() ) {
00844 unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00845 }
00846 }
00847 }
00848 }
00849
00858 public function onOrderExecute( $oBasket, $iSuccess )
00859 {
00860
00861 if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00862
00863 if ( !$this->oxuser__oxdisableautogrp->value ) {
00864
00865 $myConfig = $this->getConfig();
00866 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00867 $dLargeCustPrice = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00868
00869 $this->addToGroup( 'oxidcustomer' );
00870 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00871 if ( $dBasketPrice < $dMidlleCustPrice ) {
00872 $this->addToGroup( 'oxidsmallcust' );
00873 }
00874 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00875 $this->addToGroup( 'oxidmiddlecust' );
00876 }
00877 if ( $dBasketPrice >= $dLargeCustPrice ) {
00878 $this->addToGroup( 'oxidgoodcust' );
00879 }
00880 }
00881
00882 if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00883 $this->removeFromGroup( 'oxidnotyetordered' );
00884 }
00885 }
00886 }
00887
00895 public function getBasket( $sName )
00896 {
00897 if ( !isset( $this->_aBaskets[$sName] ) ) {
00898 $oBasket = oxNew( 'oxuserbasket' );
00899 $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00900
00901
00902 if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00903 $oBasket->oxuserbaskets__oxtitle = new oxField($sName);
00904 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00905
00906
00907 $oBasket->setIsNewBasket();
00908 }
00909
00910 $this->_aBaskets[$sName] = $oBasket;
00911 }
00912
00913 return $this->_aBaskets[$sName];
00914 }
00915
00924 public function convertBirthday( $aData )
00925 {
00926
00927
00928 $iYear = isset($aData['year'])?((int) $aData['year']):false;
00929 $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00930 $iDay = isset($aData['day'])?((int) $aData['day']):false;
00931
00932
00933 if ( !$iYear && !$iMonth && !$iDay )
00934 return "";
00935
00936
00937 if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00938 $iYear = date('Y');
00939
00940
00941 if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00942 $iMonth = 1;
00943
00944
00945 $iMaxDays = 31;
00946 switch( $iMonth) {
00947 case 2 :
00948 if ($iMaxDays > 28)
00949 $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00950 break;
00951 case 4 :
00952 case 6 :
00953 case 9 :
00954 case 11 :
00955 $iMaxDays = min(30, $iMaxDays);
00956 break;
00957 }
00958
00959
00960 if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00961 $iDay = 1;
00962 }
00963
00964
00965 return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00966 }
00967
00974 public function getBoni()
00975 {
00976 return 1000;
00977 }
00978
00991 public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00992 {
00993
00994 $sDynGoup = strtolower( trim( $sDynGoup ) );
00995
00996
00997 $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
00998
00999
01000 $blAdd = false;
01001
01002
01003 if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
01004 $blAdd = false;
01005 } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
01006
01007 $blAdd = false;
01008 } elseif ( $this->addToGroup( $sDynGoup ) ) {
01009 $blAdd = true;
01010 }
01011
01012
01013 oxSession::deleteVar( 'dgr' );
01014
01015 return $blAdd;
01016 }
01017
01036 public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01037 {
01038
01039 $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
01040
01041
01042 $this->_checkEmail( $sLogin );
01043
01044
01045 $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
01046
01047
01048 $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
01049
01050
01051 $this->_checkCountries( $aInvAddress, $aDelAddress );
01052
01053
01054 $this->_checkVatId( $aInvAddress );
01055
01056
01057
01058 if ( $oError = oxInputValidator::getInstance()->getFirstValidationError( $this->getId() ) ) {
01059 throw $oError;
01060 }
01061 }
01062
01071 public function setNewsSubscription( $blSubscribe, $blSendOptIn )
01072 {
01073
01074 $blSuccess = false;
01075 $myConfig = $this->getConfig();
01076 $mySession = $this->getSession();
01077
01078
01079 $oNewsSubscription = $this->getNewsSubscription();
01080 if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
01081 if ( !$blSendOptIn ) {
01082
01083
01084 $this->addToGroup( 'oxidnewsletter' );
01085
01086 $oNewsSubscription->setOptInStatus( 1 );
01087 $blSuccess = true;
01088 } else {
01089
01090 $oNewsSubscription->setOptInStatus( 2 );
01091
01092
01093 if ( !$mySession->getVar( "blDBOptInMailAlreadyDone" ) ) {
01094
01095
01096 $oEmail = oxNew( 'oxemail' );
01097 $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01098
01099 if ( $blSuccess ) {
01100
01101 oxSession::setVar( "blDBOptInMailAlreadyDone", true );
01102 }
01103 } else {
01104
01105 $blSuccess = true;
01106 }
01107 }
01108 } elseif ( !$blSubscribe ) {
01109
01110 $this->removeFromGroup( 'oxidnewsletter' );
01111 $oNewsSubscription->setOptInStatus( 0 );
01112 $blSuccess = true;
01113 }
01114
01115 return $blSuccess;
01116 }
01117
01135 public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01136 {
01137
01138 $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01139
01140
01141 $this->assign( $aInvAddress );
01142
01143
01144
01145 $this->_assignAddress( $aDelAddress );
01146
01147
01148 if ( $this->save() ) {
01149
01150
01151 $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01152 $this->_setAutoGroups( $sCountryId );
01153 }
01154 }
01155
01166 public function addUserAddress( $oUser )
01167 {
01168 if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01169 return false;
01170 }
01171
01172 $oAddress = oxNew( 'oxaddress' );
01173 $oAddress->oxaddress__oxuserid = new oxField($this->getId(), oxField::T_RAW);
01174 $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01175 $oAddress->oxaddress__oxfname = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01176 $oAddress->oxaddress__oxlname = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01177 $oAddress->oxaddress__oxstreet = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01178 $oAddress->oxaddress__oxstreetnr = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01179 $oAddress->oxaddress__oxcity = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01180 $oAddress->oxaddress__oxzip = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01181 $oAddress->oxaddress__oxcountry = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01182 $oAddress->oxaddress__oxcountryid = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01183 $oAddress->oxaddress__oxcompany = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01184
01185
01186 if ( $oAddress->save() ) {
01187
01188 $this->_aAddresses = null;
01189 return $oAddress->getId();
01190 }
01191 }
01192
01200 protected function _assignAddress( $aDelAddress )
01201 {
01202 if ( is_array( $aDelAddress ) && count( $aDelAddress ) ) {
01203 $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01204 $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ? null : $sAddressId;
01205 $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01206 $oAddress = oxNew( 'oxaddress' );
01207 $oAddress->assign( $aDelAddress );
01208 $oAddress->oxaddress__oxuserid = new oxField( $this->getId(), oxField::T_RAW );
01209 $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01210 $oAddress->save();
01211
01212
01213 $this->_aAddresses = null;
01214
01215
01216 oxSession::setVar( 'deladrid', $oAddress->getId() );
01217 } else {
01218
01219 oxSession::setVar( 'deladrid', null );
01220 }
01221 }
01222
01235 public function login( $sUser, $sPassword, $blCookie = false)
01236 {
01237 if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01238 $oEx = oxNew( 'oxCookieException' );
01239 $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01240 throw $oEx;
01241 }
01242
01243 $myConfig = $this->getConfig();
01244 if ( $sPassword ) {
01245
01246 $sShopID = $myConfig->getShopId();
01247 $oDb = oxDb::getDb();
01248
01249 $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01250 $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01251 $sShopSelect = "";
01252
01253
01254
01255 if ( $this->isAdmin() ) {
01256 $sShopSelect = " and ( oxrights != 'user' ) ";
01257 }
01258
01259 $sWhat = "oxid";
01260
01261 $sSelect = "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01262 if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
01263 if ( $sPassword == "admin" && $sUser == "admin" ) {
01264 $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01265 } else {
01266 $oEx = oxNew( 'oxUserException' );
01267 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01268 throw $oEx;
01269 }
01270 }
01271
01272
01273 $aData = $oDb->getAll( $sSelect );
01274 $sOXID = @$aData[0][0];
01275 if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01276
01277 if ( !$this->load( $sOXID ) ) {
01278 $oEx = oxNew( 'oxUserException' );
01279 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01280 throw $oEx;
01281 }
01282 }
01283 }
01284
01285
01286
01287 if ( $this->oxuser__oxid->value ) {
01288
01289
01290
01291 $this->setUser( null );
01292
01293 if ( $this->isAdmin() ) {
01294 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01295 } else {
01296 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01297 }
01298
01299
01300 if ( $blCookie ) {
01301 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01302 }
01303
01304
01305 try {
01306 if ($oBasket = $this->getSession()->getBasket()) {
01307 $oBasket->load();
01308 }
01309
01310 } catch (Exception $oE) {
01311
01312 }
01313
01314 return true;
01315 } else {
01316 $oEx = oxNew( 'oxUserException' );
01317 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01318 throw $oEx;
01319 }
01320 }
01321
01327 public function logout()
01328 {
01329
01330 oxSession::deleteVar( 'usr' );
01331 oxSession::deleteVar( 'auth' );
01332 oxSession::deleteVar( 'dgr' );
01333 oxSession::deleteVar( 'dynvalue' );
01334 oxSession::deleteVar( 'paymentid' );
01335
01336
01337
01338 oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01339
01340
01341 $this->setUser( null );
01342
01343 return true;
01344 }
01345
01352 public function loadAdminUser()
01353 {
01354 return $this->loadActiveUser( true );
01355 }
01356
01365 public function loadActiveUser( $blForceAdmin = false )
01366 {
01367 $myConfig = $this->getConfig();
01368
01369 $blAdmin = $this->isAdmin() || $blForceAdmin;
01370 $oDB = oxDb::getDb();
01371
01372
01373 $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01374 $blFoundInCookie = false;
01375
01376
01377 if ( !$sUserID && !$blAdmin ) {
01378 $sShopID = $myConfig->getShopId();
01379 if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01380 $aData = explode( '@@@', $sSet );
01381 $sUser = $aData[0];
01382 $sPWD = @$aData[1];
01383
01384 $sSelect = 'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and oxuser.oxactive = 1 and oxuser.oxusername = '.$oDB->quote($sUser);
01385
01386
01387 $oDB = oxDb::getDb();
01388 $rs = $oDB->execute( $sSelect );
01389 if ( $rs != false && $rs->recordCount() > 0 ) {
01390 while (!$rs->EOF) {
01391 $sTest = crypt( $rs->fields[1], 'ox' );
01392 if ( $sTest == $sPWD ) {
01393
01394 $sUserID = $rs->fields[0];
01395 $blFoundInCookie = true;
01396 break;
01397 }
01398 $rs->moveNext();
01399 }
01400 }
01401 }
01402 }
01403
01404
01405
01406 if ( $myConfig->getConfigParam( "bl_showFbConnect") && !$sUserID && !$blAdmin ) {
01407 $oFb = oxFb::getInstance();
01408 if ( $oFb->isConnected() && $oFb->getUser() ) {
01409 $sUserSelect = "oxuser.oxfbid = " . $oDB->quote( $oFb->getUser() );
01410 $sShopSelect = "";
01411
01412
01413 $sSelect = "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01414 $sUserID = $oDB->getOne( $sSelect );
01415 }
01416 }
01417
01418
01419 if ( $sUserID ) {
01420 if ( $this->load( $sUserID ) ) {
01421
01422 if ($blAdmin) {
01423 oxSession::setVar( 'auth', $sUserID );
01424 } else {
01425 oxSession::setVar( 'usr', $sUserID );
01426 }
01427
01428
01429 $this->_blLoadedFromCookie = $blFoundInCookie;
01430 return true;
01431 }
01432 } else {
01433
01434 if ($blAdmin) {
01435 oxSession::deleteVar( 'auth' );
01436 } else {
01437 oxSession::deleteVar( 'usr' );
01438 }
01439
01440 return false;
01441 }
01442 }
01443
01456 protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01457 {
01458 $aLDAPParams = $this->getConfig()->getConfigParam( 'aLDAPParams' );
01459 $oLDAP = oxNew( "oxLDAP", $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01460
01461
01462 $oDb = oxDb::getDb();
01463 $sLDAPKey = $oDb->getOne( "select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote( $sUser )." $sShopSelect");
01464 if ( isset( $sLDAPKey) && $sLDAPKey) {
01465 $sUser = $sLDAPKey;
01466 }
01467
01468
01469 $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER'] );
01470
01471 $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01472 if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01473
01474
01475
01476 $sSelect = "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote( $aData['OXUSERNAME'] )." $sShopSelect";
01477 $sOXID = $oDb->getOne( $sSelect );
01478
01479 if ( !isset( $sOXID ) || !$sOXID ) {
01480
01481
01482 $this->setId();
01483
01484
01485 foreach ( $aData as $fldname => $value) {
01486 $sField = "oxuser__".strtolower( $fldname);
01487 $this->$sField = new oxField( $aData[$fldname] );
01488 }
01489
01490 $this->oxuser__oxactive = new oxField( 1 );
01491 $this->oxuser__oxshopid = new oxField( $sShopID );
01492 $this->oxuser__oxldapkey = new oxField( $sUser );
01493 $this->oxuser__oxrights = new oxField( "user" );
01494 $this->setPassword( "ldap user" );
01495
01496 $this->save();
01497 } else {
01498
01499 $this->load( $sOXID);
01500 }
01501
01502 } else {
01503 $oEx = oxNew( 'oxUserException' );
01504 $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01505 throw $oEx;
01506 }
01507 }
01508
01515 protected function _getUserRights()
01516 {
01517
01518 if ( !$this->oxuser__oxrights->value )
01519 return 'user';
01520
01521 $oDB = oxDb::getDb();
01522 $myConfig = $this->getConfig();
01523 $sAuthRights = null;
01524
01525
01526 $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01527 $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01528 if ( $sAuthUserID ) {
01529 $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01530 }
01531
01532
01533 $aRights = array();
01534
01535
01536 if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01537 $aRights[] = $sCurrRights;
01538 }
01539 $aRights[] = 'user';
01540
01541 if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01542 return current( $aRights );
01543 } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01544 $aRights[] = $sAuthRights;
01545 if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01546 return current( $aRights );
01547 }
01548 }
01549
01550
01551 return $this->oxuser__oxrights->value;
01552 }
01553
01563 protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01564 {
01565
01566
01567
01568
01569
01570
01571 return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01572 }
01573
01579 protected function _insert()
01580 {
01581
01582
01583 $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01584
01585 if ( !isset( $this->oxuser__oxboni->value ) ) {
01586 $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01587 }
01588
01589 return parent::_insert();
01590 }
01591
01597 protected function _update()
01598 {
01599
01600 if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01601 $this->_aSkipSaveFields[] = 'oxboni';
01602 }
01603
01604
01605 $this->_aSkipSaveFields[] = 'oxcreate';
01606 if ( !$this->isAdmin() ) {
01607 $this->_aSkipSaveFields[] = 'oxcustnr';
01608 $this->_aSkipSaveFields[] = 'oxrights';
01609 }
01610
01611
01612 if ( ( $blUpdate = parent::_update() ) ) {
01613 $this->getNewsSubscription()->updateSubscription( $this );
01614 }
01615
01616 return $blUpdate;
01617 }
01618
01634 protected function _checkLogin( $sLogin, $aInvAddress )
01635 {
01636 $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername'] : $sLogin;
01637 oxInputValidator::getInstance()->checkLogin( $this, $sLogin, $aInvAddress );
01638 return $sLogin;
01639 }
01640
01648 public function checkIfEmailExists( $sEmail )
01649 {
01650 $myConfig = $this->getConfig();
01651 $oDB = oxDb::getDb();
01652 $iShopId = $myConfig->getShopId();
01653 $blExists = false;
01654
01655 $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01656 if ( ( $sOxid = $this->getId() ) ) {
01657 $sQ .= " and oxid <> '$sOxid' ";
01658 }
01659
01660 $oRs = $oDB->execute( $sQ );
01661 if ( $oRs != false && $oRs->recordCount() > 0 ) {
01662
01663 if ( $this->_blMallUsers ) {
01664
01665 $blExists = true;
01666 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01667
01668
01669 $blExists = false;
01670 }
01671 } else {
01672
01673 $blExists = false;
01674 while ( !$oRs->EOF ) {
01675 if ( $oRs->fields[1] != 'user' ) {
01676
01677
01678 $blExists = true;
01679 break;
01680 } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01681
01682
01683 $blExists = true;
01684 break;
01685 }
01686
01687 $oRs->moveNext();
01688 }
01689 }
01690 }
01691 return $blExists;
01692 }
01693
01701 public function getUserRecommLists( $sOXID = null )
01702 {
01703 if ( !$sOXID )
01704 $sOXID = $this->getId();
01705
01706
01707 $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01708 $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01709
01710
01711 $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01712 $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01713
01714
01715 $oRecommList = oxNew( 'oxlist' );
01716 $oRecommList->init( 'oxrecommlist' );
01717 $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01718 $iShopId = $this->getConfig()->getShopId();
01719 $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01720 $oRecommList->selectString( $sSelect );
01721
01722 return $oRecommList;
01723 }
01724
01732 public function getRecommListsCount( $sOx = null )
01733 {
01734 if ( !$sOx ) {
01735 $sOx = $this->getId();
01736 }
01737
01738 if ( $this->_iCntRecommLists === null || $sOx ) {
01739 $oDb = oxDb::getDb();
01740 $this->_iCntRecommLists = 0;
01741 $iShopId = $this->getConfig()->getShopId();
01742 $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01743 $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01744 }
01745 return $this->_iCntRecommLists;
01746 }
01747
01758 protected function _checkEmail( $sEmail )
01759 {
01760 oxInputValidator::getInstance()->checkEmail( $this, $sEmail );
01761 }
01762
01777 protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01778 {
01779 return $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01780 }
01781
01794 public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01795 {
01796 return oxInputValidator::getInstance()->checkPassword( $this, $sNewPass, $sConfPass, $blCheckLenght );
01797 }
01798
01809 protected function _checkCountries( $aInvAddress, $aDelAddress )
01810 {
01811 oxInputValidator::getInstance()->checkCountries( $this, $aInvAddress, $aDelAddress );
01812 }
01813
01825 protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01826 {
01827 oxInputValidator::getInstance()->checkRequiredFields( $this, $aInvAddress, $aDelAddress );
01828 }
01829
01840 protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01841 {
01842 oxInputValidator::getInstance()->checkRequiredArrayFields( $this, $sFieldName, $aFieldValues );
01843 }
01844
01855 protected function _checkVatId( $aInvAddress )
01856 {
01857 oxInputValidator::getInstance()->checkVatId( $this, $aInvAddress );
01858 }
01859
01868 protected function _setAutoGroups( $sCountryId )
01869 {
01870
01871 $blForeigner = true;
01872 $blForeignGroupExists = false;
01873 $blInlandGroupExists = false;
01874
01875 $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01876
01877 if ( is_array($aHomeCountry)) {
01878 if (in_array($sCountryId, $aHomeCountry)) {
01879 $blForeigner = false;
01880 }
01881 } elseif ($sCountryId == $aHomeCountry) {
01882 $blForeigner = false;
01883 }
01884
01885 if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01886 $blForeignGroupExists = true;
01887 if ( !$blForeigner ) {
01888 $this->removeFromGroup( 'oxidforeigncustomer' );
01889 }
01890 }
01891
01892 if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01893 $blInlandGroupExists = true;
01894 if ( $blForeigner ) {
01895 $this->removeFromGroup( 'oxidnewcustomer' );
01896 }
01897 }
01898
01899 if ( !$this->oxuser__oxdisableautogrp->value ) {
01900 if ( !$blForeignGroupExists && $blForeigner ) {
01901 $this->addToGroup( 'oxidforeigncustomer' );
01902 }
01903 if ( !$blInlandGroupExists && !$blForeigner ) {
01904 $this->addToGroup( 'oxidnewcustomer' );
01905 }
01906 }
01907 }
01908
01917 protected function _hasUserAddress( $sUserId )
01918 {
01919 $oAddresses = $this->getUserAddresses();
01920 if ( $oAddresses && count($oAddresses)>0 ) {
01921 $oAddresses->rewind() ;
01922 foreach ($oAddresses as $key => $oAddress) {
01923 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01924 return true;
01925 }
01926 }
01927 }
01928 return false;
01929 }
01930
01943 protected function _setUserCookie( $sUser, $sPassword, $sShopId = null, $iTimeout = 31536000 )
01944 {
01945 oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
01946 }
01947
01957 protected function _deleteUserCookie( $sShopId = null )
01958 {
01959 oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
01960 }
01961
01971 protected static function _getUserCookie( $sShopId = null )
01972 {
01973 return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
01974 }
01975
01976
01985 public function loadUserByUpdateId( $sUid )
01986 {
01987 $oDb = oxDb::getDb();
01988 $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
01989 if ( $sUserId = $oDb->getOne( $sQ ) ) {
01990 return $this->load( $sUserId );
01991 }
01992 }
01993
02001 public function setUpdateKey( $blReset = false )
02002 {
02003 $sUpKey = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02004 $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02005
02006
02007 $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02008
02009
02010 $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02011
02012
02013 $this->save();
02014 }
02015
02021 public function getUpdateLinkTerm()
02022 {
02023 return 3600 * 6;
02024 }
02025
02033 public function isExpiredUpdateId( $sKey )
02034 {
02035 $oDb = oxDb::getDb();
02036 $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02037 return !( (bool) $oDb->getOne( $sQ ) );
02038 }
02039
02045 public function getUpdateId()
02046 {
02047 if ( $this->_sUpdateKey === null ) {
02048 $this->setUpdateKey();
02049 $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02050 }
02051 return $this->_sUpdateKey;
02052 }
02053
02062 public function encodePassword( $sPassword, $sSalt )
02063 {
02064 $oDb = oxDb::getDb();
02065 return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02066 }
02067
02075 public function prepareSalt( $sSalt )
02076 {
02077 return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02078 }
02079
02087 public function decodeSalt( $sSaltHex )
02088 {
02089 return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02090 }
02091
02099 public function setPassword( $sPassword = null )
02100 {
02101
02102 $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02103
02104
02105 $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02106
02107 $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02108 $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02109 }
02110
02118 public function isSamePassword( $sNewPass )
02119 {
02120 return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02121 }
02122
02128 public function isLoadedFromCookie()
02129 {
02130 return $this->_blLoadedFromCookie;
02131 }
02132
02139 public function getPasswordHash()
02140 {
02141 $sHash = null;
02142 if ( $this->oxuser__oxpassword->value ) {
02143 if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02144
02145 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02146 } elseif ( strlen( $this->oxuser__oxpassword->value ) < 32 ) {
02147
02148 $this->setPassword( $this->oxuser__oxpassword->value );
02149 }
02150 $sHash = $this->oxuser__oxpassword->value;
02151 }
02152 return $sHash;
02153 }
02154
02163 public static function getAdminUser()
02164 {
02165 return self::getActiveUser( true );
02166 }
02167
02178 public static function getActiveUser( $blForceAdmin = false )
02179 {
02180 $oUser = oxNew( 'oxuser' );
02181 if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02182 return $oUser;
02183 } else {
02184 return false;
02185 }
02186 }
02187
02195 public function getReviewUserHash( $sUserId )
02196 {
02197 $oDb = oxDb::getDb();
02198 $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02199 return $sReviewUserHash;
02200 }
02201
02209 public function getReviewUserId( $sReviewUserHash )
02210 {
02211 $oDb = oxDb::getDb();
02212 $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02213 return $sUserId;
02214 }
02215
02221 public function getState()
02222 {
02223 return $this->oxuser__oxstateid->value;
02224 }
02225
02231 public function isTermsAccepted()
02232 {
02233 $sShopId = $this->getConfig()->getShopId();
02234 $sUserId = $this->getId();
02235 return (bool) oxDb::getDb()->getOne( "select 1 from oxacceptedterms where oxuserid='{$sUserId}' and oxshopid='{$sShopId}'" );
02236 }
02237
02243 public function acceptTerms()
02244 {
02245 $sUserId = $this->getId();
02246 $sShopId = $this->getConfig()->getShopId();
02247 $sVersion = oxNew( "oxcontent" )->getTermsVersion();
02248
02249 oxDb::getDb()->execute( "replace oxacceptedterms set oxuserid='{$sUserId}', oxshopid='{$sShopId}', oxtermversion='{$sVersion}'" );
02250 }
02251
02261 public function setCreditPointsForRegistrant( $sUserId, $sRecEmail )
02262 {
02263 $blSet = false;
02264 $oDb = oxDb::getDb();
02265 $iPoints = $this->getConfig()->getConfigParam( 'dPointsForRegistration' );
02266
02267 $iPending = $oDb->getOne( "select count(oxuserid) from oxinvitations where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail )." and oxpending = 1 and oxaccepted = 0" );
02268 if ( $iPoints && $iPending ) {
02269 $this->oxuser__oxpoints = new oxField( $iPoints, oxField::T_RAW );
02270 if ( $blSet = $this->save() ) {
02271
02272 $oDb->execute( "UPDATE oxinvitations SET oxpending = '0', oxaccepted = '1' where oxuserid = ".$oDb->quote( $sUserId )." and md5(oxemail) = ".$oDb->quote( $sRecEmail ) );
02273 $oInvUser = oxNew( "oxuser" );
02274 if ( $oInvUser->load( $sUserId ) ) {
02275 $blSet = $oInvUser->setCreditPointsForInviter();
02276 }
02277 }
02278 }
02279 oxSession::deleteVar( 'su' );
02280 oxSession::deleteVar( 're' );
02281
02282 return $blSet;
02283 }
02284
02290 public function setCreditPointsForInviter()
02291 {
02292 $blSet = false;
02293 $iPoints = $this->getConfig()->getConfigParam( 'dPointsForInvitation' );
02294 if ( $iPoints ) {
02295 $iNewPoints = $this->oxuser__oxpoints->value + $iPoints;
02296 $this->oxuser__oxpoints = new oxField( $iNewPoints, oxField::T_RAW );
02297 $blSet = $this->save();
02298 }
02299
02300 return $blSet;
02301 }
02302
02308 public function updateFbId()
02309 {
02310 $oFb = oxFb::getInstance();
02311 $blRet = false;
02312
02313 if ( $oFb->isConnected() && $oFb->getUser() ) {
02314 $this->oxuser__oxfbid = new oxField( $oFb->getUser() );
02315 $blRet = $this->save();
02316 }
02317
02318 return $blRet;
02319 }
02320
02328 public function updateInvitationStatistics( $aRecEmail )
02329 {
02330 $oDb = oxDb::getDb( true );
02331 $sUserId = $this->getId();
02332
02333 if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
02334
02335 $sDate = oxUtilsDate::getInstance()->formatDBDate( date("Y-m-d"), true );
02336 $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
02337 foreach ( $aRecEmail as $sRecEmail ) {
02338 $sSql = "INSERT INTO oxinvitations SET oxuserid = '$sUserId', oxemail = $sRecEmail, oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
02339 $oDb->execute( $sSql );
02340 }
02341 }
02342 }
02343
02344 }