oxuser.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxUser extends oxBase
00009 {
00014     protected $_blDisableShopCheck = true;
00015 
00020     protected $_oNewsSubscription = null;
00021 
00026     protected $_sCoreTbl = 'oxuser';
00027 
00032     protected $_sClassName = 'oxuser';
00033 
00039     protected $_aBaskets = array();
00040 
00046     protected $_oGroups;
00047 
00053     protected $_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     public function __construct()
00124     {
00125         $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00126 
00127         parent::__construct();
00128         $this->init( 'oxuser' );
00129     }
00130 
00138     public function setMallUsersStatus( $blOn = false )
00139     {
00140         $this->_blMallUsers = $blOn;
00141     }
00142 
00150     public function __get( $sParamName )
00151     {
00152         // it saves memory using - loads data only if it is used
00153         switch ( $sParamName ) {
00154             case 'oGroups':
00155                 return $this->_oGroups = $this->getUserGroups();
00156                 break;
00157             case 'iCntNoticeListArticles':
00158                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00159                 break;
00160             case 'iCntWishListArticles':
00161                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00162                 break;
00163             case 'iCntRecommLists':
00164                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00165                 break;
00166             case 'oAddresses':
00167                 return $this->getUserAddresses();
00168                 break;
00169             case 'oPayments':
00170                 return $this->_oPayments = $this->getUserPayments();
00171                 break;
00172             case 'oxuser__oxcountry':
00173                 return $this->oxuser__oxcountry = $this->getUserCountry();
00174                 break;
00175             case 'sDBOptin':
00176                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00177                 break;
00178             case 'sEmailFailed':
00179                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00180                 break;
00181         }
00182     }
00183 
00189     public function getNewsSubscription()
00190     {
00191         if ( $this->_oNewsSubscription !== null ) {
00192             return $this->_oNewsSubscription;
00193         }
00194 
00195         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00196 
00197         // if subscription object is not set yet - we should create one
00198         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00199             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00200 
00201                 // no subscription defined yet - creating one
00202                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00203                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00204                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00205                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00206                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00207             }
00208         }
00209 
00210         return $this->_oNewsSubscription;
00211     }
00212 
00222     public function getUserCountry( $sCountryId = null, $iLang = null )
00223     {
00224         $oDb = oxDb::getDb();
00225         if ( !$sCountryId ) {
00226             $sCountryId = $this->oxuser__oxcountryid->value;
00227         }
00228 
00229         $sQ = "select oxtitle".oxLang::getInstance()->getLanguageTag( $iLang )." from oxcountry where oxid = " . $oDb->quote( $sCountryId ) . " ";
00230         $this->oxuser__oxcountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00231 
00232         return $this->oxuser__oxcountry;
00233     }
00234 
00242     public function getUserCountryId( $sCountry = null )
00243     {
00244         $oDb = oxDb::getDb();
00245         $sQ = "select oxid from oxcountry where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00246         $sCountryId = $oDb->getOne( $sQ );
00247 
00248         return $sCountryId;
00249     }
00250 
00258     public function getUserGroups( $sOXID = null )
00259     {
00260 
00261         if ( isset( $this->_oGroups ) ) {
00262             return $this->_oGroups;
00263         }
00264 
00265         if ( !$sOXID ) {
00266             $sOXID = $this->getId();
00267         }
00268 
00269         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00270         //$this->oGroups->Init( 'oxbase' );
00271         //$this->oGroups->oLstoTpl->Init( array( 'oxgroups', 'oxobject2group' ) );
00272         $sSelect  = 'select oxgroups.* from oxgroups left join oxobject2group on oxobject2group.oxgroupsid = oxgroups.oxid ';
00273         $sSelect .= 'where oxobject2group.oxobjectid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00274         $this->_oGroups->selectString( $sSelect );
00275         return $this->_oGroups;
00276     }
00277 
00285     public function getUserAddresses( $sUserId = null )
00286     {
00287         $sUserId = isset( $sUserId ) ? $sUserId : $this->getId();
00288         if ( !isset( $this->_aAddresses[$sUserId] ) ) {
00289             $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId ) . "";
00290 
00291             //P
00292             $this->_aAddresses[$sUserId] = oxNew( 'oxlist' );
00293             $this->_aAddresses[$sUserId]->init( "oxaddress" );
00294             $this->_aAddresses[$sUserId]->selectString( $sSelect );
00295 
00296             // marking selected
00297             if ( $sAddressId = $this->getSelectedAddressId() ) {
00298                 foreach ( $this->_aAddresses[$sUserId] as $oAddress ) {
00299                     $oAddress->selected = 0;
00300                     if ( $oAddress->getId() === $sAddressId ) {
00301                         $oAddress->selected = 1;
00302                         break;
00303                     }
00304                 }
00305             }
00306         }
00307         return $this->_aAddresses[$sUserId];
00308     }
00309 
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             // in case none is set - setting first one
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                 // add custom fields to this class
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             //save oxregister value
00424             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00425         }
00426 
00427         // setting user rights
00428         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00429 
00430         // processing birth date which came from output as array
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         //add registered remark
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             // deleting stored payment, address, group dependencies, remarks info
00501             $rs = $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00502             $rs = $oDB->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00503 
00504             // deleting notice/wish lists
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             // deleting newsletter subscription
00509             $rs = $oDB->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00510 
00511             // delivery and delivery sets
00512             $rs = $oDB->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00513 
00514             // discounts
00515             $rs = $oDB->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00516 
00517 
00518             // and leaving all order related information
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         // convert date's to international format
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              // update - set oxid
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         //P
00595         // Lists does not support loading from two tables, so orders
00596         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00597         // forcing to load product info which is used in templates
00598         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00599 
00600         //loading order for registered user
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             //#1546 - shopid check added, if it is not multishop
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         // check if user exists AND there is no password - in this case we update otherwise we try to insert
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         // user without password found - lets use
00707         if ( isset( $sOXID ) && $sOXID ) {
00708             // try to update
00709             $this->delete( $sOXID );
00710         } elseif ( $this->_blMallUsers ) {
00711             // must be sure if there is no dublicate user
00712             $sQ = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00713             if ( $oDB->getOne( $sQ ) ) {
00714                 $oEx = oxNew( 'oxUserException' );
00715                 $oLang = oxLang::getInstance();
00716                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00717                 throw $oEx;
00718             }
00719         }
00720 
00721         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00722         if ( ( $blOK = $this->save() ) ) {
00723             // dropping/cleaning old delivery address/payment info
00724             $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00725             $oDB->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00726         } else {
00727             $oEx = oxNew( 'oxUserException' );
00728             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00729             throw $oEx;
00730         }
00731 
00732         return $blOK;
00733     }
00734 
00742     public function addToGroup( $sGroupID )
00743     {
00744         if ( !$this->inGroup( $sGroupID ) ) {
00745             $oNewGroup = oxNew( 'oxobject2group' );
00746             $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00747             $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00748             if ( $oNewGroup->save() ) {
00749                 $this->_oGroups[$sGroupID] = $oNewGroup;
00750                 return true;
00751             }
00752         }
00753         return false;
00754     }
00755 
00763     public function removeFromGroup( $sGroupID = null )
00764     {
00765         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00766             $oGroups = oxNew( 'oxlist' );
00767             $oGroups->init( 'oxobject2group' );
00768             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00769             $oGroups->selectString( $sSelect );
00770             foreach ( $oGroups as $oRemgroup ) {
00771                 if ( $oRemgroup->delete() ) {
00772                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00773                 }
00774             }
00775         }
00776     }
00777 
00786     public function onOrderExecute( $oBasket, $iSuccess )
00787     {
00788 
00789         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00790             //adding user to particular customer groups
00791             if ( !$this->oxuser__oxdisableautogrp->value ) {
00792 
00793                 $myConfig = $this->getConfig();
00794                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00795                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00796 
00797                 $this->addToGroup( 'oxidcustomer' );
00798                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00799                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00800                     $this->addToGroup( 'oxidsmallcust' );
00801                 }
00802                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00803                     $this->addToGroup( 'oxidmiddlecust' );
00804                 }
00805                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00806                     $this->addToGroup( 'oxidgoodcust' );
00807                 }
00808             }
00809 
00810             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00811                 $this->removeFromGroup( 'oxidnotyetordered' );
00812             }
00813         }
00814     }
00815 
00823     public function getBasket( $sName )
00824     {
00825         if ( !isset( $this->_aBaskets[$sName] ) ) {
00826             $oBasket = oxNew( 'oxuserbasket' );
00827             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00828 
00829             // creating if it does not exist
00830             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00831                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00832                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00833 
00834                 // marking basket as new (it will not be saved in DB yet)
00835                 $oBasket->setIsNewBasket();
00836             }
00837 
00838             $this->_aBaskets[$sName] = $oBasket;
00839         }
00840 
00841         return $this->_aBaskets[$sName];
00842     }
00843 
00852     public function convertBirthday( $aData )
00853     {
00854 
00855         // preparing data to process
00856         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00857         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00858         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00859 
00860         // leaving empty if not set
00861         if ( !$iYear && !$iMonth && !$iDay )
00862             return "";
00863 
00864         // year
00865         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00866             $iYear = date('Y');
00867 
00868         // month
00869         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00870             $iMonth = 1;
00871 
00872         // maximum nuber of days in month
00873         $iMaxDays = 31;
00874         switch( $iMonth) {
00875             case 2 :
00876                 if ($iMaxDays > 28)
00877                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00878                 break;
00879             case 4  :
00880             case 6  :
00881             case 9  :
00882             case 11 :
00883                 $iMaxDays = min(30, $iMaxDays);
00884                 break;
00885         }
00886 
00887         // day
00888         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00889             $iDay = 1;
00890         }
00891 
00892         // whole date
00893         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00894     }
00895 
00902     public function getBoni()
00903     {
00904         return 1000;
00905     }
00906 
00919     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00920     {
00921         // preparing input
00922         $sDynGoup = strtolower( trim( $sDynGoup ) );
00923 
00924         // setting denied groups from admin settings also
00925         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
00926 
00927         // default state ..
00928         $blAdd = false;
00929 
00930         // user assignment to dyn group is not allowed
00931         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
00932             $blAdd = false;
00933         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
00934             // trying to add user to prohibited user group?
00935             $blAdd = false;
00936         } elseif ( $this->addToGroup( $sDynGoup ) ) {
00937             $blAdd = true;
00938         }
00939 
00940         // cleanup
00941         oxSession::deleteVar( 'dgr' );
00942 
00943         return $blAdd;
00944     }
00945 
00961     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
00962     {
00963         // 1. checking user name
00964         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
00965 
00966         // 2. cheking email
00967         $this->_checkEmail( $sLogin );
00968 
00969         // 3. password
00970         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
00971 
00972         // 4. required fields
00973         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
00974 
00975         // 5. country check
00976         $this->_checkCountries( $aInvAddress, $aDelAddress );
00977 
00978         // 6. vat id check.
00979             $this->_checkVatId( $aInvAddress );
00980     }
00981 
00990     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
00991     {
00992         // assigning to newsletter
00993         $blSuccess = false;
00994         $myConfig  = $this->getConfig();
00995 
00996         // user wants to get newsletter messages or no ?
00997         $oNewsSubscription = $this->getNewsSubscription();
00998         if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
00999             if ( !$blSendOptIn ) {
01000 
01001                 // double-opt-in check is disabled - assigning automatically
01002                 $this->addToGroup( 'oxidnewsletter' );
01003                 // and setting subscribed status
01004                 $oNewsSubscription->setOptInStatus( 1 );
01005                 $blSuccess = true;
01006             } else {
01007 
01008                 // double-opt-in check enabled - sending confirmation email and setting waiting status
01009                 $oNewsSubscription->setOptInStatus( 2 );
01010 
01011                 // sending double-opt-in mail
01012                 $oEmail = oxNew( 'oxemail' );
01013                 $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01014             }
01015         } elseif ( !$blSubscribe ) {
01016             // removing user from newsletter subscribers
01017             $this->removeFromGroup( 'oxidnewsletter' );
01018             $oNewsSubscription->setOptInStatus( 0 );
01019             $blSuccess = true;
01020         }
01021 
01022         return $blSuccess;
01023     }
01024 
01042     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01043     {
01044 
01045         // validating values before saving. If validation fails - exception is thrown
01046         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01047 
01048         // input data is fine - lets save updated user info
01049         $this->assign( $aInvAddress );
01050 
01051 
01052         // update old or add new delivery address
01053         $this->_assignAddress( $aDelAddress );
01054 
01055         // saving new values
01056         if ( $this->save() ) {
01057 
01058             // assigning automatically to specific groups
01059             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01060             $this->_setAutoGroups( $sCountryId );
01061         }
01062     }
01063 
01072     public function addUserAddress( $oUser )
01073     {
01074         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01075             return false;
01076         }
01077 
01078         $oAddress = oxNew( 'oxaddress' );
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         // adding new address
01092         if ( $oAddress->save() ) {
01093             // resetting addresses
01094             $this->_aAddresses = 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             // resetting addresses
01119             $this->_aAddresses = null;
01120 
01121             // saving delivery Address for later use
01122             oxSession::setVar( 'deladrid', $oAddress->getId() );
01123         } else {
01124             // resetting
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             // admin view: can only login with higher than 'user' rights
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             // load from DB
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         //login successfull?
01193         if ( $this->oxuser__oxid->value ) {
01194             // yes, successful login
01195             if ( $this->isAdmin() ) {
01196                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01197             } else {
01198                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01199             }
01200 
01201             // cookie must be set ?
01202             if ( $blCookie ) {
01203                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01204             }
01205             return true;
01206         } else {
01207             $oEx = oxNew( 'oxUserException' );
01208             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01209             throw $oEx;
01210         }
01211     }
01212 
01223     public function openIdLogin( $sUser )
01224     {
01225         $myConfig = $this->getConfig();
01226         $sShopID = $myConfig->getShopId();
01227         $oDb = oxDb::getDb();
01228 
01229         $sUserSelect = "oxuser.oxusername = " . $oDb->quote( $sUser );
01230         $sShopSelect = "";
01231 
01232 
01233         $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01234 
01235         // load from DB
01236         $aData = $oDb->getAll( $sSelect );
01237         $sOXID = @$aData[0][0];
01238         if ( isset( $sOXID ) && $sOXID ) {
01239 
01240             if ( !$this->load( $sOXID ) ) {
01241                 $oEx = oxNew( 'oxUserException' );
01242                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01243                 throw $oEx;
01244             }
01245         }
01246 
01247         //login successfull?
01248         if ( $this->oxuser__oxid->value ) {
01249             // yes, successful login
01250             oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01251             return true;
01252         } else {
01253             $oEx = oxNew( 'oxUserException' );
01254             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01255             throw $oEx;
01256         }
01257     }
01258 
01264     public function logout()
01265     {
01266         // deleting session info
01267         oxSession::deleteVar( 'usr' );  // for front end
01268         oxSession::deleteVar( 'auth' ); // for back end
01269         oxSession::deleteVar( 'dgr' );
01270         oxSession::deleteVar( 'dynvalue' );
01271         oxSession::deleteVar( 'paymentid' );
01272         // oxSession::deleteVar( 'deladrid' );
01273 
01274         // delete cookie
01275         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01276 
01277         // unsetting global user
01278         $this->setUser( null );
01279 
01280         return true;
01281     }
01282 
01289     public function loadAdminUser()
01290     {
01291         return $this->loadActiveUser( true );
01292     }
01293 
01302     public function loadActiveUser( $blForceAdmin = false )
01303     {
01304         $myConfig = $this->getConfig();
01305 
01306         $blAdmin = $myConfig->isAdmin() || $blForceAdmin;
01307         $oDB = oxDb::getDb();
01308 
01309         // first - checking session info
01310         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01311         $blFoundInCookie = false;
01312 
01313         //trying automatic login (by 'remember me' cookie)
01314         if ( !$sUserID && !$blAdmin ) {
01315             $sShopID = $myConfig->getShopId();
01316             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01317                 $aData = explode( '@@@', $sSet );
01318                 $sUser = $aData[0];
01319                 $sPWD  = @$aData[1];
01320 
01321                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDB->quote($sUser);
01322 
01323 
01324                 $oDB = oxDb::getDb();
01325                 $rs = $oDB->execute( $sSelect );
01326                 if ( $rs != false && $rs->recordCount() > 0 ) {
01327                     while (!$rs->EOF) {
01328                         $sTest = crypt( $rs->fields[1], 'ox' );
01329                         if ( $sTest == $sPWD ) {
01330                             // found
01331                             $sUserID = $rs->fields[0];
01332                             $blFoundInCookie = true;
01333                             break;
01334                         }
01335                         $rs->moveNext();
01336                     }
01337                 }
01338             }
01339         }
01340 
01341         // checking user results
01342         if ( $sUserID ) {
01343             if ( $this->load( $sUserID ) ) {
01344                 // storing into session
01345                 if ($blAdmin) {
01346                     oxSession::setVar( 'auth', $sUserID );
01347                 } else {
01348                     oxSession::setVar( 'usr', $sUserID );
01349                 }
01350 
01351                 // marking the way user was loaded
01352                 $this->_blLoadedFromCookie = $blFoundInCookie;
01353                 return true;
01354             }
01355         } else {
01356             // no user
01357             oxSession::deleteVar( 'usr' );
01358             oxSession::deleteVar( 'auth' );
01359 
01360             return false;
01361         }
01362     }
01363 
01376     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01377     {
01378         include "oxldap.php";
01379         $myConfig = $this->getConfig();
01380         $oDb = oxDb::getDb();
01381         //$throws oxConnectionException
01382         $aLDAPParams = $myConfig->getConfigParam( 'aLDAPParams' );
01383         $oLDAP = new oxLDAP( $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01384         // maybe this is LDAP user but supplied email Address instead of LDAP login
01385         $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote($sUser)." $sShopSelect");
01386         if ( isset( $sLDAPKey) && $sLDAPKey) {
01387             $sUser = $sLDAPKey;
01388         }
01389 
01390         //$throws oxConnectionException
01391         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01392 
01393         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01394         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01395             // login successful
01396 
01397             // check if user is already in database
01398             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote($aData['OXUSERNAME'])." $sShopSelect";
01399             $sOXID = $oDb->getOne( $sSelect);
01400 
01401             if ( !isset( $sOXID) || !$sOXID) {
01402                 // we need to create a new user
01403                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01404                 $this->setId();
01405 
01406                 // map all user data fields
01407                 foreach ( $aData as $fldname => $value) {
01408                     $sField = "oxuser__".strtolower( $fldname);
01409                     $this->$sField->setValue($aData[$fldname]);
01410                 }
01411 
01412                 $this->oxuser__oxactive->setValue(1);
01413                 $this->oxuser__oxshopid->setValue($sShopID);
01414                 $this->oxuser__oxldapkey->setValue($sUser);
01415                 $this->oxuser__oxrights->setValue("user");
01416                 $this->setPassword( "ldap user" );
01417 
01418                 $this->save();
01419             } else {
01420                 // LDAP user is already in OXID DB, load it
01421                 $this->load( $sOXID);
01422             }
01423 
01424         } else {
01425             $oEx = oxNew( 'oxUserException' );
01426             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01427             throw $oEx;
01428         }
01429     }
01430 
01437     protected function _getUserRights()
01438     {
01439         // previously user had no rights defined
01440         if ( !$this->oxuser__oxrights->value )
01441             return 'user';
01442 
01443         $oDB = oxDb::getDb();
01444         $myConfig    = $this->getConfig();
01445         $sAuthRights = null;
01446 
01447         // choosing possible user rights index
01448         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01449         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01450         if ( $sAuthUserID ) {
01451             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01452         }
01453 
01454         //preventing user rights edit for non admin
01455         $aRights = array();
01456 
01457         // selecting current users rights ...
01458         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01459             $aRights[] = $sCurrRights;
01460         }
01461         $aRights[] = 'user';
01462 
01463         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01464             return current( $aRights );
01465         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01466             $aRights[] = $sAuthRights;
01467             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01468                 return current( $aRights );
01469             }
01470         }
01471 
01472         // leaving as it was set ...
01473         return $this->oxuser__oxrights->value;
01474     }
01475 
01485     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01486     {
01487 
01488         /*if ( !$myConfig->blMallUsers ) {
01489             $sShopID = $myConfig->getShopId();
01490             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01491         }*/
01492 
01493         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01494     }
01495 
01501     protected function _insert()
01502     {
01503 
01504         // set oxcreate date
01505         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01506 
01507         if ( !isset( $this->oxuser__oxboni->value ) ) {
01508             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01509         }
01510 
01511         if ( $blInsert = parent::_insert() ) {
01512             // setting customer number
01513             if ( !$this->oxuser__oxcustnr->value || !$this->isAdmin() ) {
01514                 $this->_setRecordNumber( 'oxcustnr' );
01515             }
01516         }
01517 
01518         return $blInsert;
01519     }
01520 
01526     protected function _update()
01527     {
01528         //V #M418: for not registered users, don't change boni during update
01529         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01530             $this->_aSkipSaveFields[] = 'oxboni';
01531         }
01532 
01533         // don't change this field
01534         $this->_aSkipSaveFields[] = 'oxcreate';
01535         if ( !$this->isAdmin() ) {
01536             $this->_aSkipSaveFields[] = 'oxcustnr';
01537             $this->_aSkipSaveFields[] = 'oxrights';
01538         }
01539 
01540         // updating subscription information
01541         if ( ( $blUpdate = parent::_update() ) ) {
01542             $this->getNewsSubscription()->updateSubscription( $this );
01543         }
01544 
01545         return $blUpdate;
01546     }
01547 
01563     protected function _checkLogin( $sLogin, $aInvAddress )
01564     {
01565         $myConfig = $this->getConfig();
01566 
01567         $sLogin   = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername']:$sLogin;
01568 
01569         // check only for users with password during registration
01570         // if user wants to change user name - we must check if passwords are ok before changing
01571         if ( $this->oxuser__oxpassword->value && $sLogin != $this->oxuser__oxusername->value ) {
01572 
01573             // on this case password must be taken directly from request
01574             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
01575             if ( !$sNewPass ) {
01576 
01577                 // 1. user forgot to enter password
01578                 $oEx = oxNew( 'oxInputException' );
01579                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01580                 throw $oEx;
01581             } else {
01582 
01583                 // 2. entered wrong password
01584                 if ( !$this->isSamePassword( $sNewPass ) ) {
01585                     $oEx = oxNew( 'oxUserException' );
01586                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01587                     throw $oEx;
01588                 }
01589             }
01590         }
01591 
01592         if ( $this->checkIfEmailExists( $sLogin ) ) {
01593             //if exists then we do now allow to do that
01594             $oEx = oxNew( 'oxUserException' );
01595             $oLang = oxLang::getInstance();
01596             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
01597             throw $oEx;
01598         }
01599 
01600         return $sLogin;
01601     }
01602 
01610     public function checkIfEmailExists( $sEmail )
01611     {
01612         $myConfig = $this->getConfig();
01613         $oDB = oxDb::getDb();
01614         $iShopId = $myConfig->getShopId();
01615         $blExists = false;
01616 
01617         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01618         if ( ( $sOxid = $this->getId() ) ) {
01619             $sQ .= " and oxid <> '$sOxid' ";
01620         }
01621 
01622         $oRs = $oDB->execute( $sQ );
01623         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01624 
01625             if ( $this->_blMallUsers ) {
01626 
01627                 $blExists = true;
01628                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01629 
01630                     // password is not set - allow to override
01631                     $blExists = false;
01632                 }
01633             } else {
01634 
01635                 $blExists = false;
01636                 while ( !$oRs->EOF ) {
01637                     if ( $oRs->fields[1] != 'user' ) {
01638 
01639                         // exists admin with same login - must not allow
01640                         $blExists = true;
01641                         break;
01642                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01643 
01644                         // exists same login (with password) in same shop
01645                         $blExists = true;
01646                         break;
01647                     }
01648 
01649                     $oRs->moveNext();
01650                 }
01651             }
01652         }
01653         return $blExists;
01654     }
01655 
01663     public function getUserRecommLists( $sOXID = null )
01664     {
01665         if ( !$sOXID )
01666             $sOXID = $this->getId();
01667 
01668         // sets active page
01669         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01670         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01671 
01672         // load only lists which we show on screen
01673         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01674         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01675 
01676 
01677         $oRecommList = oxNew( 'oxlist' );
01678         $oRecommList->init( 'oxrecommlist' );
01679         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01680         $iShopId = $this->getConfig()->getShopId();
01681         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01682         $oRecommList->selectString( $sSelect );
01683 
01684         return $oRecommList;
01685     }
01686 
01694     public function getRecommListsCount( $sOx = null )
01695     {
01696         if ( !$sOx ) {
01697             $sOx = $this->getId();
01698         }
01699 
01700         if ( $this->_iCntRecommLists === null || $sOx ) {
01701             $oDb = oxDb::getDb();
01702             $this->_iCntRecommLists = 0;
01703             $iShopId = $this->getConfig()->getShopId();
01704             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01705             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01706         }
01707         return $this->_iCntRecommLists;
01708     }
01709 
01718     protected function _checkEmail( $sEmail )
01719     {
01720         // missing email address (user login name) ?
01721         if ( !$sEmail ) {
01722             $oEx = oxNew( 'oxInputException' );
01723             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01724             throw $oEx;
01725         }
01726 
01727         // invalid email address ?
01728         if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
01729             $oEx = oxNew( 'oxInputException' );
01730             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
01731             throw $oEx;
01732         }
01733     }
01734 
01749     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01750     {
01751         $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01752     }
01753 
01766     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01767     {
01768         //  no password at all
01769         if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
01770             $oEx = oxNew( 'oxInputException' );
01771             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
01772             throw $oEx;
01773         }
01774 
01775         //  password is too short ?
01776         if ( $blCheckLenght &&  getStr()->strlen( $sNewPass ) < 6 ) {
01777             $oEx = oxNew( 'oxInputException' );
01778             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
01779             throw $oEx;
01780         }
01781 
01782         //  passwords do not match ?
01783         if ( $sNewPass != $sConfPass ) {
01784             $oEx = oxNew( 'oxUserException' );
01785             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01786             throw $oEx;
01787         }
01788     }
01789 
01800     protected function _checkCountries( $aInvAddress, $aDelAddress )
01801     {
01802         $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
01803         $sDelCtry  = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
01804 
01805         if ( $sBillCtry || $sDelCtry ) {
01806             $oDb = oxDb::getDb();
01807 
01808             if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
01809                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
01810                 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
01811             } else {
01812                 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
01813                               ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
01814             }
01815 
01816             if ( !$oDb->getOne( $sQ ) ) {
01817                 $oEx = oxNew( 'oxUserException' );
01818                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS' );
01819                 throw $oEx;
01820             }
01821         }
01822     }
01823 
01835     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01836     {
01837         // collecting info about required fields
01838         $aMustFields = array( 'oxuser__oxfname',
01839                               'oxuser__oxlname',
01840                               'oxuser__oxstreetnr',
01841                               'oxuser__oxstreet',
01842                               'oxuser__oxzip',
01843                               'oxuser__oxcity' );
01844 
01845         // config shoud override default fields
01846         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
01847         if ( is_array( $aMustFillFields ) ) {
01848             $aMustFields = $aMustFillFields;
01849         }
01850 
01851         // assuring data to check
01852         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
01853         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
01854 
01855         // collecting fields
01856         $aFields = array_merge( $aInvAddress, $aDelAddress );
01857 
01858 
01859         // check delivery address ?
01860         $blCheckDel = false;
01861         if ( count( $aDelAddress ) ) {
01862             $blCheckDel = true;
01863         }
01864 
01865         // checking
01866         foreach ( $aMustFields as $sMustField ) {
01867 
01868             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
01869             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
01870                 continue;
01871             }
01872 
01873             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
01874                 $this->_checkRequiredArrayFields( $sMustField, $aFields[$sMustField] );
01875             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
01876                    $oEx = oxNew( 'oxInputException' );
01877                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01878                    throw $oEx;
01879             }
01880         }
01881     }
01882 
01893     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01894     {
01895         foreach ( $aFieldValues as $sValue ) {
01896             if ( !trim( $sValue ) ) {
01897                 $oEx = oxNew( 'oxInputException' );
01898                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01899                 throw $oEx;
01900             }
01901         }
01902     }
01903 
01914     protected function _checkVatId( $aInvAddress )
01915     {
01916         if ( $aInvAddress['oxuser__oxustid'] ) {
01917 
01918             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
01919                 // no country
01920                 return;
01921             }
01922             $oCountry = oxNew('oxcountry');
01923             if (!$oCountry->load($sCountryId)) {
01924                 throw new oxObjectException();
01925             }
01926             if ($oCountry->isForeignCountry() && $oCountry->isInEU()) {
01927                     if (strncmp($aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2)) {
01928                         $oEx = oxNew( 'oxInputException' );
01929                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
01930                         throw $oEx;
01931                     }
01932             }
01933         }
01934     }
01935 
01944     protected function _setAutoGroups( $sCountryId )
01945     {
01946         // assigning automatically to specific groups
01947         $blForeigner = true;
01948         $blForeignGroupExists = false;
01949         $blInlandGroupExists = false;
01950 
01951         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01952         // foreigner ?
01953         if ( is_array($aHomeCountry)) {
01954             if (in_array($sCountryId, $aHomeCountry)) {
01955                 $blForeigner = false;
01956             }
01957         } elseif ($sCountryId == $aHomeCountry) {
01958             $blForeigner = false;
01959         }
01960 
01961         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01962             $blForeignGroupExists = true;
01963             if ( !$blForeigner ) {
01964                 $this->removeFromGroup( 'oxidforeigncustomer' );
01965             }
01966         }
01967 
01968         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01969             $blInlandGroupExists = true;
01970             if ( $blForeigner ) {
01971                 $this->removeFromGroup( 'oxidnewcustomer' );
01972             }
01973         }
01974 
01975         if ( !$this->oxuser__oxdisableautogrp->value ) {
01976             if ( !$blForeignGroupExists && $blForeigner ) {
01977                 $this->addToGroup( 'oxidforeigncustomer' );
01978             }
01979             if ( !$blInlandGroupExists && !$blForeigner ) {
01980                 $this->addToGroup( 'oxidnewcustomer' );
01981             }
01982         }
01983     }
01984 
01992     protected function _hasUserAddress( $sUserId )
01993     {
01994         $oAddresses = $this->getUserAddresses();
01995         if ( $oAddresses && count($oAddresses)>0 ) {
01996             $oAddresses->rewind() ;
01997             foreach ($oAddresses as $key => $oAddress) {
01998                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
01999                     return true;
02000                 }
02001             }
02002         }
02003         return false;
02004     }
02005 
02018     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
02019     {
02020         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
02021     }
02022 
02032     protected function _deleteUserCookie( $sShopId = null )
02033     {
02034         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
02035     }
02036 
02046     protected static function _getUserCookie( $sShopId = null )
02047     {
02048         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
02049     }
02050 
02051 
02060     public function loadUserByUpdateId( $sUid )
02061     {
02062         $oDb = oxDb::getDb();
02063         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02064         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02065             return $this->load( $sUserId );
02066         }
02067     }
02068 
02076     public function setUpdateKey( $blReset = false )
02077     {
02078         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02079         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02080 
02081         // generating key
02082         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02083 
02084         // setting expiration time for 6 hours
02085         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02086 
02087         // saving
02088         $this->save();
02089     }
02090 
02096     public function getUpdateLinkTerm()
02097     {
02098         return 3600 * 6;
02099     }
02100 
02108     public function isExpiredUpdateId( $sKey )
02109     {
02110         $oDb = oxDb::getDb();
02111         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02112         return !( (bool) $oDb->getOne( $sQ ) );
02113     }
02114 
02120     public function getUpdateId()
02121     {
02122         if ( $this->_sUpdateKey === null ) {
02123             $this->setUpdateKey();
02124             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02125         }
02126         return $this->_sUpdateKey;
02127     }
02128 
02137     public function encodePassword( $sPassword, $sSalt )
02138     {
02139         $oDb = oxDb::getDb();
02140         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02141     }
02142 
02150     public function prepareSalt( $sSalt )
02151     {
02152         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02153     }
02154 
02162     public function decodeSalt( $sSaltHex )
02163     {
02164         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02165     }
02166 
02174     public function setPassword( $sPassword = null )
02175     {
02176         // setting salt if password is not empty
02177         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02178 
02179         // encoding only if password was not empty (e.g. user registration without pass)
02180         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02181 
02182         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02183         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02184     }
02185 
02193     public function isSamePassword( $sNewPass )
02194     {
02195         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02196     }
02197 
02203     public function isLoadedFromCookie()
02204     {
02205         return $this->_blLoadedFromCookie;
02206     }
02207 
02214     public function getPasswordHash()
02215     {
02216         $sHash = null;
02217         if ( $this->oxuser__oxpassword->value ) {
02218             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02219                 // decodable pass ?
02220                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02221             } elseif ( ( strlen( $this->oxuser__oxpassword->value ) < 32 ) && ( strpos( $this->oxuser__oxpassword->value, 'openid_' ) !== 0 ) ) {
02222                 // plain pass ?
02223                 $this->setPassword( $this->oxuser__oxpassword->value );
02224             }
02225             $sHash = $this->oxuser__oxpassword->value;
02226         }
02227         return $sHash;
02228     }
02229 
02238     public static function getAdminUser()
02239     {
02240         return self::getActiveUser( true );
02241     }
02242 
02253     public static function getActiveUser( $blForceAdmin = false )
02254     {
02255         $oUser = oxNew( 'oxuser' );
02256         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02257             return $oUser;
02258         } else {
02259             return false;
02260         }
02261     }
02262 
02270     public function getOpenIdPassword( $iLength = 25 )
02271     {
02272         $sPassword= "openid_".substr( oxUtilsObject::getInstance()->generateUId(), 0, $iLength);
02273         return $sPassword;
02274     }
02275 
02283     public function getReviewUserHash( $sUserId )
02284     {
02285         $oDb = oxDb::getDb();
02286         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02287         return $sReviewUserHash;
02288     }
02289 
02297     public function getReviewUserId( $sReviewUserHash )
02298     {
02299         $oDb = oxDb::getDb();
02300         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02301         return $sUserId;
02302     }
02303 
02309     public function getState()
02310     {
02311         return $this->oxuser__oxstateid->value;
02312     }
02313 
02314 }

Generated by  doxygen 1.6.2