Repositories | Polyback | Log in to see private repositories


polyback-website / ac78c54296647694d1132210737d1561aac5f7d1 / api/admin/set-import-quota.php

1,849 bytes | parent directory

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

// Admins: see or change a user's storage limit for assets uploaded by Studio imports.
// POST username, and either check=1 or quota=<MB> ("" puts them back on the default).

use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\Users;
use pizzaboxer\ProjectPolygon\API;

API::initialize(["method" => "POST", "admin" => Users::STAFF_ADMINISTRATOR, "admin_ratelimit" => true, "secure" => true]);

$userInfo = Users::GetInfoFromName(trim($_POST["username"] ?? ""));
if (!$userInfo) API::respond(400, false, "That user doesn't exist");

$db = Database::singleton();
$default = (int) (SITE_CONFIG["StudioImportQuotaMB"] ?? 1024);
$used = (int) $db->run(
	"SELECT COALESCE(SUM(i.size), 0) FROM import_assets i JOIN assets a ON a.id = i.assetId WHERE i.uploader = :id AND a.approved != 2",
	[":id" => $userInfo->id]
)->fetchColumn();
$current = $db->run("SELECT ImportQuotaMB FROM users WHERE id = :id", [":id" => $userInfo->id])->fetchColumn();
$describe = fn($q) => $q === null ? "the default ($default MB)" : "$q MB";

if (isset($_POST["check"]))
	API::respond(200, true, sprintf("%s uses %.1f MB of %s", $userInfo->username, $used / 1048576, $describe($current)));

$quota = trim((string) ($_POST["quota"] ?? ""));
if ($quota !== "" && (!ctype_digit($quota) || (int) $quota > 1048576)) API::respond(400, false, "The limit must be a whole number of MB (at most 1048576)");
$new = $quota === "" ? null : (int) $quota;

$db->run("UPDATE users SET ImportQuotaMB = :q WHERE id = :id", [":q" => $new, ":id" => $userInfo->id]);
Users::LogStaffAction("[ Import storage ] Set the import storage limit of {$userInfo->username} ( user ID {$userInfo->id} ) to " . $describe($new));
API::respond(200, true, "{$userInfo->username}'s import storage limit is now " . $describe($new));