PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/GameEngine/Admin/database.php

https://github.com/byou2711/TravianB
PHP | 317 lines | 258 code | 33 blank | 26 comment | 34 complexity | f522fc0faccf7980fd4b1aa342bbbbcc MD5 | raw file
  1. <?php
  2. #################################################################################
  3. ## ##
  4. ## -= YOU MUST NOT REMOVE OR CHANGE THIS NOTICE =- ##
  5. ## ##
  6. ## --------------------------------------------------------------------------- ##
  7. ## ##
  8. ## Project: ZravianX ##
  9. ## Version: 2011.11.07 ##
  10. ## Filename: GameEngine/Admin/database.php ##
  11. ## Developed by: Dzoki ##
  12. ## Edited by: ZZJHONS ##
  13. ## License: Creative Commons BY-NC-SA 3.0 ##
  14. ## Copyright: ZravianX (c) 2011 - All rights reserved ##
  15. ## URLs: http://zravianx.zzjhons.com ##
  16. ## Source code: http://www.github.com/ZZJHONS/ZravianX ##
  17. ## ##
  18. #################################################################################
  19. include("../GameEngine/config.php");
  20. include("../GameEngine/Data/buidata.php");
  21. class adm_DB {
  22. var $connection;
  23. function adm_DB(){
  24. global $database;
  25. $this->connection = mysql_connect(SQL_SERVER, SQL_USER, SQL_PASS) or die(mysql_error());
  26. mysql_select_db(SQL_DB, $this->connection) or die(mysql_error());
  27. }
  28. function Login($username,$password){
  29. $q = "SELECT password FROM ".TB_PREFIX."users where username = '$username' and access >= ".MULTIHUNTER;
  30. $result = mysql_query($q, $this->connection);
  31. $dbarray = mysql_fetch_array($result);
  32. if($dbarray['password'] == md5($password)) {
  33. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,'X','$username logged in (IP: <b>".$_SERVER['REMOTE_ADDR']."</b>)',".time().")");
  34. return true;
  35. }
  36. else {
  37. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,'X','<font color=\'red\'><b>IP: ".$_SERVER['REMOTE_ADDR']." tried to log in with username <u> $username</u> but access was denied!</font></b>',".time().")");
  38. return false;
  39. }
  40. }
  41. function recountPopUser($uid){
  42. global $database;
  43. $villages = $database->getProfileVillages($uid);
  44. for ($i = 0; $i <= count($villages)-1; $i++) {
  45. $vid = $villages[$i]['wref'];
  46. $this->recountPop($vid);
  47. }
  48. }
  49. function recountPop($vid){
  50. global $database;
  51. $fdata = $database->getResourceLevel($vid);
  52. $popTot = 0;
  53. for ($i = 1; $i <= 40; $i++) {
  54. $lvl = $fdata["f".$i];
  55. $building = $fdata["f".$i."t"];
  56. if($building){
  57. $popTot += $this->buildingPOP($building,$lvl);
  58. }
  59. }
  60. $q = "UPDATE ".TB_PREFIX."vdata set pop = $popTot where wref = $vid";
  61. mysql_query($q, $this->connection);
  62. }
  63. function buildingPOP($f,$lvl){
  64. $name = "bid".$f;
  65. global $$name;
  66. $popT = 0;
  67. $dataarray = $$name;
  68. for ($i = 0; $i <= $lvl; $i++) {
  69. $popT += $dataarray[$i]['pop'];
  70. }
  71. return $popT;
  72. }
  73. function getWref($x,$y) {
  74. $q = "SELECT id FROM ".TB_PREFIX."wdata where x = $x and y = $y";
  75. $result = mysql_query($q, $this->connection);
  76. $r = mysql_fetch_array($result);
  77. return $r['id'];
  78. }
  79. function AddVillage($post){
  80. global $database;
  81. $wid = $this->getWref($post['x'],$post['y']);
  82. $uid = $post['uid'];
  83. $status = $database->getVillageState($wid);
  84. $status = 0;
  85. if($status == 0){
  86. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,".$_SESSION['id'].",'Added new village <b><a href=\'admin.php?p=village&did=$wid\'>$wid</a></b> to user <b><a href=\'admin.php?p=player&uid=$uid\'>$uid</a></b>',".time().")");
  87. $database->setFieldTaken($wid);
  88. $database->addVillage($wid,$uid,'new village','0');
  89. $database->addResourceFields($wid,$database->getVillageType($wid));
  90. $database->addUnits($wid);
  91. $database->addTech($wid);
  92. $database->addABTech($wid);
  93. }
  94. }
  95. function Punish($post){
  96. global $database;
  97. $villages = $database->getProfileVillages($post['uid']);
  98. $admid = $post['admid'];
  99. $user = $database->getUserArray($post['uid'],1);
  100. for ($i = 0; $i <= count($villages)-1; $i++) {
  101. $vid = $villages[$i]['wref'];
  102. if($post['punish']){
  103. $popOld = $villages[$i]['pop'];
  104. $proc = 100-$post['punish'];
  105. $pop = floor(($popOld/100)*($proc));
  106. if($pop <= 1 ){$pop = 2;}
  107. $this->PunishBuilding($vid,$proc,$pop);
  108. }
  109. if($post['del_troop']){
  110. if($user['tribe'] == 1) {
  111. $unit = 1;
  112. }else if($user['tribe'] == 2) {
  113. $unit = 11;
  114. }else if($user['tribe'] == 3) {
  115. $unit = 21;
  116. }
  117. $this->DelUnits($villages[$i]['wref'],$unit);
  118. }
  119. if($post['clean_ware']){
  120. $time = time();
  121. $q = "UPDATE ".TB_PREFIX."vdata SET `wood` = '0', `clay` = '0', `iron` = '0', `crop` = '0', `lastupdate` = '$time' WHERE wref = $vid;";
  122. mysql_query($q, $this->connection);
  123. }
  124. }
  125. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,".$_SESSION['id'].",'Punished user: <a href=\'admin.php?p=player&uid=".$post['uid']."\'>".$post['uid']."</a> with <b>-".$post['punish']."%</b> population',".time().")");
  126. }
  127. function PunishBuilding($vid,$proc,$pop){
  128. global $database;
  129. $q = "UPDATE ".TB_PREFIX."vdata set pop = $pop where wref = $vid;";
  130. mysql_query($q, $this->connection);
  131. $fdata = $database->getResourceLevel($vid);
  132. for ($i = 1; $i <= 40; $i++) {
  133. if($fdata['f'.$i]>1){
  134. $zm = ($fdata['f'.$i]/100)*$proc;
  135. if($zm < 1){$zm = 1;}else{$zm = floor($zm);}
  136. $q = "UPDATE ".TB_PREFIX."fdata SET `f$i` = '$zm' WHERE `vref` = $vid;";
  137. mysql_query($q, $this->connection);
  138. }
  139. }
  140. }
  141. function DelUnits($vid,$unit){
  142. for ($i = $unit; $i <= 9+$unit; $i++) {
  143. $this->DelUnits2($vid,$unit);
  144. }
  145. }
  146. function DelUnits2($vid,$unit){
  147. $q = "UPDATE ".TB_PREFIX."units SET `u$unit` = '0' WHERE `vref` = $vid;";
  148. mysql_query($q, $this->connection);
  149. }
  150. function DelPlayer($uid,$pass){
  151. global $database;
  152. $ID = $_SESSION['id'];//$database->getUserField($_SESSION['username'],'id',1);
  153. if($this->CheckPass($pass,$ID)){
  154. $villages = $database->getProfileVillages($uid);
  155. for ($i = 0; $i <= count($villages)-1; $i++) {
  156. $this->DelVillage($villages[$i]['wref']);
  157. }
  158. $name = $database->getUserField($uid,"username",0);
  159. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,$ID,'Deleted user <a>$name</a>',".time().")");
  160. $q = "DELETE FROM ".TB_PREFIX."users WHERE `id` = $uid;";
  161. mysql_query($q, $this->connection);
  162. }
  163. }
  164. function getUserActive() {
  165. $time = time() - (60*5);
  166. $q = "SELECT * FROM ".TB_PREFIX."users where timestamp > $time and username != 'support'";
  167. $result = mysql_query($q, $this->connection);
  168. return $this->mysql_fetch_all($result);
  169. }
  170. function CheckPass($password,$uid){
  171. $q = "SELECT password FROM ".TB_PREFIX."users where id = '$uid' and access = ".ADMIN;
  172. $result = mysql_query($q, $this->connection);
  173. $dbarray = mysql_fetch_array($result);
  174. if($dbarray['password'] == md5($password)) {
  175. return true;
  176. }else{
  177. return false;
  178. }
  179. }
  180. function DelVillage($wref){
  181. $q = "SELECT * FROM ".TB_PREFIX."vdata WHERE `wref` = $wref";
  182. $result = mysql_query($q, $this->connection);
  183. if(mysql_num_rows($result) > 0){
  184. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,".$_SESSION['id'].",'Deleted village <b>$wref</b>',".time().")");
  185. $q = "DELETE FROM ".TB_PREFIX."vdata WHERE `wref` = $wref";
  186. mysql_query($q, $this->connection);
  187. $q = "DELETE FROM ".TB_PREFIX."units WHERE `vref` = $wref";
  188. mysql_query($q, $this->connection);
  189. $q = "DELETE FROM ".TB_PREFIX."bdata WHERE `wid` = $wref";
  190. mysql_query($q, $this->connection);
  191. $q = "DELETE FROM ".TB_PREFIX."abdata WHERE `wid` = $wref";
  192. mysql_query($q, $this->connection);
  193. $q = "DELETE FROM ".TB_PREFIX."fdata WHERE `vref` = $wref";
  194. mysql_query($q, $this->connection);
  195. $q = "DELETE FROM ".TB_PREFIX."training WHERE `vref` = $wref";
  196. mysql_query($q, $this->connection);
  197. $q = "DELETE FROM ".TB_PREFIX."movement WHERE `from` = $wref";
  198. mysql_query($q, $this->connection);
  199. $q = "UPDATE ".TB_PREFIX."wdata SET `occupied` = '0' WHERE `id` = $wref";
  200. mysql_query($q, $this->connection);
  201. }
  202. }
  203. function DelBan($uid,$id){
  204. global $database;
  205. $name = $database->getUserField($uid,"username",0);
  206. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,".$_SESSION['id'].",'Unbanned user <a href=\'admin.php?p=player&uid=$uid\'>$name</a>',".time().")");
  207. $q = "UPDATE ".TB_PREFIX."users SET `access` = '".USER."' WHERE `id` = $uid;";
  208. mysql_query($q, $this->connection);
  209. $q = "UPDATE ".TB_PREFIX."banlist SET `active` = '0' WHERE `id` = $id;";
  210. mysql_query($q, $this->connection);
  211. }
  212. function AddBan($uid,$end,$reason){
  213. global $database;
  214. $name = $database->getUserField($uid,"username",0);
  215. mysql_query("Insert into ".TB_PREFIX."admin_log values (0,".$_SESSION['id'].",'Banned user <a href=\'admin.php?p=player&uid=$uid\'>$name</a>',".time().")");
  216. $q = "UPDATE ".TB_PREFIX."users SET `access` = '0' WHERE `id` = $uid;";
  217. mysql_query($q, $this->connection);
  218. $time = time();
  219. $admin = $_SESSION['id']; //$database->getUserField($_SESSION['username'],'id',1);
  220. $name = $database->getUserField($uid,'username',0);
  221. $q = "INSERT INTO ".TB_PREFIX."banlist (`uid`, `name`, `reason`, `time`, `end`, `admin`, `active`) VALUES ($uid, '$name' , '$reason', '$time', '$end', '$admin', '1');";
  222. mysql_query($q, $this->connection);
  223. }
  224. function search_player($player){
  225. $q = "SELECT id,username FROM ".TB_PREFIX."users WHERE `username` LIKE '%$player%' and username != 'support'";
  226. $result = mysql_query($q, $this->connection);
  227. return $this->mysql_fetch_all($result);
  228. }
  229. function search_email($email){
  230. $q = "SELECT id,email FROM ".TB_PREFIX."users WHERE `email` LIKE '%$email%' and username != 'support'";
  231. $result = mysql_query($q, $this->connection);
  232. return $this->mysql_fetch_all($result);
  233. }
  234. function search_village($village){
  235. $q = "SELECT * FROM ".TB_PREFIX."vdata WHERE `name` LIKE '%$village%' or `wref` LIKE '%$village%'";
  236. $result = mysql_query($q, $this->connection);
  237. return $this->mysql_fetch_all($result);
  238. }
  239. function search_alliance($alliance){
  240. $q = "SELECT * FROM ".TB_PREFIX."alidata WHERE `name` LIKE '%$alliance%' or `tag` LIKE '%$alliance%' or `id` LIKE '%$alliance%'";
  241. $result = mysql_query($q, $this->connection);
  242. return $this->mysql_fetch_all($result);
  243. }
  244. function search_ip($ip){
  245. $q = "SELECT * FROM ".TB_PREFIX."login_log WHERE `ip` LIKE '%$ip%'";
  246. $result = mysql_query($q, $this->connection);
  247. return $this->mysql_fetch_all($result);
  248. }
  249. function search_banned(){
  250. $q = "SELECT * FROM ".TB_PREFIX."banlist where active = '1'";
  251. $result = mysql_query($q, $this->connection);
  252. return $this->mysql_fetch_all($result);
  253. }
  254. function Del_banned(){
  255. //$q = "SELECT * FROM ".TB_PREFIX."banlist";
  256. $result = mysql_query($q, $this->connection);
  257. return $this->mysql_fetch_all($result);
  258. }
  259. /***************************
  260. Function to process MYSQLi->fetch_all (Only exist in MYSQL)
  261. References: Result
  262. ***************************/
  263. function mysql_fetch_all($result) {
  264. $all = array();
  265. if($result) {
  266. while ($row = mysql_fetch_assoc($result)){ $all[] = $row; }
  267. return $all;
  268. }
  269. }
  270. function query_return($q) {
  271. $result = mysql_query($q, $this->connection);
  272. return $this->mysql_fetch_all($result);
  273. }
  274. /***************************
  275. Function to do free query
  276. References: Query
  277. ***************************/
  278. function query($query) {
  279. return mysql_query($query, $this->connection);
  280. }
  281. };
  282. $admin = new adm_DB;
  283. include("function.php");
  284. ?>