Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

user.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  This program is free software; you can redistribute it and/or
00004  modify it under the terms of the GNU General Public License
00005  as published by the Free Software Foundation; either version 2
00006  of the License, or (at your option) any later version.
00007 
00008  This program is distributed in the hope that it will be useful,
00009  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  GNU General Public License for more details.
00012 
00013  You should have received a copy of the GNU General Public License
00014  along with this program; if not, write to the Free Software
00015  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00016  ***************************************************************************/
00017 
00022 class User
00023 {
00024     var $oDB;           
00025     var $oLog;          
00026     var $aAdmin;        
00027     var $aAdminArrays;  
00028     var $aRights;       
00029     var $intUserID;
00030     var $strUser;       
00031     var $strPasswd;     
00032     var $booLoggedIn;   
00033     var $cookie_name;   
00034     var $intHomeX;
00035     var $intHomeY;
00036     var $intLastX;
00037     var $intLastY;
00038     var $intZoom;
00039     var $intSizeX;
00040     var $intSizeY;
00041     var $intFog;            
00042     var $intDruidFill;      
00043     var $intDruidBorder;    
00044     var $intUnit;
00045     var $intPlayer;         
00046     var $aTowns;
00047     var $intLimitView;
00048 
00058     function User( &$oDB, &$oLog, &$aAdmin, $aAdminArrays, $strCookie )
00059     {
00060         $this->oDB =& $oDB;
00061         $this->oLog =& $oLog;
00062         $this->aAdmin =& $aAdmin;
00063         $this->aAdminArrays = $aAdminArrays;
00064         $this->cookie_name = $strCookie;
00065         $this->_clearUser();
00066     }
00067 
00068 
00073     function checklogin()
00074     {
00075         $this->booLoggedIn = false;
00076 
00077         // check if cookies are set
00078         if( isset( $_COOKIE[$this->cookie_name]['login']) && isset( $_COOKIE[$this->cookie_name]['pass'] ) )
00079         {
00080             $this->strUser = $_COOKIE[$this->cookie_name]['login'];
00081             $this->strPasswd = $_COOKIE[$this->cookie_name]['pass'];
00082 
00083             // get admin fields in db
00084             $strAdmin = '';
00085             foreach( $this->aAdminArrays AS $strField )
00086             {
00087                 $strAdmin .= ', g.'.$strField;
00088             }
00089 
00090             $this->oDB->query("SELECT p.name AS pname, u.*$strAdmin
00091                                FROM `".$this->oDB->tblUser."` u
00092                                LEFT JOIN ".$this->oDB->tblUsergroup." g USING (usergroup_id)
00093                                LEFT JOIN ".$this->oDB->tblPlayer." p ON (u.player_id=p.player_id)
00094                                WHERE u.`name`='".$this->strUser."' AND u.`pass` = '".$this->strPasswd."'");
00095             $aRow = $this->oDB->fetch_assoc();
00096             if( $aRow['name'] == $this->strUser )
00097             {
00098                 $this->_setUser( $aRow );
00099             }
00100             else
00101             {
00102                 $this->oLog->write( 'login cookies incorrect', 3 );
00103                 $this->logout();
00104             }
00105         }
00106         elseif( isset( $_SERVER['HTTP_WDSMAP_USER'] ) && isset( $_SERVER['HTTP_WDSMAP_PASSWORD'] ) && preg_match( '/^WDSMAP\//ui', $_SERVER['HTTP_USER_AGENT'] ) )
00107         {
00108             $this->strUser = $_SERVER['HTTP_WDSMAP_USER'];
00109             $this->strPasswd = $_SERVER['HTTP_WDSMAP_PASSWORD'];
00110 
00111             // get admin fields in db
00112             $strAdmin = '';
00113             foreach( $this->aAdminArrays AS $strField )
00114             {
00115                 $strAdmin .= ', g.'.$strField;
00116             }
00117 
00118             $this->oDB->query("SELECT p.name AS pname, u.*$strAdmin
00119                                FROM `".$this->oDB->tblUser."` u
00120                                LEFT JOIN ".$this->oDB->tblUsergroup." g USING (usergroup_id)
00121                                LEFT JOIN ".$this->oDB->tblPlayer." p ON (u.player_id=p.player_id)
00122                                WHERE u.`name`='".$this->strUser."' AND u.`pass` = '".$this->strPasswd."'");
00123             $aRow = $this->oDB->fetch_assoc();
00124             if( $aRow['name'] == $this->strUser )
00125             {
00126                 $this->_setUser( $aRow );
00127             }
00128             else
00129             {
00130                 header("phpmap-ReturnCode: 1");
00131                 header("phpmap-ErrorMessage: Invalid login");
00132                 die();
00133             }
00134         }
00135 
00136         return( $this->booLoggedIn );
00137     }
00138 
00145     function login( $strLoginName, $strPass )
00146     {
00147         $this->booLoggedIn = false;
00148 
00149         // check for empty name
00150         if( trim( $strLoginName ) == '' ) return false;
00151 
00152         // get admin fields in db
00153         $strAdmin = '';
00154         foreach( $this->aAdminArrays AS $strField )
00155         {
00156             $strAdmin .= ', g.'.$strField;
00157         }
00158 
00159         $this->oDB->query("SELECT p.name AS pname, u.*$strAdmin
00160                            FROM `".$this->oDB->tblUser."` u
00161                            LEFT JOIN ".$this->oDB->tblUsergroup." g USING (usergroup_id)
00162                            LEFT JOIN ".$this->oDB->tblPlayer." p ON (u.player_id=p.player_id)
00163                            WHERE u.`name`='".$strLoginName."' AND u.`pass`='".md5($strPass)."';");
00164 
00165         $aRow = $this->oDB->fetch_assoc();
00166         if( $aRow['name'] == $strLoginName )
00167         {
00168             $this->strUser = $aRow['name'];
00169             $this->strPasswd = $aRow['pass'];
00170             $this->_setUser( $aRow );
00171         }
00172 
00173         return( $this->booLoggedIn );
00174     }
00175 
00179     function logout()
00180     {
00181         setcookie( $this->cookie_name . '[login]', '', -1000 );
00182         setcookie( $this->cookie_name . '[pass]',  '', -1000 );
00183         $this->oLog->write( 'logout', 3 );
00184         $this->_clearUser();
00185     }
00186 
00192     function isAdmin( $strRight )
00193     {
00194         // check in which array this right is set
00195         foreach( $this->aAdminArrays AS $intKey => $strField )
00196         {
00197             if( isset( $this->aAdmin[$intKey][$strRight] ) && isset( $this->aRights[$intKey] ) )
00198             {
00199                 return( ( $this->aRights[$intKey] & $this->aAdmin[$intKey][$strRight] ) > 0 );
00200             }
00201         }
00202         return false;
00203     }
00204 
00211     function check( $x, $y )
00212     {
00213         if( $this->intLimitView == 0 )
00214         {
00215             // the user's view is not limited
00216             return true;
00217         }
00218 
00219         if( empty( $this->aTowns ) )
00220         {
00221             // check x and y
00222             return ( $x > ( $this->intHomeX - $this->intLimitView) )
00223                 && ( $x < ( $this->intHomeX + $this->intLimitView) )
00224                 && ( $y > ( $this->intHomeY - $this->intLimitView) )
00225                 && ( $y < ( $this->intHomeY + $this->intLimitView) );
00226         }
00227 
00228         $booReturn = false;
00229         // check view for each of his towns
00230         foreach( $this->aTowns AS $aTown )
00231         {
00232             $booReturn = $booReturn || ( $x > ( $aTown['x']- $this->intLimitView) )
00233                 && ( $x < ( $aTown['x'] + $this->intLimitView) )
00234                 && ( $y > ( $aTown['y'] - $this->intLimitView) )
00235                 && ( $y < ( $aTown['y'] + $this->intLimitView) );
00236         }
00237         return $booReturn;
00238 
00239     }
00240 
00245     function giveRight( $strRight )
00246     {
00247         // check in which array this right is set
00248         foreach( $this->aAdminArrays AS $intKey => $strField )
00249         {
00250             if( isset( $this->aAdmin[$intKey][$strRight] ) )
00251             {
00252                 $this->aRights[$intKey] += $this->aAdmin[$intKey][$strRight];
00253             }
00254         }
00255     }
00256 
00260     function _clearUser()
00261     {
00262         $this->booLoggedIn = false;
00263         $this->intUserID      = -1;
00264         $this->strUser        = '';
00265         $this->strPasswd      = '';
00266         $this->intHomeX       = 0;
00267         $this->intHomeY       = 0;
00268         $this->intLastX       = 0;
00269         $this->intLastY       = 0;
00270         $this->intZoom        = 0;
00271         $this->intSizeX       = 640;
00272         $this->intSizeY       = 320;
00273         $this->intFog         = 0;
00274         $this->intDruidFill   = 1;
00275         $this->intDruidBorder = 1;
00276         $this->intUnit        = 0;
00277         $this->intLimitView   = 0;
00278         $this->intPlayer      = 0;
00279         $this->aRights        = array();
00280     }
00281 
00286     function _setUser( $aRow )
00287     {
00288         $this->booLoggedIn    = true;
00289         $this->intUserID      = $aRow['user_id'];
00290         //  $this->strUser     = ...            user and password are already set
00291         //  $this->strPasswd   = ...
00292         $this->intHomeX     = $aRow['home_x'];
00293         $this->intHomeY     = $aRow['home_y'];
00294         $this->intLastX     = $aRow['last_x'];
00295         $this->intLastY     = $aRow['last_y'];
00296         $this->intZoom      = $aRow['zoom_last'];
00297         $this->intSizeX     = $aRow['size_x'];
00298         $this->intSizeY     = $aRow['size_y'];
00299         $this->intFog       = $aRow['show_fog'];
00300         $this->intTownFill  = $aRow['show_town_fill'];
00301         $this->intTownSight = $aRow['show_town_sight'];
00302         $this->intUnit      = $aRow['show_unit'];
00303         $this->intPlayer    = $aRow['player_id'];
00304         $this->strPlayer    = $aRow['pname'];
00305 
00306         $this->aTowns = array();
00307         if( $this->intPlayer > 1 )
00308         {
00309             $sqlResult = $this->oDB->query( "SELECT town_id, player_id, x, y, name FROM ".$this->oDB->tblTown." WHERE player_id='".$this->intPlayer."';" );
00310             while( $aRow2 = $this->oDB->fetch_assoc( $sqlResult ) )
00311             {
00312                 $this->aTowns[] = $aRow2;
00313             }
00314         }
00315 
00316         // set admin rights
00317         foreach( $this->aAdminArrays AS $intKey => $strField )
00318         {
00319             $this->aRights[$intKey] = $aRow[$strField];
00320         }
00321         $this->intLimitView = $this->isAdmin( 'limit_view' ) ? 50 : 0;
00322 
00323         $this->oDB->query("UPDATE ".$this->oDB->tblUser." SET lastlogin = ".time()." WHERE user_id='".$this->intUserID."'");
00324         // Fehlerbehandlung
00325 
00326         // user is valid, set cookie for 7 days
00327         setcookie( $this->cookie_name . '[login]', $aRow['name'], time()+7*24*60*60);
00328         setcookie( $this->cookie_name . '[pass]',  $aRow['pass'], time()+7*24*60*60);
00329     }
00330 }

Generated on Sun May 8 19:29:45 2005 for PhpMap by  doxygen 1.4.2