DELETE.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, "[APIv3] 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, "[APIv3] 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, "[APIv3] HTTP/1.1 401 Invalid credentials.");
  48. return 401;
  49. }
  50. $statement = $db->prepare("
  51. DELETE FROM player
  52. WHERE
  53. uid = :uid AND
  54. api_key, = :api_key;
  55. ");
  56. $statement->bindValue(":uid", $id, SQLITE3_INTEGER);
  57. $statement->bindValue(":api_key", $api_key, SQLITE3_TEXT);
  58. $statement->execute();
  59. header("HTTP/1.1 204 Profile deleted.");
  60. syslog(LOG_INFO, "[APIv3] HTTP/1.1 204 Profile deleted.");
  61. return 204;
  62. }
  63. catch(Exception $e) {
  64. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  65. syslog(LOG_ERR, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  66. return 500;
  67. }
  68. ?>