DELETE.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * @author Iñigo Valentin <i@inigovalentin.com>
  16. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  17. * @package SWDB
  18. * @category API
  19. */
  20. try{
  21. header("Content-type: application/json; charset=utf-8");
  22. // Get put data
  23. $_DELETE = [];
  24. parse_str(file_get_contents("php://input"), $_DELETE);
  25. // Get profile ID
  26. /** @var mixed $query Request parameters, from API_CONTROLLER*/
  27. if (count($query) > 0){
  28. $player_id = $query[0];
  29. }
  30. else{
  31. // ID is mandatory
  32. header("HTTP/1.1 400 User ID not received.");
  33. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 User ID not received.");
  34. return 400;
  35. }
  36. // Check API key.
  37. $api_key = $_DELETE["key"];
  38. if ($api_key == null || $api_key == false){
  39. header("HTTP/1.1 400 API key not received.");
  40. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 API key not received.");
  41. return 400;
  42. }
  43. $db = get_context()->get_db();
  44. // Authenticate
  45. $statement = $db->prepare("
  46. SELECT COUNT(player.id) AS c
  47. FROM
  48. user,
  49. player
  50. WHERE
  51. user.id = player.user AND
  52. player.id = :id AND
  53. user.api_key = :api_key;
  54. ");
  55. $statement->bindValue(":id", $player_id, SQLITE3_INTEGER);
  56. $statement->bindValue(":api_key", $api_key, SQLITE3_TEXT);
  57. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  58. header("HTTP/1.1 401 Invalid credentials.");
  59. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  60. return 401;
  61. }
  62. // Delete all player data
  63. $db->query("PRAGMA foreign_keys = OFF;");
  64. $statement = $db->prepare(
  65. "DELETE FROM unit_skill
  66. WHERE unit IN (SELECT id FROM unit WHERE player = :player);
  67. ");
  68. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  69. $statement->execute();
  70. $statement = $db->prepare("
  71. DELETE FROM unit_rune
  72. WHERE unit IN (SELECT id FROM unit WHERE player = :player);
  73. ");
  74. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  75. $statement->execute();
  76. $statement = $db->prepare("
  77. DELETE FROM rune_stat
  78. WHERE rune IN (SELECT id FROM rune WHERE player = :player);
  79. ");
  80. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  81. $statement->execute();
  82. $statement = $db->prepare("
  83. DELETE FROM unit_artifact
  84. WHERE unit IN (SELECT id FROM unit WHERE player = :player);n ");
  85. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  86. $statement->execute();
  87. $statement = $db->prepare("
  88. DELETE FROM artifact_stat
  89. WHERE artifact IN (SELECT id FROM artifact WHERE player = :player);n ");
  90. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  91. $statement->execute();
  92. $statement = $db->prepare("
  93. DELETE FROM artifact_effect
  94. WHERE artifact IN (SELECT id FROM artifact WHERE player = :player);n ");
  95. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  96. $statement->execute();
  97. $tables_to_clear = [
  98. "defense",
  99. "unit",
  100. "rune",
  101. "artifact",
  102. "building",
  103. "decoration",
  104. "item",
  105. "enchantment"
  106. ];
  107. foreach ($tables_to_clear as $table){
  108. $statement = $db->prepare("DELETE FROM $table WHERE player = :player;");
  109. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  110. $statement->execute();
  111. }
  112. $statement = $db->prepare("
  113. DELETE FROM player
  114. WHERE id = :id
  115. ");
  116. $statement->bindValue(":id", $player_id, SQLITE3_INTEGER);
  117. $statement->execute();
  118. header("HTTP/1.1 204 Profile deleted.");
  119. syslog(LOG_INFO, "[APIv1] HTTP/1.1 204 Profile deleted.");
  120. return 204;
  121. }
  122. catch(Exception $e) {
  123. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  124. syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  125. return 500;
  126. }