Repositories | Polyback | Log in to see private repositories


polyback-website / 614f95c3c341c30330ef2016ea9bf11385e0c61a / api/studio/launch.php

1,865 bytes | parent directory

<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';

// The game page's Edit button, and Develop's Open Studio (no placeId: just Studio): a
// polyback-player:// link that opens 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]);

// "no place" in a link: Studio (NO_PLACE in launch/options.odin) opens its start screen. Not 0,
// because Players installed before Open Studio existed refuse a link with place 0.
const NO_PLACE = 1 << 62;

$linkPlace = NO_PLACE;
if (isset($_POST["placeId"]))
{
	$linkPlace = (int) $_POST["placeId"];
	$place = Database::singleton()->run("SELECT id, creator FROM assets WHERE id = :id AND type = 9", [":id" => $linkPlace])->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" => $linkPlace === NO_PLACE ? 0 : $linkPlace]
);
API::respondCustom(["status" => 200, "success" => true, "url" => "polyback-player://edit?" . http_build_query(["place" => $linkPlace, "code" => $code, "site" => Polygon::SiteURL()])]);