Skip to content

Commit

Permalink
s/global $user_config/$user->get_config()/
Browse files Browse the repository at this point in the history
Global variables are a pain, doubly so when you have two global variables which need to be kept in sync or things will go very very badly (`$user` and `$user_config`). Having a global `$user` feels like an acceptable tradeoff, but `$user_config` is redundant.
  • Loading branch information
shish committed Feb 10, 2025
1 parent 3a0462f commit 9de91fd
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 89 deletions.
11 changes: 11 additions & 0 deletions core/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class User
public ?string $passhash;
#[Field]
public UserClass $class;
private ?Config $config = null;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Initialisation *
Expand Down Expand Up @@ -58,6 +59,16 @@ public function __construct(array $row)
}
}

public function get_config(): Config
{
global $database;
if (is_null($this->config)) {
$this->config = new DatabaseConfig($database, "user_config", "user_id", "{$this->id}");
send_event(new InitUserConfigEvent($this, $this->config));
}
return $this->config;
}

#[Query]
public static function me(): User
{
Expand Down
10 changes: 5 additions & 5 deletions ext/avatar_post/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function onInitUserConfig(InitUserConfigEvent $event): void

public function onPageRequest(PageRequestEvent $event): void
{
global $page, $user, $user_config;
global $page, $user;
if ($event->page_matches("set_avatar/{image_id}", method: "POST", permission: Permissions::CHANGE_USER_SETTING)) {
$image_id = int_escape($event->get_arg('image_id'));
$page->set_mode(PageMode::REDIRECT);
Expand All @@ -35,7 +35,7 @@ public function onPageRequest(PageRequestEvent $event): void
$this->theme->display_avatar_edit_page($page, $image_id);
} elseif ($event->page_matches("save_avatar", method: "POST", permission: Permissions::CHANGE_USER_SETTING)) {
$settings = ConfigSaveEvent::postToSettings($event->POST);
send_event(new ConfigSaveEvent($user_config, $settings));
send_event(new ConfigSaveEvent($user->get_config(), $settings));
$page->flash("Image set as avatar");
$page->set_mode(PageMode::REDIRECT);
if (key_exists(AvatarPostConfig::AVATAR_ID, $settings) && is_int($settings[AvatarPostConfig::AVATAR_ID])) {
Expand All @@ -48,10 +48,10 @@ public function onPageRequest(PageRequestEvent $event): void

public function onUserOptionsBuilding(UserOptionsBuildingEvent $event): void
{
global $config, $user_config;
global $config, $user;
$sb = $event->panel->create_new_block("Avatar");
$sb->add_int_option(AvatarPostConfig::AVATAR_ID, 'Avatar post ID: ');
$image_id = $user_config->get_int(AvatarPostConfig::AVATAR_ID, null);
$image_id = $user->get_config()->get_int(AvatarPostConfig::AVATAR_ID, null);
if (!is_null($image_id)) {
$sb->add_label("<br><a href=".make_link("set_avatar/$image_id").">Change cropping</a>");
}
Expand Down Expand Up @@ -85,7 +85,7 @@ public function avatar_html(User $user): HTMLElement|null
public function get_avatar_html(User $user): HTMLElement|null
{
global $database, $config;
$user_config = UserConfig::get_for_user($user);
$user_config = $user->get_config();
$id = $user_config->get_int(AvatarPostConfig::AVATAR_ID, 0);
if ($id === 0) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion ext/avatar_post/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AvatarPostTheme extends Themelet
{
public function display_avatar_edit_page(Page $page, int $image_id): void
{
global $user, $user_config;
global $user;
/** @var BuildAvatarEvent $avatar_e */
$avatar_e = send_event(new BuildAvatarEvent($user));
$current = $avatar_e->html;
Expand Down
7 changes: 3 additions & 4 deletions ext/biography/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public function onUserPageBuilding(UserPageBuildingEvent $event): void
{
global $page, $user;
$duser = $event->display_user;
$duser_config = UserConfig::get_for_user($event->display_user);
$bio = $duser_config->get_string("biography", "");
$bio = $duser->get_config()->get_string("biography", "");

if ($user->id == $duser->id) {
$this->theme->display_composer($page, $bio);
Expand All @@ -25,11 +24,11 @@ public function onUserPageBuilding(UserPageBuildingEvent $event): void

public function onPageRequest(PageRequestEvent $event): void
{
global $page, $user, $user_config;
global $page, $user;
if ($event->page_matches("biography", method: "POST")) {
$bio = $event->get_POST('biography');
log_info("biography", "Set biography to $bio");
$user_config->set_string("biography", $bio);
$user->get_config()->set_string("biography", $bio);
$page->flash("Bio Updated");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(referer_or(make_link()));
Expand Down
46 changes: 23 additions & 23 deletions ext/cron_uploader/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ public function onAdminAction(AdminActionEvent $event): void

public function onLog(LogEvent $event): void
{
global $user_config;
global $user;

if (self::$IMPORT_RUNNING) {
$all = $user_config->get_bool(CronUploaderConfig::INCLUDE_ALL_LOGS);
if ($event->priority >= $user_config->get_int(CronUploaderConfig::LOG_LEVEL) &&
$all = $user->get_config()->get_bool(CronUploaderConfig::INCLUDE_ALL_LOGS);
if ($event->priority >= $user->get_config()->get_int(CronUploaderConfig::LOG_LEVEL) &&
($event->section == self::NAME || $all)) {
$output = "[" . date('Y-m-d H:i:s') . "] " . ($all ? '[' . $event->section . '] ' : '') . "[" . LOGGING_LEVEL_NAMES[$event->priority] . "] " . $event->message;

Expand Down Expand Up @@ -186,18 +186,18 @@ private function restage_folder(string $folder): void

private function clear_folder(string $folder): void
{
global $page, $user_config;
$path = join_path($user_config->get_string(CronUploaderConfig::DIR), $folder);
global $page, $user;
$path = join_path($user->get_config()->get_string(CronUploaderConfig::DIR), $folder);
deltree($path);
$page->flash("Cleared $path");
}


private function get_cron_url(): string
{
global $user_config;
global $user;

$user_api_key = $user_config->get_string(UserConfig::API_KEY, "API_KEY");
$user_api_key = $user->get_config()->get_string(UserConfig::API_KEY, "API_KEY");

return make_http(make_link("/cron_upload/run", "api_key=".urlencode($user_api_key)));
}
Expand Down Expand Up @@ -255,34 +255,34 @@ private function display_documentation(): void

public function get_queue_dir(): string
{
global $user_config;
global $user;

$dir = $user_config->get_string(CronUploaderConfig::DIR);
$dir = $user->get_config()->get_string(CronUploaderConfig::DIR);
return join_path($dir, self::QUEUE_DIR);
}

public function get_uploaded_dir(): string
{
global $user_config;
global $user;

$dir = $user_config->get_string(CronUploaderConfig::DIR);
$dir = $user->get_config()->get_string(CronUploaderConfig::DIR);
return join_path($dir, self::UPLOADED_DIR);
}

public function get_failed_dir(): string
{
global $user_config;
global $user;

$dir = $user_config->get_string(CronUploaderConfig::DIR);
$dir = $user->get_config()->get_string(CronUploaderConfig::DIR);
return join_path($dir, self::FAILED_DIR);
}

private function prep_root_dir(): string
{
global $user_config;
global $user;

// Determine directory (none = default)
$dir = $user_config->get_string(CronUploaderConfig::DIR);
$dir = $user->get_config()->get_string(CronUploaderConfig::DIR);

// Make the directory if it doesn't exist yet
if (!is_dir($this->get_queue_dir())) {
Expand All @@ -300,9 +300,9 @@ private function prep_root_dir(): string

private function get_lock_file(): string
{
global $user_config;
global $user;

$root_dir = $user_config->get_string(CronUploaderConfig::DIR);
$root_dir = $user->get_config()->get_string(CronUploaderConfig::DIR);
return join_path($root_dir, ".lock");
}

Expand All @@ -311,7 +311,7 @@ private function get_lock_file(): string
*/
public function process_upload(): bool
{
global $database, $user, $user_config, $config, $_shm_load_start;
global $database, $user, $config, $_shm_load_start;

$max_time = intval(ini_get('max_execution_time')) * .8;

Expand Down Expand Up @@ -375,7 +375,7 @@ public function process_upload(): bool
$failed++;
$this->log_message(SCORE_LOG_ERROR, "(" . gettype($e) . ") " . $e->getMessage());
$this->log_message(SCORE_LOG_ERROR, $e->getTraceAsString());
if ($user_config->get_bool(CronUploaderConfig::STOP_ON_ERROR)) {
if ($user->get_config()->get_bool(CronUploaderConfig::STOP_ON_ERROR)) {
break;
} else {
$this->move_uploaded($img[0], $img[1], $output_subdir, true);
Expand Down Expand Up @@ -404,9 +404,9 @@ public function process_upload(): bool

private function move_uploaded(string $path, string $filename, string $output_subdir, bool $corrupt = false): void
{
global $user_config;
global $user;

$rootDir = $user_config->get_string(CronUploaderConfig::DIR);
$rootDir = $user->get_config()->get_string(CronUploaderConfig::DIR);
$rootLength = strlen($rootDir);
if ($rootDir[$rootLength - 1] == "/" || $rootDir[$rootLength - 1] == "\\") {
$rootLength--;
Expand Down Expand Up @@ -514,9 +514,9 @@ private function log_message(int $severity, string $message): void

private function get_log_file(): string
{
global $user_config;
global $user;

$dir = $user_config->get_string(CronUploaderConfig::DIR);
$dir = $user->get_config()->get_string(CronUploaderConfig::DIR);

return join_path($dir, "uploads.log");
}
Expand Down
4 changes: 2 additions & 2 deletions ext/cron_uploader/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function display_documentation(
string $cron_url,
?array $log_entries
): void {
global $page, $config, $user_config;
global $page, $config, $user;

$info_html = "";

Expand Down Expand Up @@ -109,7 +109,7 @@ public function display_documentation(
<li>If an import is already running, another cannot start until it is done.</li>
<li>Each time it runs it will import for up to ".number_format($max_time)." seconds. This is controlled by the PHP max execution time.</li>
<li>Uploaded images will be moved to the 'uploaded' directory into a subfolder named after the time the import started. It's recommended that you remove everything out of this directory from time to time. If you have admin controls enabled, this can be done from <a href='".make_link("admin")."'>Board Admin</a>.</li>
<li>If you enable the db logging extension, you can view the log output on this screen. Otherwise the log will be written to a file at ".$user_config->get_string(CronUploaderConfig::DIR).DIRECTORY_SEPARATOR."uploads.log</li>
<li>If you enable the db logging extension, you can view the log output on this screen. Otherwise the log will be written to a file at ".$user->get_config()->get_string(CronUploaderConfig::DIR).DIRECTORY_SEPARATOR."uploads.log</li>
</ul>
";

Expand Down
4 changes: 2 additions & 2 deletions ext/filter/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class FilterTheme extends Themelet
{
public function addFilterBox(): void
{
global $config, $page, $user, $user_config;
global $config, $page, $user;

// If user is not able to set their own filters, use the default filters.
if ($user->can(Permissions::CHANGE_USER_SETTING)) {
$tags = $user_config->get_string("filter_tags");
$tags = $user->get_config()->get_string("filter_tags");
} else {
$tags = $config->get_string("filter_tags");
}
Expand Down
14 changes: 7 additions & 7 deletions ext/private_image/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function onUserOptionsBuilding(UserOptionsBuildingEvent $event): void

public function onPageRequest(PageRequestEvent $event): void
{
global $page, $user, $user_config;
global $page, $user;

if ($event->page_matches("privatize_image/{image_id}", method: "POST", permission: Permissions::SET_PRIVATE_IMAGE)) {
$image_id = $event->get_iarg('image_id');
Expand Down Expand Up @@ -68,8 +68,8 @@ public function onPageRequest(PageRequestEvent $event): void
$set_default = array_key_exists("set_default", $event->POST);
$view_default = array_key_exists("view_default", $event->POST);

$user_config->set_bool(PrivateImageConfig::USER_SET_DEFAULT, $set_default);
$user_config->set_bool(PrivateImageConfig::USER_VIEW_DEFAULT, $view_default);
$user->get_config()->set_bool(PrivateImageConfig::USER_SET_DEFAULT, $set_default);
$user->get_config()->set_bool(PrivateImageConfig::USER_VIEW_DEFAULT, $view_default);

$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("user"));
Expand All @@ -89,8 +89,8 @@ public function onDisplayingImage(DisplayingImageEvent $event): void
public const SEARCH_REGEXP = "/^private:(yes|no|any)/i";
public function onSearchTermParse(SearchTermParseEvent $event): void
{
global $user, $user_config;
$show_private = $user_config->get_bool(PrivateImageConfig::USER_VIEW_DEFAULT);
global $user;
$show_private = $user->get_config()->get_bool(PrivateImageConfig::USER_VIEW_DEFAULT);

if (is_null($event->term) && $this->no_private_query($event->context)) {
if ($show_private) {
Expand Down Expand Up @@ -190,8 +190,8 @@ public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event):

public function onImageAddition(ImageAdditionEvent $event): void
{
global $user, $user_config;
if ($user_config->get_bool(PrivateImageConfig::USER_SET_DEFAULT) && $user->can(Permissions::SET_PRIVATE_IMAGE)) {
global $user;
if ($user->get_config()->get_bool(PrivateImageConfig::USER_SET_DEFAULT) && $user->can(Permissions::SET_PRIVATE_IMAGE)) {
self::privatize_image($event->image->id);
}
}
Expand Down
4 changes: 2 additions & 2 deletions ext/rating/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ public static function get_user_class_privs(User $user): array
*/
public static function get_user_default_ratings(): array
{
global $user_config, $user;
global $user;

$available = self::get_user_class_privs($user);
$selected = $user_config->get_array(RatingsConfig::USER_DEFAULTS);
$selected = $user->get_config()->get_array(RatingsConfig::USER_DEFAULTS);

return array_intersect($available, $selected);
}
Expand Down
16 changes: 8 additions & 8 deletions ext/rating/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testRatingExplicit(): void

public function testUserConfig(): void
{
global $config, $user_config;
global $config, $user;

// post a safe image and an explicit image
$this->log_in_as_user();
Expand All @@ -56,7 +56,7 @@ public function testUserConfig(): void
$config->set_array("ext_rating_user_privs", ["s", "q", "e"]);

// user prefers safe-only by default
$user_config->set_array(RatingsConfig::USER_DEFAULTS, ["s"]);
$user->get_config()->set_array(RatingsConfig::USER_DEFAULTS, ["s"]);

// search with no tags should return only safe image
$this->assert_search_results([], [$image_id_s]);
Expand All @@ -67,21 +67,21 @@ public function testUserConfig(): void

// If user prefers to see all images, going to the safe image
// and clicking next should show the explicit image
$user_config->set_array(RatingsConfig::USER_DEFAULTS, ["s", "q", "e"]);
$user->get_config()->set_array(RatingsConfig::USER_DEFAULTS, ["s", "q", "e"]);
$next = $image_s->get_next();
$this->assertNotNull($next);
$this->assertEquals($next->id, $image_id_e);

// If the user prefers to see only safe images by default, then
// going to the safe image and clicking next should not show
// the explicit image (See bug #984)
$user_config->set_array(RatingsConfig::USER_DEFAULTS, ["s"]);
$user->get_config()->set_array(RatingsConfig::USER_DEFAULTS, ["s"]);
$this->assertEquals($image_s->get_next(), null);
}

public function testCountImages(): void
{
global $config, $user_config;
global $config, $user;

$this->log_in_as_user();

Expand All @@ -96,7 +96,7 @@ public function testCountImages(): void
send_event(new RatingSetEvent($image_e, "e"));

$config->set_array("ext_rating_user_privs", ["s", "q"]);
$user_config->set_array(RatingsConfig::USER_DEFAULTS, ["s"]);
$user->get_config()->set_array(RatingsConfig::USER_DEFAULTS, ["s"]);

$this->assertEquals(1, Search::count_images(["rating=s"]), "UserClass has access to safe, show safe");
$this->assertEquals(2, Search::count_images(["rating=*"]), "UserClass has access to s/q - if user asks for everything, show those two but hide e");
Expand All @@ -107,10 +107,10 @@ public function testCountImages(): void
// that it doesn't mess with other unrelated tests
public function tearDown(): void
{
global $user_config;
global $user;

$this->log_in_as_user();
$user_config->set_array(RatingsConfig::USER_DEFAULTS, ["?", "s", "q", "e"]);
$user->get_config()->set_array(RatingsConfig::USER_DEFAULTS, ["?", "s", "q", "e"]);

parent::tearDown();
}
Expand Down
4 changes: 2 additions & 2 deletions ext/ratings_blur/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public function onSetupBuilding(SetupBuildingEvent $event): void

public function blur(string $rating): bool
{
global $user_config;
global $user;

$blur_ratings = $user_config->get_array(RatingsBlurConfig::USER_DEFAULTS);
$blur_ratings = $user->get_config()->get_array(RatingsBlurConfig::USER_DEFAULTS);
if (in_array(RatingsBlurConfig::NULL_OPTION, $blur_ratings)) {
return false;
}
Expand Down
Loading

0 comments on commit 9de91fd

Please sign in to comment.