DELETE.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. parse_str(file_get_contents("php://input"), $_DELETE);
  22. // Get profile ID
  23. // $query heredated from API_CONTROLLER
  24. if (count($query) > 0){
  25. $id = $query[0];
  26. }
  27. else{
  28. // ID is mandatory
  29. header("HTTP/1.1 400 User ID not received.");
  30. syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 User ID not received.");
  31. return 400;
  32. }
  33. // Check API key.
  34. $api_key = $_DELETE["key"];
  35. if ($api_key == null || $api_key == false){
  36. header("HTTP/1.1 400 API key not received.");
  37. syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 API key not received.");
  38. return 400;
  39. }
  40. // Authenticate
  41. $statement = $db->prepare("SELECT COUNT(uid) AS c FROM player WHERE uid = :uid AND api_key = :api_key;");
  42. $statement->bindValue(":uid", $uid);
  43. $statement->bindValue(":api_key", $api_key);
  44. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  45. header("HTTP/1.1 401 Invalid credentials.");
  46. syslog(LOG_INFO, "[APIv3] HTTP/1.1 401 Invalid credentials.");
  47. return 401;
  48. }
  49. $statement = $db->prepare("
  50. DELETE FROM player
  51. WHERE
  52. uid = :uid AND
  53. api_key, = :api_key;
  54. ");
  55. $statement->bindValue(":uid", $uid);
  56. $statement->bindValue(":api_key", $api_key);
  57. $statement->execute();
  58. header("HTTP/1.1 204 Profile deleted.");
  59. syslog(LOG_INFO, "[APIv3] HTTP/1.1 204 Profile deleted.");
  60. return 204;
  61. }
  62. catch(Exception $e) {
  63. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  64. syslog(LOG_ERR, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  65. return 500;
  66. }
  67. ?>