Repositories | Polyback | Log in to see private repositories


polyback-website / 5d0cdca2e1e9b3c3c09f52c4f48b8cf10042ac86 / rbxclient/asset/fetch.php

6,561 bytes | parent directory

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

use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\Polygon;
use pizzaboxer\ProjectPolygon\Users;
use pizzaboxer\ProjectPolygon\Games;
use pizzaboxer\ProjectPolygon\Catalog;
use pizzaboxer\ProjectPolygon\Gzip;
use pizzaboxer\ProjectPolygon\RBXClient;
use pizzaboxer\ProjectPolygon\ClientContent;

// signed-in browsers, game clients (join token) and game servers only
ClientContent::RequireAssetAuthorization();

header("Cache-Control: private, max-age=120");

$AssetID = $_GET['ID'] ?? $_GET['id'] ?? false;
$AssetHost = $_SERVER['HTTP_HOST'];

// Two asset spaces, never mixed:
//   /asset/?id=<roblox id>      the archived Roblox asset dump (rbxassetid://, roblox.com/asset URLs)
//   /polyasset/?id=<polygon id> Polygon's own assets (polyasset://)  [routed here with ?polygon=1]
// Nothing is fetched from Roblox's servers.
$PolygonAsset = isset($_GET['polygon']);
$RobloxAsset = !$PolygonAsset;

if (!$AssetID || !ctype_digit((string) $AssetID)) die(http_response_code(400));

if ($RobloxAsset)
{
	$Dump = Database::singleton()->run("SELECT * FROM roblox_assets WHERE id = :id", [":id" => $AssetID])->fetch(\PDO::FETCH_OBJ);
	if (!$Dump) die(http_response_code(404));

	$Sha256 = $Dump->sha256;
	$Version = $_GET['version'] ?? null;
	if ($Version !== null && ctype_digit((string) $Version))
	{
		$Sha256 = Database::singleton()->run(
			"SELECT sha256 FROM roblox_asset_versions WHERE asset_id = :id AND version = :v",
			[":id" => $AssetID, ":v" => $Version]
		)->fetchColumn();
		// assets imported without version history only have their current content: that's what
		// is served when its own version is asked for, or any version of one whose version isn't
		// known (most of the archive; old content often asks for version=1)
		if (!$Sha256 && ((int) $Version === (int) $Dump->latest_version || $Dump->latest_version === null)) $Sha256 = $Dump->sha256;
	}
	if (!$Sha256 || !preg_match('/^[0-9a-f]{64}$/', $Sha256)) die(http_response_code(404));

	$AssetLocation = Polygon::GetSharedResource("assets/rbxdump/" . substr($Sha256, 0, 2) . "/{$Sha256}");
	if (!file_exists($AssetLocation)) die(http_response_code(404));
	$AssetData = file_get_contents($AssetLocation);
}
else
{
	$Asset = Database::singleton()->run("SELECT * FROM assets WHERE id = :id", [":id" => $AssetID])->fetch(\PDO::FETCH_OBJ);
	if (!$Asset) die(http_response_code(404));

	$AssetLocation = Polygon::GetSharedResource("assets/{$Asset->id}");
	// a catalog item made from the Roblox archive has no file of its own: the archive's is served
	if ($Asset->RobloxID)
	{
		$Dump = Database::singleton()->run("SELECT * FROM roblox_assets WHERE id = :id", [":id" => $Asset->RobloxID])->fetch(\PDO::FETCH_OBJ);
		if (!$Dump || !preg_match('/^[0-9a-f]{64}$/', (string) $Dump->sha256)) die(http_response_code(404));
		$AssetLocation = Polygon::GetSharedResource("assets/rbxdump/" . substr($Dump->sha256, 0, 2) . "/{$Dump->sha256}");
	}

	if (!file_exists($AssetLocation)) die(http_response_code(404));

	// we shouldn't authorize gameservers based on ip as that will allow any asset request to go through
	// which is an issue as someone could load an offsale model ingame, save the place and have the model
	// this might pose an issue for the insert tool(?) as players may not be able to insert models they own
	// idk how it dealed with asset ownership back then but for now this will do

	// assets imported for a place (Studio or the server importer) load for everyone allowed
	// to fetch assets, even while pending moderation; a declined one doesn't
	$Imported = (bool) Database::singleton()->run("SELECT 1 FROM import_assets WHERE assetId = :id", [":id" => $Asset->id])->fetchColumn();

	if (isset($_GET["force"]) || Polygon::IsThumbnailServerIP() || Polygon::IsGameserverAuthorized() || $Imported)
	{
		if ($Asset->approved == 2) die(http_response_code(403));
	}
	else
	{
		if ($Asset->approved != 1) die(http_response_code(403));
		if ($Asset->Access == "Friends" && !Games::CanPlayGame($Asset)) die(http_response_code(403));
		if (!$Asset->publicDomain && (!SESSION || !Catalog::OwnsAsset(SESSION["user"]["id"], $Asset->id))) die(http_response_code(403));
	}

	$AssetData = file_get_contents($AssetLocation);

	if($Asset->type == 9 || $Asset->type == 10 && (!stripos($AssetData, 'roblox'))) 
	{
		$StartTime = time();
		$AssetData = Gzip::Decompress($AssetLocation);
		$EndTime = time();
		header("Decompression-Time: " . ($EndTime - $StartTime));
	}

	if (SESSION && SESSION["user"]["id"] != $Asset->creator && ($Asset->type == 9 || $Asset->type == 10)) 
	{
		Users::LogStaffAction("[ Asset Download ] Downloaded \"{$Asset->name}\" [" . Catalog::GetTypeByNum($Asset->type) . " ID {$Asset->id}]"); 
	}
}

// replace asset urls: Polygon's own references go to /polyasset, Roblox references to the dump
// (text formats only: rewriting inside binary files would break their length-prefixed strings)
$IsText = ($PolygonAsset && empty($Asset->RobloxID)) || in_array($Dump->kind ?? "", ["rbxxml", "lua", "text", "json"]);
if (strncmp($AssetData, "PGPLACE\0", 8) === 0) $IsText = false; // polygon-engine's own binary place format
if ($IsText) {
$AssetData = str_replace("%ASSETURL%", "http://{$AssetHost}/polyasset/?id=", $AssetData);
$AssetData = str_replace("%ROBLOXASSETURL%", "http://{$AssetHost}/asset/?id=", $AssetData);
$AssetData = str_ireplace(
	["https://assetdelivery.roblox.com/v1/asset", "http://assetdelivery.roblox.com/v1/asset", "www.roblox.com/asset", "roblox.com/asset", "www.roblox.com/thumbs", "roblox.com/thumbs"],
	["http://{$AssetHost}/asset", "http://{$AssetHost}/asset", "{$AssetHost}/asset", "{$AssetHost}/asset", "{$AssetHost}/thumbs", "{$AssetHost}/thumbs"],
	$AssetData
);
}

if(!$RobloxAsset && $Asset->type == 3 && isset($_GET['audiostream']))
{
	$FileExtensions = ["audio/mpeg" => ".mp3", "video/ogg" => ".ogg", "audio/ogg" => ".ogg", "audio/mid" => ".mid", "audio/wav" => ".wav"];

	header('Content-Type: '.$Asset->audioType); 
	header('Content-Disposition: attachment; filename="'.htmlentities($Asset->name).$FileExtensions[$Asset->audioType].'"'); 
}
else
{
	header('Content-Type: application/octet-stream'); 
	if($RobloxAsset) header('Content-Disposition: attachment; filename="'.sha1($AssetData).'"'); 
	else header('Content-Disposition: attachment; filename="'.sha1_file($AssetLocation).'"'); 
}

if(!$RobloxAsset && $Asset->type == 5) $AssetData = RBXClient::CryptSignScript($AssetData, $Asset->id);

header('Content-Length: '.strlen($AssetData));
die($AssetData);