[core] kill a secret global
This commit is contained in:
parent
8b797a9a31
commit
05199c13cc
7 changed files with 19 additions and 33 deletions
|
@ -721,8 +721,7 @@ function validate_input(array $inputs): array
|
|||
}
|
||||
$outputs[$key] = $value;
|
||||
} elseif (in_array('user_class', $flags)) {
|
||||
global $_shm_user_classes;
|
||||
if (!array_key_exists($value, $_shm_user_classes)) {
|
||||
if (!array_key_exists($value, UserClass::$known_classes)) {
|
||||
throw new InvalidInput("Invalid user class: ".html_escape($value));
|
||||
}
|
||||
$outputs[$key] = $value;
|
||||
|
|
|
@ -49,16 +49,14 @@ class User
|
|||
*/
|
||||
public function __construct(array $row)
|
||||
{
|
||||
global $_shm_user_classes;
|
||||
|
||||
$this->id = int_escape((string)$row['id']);
|
||||
$this->name = $row['name'];
|
||||
$this->email = $row['email'];
|
||||
$this->join_date = $row['joindate'];
|
||||
$this->passhash = $row['pass'];
|
||||
|
||||
if (array_key_exists($row["class"], $_shm_user_classes)) {
|
||||
$this->class = $_shm_user_classes[$row["class"]];
|
||||
if (array_key_exists($row["class"], UserClass::$known_classes)) {
|
||||
$this->class = UserClass::$known_classes[$row["class"]];
|
||||
} else {
|
||||
throw new SCoreException("User '{$this->name}' has invalid class '{$row["class"]}'");
|
||||
}
|
||||
|
|
|
@ -6,13 +6,6 @@ namespace Shimmie2;
|
|||
|
||||
use GQLA\Type;
|
||||
use GQLA\Field;
|
||||
use GQLA\Query;
|
||||
|
||||
/**
|
||||
* @global UserClass[] $_shm_user_classes
|
||||
*/
|
||||
global $_shm_user_classes;
|
||||
$_shm_user_classes = [];
|
||||
|
||||
/**
|
||||
* Class UserClass
|
||||
|
@ -20,6 +13,9 @@ $_shm_user_classes = [];
|
|||
#[Type(name: "UserClass")]
|
||||
class UserClass
|
||||
{
|
||||
/** @var array<string, UserClass> */
|
||||
public static array $known_classes = [];
|
||||
|
||||
#[Field]
|
||||
public ?string $name = null;
|
||||
public ?UserClass $parent = null;
|
||||
|
@ -32,16 +28,14 @@ class UserClass
|
|||
*/
|
||||
public function __construct(string $name, string $parent = null, array $abilities = [])
|
||||
{
|
||||
global $_shm_user_classes;
|
||||
|
||||
$this->name = $name;
|
||||
$this->abilities = $abilities;
|
||||
|
||||
if (!is_null($parent)) {
|
||||
$this->parent = $_shm_user_classes[$parent];
|
||||
$this->parent = static::$known_classes[$parent];
|
||||
}
|
||||
|
||||
$_shm_user_classes[$name] = $this;
|
||||
static::$known_classes[$name] = $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -72,10 +66,9 @@ class UserClass
|
|||
} elseif (!is_null($this->parent)) {
|
||||
return $this->parent->can($ability);
|
||||
} else {
|
||||
global $_shm_user_classes;
|
||||
$min_dist = 9999;
|
||||
$min_ability = null;
|
||||
foreach ($_shm_user_classes['base']->abilities as $a => $cando) {
|
||||
foreach (UserClass::$known_classes['base']->abilities as $a => $cando) {
|
||||
$v = levenshtein($ability, $a);
|
||||
if ($v < $min_dist) {
|
||||
$min_dist = $v;
|
||||
|
|
|
@ -102,7 +102,7 @@ class IPBan extends Extension
|
|||
|
||||
public function onUserLogin(UserLoginEvent $event): void
|
||||
{
|
||||
global $cache, $config, $database, $page, $_shm_user_classes;
|
||||
global $cache, $config, $database, $page;
|
||||
|
||||
// Get lists of banned IPs and banned networks
|
||||
$ips = $cache->get("ip_bans");
|
||||
|
@ -160,14 +160,14 @@ class IPBan extends Extension
|
|||
$b->is_content = false;
|
||||
$page->add_block($b);
|
||||
$page->add_cookie("nocache", "Ghost Banned", time() + 60 * 60 * 2, "/");
|
||||
$event->user->class = $_shm_user_classes["ghost"];
|
||||
$event->user->class = UserClass::$known_classes["ghost"];
|
||||
} elseif ($row["mode"] == "anon-ghost") {
|
||||
if ($event->user->is_anonymous()) {
|
||||
$b = new Block(null, $msg, "main", 0);
|
||||
$b->is_content = false;
|
||||
$page->add_block($b);
|
||||
$page->add_cookie("nocache", "Ghost Banned", time() + 60 * 60 * 2, "/");
|
||||
$event->user->class = $_shm_user_classes["ghost"];
|
||||
$event->user->class = UserClass::$known_classes["ghost"];
|
||||
}
|
||||
} else {
|
||||
header("HTTP/1.1 403 Forbidden");
|
||||
|
|
|
@ -81,7 +81,7 @@ class Ratings extends Extension
|
|||
|
||||
public function onInitExt(InitExtEvent $event): void
|
||||
{
|
||||
global $config, $_shm_user_classes, $_shm_ratings;
|
||||
global $config, $_shm_ratings;
|
||||
|
||||
$codes = implode("", array_keys($_shm_ratings));
|
||||
$search_terms = [];
|
||||
|
@ -91,7 +91,7 @@ class Ratings extends Extension
|
|||
$this->search_regexp = "/^rating[=|:](?:(\*|[" . $codes . "]+)|(" .
|
||||
implode("|", $search_terms) . "|".implode("|", self::UNRATED_KEYWORDS)."))$/D";
|
||||
|
||||
foreach (array_keys($_shm_user_classes) as $key) {
|
||||
foreach (array_keys(UserClass::$known_classes) as $key) {
|
||||
if ($key == "base" || $key == "hellbanned") {
|
||||
continue;
|
||||
}
|
||||
|
@ -146,8 +146,6 @@ class Ratings extends Extension
|
|||
|
||||
public function onSetupBuilding(SetupBuildingEvent $event): void
|
||||
{
|
||||
global $_shm_user_classes;
|
||||
|
||||
$ratings = self::get_sorted_ratings();
|
||||
|
||||
$options = [];
|
||||
|
@ -157,7 +155,7 @@ class Ratings extends Extension
|
|||
|
||||
$sb = $event->panel->create_new_block("Post Rating Visibility");
|
||||
$sb->start_table();
|
||||
foreach (array_keys($_shm_user_classes) as $key) {
|
||||
foreach (array_keys(UserClass::$known_classes) as $key) {
|
||||
if ($key == "base" || $key == "hellbanned") {
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -46,9 +46,8 @@ class UserTable extends Table
|
|||
{
|
||||
public function __construct(\FFSPHP\PDO $db)
|
||||
{
|
||||
global $_shm_user_classes;
|
||||
$classes = [];
|
||||
foreach ($_shm_user_classes as $cls) {
|
||||
foreach (UserClass::$known_classes as $cls) {
|
||||
$classes[$cls->name] = $cls->name;
|
||||
}
|
||||
ksort($classes);
|
||||
|
@ -160,7 +159,7 @@ class UserPage extends Extension
|
|||
|
||||
public function onPageRequest(PageRequestEvent $event): void
|
||||
{
|
||||
global $config, $database, $page, $user, $_shm_user_classes;
|
||||
global $config, $database, $page, $user;
|
||||
|
||||
$this->show_user_info();
|
||||
|
||||
|
@ -199,7 +198,7 @@ class UserPage extends Extension
|
|||
} elseif ($event->get_arg(0) == "classes") {
|
||||
$this->theme->display_user_classes(
|
||||
$page,
|
||||
$_shm_user_classes,
|
||||
UserClass::$known_classes,
|
||||
(new \ReflectionClass(Permissions::class))->getReflectionConstants()
|
||||
);
|
||||
} elseif ($event->get_arg(0) == "logout") {
|
||||
|
|
|
@ -313,9 +313,8 @@ class UserPageTheme extends Themelet
|
|||
));
|
||||
|
||||
if ($user->can(Permissions::EDIT_USER_CLASS)) {
|
||||
global $_shm_user_classes;
|
||||
$select = SELECT(["name" => "class"]);
|
||||
foreach ($_shm_user_classes as $name => $values) {
|
||||
foreach (UserClass::$known_classes as $name => $values) {
|
||||
$select->appendChild(
|
||||
OPTION(["value" => $name, "selected" => $name == $duser->class->name], ucwords($name))
|
||||
);
|
||||
|
|
Reference in a new issue