| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?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.
- *
- * @author Iñigo Valentin <i@inigovalentin.com>
- * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
- * @package SWDB
- * @category API
- */
- 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;
- }
- $db = get_context()->get_db();
- // 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->get_name(),
- "level" => $player->get_level(),
- "public" => $player->is_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;
- }
|