PageRenderTime 44ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/HawkEye Interface/interface.php

https://github.com/Lithixium/HawkEye
PHP | 244 lines | 193 code | 27 blank | 24 comment | 73 complexity | a31fc7598a90e195c6776ae5f1ce8d5a MD5 | raw file
  1. <?php
  2. ///////////////////////////////////////////////////
  3. // HawkEye Interface Search File //
  4. // by oliverw92 //
  5. ///////////////////////////////////////////////////
  6. error_reporting(E_ALL);
  7. session_start();
  8. //Include config, lang pack and MySQL connector
  9. include("config.php");
  10. include("langs/" . $config["langFile"]);
  11. //Set up output array
  12. $output = array(
  13. "error" => "",
  14. "columns" => $lang["results"],
  15. "data" => array()
  16. );
  17. //Check if required functions are here
  18. if (!function_exists("json_decode")) return error("JSON PHP library not installed! Update to PHP 5.3 or later!");
  19. //If not logged in, throw an error
  20. if (!isset($_SESSION["loggedin"]) && $config["password"] != "")
  21. return error($lang["messages"]["notLoggedIn"]);
  22. if (!isset($_GET["data"]))
  23. return error($lang["messages"]["breakMe"]);
  24. $data = json_decode(stripslashes($_GET["data"]), true);
  25. //Get players
  26. $players = array();
  27. $res = mysql_query("SELECT * FROM `" . $config["dbPlayerTable"] . "`");
  28. if (!$res)
  29. return error(mysql_error());
  30. if (mysql_num_rows($res) == 0)
  31. return error($lang["messages"]["noResults"]);
  32. while ($player = mysql_fetch_object($res))
  33. $players[$player->player_id] = $player->player;
  34. //Get worlds
  35. $worlds = array();
  36. $res = mysql_query("SELECT * FROM `" . $config["dbWorldTable"] . "`");
  37. if (!$res)
  38. return error(mysql_error());
  39. if (mysql_num_rows($res) == 0)
  40. return error($lang["messages"]["noResults"]);
  41. while ($world = mysql_fetch_object($res))
  42. $worlds[$world->world_id] = $world->world;
  43. $sql = "SELECT * FROM `" . $config["dbTable"] . "` WHERE ";
  44. $args = array();
  45. if ($data["players"][0] != "") {
  46. $pids = array();
  47. foreach ($data["players"] as $key => $val)
  48. foreach ($players as $key2 => $val2)
  49. if (stristr($val2, $val))
  50. array_push($pids, $key2);
  51. if (count($pids) > 0)
  52. array_push($args, "player_id IN (" . join(",", $pids) . ")");
  53. else
  54. return error($lang["messages"]["noResults"]);
  55. }
  56. if ($data["worlds"][0] != "") {
  57. $wids = array();
  58. foreach ($data["worlds"] as $key => $val)
  59. foreach ($worlds as $key2 => $val2)
  60. if (stristr($val2, $val))
  61. array_push($wids, $key2);
  62. if (count($wids) > 0)
  63. array_push($args, "world_id IN (" . join(",", $wids) . ")");
  64. else
  65. return error($lang["messages"]["noResults"]);
  66. }
  67. if (count($data["actions"]) == 0)
  68. return error($lang["messages"]["noActions"]);
  69. else
  70. array_push($args, "`action` IN (" . join(",", $data["actions"]) . ")");
  71. $range = $config["radius"];
  72. if ($data["range"] != "")
  73. $range = $data["range"];
  74. if ($data["loc"][0] != "")
  75. array_push($args, "(`x` BETWEEN " . ($data["loc"][0] - $range) . " AND " . ($data["loc"][0] + $range) . ")");
  76. if ($data["loc"][1] != "")
  77. array_push($args, "(`y` BETWEEN " . ($data["loc"][1] - $range) . " AND " . ($data["loc"][1] + $range) . ")");
  78. if ($data["loc"][2] != "")
  79. array_push($args, "(`z` BETWEEN " . ($data["loc"][2] - $range) . " AND " . ($data["loc"][2] + $range) . ")");
  80. if ($data["block"] != "00") {
  81. if ($data["keywords"][0] == "")
  82. $data["keywords"][0] = $data["block"];
  83. else
  84. array_push($data["keywords"], $data["block"]);
  85. }
  86. if ($data["dateFrom"] != "" && $data["dateFrom"] != " ")
  87. array_push($args, "`date` >= '" . $data["dateFrom"] . "'");
  88. if ($data["dateTo"] != "" && $data["dateTo"] != " ")
  89. array_push($args, "`date` <= '" . $data["dateTo"] . "'");
  90. if ($data["keywords"][0] != "") {
  91. foreach ($data["keywords"] as $key => $val)
  92. $data["keywords"][$key] = "'%" . $val . "%'";
  93. array_push($args, "`data` LIKE " . join(" OR `data` LIKE ", $data["keywords"]));
  94. }
  95. if ($data["exclude"][0] != "") {
  96. foreach ($data["exclude"] as $key => $val)
  97. $data["exclude"][$key] = "'%" . $val . "%'";
  98. array_push($args, "`data` NOT LIKE " . join(" OR `data` LIKE ", $data["exclude"]));
  99. }
  100. //Compile SQL statement
  101. $sql .= join(" AND ", $args);
  102. if ($config["maxResults"] > 0)
  103. $sql .= " LIMIT " . $config["maxResults"];
  104. //Log query
  105. set_error_handler('handleError');
  106. if ($config["logQueries"] == true) {
  107. try {
  108. if (!file_put_contents("log.txt", date("m.d.y G:i:s") . " - " . $_SERVER["REMOTE_ADDR"] . " - " . $sql . "\n", FILE_APPEND))
  109. return error("Unable to open/write to log.txt!");
  110. } catch (ErrorException $e) {
  111. if (stristr($e, "Warning:"))
  112. return error("Unable to open/write to log.txt!");
  113. }
  114. }
  115. restore_error_handler();
  116. //Run query
  117. $res = mysql_query($sql);
  118. if (!$res)
  119. return error(mysql_error());
  120. $items = explode("\n", file_get_contents("items.txt"));
  121. $results = array();
  122. //Get results from MySQL
  123. while ($entry = mysql_fetch_object($res))
  124. array_push($results, $entry);
  125. foreach ($results as $key => $entry) {
  126. $row = array();
  127. $fdata = $entry->data;
  128. $action = $entry->action;
  129. //Manipulate data according to action
  130. switch ($action) {
  131. case 0:
  132. case 10:
  133. $fdata = getBlockName($fdata);
  134. break;
  135. case 1:
  136. case 19:
  137. case 25:
  138. $arr = explode("-", $fdata);
  139. if (count($arr) > 1)
  140. $fdata = getBlockName($arr[0]) . " replaced by " . getBlockName($arr[1]);
  141. else $fdata = getBlockName($arr[0]);
  142. break;
  143. case 16:
  144. $arr = explode("-", $fdata);
  145. if (count($arr) > 0)
  146. $action = array_shift($arr);
  147. $action .= $entry->plugin . " - ";
  148. $fdata = join("-", $arr);
  149. break;
  150. case 28:
  151. $changeString = "";
  152. $i = 0;
  153. foreach (explode("@", $fdata) as $op) {
  154. $changes = array();
  155. foreach (explode("&", $op) as $change) {
  156. if ($change == "") break;
  157. $item = explode(",", $change);
  158. $changes[] = $item[1] . "x " . getBlockName($item[0]);
  159. }
  160. if ($i == 0 && count($changes) > 0) $changeString .= '<span style="color: green">+(' . trim(implode(", ", $changes)) . ')</span>';
  161. if ($i == 1 && count($changes) > 0) $changeString .= '<span style="color: red">-(' . trim(implode(", ", $changes)) . ')</span>';
  162. $i++;
  163. }
  164. $fdata = $changeString;
  165. break;
  166. case 2:
  167. case 29:
  168. if (strpos("@", $fdata)) {
  169. $fdata = str_replace("|", "<br />", $fdata);
  170. break;
  171. }
  172. $arr = explode("@", $fdata);
  173. if (count($arr) < 3) break;
  174. $lines = explode(",", $arr[2]);
  175. foreach ($lines as $key => $value)
  176. $lines[$key] = base64_decode($value);
  177. $fdata = implode("<br />", $lines);
  178. break;
  179. }
  180. $action = str_replace(array_reverse(array_keys($lang["actions"])), array_reverse($lang["actions"]), $action);
  181. //Add to output row
  182. array_push($row, $entry->data_id, $entry->date, $players[$entry->player_id], $action, $worlds[$entry->world_id], round($entry->x, 1).",".round($entry->y, 1).",".round($entry->z, 1), $fdata);
  183. array_push($output["data"], $row);
  184. }
  185. echo json_encode($output);
  186. /*
  187. // FUNCTION: getBlockName($string);
  188. // Gets block name of block
  189. */
  190. function getBlockName($string) {
  191. global $items;
  192. $parts = explode(":", $string);
  193. foreach ($items as $i) {
  194. $item = explode(",", $i);
  195. if ($item[0] == $parts[0]) {
  196. if (count($parts) == 2)
  197. return $item[1] . ":" . $parts[1];
  198. else return $item[1];
  199. }
  200. }
  201. return $string;
  202. }
  203. /*
  204. // FUNCTION: error($message);
  205. // Displays an error box with the inputted text
  206. */
  207. function error($message) {
  208. global $lang;
  209. $output["error"] = '<div class="ui-widget">
  210. <div class="ui-state-highlight ui-corner-all searchError">
  211. <p><span class="ui-icon ui-icon-alert"></span>
  212. <strong>' . $lang["messages"]["error"] . '</strong> ' . $message . '</p>
  213. </div>
  214. </div>';
  215. echo json_encode($output);
  216. }
  217. ?>