Repositories | Polyback | Log in to see private repositories
9,938 bytes | parent directory
<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';
// Asset upload endpoint for the polygon-engine place importer.
// With roblox_id (and optional version): an asset fetched from live Roblox is added to the
// Roblox archive (roblox_assets) under that id. Archived content is never replaced (409),
// except by a strictly newer version, which becomes current while the previous one stays
// in roblox_asset_versions. Without roblox_id: a new Polygon-own asset.
// The importer POSTs one fetched asset at a time (multipart): file, sha256, kind, source.
// Assets are de-duplicated by content hash, so re-importing a place re-uses existing ids.
// Auth, one of:
// - header "X-Polygon-Import-Key" equal to SITE_CONFIG["keys"]["AssetImport"] (at least 16 chars)
// - a Studio sign-in (Authorization: Bearer) of an administrator with 2FA, for Roblox assets
// only (roblox_id); their other assets go through /api/studio/import-upload
use pizzaboxer\ProjectPolygon\Catalog;
use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\Polygon;
use pizzaboxer\ProjectPolygon\Studio;
header("content-type: application/json");
function respond($status, $data)
{
http_response_code($status);
die(json_encode($data));
}
if ($_SERVER["REQUEST_METHOD"] !== "POST") respond(405, ["success" => false, "message" => "Method Not Allowed"]);
$configuredKey = SITE_CONFIG["keys"]["AssetImport"] ?? "";
$givenKey = $_SERVER["HTTP_X_POLYGON_IMPORT_KEY"] ?? "";
$studioUser = null;
if ($givenKey === "" && ($studioUser = Studio::RequestUser()))
{
if (!Studio::CanArchive($studioUser)) respond(403, ["success" => false, "message" => "Only administrators with two-factor authentication can add to the Roblox archive"]);
if (($_POST["roblox_id"] ?? "") === "") respond(400, ["success" => false, "message" => "Only Roblox assets go into the archive"]);
}
else
{
if (strlen($configuredKey) < 16) respond(503, ["success" => false, "message" => "Asset import is not enabled on this server"]);
if (!hash_equals($configuredKey, $givenKey)) respond(401, ["success" => false, "message" => "Unauthorized"]);
}
$file = $_FILES["file"] ?? null;
$sha256 = strtolower($_POST["sha256"] ?? "");
$kind = $_POST["kind"] ?? "";
$source = substr($_POST["source"] ?? "", 0, 255);
if (!$file || $file["error"] !== UPLOAD_ERR_OK) respond(400, ["success" => false, "message" => "Missing file"]);
if (!preg_match('/^[0-9a-f]{64}$/', $sha256)) respond(400, ["success" => false, "message" => "Bad sha256"]);
if (!hash_equals($sha256, hash_file("sha256", $file["tmp_name"]))) respond(400, ["success" => false, "message" => "sha256 does not match the uploaded data"]);
// thumbnail_of=<roblox id>: a picture of an archived asset (for browsing the archive). Only a
// PNG or JPEG, only with the import key, and only where the archive has none yet.
if (($_POST["thumbnail_of"] ?? "") !== "")
{
$of = $_POST["thumbnail_of"];
if ($studioUser || !ctype_digit($of) || strlen($of) > 19) respond(400, ["success" => false, "message" => "Bad thumbnail_of"]);
$head = file_get_contents($file["tmp_name"], false, null, 0, 8);
if (!str_starts_with($head, "\x89PNG") && !str_starts_with($head, "\xff\xd8\xff")) respond(400, ["success" => false, "message" => "A picture has to be a PNG or JPEG"]);
if (!Database::singleton()->run("SELECT 1 FROM roblox_assets WHERE id = :id", [":id" => $of])->fetchColumn()) respond(404, ["success" => false, "message" => "That id isn't archived"]);
$target = Polygon::GetSharedResource("assets/rbxdump/" . substr($sha256, 0, 2) . "/$sha256");
if (!is_dir(dirname($target))) mkdir(dirname($target), 0775, true);
if (!file_exists($target) && !move_uploaded_file($file["tmp_name"], $target)) respond(500, ["success" => false, "message" => "Could not store the picture"]);
$set = Database::singleton()->run("UPDATE roblox_assets SET thumbnail_sha256 = :h WHERE id = :id AND thumbnail_sha256 IS NULL", [":h" => $sha256, ":id" => $of])->rowCount();
respond(200, ["success" => true, "set" => (bool) $set]);
}
// engine asset kind -> site asset type
$types = [
"image" => 1,
"audio" => 3,
"mesh" => 4,
"lua" => 5,
"model" => 10,
];
if (!isset($types[$kind])) respond(400, ["success" => false, "message" => "Unsupported asset kind"]);
// Roblox assets the importer fetched from live Roblox go into the Roblox archive
// (roblox_assets) under their real id, owned by the import creator and not for sale.
$robloxId = $_POST["roblox_id"] ?? "";
if ($robloxId !== "")
{
if (!ctype_digit($robloxId) || strlen($robloxId) > 19) respond(400, ["success" => false, "message" => "Bad roblox_id"]);
$version = $_POST["version"] ?? "";
if ($version !== "" && !ctype_digit($version)) respond(400, ["success" => false, "message" => "Bad version"]);
$target = Polygon::GetSharedResource("assets/rbxdump/" . substr($sha256, 0, 2) . "/$sha256");
if (!is_dir(dirname($target))) mkdir(dirname($target), 0775, true);
$storedNew = !file_exists($target);
if ($storedNew && !move_uploaded_file($file["tmp_name"], $target)) respond(500, ["success" => false, "message" => "Could not store the asset"]);
$head = file_get_contents($target, false, null, 0, 16);
$format = match (true) {
str_starts_with($head, "\x89PNG") => "png",
str_starts_with($head, "\xff\xd8\xff") => "jpeg",
str_starts_with($head, "GIF8") => "gif",
str_starts_with($head, "OggS") => "ogg",
str_starts_with($head, "ID3") => "mp3",
str_starts_with($head, "RIFF") => "wav",
str_starts_with($head, "version ") => "mesh",
str_starts_with($head, "<roblox!") => "rbxbin",
str_starts_with($head, "<roblox") => "rbxxml",
$kind === "lua" => "lua",
default => $kind,
};
$creator = (int) (SITE_CONFIG["AssetImportCreator"] ?? 1);
$size = filesize($target);
$db = Database::singleton();
$row = $db->run("SELECT sha256, latest_version FROM roblox_assets WHERE id = :id", [":id" => $robloxId])->fetch(PDO::FETCH_ASSOC);
if (!$row)
{
$db->run(
"INSERT INTO roblox_assets (id, asset_type_id, type_inferred, owner_id, for_sale, price, latest_version, sha256, size, kind, source, imported)
VALUES (:id, :t, 1, :owner, 0, NULL, :v, :h, :size, :kind, :src, UNIX_TIMESTAMP())",
[":id" => $robloxId, ":t" => $types[$kind], ":owner" => $creator, ":v" => $version !== "" ? (int) $version : null, ":h" => $sha256, ":size" => $size, ":kind" => $format,
// rebuilt from other sources (not the original file): marked so a genuine original can be found and added later
":src" => str_starts_with($source, "reconstructed") ? "reconstructed" : "import"]
);
}
else if ($row["sha256"] !== null && !hash_equals($row["sha256"], $sha256))
{
// Content already archived is never replaced, except by a strictly newer version:
// then the new one becomes current and the previous one stays in the history.
$latest = $row["latest_version"] === null ? 0 : (int) $row["latest_version"];
if ($version === "" || (int) $version <= $latest)
{
if ($storedNew) @unlink($target); // don't keep the rejected file
respond(409, ["success" => false, "assetId" => (int) $robloxId, "archive" => true, "existing" => true,
"message" => "This archive id already has content; only a newer version (above $latest) can be added"]);
}
if ($row["latest_version"] !== null)
{
$db->run(
"INSERT IGNORE INTO roblox_asset_versions (asset_id, version, sha256, size, kind)
SELECT id, latest_version, sha256, size, kind FROM roblox_assets WHERE id = :id",
[":id" => $robloxId]
);
}
$db->run(
"UPDATE roblox_assets SET sha256 = :h, size = :size, kind = :kind, latest_version = :v WHERE id = :id",
[":id" => $robloxId, ":h" => $sha256, ":size" => $size, ":kind" => $format, ":v" => (int) $version]
);
}
else if ($row["sha256"] === null)
{
// a metadata-only archive entry gets its first content
$db->run(
"UPDATE roblox_assets SET sha256 = :h, size = :size, kind = :kind, latest_version = COALESCE(:v, latest_version) WHERE id = :id",
[":id" => $robloxId, ":h" => $sha256, ":size" => $size, ":kind" => $format, ":v" => $version !== "" ? (int) $version : null]
);
}
// a version number not seen before is recorded in the history (INSERT IGNORE never
// replaces one); it doesn't change what the id serves
if ($version !== "")
{
$db->run(
"INSERT IGNORE INTO roblox_asset_versions (asset_id, version, sha256, size, kind) VALUES (:id, :v, :h, :size, :kind)",
[":id" => $robloxId, ":v" => (int) $version, ":h" => $sha256, ":size" => $size, ":kind" => $format]
);
}
if ($studioUser) Studio::LogArchiveUse($studioUser);
respond(200, ["success" => true, "assetId" => (int) $robloxId, "archive" => true, "existing" => (bool) $row]);
}
$existing = Database::singleton()->run("SELECT assetId FROM import_assets WHERE sha256 = :h", [":h" => $sha256])->fetchColumn();
if ($existing) respond(200, ["success" => true, "assetId" => (int) $existing, "existing" => true]);
$creator = (int) (SITE_CONFIG["AssetImportCreator"] ?? 1);
$assetId = Catalog::CreateAsset([
"type" => $types[$kind],
"creator" => $creator,
"name" => "Imported " . ($source !== "" ? $source : substr($sha256, 0, 12)),
"description" => "Imported by polygon-engine from $source",
"approved" => 1,
]);
$target = Polygon::GetSharedResource("assets/$assetId");
if (!is_dir(dirname($target))) mkdir(dirname($target), 0775, true);
if (!move_uploaded_file($file["tmp_name"], $target))
{
Database::singleton()->run("DELETE FROM assets WHERE id = :id", [":id" => $assetId]);
Database::singleton()->run("DELETE FROM ownedAssets WHERE assetId = :id", [":id" => $assetId]);
respond(500, ["success" => false, "message" => "Could not store the asset"]);
}
Database::singleton()->run(
"INSERT INTO import_assets (sha256, assetId, source, created) VALUES (:h, :id, :src, UNIX_TIMESTAMP())",
[":h" => $sha256, ":id" => $assetId, ":src" => $source]
);
respond(200, ["success" => true, "assetId" => (int) $assetId, "existing" => false]);