units.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Unit api.
  4. *
  5. * Exposes an API to get a list of units of a player, or a unit details.
  6. * Used POST parameters are:
  7. * - key: User API key. If not, only public user's unit can be seen.
  8. *
  9. * @category API
  10. * @magic $params URL parameters:
  11. * 1: User ID
  12. * 2: Unit ID (optional)
  13. */
  14. global $db;
  15. try{
  16. if (count($params) <= 1){
  17. http_response_code(400);
  18. return 400;
  19. }
  20. $uid = $params[1];
  21. $id = null;
  22. if (count($params) > 2){
  23. $id = $params[2];
  24. }
  25. // Check API key or public profile.
  26. if (isset($_POST['key'])){
  27. $key = filter_input(INPUT_POST, 'key');
  28. }
  29. else{
  30. $key = "";
  31. }
  32. $s = "SELECT COUNT(uid) AS c FROM player WHERE uid = $uid AND (public = 1 OR api_key = '$key');";
  33. if (1 != $db->query($s)->fetchArray(SQLITE3_ASSOC)["c"]){
  34. http_response_code(401);
  35. return 401;
  36. }
  37. // No unit ID specified, show a list.
  38. if ($id == null){
  39. $units = [];
  40. $s = "SELECT id, '" . URL::BASE . "API/v2/units/$uid/' || id AS uri FROM unit WHERE uid = '$uid'";
  41. $q = $db->query($s);
  42. $content = false;
  43. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  44. array_push($units, $r);
  45. $content = true;
  46. }
  47. if ($content == true){
  48. header('Content-Type: application/json');
  49. echo json_encode($units);
  50. http_response_code(200);
  51. return 200;
  52. }
  53. else{
  54. http_response_code(204);
  55. return 204;
  56. }
  57. }
  58. // Specific unit, show details.
  59. else{
  60. $s = "SELECT * FROM unit WHERE uid = '$uid' AND id = '$id'";
  61. $q = $db->query($s);
  62. if($r = $q->fetchArray(SQLITE3_ASSOC)){
  63. $unit = $r;
  64. // Runes
  65. $unit["runes"] = [];
  66. $s = "SELECT * FROM rune WHERE assigned_to = '" . $unit["id"] . "' ORDER BY slot;";
  67. $q = $db->query($s);
  68. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  69. $rune = $r;
  70. unset($rune["uid"]); # Not needed
  71. unset($rune["assigned_to"]); # Not needed
  72. array_push($unit["runes"], $rune);
  73. }
  74. // Skills
  75. $unit["skills"] = [];
  76. $s = "SELECT skill, level FROM unit_skill WHERE unit = '" . $unit["id"] . "';";
  77. $q = $db->query($s);
  78. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  79. array_push($unit["skills"], $r);
  80. }
  81. header('Content-Type: application/json');
  82. echo json_encode($unit);
  83. http_response_code(200);
  84. return 200;
  85. }
  86. else{
  87. http_response_code(404);
  88. return 404;
  89. }
  90. }
  91. }
  92. catch(Exception $e) {
  93. error_log("Unknown error fetching units: " . $e->getMessage());
  94. http_response_code(500);
  95. return 500;
  96. }
  97. ?>