3 * Store the desired username during the login process
5 * It will be read by the ouya when calling api/v1/gamers/me.
7 * @author Christian Weiske <cweiske@cweiske.de>
9 * @see api/v1/gamers/me
11 $dbFile = __DIR__ . '/../../../data/usernames.sqlite3';
13 if (!isset($_POST['username'])) {
14 header('HTTP/1.0 400 Bad Request');
15 header('Content-type: application/json');
16 echo '{"error":{"message":"Username missing","code": 2001}}' . "\n";
19 $username = $_POST['username'];
21 $ip = $_SERVER['REMOTE_ADDR'];
23 header('HTTP/1.0 400 Bad Request');
24 header('Content-type: application/json');
25 echo '{"error":{"message":"Cannot detect your IP address","code": 2002}}'
31 $db = new SQLite3($dbFile, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
32 } catch (Exception $e) {
33 header('HTTP/1.0 500 Internal server error');
34 header('Content-type: application/json');
35 echo '{"error":{"message":"Cannot open username database","code": 2003}}'
37 echo $e->getMessage() . "\n";
41 $res = $db->querySingle(
42 'SELECT name FROM sqlite_master WHERE type = "table" AND name = "usernames"'
45 //table does not exist yet
48 CREATE TABLE usernames (
49 id INTEGER PRIMARY KEY AUTOINCREMENT,
50 username TEXT NOT NULL,
52 created_at TEXT DEFAULT CURRENT_TIMESTAMP
58 //clean up old usernames
60 'DELETE FROM usernames'
61 . ' WHERE created_at < \'' . gmdate('Y-m-d H:i:s', time() - 86400) . '\''
64 //clean up previous logins
65 $stmt = $db->prepare('DELETE FROM usernames WHERE ip = :ip');
66 $stmt->bindValue(':ip', $ip, SQLITE3_TEXT);
67 $stmt->execute()->finalize();
70 $stmt = $db->prepare('INSERT INTO usernames (ip, username) VALUES(:ip, :username)');
71 $stmt->bindValue(':ip', $ip);
72 $stmt->bindValue(':username', $username);
73 $res = $stmt->execute();
75 header('HTTP/1.0 500 Internal server error');
76 header('Content-type: application/json');
77 echo '{"error":{"message":"Cannot store username","code": 2004}}'
83 header('HTTP/1.0 200 OK');
84 header('Content-type: application/json');
85 require __DIR__ . '/sessions';