oxuser.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxUser extends oxBase
00009 {
00014     protected $_blDisableShopCheck = true;
00015 
00020     protected $_oNewsSubscription = null;
00021 
00026     protected $_sCoreTbl = 'oxuser';
00027 
00032     protected $_sClassName = 'oxuser';
00033 
00039     protected $_aBaskets = array();
00040 
00046     protected $_oGroups;
00047 
00053     protected $_oAddresses;
00054 
00060     protected $_oPayments;
00061 
00067     protected $_oRecommList;
00068 
00074     protected $_blMallUsers = false;
00075 
00081     protected static $_aUserCookie = array();
00082 
00088     protected $_iCntNoticeListArticles = null;
00089 
00095     protected $_iCntWishListArticles = null;
00096 
00102     protected $_iCntRecommLists = null;
00103 
00109      protected $_sUpdateKey = null;
00110 
00116      protected $_blLoadedFromCookie  = null;
00117 
00123     public function __construct()
00124     {
00125         $this->setMallUsersStatus( $this->getConfig()->getConfigParam( 'blMallUsers' ) );
00126 
00127         parent::__construct();
00128         $this->init( 'oxuser' );
00129     }
00130 
00138     public function setMallUsersStatus( $blOn = false )
00139     {
00140         $this->_blMallUsers = $blOn;
00141     }
00142 
00150     public function __get( $sParamName )
00151     {
00152         // it saves memory using - loads data only if it is used
00153         switch ( $sParamName ) {
00154             case 'oGroups':
00155                 return $this->_oGroups = $this->getUserGroups();
00156                 break;
00157             case 'iCntNoticeListArticles':
00158                 return $this->_iCntNoticeListArticles = $this->getNoticeListArtCnt();
00159                 break;
00160             case 'iCntWishListArticles':
00161                 return $this->_iCntWishListArticles = $this->getWishListArtCnt();
00162                 break;
00163             case 'iCntRecommLists':
00164                 return $this->_iCntRecommLists = $this->getRecommListsCount();
00165                 break;
00166             case 'oAddresses':
00167                 return $this->_oAddresses = $this->getUserAddresses();
00168                 break;
00169             case 'oPayments':
00170                 return $this->_oPayments = $this->getUserPayments();
00171                 break;
00172             case 'oxuser__oxcountry':
00173                 return $this->oxuser__oxcountry = $this->getUserCountry();
00174                 break;
00175             case 'sDBOptin':
00176                 return $this->sDBOptin = $this->getNewsSubscription()->getOptInStatus();
00177                 break;
00178             case 'sEmailFailed':
00179                 return $this->sEmailFailed = $this->getNewsSubscription()->getOptInEmailStatus();
00180                 break;
00181         }
00182     }
00183 
00189     public function getNewsSubscription()
00190     {
00191         if ( $this->_oNewsSubscription !== null ) {
00192             return $this->_oNewsSubscription;
00193         }
00194 
00195         $this->_oNewsSubscription = oxNew( 'oxnewssubscribed' );
00196 
00197         // if subscription object is not set yet - we should create one
00198         if ( !$this->_oNewsSubscription->loadFromUserId( $this->getId() ) ) {
00199             if ( !$this->_oNewsSubscription->loadFromEmail( $this->oxuser__oxusername->value ) ) {
00200 
00201                 // no subscription defined yet - creating one
00202                 $this->_oNewsSubscription->oxnewssubscribed__oxuserid = new oxField($this->getId(), oxField::T_RAW);
00203                 $this->_oNewsSubscription->oxnewssubscribed__oxemail  = new oxField($this->oxuser__oxusername->value, oxField::T_RAW);
00204                 $this->_oNewsSubscription->oxnewssubscribed__oxsal    = new oxField($this->oxuser__oxsal->value, oxField::T_RAW);
00205                 $this->_oNewsSubscription->oxnewssubscribed__oxfname  = new oxField($this->oxuser__oxfname->value, oxField::T_RAW);
00206                 $this->_oNewsSubscription->oxnewssubscribed__oxlname  = new oxField($this->oxuser__oxlname->value, oxField::T_RAW);
00207             }
00208         }
00209 
00210         return $this->_oNewsSubscription;
00211     }
00212 
00222     public function getUserCountry( $sCountryId = null, $iLang = null )
00223     {
00224         $oDb = oxDb::getDb();
00225         if ( !$sCountryId ) {
00226             $sCountryId = $this->oxuser__oxcountryid->value;
00227         }
00228 
00229         $sQ = "select oxtitle".oxLang::getInstance()->getLanguageTag( $iLang )." from oxcountry where oxid = " . $oDb->quote( $sCountryId ) . " ";
00230         $this->oxuser__oxcountry = new oxField( $oDb->getOne( $sQ ), oxField::T_RAW);
00231 
00232         return $this->oxuser__oxcountry;
00233     }
00234 
00242     public function getUserCountryId( $sCountry = null )
00243     {
00244         $oDb = oxDb::getDb();
00245         $sQ = "select oxid from oxcountry where oxactive = '1' and oxisoalpha2 = " . $oDb->quote( $sCountry ) . " ";
00246         $sCountryId = $oDb->getOne( $sQ );
00247 
00248         return $sCountryId;
00249     }
00250 
00258     public function getUserGroups( $sOXID = null )
00259     {
00260 
00261         if ( isset( $this->_oGroups ) ) {
00262             return $this->_oGroups;
00263         }
00264 
00265         if ( !$sOXID ) {
00266             $sOXID = $this->getId();
00267         }
00268 
00269         $this->_oGroups = oxNew( 'oxlist', 'oxgroups' );
00270         //$this->oGroups->Init( 'oxbase' );
00271         //$this->oGroups->oLstoTpl->Init( array( 'oxgroups', 'oxobject2group' ) );
00272         $sSelect  = 'select oxgroups.* from oxgroups left join oxobject2group on oxobject2group.oxgroupsid = oxgroups.oxid ';
00273         $sSelect .= 'where oxobject2group.oxobjectid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00274         $this->_oGroups->selectString( $sSelect );
00275         return $this->_oGroups;
00276     }
00277 
00285     public function getUserAddresses( $sUserId = null )
00286     {
00287 
00288         if ( $this->_oAddresses == null ) {
00289 
00290             $sUserId = ( $sUserId ) ? $sUserId : $this->getId();
00291             $sSelect = "select * from oxaddress where oxaddress.oxuserid = " . oxDb::getDb()->quote( $sUserId ) . "";
00292 
00293             //P
00294             $this->_oAddresses = oxNew( 'oxlist' );
00295             $this->_oAddresses->init( "oxaddress" );
00296             $this->_oAddresses->selectString( $sSelect );
00297 
00298             // marking selected
00299             if ( $sAddressId = $this->getSelectedAddressId() ) {
00300                 foreach ( $this->_oAddresses as $oAddress ) {
00301                     $oAddress->selected = 0;
00302                     if ( $oAddress->getId() === $sAddressId ) {
00303                         $oAddress->selected = 1;
00304                         break;
00305                     }
00306                 }
00307             }
00308         }
00309         return $this->_oAddresses;
00310     }
00311 
00319     public function setSelectedAddressId( $sAddressId )
00320     {
00321         $this->_sSelAddressId = $sAddressId;
00322     }
00323 
00329     public function getSelectedAddressId()
00330     {
00331         if ( $this->_sSelAddressId !== null ) {
00332             return $this->_sSelAddressId;
00333         }
00334 
00335         $sAddressId = oxConfig::getParameter( "oxaddressid");
00336         if ( !$sAddressId && !oxConfig::getParameter( 'reloadaddress' ) ) {
00337             $sAddressId = oxSession::getVar( "deladrid" );
00338         }
00339         return $sAddressId;
00340     }
00341 
00350     public function getSelectedAddress( $sWishId = false )
00351     {
00352         $oAddresses = $this->getUserAddresses();
00353         if ( $oAddresses->count() ) {
00354             if ( $sAddressId = $this->getSelectedAddressId() ) {
00355                 foreach ( $oAddresses as $oAddress ) {
00356                     if ( $oAddress->selected == 1 ) {
00357                         $sAddressId = $oAddress->getId();
00358                         break;
00359                     }
00360                 }
00361             } elseif ( $sWishId ) {
00362                 foreach ( $oAddresses as $oAddress ) {
00363                     $oAddress->selected = 0;
00364                     if ( $oAddress->oxaddress__oxaddressuserid->value == $sWishId ) {
00365                         $oAddress->selected = 1;
00366                         $sAddressId = $oAddress->getId();
00367                     }
00368                 }
00369             }
00370 
00371             // in case none is set - setting first one
00372             if ( !$sAddressId ) {
00373                 $oAddresses->rewind();
00374                 $oAddress = $oAddresses->current();
00375                 $oAddress->selected = 1;
00376                 $sAddressId = $oAddress->getId();
00377             }
00378         }
00379 
00380         return $sAddressId;
00381     }
00382 
00390     public function getUserPayments( $sOXID = null )
00391     {
00392         if ( $this->_oPayments === null ) {
00393 
00394             if ( !$sOXID ) {
00395                 $sOXID = $this->getId();
00396             }
00397 
00398             $sSelect = 'select * from oxuserpayments where oxuserid = ' . oxDb::getDb()->quote( $sOXID ) . ' ';
00399 
00400             $this->_oPayments = oxNew( 'oxlist' );
00401             $this->_oPayments->init( 'oxUserPayment' );
00402             $this->_oPayments->selectString( $sSelect );
00403 
00404             $myUtils = oxUtils::getInstance();
00405             foreach ( $this->_oPayments as $oPayment ) {
00406                 // add custom fields to this class
00407                 $oPayment = $myUtils->assignValuesFromText( $val->oxuserpayments__oxvalue->value );
00408             }
00409         }
00410 
00411         return $this->_oPayments;
00412     }
00413 
00419     public function save()
00420     {
00421 
00422         $blAddRemark = false;
00423         if ( $this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1 ) {
00424             $blAddRemark = true;
00425             //save oxregister value
00426             $this->oxuser__oxregister = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
00427         }
00428 
00429         // setting user rights
00430         $this->oxuser__oxrights = new oxField($this->_getUserRights(), oxField::T_RAW);
00431 
00432         // processing birth date which came from output as array
00433         if ( is_array( $this->oxuser__oxbirthdate->value ) ) {
00434             $this->oxuser__oxbirthdate = new oxField($this->convertBirthday( $this->oxuser__oxbirthdate->value ), oxField::T_RAW);
00435         }
00436 
00437         $blRet = parent::save();
00438 
00439         //add registered remark
00440         if ( $blAddRemark && $blRet ) {
00441             $oRemark = oxNew( 'oxremark' );
00442             $oRemark->oxremark__oxtext     = new oxField(oxLang::getInstance()->translateString( 'usrRegistered' ), oxField::T_RAW);
00443             $oRemark->oxremark__oxtype     = new oxField('r', oxField::T_RAW);
00444             $oRemark->oxremark__oxparentid = new oxField($this->getId(), oxField::T_RAW);
00445             $oRemark->save();
00446         }
00447 
00448         return $blRet;
00449     }
00450 
00456     public function allowDerivedUpdate()
00457     {
00458         return true;
00459     }
00460 
00468     public function inGroup( $sGroupID )
00469     {
00470         $blIn = false;
00471         if ( ( $oGroups = $this->getUserGroups() ) ) {
00472             $blIn = isset( $oGroups[ $sGroupID ] );
00473         }
00474 
00475         return $blIn;
00476     }
00477 
00486     public function delete( $sOXID = null )
00487     {
00488 
00489         if ( !$sOXID ) {
00490             $sOXID = $this->getId();
00491         }
00492         if ( !$sOXID ) {
00493             return false;
00494         }
00495 
00496         $blDeleted = parent::delete( $sOXID );
00497 
00498         if ( $blDeleted ) {
00499             $oDB = oxDb::getDb();
00500             $sOXIDQuoted = $oDB->quote($sOXID);
00501 
00502             // deleting stored payment, address, group dependencies, remarks info
00503             $rs = $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = {$sOXIDQuoted}" );
00504             $rs = $oDB->execute( "delete from oxobject2group where oxobject2group.oxobjectid = {$sOXIDQuoted}" );
00505 
00506             // deleting notice/wish lists
00507             $rs = $oDB->execute( "delete oxuserbasketitems.* from oxuserbasketitems, oxuserbaskets where oxuserbasketitems.oxbasketid = oxuserbaskets.oxid and oxuserid = {$sOXIDQuoted}" );
00508             $rs = $oDB->execute( "delete from oxuserbaskets where oxuserid = {$sOXIDQuoted}" );
00509 
00510             // deleting newsletter subscription
00511             $rs = $oDB->execute( "delete from oxnewssubscribed where oxuserid = {$sOXIDQuoted}" );
00512 
00513             // delivery and delivery sets
00514             $rs = $oDB->execute( "delete from oxobject2delivery where oxobjectid = {$sOXIDQuoted}");
00515 
00516             // discounts
00517             $rs = $oDB->execute( "delete from oxobject2discount where oxobjectid = {$sOXIDQuoted}");
00518 
00519 
00520             // and leaving all order related information
00521             $rs = $oDB->execute( "delete from oxremark where oxparentid = {$sOXIDQuoted} and oxtype !='o'" );
00522 
00523             $blDeleted = $rs->EOF;
00524         }
00525 
00526         return $blDeleted;
00527     }
00528 
00536     public function load( $oxID )
00537     {
00538 
00539         $blRet = parent::Load( $oxID );
00540 
00541         // convert date's to international format
00542         if ( isset( $this->oxuser__oxcreate->value ) ) {
00543             $this->oxuser__oxcreate->setValue(oxUtilsDate::getInstance()->formatDBDate( $this->oxuser__oxcreate->value ));
00544         }
00545 
00546         return $blRet;
00547     }
00548 
00556     public function exists( $sOXID = null )
00557     {
00558         $oDb = oxDb::getDb();
00559         if ( !$sOXID ) {
00560             $sOXID = $this->getId();
00561         }
00562 
00563         $sSelect = 'SELECT oxid FROM '.$this->getViewName().'
00564                     WHERE ( oxusername = '.$oDb->quote( $this->oxuser__oxusername->value).'';
00565 
00566         if ( $sOXID ) {
00567             $sSelect.= " or oxid = ".$oDb->quote( $sOXID ) . " ) ";
00568         } else {
00569             $sSelect.= ' ) ';
00570         }
00571 
00572         if ( !$this->_blMallUsers && $this->oxuser__oxrights->value != 'malladmin') {
00573             $sSelect .= ' AND oxshopid = "'.$this->getConfig()->getShopId().'" ';
00574         }
00575 
00576         $blExists = false;
00577         if ( ( $sOxid = oxDb::getDb()->getOne( $sSelect ) ) ) {
00578              // update - set oxid
00579             $this->setId( $sOxid );
00580             $blExists = true;
00581         }
00582         return $blExists;
00583     }
00584 
00590     public function getOrders()
00591     {
00592         $myConfig = $this->getConfig();
00593         $oOrders = oxNew( 'oxlist' );
00594         $oOrders->init( 'oxorder' );
00595 
00596         //P
00597         // Lists does not support loading from two tables, so orders
00598         // articles now are loaded in account_order.php view and no need to use blLoadProdInfo
00599         // forcing to load product info which is used in templates
00600         // $oOrders->aSetBeforeAssign['blLoadProdInfo'] = true;
00601 
00602         //loading order for registered user
00603         if ( $this->oxuser__oxregister->value > 1 ) {
00604             $sQ = 'select * from oxorder where oxuserid = "'.$this->getId().'" and oxorderdate >= ' . oxDb::getDb()->quote( $this->oxuser__oxregister->value ) . ' ';
00605 
00606             //#1546 - shopid check added, if it is not multishop
00607 
00608             $sQ .= ' order by oxorderdate desc ';
00609             $oOrders->selectString( $sQ );
00610         }
00611 
00612         return $oOrders;
00613     }
00614 
00620     public function getOrderCount()
00621     {
00622         $iCnt = 0;
00623         if ( $this->getId() && $this->oxuser__oxregister->value > 1 ) {
00624             $oDb = oxDb::getDb();
00625             $sQ  = 'select count(*) from oxorder where oxuserid = "'.$this->getId().'" AND oxorderdate >= ' . $oDb->quote( $this->oxuser__oxregister->value) . ' and oxshopid = "'.$this->getConfig()->getShopId().'" ';
00626             $iCnt = (int) $oDb->getOne( $sQ );
00627         }
00628 
00629         return $iCnt;
00630     }
00631 
00637     public function getNoticeListArtCnt()
00638     {
00639         if ( $this->_iCntNoticeListArticles === null ) {
00640             $this->_iCntNoticeListArticles = 0;
00641             if ( $this->getId() ) {
00642                 $this->_iCntNoticeListArticles = $this->getBasket( 'noticelist' )->getItemCount();
00643             }
00644         }
00645         return $this->_iCntNoticeListArticles;
00646     }
00647 
00653     public function getWishListArtCnt()
00654     {
00655         if ( $this->_iCntWishListArticles === null ) {
00656             $this->_iCntWishListArticles = false;
00657             if ( $this->getId() ) {
00658                 $this->_iCntWishListArticles = $this->getBasket( 'wishlist' )->getItemCount();
00659             }
00660         }
00661         return $this->_iCntWishListArticles;
00662     }
00663 
00670     public function getActiveCountry()
00671     {
00672         $sDeliveryCountry = '';
00673         if ( $soxAddressId = oxConfig::getParameter( 'deladrid' ) ) {
00674             $oDelAddress = oxNew( 'oxaddress' );
00675             $oDelAddress->load( $soxAddressId );
00676             $sDeliveryCountry = $oDelAddress->oxaddress__oxcountryid->value;
00677         } elseif ( $this->getId() ) {
00678             $sDeliveryCountry = $this->oxuser__oxcountryid->value;
00679         } else {
00680             $oUser = oxNew( 'oxuser' );
00681             if ( $oUser->loadActiveUser() ) {
00682                 $sDeliveryCountry = $oUser->oxuser__oxcountryid->value;
00683             }
00684         }
00685 
00686         return $sDeliveryCountry;
00687     }
00688 
00696     public function createUser()
00697     {
00698         $oDB = oxDb::getDb();
00699         $sShopID = $this->getConfig()->getShopId();
00700 
00701         // check if user exists AND there is no password - in this case we update otherwise we try to insert
00702         $sSelect = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxpassword = '' ";
00703         if ( !$this->_blMallUsers ) {
00704             $sSelect .= " and oxshopid = '{$sShopID}' ";
00705         }
00706         $sOXID = $oDB->getOne( $sSelect );
00707 
00708         // user without password found - lets use
00709         if ( isset( $sOXID ) && $sOXID ) {
00710             // try to update
00711             $this->delete( $sOXID );
00712         } elseif ( $this->_blMallUsers ) {
00713             // must be sure if there is no dublicate user
00714             $sQ = "select oxid from oxuser where oxusername = " . $oDB->quote( $this->oxuser__oxusername->value ) . " and oxusername != '' ";
00715             if ( $oDB->getOne( $sQ ) ) {
00716                 $oEx = oxNew( 'oxUserException' );
00717                 $oLang = oxLang::getInstance();
00718                 $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $this->oxuser__oxusername->value ) );
00719                 throw $oEx;
00720             }
00721         }
00722 
00723         $this->oxuser__oxshopid = new oxField( $sShopID, oxField::T_RAW );
00724         if ( ( $blOK = $this->save() ) ) {
00725             // dropping/cleaning old delivery address/payment info
00726             $oDB->execute( "delete from oxaddress where oxaddress.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00727             $oDB->execute( "update oxuserpayments set oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxusername->value ) . " where oxuserpayments.oxuserid = " . $oDB->quote( $this->oxuser__oxid->value ) . " " );
00728         } else {
00729             $oEx = oxNew( 'oxUserException' );
00730             $oEx->setMessage( 'EXCEPTION_USER_USERCREATIONFAILED' );
00731             throw $oEx;
00732         }
00733 
00734         return $blOK;
00735     }
00736 
00744     public function addToGroup( $sGroupID )
00745     {
00746         if ( !$this->inGroup( $sGroupID ) ) {
00747             $oNewGroup = oxNew( 'oxobject2group' );
00748             $oNewGroup->oxobject2group__oxobjectid = new oxField($this->getId(), oxField::T_RAW);
00749             $oNewGroup->oxobject2group__oxgroupsid = new oxField($sGroupID, oxField::T_RAW);
00750             if ( $oNewGroup->save() ) {
00751                 $this->_oGroups[$sGroupID] = $oNewGroup;
00752                 return true;
00753             }
00754         }
00755         return false;
00756     }
00757 
00765     public function removeFromGroup( $sGroupID = null )
00766     {
00767         if ( $sGroupID != null && $this->inGroup( $sGroupID ) ) {
00768             $oGroups = oxNew( 'oxlist' );
00769             $oGroups->init( 'oxobject2group' );
00770             $sSelect = 'select * from oxobject2group where oxobject2group.oxobjectid = "'.$this->getId().'" and oxobject2group.oxgroupsid = "'.$sGroupID.'" ';
00771             $oGroups->selectString( $sSelect );
00772             foreach ( $oGroups as $oRemgroup ) {
00773                 if ( $oRemgroup->delete() ) {
00774                     unset( $this->_oGroups[$oRemgroup->oxobject2group__oxgroupsid->value] );
00775                 }
00776             }
00777         }
00778     }
00779 
00788     public function onOrderExecute( $oBasket, $iSuccess )
00789     {
00790 
00791         if ( is_numeric( $iSuccess ) && $iSuccess != 2 && $iSuccess <= 3 ) {
00792             //adding user to particular customer groups
00793             if ( !$this->oxuser__oxdisableautogrp->value ) {
00794 
00795                 $myConfig = $this->getConfig();
00796                 $dMidlleCustPrice = (float) $myConfig->getConfigParam( 'sMidlleCustPrice' );
00797                 $dLargeCustPrice  = (float) $myConfig->getConfigParam( 'sLargeCustPrice' );
00798 
00799                 $this->addToGroup( 'oxidcustomer' );
00800                 $dBasketPrice = $oBasket->getPrice()->getBruttoPrice();
00801                 if ( $dBasketPrice < $dMidlleCustPrice ) {
00802                     $this->addToGroup( 'oxidsmallcust' );
00803                 }
00804                 if ( $dBasketPrice >= $dMidlleCustPrice && $dBasketPrice < $dLargeCustPrice ) {
00805                     $this->addToGroup( 'oxidmiddlecust' );
00806                 }
00807                 if ( $dBasketPrice >= $dLargeCustPrice ) {
00808                     $this->addToGroup( 'oxidgoodcust' );
00809                 }
00810             }
00811 
00812             if ( $this->inGroup( 'oxidnotyetordered' ) ) {
00813                 $this->removeFromGroup( 'oxidnotyetordered' );
00814             }
00815         }
00816     }
00817 
00825     public function getBasket( $sName )
00826     {
00827         if ( !isset( $this->_aBaskets[$sName] ) ) {
00828             $oBasket = oxNew( 'oxuserbasket' );
00829             $aWhere = array( 'oxuserbaskets.oxuserid' => $this->getId(), 'oxuserbaskets.oxtitle' => $sName );
00830 
00831             // creating if it does not exist
00832             if ( !$oBasket->assignRecord( $oBasket->buildSelectString( $aWhere ) ) ) {
00833                 $oBasket->oxuserbaskets__oxtitle  = new oxField($sName);
00834                 $oBasket->oxuserbaskets__oxuserid = new oxField($this->getId());
00835 
00836                 // marking basket as new (it will not be saved in DB yet)
00837                 $oBasket->setIsNewBasket();
00838             }
00839 
00840             $this->_aBaskets[$sName] = $oBasket;
00841         }
00842 
00843         return $this->_aBaskets[$sName];
00844     }
00845 
00854     public function convertBirthday( $aData )
00855     {
00856 
00857         // preparing data to process
00858         $iYear  = isset($aData['year'])?((int) $aData['year']):false;
00859         $iMonth = isset($aData['month'])?((int) $aData['month']):false;
00860         $iDay   = isset($aData['day'])?((int) $aData['day']):false;
00861 
00862         // leaving empty if not set
00863         if ( !$iYear && !$iMonth && !$iDay )
00864             return "";
00865 
00866         // year
00867         if ( !$iYear || $iYear < 1000 || $iYear > 9999)
00868             $iYear = date('Y');
00869 
00870         // month
00871         if ( !$iMonth || $iMonth < 1 || $iMonth > 12)
00872             $iMonth = 1;
00873 
00874         // maximum nuber of days in month
00875         $iMaxDays = 31;
00876         switch( $iMonth) {
00877             case 2 :
00878                 if ($iMaxDays > 28)
00879                     $iMaxDays = ($iYear % 4 == 0 && ($iYear % 100 != 0 || $iYear % 400 == 0)) ? 29 : 28;
00880                 break;
00881             case 4  :
00882             case 6  :
00883             case 9  :
00884             case 11 :
00885                 $iMaxDays = min(30, $iMaxDays);
00886                 break;
00887         }
00888 
00889         // day
00890         if ( !$iDay || $iDay < 1 || $iDay > $iMaxDays) {
00891             $iDay = 1;
00892         }
00893 
00894         // whole date
00895         return sprintf("%04d-%02d-%02d", $iYear, $iMonth, $iDay);
00896     }
00897 
00904     public function getBoni()
00905     {
00906         return 1000;
00907     }
00908 
00921     public function addDynGroup( $sDynGoup, $aDeniedDynGroups )
00922     {
00923         // preparing input
00924         $sDynGoup = strtolower( trim( $sDynGoup ) );
00925 
00926         // setting denied groups from admin settings also
00927         $aDisabledDynGroups = array_merge( array( 'oxidadmin' ), (array) $aDeniedDynGroups );
00928 
00929         // default state ..
00930         $blAdd = false;
00931 
00932         // user assignment to dyn group is not allowed
00933         if ( $this->oxuser__oxdisableautogrp->value || !$sDynGoup ) {
00934             $blAdd = false;
00935         } elseif ( in_array( $sDynGoup, $aDisabledDynGroups ) ) {
00936             // trying to add user to prohibited user group?
00937             $blAdd = false;
00938         } elseif ( $this->addToGroup( $sDynGoup ) ) {
00939             $blAdd = true;
00940         }
00941 
00942         // cleanup
00943         oxSession::deleteVar( 'dgr' );
00944 
00945         return $blAdd;
00946     }
00947 
00963     public function checkValues( $sLogin, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
00964     {
00965         // 1. checking user name
00966         $sLogin = $this->_checkLogin( $sLogin, $aInvAddress );
00967 
00968         // 2. cheking email
00969         $this->_checkEmail( $sLogin );
00970 
00971         // 3. password
00972         $this->_checkPassword( $sPassword, $sPassword2, ((int) oxConfig::getParameter( 'option' ) == 3) );
00973 
00974         // 4. required fields
00975         $this->_checkRequiredFields( $aInvAddress, $aDelAddress );
00976 
00977         // 5. country check
00978         $this->_checkCountries( $aInvAddress, $aDelAddress );
00979 
00980         // 6. vat id check.
00981             $this->_checkVatId( $aInvAddress );
00982     }
00983 
00992     public function setNewsSubscription( $blSubscribe, $blSendOptIn )
00993     {
00994         // assigning to newsletter
00995         $blSuccess = false;
00996         $myConfig  = $this->getConfig();
00997 
00998         // user wants to get newsletter messages or no ?
00999         $oNewsSubscription = $this->getNewsSubscription();
01000         if ( $blSubscribe && $oNewsSubscription->getOptInStatus() != 1 ) {
01001             if ( !$blSendOptIn ) {
01002 
01003                 // double-opt-in check is disabled - assigning automatically
01004                 $this->addToGroup( 'oxidnewsletter' );
01005                 // and setting subscribed status
01006                 $oNewsSubscription->setOptInStatus( 1 );
01007                 $blSuccess = true;
01008             } else {
01009 
01010                 // double-opt-in check enabled - sending confirmation email and setting waiting status
01011                 $oNewsSubscription->setOptInStatus( 2 );
01012 
01013                 // sending double-opt-in mail
01014                 $oEmail = oxNew( 'oxemail' );
01015                 $blSuccess = $oEmail->sendNewsletterDBOptInMail( $this );
01016             }
01017         } elseif ( !$blSubscribe ) {
01018             // removing user from newsletter subscribers
01019             $this->removeFromGroup( 'oxidnewsletter' );
01020             $oNewsSubscription->setOptInStatus( 0 );
01021             $blSuccess = true;
01022         }
01023 
01024         return $blSuccess;
01025     }
01026 
01044     public function changeUserData( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress )
01045     {
01046 
01047         // validating values before saving. If validation fails - exception is thrown
01048         $this->checkValues( $sUser, $sPassword, $sPassword2, $aInvAddress, $aDelAddress );
01049 
01050         // input data is fine - lets save updated user info
01051         $this->assign( $aInvAddress );
01052 
01053 
01054         // update old or add new delivery address
01055         $this->_assignAddress( $aDelAddress );
01056 
01057         // saving new values
01058         if ( $this->save() ) {
01059 
01060             // assigning automatically to specific groups
01061             $sCountryId = isset( $aInvAddress['oxuser__oxcountryid'] )?$aInvAddress['oxuser__oxcountryid']:'';
01062             $this->_setAutoGroups( $sCountryId );
01063         }
01064     }
01065 
01074     public function addUserAddress( $oUser )
01075     {
01076 
01077         if ( $this->_hasUserAddress( $oUser->getId() ) ) {
01078             return false;
01079         }
01080 
01081         $oAddress = oxNew( 'oxaddress' );
01082 
01083         $oAddress->oxaddress__oxuserid        = new oxField($this->getId(), oxField::T_RAW);
01084         $oAddress->oxaddress__oxaddressuserid = new oxField($oUser->getId(), oxField::T_RAW);
01085         $oAddress->oxaddress__oxfname         = new oxField($oUser->oxuser__oxfname->value, oxField::T_RAW);
01086         $oAddress->oxaddress__oxlname         = new oxField($oUser->oxuser__oxlname->value, oxField::T_RAW);
01087         $oAddress->oxaddress__oxstreet        = new oxField($oUser->oxuser__oxstreet->value, oxField::T_RAW);
01088         $oAddress->oxaddress__oxstreetnr      = new oxField($oUser->oxuser__oxstreetnr->value, oxField::T_RAW);
01089         $oAddress->oxaddress__oxcity          = new oxField($oUser->oxuser__oxcity->value, oxField::T_RAW);
01090         $oAddress->oxaddress__oxzip           = new oxField($oUser->oxuser__oxzip->value, oxField::T_RAW);
01091         $oAddress->oxaddress__oxcountry       = new oxField($oUser->oxuser__oxcountry->value, oxField::T_RAW);
01092         $oAddress->oxaddress__oxcountryid     = new oxField($oUser->oxuser__oxcountryid->value, oxField::T_RAW);
01093         $oAddress->oxaddress__oxcompany       = new oxField($oUser->oxuser__oxcompany->value, oxField::T_RAW);
01094 
01095         // adding new address
01096         if ( $oAddress->save() ) {
01097             // resetting addresses
01098             $this->_oAddresses = null;
01099             return $oAddress->getId();
01100         }
01101     }
01102 
01110     protected function _assignAddress( $aDelAddress )
01111     {
01112         if (isset($aDelAddress) && count($aDelAddress)) {
01113             $sAddressId = oxConfig::getParameter( 'oxaddressid' );
01114             $sMyAddressId = ( $sAddressId === null || $sAddressId == -1 || $sAddressId == -2 ) ?  null : $sAddressId;
01115             $aDelAddress['oxaddress__oxid'] = $sMyAddressId;
01116             $oAddress = oxNew( 'oxaddress' );
01117             $oAddress->assign( $aDelAddress );
01118             $oAddress->oxaddress__oxuserid  = new oxField( $this->getId(), oxField::T_RAW );
01119             $oAddress->oxaddress__oxcountry = $this->getUserCountry( $oAddress->oxaddress__oxcountryid->value );
01120             $oAddress->save();
01121 
01122             // resetting addresses
01123             $this->_oAddresses = null;
01124 
01125             // saving delivery Address for later use
01126             oxSession::setVar( 'deladrid', $oAddress->getId() );
01127         } else {
01128             // resetting
01129             oxSession::setVar( 'deladrid', null );
01130         }
01131     }
01132 
01145     public function login( $sUser, $sPassword, $blCookie = false)
01146     {
01147         if ( $this->isAdmin() && !count( oxUtilsServer::getInstance()->getOxCookie() ) ) {
01148             $oEx = oxNew( 'oxCookieException' );
01149             $oEx->setMessage( 'EXCEPTION_COOKIE_NOCOOKIE' );
01150             throw $oEx;
01151         }
01152 
01153         $myConfig = $this->getConfig();
01154         if ( $sPassword ) {
01155 
01156             $sShopID = $myConfig->getShopId();
01157             $oDb = oxDb::getDb();
01158 
01159             $sUserSelect = is_numeric( $sUser ) ? "oxuser.oxcustnr = {$sUser} " : "oxuser.oxusername = " . $oDb->quote( $sUser );
01160             $sPassSelect = " oxuser.oxpassword = MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( oxuser.oxpasssalt ) ) ) ";
01161             $sShopSelect = "";
01162 
01163 
01164             // admin view: can only login with higher than 'user' rights
01165             if ( $this->isAdmin() ) {
01166                 $sShopSelect = " and ( oxrights != 'user' ) ";
01167             }
01168 
01169             $sWhat = "oxid";
01170 
01171             $sSelect =  "select $sWhat from oxuser where oxuser.oxactive = 1 and {$sPassSelect} and {$sUserSelect} {$sShopSelect} ";
01172             if ( $myConfig->isDemoShop() && $this->isAdmin() ) {
01173                 if ( $sPassword == "admin" && $sUser == "admin" ) {
01174                     $sSelect = "select $sWhat from oxuser where oxrights = 'malladmin' {$sShopSelect} ";
01175                 } else {
01176                     $oEx = oxNew( 'oxUserException' );
01177                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01178                     throw $oEx;
01179                 }
01180             }
01181 
01182             // load from DB
01183             $aData = $oDb->getAll( $sSelect );
01184             $sOXID = @$aData[0][0];
01185             if ( isset( $sOXID ) && $sOXID && !@$aData[0][1] ) {
01186 
01187                 if ( !$this->load( $sOXID ) ) {
01188                     $oEx = oxNew( 'oxUserException' );
01189                     $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01190                     throw $oEx;
01191                 }
01192             }
01193         }
01194 
01195 
01196         //login successfull?
01197         if ( $this->oxuser__oxid->value ) {
01198             // yes, successful login
01199             if ( $this->isAdmin() ) {
01200                 oxSession::setVar( 'auth', $this->oxuser__oxid->value );
01201             } else {
01202                 oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01203             }
01204 
01205             // cookie must be set ?
01206             if ( $blCookie ) {
01207                 oxUtilsServer::getInstance()->setUserCookie( $this->oxuser__oxusername->value, $this->oxuser__oxpassword->value, $myConfig->getShopId() );
01208             }
01209             return true;
01210         } else {
01211             $oEx = oxNew( 'oxUserException' );
01212             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01213             throw $oEx;
01214         }
01215     }
01216 
01227     public function openIdLogin( $sUser )
01228     {
01229         $myConfig = $this->getConfig();
01230         $sShopID = $myConfig->getShopId();
01231         $oDb = oxDb::getDb();
01232 
01233         $sUserSelect = "oxuser.oxusername = " . $oDb->quote( $sUser );
01234         $sShopSelect = "";
01235 
01236 
01237         $sSelect =  "select oxid from oxuser where oxuser.oxactive = 1 and {$sUserSelect} {$sShopSelect} ";
01238 
01239         // load from DB
01240         $aData = $oDb->getAll( $sSelect );
01241         $sOXID = @$aData[0][0];
01242         if ( isset( $sOXID ) && $sOXID ) {
01243 
01244             if ( !$this->load( $sOXID ) ) {
01245                 $oEx = oxNew( 'oxUserException' );
01246                 $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01247                 throw $oEx;
01248             }
01249         }
01250 
01251         //login successfull?
01252         if ( $this->oxuser__oxid->value ) {
01253             // yes, successful login
01254             oxSession::setVar( 'usr', $this->oxuser__oxid->value );
01255             return true;
01256         } else {
01257             $oEx = oxNew( 'oxUserException' );
01258             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDLOGIN' );
01259             throw $oEx;
01260         }
01261     }
01262 
01268     public function logout()
01269     {
01270         // deleting session info
01271         oxSession::deleteVar( 'usr' );  // for front end
01272         oxSession::deleteVar( 'auth' ); // for back end
01273         oxSession::deleteVar( 'dgr' );
01274         oxSession::deleteVar( 'dynvalue' );
01275         oxSession::deleteVar( 'paymentid' );
01276         // oxSession::deleteVar( 'deladrid' );
01277 
01278         // delete cookie
01279         oxUtilsServer::getInstance()->deleteUserCookie( $this->getConfig()->getShopID() );
01280 
01281         // unsetting global user
01282         $this->setUser( null );
01283 
01284         return true;
01285     }
01286 
01293     public function loadAdminUser()
01294     {
01295         return $this->loadActiveUser( true );
01296     }
01297 
01306     public function loadActiveUser( $blForceAdmin = false )
01307     {
01308         $myConfig = $this->getConfig();
01309 
01310         $blAdmin = $myConfig->isAdmin() || $blForceAdmin;
01311         $oDB = oxDb::getDb();
01312 
01313         // first - checking session info
01314         $sUserID = $blAdmin ? oxSession::getVar( 'auth' ) : oxSession::getVar( 'usr' );
01315         $blFoundInCookie = false;
01316 
01317         //trying automatic login (by 'remember me' cookie)
01318         if ( !$sUserID && !$blAdmin ) {
01319             $sShopID = $myConfig->getShopId();
01320             if ( ( $sSet = oxUtilsServer::getInstance()->getUserCookie( $sShopID ) ) ) {
01321                 $aData = explode( '@@@', $sSet );
01322                 $sUser = $aData[0];
01323                 $sPWD  = @$aData[1];
01324 
01325                 $sSelect =  'select oxid, oxpassword from oxuser where oxuser.oxpassword != "" and  oxuser.oxactive = 1 and oxuser.oxusername = '.$oDB->quote($sUser);
01326 
01327 
01328                 $oDB = oxDb::getDb();
01329                 $rs = $oDB->execute( $sSelect );
01330                 if ( $rs != false && $rs->recordCount() > 0 ) {
01331                     while (!$rs->EOF) {
01332                         $sTest = crypt( $rs->fields[1], 'ox' );
01333                         if ( $sTest == $sPWD ) {
01334                             // found
01335                             $sUserID = $rs->fields[0];
01336                             $blFoundInCookie = true;
01337                             break;
01338                         }
01339                         $rs->moveNext();
01340                     }
01341                 }
01342             }
01343         }
01344 
01345         // checking user results
01346         if ( $sUserID ) {
01347             if ( $this->load( $sUserID ) ) {
01348                 // storing into session
01349                 if ($blAdmin) {
01350                     oxSession::setVar( 'auth', $sUserID );
01351                 } else {
01352                     oxSession::setVar( 'usr', $sUserID );
01353                 }
01354 
01355                 // marking the way user was loaded
01356                 $this->_blLoadedFromCookie = $blFoundInCookie;
01357                 return true;
01358             }
01359         } else {
01360             // no user
01361             oxSession::deleteVar( 'usr' );
01362             oxSession::deleteVar( 'auth' );
01363 
01364             return false;
01365         }
01366     }
01367 
01380     protected function _ldapLogin( $sUser, $sPassword, $sShopID, $sShopSelect)
01381     {
01382         include "oxldap.php";
01383         $myConfig = $this->getConfig();
01384         $oDb = oxDb::getDb();
01385         //$throws oxConnectionException
01386         $aLDAPParams = $myConfig->getConfigParam( 'aLDAPParams' );
01387         $oLDAP = new oxLDAP( $aLDAPParams['HOST'], $aLDAPParams['PORT'] );
01388         // maybe this is LDAP user but supplied email Address instead of LDAP login
01389         $sLDAPKey = $oDb->getOne("select oxldapkey from oxuser where oxuser.oxactive = 1 and oxuser.oxusername = ".$oDb->quote($sUser)." $sShopSelect");
01390         if ( isset( $sLDAPKey) && $sLDAPKey) {
01391             $sUser = $sLDAPKey;
01392         }
01393 
01394         //$throws oxConnectionException
01395         $oLDAP->login( $sUser, $sPassword, $aLDAPParams['USERQUERY'], $aLDAPParams['BASEDN'], $aLDAPParams['FILTER']);
01396 
01397         $aData = $oLDAP->mapData($aLDAPParams['DATAMAP']);
01398         if ( isset( $aData['OXUSERNAME']) && $aData['OXUSERNAME']) {
01399             // login successful
01400 
01401             // check if user is already in database
01402             $sSelect =  "select oxid from oxuser where oxuser.oxusername = ".$oDb->quote($aData['OXUSERNAME'])." $sShopSelect";
01403             $sOXID = $oDb->getOne( $sSelect);
01404 
01405             if ( !isset( $sOXID) || !$sOXID) {
01406                 // we need to create a new user
01407                 //$oUser->oxuser__oxid->setValue($oUser->setId());
01408                 $this->setId();
01409 
01410                 // map all user data fields
01411                 foreach ( $aData as $fldname => $value) {
01412                     $sField = "oxuser__".strtolower( $fldname);
01413                     $this->$sField->setValue($aData[$fldname]);
01414                 }
01415 
01416                 $this->oxuser__oxactive->setValue(1);
01417                 $this->oxuser__oxshopid->setValue($sShopID);
01418                 $this->oxuser__oxldapkey->setValue($sUser);
01419                 $this->oxuser__oxrights->setValue("user");
01420                 $this->setPassword( "ldap user" );
01421 
01422                 $this->save();
01423             } else {
01424                 // LDAP user is already in OXID DB, load it
01425                 $this->load( $sOXID);
01426             }
01427 
01428         } else {
01429             $oEx = oxNew( 'oxUserException' );
01430             $oEx->setMessage('EXCEPTION_USER_NOVALUES');
01431             throw $oEx;
01432         }
01433     }
01434 
01441     protected function _getUserRights()
01442     {
01443         // previously user had no rights defined
01444         if ( !$this->oxuser__oxrights->value )
01445             return 'user';
01446 
01447         $oDB = oxDb::getDb();
01448         $myConfig    = $this->getConfig();
01449         $sAuthRights = null;
01450 
01451         // choosing possible user rights index
01452         $sAuthUserID = $this->isAdmin()?oxSession::getVar( 'auth' ):null;
01453         $sAuthUserID = $sAuthUserID?$sAuthUserID:oxSession::getVar( 'usr' );
01454         if ( $sAuthUserID ) {
01455             $sAuthRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid='.$oDB->quote( $sAuthUserID ) );
01456         }
01457 
01458         //preventing user rights edit for non admin
01459         $aRights = array();
01460 
01461         // selecting current users rights ...
01462         if ( $sCurrRights = $oDB->getOne( 'select oxrights from '.$this->getViewName().' where oxid="'.$this->getId().'"' ) ) {
01463             $aRights[] = $sCurrRights;
01464         }
01465         $aRights[] = 'user';
01466 
01467         if ( !$sAuthRights || !( $sAuthRights == 'malladmin' || $sAuthRights == $myConfig->getShopId() ) ) {
01468             return current( $aRights );
01469         } elseif ( $sAuthRights == $myConfig->getShopId() ) {
01470             $aRights[] = $sAuthRights;
01471             if ( !in_array( $this->oxuser__oxrights->value, $aRights ) ) {
01472                 return current( $aRights );
01473             }
01474         }
01475 
01476         // leaving as it was set ...
01477         return $this->oxuser__oxrights->value;
01478     }
01479 
01489     protected function _setRecordNumber( $sMaxField, $aWhere = null ,$iMaxTryCnt = 5 )
01490     {
01491 
01492         /*if ( !$myConfig->blMallUsers ) {
01493             $sShopID = $myConfig->getShopId();
01494             $aWhere = array(" {$this->getViewName()}.oxshopid = '$sShopID' ");
01495         }*/
01496 
01497         return parent::_setRecordNumber( $sMaxField, $aWhere, $iMaxTryCnt );
01498     }
01499 
01505     protected function _insert()
01506     {
01507 
01508         // set oxcreate date
01509         $this->oxuser__oxcreate = new oxField(date( 'Y-m-d H:i:s' ), oxField::T_RAW);
01510 
01511         if ( !isset( $this->oxuser__oxboni->value ) ) {
01512             $this->oxuser__oxboni = new oxField($this->getBoni(), oxField::T_RAW);
01513         }
01514 
01515         if ( $blInsert = parent::_insert() ) {
01516             // setting customer number
01517             if ( !$this->oxuser__oxcustnr->value || !$this->isAdmin() ) {
01518                 $this->_setRecordNumber( 'oxcustnr' );
01519             }
01520         }
01521 
01522         return $blInsert;
01523     }
01524 
01530     protected function _update()
01531     {
01532         //V #M418: for not registered users, don't change boni during update
01533         if (!$this->oxuser__oxpassword->value && $this->oxuser__oxregister->value < 1) {
01534             $this->_aSkipSaveFields[] = 'oxboni';
01535         }
01536 
01537         // don't change this field
01538         $this->_aSkipSaveFields[] = 'oxcreate';
01539         if ( !$this->isAdmin() ) {
01540             $this->_aSkipSaveFields[] = 'oxcustnr';
01541             $this->_aSkipSaveFields[] = 'oxrights';
01542         }
01543 
01544         // updating subscription information
01545         if ( ( $blUpdate = parent::_update() ) ) {
01546             $this->getNewsSubscription()->updateSubscription( $this );
01547         }
01548 
01549         return $blUpdate;
01550     }
01551 
01567     protected function _checkLogin( $sLogin, $aInvAddress )
01568     {
01569         $myConfig = $this->getConfig();
01570 
01571         $sLogin   = ( isset( $aInvAddress['oxuser__oxusername'] ) )?$aInvAddress['oxuser__oxusername']:$sLogin;
01572 
01573         // check only for users with password during registration
01574         // if user wants to change user name - we must check if passwords are ok before changing
01575         if ( $this->oxuser__oxpassword->value && $sLogin != $this->oxuser__oxusername->value ) {
01576 
01577             // on this case password must be taken directly from request
01578             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
01579             if ( !$sNewPass ) {
01580 
01581                 // 1. user forgot to enter password
01582                 $oEx = oxNew( 'oxInputException' );
01583                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01584                 throw $oEx;
01585             } else {
01586 
01587                 // 2. entered wrong password
01588                 if ( !$this->isSamePassword( $sNewPass ) ) {
01589                     $oEx = oxNew( 'oxUserException' );
01590                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01591                     throw $oEx;
01592                 }
01593             }
01594         }
01595 
01596         if ( $this->checkIfEmailExists( $sLogin ) ) {
01597             //if exists then we do now allow to do that
01598             $oEx = oxNew( 'oxUserException' );
01599             $oLang = oxLang::getInstance();
01600             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
01601             throw $oEx;
01602         }
01603 
01604         return $sLogin;
01605     }
01606 
01614     public function checkIfEmailExists( $sEmail )
01615     {
01616         $myConfig = $this->getConfig();
01617         $oDB = oxDb::getDb();
01618         $iShopId = $myConfig->getShopId();
01619         $blExists = false;
01620 
01621         $sQ = 'select oxshopid, oxrights, oxpassword from oxuser where oxusername = '. $oDB->quote( $sEmail );
01622         if ( ( $sOxid = $this->getId() ) ) {
01623             $sQ .= " and oxid <> '$sOxid' ";
01624         }
01625 
01626         $oRs = $oDB->execute( $sQ );
01627         if ( $oRs != false && $oRs->recordCount() > 0 ) {
01628 
01629             if ( $this->_blMallUsers ) {
01630 
01631                 $blExists = true;
01632                 if ( $oRs->fields[1] == 'user' && !$oRs->fields[2] ) {
01633 
01634                     // password is not set - allow to override
01635                     $blExists = false;
01636                 }
01637             } else {
01638 
01639                 $blExists = false;
01640                 while ( !$oRs->EOF ) {
01641                     if ( $oRs->fields[1] != 'user' ) {
01642 
01643                         // exists admin with same login - must not allow
01644                         $blExists = true;
01645                         break;
01646                     } elseif ( $oRs->fields[0] == $iShopId && $oRs->fields[2] ) {
01647 
01648                         // exists same login (with password) in same shop
01649                         $blExists = true;
01650                         break;
01651                     }
01652 
01653                     $oRs->moveNext();
01654                 }
01655             }
01656         }
01657         return $blExists;
01658     }
01659 
01667     public function getUserRecommLists( $sOXID = null )
01668     {
01669         if ( !$sOXID )
01670             $sOXID = $this->getId();
01671 
01672         // sets active page
01673         $iActPage = (int) oxConfig::getParameter( 'pgNr' );
01674         $iActPage = ($iActPage < 0) ? 0 : $iActPage;
01675 
01676         // load only lists which we show on screen
01677         $iNrofCatArticles = $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
01678         $iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 10;
01679 
01680 
01681         $oRecommList = oxNew( 'oxlist' );
01682         $oRecommList->init( 'oxrecommlist' );
01683         $oRecommList->setSqlLimit( $iNrofCatArticles * $iActPage, $iNrofCatArticles );
01684         $iShopId = $this->getConfig()->getShopId();
01685         $sSelect = 'select * from oxrecommlists where oxuserid ='. oxDb::getDb()->quote( $sOXID ) . ' and oxshopid ="'. $iShopId .'"';
01686         $oRecommList->selectString( $sSelect );
01687 
01688         return $oRecommList;
01689     }
01690 
01698     public function getRecommListsCount( $sOx = null )
01699     {
01700         if ( !$sOx ) {
01701             $sOx = $this->getId();
01702         }
01703 
01704         if ( $this->_iCntRecommLists === null || $sOx ) {
01705             $oDb = oxDb::getDb();
01706             $this->_iCntRecommLists = 0;
01707             $iShopId = $this->getConfig()->getShopId();
01708             $sSelect = 'select count(oxid) from oxrecommlists where oxuserid = ' . $oDb->quote( $sOx ) . ' and oxshopid ="'. $iShopId .'"';
01709             $this->_iCntRecommLists = $oDb->getOne( $sSelect );
01710         }
01711         return $this->_iCntRecommLists;
01712     }
01713 
01722     protected function _checkEmail( $sEmail )
01723     {
01724         // missing email address (user login name) ?
01725         if ( !$sEmail ) {
01726             $oEx = oxNew( 'oxInputException' );
01727             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01728             throw $oEx;
01729         }
01730 
01731         // invalid email address ?
01732         if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
01733             $oEx = oxNew( 'oxInputException' );
01734             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
01735             throw $oEx;
01736         }
01737     }
01738 
01753     protected function _checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01754     {
01755         $this->checkPassword( $sNewPass, $sConfPass, $blCheckLenght );
01756     }
01757 
01770     public function checkPassword( $sNewPass, $sConfPass, $blCheckLenght = false )
01771     {
01772         //  no password at all
01773         if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
01774             $oEx = oxNew( 'oxInputException' );
01775             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
01776             throw $oEx;
01777         }
01778 
01779         //  password is too short ?
01780         if ( $blCheckLenght &&  getStr()->strlen( $sNewPass ) < 6 ) {
01781             $oEx = oxNew( 'oxInputException' );
01782             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
01783             throw $oEx;
01784         }
01785 
01786         //  passwords do not match ?
01787         if ( $sNewPass != $sConfPass ) {
01788             $oEx = oxNew( 'oxUserException' );
01789             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
01790             throw $oEx;
01791         }
01792     }
01793 
01804     protected function _checkCountries( $aInvAddress, $aDelAddress )
01805     {
01806         $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
01807         $sDelCtry  = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
01808 
01809         if ( $sBillCtry || $sDelCtry ) {
01810             $oDb = oxDb::getDb();
01811 
01812             if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
01813                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
01814                 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
01815             } else {
01816                 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
01817                               ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
01818             }
01819 
01820             if ( !$oDb->getOne( $sQ ) ) {
01821                 $oEx = oxNew( 'oxUserException' );
01822                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS' );
01823                 throw $oEx;
01824             }
01825         }
01826     }
01827 
01839     protected function _checkRequiredFields( $aInvAddress, $aDelAddress )
01840     {
01841         // collecting info about required fields
01842         $aMustFields = array( 'oxuser__oxfname',
01843                               'oxuser__oxlname',
01844                               'oxuser__oxstreetnr',
01845                               'oxuser__oxstreet',
01846                               'oxuser__oxzip',
01847                               'oxuser__oxcity' );
01848 
01849         // config shoud override default fields
01850         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
01851         if ( is_array( $aMustFillFields ) ) {
01852             $aMustFields = $aMustFillFields;
01853         }
01854 
01855         // assuring data to check
01856         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
01857         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
01858 
01859         // collecting fields
01860         $aFields = array_merge( $aInvAddress, $aDelAddress );
01861 
01862 
01863         // check delivery address ?
01864         $blCheckDel = false;
01865         if ( count( $aDelAddress ) ) {
01866             $blCheckDel = true;
01867         }
01868 
01869         // checking
01870         foreach ( $aMustFields as $sMustField ) {
01871 
01872             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
01873             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
01874                 continue;
01875             }
01876 
01877             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
01878                 $this->_checkRequiredArrayFields( $sMustField, $aFields[$sMustField] );
01879             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
01880                    $oEx = oxNew( 'oxInputException' );
01881                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01882                    throw $oEx;
01883             }
01884         }
01885     }
01886 
01897     protected function _checkRequiredArrayFields( $sFieldName, $aFieldValues )
01898     {
01899         foreach ( $aFieldValues as $sValue ) {
01900             if ( !trim( $sValue ) ) {
01901                 $oEx = oxNew( 'oxInputException' );
01902                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
01903                 throw $oEx;
01904             }
01905         }
01906     }
01907 
01918     protected function _checkVatId( $aInvAddress )
01919     {
01920         if ( $aInvAddress['oxuser__oxustid'] ) {
01921 
01922             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
01923                 // no country
01924                 return;
01925             }
01926             $oCountry = oxNew('oxcountry');
01927             if (!$oCountry->load($sCountryId)) {
01928                 throw new oxObjectException();
01929             }
01930             if ($oCountry->isForeignCountry() && $oCountry->isInEU()) {
01931                     if (strncmp($aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2)) {
01932                         $oEx = oxNew( 'oxInputException' );
01933                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
01934                         throw $oEx;
01935                     }
01936             }
01937         }
01938     }
01939 
01948     protected function _setAutoGroups( $sCountryId )
01949     {
01950         // assigning automatically to specific groups
01951         $blForeigner = true;
01952         $blForeignGroupExists = false;
01953         $blInlandGroupExists = false;
01954 
01955         $aHomeCountry = $this->getConfig()->getConfigParam( 'aHomeCountry' );
01956         // foreigner ?
01957         if ( is_array($aHomeCountry)) {
01958             if (in_array($sCountryId, $aHomeCountry)) {
01959                 $blForeigner = false;
01960             }
01961         } elseif ($sCountryId == $aHomeCountry) {
01962             $blForeigner = false;
01963         }
01964 
01965         if ( $this->inGroup( 'oxidforeigncustomer' ) ) {
01966             $blForeignGroupExists = true;
01967             if ( !$blForeigner ) {
01968                 $this->removeFromGroup( 'oxidforeigncustomer' );
01969             }
01970         }
01971 
01972         if ( $this->inGroup( 'oxidnewcustomer' ) ) {
01973             $blInlandGroupExists = true;
01974             if ( $blForeigner ) {
01975                 $this->removeFromGroup( 'oxidnewcustomer' );
01976             }
01977         }
01978 
01979         if ( !$this->oxuser__oxdisableautogrp->value ) {
01980             if ( !$blForeignGroupExists && $blForeigner ) {
01981                 $this->addToGroup( 'oxidforeigncustomer' );
01982             }
01983             if ( !$blInlandGroupExists && !$blForeigner ) {
01984                 $this->addToGroup( 'oxidnewcustomer' );
01985             }
01986         }
01987     }
01988 
01996     protected function _hasUserAddress( $sUserId )
01997     {
01998         $oAddresses = $this->getUserAddresses();
01999         if ( $oAddresses && count($oAddresses)>0 ) {
02000             $oAddresses->rewind() ;
02001             foreach ($oAddresses as $key => $oAddress) {
02002                 if ( $oAddress->oxaddress__oxaddressuserid->value == $sUserId ) {
02003                     return true;
02004                 }
02005             }
02006         }
02007         return false;
02008     }
02009 
02022     protected function _setUserCookie( $sUser, $sPassword,  $sShopId = null, $iTimeout = 31536000 )
02023     {
02024         oxUtilsServer::getInstance()->setUserCookie( $sUser, $sPassword, $sShopId, $iTimeout );
02025     }
02026 
02036     protected function _deleteUserCookie( $sShopId = null )
02037     {
02038         oxUtilsServer::getInstance()->deleteUserCookie( $sShopId );
02039     }
02040 
02050     protected static function _getUserCookie( $sShopId = null )
02051     {
02052         return oxUtilsServer::getInstance()->getUserCookie( $sShopId );
02053     }
02054 
02055 
02064     public function loadUserByUpdateId( $sUid )
02065     {
02066         $oDb = oxDb::getDb();
02067         $sQ = "select oxid from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sUid );
02068         if ( $sUserId = $oDb->getOne( $sQ ) ) {
02069             return $this->load( $sUserId );
02070         }
02071     }
02072 
02080     public function setUpdateKey( $blReset = false )
02081     {
02082         $sUpKey  = $blReset ? '' : oxUtilsObject::getInstance()->generateUId();
02083         $iUpTime = $blReset ? 0 : oxUtilsDate::getInstance()->getTime() + $this->getUpdateLinkTerm();
02084 
02085         // generating key
02086         $this->oxuser__oxupdatekey = new oxField( $sUpKey, oxField::T_RAW );
02087 
02088         // setting expiration time for 6 hours
02089         $this->oxuser__oxupdateexp = new oxField( $iUpTime, oxField::T_RAW );
02090 
02091         // saving
02092         $this->save();
02093     }
02094 
02100     public function getUpdateLinkTerm()
02101     {
02102         return 3600 * 6;
02103     }
02104 
02112     public function isExpiredUpdateId( $sKey )
02113     {
02114         $oDb = oxDb::getDb();
02115         $sQ = "select 1 from ".$this->getViewName()." where oxupdateexp >= ".time()." and MD5( CONCAT( oxid, oxshopid, oxupdatekey ) ) = ".$oDb->quote( $sKey );
02116         return !( (bool) $oDb->getOne( $sQ ) );
02117     }
02118 
02124     public function getUpdateId()
02125     {
02126         if ( $this->_sUpdateKey === null ) {
02127             $this->setUpdateKey();
02128             $this->_sUpdateKey = md5( $this->getId() . $this->oxuser__oxshopid->value . $this->oxuser__oxupdatekey->value );
02129         }
02130         return $this->_sUpdateKey;
02131     }
02132 
02141     public function encodePassword( $sPassword, $sSalt )
02142     {
02143         $oDb = oxDb::getDb();
02144         return $oDb->getOne( "select MD5( CONCAT( ".$oDb->quote( $sPassword ).", UNHEX( '{$sSalt}' ) ) )" );
02145     }
02146 
02154     public function prepareSalt( $sSalt )
02155     {
02156         return ( $sSalt ? oxDb::getDb()->getOne( "select HEX( '{$sSalt}' )" ) : '' );
02157     }
02158 
02166     public function decodeSalt( $sSaltHex )
02167     {
02168         return ( $sSaltHex ? oxDb::getDb()->getOne( "select UNHEX( '{$sSaltHex}' )" ) : '' );
02169     }
02170 
02178     public function setPassword( $sPassword = null )
02179     {
02180         // setting salt if password is not empty
02181         $sSalt = $sPassword ? $this->prepareSalt( oxUtilsObject::getInstance()->generateUID() ) : '';
02182 
02183         // encoding only if password was not empty (e.g. user registration without pass)
02184         $sPassword = $sPassword ? $this->encodePassword( $sPassword, $sSalt ) : '';
02185 
02186         $this->oxuser__oxpassword = new oxField( $sPassword, oxField::T_RAW );
02187         $this->oxuser__oxpasssalt = new oxField( $sSalt, oxField::T_RAW );
02188     }
02189 
02197     public function isSamePassword( $sNewPass )
02198     {
02199         return $this->encodePassword( $sNewPass, $this->oxuser__oxpasssalt->value ) == $this->oxuser__oxpassword->value;
02200     }
02201 
02207     public function isLoadedFromCookie()
02208     {
02209         return $this->_blLoadedFromCookie;
02210     }
02211 
02218     public function getPasswordHash()
02219     {
02220         $sHash = null;
02221         if ( $this->oxuser__oxpassword->value ) {
02222             if ( strpos( $this->oxuser__oxpassword->value, 'ox_' ) === 0 ) {
02223                 // decodable pass ?
02224                 $this->setPassword( oxUtils::getInstance()->strRem( $this->oxuser__oxpassword->value ) );
02225             } elseif ( ( strlen( $this->oxuser__oxpassword->value ) < 32 ) && ( strpos( $this->oxuser__oxpassword->value, 'openid_' ) !== 0 ) ) {
02226                 // plain pass ?
02227                 $this->setPassword( $this->oxuser__oxpassword->value );
02228             }
02229             $sHash = $this->oxuser__oxpassword->value;
02230         }
02231         return $sHash;
02232     }
02233 
02242     public static function getAdminUser()
02243     {
02244         return self::getActiveUser( true );
02245     }
02246 
02257     public static function getActiveUser( $blForceAdmin = false )
02258     {
02259         $oUser = oxNew( 'oxuser' );
02260         if ( $oUser->loadActiveUser( $blForceAdmin ) ) {
02261             return $oUser;
02262         } else {
02263             return false;
02264         }
02265     }
02266 
02274     public function getOpenIdPassword( $iLength = 25 )
02275     {
02276         $sPassword= "openid_".substr( oxUtilsObject::getInstance()->generateUId(), 0, $iLength);
02277         return $sPassword;
02278     }
02279 
02287     public function getReviewUserHash( $sUserId )
02288     {
02289         $oDb = oxDb::getDb();
02290         $sReviewUserHash = $oDb->getOne('select md5(concat("oxid", oxpassword, oxusername )) from oxuser where oxid = ' . $oDb->quote( $sUserId ) .'');
02291         return $sReviewUserHash;
02292     }
02293 
02301     public function getReviewUserId( $sReviewUserHash )
02302     {
02303         $oDb = oxDb::getDb();
02304         $sUserId = $oDb->getOne('select oxid from oxuser where md5(concat("oxid", oxpassword, oxusername )) = ' . $oDb->quote( $sReviewUserHash ) .'');
02305         return $sUserId;
02306     }
02307 
02313     public function getState()
02314     {
02315         return $this->oxuser__oxstateid->value;
02316     }
02317 
02318 }

Generated by  doxygen 1.6.2