PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/GameEngine/Admin/database.php

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