Repositories | Polyback | Log in to see private repositories
1,531 bytes | parent directory
<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';
// The game page's Edit button: a polyback-player:// link that opens this place in Studio,
// signed in as the current user (through a one-time code, never a long-lived token in a link).
// POST placeId -> {"success":true,"url":"polyback-player://edit?place=..&code=.."}
use pizzaboxer\ProjectPolygon\API;
use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\Polygon;
API::initialize(["method" => "POST", "logged_in" => true, "secure" => true]);
$placeId = (int) ($_POST["placeId"] ?? 0);
$place = Database::singleton()->run("SELECT id, creator FROM assets WHERE id = :id AND type = 9", [":id" => $placeId])->fetch(PDO::FETCH_OBJ);
if (!$place) API::respond(404, false, "That game doesn't exist");
$isAdmin = (int) (SESSION["user"]["adminlevel"] ?? 0) > 0;
if ((int) $place->creator !== (int) SESSION["user"]["id"] && !$isAdmin) API::respond(403, false, "Only the game's owner can edit it");
$code = bin2hex(random_bytes(32));
Database::singleton()->run("DELETE FROM StudioLaunchCodes WHERE Expires < UNIX_TIMESTAMP()");
Database::singleton()->run(
"INSERT INTO StudioLaunchCodes (CodeHash, UserID, PlaceID, Expires) VALUES (:h, :u, :p, UNIX_TIMESTAMP() + 120)",
[":h" => hash("sha256", $code), ":u" => SESSION["user"]["id"], ":p" => $place->id]
);
API::respondCustom(["status" => 200, "success" => true, "url" => "polyback-player://edit?" . http_build_query(["place" => (int) $place->id, "code" => $code, "site" => Polygon::SiteURL()])]);