| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- /**
- * Profile getter script.
- *
- * Exposes an API to rerieve a user profile.
- *
- * A lst of profiles can't be retrieved.
- *
- * Mandatory query parameters are:
- * - id: Player ID.
- *
- * Optional GET parameters are.
- * - key: User API key to retrieve a private profile.
- *
- * @category API
- */
- global $db;
- try{
- header("Content-type: application/json; charset=utf-8");
- // Get profile ID
- /** @var mixed $query Request parameters, from API_CONTROLLER*/
- if (count($query) > 1){
- $id = $query[0];
- }
- else{
- // Id is mandatory, a list can be retrieved
- header("HTTP/1.1 403 A list of profiles cant be retrieved. Please specify a profile ID or player name.");
- syslog(LOG_INFO, "[APIv1] HTTP/1.1 403 A list of profiles cant be retrieved. Please specify a profile ID or player name.");
- return 403;
- }
- // Get player
- $statement = $db->prepare("
- SELECT
- user.api_key AS api_key,
- player.id AS id,
- player.public AS public
- FROM
- user,
- player
- WHERE
- user.id = player.user AND
- player.id = :id AND
- user.api_key = :api_key;
- ");
- $statement->bindValue(":id", $id, SQLITE3_INTEGER);
- $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
- if ($r == null){
- // No player found
- header("HTTP/1.1 404 Player not found.");
- syslog(LOG_INFO, "[APIv1] HTTP/1.1 404 Player not found.");
- return 403;
- }
- if ($r["public"] != 1 && $r["key"] != filter_input(INPUT_GET, "key")){
- header("HTTP/1.1 403 The profile is not public.");
- syslog(LOG_INFO, "[APIv1] HTTP/1.1 403 The profile is not public.");
- return 403;
- }
- // Permission granted, get player data.
- $player_id = $r["player_id"];
- $player = new Player($player_id);
- // Build array
- // TODO: Add way more data
- $player = [
- "username" => $player->name,
- "level" => $player->level,
- "public" => $player->public
- ];
- echo(json_encode($player));
- header("HTTP/1.1 200 Success.");
- syslog(LOG_INFO, "[APIv1] HTTP/1.1 200 Success.");
- return 200;
- }
- catch(Exception $e) {
- header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
- syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
- return 500;
- }
- ?>
|