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
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
00198 if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00199 if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00200
00201
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
00271
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
00294 $this->_oAddresses = oxNew( 'oxlist' );
00295 $this->_oAddresses->init( "oxaddress" );
00296 $this->_oAddresses->selectString( $sSelect );
00297
00298
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 setSelectedAddressId( $sAddressId )
00318 {
00319 $this->_sSelAddressId = $sAddressId;
00320 }
00321
00327 public function getSelectedAddressId()
00328 {
00329 if ( $this->_sSelAddressId !== null ) {
00330 return $this->_sSelAddressId;
00331 }
00332
00333 $sAddressId = oxConfig::getParameter( "oxaddressid");
00334 if ( !$sAddressId && !oxConfig::getParameter( 'reloadaddress' ) ) {
00335 $sAddressId = oxSession::getVar( "deladrid" );
00336 }
00337 return $sAddressId;
00338 }
00339
00348 public function getSelectedAddress( $sWishId = false )
00349 {
00350 $oAddresses = $this->getUserAddresses();
00351 if ( $oAddresses->count() ) {
00352 if ( $sAddressId = $this->getSelectedAddressId() ) {
00353 foreach ( $oAddresses as $oAddress ) {
00354 if ( $oAddress->selected == 1 ) {
00355 $sAddressId = $oAddress->getId();
00356 break;
00357 }
00358 }
00359 } elseif ( $sWishId ) {
00360 foreach ( $oAddresses as $oAddress ) {
00361 $oAddress->selected = 0;
00362 if ( $oAddress->oxaddress__oxaddressuserid->value == $sWishId ) {
00363 $oAddress->selected = 1;
00364 $sAddressId = $oAddress->getId();
00365 }
00366 }
00367 }
00368
00369
00370 if ( !$sAddressId ) {
00371 $oAddresses->rewind();
00372 $oAddress = $oAddresses->current();
00373 $oAddress->selected = 1;
00374 $sAddressId = $oAddress->getId();
00375 }
00376 }
00377
00378 return $sAddressId;
00379 }
00380
00388 public function getUserPayments( $sOXID = null )
00389 {
00390 if ( $this->_oPayments === null ) {
00391
00392 if ( !$sOXID ) {
00393 $sOXID = $this->getId();
00394 }
00395
00396 $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00397
00398 $this->_oPayments = oxNew( 'oxlist' );
00399 $this->_oPayments->init( 'oxUserPayment' );
00400 $this->_oPayments->selectString( $sSelect );
00401
00402 $myUtils = oxUtils::getInstance();
00403 foreach ( $this->_oPayments as $oPayment ) {
00404
00405 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00406 }
00407 }
00408
00409 return $this->_oPayments;
00410 }
00411
00417 public function save()
00418 {
00419
00420 $blAddRemark = false;
00421 if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00422 $blAddRemark = true;
00423
00424 $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00425 }
00426
00427
00428 $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00429
00430
00431 if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00432 $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00433 }
00434
00435 $blRet = parent::save();
00436
00437
00438 if ( $blAddRemark && $blRet ) {
00439 $oRemark = oxNew( 'oxremark' );
00440 $oRemark->oxremark__oxtext = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00441 $oRemark->oxremark__oxtype = new oxField('r', oxField::T_RAW);
00442 $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00443 $oRemark->save();
00444 }
00445
00446 return $blRet;
00447 }
00448
00454 public function allowDerivedUpdate()
00455 {
00456 return true;
00457 }
00458
00466 public function inGroup( $sGroupID )
00467 {
00468 $blIn = false;
00469 if ( ( $oGroups = $this->getUserGroups() ) ) {
00470 $blIn = isset( $oGroups[ $sGroupID ] );
00471 }
00472
00473 return $blIn;
00474 }
00475
00484 public function delete( $sOXID = null )
00485 {
00486
00487 if ( !$sOXID ) {
00488 $sOXID = $this->getId();
00489 }
00490 if ( !$sOXID ) {
00491 return false;
00492 }
00493
00494 $blDeleted = parent::delete( $sOXID );
00495
00496 if ( $blDeleted ) {
00497 $oDB = oxDb::getDb();
00498 $sOXIDQuoted = $oDB->quote($sOXID);
00499
00500
00501 $rs = $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00502 $rs = $oDB->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00503
00504
00505 $rs = $oDB->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00506 $rs = $oDB->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00507
00508
00509 $rs = $oDB->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00510
00511
00512 $rs = $oDB->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00513
00514
00515 $rs = $oDB->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00516
00517
00518
00519 $rs = $oDB->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00520
00521 $blDeleted = $rs->EOF;
00522 }
00523
00524 return $blDeleted;
00525 }
00526
00534 public function load( $oxID )
00535 {
00536
00537 $blRet = parent::Load( $oxID );
00538
00539
00540 if ( isset( $this->oxuser__oxcreate->value ) ) {
00541 $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00542 }
00543
00544 return $blRet;
00545 }
00546
00554 public function exists( $sOXID = null )
00555 {
00556 $oDb = oxDb::getDb();
00557 if ( !$sOXID ) {
00558 $sOXID = $this->getId();
00559 }
00560
00561 $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00562 WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).'';
00563
00564 if ( $sOXID ) {
00565 $sSelect.= " or oxid = ".$oDb->quote( $sOXID ) . " ) ";
00566 } else {
00567 $sSelect.= ' ) ';
00568 }
00569
00570 if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00571 $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00572 }
00573
00574 $blExists = false;
00575 if ( ( $sOxid = oxDb::getDb()->getOne( $sSelect ) ) ) {
00576
00577 $this->setId( $sOxid );
00578 $blExists = true;
00579 }
00580 return $blExists;
00581 }
00582
00588 public function getOrders()
00589 {
00590 $myConfig = $this->getConfig();
00591 $oOrders = oxNew( 'oxlist' );
00592 $oOrders->init( 'oxorder' );
00593
00594
00595
00596
00597
00598
00599
00600
00601 if ( $this->oxuser__oxregister->value > 1 ) {
00602 $sQ = 'select * from oxorder where oxuserid = "'.$this->getId().'" and oxorderdate >= ' . oxDb::getDb()->quote( $this->oxuser__oxregister->value ) . ' ';
00603
00604
00605
00606 $sQ .= ' order by oxorderdate desc ';
00607 $oOrders->selectString( $sQ );
00608 }
00609
00610 return $oOrders;
00611 }
00612
00618 public function getOrderCount()
00619 {
00620 $iCnt = 0;
00621 if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00622 $oDb = oxDb::getDb();
00623 $sQ = 'select count(*) from oxorder where oxuserid = "'.$this->getId().'" AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00624 $iCnt = (int) $oDb->getOne( $sQ );
00625 }
00626
00627 return $iCnt;
00628 }
00629
00635 public function getNoticeListArtCnt()
00636 {
00637 if ( $this->_iCntNoticeListArticles === null ) {
00638 $this->_iCntNoticeListArticles = 0;
00639 if ( $this->getId() ) {
00640 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00641 }
00642 }
00643 return $this->_iCntNoticeListArticles;
00644 }
00645
00651 public function getWishListArtCnt()
00652 {
00653 if ( $this->_iCntWishListArticles === null ) {
00654 $this->_iCntWishListArticles = false;
00655 if ( $this->getId() ) {
00656 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00657 }
00658 }
00659 return $this->_iCntWishListArticles;
00660 }
00661
00668 public function getActiveCountry()
00669 {
00670 $sDeliveryCountry = '';
00671 if ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) {
00672 $oDelAddress = oxNew( 'oxaddress' );
00673 $oDelAddress->load( $soxAddressId );
00674 $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00675 } elseif ( $this->getId() ) {
00676 $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00677 } else {
00678 $oUser = oxNew( 'oxuser' );
00679 if ( $oUser->loadActiveUser() ) {
00680 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00681 }
00682 }
00683
00684 return $sDeliveryCountry;
00685 }
00686
00694 public function createUser()
00695 {
00696 $oDB = oxDb::getDb();
00697 $sShopID = $this->getConfig()->getShopId();
00698
00699
00700 $sSelect = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00701 if ( !$this->_blMallUsers ) {
00702 $sSelect .= " and oxshopid = '{$sShopID}' ";
00703 }
00704 $sOXID = $oDB->getOne( $sSelect );
00705
00706
00707 if ( isset( $sOXID ) && $sOXID ) {
00708
00709 $this->delete( $sOXID );
00710 } elseif ( $this->_blMallUsers ) {
00711 $sQ = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00712 if ( $oDB->getOne( $sQ ) ) {
00713 $oEx = oxNew( 'oxUserException' );
00714 $oLang = oxLang::getInstance();
00715 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00716 throw $oEx;
00717 }
00718 }
00719
00720 $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00721 if ( ( $blOK = $this->save() ) ) {
00722
00723 $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00724 $oDB->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00725 } else {
00726 $oEx = oxNew( 'oxUserException' );
00727 $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00728 throw $oEx;
00729 }
00730
00731 return $blOK;
00732 }
00733
00741 public function addToGroup( $sGroupID )
00742 {
00743 if ( !$this->inGroup( $sGroupID ) ) {
00744 $oNewGroup = oxNew( 'oxobject2group' );
00745 $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00746 $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00747 if ( $oNewGroup->save() ) {
00748 $this->_oGroups[$sGroupID] = $oNewGroup;
00749 return true;
00750 }
00751 }
00752 return false;
00753 }
00754
00762 public function removeFromGroup( $sGroupID = null )
00763 {
00764 if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00765 $oGroups = oxNew( 'oxlist' );
00766 $oGroups->init( 'oxobject2group' );
00767 $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00768 $oGroups->selectString( $sSelect );
00769 foreach ( $oGroups as $oRemgroup ) {
00770 if ( $oRemgroup->delete() ) {
00771 unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00772 }
00773 }
00774 }
00775 }
00776
00785 public function onOrderExecute( $oBasket, $iSuccess )
00786 {
00787
00788 if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00789
00790 if ( !$this->oxuser__oxdisableautogrp->value ) {
00791
00792 $myConfig = $this->getConfig();
00793 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00794 $dLargeCustPrice = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00795
00796 $this->addToGroup( 'oxidcustomer' );
00797 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00798 if ( $dBasketPrice < $dMidlleCustPrice ) {
00799 $this->addToGroup( 'oxidsmallcust' );
00800 }
00801 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00802 $this->addToGroup( 'oxidmiddlecust' );
00803 }
00804 if ( $dBasketPrice >= $dLargeCustPrice ) {
00805 $this->addToGroup( 'oxidgoodcust' );
00806 }
00807 }
00808
00809 if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00810 $this->removeFromGroup( 'oxidnotyetordered' );
00811 }
00812 }
00813 }
00814
00822 public function getBasket( $sName )
00823 {
00824 if ( !isset( $this->_aBaskets[$sName] ) ) {
00825 $oBasket = oxNew( 'oxuserbasket' );
00826 $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00827
00828
00829 if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00830 $oBasket->oxuserbaskets__oxtitle = new oxField($sName);
00831 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00832
00833
00834 $oBasket->setIsNewBasket();
00835 }
00836
00837 $this->_aBaskets[$sName] = $oBasket;
00838 }
00839
00840 return $this->_aBaskets[$sName];
00841 }
00842
00851 public function convertBirthday( $aData )
00852 {
00853
00854
00855 $iYear = isset($aData['year'])?((int) $aData['year']):false;
00856 $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00857 $iDay = isset($aData['day'])?((int) $aData['day']):false;
00858
00859
00860 if ( !$iYear && !$iMonth && !$iDay )
00861 return "";
00862
00863
00864 if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00865 $iYear = date('Y');
00866
00867
00868 if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00869 $iMonth = 1;
00870
00871
00872 $iMaxDays = 31;
00873 switch( $iMonth) {
00874 case 2 :
00875 if ($iMaxDays > 28)
00876 $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00877 break;
00878 case 4 :
00879 case 6 :
00880 case 9 :
00881 case 11 :
00882 $iMaxDays = min(30, $iMaxDays);
00883 break;
00884 }
00885
00886
00887 if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00888 $iDay = 1;
00889 }
00890
00891
00892 return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00893 }
00894
00901 public function getBoni()
00902 {
00903 return 1000;
00904 }
00905
00918 public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00919 {
00920
00921 $sDynGoup = strtolower( trim( $sDynGoup ) );
00922
00923
00924 $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
00925
00926
00927 $blAdd = false;
00928
00929
00930 if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
00931 $blAdd = false;
00932 } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
00933
00934 $blAdd = false;
00935 } elseif ( $this->addToGroup( $sDynGoup ) ) {
00936 $blAdd = true;
00937 }
00938
00939
00940 oxSession::deleteVar( 'dgr' );
00941
00942 return $blAdd;
00943 }
00944
00960 public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
00961 {
00962
00963 $this->_checkLogin( $sLogin, $aInvAddress );
00964
00965
00966 $this->_checkEmail( $sLogin );
00967
00968
00969 $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
00970
00971
00972 $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
00973
00974
00975 $this->_checkCountries( $aInvAddress, $aDelAddress );
00976
00977
00978 $this->_checkVatId( $aInvAddress );
00979 }
00980
00989 public function setNewsSubscription( $blSubscribe, $blSendOptIn )
00990 {
00991
00992 $blSuccess = false;
00993 $myConfig = $this->getConfig();
00994
00995
00996 $oNewsSubscription = $this->getNewsSubscription();
00997 if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
00998 if ( !$blSendOptIn ) {
00999
01000
01001 $this->addToGroup( 'oxidnewsletter' );
01002
01003 $oNewsSubscription->setOptInStatus( 1 );
01004 $blSuccess = true;
01005 } else {
01006
01007
01008 $oNewsSubscription->setOptInStatus( 2 );
01009
01010
01011 $oEmail = oxNew( 'oxemail' );
01012 $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01013 }
01014 } elseif ( !$blSubscribe ) {
01015 $this->removeFromGroup( 'oxidnewsletter' );
01016 $oNewsSubscription->setOptInStatus( 0 );
01017 $blSuccess = true;
01018 }
01019
01020 return $blSuccess;
01021 }
01022
01040 public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01041 {
01042
01043
01044 $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01045
01046
01047 $this->assign( $aInvAddress );
01048
01049
01050
01051 $this->_assignAddress( $aDelAddress );
01052
01053
01054 if ( $this->save() ) {
01055
01056
01057 $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01058 $this->_setAutoGroups( $sCountryId );
01059 }
01060 }
01061
01070 public function addUserAddress( $oUser )
01071 {
01072
01073 if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01074 return false;
01075 }
01076
01077 $oAddress = oxNew( 'oxaddress' );
01078
01079 $oAddress->oxaddress__oxuserid = new oxField($this->getId(), oxField::T_RAW);
01080 $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01081 $oAddress->oxaddress__oxfname = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01082 $oAddress->oxaddress__oxlname = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01083 $oAddress->oxaddress__oxstreet = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01084 $oAddress->oxaddress__oxstreetnr = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01085 $oAddress->oxaddress__oxcity = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01086 $oAddress->oxaddress__oxzip = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01087 $oAddress->oxaddress__oxcountry = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01088 $oAddress->oxaddress__oxcountryid = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01089 $oAddress->oxaddress__oxcompany = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01090
01091
01092 if ( $oAddress->save() ) {
01093
01094 $this->_oAddresses = null;
01095 return $oAddress->getId();
01096 }
01097 }
01098
01106 protected function _assignAddress( $aDelAddress )
01107 {
01108 if (isset($aDelAddress) && count($aDelAddress)) {
01109 $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01110 $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ? null : $sAddressId;
01111 $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01112 $oAddress = oxNew( 'oxaddress' );
01113 $oAddress->assign( $aDelAddress );
01114 $oAddress->oxaddress__oxuserid = new oxField( $this->getId(), oxField::T_RAW );
01115 $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01116 $oAddress->save();
01117
01118
01119 $this->_oAddresses = null;
01120
01121
01122 oxSession::setVar( 'deladrid', $oAddress->getId() );
01123 } else {
01124
01125 oxSession::setVar( 'deladrid', null );
01126 }
01127 }
01128
01141 public function login( $sUser, $sPassword, $blCookie = false)
01142 {
01143 if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01144 $oEx = oxNew( 'oxCookieException' );
01145 $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01146 throw $oEx;
01147 }
01148
01149 $myConfig = $this->getConfig();
01150 if ( $sPassword ) {
01151
01152 $sShopID = $myConfig->getShopId();
01153 $oDb = oxDb::getDb();
01154
01155 $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01156 $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01157 $sShopSelect = "";
01158
01159
01160
01161 if ( $this->isAdmin() ) {
01162 $sShopSelect = " and ( oxrights != 'user' ) ";
01163 }
01164
01165 $sWhat = "oxid";
01166
01167 $sSelect = "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01168 if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
01169 if ( $sPassword == "admin" && $sUser == "admin" ) {
01170 $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01171 } else {
01172 $oEx = oxNew( 'oxUserException' );
01173 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01174 throw $oEx;
01175 }
01176 }
01177
01178
01179 $aData = $oDb->getAll( $sSelect );
01180 $sOXID = @$aData[0][0];
01181 if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01182
01183 if ( !$this->load( $sOXID ) ) {
01184 $oEx = oxNew( 'oxUserException' );
01185 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01186 throw $oEx;
01187 }
01188 }
01189 }
01190
01191
01192
01193 if ( $this->oxuser__oxid->value ) {
01194 if ( $this->isAdmin() ) {
01195 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01196 } else {
01197 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01198 }
01199
01200
01201 if ( $blCookie ) {
01202 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01203 }
01204 return true;
01205 } else {
01206 $oEx = oxNew( 'oxUserException' );
01207 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01208 throw $oEx;
01209 }
01210 }
01211
01222 public function openIdLogin( $sUser )
01223 {
01224 $myConfig = $this->getConfig();
01225 $sShopID = $myConfig->getShopId();
01226 $oDb = oxDb::getDb();
01227
01228 $sUserSelect = "oxuser.oxusername = " . $oDb->quote( $sUser );
01229 $sShopSelect = "";
01230
01231
01232 $sSelect = "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01233
01234
01235 $aData = $oDb->getAll( $sSelect );
01236 $sOXID = @$aData[0][0];
01237 if ( isset( $sOXID ) && $sOXID ) {
01238
01239 if ( !$this->load( $sOXID ) ) {
01240 $oEx = oxNew( 'oxUserException' );
01241 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01242 throw $oEx;
01243 }
01244 }
01245
01246
01247 if ( $this->oxuser__oxid->value ) {
01248 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01249 return true;
01250 } else {
01251 $oEx = oxNew( 'oxUserException' );
01252 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01253 throw $oEx;
01254 }
01255 }
01256
01262 public function logout()
01263 {
01264
01265 oxSession::deleteVar( 'usr' );
01266 oxSession::deleteVar( 'auth' );
01267 oxSession::deleteVar( 'dgr' );
01268 oxSession::deleteVar( 'dynvalue' );
01269 oxSession::deleteVar( 'paymentid' );
01270
01271
01272
01273 oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01274
01275
01276 $this->setUser( null );
01277
01278 return true;
01279 }
01280
01287 public function loadAdminUser()
01288 {
01289 return $this->loadActiveUser( true );
01290 }
01291
01300 public function loadActiveUser( $blForceAdmin = false )
01301 {
01302 $myConfig = $this->getConfig();
01303
01304 $blAdmin = $myConfig->isAdmin() || $blForceAdmin;
01305 $oDB = oxDb::getDb();
01306
01307
01308 $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01309 $blFoundInCookie = false;
01310
01311
01312 if ( !$sUserID && !$blAdmin ) {
01313 $sShopID = $myConfig->getShopId();
01314 if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01315 $aData = explode( '@@@', $sSet );
01316 $sUser = $aData[0];
01317 $sPWD = @$aData[1];
01318
01319 $sSelect = 'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and oxuser.oxactive = 1 and oxuser.oxusername = '.$oDB->quote($sUser);
01320
01321
01322 $oDB = oxDb::getDb();
01323 $rs = $oDB->execute( $sSelect );
01324 if ( $rs != false && $rs->recordCount() > 0 ) {
01325 while (!$rs->EOF) {
01326 $sTest = crypt( $rs->fields[1], 'ox' );
01327 if ( $sTest == $sPWD ) {
01328
01329 $sUserID = $rs->fields[0];
01330 $blFoundInCookie = true;
01331 break;
01332 }
01333 $rs->moveNext();
01334 }
01335 }
01336 }
01337 }
01338
01339
01340 if ( $sUserID ) {
01341 if ( $this->load( $sUserID ) ) {
01342
01343 if ($blAdmin) {
01344 oxSession::setVar( 'auth', $sUserID );
01345 } else {
01346 oxSession::setVar( 'usr', $sUserID );
01347 }
01348
01349
01350 $this->_blLoadedFromCookie = $blFoundInCookie;
01351 return true;
01352 }
01353 } else {
01354
01355 oxSession::deleteVar( 'usr' );
01356 oxSession::deleteVar( 'auth' );
01357
01358 return false;
01359 }
01360 }
01361
01374 protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01375 {
01376 include "oxldap.php";
01377 $myConfig = $this->getConfig();
01378 $oDb = oxDb::getDb();
01379
01380 $aLDAPParams = $myConfig->getConfigParam( 'aLDAPParams' );
01381 $oLDAP = new oxLDAP( $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01382
01383 $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote($sUser)." $sShopSelect");
01384 if ( isset( $sLDAPKey) && $sLDAPKey) {
01385 $sUser = $sLDAPKey;
01386 }
01387
01388
01389 $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01390
01391 $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01392 if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01393
01394
01395 $sSelect = "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote($aData['OXUSERNAME'])." $sShopSelect";
01396 $sOXID = $oDb->getOne( $sSelect);
01397
01398 if ( !isset( $sOXID) || !$sOXID) {
01399
01400 $this->setId();
01401
01402
01403 foreach ( $aData as $fldname => $value) {
01404 $sField = "oxuser__".strtolower( $fldname);
01405 $this->$sField->setValue($aData[$fldname]);
01406 }
01407
01408 $this->oxuser__oxactive->setValue(1);
01409 $this->oxuser__oxshopid->setValue($sShopID);
01410 $this->oxuser__oxldapkey->setValue($sUser);
01411 $this->oxuser__oxrights->setValue("user");
01412 $this->setPassword( "ldap user" );
01413
01414 $this->save();
01415 } else {
01416 $this->load( $sOXID);
01417 }
01418
01419 } else {
01420 $oEx = oxNew( 'oxUserException' );
01421 $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01422 throw $oEx;
01423 }
01424 }
01425
01432 protected function _getUserRights()
01433 {
01434
01435 if ( !$this->oxuser__oxrights->value )
01436 return 'user';
01437
01438 $oDB = oxDb::getDb();
01439 $myConfig = $this->getConfig();
01440 $sAuthRights = null;
01441
01442
01443 $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01444 $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01445 if ( $sAuthUserID ) {
01446 $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01447 }
01448
01449
01450 $aRights = array();
01451
01452
01453 if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01454 $aRights[] = $sCurrRights;
01455 }
01456 $aRights[] = 'user';
01457
01458 if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01459 return current( $aRights );
01460 } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01461 $aRights[] = $sAuthRights;
01462 if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01463 return current( $aRights );
01464 }
01465 }
01466
01467
01468 return $this->oxuser__oxrights->value;
01469 }
01470
01480 protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01481 {
01482
01483
01484
01485
01486
01487
01488 return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01489 }
01490
01496 protected function _insert()
01497 {
01498
01499
01500 $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01501
01502 if ( !isset( $this->oxuser__oxboni->value ) ) {
01503 $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01504 }
01505
01506 if ( $blInsert = parent::_insert() ) {
01507
01508 if ( !$this->oxuser__oxcustnr->value || !$this->isAdmin() ) {
01509 $this->_setRecordNumber( 'oxcustnr' );
01510 }
01511 }
01512
01513 return $blInsert;
01514 }
01515
01521 protected function _update()
01522 {
01523
01524 if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01525 $this->_aSkipSaveFields[] = 'oxboni';
01526 }
01527
01528
01529 $this->_aSkipSaveFields[] = 'oxcreate';
01530 if ( !$this->isAdmin() ) {
01531 $this->_aSkipSaveFields[] = 'oxcustnr';
01532 $this->_aSkipSaveFields[] = 'oxrights';
01533 }
01534
01535
01536 if ( ( $blUpdate = parent::_update() ) ) {
01537 $this->getNewsSubscription()->updateSubscription( $this );
01538 }
01539
01540 return $blUpdate;
01541 }
01542
01558 protected function _checkLogin( &$sLogin, $aInvAddress )
01559 {
01560 $myConfig = $this->getConfig();
01561
01562 $sLogin = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername']:$sLogin;
01563
01564
01565
01566 if ( $this->oxuser__oxpassword->value && $sLogin != $this->oxuser__oxusername->value ) {
01567
01568
01569 $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
01570 if ( !$sNewPass ) {
01571
01572
01573 $oEx = oxNew( 'oxInputException' );
01574 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01575 throw $oEx;
01576 } else {
01577
01578
01579 if ( !$this->isSamePassword( $sNewPass ) ) {
01580 $oEx = oxNew( 'oxUserException' );
01581 $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01582 throw $oEx;
01583 }
01584 }
01585 }
01586
01587 if ( $this->checkIfEmailExists( $sLogin ) ) {
01588
01589 $oEx = oxNew( 'oxUserException' );
01590 $oLang = oxLang::getInstance();
01591 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
01592 throw $oEx;
01593 }
01594 }
01595
01603 public function checkIfEmailExists( $sEmail )
01604 {
01605 $myConfig = $this->getConfig();
01606 $oDB = oxDb::getDb();
01607 $iShopId = $myConfig->getShopId();
01608 $blExists = false;
01609
01610 $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01611 if ( ( $sOxid = $this->getId() ) ) {
01612 $sQ .= " and oxid <> '$sOxid' ";
01613 }
01614
01615 $oRs = $oDB->execute( $sQ );
01616 if ( $oRs != false && $oRs->recordCount() > 0 ) {
01617
01618 if ( $this->_blMallUsers ) {
01619
01620 $blExists = true;
01621 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01622
01623
01624 $blExists = false;
01625 }
01626 } else {
01627
01628 $blExists = false;
01629 while ( !$oRs->EOF ) {
01630 if ( $oRs->fields[1] != 'user' ) {
01631
01632
01633 $blExists = true;
01634 break;
01635 } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01636
01637
01638 $blExists = true;
01639 break;
01640 }
01641
01642 $oRs->moveNext();
01643 }
01644 }
01645 }
01646 return $blExists;
01647 }
01648
01656 public function getUserRecommLists( $sOXID = null )
01657 {
01658 if ( !$sOXID )
01659 $sOXID = $this->getId();
01660
01661
01662 $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01663 $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01664
01665
01666 $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01667 $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01668
01669
01670 $oRecommList = oxNew( 'oxlist' );
01671 $oRecommList->init( 'oxrecommlist' );
01672 $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01673 $iShopId = $this->getConfig()->getShopId();
01674 $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01675 $oRecommList->selectString( $sSelect );
01676
01677 return $oRecommList;
01678 }
01679
01687 public function getRecommListsCount( $sOx = null )
01688 {
01689 if ( !$sOx ) {
01690 $sOx = $this->getId();
01691 }
01692
01693 if ( $this->_iCntRecommLists === null || $sOx ) {
01694 $oDb = oxDb::getDb();
01695 $this->_iCntRecommLists = 0;
01696 $iShopId = $this->getConfig()->getShopId();
01697 $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01698 $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01699 }
01700 return $this->_iCntRecommLists;
01701 }
01702
01711 protected function _checkEmail( $sEmail )
01712 {
01713
01714 if ( !$sEmail ) {
01715 $oEx = oxNew( 'oxInputException' );
01716 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01717 throw $oEx;
01718 }
01719
01720
01721 if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
01722 $oEx = oxNew( 'oxInputException' );
01723 $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
01724 throw $oEx;
01725 }
01726 }
01727
01742 protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01743 {
01744 $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01745 }
01746
01759 public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01760 {
01761
01762 if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
01763 $oEx = oxNew( 'oxInputException' );
01764 $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
01765 throw $oEx;
01766 }
01767
01768
01769 if ( $blCheckLenght && getStr()->strlen( $sNewPass ) < 6 ) {
01770 $oEx = oxNew( 'oxInputException' );
01771 $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
01772 throw $oEx;
01773 }
01774
01775
01776 if ( $sNewPass != $sConfPass ) {
01777 $oEx = oxNew( 'oxUserException' );
01778 $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01779 throw $oEx;
01780 }
01781 }
01782
01791 protected function _checkCountries( $aInvAddress, $aDelAddress )
01792 {
01793 $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
01794 $sDelCtry = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
01795
01796 if ( $sBillCtry || $sDelCtry ) {
01797 $oDb = oxDb::getDb();
01798
01799 if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
01800 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
01801 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
01802 } else {
01803 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
01804 ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
01805 }
01806
01807 if ( !$oDb->getOne( $sQ ) ) {
01808 $oEx = oxNew( 'oxUserException' );
01809 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS' );
01810 throw $oEx;
01811 }
01812 }
01813 }
01814
01826 protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01827 {
01828
01829 $aMustFields = array( 'oxuser__oxfname',
01830 'oxuser__oxlname',
01831 'oxuser__oxstreetnr',
01832 'oxuser__oxstreet',
01833 'oxuser__oxzip',
01834 'oxuser__oxcity' );
01835
01836
01837 $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
01838 if ( is_array( $aMustFillFields ) ) {
01839 $aMustFields = $aMustFillFields;
01840 }
01841
01842
01843 $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
01844 $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
01845
01846
01847 $aFields = array_merge( $aInvAddress, $aDelAddress );
01848
01849
01850
01851 $blCheckDel = false;
01852 if ( count( $aDelAddress ) ) {
01853 $blCheckDel = true;
01854 }
01855
01856
01857 foreach ( $aMustFields as $sMustField ) {
01858
01859
01860 if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
01861 continue;
01862 }
01863
01864 if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
01865 $this->_checkRequiredArrayFields( $sMustField, $aFields[$sMustField] );
01866 } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
01867 $oEx = oxNew( 'oxInputException' );
01868 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01869 throw $oEx;
01870 }
01871 }
01872 }
01873
01884 protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01885 {
01886 foreach ( $aFieldValues as $sValue ) {
01887 if ( !trim( $sValue ) ) {
01888 $oEx = oxNew( 'oxInputException' );
01889 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01890 throw $oEx;
01891 }
01892 }
01893 }
01894
01905 protected function _checkVatId( $aInvAddress )
01906 {
01907
01908 if ( $aInvAddress['oxuser__oxustid'] && $aInvAddress['oxuser__oxcompany'] ) {
01909
01910 if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
01911
01912 return;
01913 }
01914 $oCountry = oxNew('oxcountry');
01915 if (!$oCountry->load($sCountryId)) {
01916 throw new oxObjectException();
01917 }
01918 if ($oCountry->isForeignCountry() && $oCountry->isInEU()) {
01919 if (strncmp($aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2)) {
01920 $oEx = oxNew( 'oxInputException' );
01921 $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
01922 throw $oEx;
01923 }
01924 }
01925
01926 }
01927 }
01928
01937 protected function _setAutoGroups( $sCountryId )
01938 {
01939
01940 $blForeigner = true;
01941 $blForeignGroupExists = false;
01942 $blInlandGroupExists = false;
01943
01944 $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01945
01946 if ( is_array($aHomeCountry)) {
01947 if (in_array($sCountryId, $aHomeCountry)) {
01948 $blForeigner = false;
01949 }
01950 } elseif ($sCountryId == $aHomeCountry) {
01951 $blForeigner = false;
01952 }
01953
01954 if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01955 $blForeignGroupExists = true;
01956 if ( !$blForeigner ) {
01957 $this->removeFromGroup( 'oxidforeigncustomer' );
01958 }
01959 }
01960
01961 if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01962 $blInlandGroupExists = true;
01963 if ( $blForeigner ) {
01964 $this->removeFromGroup( 'oxidnewcustomer' );
01965 }
01966 }
01967
01968 if ( !$this->oxuser__oxdisableautogrp->value ) {
01969 if ( !$blForeignGroupExists && $blForeigner ) {
01970 $this->addToGroup( 'oxidforeigncustomer' );
01971 }
01972 if ( !$blInlandGroupExists && !$blForeigner ) {
01973 $this->addToGroup( 'oxidnewcustomer' );
01974 }
01975 }
01976 }
01977
01985 protected function _hasUserAddress( $sUserId )
01986 {
01987 $oAddresses = $this->getUserAddresses();
01988 if ( $oAddresses && count($oAddresses)>0 ) {
01989 $oAddresses->rewind() ;
01990 foreach ($oAddresses as $key => $oAddress) {
01991 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01992 return true;
01993 }
01994 }
01995 }
01996 return false;
01997 }
01998
02011 protected function _setUserCookie( $sUser, $sPassword, $sShopId = null, $iTimeout = 31536000 )
02012 {
02013 oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
02014 }
02015
02025 protected function _deleteUserCookie( $sShopId = null )
02026 {
02027 oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
02028 }
02029
02039 protected static function _getUserCookie( $sShopId = null )
02040 {
02041 return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
02042 }
02043
02044
02053 public function loadUserByUpdateId( $sUid )
02054 {
02055 $oDb = oxDb::getDb();
02056 $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02057 if ( $sUserId = $oDb->getOne( $sQ ) ) {
02058 return $this->load( $sUserId );
02059 }
02060 }
02061
02069 public function setUpdateKey( $blReset = false )
02070 {
02071 $sUpKey = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02072 $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02073
02074
02075 $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02076
02077
02078 $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02079
02080
02081 $this->save();
02082 }
02083
02089 public function getUpdateLinkTerm()
02090 {
02091 return 3600 * 6;
02092 }
02093
02101 public function isExpiredUpdateId( $sKey )
02102 {
02103 $oDb = oxDb::getDb();
02104 $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02105 return !( (bool) $oDb->getOne( $sQ ) );
02106 }
02107
02113 public function getUpdateId()
02114 {
02115 if ( $this->_sUpdateKey === null ) {
02116 $this->setUpdateKey();
02117 $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02118 }
02119 return $this->_sUpdateKey;
02120 }
02121
02130 public function encodePassword( $sPassword, $sSalt )
02131 {
02132 $oDb = oxDb::getDb();
02133 return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02134 }
02135
02143 public function prepareSalt( $sSalt )
02144 {
02145 return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02146 }
02147
02155 public function decodeSalt( $sSaltHex )
02156 {
02157 return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02158 }
02159
02167 public function setPassword( $sPassword = null )
02168 {
02169
02170 $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02171
02172
02173 $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02174
02175 $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02176 $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02177 }
02178
02186 public function isSamePassword( $sNewPass )
02187 {
02188 return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02189 }
02190
02196 public function isLoadedFromCookie()
02197 {
02198 return $this->_blLoadedFromCookie;
02199 }
02200
02207 public function getPasswordHash()
02208 {
02209 $sHash = null;
02210 if ( $this->oxuser__oxpassword->value ) {
02211 if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02212
02213 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02214 } elseif ( ( strlen( $this->oxuser__oxpassword->value ) < 32 ) && ( strpos( $this->oxuser__oxpassword->value, 'openid_' ) !== 0 ) ) {
02215
02216 $this->setPassword( $this->oxuser__oxpassword->value );
02217 }
02218 $sHash = $this->oxuser__oxpassword->value;
02219 }
02220 return $sHash;
02221 }
02222
02231 public static function getAdminUser()
02232 {
02233 return self::getActiveUser( true );
02234 }
02235
02246 public static function getActiveUser( $blForceAdmin = false )
02247 {
02248 $oUser = oxNew( 'oxuser' );
02249 if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02250 return $oUser;
02251 } else {
02252 return false;
02253 }
02254 }
02255
02263 public function getOpenIdPassword( $iLength = 25 )
02264 {
02265 $sPassword= "openid_".substr( oxUtilsObject::getInstance()->generateUId(), 0, $iLength);
02266 return $sPassword;
02267 }
02268
02276 public function getReviewUserHash( $sUserId )
02277 {
02278 $oDb = oxDb::getDb();
02279 $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02280 return $sReviewUserHash;
02281 }
02282
02290 public function getReviewUserId( $sReviewUserHash )
02291 {
02292 $oDb = oxDb::getDb();
02293 $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02294 return $sUserId;
02295 }
02296
02297 }