GET.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. try{
  18. header("Content-type: application/json; charset=utf-8");
  19. // Get profile ID
  20. /** @var mixed $query Request parameters, from API_CONTROLLER*/
  21. if (count($query) > 1){
  22. $id = $query[0];
  23. }
  24. else{
  25. // Id is mandatory, a list can be retrieved
  26. header("HTTP/1.1 403 A list of profiles cant be retrieved. Please specify a profile ID or player name.");
  27. syslog(LOG_INFO, "[APIv1] HTTP/1.1 403 A list of profiles cant be retrieved. Please specify a profile ID or player name.");
  28. return 403;
  29. }
  30. $db = get_context()->get_db();
  31. // Get player
  32. $statement = $db->prepare("
  33. SELECT
  34. user.api_key AS api_key,
  35. player.id AS id,
  36. player.public AS public
  37. FROM
  38. user,
  39. player
  40. WHERE
  41. user.id = player.user AND
  42. player.id = :id AND
  43. user.api_key = :api_key;
  44. ");
  45. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  46. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  47. if ($r == null){
  48. // No player found
  49. header("HTTP/1.1 404 Player not found.");
  50. syslog(LOG_INFO, "[APIv1] HTTP/1.1 404 Player not found.");
  51. return 403;
  52. }
  53. if ($r["public"] != 1 && $r["key"] != filter_input(INPUT_GET, "key")){
  54. header("HTTP/1.1 403 The profile is not public.");
  55. syslog(LOG_INFO, "[APIv1] HTTP/1.1 403 The profile is not public.");
  56. return 403;
  57. }
  58. // Permission granted, get player data.
  59. $player_id = $r["player_id"];
  60. $player = new Player($player_id);
  61. // Build array
  62. // TODO: Add way more data
  63. $player = [
  64. "username" => $player->get_name(),
  65. "level" => $player->get_level(),
  66. "public" => $player->is_public()
  67. ];
  68. echo(json_encode($player));
  69. header("HTTP/1.1 200 Success.");
  70. syslog(LOG_INFO, "[APIv1] HTTP/1.1 200 Success.");
  71. return 200;
  72. }
  73. catch(Exception $e) {
  74. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  75. syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  76. return 500;
  77. }
  78. ?>