DELETE.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. $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("SELECT COUNT(uid) AS c FROM player WHERE uid = :uid AND api_key = :api_key;");
  43. $statement->bindValue(":uid", $id, SQLITE3_INTEGER);
  44. $statement->bindValue(":api_key", $api_key, SQLITE3_TEXT);
  45. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  46. header("HTTP/1.1 401 Invalid credentials.");
  47. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  48. return 401;
  49. }
  50. $statement = $db->prepare("
  51. DELETE FROM data.player
  52. WHERE
  53. uid = :uid AND
  54. id IN (
  55. SELECT player.id
  56. FROM
  57. data.player player,
  58. data.user user
  59. WHERE
  60. player.user = user.id AND
  61. user.api_key = :api_key
  62. );
  63. ");
  64. $statement->bindValue(":uid", $id, SQLITE3_INTEGER);
  65. $statement->bindValue(":api_key", $api_key, SQLITE3_TEXT);
  66. $statement->execute();
  67. header("HTTP/1.1 204 Profile deleted.");
  68. syslog(LOG_INFO, "[APIv1] HTTP/1.1 204 Profile deleted.");
  69. return 204;
  70. }
  71. catch(Exception $e) {
  72. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  73. syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  74. return 500;
  75. }
  76. ?>