| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?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, "[APIv3] 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 * FROM player WHERE uid = :uid OR upper(name) = upper(:uid);");
- $statement->bindValue(":uid", $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, "[APIv3] 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, "[APIv3] HTTP/1.1 403 The profile is not public.");
- return 403;
- }
- // Build array
- $player = [
- "username" => $r["name"],
- "level" => $r["level"],
- "public" => "1"
- ];
- echo(json_encode($player));
- header("HTTP/1.1 200 Success.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 200 Success.");
- return 200;
- }
- catch(Exception $e) {
- header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
- syslog(LOG_ERR, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
- return 500;
- }
- ?>
|