Unify domain names in README
[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 $cfgFile = __DIR__ . '/../../../../config.php';
10 if (file_exists($cfgFile)) {
11     include $cfgFile;
12 }
13
14
15 $ip = $_SERVER['REMOTE_ADDR'];
16 if ($ip == '') {
17     //empty ip
18     header('X-Fail-Reason: empty ip address');
19     header('Content-type: application/json');
20     echo file_get_contents('me.json');
21     exit(1);
22 }
23
24 try {
25     $db = new SQLite3($dbFile, SQLITE3_OPEN_READONLY);
26 } catch (Exception $e) {
27     //db file not found
28     header('X-Fail-Reason: database file not found');
29     header('Content-type: application/json');
30     echo file_get_contents('me.json');
31     exit(1);
32 }
33
34 $stmt = $db->prepare('SELECT * FROM usernames WHERE ip = :ip');
35 $stmt->bindValue(':ip', $ip);
36 $res = $stmt->execute();
37 $row = $res->fetchArray(SQLITE3_ASSOC);
38 $res->finalize();
39
40 if ($row === false) {
41     header('Content-type: application/json');
42     echo file_get_contents('me.json');
43     exit();
44 }
45
46 $data = json_decode(file_get_contents('me.json'));
47 $data->gamer->username = $row['username'];
48 $data->gamer->nickname = $row['username'];
49
50 switch (strtolower($row['username'])) {
51 case 'cweiske':
52     $data->gamer->founder = true;
53     $data->gamer->avatar = $GLOBALS['baseUrl'] . 'avatars/cweiske.png';
54     break;
55 case 'szeraax':
56     $data->gamer->founder = true;
57     break;
58 }
59
60 header('Content-type: application/json');
61 echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n";
62 ?>