Repositories | Polyback | Log in to see private repositories


polyback-website / master / api/private/classes/pizzaboxer/ProjectPolygon/SiteSettings.php

1,283 bytes | parent directory

<?php

namespace pizzaboxer\ProjectPolygon;

// Site settings staff change on /admin/site-settings (the SiteSettings table). Unset ones are
// their defaults here.
class SiteSettings
{
	const DEFAULTS = [
		// sign-up: at most this many accounts per IP address ("AccountLimitEnabled" = "0": no limit)
		"AccountLimitEnabled" => "1",
		"AccountsPerIP" => "2",
	];

	private static $cache = null;

	static function Get($name)
	{
		if (self::$cache === null)
		{
			self::$cache = [];
			foreach (Database::singleton()->run("SELECT Name, Value FROM SiteSettings")->fetchAll(\PDO::FETCH_OBJ) as $row)
				self::$cache[$row->Name] = $row->Value;
		}
		return self::$cache[$name] ?? (self::DEFAULTS[$name] ?? null);
	}

	static function Set($name, $value, $userId)
	{
		Database::singleton()->run(
			"REPLACE INTO SiteSettings (Name, Value, SetBy, SetAt) VALUES (:name, :value, :uid, UNIX_TIMESTAMP())",
			[":name" => $name, ":value" => (string) $value, ":uid" => $userId]
		);
		if (self::$cache !== null) self::$cache[$name] = (string) $value;
	}

	// The account limit for sign-ups from one IP address, or null when there is none.
	static function AccountsPerIP()
	{
		if (self::Get("AccountLimitEnabled") !== "1") return null;
		return max(1, (int) self::Get("AccountsPerIP"));
	}
}