GET.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Profile getter script.
  4. *
  5. * Exposes an API to rerieve a user profile.
  6. *
  7. * A lst of profiles can't be retrieved.
  8. *
  9. * Mandatory query parameters are:
  10. * - id: Player ID.
  11. *
  12. * Optional GET parameters are.
  13. * - key: User API key to retrieve a private profile.
  14. *
  15. * @category API
  16. */
  17. global $db;
  18. try{
  19. header("Content-type: application/json; charset=utf-8");
  20. // Get profile ID
  21. /** @var mixed $query Request parameters, from API_CONTROLLER*/
  22. if (count($query) > 1){
  23. $id = $query[0];
  24. }
  25. else{
  26. // Id is mandatory, a list can be retrieved
  27. header("HTTP/1.1 403 A list of profiles cant be retrieved. Please specify a profile ID or player name.");
  28. syslog(LOG_INFO, "[APIv3] HTTP/1.1 403 A list of profiles cant be retrieved. Please specify a profile ID or player name.");
  29. return 403;
  30. }
  31. // Get player
  32. $statement = $db->prepare("SELECT * FROM player WHERE uid = :uid OR upper(name) = upper(:uid);");
  33. $statement->bindValue(":uid", $id, SQLITE3_INTEGER);
  34. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  35. if ($r == null){
  36. // No player found
  37. header("HTTP/1.1 404 Player not found.");
  38. syslog(LOG_INFO, "[APIv3] HTTP/1.1 404 Player not found.");
  39. return 403;
  40. }
  41. if ($r["public"] != 1 && $r["key"] != filter_input(INPUT_GET, "key")){
  42. header("HTTP/1.1 403 The profile is not public.");
  43. syslog(LOG_INFO, "[APIv3] HTTP/1.1 403 The profile is not public.");
  44. return 403;
  45. }
  46. // Build array
  47. $player = [
  48. "username" => $r["name"],
  49. "level" => $r["level"],
  50. "public" => "1"
  51. ];
  52. echo(json_encode($player));
  53. header("HTTP/1.1 200 Success.");
  54. syslog(LOG_INFO, "[APIv3] HTTP/1.1 200 Success.");
  55. return 200;
  56. }
  57. catch(Exception $e) {
  58. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  59. syslog(LOG_ERR, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  60. return 500;
  61. }
  62. ?>