1f20900f6d40e063d388f9069b6ea883770578ba
[stouyapi.git] / www / api / v1 / gamers / me.php
1 <?php
2 /**
3  * Return user data with dynamic username that has been saved during login
4  *
5  * @author Christian Weiske <cweiske@cweiske.de>
6  * @see    api/v1/sessions
7  */
8 $dbFile  = __DIR__ . '/../../../../data/usernames.sqlite3';
9
10 $ip = $_SERVER['REMOTE_ADDR'];
11 if ($ip == '') {
12     //empty ip
13     header('X-Fail-Reason: empty ip address');
14     header('Content-type: application/json');
15     echo file_get_contents('me.json');
16     exit(1);
17 }
18
19 try {
20     $db = new SQLite3($dbFile, SQLITE3_OPEN_READONLY);
21 } catch (Exception $e) {
22     //db file not found
23     header('X-Fail-Reason: database file not found');
24     header('Content-type: application/json');
25     echo file_get_contents('me.json');
26     exit(1);
27 }
28
29 $stmt = $db->prepare('SELECT * FROM usernames WHERE ip = :ip');
30 $stmt->bindValue(':ip', $ip);
31 $res = $stmt->execute();
32 $row = $res->fetchArray(SQLITE3_ASSOC);
33 $res->finalize();
34
35 if ($row === false) {
36     header('Content-type: application/json');
37     echo file_get_contents('me.json');
38     exit();
39 }
40
41 $data = json_decode(file_get_contents('me.json'));
42 $data->gamer->username = $row['username'];
43
44 switch (strtolower($row['username'])) {
45 case 'cweiske':
46 case 'szeraax':
47     $data->gamer->founder = true;
48 }
49
50 header('Content-type: application/json');
51 echo json_encode($data) . "\n";
52 ?>