DELETE.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Profile deleter script.
  4. *
  5. * Exposes an API to update an existing profile.
  6. *
  7. * It also saves the data to a JSON file in the data directory.
  8. *
  9. * Mandatory query parameters are:
  10. * - id: Player ID.
  11. *
  12. * Mandatory DELETE parameters are:
  13. * - key: User API key.
  14. *
  15. * @category API
  16. */
  17. global $db;
  18. try{
  19. header("Content-type: application/json; charset=utf-8");
  20. // Get put data
  21. $_DELETE = [];
  22. parse_str(file_get_contents("php://input"), $_DELETE);
  23. // Get profile ID
  24. /** @var mixed $query Request parameters, from API_CONTROLLER*/
  25. if (count($query) > 0){
  26. $player_id = $query[0];
  27. }
  28. else{
  29. // ID is mandatory
  30. header("HTTP/1.1 400 User ID not received.");
  31. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 User ID not received.");
  32. return 400;
  33. }
  34. // Check API key.
  35. $api_key = $_DELETE["key"];
  36. if ($api_key == null || $api_key == false){
  37. header("HTTP/1.1 400 API key not received.");
  38. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 API key not received.");
  39. return 400;
  40. }
  41. // Authenticate
  42. $statement = $db->prepare("
  43. SELECT COUNT(player.id) AS c
  44. FROM
  45. user,
  46. player
  47. WHERE
  48. user.id = player.user AND
  49. player.id = :id AND
  50. user.api_key = :api_key;
  51. ");
  52. $statement->bindValue(":id", $player_id, SQLITE3_INTEGER);
  53. $statement->bindValue(":api_key", $api_key, SQLITE3_TEXT);
  54. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  55. header("HTTP/1.1 401 Invalid credentials.");
  56. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  57. return 401;
  58. }
  59. // Delete all player data
  60. $db->query("PRAGMA foreign_keys = OFF;");
  61. $statement = $db->prepare(
  62. "DELETE FROM unit_skill
  63. WHERE unit IN (SELECT id FROM unit WHERE player = :player);
  64. ");
  65. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  66. $statement->execute();
  67. $statement = $db->prepare("
  68. DELETE FROM unit_rune
  69. WHERE unit IN (SELECT id FROM unit WHERE player = :player);
  70. ");
  71. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  72. $statement->execute();
  73. $statement = $db->prepare("
  74. DELETE FROM rune_stat
  75. WHERE rune IN (SELECT id FROM rune WHERE player = :player);
  76. ");
  77. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  78. $statement->execute();
  79. $statement = $db->prepare("
  80. DELETE FROM unit_artifact
  81. WHERE unit IN (SELECT id FROM unit WHERE player = :player);n ");
  82. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  83. $statement->execute();
  84. $statement = $db->prepare("
  85. DELETE FROM artifact_stat
  86. WHERE artifact IN (SELECT id FROM artifact WHERE player = :player);n ");
  87. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  88. $statement->execute();
  89. $statement = $db->prepare("
  90. DELETE FROM artifact_effect
  91. WHERE artifact IN (SELECT id FROM artifact WHERE player = :player);n ");
  92. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  93. $statement->execute();
  94. $tables_to_clear = [
  95. "defense",
  96. "unit",
  97. "rune",
  98. "artifact",
  99. "building",
  100. "decoration",
  101. "item",
  102. "enchantment"
  103. ];
  104. foreach ($tables_to_clear as $table){
  105. $statement = $db->prepare("DELETE FROM $table WHERE player = :player;");
  106. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  107. $statement->execute();
  108. }
  109. $statement = $db->prepare("
  110. DELETE FROM player
  111. WHERE id = :id
  112. ");
  113. $statement->bindValue(":id", $player_id, SQLITE3_INTEGER);
  114. $statement->execute();
  115. header("HTTP/1.1 204 Profile deleted.");
  116. syslog(LOG_INFO, "[APIv1] HTTP/1.1 204 Profile deleted.");
  117. return 204;
  118. }
  119. catch(Exception $e) {
  120. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  121. syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  122. return 500;
  123. }
  124. ?>