00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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
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\
00107 {
00108 $this->strUser = $_SERVER['HTTP_WDSMAP_USER'];
00109 $this->strPasswd = $_SERVER['HTTP_WDSMAP_PASSWORD'];
00110
00111
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
00150 if( trim( $strLoginName ) == '' ) return false;
00151
00152
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
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
00216 return true;
00217 }
00218
00219 if( empty( $this->aTowns ) )
00220 {
00221
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
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
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
00291
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
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
00325
00326
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 }