Add support for dynamic usernames.
authorChristian Weiske <cweiske@cweiske.de>
Thu, 19 Aug 2021 19:53:27 +0000 (21:53 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 19 Aug 2021 19:53:33 +0000 (21:53 +0200)
Simply specify the desired username during login.

.gitignore
README.rst
www/.htaccess
www/api/v1/gamers/me.json
www/api/v1/gamers/me.php [new file with mode: 0644]
www/api/v1/sessions.php [new file with mode: 0644]

index f24be71b6a09d11d533121f312f3d516ac6dde75..708d7ea92adbbc8c55589532ed0b1b5be84f01d6 100644 (file)
@@ -1,5 +1,6 @@
 /config.php
 /data/push-to-my-ouya.sqlite3
+/data/usernames.sqlite3
 /README.html
 www/api/v1/apps/
 www/api/v1/details-data/
index 4a5593eff1e151b81b5a4f183280ef9c4b720309..785269d18658b1c8201b5aefaf4ca727860fc9f4 100644 (file)
@@ -30,6 +30,8 @@ OUYA setup
 2. Enter any username, leave password empty. Continue.
 3. Skip credit card registration
 
+The username will appear on your ouya main screen.
+
 
 Apache setup
 ============
index 2c5ece0d51c92e38fb5366b3a522d36f2733dab7..541105ec88e8072956ee6da91d682fd844a13ae7 100644 (file)
@@ -35,6 +35,11 @@ RewriteRule ^api/v1/gamers$ /api/v1/gamers/register-error.json [R=400,END]
 <Files "me">
     DirectorySlash Off
 </Files>
+
+#Disable the next two lines to have static usernames only
+RewriteRule ^api/v1/gamers/me$ /api/v1/gamers/me.php [END]
+RewriteRule ^api/v1/sessions$ /api/v1/sessions.php [END]
+
 RewriteRule ^api/v1/gamers/me$ /api/v1/gamers/me.json [END]
 
 #purchased games/products
index 1b1527bb244e3d4d22c2e33f6a075b4607f39747..ef91c6e7c9c120d7944d83aab5fcb8ae9aa48734 100644 (file)
@@ -2,7 +2,7 @@
   "gamer": {
     "uuid": "00702342-0000-1111-2222-c3e1500cafe2",
     "settings": {},
-    "founder": true,
+    "founder": false,
     "email": "stouyapi@example.org",
     "username": "stouyapi"
   }
diff --git a/www/api/v1/gamers/me.php b/www/api/v1/gamers/me.php
new file mode 100644 (file)
index 0000000..fc941c7
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Return user data with dynamic username that has been saved during login
+ *
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @see    api/v1/sessions
+ */
+$dbFile  = __DIR__ . '/../../../../data/usernames.sqlite3';
+
+$ip = $_SERVER['REMOTE_ADDR'];
+if ($ip == '') {
+    //empty ip
+    header('X-Fail-Reason: empty ip address');
+    header('Content-type: application/json');
+    echo file_get_contents('me.json');
+    exit(1);
+}
+
+try {
+    $db = new SQLite3($dbFile, SQLITE3_OPEN_READONLY);
+} catch (Exception $e) {
+    //db file not found
+    header('X-Fail-Reason: database file not found');
+    header('Content-type: application/json');
+    echo file_get_contents('me.json');
+    exit(1);
+}
+
+$stmt = $db->prepare('SELECT * FROM usernames WHERE ip = :ip');
+$stmt->bindValue(':ip', $ip);
+$res = $stmt->execute();
+$row = $res->fetchArray(SQLITE3_ASSOC);
+$res->finalize();
+
+if ($row === false) {
+    header('Content-type: application/json');
+    echo file_get_contents('me.json');
+    exit();
+}
+
+$data = json_decode(file_get_contents('me.json'));
+$data->gamer->username = $row['username'];
+
+switch ($row['username']) {
+case 'cweiske':
+case 'szeraax':
+    $data->gamer->founder = true;
+}
+
+header('Content-type: application/json');
+echo json_encode($data) . "\n";
+?>
diff --git a/www/api/v1/sessions.php b/www/api/v1/sessions.php
new file mode 100644 (file)
index 0000000..39567ae
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+/**
+ * Store the desired username during the login process
+ *
+ * It will be read by the ouya when calling api/v1/gamers/me.
+ *
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @see    api/v1/sessions
+ * @see    api/v1/gamers/me
+ */
+$dbFile  = __DIR__ . '/../../../data/usernames.sqlite3';
+
+if (!isset($_POST['username'])) {
+    header('HTTP/1.0 400 Bad Request');
+    header('Content-type: application/json');
+    echo '{"error":{"message":"Username missing","code": 2001}}' . "\n";
+    exit(1);
+}
+$username = $_POST['username'];
+
+$ip = $_SERVER['REMOTE_ADDR'];
+if ($ip == '') {
+    header('HTTP/1.0 400 Bad Request');
+    header('Content-type: application/json');
+    echo '{"error":{"message":"Cannot detect your IP address","code": 2002}}'
+        . "\n";
+    exit(1);
+}
+
+try {
+    $db = new SQLite3($dbFile, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
+} catch (Exception $e) {
+    header('HTTP/1.0 500 Internal server error');
+    header('Content-type: application/json');
+    echo '{"error":{"message":"Cannot open username database","code": 2003}}'
+        . "\n";
+    echo $e->getMessage() . "\n";
+    exit(2);
+}
+
+$res = $db->querySingle(
+    'SELECT name FROM sqlite_master WHERE type = "table" AND name = "usernames"'
+);
+if ($res === null) {
+    //table does not exist yet
+    $db->exec(
+        <<<SQL
+        CREATE TABLE usernames (
+            id INTEGER PRIMARY KEY AUTOINCREMENT,
+            username TEXT NOT NULL,
+            ip TEXT NOT NULL,
+            created_at TEXT DEFAULT CURRENT_TIMESTAMP
+        )
+SQL
+    );
+}
+
+//clean up old usernames
+$db->exec(
+    'DELETE FROM usernames'
+    . ' WHERE created_at < \'' . gmdate('Y-m-d H:i:s', time() - 86400) . '\''
+);
+
+//clean up previous logins
+$stmt = $db->prepare('DELETE FROM usernames WHERE ip = :ip');
+$stmt->bindValue(':ip', $ip, SQLITE3_TEXT);
+$stmt->execute()->finalize();
+
+//store the username
+$stmt = $db->prepare('INSERT INTO usernames (ip, username) VALUES(:ip, :username)');
+$stmt->bindValue(':ip', $ip);
+$stmt->bindValue(':username', $username);
+$res = $stmt->execute();
+if ($res === false) {
+    header('HTTP/1.0 500 Internal server error');
+    header('Content-type: application/json');
+    echo '{"error":{"message":"Cannot store username","code": 2004}}'
+        . "\n";
+    exit(3);
+}
+$res->finalize();
+
+header('HTTP/1.0 200 OK');
+header('Content-type: application/json');
+require __DIR__ . '/sessions';
+?>