Repositories | Polyback | Log in to see private repositories


polyback-website / 614f95c3c341c30330ef2016ea9bf11385e0c61a / api/private/classes/pizzaboxer/ProjectPolygon/Archive.php

6,148 bytes | parent directory

<?php

namespace pizzaboxer\ProjectPolygon;

// Catalog items made from the Roblox archive. Only administrators make them; their files stay in
// the archive and are served from there (rbxclient/asset/fetch.php), so a newer archived version
// applies by itself. Everybody else uploads their own items on the Develop page.
class Archive
{
	// archive type => how its picture is made
	const SELLABLE = [8 => "Model", 19 => "Model", 11 => "Clothing", 12 => "Clothing", 17 => "Head", 2 => "TShirt"];
	const MAX_PRICE = 1000000;

	static function Info($robloxId)
	{
		return Database::singleton()->run(
			"SELECT r.id, r.asset_type_id, r.sha256, r.kind, r.source, a.id AS CatalogID FROM roblox_assets r LEFT JOIN assets a ON a.RobloxID = r.id WHERE r.id = :id",
			[":id" => (int) $robloxId]
		)->fetch(\PDO::FETCH_OBJ);
	}

	static function File($row)
	{
		if (!$row || !preg_match('/^[0-9a-f]{64}$/', (string) $row->sha256)) return false;
		$path = Polygon::GetSharedResource("assets/rbxdump/" . substr($row->sha256, 0, 2) . "/{$row->sha256}");
		if (!file_exists($path)) return false;
		$data = file_get_contents($path);
		return Gzip::IsGzEncoded($data) ? gzdecode($data) : $data;
	}

	// Lists an archived item in the catalog. Returns the new catalog id, or an error text.
	static function Sell($robloxId, $name, $description, $price, $creatorId)
	{
		$row = self::Info($robloxId);
		if (!$row) return "That id isn't in the Roblox archive yet";
		if ($row->CatalogID) return "It's in the catalog already";
		if (!isset(self::SELLABLE[(int) $row->asset_type_id])) return "Only hats, gear, shirts, pants, T-shirts and heads can be sold from the archive";
		$name = trim((string) $name);
		if ($name === "" || mb_strlen($name) > 50) return "The name has to be 1 to 50 characters";
		if (mb_strlen((string) $description) > 1000) return "The description can't be longer than 1000 characters";
		if (!preg_match('/^[0-9]{1,7}$/', (string) $price) || (int) $price > self::MAX_PRICE) return "The price has to be from 0 to " . number_format(self::MAX_PRICE) . " Pizzas";
		$data = self::File($row);
		if ($data === false || strncmp($data, "<roblox", 7) !== 0) return "The archived file isn't a ROBLOX model";

		$type = (int) $row->asset_type_id;
		$options = ["type" => $type, "creator" => (int) $creatorId, "name" => $name, "description" => (string) $description,
			"approved" => 1, "sale" => 1, "price" => (int) $price, "RobloxID" => (int) $row->id];
		if ($type == 19) $options["gear_attributes"] = '{"melee":false,"powerup":false,"ranged":false,"navigation":false,"explosive":false,"musical":false,"social":false,"transport":false,"building":false}';
		$id = Catalog::CreateAsset($options);

		$render = self::SELLABLE[$type];
		if ($render === "TShirt") self::TShirtPicture($id, $data);
		else Polygon::RequestRender($render, $id);
		Users::LogStaffAction("[ Archive ] Put Roblox asset {$row->id} in the catalog as \"$name\" [ID $id] for $price Pizzas");
		return (int) $id;
	}

	// A T-shirt's picture: its image (from the archive too) on the T-shirt template.
	private static function TShirtPicture($id, $data)
	{
		if (!preg_match('/(?:rbxassetid:\/\/|asset\/?\?(?:[a-z]+=[^&<]*&(?:amp;)?)*id=)([0-9]+)/i', $data, $m)) return;
		$image = self::File(self::Info($m[1]));
		$decal = $image !== false ? @imagecreatefromstring($image) : false;
		if (!$decal) return;
		$template = imagecreatefrompng($_SERVER['DOCUMENT_ROOT'] . "/img/tshirt-template.png");
		imagesavealpha($template, true);
		imagealphablending($template, true);
		imagecopyresampled($template, $decal, 85, 85, 0, 0, 250, 250, imagesx($decal), imagesy($decal));
		$out = SITE_CONFIG['paths']['thumbs_assets'] . "$id-420x420.png";
		imagepng($template, $out);
		Thumbnails::UploadToCDN($out);
	}

	const BROWSE_TYPES = [8 => "Hats", 19 => "Gear", 11 => "Shirts", 12 => "Pants", 2 => "T-Shirts", 17 => "Heads"];
	const PER_PAGE = 24;

	// A page of the archive's items of one kind, newest ids first. $search: an id, or part of a
	// name (most archived items have none). Returns [items, total].
	static function Browse($type, $search, $page)
	{
		$where = "r.asset_type_id = :t AND r.sha256 IS NOT NULL";
		$args = [":t" => (int) $type];
		$search = trim((string) $search);
		if ($search !== "")
		{
			if (ctype_digit($search)) { $where .= " AND r.id = :id"; $args[":id"] = $search; }
			else { $where .= " AND r.name LIKE :q"; $args[":q"] = "%" . addcslashes($search, "%_\\") . "%"; }
		}
		$db = Database::singleton();
		$total = (int) $db->run("SELECT COUNT(*) FROM roblox_assets r WHERE $where", $args)->fetchColumn();
		$offset = max(0, ((int) $page - 1) * self::PER_PAGE);
		$items = $db->run(
			"SELECT r.id, r.name, r.thumbnail_sha256 IS NOT NULL AS HasPicture, a.id AS CatalogID, a.name AS CatalogName, a.price FROM roblox_assets r
			 LEFT JOIN assets a ON a.RobloxID = r.id WHERE $where ORDER BY r.id DESC LIMIT " . self::PER_PAGE . " OFFSET $offset", $args
		)->fetchAll(\PDO::FETCH_OBJ);
		return [$items, $total];
	}

	// What's been put in the catalog from the archive, newest first.
	static function Sold($limit = 100)
	{
		return Database::singleton()->run(
			"SELECT a.id, a.name, a.type, a.price, a.sale, a.RobloxID, a.created, u.username FROM assets a LEFT JOIN users u ON u.id = a.creator
			 WHERE a.RobloxID IS NOT NULL ORDER BY a.id DESC LIMIT " . (int) $limit
		)->fetchAll(\PDO::FETCH_OBJ);
	}

	// A model file listing ROBLOX ids, to import in Studio with "Add to the ROBLOX archive": the
	// importer fetches each from live ROBLOX (and what they use, like meshes and textures).
	static function ImportFile($ids)
	{
		$items = "";
		foreach ($ids as $i => $id)
			$items .= "\t\t<Item class=\"StringValue\" referent=\"R" . ($i + 1) . "\"><Properties><string name=\"Name\">$id</string><string name=\"Value\">rbxassetid://$id</string></Properties></Item>\n";
		return "<roblox xmlns:xmime=\"http://www.w3.org/2005/05/xmlmime\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"4\">\n"
			. "\t<Item class=\"Model\" referent=\"R0\"><Properties><string name=\"Name\">Archive request</string></Properties>\n$items\t</Item>\n</roblox>\n";
	}
}