Repositories | Polyback | Log in to see private repositories
2,905 bytes | parent directory
<?php
namespace pizzaboxer\ProjectPolygon;
// The era client content packs (the official clients' built-in files) published by
// polygon-engine/tools/publish-client-content.sh into the shared assets volume
// (client-content/<era>.tar.gz + manifest.json). They're only handed to the Polyback Player
// with a short-lived token tied to a signed-in user (put in the join link); they are not
// web-served directly.
class ClientContent
{
const TOKEN_LIFETIME = 86400; // seconds: the game also fetches assets with it while playing
static function CreateToken($userId)
{
$token = bin2hex(random_bytes(32));
Database::singleton()->run("DELETE FROM ClientContentTokens WHERE Expires < UNIX_TIMESTAMP()");
Database::singleton()->run(
"INSERT INTO ClientContentTokens (Token, UserID, Created, Expires) VALUES (:t, :u, UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + :life)",
[":t" => $token, ":u" => $userId, ":life" => self::TOKEN_LIFETIME]
);
return $token;
}
// The user a token belongs to, or null (unknown, expired or the user is banned).
static function TokenUser($token)
{
if (!is_string($token) || !preg_match('/^[0-9a-f]{64}$/', $token)) return null;
$row = Database::singleton()->run(
"SELECT t.UserID, u.Banned FROM ClientContentTokens t JOIN users u ON u.id = t.UserID
WHERE t.Token = :t AND t.Expires > UNIX_TIMESTAMP()",
[":t" => $token]
)->fetch(\PDO::FETCH_OBJ);
if (!$row || $row->Banned) return null;
return (int) $row->UserID;
}
// The bearer token on this request, if any.
static function RequestToken()
{
$auth = $_SERVER["HTTP_AUTHORIZATION"] ?? "";
return preg_match('/^Bearer\s+(\S+)$/', $auth, $m) ? $m[1] : "";
}
// Asset downloads (Polyback's own and the Roblox archive) need one of: a signed-in site
// session (the browser), a player token from the join link (the game client), or the game
// server key (game servers, render jobs, tools).
static function AssetRequestAuthorized()
{
// a finished sign-in: 2FA done (asset pages skip the 2FA redirect), not banned
if (SESSION && (!SESSION["user"]["twofa"] || SESSION["2faVerified"]) && !SESSION["user"]["Banned"]) return true;
if (Gameservers::RequestAuthorized()) return true;
if (self::TokenUser(self::RequestToken()) !== null) return true;
return Studio::RequestUser() !== null; // Polygon Studio's sign-in token
}
static function RequireAssetAuthorization()
{
if (self::AssetRequestAuthorized()) return;
http_response_code(401);
header("Cache-Control: no-store");
die("Unauthorized");
}
static function Directory()
{
return Polygon::GetSharedResource("assets/client-content");
}
static function Manifest()
{
$path = self::Directory() . "/manifest.json";
$data = file_exists($path) ? json_decode(file_get_contents($path), true) : null;
return is_array($data) && isset($data["eras"]) && is_array($data["eras"]) ? $data : ["eras" => []];
}
}