From b522d687366a39c9852aba00255f42b02287e943 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 13 Jun 2019 14:41:03 -0500 Subject: [PATCH 01/20] Custom rating support --- core/userclass.php | 7 ++ ext/rating/main.php | 211 ++++++++++++++++++++++++++++++------------- ext/rating/theme.php | 62 ++++++------- 3 files changed, 186 insertions(+), 94 deletions(-) diff --git a/core/userclass.php b/core/userclass.php index de781aa2..55be5e91 100644 --- a/core/userclass.php +++ b/core/userclass.php @@ -128,6 +128,10 @@ new UserClass("base", null, [ "view_hellbanned" => false, "protected" => false, # only admins can modify protected users (stops a moderator changing an admin's password) + + "edit_image_rating" => false, + "bulk_edit_image_rating" => false, + ]); new UserClass("anonymous", "base", [ @@ -140,6 +144,7 @@ new UserClass("user", "base", [ "edit_image_tag" => true, "edit_image_source" => true, "create_image_report" => true, + "edit_image_rating" => true, ]); new UserClass("admin", "base", [ @@ -184,6 +189,8 @@ new UserClass("admin", "base", [ "view_sysinfo" => true, "view_hellbanned" => true, "protected" => true, + "edit_image_rating" => true, + "bulk_edit_image_rating" => true, ]); new UserClass("hellbanned", "user", [ diff --git a/ext/rating/main.php b/ext/rating/main.php index 18b40823..c022d391 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -19,6 +19,62 @@ * */ + /** + * @global Rating[] $_shm_ratings + */ +global $_shm_ratings; +$_shm_ratings = []; + +class Rating { + /** + * @var string + */ + public $name = null; + + /** + * @var string + */ + public $code = null; + + /** + * @var string + */ + public $search_term = null; + + /** + * @var int + */ + public $order = 0; + + public function __construct( string $code, string $name, string $search_term, int $order) + { + global $_shm_ratings; + + if(strlen($code)!=1) { + throw new Exception("Rating code must be exactly one character"); + } + if($search_term[0]!=$code) { + throw new Exception("Code must be the same as the first letter of search_term"); + } + + $this->name = $name; + $this->code = $code; + $this->search_term = $search_term; + $this->order = $order; + + if($code=="u"&&array_key_exists("u",$_shm_ratings)) { + throw new Exception("u is a reserved rating code that cnanot be overridden"); + } + $_shm_ratings[$code] = $this; + } +} + +new Rating("s", "Safe", "safe", 0); +new Rating("q", "Questionable", "questionable", 500); +new Rating("e", "Explicit", "explicit", 1000); +new Rating("u", "Unrated", "unrated", 99999); +@include_once "data/config/ratings.conf.php"; + class RatingSetEvent extends Event { /** @var Image */ @@ -28,7 +84,9 @@ class RatingSetEvent extends Event public function __construct(Image $image, string $rating) { - assert(in_array($rating, ["s", "q", "e", "u"])); + global $_shm_ratings; + + assert(in_array($rating, array_keys($_shm_ratings))); $this->image = $image; $this->rating = $rating; @@ -37,7 +95,25 @@ class RatingSetEvent extends Event class Ratings extends Extension { - protected $db_support = ['mysql','pgsql']; // ? + + protected $db_support = ['mysql','pgsql']; + + private $search_regexp; + + + public function __construct() { + parent::__construct(); + + global $_shm_ratings; + + $codes = implode("",array_keys($_shm_ratings)); + $search_terms = []; + foreach($_shm_ratings as $key=>$rating) { + array_push($search_terms, $rating->search_term); + } + $this->search_regexp = "/^rating[=|:](?:([".$codes."]+)|(". + implode("|", $search_terms)."|unknown))$/D"; + } public function get_priority(): int { @@ -46,30 +122,43 @@ class Ratings extends Extension public function onInitExt(InitExtEvent $event) { - global $config; + global $config, $_shm_user_classes, $_shm_ratings; - if ($config->get_int("ext_ratings2_version") < 2) { + if ($config->get_int("ext_ratings2_version") < 4) { $this->install(); } - $config->set_default_string("ext_rating_anon_privs", 'squ'); - $config->set_default_string("ext_rating_user_privs", 'sqeu'); - $config->set_default_string("ext_rating_admin_privs", 'sqeu'); + foreach(array_keys($_shm_user_classes) as $key){ + if($key=="base"||$key=="hellbanned") { + continue; + } + $config->set_default_array("ext_rating_".$key."_privs", array_keys($_shm_ratings)); + } + + } public function onSetupBuilding(SetupBuildingEvent $event) { - $privs = []; - $privs['Safe Only'] = 's'; - $privs['Safe and Unknown'] = 'su'; - $privs['Safe and Questionable'] = 'sq'; - $privs['Safe, Questionable, Unknown'] = 'squ'; - $privs['All'] = 'sqeu'; + global $config, $_shm_user_classes, $_shm_ratings; + + $ratings = array_values($_shm_ratings); + usort($ratings, function($a, $b) { + return $a->order <=> $b->order; + }); + $options = []; + foreach($ratings as $key => $rating) { + $options[$rating->name] = $rating->code; + } $sb = new SetupBlock("Image Ratings"); - $sb->add_choice_option("ext_rating_anon_privs", $privs, "Anonymous: "); - $sb->add_choice_option("ext_rating_user_privs", $privs, "
Users: "); - $sb->add_choice_option("ext_rating_admin_privs", $privs, "
Admins: "); + foreach(array_keys($_shm_user_classes) as $key){ + if($key=="base"||$key=="hellbanned") { + continue; + } + $sb->add_multichoice_option("ext_rating_".$key."_privs", $options, "
".$key.": "); + } + $event->panel->add_block($sb); } @@ -89,7 +178,6 @@ class Ratings extends Extension * Deny images upon insufficient permissions. **/ $user_view_level = Ratings::get_user_privs($user); - $user_view_level = preg_split('//', $user_view_level, -1); if (!in_array($event->image->rating, $user_view_level)) { $page->set_mode("redirect"); $page->set_redirect(make_link("post/list")); @@ -128,16 +216,20 @@ class Ratings extends Extension public function onSearchTermParse(SearchTermParseEvent $event) { - global $user; + global $user, $_shm_ratings; $matches = []; if (is_null($event->term) && $this->no_rating_query($event->context)) { $set = Ratings::privs_to_sql(Ratings::get_user_privs($user)); $event->add_querylet(new Querylet("rating IN ($set)")); } - if (preg_match("/^rating[=|:](?:([sqeu]+)|(safe|questionable|explicit|unknown))$/D", strtolower($event->term), $matches)) { + + + if (preg_match($this->search_regexp, strtolower($event->term), $matches)) { $ratings = $matches[1] ? $matches[1] : $matches[2][0]; - $ratings = array_intersect(str_split($ratings), str_split(Ratings::get_user_privs($user))); + + $ratings = array_intersect(str_split($ratings), Ratings::get_user_privs($user)); + $set = "'" . join("', '", $ratings) . "'"; $event->add_querylet(new Querylet("rating IN ($set)")); } @@ -147,9 +239,9 @@ class Ratings extends Extension { $matches = []; - if (preg_match("/^rating[=|:](?:([sqeu]+)|(safe|questionable|explicit|unknown))$/D", strtolower($event->term), $matches) && $event->parse) { + if (preg_match($this->search_regexp, strtolower($event->term), $matches) && $event->parse) { $ratings = $matches[1] ? $matches[1] : $matches[2][0]; - $ratings = array_intersect(str_split($ratings), str_split(Ratings::get_user_privs($user))); + $ratings = array_intersect(str_split($ratings), Ratings::get_user_privs($user)); $rating = $ratings[0]; @@ -169,8 +261,8 @@ class Ratings extends Extension { global $user; - if ($user->is_admin()) { - $event->add_action("bulk_rate", "Set Rating", "", $this->theme->get_selection_rater_html("bulk_rating")); + if ($user->can("bulk_edit_image_rating")) { + $event->add_action("bulk_rate","Set Rating","",$this->theme->get_selection_rater_html("bulk_rating")); } } @@ -183,7 +275,7 @@ class Ratings extends Extension if (!isset($_POST['bulk_rating'])) { return; } - if ($user->is_admin()) { + if ($user->can("bulk_edit_image_rating")) { $rating = $_POST['bulk_rating']; $total = 0; foreach ($event->items as $id) { @@ -206,7 +298,7 @@ class Ratings extends Extension global $user, $page; if ($event->page_matches("admin/bulk_rate")) { - if (!$user->is_admin()) { + if (!$user->can("bulk_edit_image_rating")) { throw new PermissionDeniedException(); } else { $n = 0; @@ -234,26 +326,21 @@ class Ratings extends Extension } } - public static function get_user_privs(User $user): string + public static function get_user_privs(User $user): array { - global $config; + global $config, $_shm_ratings; - if ($user->is_anonymous()) { - $sqes = $config->get_string("ext_rating_anon_privs"); - } elseif ($user->is_admin()) { - $sqes = $config->get_string("ext_rating_admin_privs"); - } else { - $sqes = $config->get_string("ext_rating_user_privs"); - } - return $sqes; + return $config->get_array("ext_rating_".$user->class->name."_privs"); } - public static function privs_to_sql(string $sqes): string + public static function privs_to_sql(array $sqes): string { $arr = []; - $length = strlen($sqes); - for ($i=0; $i<$length; $i++) { - $arr[] = "'" . $sqes[$i] . "'"; + foreach($sqes as $i) { + $arr[] = "'" . $i . "'"; + } + if(sizeof($arr)==0) { + return "' '"; } $set = join(', ', $arr); return $set; @@ -261,25 +348,19 @@ class Ratings extends Extension public static function rating_to_human(string $rating): string { - switch ($rating) { - case "s": return "Safe"; - case "q": return "Questionable"; - case "e": return "Explicit"; - default: return "Unknown"; + global $_shm_ratings; + + if(array_key_exists($rating, $_shm_ratings)) { + return $_shm_ratings[$rating]->name; } + return "Unknown"; } public static function rating_is_valid(string $rating): bool { - switch ($rating) { - case "s": - case "q": - case "e": - case "u": - return true; - default: - return false; - } + global $_shm_ratings; + + return in_array($rating, array_keys($_shm_ratings)); } /** @@ -288,13 +369,7 @@ class Ratings extends Extension private function can_rate(): bool { global $config, $user; - if ($user->is_anonymous() && $config->get_string("ext_rating_anon_privs") == "sqeu") { - return false; - } - if ($user->is_admin()) { - return true; - } - if (!$user->is_anonymous() && $config->get_string("ext_rating_user_privs") == "sqeu") { + if ($user->can("edit_image_rating")) { return true; } return false; @@ -328,7 +403,7 @@ class Ratings extends Extension $config->set_int("ext_ratings2_version", 2); } - if ($config->get_int("ext_ratings2_version") < 3) { + if($config->get_int("ext_ratings2_version") < 3) { $database->Execute("UPDATE images SET rating = 'u' WHERE rating is null"); switch ($database->get_driver_name()) { case "mysql": @@ -339,7 +414,17 @@ class Ratings extends Extension $database->Execute("ALTER TABLE images ALTER COLUMN rating SET NOT NULL"); break; } - $config->set_int("ext_ratings2_version", 3); + $config->set_int("ext_ratings2_version", 3); + } + + if ($config->get_int("ext_ratings2_version") < 4) { + $value = $config->get_string("ext_rating_anon_privs"); + $config->set_array("ext_rating_anonymous_privs", str_split($value)); + $value = $config->get_string("ext_rating_user_privs"); + $config->set_array("ext_rating_user_privs", str_split($value)); + $value = $config->get_string("ext_rating_admin_privs"); + $config->set_array("ext_rating_admin_privs", str_split($value)); + $config->set_int("ext_ratings2_version", 4); } } diff --git a/ext/rating/theme.php b/ext/rating/theme.php index d414e3f6..39d843f1 100644 --- a/ext/rating/theme.php +++ b/ext/rating/theme.php @@ -4,9 +4,6 @@ class RatingsTheme extends Themelet { public function get_rater_html(int $image_id, string $rating, bool $can_rate): string { - $s_checked = $rating == 's' ? " checked" : ""; - $q_checked = $rating == 'q' ? " checked" : ""; - $e_checked = $rating == 'e' ? " checked" : ""; $human_rating = Ratings::rating_to_human($rating); $html = " @@ -15,9 +12,7 @@ class RatingsTheme extends Themelet ".($can_rate ? " $human_rating - - - + ".$this->get_selection_rater_html($rating)." " : " $human_rating @@ -28,31 +23,36 @@ class RatingsTheme extends Themelet return $html; } - public function display_bulk_rater(string $terms) - { - global $page; - $html = " - ".make_form(make_link("admin/bulk_rate"))." - - - - - "; - $page->add_block(new Block("List Controls", $html, "left")); - } + // public function display_bulk_rater(string $terms) + // { + // global $page; + // $html = " + // ".make_form(make_link("admin/bulk_rate"))." + // + // + // + // + // "; + // $page->add_block(new Block("List Controls", $html, "left")); + // } - public function get_selection_rater_html(String $id = "select_rating") - { - return ""; + public function get_selection_rater_html(String $selected_option, String $id = "rating") { + global $_shm_ratings; + + $output = ""; } } From e854b6d884c4d6731150f4dbcd0a8e75bf9f883a Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Fri, 14 Jun 2019 09:46:32 -0500 Subject: [PATCH 02/20] Custom rating changes --- ext/rating/main.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ext/rating/main.php b/ext/rating/main.php index c022d391..7f4e111a 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -237,6 +237,7 @@ class Ratings extends Extension public function onTagTermParse(TagTermParseEvent $event) { + global $user; $matches = []; if (preg_match($this->search_regexp, strtolower($event->term), $matches) && $event->parse) { @@ -328,15 +329,15 @@ class Ratings extends Extension public static function get_user_privs(User $user): array { - global $config, $_shm_ratings; + global $config; return $config->get_array("ext_rating_".$user->class->name."_privs"); } - public static function privs_to_sql(array $sqes): string + public static function privs_to_sql(array $privs): string { $arr = []; - foreach($sqes as $i) { + foreach($privs as $i) { $arr[] = "'" . $i . "'"; } if(sizeof($arr)==0) { From 8f0aa8a4ca828d86b5d061b7d2c4fa6142aff117 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Wed, 26 Jun 2019 22:11:53 -0500 Subject: [PATCH 03/20] Added $user_config global based on existing config object for storing user-specific settings. Added event to the user page so that extensions can hook into it, providing user-specific setting controls --- core/_bootstrap.php | 5 +++ core/config.php | 83 +++++++++++++++++++++++++++++++++++++++----- ext/upgrade/main.php | 17 +++++++++ ext/user/main.php | 22 ++++++++++++ ext/user/theme.php | 21 ++++++----- index.php | 1 - 6 files changed, 129 insertions(+), 20 deletions(-) diff --git a/core/_bootstrap.php b/core/_bootstrap.php index a003ed6e..c9869a36 100644 --- a/core/_bootstrap.php +++ b/core/_bootstrap.php @@ -52,6 +52,11 @@ unset($themelet); $page = class_exists("CustomPage") ? new CustomPage() : new Page(); $_tracer->end(); +$_shm_ctx->log_start("Loading user information"); +$user = _get_user(); +$user_config = new DatabaseConfig($database, "user_config","user_id", $user->id); +$_shm_ctx->log_endok(); + // hook up event handlers $_tracer->begin("Loading extensions"); _load_event_listeners(); diff --git a/core/config.php b/core/config.php index c9fb225b..6b1ac6fa 100644 --- a/core/config.php +++ b/core/config.php @@ -20,6 +20,11 @@ interface Config */ public function set_int(string $name, ?string $value): void; + /** + * Set a configuration option to a new value, regardless of what the value is at the moment. + */ + public function set_float(string $name, ?string $value): void; + /** * Set a configuration option to a new value, regardless of what the value is at the moment. */ @@ -48,6 +53,16 @@ interface Config */ public function set_default_int(string $name, int $value): void; + /** + * Set a configuration option to a new value, if there is no value currently. + * + * Extensions should generally call these from their InitExtEvent handlers. + * This has the advantage that the values will show up in the "advanced" setup + * page where they can be modified, while calling get_* with a "default" + * parameter won't show up. + */ + public function set_default_float(string $name, float $value): void; + /** * Set a configuration option to a new value, if there is no value currently. * @@ -85,6 +100,11 @@ interface Config */ public function get_int(string $name, ?int $default=null): ?int; + /** + * Pick a value out of the table by name, cast to the appropriate data type. + */ + public function get_float(string $name, ?float $default=null): ?float; + /** * Pick a value out of the table by name, cast to the appropriate data type. */ @@ -119,6 +139,12 @@ abstract class BaseConfig implements Config $this->save($name); } + public function set_float(string $name, ?string $value): void + { + $this->values[$name] = $value; + $this->save($name); + } + public function set_string(string $name, ?string $value): void { $this->values[$name] = $value; @@ -131,9 +157,13 @@ abstract class BaseConfig implements Config $this->save($name); } - public function set_array(string $name, array $value): void + public function set_array(string $name, ?array $value): void { - $this->values[$name] = implode(",", $value); + if($value!=null) { + $this->values[$name] = implode(",", $value); + } else { + $this->values[$name] = null; + } $this->save($name); } @@ -279,19 +309,41 @@ class DatabaseConfig extends BaseConfig /** @var Database */ private $database = null; - public function __construct(Database $database) + private $table_name; + private $sub_column; + private $sub_value; + + public function __construct(Database $database, string $table_name = "config", + string $sub_column = null, string $sub_value = null) { $this->database = $database; + $this->table_name = $table_name; + $this->sub_value = $sub_value; + $this->sub_column = $sub_column; - $cached = $this->database->cache->get("config"); + $cache_name = "config"; + if(!empty($sub_value)) { + $cache_name .= "_".$sub_value; + } + + $cached = $this->database->cache->get($cache_name); if ($cached) { $this->values = $cached; } else { $this->values = []; - foreach ($this->database->get_all("SELECT name, value FROM config") as $row) { + + $query = "SELECT name, value FROM {$this->table_name}"; + $args = []; + + if(!empty($sub_column)&&!empty($sub_value)) { + $query .= " WHERE $sub_column = :sub_value"; + $args["sub_value"] = $sub_value; + } + + foreach ($this->database->get_all($query, $args ) as $row) { $this->values[$row["name"]] = $row["value"]; } - $this->database->cache->set("config", $this->values); + $this->database->cache->set($cache_name, $this->values); } } @@ -303,8 +355,23 @@ class DatabaseConfig extends BaseConfig $this->save($name); } } else { - $this->database->Execute("DELETE FROM config WHERE name = :name", ["name"=>$name]); - $this->database->Execute("INSERT INTO config VALUES (:name, :value)", ["name"=>$name, "value"=>$this->values[$name]]); + $query = "DELETE FROM {$this->table_name} WHERE name = :name"; + $args = ["name"=>$name]; + $cols = ["name","value"]; + $params = [":name",":value"]; + if(!empty($this->sub_column)&&!empty($this->sub_value)) { + $query .= " AND $this->sub_column = :sub_value"; + $args["sub_value"] = $this->sub_value; + $cols[] = $this->sub_column; + $params[] = ":sub_value"; + } + + $this->database->Execute($query, $args); + + $args["value"] =$this->values[$name]; + $this->database->Execute( + "INSERT INTO {$this->table_name} (".join(",",$cols).") VALUES (".join(",",$params).")", + $args); } // rather than deleting and having some other request(s) do a thundering // herd of race-conditioned updates, just save the updated version once here diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php index 3bad2d52..2ea12cb9 100644 --- a/ext/upgrade/main.php +++ b/ext/upgrade/main.php @@ -224,7 +224,24 @@ class Upgrade extends Extension $config->set_bool("in_upgrade", false); } + if ($config->get_int("db_version") < 18) { + $config->set_bool("in_upgrade", true); + $config->set_int("db_version", 18); + log_info("upgrade", "Adding user config table"); + + $database->create_table("user_config", " + user_id INTEGER NOT NULL, + name VARCHAR(128) NOT NULL, + value TEXT, + PRIMARY KEY (user_id, name), + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + "); + $database->execute("CREATE INDEX user_config_user_id_idx ON user_config(user_id)"); + + log_info("upgrade", "Database at version 18"); + $config->set_bool("in_upgrade", false); + } } diff --git a/ext/user/main.php b/ext/user/main.php index 4bb726d0..d6a59f8d 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -19,6 +19,17 @@ class UserBlockBuildingEvent extends Event } } +class UserOptionsBuildingEvent extends Event +{ + /** @var array */ + public $parts = []; + + public function add__html(string $html) + { + $this->parts[] = $html; + } +} + class UserPageBuildingEvent extends Event { /** @var User */ @@ -254,6 +265,17 @@ class UserPage extends Extension ksort($event->stats); $this->theme->display_user_page($event->display_user, $event->stats); + + if (!$user->is_anonymous()) { + if ($user->id == $event->display_user->id || $user->can("edit_user_info")) { + $uobe = new UserOptionsBuildingEvent(); + send_event($uobe); + + $page->add_block(new Block("Options", $this->theme->build_options($event->display_user, $uobe), "main", 60)); + } + } + + if ($user->id == $event->display_user->id) { $ubbe = new UserBlockBuildingEvent(); send_event($ubbe); diff --git a/ext/user/theme.php b/ext/user/theme.php index dab26e8c..a1650db4 100644 --- a/ext/user/theme.php +++ b/ext/user/theme.php @@ -243,14 +243,9 @@ class UserPageTheme extends Themelet $page->add_block(new NavBlock()); $page->add_block(new Block("Stats", join("
", $stats), "main", 10)); - if (!$user->is_anonymous()) { - if ($user->id == $duser->id || $user->can("edit_user_info")) { - $page->add_block(new Block("Options", $this->build_options($duser), "main", 60)); - } - } } - protected function build_options(User $duser) + public function build_options(User $duser, UserOptionsBuildingEvent $event) { global $config, $user; $html = ""; @@ -266,7 +261,7 @@ class UserPageTheme extends Themelet - "; +

"; } $html .= " @@ -285,7 +280,7 @@ class UserPageTheme extends Themelet - +

".make_form(make_link("user_admin/change_email"))." @@ -294,7 +289,7 @@ class UserPageTheme extends Themelet
- "; +

"; $i_user_id = int_escape($duser->id); @@ -316,7 +311,7 @@ class UserPageTheme extends Themelet - "; +

"; } if ($user->can(Permissions::DELETE_USER)) { @@ -337,8 +332,12 @@ class UserPageTheme extends Themelet - "; +

"; } + foreach ($event->parts as $part) { + $html .= $part; + } + } return $html; } diff --git a/index.php b/index.php index fdde0b16..95300516 100644 --- a/index.php +++ b/index.php @@ -90,7 +90,6 @@ $_tracer->begin($_SERVER["REQUEST_URI"] ?? "No Request"); try { // start the page generation waterfall - $user = _get_user(); if (PHP_SAPI === 'cli' || PHP_SAPI == 'phpdbg') { send_event(new CommandEvent($argv)); } else { From 68ee4d0e77ea873d3c086ccc3b78a4331342b81a Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Wed, 26 Jun 2019 23:11:59 -0500 Subject: [PATCH 04/20] Custom rating support, user rating filter settings --- core/imageboard/image.php | 3 + ext/comment/main.php | 2 +- ext/danbooru_api/main.php | 2 +- ext/featured/main.php | 2 +- ext/pools/main.php | 2 +- ext/rating/main.php | 262 ++++++++++++++++++++++++-------- ext/rating/theme.php | 69 +++++++-- ext/upgrade/main.php | 17 +++ themes/danbooru/view.theme.php | 4 +- themes/danbooru2/view.theme.php | 4 +- themes/lite/view.theme.php | 4 +- 11 files changed, 288 insertions(+), 83 deletions(-) diff --git a/core/imageboard/image.php b/core/imageboard/image.php index 598a2ab7..7f17141c 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -299,6 +299,9 @@ class Image ["tag"=>$tags[0]] ); } else { + if(ext_is_live("Ratings")) { + $tags[] = "rating:*"; + } list($tag_conditions, $img_conditions) = self::terms_to_conditions($tags); $total = Image::get_accelerated_count($tag_conditions, $img_conditions); if (is_null($total)) { diff --git a/ext/comment/main.php b/ext/comment/main.php index f58a3076..ad2e9507 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -407,7 +407,7 @@ class CommentList extends Extension LIMIT :limit OFFSET :offset ", ["limit"=>$threads_per_page, "offset"=>$start]); - $user_ratings = ext_is_live("Ratings") ? Ratings::get_user_privs($user) : ""; + $user_ratings = ext_is_live("Ratings") ? Ratings::get_user_class_privs($user) : ""; $images = []; while ($row = $result->fetch()) { diff --git a/ext/danbooru_api/main.php b/ext/danbooru_api/main.php index cb55766f..0c24ab78 100644 --- a/ext/danbooru_api/main.php +++ b/ext/danbooru_api/main.php @@ -246,7 +246,7 @@ class DanbooruApi extends Extension "preview_url" => $img->get_thumb_link(), "preview_height" => $previewsize[1], "preview_width" => $previewsize[0], - "rating" => "u", + "rating" => "?", "date" => $img->posted, "is_warehoused" => false, "tags" => $taglist, diff --git a/ext/featured/main.php b/ext/featured/main.php index ae5946dd..eac762a5 100644 --- a/ext/featured/main.php +++ b/ext/featured/main.php @@ -74,7 +74,7 @@ class Featured extends Extension } if (!is_null($image)) { if (ext_is_live("Ratings")) { - if (strpos(Ratings::get_user_privs($user), $image->rating) === false) { + if (strpos(Ratings::get_user_class_privs($user), $image->rating) === false) { return; } } diff --git a/ext/pools/main.php b/ext/pools/main.php index 71d3f492..2fefbf73 100644 --- a/ext/pools/main.php +++ b/ext/pools/main.php @@ -807,7 +807,7 @@ class Pools extends Extension // WE CHECK IF THE EXTENSION RATING IS INSTALLED, WHICH VERSION AND IF IT // WORKS TO SHOW/HIDE SAFE, QUESTIONABLE, EXPLICIT AND UNRATED IMAGES FROM USER if (ext_is_live("Ratings")) { - $query .= "AND i.rating IN (".Ratings::privs_to_sql(Ratings::get_user_privs($user)).")"; + $query .= "AND i.rating IN (".Ratings::privs_to_sql(Ratings::get_user_class_privs($user)).")"; } if(ext_is_live("trash")) { $query .= $database->scoreql_to_sql(" AND trash = SCORE_BOOL_N "); diff --git a/ext/rating/main.php b/ext/rating/main.php index b2ee61e2..1a83b1c2 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -48,31 +48,43 @@ class Rating { public function __construct( string $code, string $name, string $search_term, int $order) { - global $_shm_ratings; - if(strlen($code)!=1) { throw new Exception("Rating code must be exactly one character"); } - if($search_term[0]!=$code) { - throw new Exception("Code must be the same as the first letter of search_term"); - } $this->name = $name; $this->code = $code; $this->search_term = $search_term; $this->order = $order; - if($code=="u"&&array_key_exists("u",$_shm_ratings)) { - throw new Exception("u is a reserved rating code that cnanot be overridden"); - } - $_shm_ratings[$code] = $this; } } -new Rating("s", "Safe", "safe", 0); -new Rating("q", "Questionable", "questionable", 500); -new Rating("e", "Explicit", "explicit", 1000); -new Rating("u", "Unrated", "unrated", 99999); +function clear_ratings() { + global $_shm_ratings; + $keys = array_keys($_shm_ratings); + foreach ($keys as $key){ + if($key=="?") { + continue; + } + unset($_shm_ratings[$key]); + } +} + +function add_rating(Rating $rating) { + global $_shm_ratings; + + if($rating->code=="?"&&array_key_exists("?",$_shm_ratings)) { + throw new Exception("? is a reserved rating code that cannot be overridden"); + } + $_shm_ratings[$rating->code] = $rating; +} + +add_rating(new Rating("?", "Unrated", "unrated", 99999)); + +add_rating(new Rating("s", "Safe", "safe", 0)); +add_rating(new Rating("q", "Questionable", "questionable", 500)); +add_rating(new Rating("e", "Explicit", "explicit", 1000)); @include_once "data/config/ratings.conf.php"; class RatingSetEvent extends Event @@ -93,6 +105,11 @@ class RatingSetEvent extends Event } } +abstract class RatingsConfig { + const VERSION = "ext_ratings2_version"; + const USER_DEFAULTS = "ratings_default"; +} + class Ratings extends Extension { protected $db_support = [DatabaseDriver::MYSQL, DatabaseDriver::PGSQL]; @@ -100,18 +117,19 @@ class Ratings extends Extension private $search_regexp; - public function __construct() { + public function __construct() + { parent::__construct(); global $_shm_ratings; - $codes = implode("",array_keys($_shm_ratings)); + $codes = implode("", array_keys($_shm_ratings)); $search_terms = []; - foreach($_shm_ratings as $key=>$rating) { + foreach ($_shm_ratings as $key => $rating) { array_push($search_terms, $rating->search_term); } - $this->search_regexp = "/^rating[=|:](?:([".$codes."]+)|(". - implode("|", $search_terms)."|unknown))$/D"; + $this->search_regexp = "/^rating[=|:](?:(\*|[" . $codes . "]+)|(" . + implode("|", $search_terms) . "|unknown))$/D"; } public function get_priority(): int @@ -121,46 +139,59 @@ class Ratings extends Extension public function onInitExt(InitExtEvent $event) { - global $config, $_shm_user_classes, $_shm_ratings; - - if ($config->get_int("ext_ratings2_version") < 4) { + global $user, $config, $user_config, $_shm_user_classes, $_shm_ratings; + + if ($config->get_int(RatingsConfig::VERSION) < 4) { $this->install(); } - foreach(array_keys($_shm_user_classes) as $key){ - if($key=="base"||$key=="hellbanned") { + foreach (array_keys($_shm_user_classes) as $key) { + if ($key == "base" || $key == "hellbanned") { continue; } - $config->set_default_array("ext_rating_".$key."_privs", array_keys($_shm_ratings)); + $config->set_default_array("ext_rating_" . $key . "_privs", array_keys($_shm_ratings)); } + $user_config->set_default_array(RatingsConfig::USER_DEFAULTS, self::get_user_class_privs($user)); + } - + + + public function onUserOptionsBuilding(UserOptionsBuildingEvent $event) + { + global $user, $user_config; + + $event->add__html( + $this->theme->get_user_options($user, + self::get_user_default_ratings($user), + self::get_user_class_privs($user))); + } + public function onSetupBuilding(SetupBuildingEvent $event) { global $config, $_shm_user_classes, $_shm_ratings; - $ratings = array_values($_shm_ratings); - usort($ratings, function($a, $b) { - return $a->order <=> $b->order; - }); + $ratings = self::get_sorted_ratings(); + $options = []; - foreach($ratings as $key => $rating) { - $options[$rating->name] = $rating->code; - } + foreach ($ratings as $key => $rating) { + $options[$rating->name] = $rating->code; + } $sb = new SetupBlock("Image Ratings"); - foreach(array_keys($_shm_user_classes) as $key){ - if($key=="base"||$key=="hellbanned") { + $sb->start_table(); + foreach (array_keys($_shm_user_classes) as $key) { + if ($key == "base" || $key == "hellbanned") { continue; } - $sb->add_multichoice_option("ext_rating_".$key."_privs", $options, "
".$key.": "); + $sb->add_multichoice_option("ext_rating_" . $key . "_privs", $options, $key, true); } + $sb->end_table(); $event->panel->add_block($sb); } - + // public function onPostListBuilding(PostListBuildingEvent $event) // { // global $user; @@ -169,20 +200,20 @@ class Ratings extends Extension // } // } - + public function onDisplayingImage(DisplayingImageEvent $event) { global $user, $page; /** * Deny images upon insufficient permissions. **/ - $user_view_level = Ratings::get_user_privs($user); + $user_view_level = Ratings::get_user_class_privs($user); if (!in_array($event->image->rating, $user_view_level)) { $page->set_mode(PageMode::REDIRECT); $page->set_redirect(make_link("post/list")); } } - + public function onRatingSet(RatingSetEvent $event) { if (empty($event->image->rating)) { @@ -192,12 +223,12 @@ class Ratings extends Extension } $this->set_rating($event->image->id, $event->rating, $old_rating); } - + public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) { $event->add_part($this->theme->get_rater_html($event->image->id, $event->image->rating, $this->can_rate()), 80); } - + public function onImageInfoSet(ImageInfoSetEvent $event) { if ($this->can_rate() && isset($_POST["rating"])) { @@ -231,10 +262,10 @@ class Ratings extends Extension public function onSearchTermParse(SearchTermParseEvent $event) { global $user, $_shm_ratings; - + $matches = []; if (is_null($event->term) && $this->no_rating_query($event->context)) { - $set = Ratings::privs_to_sql(Ratings::get_user_privs($user)); + $set = Ratings::privs_to_sql(Ratings::get_user_default_ratings($user)); $event->add_querylet(new Querylet("rating IN ($set)")); } @@ -242,7 +273,11 @@ class Ratings extends Extension if (preg_match($this->search_regexp, strtolower($event->term), $matches)) { $ratings = $matches[1] ? $matches[1] : $matches[2][0]; - $ratings = array_intersect(str_split($ratings), Ratings::get_user_privs($user)); + if ($ratings == '*') { + $ratings = Ratings::get_user_class_privs($user); + } else { + $ratings = array_intersect(str_split($ratings), Ratings::get_user_class_privs($user)); + } $set = "'" . join("', '", $ratings) . "'"; $event->add_querylet(new Querylet("rating IN ($set)")); @@ -256,10 +291,11 @@ class Ratings extends Extension if (preg_match($this->search_regexp, strtolower($event->term), $matches) && $event->parse) { $ratings = $matches[1] ? $matches[1] : $matches[2][0]; - $ratings = array_intersect(str_split($ratings), Ratings::get_user_privs($user)); + + $ratings = array_intersect(str_split($ratings), Ratings::get_user_class_privs($user)); $rating = $ratings[0]; - + $image = Image::by_id($event->id); $re = new RatingSetEvent($image, $rating); @@ -271,13 +307,57 @@ class Ratings extends Extension $event->metatag = true; } } - + + + public function onAdminBuilding(AdminBuildingEvent $event) + { + global $database, $_shm_ratings; + + $results = $database->get_col("SELECT DISTINCT rating FROM images ORDER BY rating"); + $original_values = []; + foreach ($results as $result) { + if (array_key_exists($result, $_shm_ratings)) { + $original_values[$result] = $_shm_ratings[$result]->name; + } else { + $original_values[$result] = $result; + } + } + + + $this->theme->display_form($original_values, self::get_sorted_ratings()); + } + + public function onAdminAction(AdminActionEvent $event) + { + global $database, $user; + $action = $event->action; + switch ($action) { + case "update_ratings": + $event->redirect = true; + if(!array_key_exists("rating_old", $_POST) || empty($_POST["rating_old"])) { + return; + } + if(!array_key_exists("rating_new", $_POST) || empty($_POST["rating_new"])) { + return; + } + $old = $_POST["rating_old"]; + $new = $_POST["rating_new"]; + + if($user->can("bulk_edit_image_rating")) { + $database->execute("UPDATE images SET rating = :new WHERE rating = :old", ["new"=>$new, "old"=>$old ]); + } + + break; + } + } + + public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event) { global $user; if ($user->can(Permissions::BULK_EDIT_IMAGE_RATING)) { - $event->add_action("bulk_rate","Set Rating","",$this->theme->get_selection_rater_html("bulk_rating")); + $event->add_action("bulk_rate", "Set (R)ating", "r","", $this->theme->get_selection_rater_html(["?"])); } } @@ -287,11 +367,11 @@ class Ratings extends Extension switch ($event->action) { case "bulk_rate": - if (!isset($_POST['bulk_rating'])) { + if (!isset($_POST['rating'])) { return; } if ($user->can(Permissions::BULK_EDIT_IMAGE_RATING)) { - $rating = $_POST['bulk_rating']; + $rating = $_POST['rating']; $total = 0; foreach ($event->items as $image) { send_event(new RatingSetEvent($image, $rating)); @@ -305,8 +385,8 @@ class Ratings extends Extension public function onPageRequest(PageRequestEvent $event) { - global $user, $page; - + global $user, $page, $user_config; + if ($event->page_matches("admin/bulk_rate")) { if (!$user->can(Permissions::BULK_EDIT_IMAGE_RATING)) { throw new PermissionDeniedException(); @@ -317,9 +397,9 @@ class Ratings extends Extension if (count($images) == 0) { break; } - + reset($images); // rewind to first element in array. - + foreach ($images as $image) { send_event(new RatingSetEvent($image, $_POST['rating'])); } @@ -334,15 +414,65 @@ class Ratings extends Extension $page->set_redirect(make_link("post/list")); } } + + if ($event->page_matches("user_admin")) { + if (!$user->check_auth_token()) { + return; + } + switch ($event->get_arg(0)) { + case "default_ratings": + if (!array_key_exists("id", $_POST) || empty($_POST["id"])) { + return; + } + if (!array_key_exists("rating", $_POST) || empty($_POST["rating"])) { + return; + } + $id = intval($_POST["id"]); + if ($id != $user->id) { + throw new SCoreException("Cannot change another user's settings"); + } + $ratings = $_POST["rating"]; + + $user_config->set_array(RatingsConfig::USER_DEFAULTS, $ratings); + + $page->set_mode(PageMode::REDIRECT); + $page->set_redirect(make_link("user")); + + break; + } + } + } - public static function get_user_privs(User $user): array + public static function get_sorted_ratings(): array + { + global $_shm_ratings; + + $ratings = array_values($_shm_ratings); + usort($ratings, function ($a, $b) { + return $a->order <=> $b->order; + }); + return $ratings; + } + + + public static function get_user_class_privs(User $user): array { global $config; return $config->get_array("ext_rating_".$user->class->name."_privs"); } + public static function get_user_default_ratings(User $user): array + { + global $user_config; + + $available = self::get_user_class_privs($user); + $selected = $user_config->get_array(RatingsConfig::USER_DEFAULTS); + + return array_intersect($available, $selected); + } + public static function privs_to_sql(array $privs): string { $arr = []; @@ -402,18 +532,18 @@ class Ratings extends Extension { global $database, $config; - if ($config->get_int("ext_ratings2_version") < 1) { + if ($config->get_int(RatingsConfig::VERSION) < 1) { $database->Execute("ALTER TABLE images ADD COLUMN rating CHAR(1) NOT NULL DEFAULT 'u'"); $database->Execute("CREATE INDEX images__rating ON images(rating)"); - $config->set_int("ext_ratings2_version", 3); + $config->set_int(RatingsConfig::VERSION, 3); } - if ($config->get_int("ext_ratings2_version") < 2) { + if ($config->get_int(RatingsConfig::VERSION) < 2) { $database->Execute("CREATE INDEX images__rating ON images(rating)"); - $config->set_int("ext_ratings2_version", 2); + $config->set_int(RatingsConfig::VERSION, 2); } - if($config->get_int("ext_ratings2_version") < 3) { + if($config->get_int(RatingsConfig::VERSION) < 3) { $database->Execute("UPDATE images SET rating = 'u' WHERE rating is null"); switch ($database->get_driver_name()) { case DatabaseDriver::MYSQL: @@ -424,17 +554,23 @@ class Ratings extends Extension $database->Execute("ALTER TABLE images ALTER COLUMN rating SET NOT NULL"); break; } - $config->set_int("ext_ratings2_version", 3); + $config->set_int(RatingsConfig::VERSION, 3); } - if ($config->get_int("ext_ratings2_version") < 4) { + if ($config->get_int(RatingsConfig::VERSION) < 4) { $value = $config->get_string("ext_rating_anon_privs"); $config->set_array("ext_rating_anonymous_privs", str_split($value)); $value = $config->get_string("ext_rating_user_privs"); $config->set_array("ext_rating_user_privs", str_split($value)); $value = $config->get_string("ext_rating_admin_privs"); $config->set_array("ext_rating_admin_privs", str_split($value)); - $config->set_int("ext_ratings2_version", 4); + + $config->set_int(RatingsConfig::VERSION, 4); + } + + if ($config->get_int(RatingsConfig::VERSION) < 5) { + + $config->set_int(RatingsConfig::VERSION, 5); } } diff --git a/ext/rating/theme.php b/ext/rating/theme.php index 093feeac..48facc11 100644 --- a/ext/rating/theme.php +++ b/ext/rating/theme.php @@ -12,7 +12,7 @@ class RatingsTheme extends Themelet ".($can_rate ? " $human_rating - ".$this->get_selection_rater_html($rating)." + ".$this->get_selection_rater_html([$rating])." " : " $human_rating @@ -23,6 +23,29 @@ class RatingsTheme extends Themelet return $html; } + + public function display_form(array $current_ratings, array $available_ratings) + { + global $page, $database; + + $html = make_form(make_link("admin/update_ratings"))." + + +
Change
To
+ \n"; + $page->add_block(new Block("Update Ratings", $html)); + } + + + // public function display_bulk_rater(string $terms) // { // global $page; @@ -41,17 +64,21 @@ class RatingsTheme extends Themelet // $page->add_block(new Block("List Controls", $html, "left")); // } - public function get_selection_rater_html(String $selected_option, String $id = "rating") { + public function get_selection_rater_html(array $selected_options, bool $multiple = false, array $available_options = null) { global $_shm_ratings; - $output = ""; + + $options = Ratings::get_sorted_ratings(); + + foreach($options as $option) { + if($available_options!=null && !in_array($option->code, $available_options)) { + continue; + } + + $output .= ""; } return $output.""; } @@ -82,5 +109,27 @@ class RatingsTheme extends Themelet } $output .= ""; return $output; + public function get_user_options(User $user, array $selected_ratings, array $available_ratings): string + { + $html = " +

".make_form(make_link("user_admin/default_ratings"))." + + + + + + + + + + + + +
Default Rating Filter
This controls the default rating search results will be filtered by, and nothing else. To override in your search results, add rating:* to your search.
+ ".$this->get_selection_rater_html($selected_ratings, true, $available_ratings)." +
+ + "; + return $html; } } diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php index 2ea12cb9..4be2a25d 100644 --- a/ext/upgrade/main.php +++ b/ext/upgrade/main.php @@ -243,6 +243,23 @@ class Upgrade extends Extension $config->set_bool("in_upgrade", false); } + if ($config->get_int("db_version") < 19) { + $config->set_bool("in_upgrade", true); + $config->set_int("db_version", 19); + + log_info("upgrade", "Updating to new unrated code"); + + if ($database->get_driver_name()==DatabaseDriver::PGSQL) { // These updates can take a little bit + $database->execute("SET statement_timeout TO 300000;"); + } + $database->execute("UPDATE images SET rating = :new WHERE rating = :old", ["new"=>'?', "old"=>'u' ]); + + + log_info("upgrade", "Database at version 19"); + $config->set_bool("in_upgrade", false); + } + + } public function get_priority(): int diff --git a/themes/danbooru/view.theme.php b/themes/danbooru/view.theme.php index 51adbe1b..0886f297 100644 --- a/themes/danbooru/view.theme.php +++ b/themes/danbooru/view.theme.php @@ -47,8 +47,8 @@ class CustomViewImageTheme extends ViewImageTheme } if (ext_is_live("Ratings")) { - if ($image->rating == null || $image->rating == "u") { - $image->rating = "u"; + if ($image->rating == null || $image->rating == "?") { + $image->rating = "?"; } $h_rating = Ratings::rating_to_human($image->rating); $html .= "
Rating: $h_rating"; diff --git a/themes/danbooru2/view.theme.php b/themes/danbooru2/view.theme.php index 7f3439b5..ff9c4ce0 100644 --- a/themes/danbooru2/view.theme.php +++ b/themes/danbooru2/view.theme.php @@ -48,8 +48,8 @@ class CustomViewImageTheme extends ViewImageTheme } if (ext_is_live("Ratings")) { - if ($image->rating == null || $image->rating == "u") { - $image->rating = "u"; + if ($image->rating == null || $image->rating == "?") { + $image->rating = "?"; } if (ext_is_live("Ratings")) { $h_rating = Ratings::rating_to_human($image->rating); diff --git a/themes/lite/view.theme.php b/themes/lite/view.theme.php index f9ce414f..91767252 100644 --- a/themes/lite/view.theme.php +++ b/themes/lite/view.theme.php @@ -48,8 +48,8 @@ class CustomViewImageTheme extends ViewImageTheme } if (ext_is_live("Ratings")) { - if ($image->rating == null || $image->rating == "u") { - $image->rating = "u"; + if ($image->rating == null || $image->rating == "?") { + $image->rating = "?"; } $h_rating = Ratings::rating_to_human($image->rating); $html .= "
Rating: $h_rating"; From 40be8f045a3a9fe215a365195ca9313f72b75b47 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 27 Jun 2019 09:48:07 -0500 Subject: [PATCH 05/20] Changed to use user_config extension --- ext/rating/main.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ext/rating/main.php b/ext/rating/main.php index 1a83b1c2..e89fa38d 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -139,7 +139,7 @@ class Ratings extends Extension public function onInitExt(InitExtEvent $event) { - global $user, $config, $user_config, $_shm_user_classes, $_shm_ratings; + global $user, $config, $_shm_user_classes, $_shm_ratings; if ($config->get_int(RatingsConfig::VERSION) < 4) { $this->install(); @@ -151,12 +151,11 @@ class Ratings extends Extension } $config->set_default_array("ext_rating_" . $key . "_privs", array_keys($_shm_ratings)); } - - - $user_config->set_default_array(RatingsConfig::USER_DEFAULTS, self::get_user_class_privs($user)); - } + public function onInitUserConfig(InitUserConfigEvent $event) { + $event->user_config->set_default_array(RatingsConfig::USER_DEFAULTS, self::get_user_class_privs($event->user)); + } public function onUserOptionsBuilding(UserOptionsBuildingEvent $event) { From 91b46d65985196cc3ac5e9c274880222515c665d Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 27 Jun 2019 09:47:22 -0500 Subject: [PATCH 06/20] Moved user config stuff into an extension --- core/_bootstrap.php | 5 --- core/sys_config.php | 2 +- ext/upgrade/main.php | 23 ++------------ ext/user_config/main.php | 69 ++++++++++++++++++++++++++++++++++++++++ index.php | 3 ++ 5 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 ext/user_config/main.php diff --git a/core/_bootstrap.php b/core/_bootstrap.php index c9869a36..a003ed6e 100644 --- a/core/_bootstrap.php +++ b/core/_bootstrap.php @@ -52,11 +52,6 @@ unset($themelet); $page = class_exists("CustomPage") ? new CustomPage() : new Page(); $_tracer->end(); -$_shm_ctx->log_start("Loading user information"); -$user = _get_user(); -$user_config = new DatabaseConfig($database, "user_config","user_id", $user->id); -$_shm_ctx->log_endok(); - // hook up event handlers $_tracer->begin("Loading extensions"); _load_event_listeners(); diff --git a/core/sys_config.php b/core/sys_config.php index 5dc80459..13de59a4 100644 --- a/core/sys_config.php +++ b/core/sys_config.php @@ -40,7 +40,7 @@ _d("SEARCH_ACCEL", false); // boolean use search accelerator _d("WH_SPLITS", 1); // int how many levels of subfolders to put in the warehouse _d("VERSION", '2.7-beta'); // string shimmie version _d("TIMEZONE", null); // string timezone -_d("CORE_EXTS", "bbcode,user,mail,upload,image,view,handle_pixel,ext_manager,setup,upgrade,handle_404,handle_static,comment,tag_list,index,tag_edit,alias_editor,media,help_pages,system"); // extensions to always enable +_d("CORE_EXTS", "bbcode,user,mail,upload,image,view,handle_pixel,ext_manager,setup,upgrade,handle_404,handle_static,comment,tag_list,index,tag_edit,alias_editor,media,help_pages,system,user_config"); // extensions to always enable _d("EXTRA_EXTS", ""); // string optional extra extensions _d("BASE_URL", null); // string force a specific base URL (default is auto-detect) _d("MIN_PHP_VERSION", '7.1');// string minimum supported PHP version diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php index 4be2a25d..2729b1ce 100644 --- a/ext/upgrade/main.php +++ b/ext/upgrade/main.php @@ -224,29 +224,11 @@ class Upgrade extends Extension $config->set_bool("in_upgrade", false); } + if ($config->get_int("db_version") < 18) { $config->set_bool("in_upgrade", true); $config->set_int("db_version", 18); - log_info("upgrade", "Adding user config table"); - - $database->create_table("user_config", " - user_id INTEGER NOT NULL, - name VARCHAR(128) NOT NULL, - value TEXT, - PRIMARY KEY (user_id, name), - FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE - "); - $database->execute("CREATE INDEX user_config_user_id_idx ON user_config(user_id)"); - - log_info("upgrade", "Database at version 18"); - $config->set_bool("in_upgrade", false); - } - - if ($config->get_int("db_version") < 19) { - $config->set_bool("in_upgrade", true); - $config->set_int("db_version", 19); - log_info("upgrade", "Updating to new unrated code"); if ($database->get_driver_name()==DatabaseDriver::PGSQL) { // These updates can take a little bit @@ -255,11 +237,10 @@ class Upgrade extends Extension $database->execute("UPDATE images SET rating = :new WHERE rating = :old", ["new"=>'?', "old"=>'u' ]); - log_info("upgrade", "Database at version 19"); + log_info("upgrade", "Database at version 18"); $config->set_bool("in_upgrade", false); } - } public function get_priority(): int diff --git a/ext/user_config/main.php b/ext/user_config/main.php new file mode 100644 index 00000000..ed728101 --- /dev/null +++ b/ext/user_config/main.php @@ -0,0 +1,69 @@ + + * Description: Provides system-wide support for user-specific settings + * Visibility: admin + */ + +// The user object doesn't exist until after database setup operations and the first wave of InitExtEvents, +// so we can't reliably access this data until then. This event is triggered by the system after all of that is done. +class InitUserConfigEvent extends Event +{ + public $user; + public $user_config; + + public function __construct(User $user) + { + $this->user = $user; + } +} + +class UserConfig extends Extension +{ + private const VERSION = "ext_user_config_version"; + + public function onInitExt(InitExtEvent $event) + { + global $config; + + if ($config->get_int(self::VERSION,0)<1) { + $this->install(); + } + } + + public function onInitUserConfig(InitUserConfigEvent $event) { + global $database, $user_config; + + $user_config = new DatabaseConfig($database, "user_config", "user_id", $event->user->id); + $event->user_config = $user_config; + } + + private function install(): void + { + global $config, $database; + + if ($config->get_int(self::VERSION,0) < 1) { + + log_info("upgrade", "Adding user config table"); + + $database->create_table("user_config", " + user_id INTEGER NOT NULL, + name VARCHAR(128) NOT NULL, + value TEXT, + PRIMARY KEY (user_id, name), + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + "); + $database->execute("CREATE INDEX user_config_user_id_idx ON user_config(user_id)"); + + $config->set_int(self::VERSION, 1); + } + } + + + // This needs to happen before any other events, but after db upgrade + public function get_priority(): int + { + return 6; + } +} diff --git a/index.php b/index.php index 95300516..67855129 100644 --- a/index.php +++ b/index.php @@ -89,7 +89,10 @@ $_tracer->begin($_SERVER["REQUEST_URI"] ?? "No Request"); try { + // start the page generation waterfall + $user = _get_user(); + send_event(new InitUserConfigEvent($user)); if (PHP_SAPI === 'cli' || PHP_SAPI == 'phpdbg') { send_event(new CommandEvent($argv)); } else { From 5e87dff03352b5d5d410961e7fcfdded5b899cfb Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 27 Jun 2019 15:13:49 -0500 Subject: [PATCH 07/20] Adjustments to rating upgrade --- ext/rating/main.php | 11 ++++++----- ext/upgrade/main.php | 17 ----------------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/ext/rating/main.php b/ext/rating/main.php index e89fa38d..63a859b7 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -564,13 +564,14 @@ class Ratings extends Extension $value = $config->get_string("ext_rating_admin_privs"); $config->set_array("ext_rating_admin_privs", str_split($value)); + + if ($database->get_driver_name()==DatabaseDriver::PGSQL) { // These updates can take a little bit + $database->execute("SET statement_timeout TO 300000;"); + } + $database->execute("UPDATE images SET rating = :new WHERE rating = :old", ["new"=>'?', "old"=>'u' ]); + $config->set_int(RatingsConfig::VERSION, 4); } - - if ($config->get_int(RatingsConfig::VERSION) < 5) { - - $config->set_int(RatingsConfig::VERSION, 5); - } } private function set_rating(int $image_id, string $rating, string $old_rating) diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php index 2729b1ce..c818a526 100644 --- a/ext/upgrade/main.php +++ b/ext/upgrade/main.php @@ -224,23 +224,6 @@ class Upgrade extends Extension $config->set_bool("in_upgrade", false); } - - if ($config->get_int("db_version") < 18) { - $config->set_bool("in_upgrade", true); - $config->set_int("db_version", 18); - - log_info("upgrade", "Updating to new unrated code"); - - if ($database->get_driver_name()==DatabaseDriver::PGSQL) { // These updates can take a little bit - $database->execute("SET statement_timeout TO 300000;"); - } - $database->execute("UPDATE images SET rating = :new WHERE rating = :old", ["new"=>'?', "old"=>'u' ]); - - - log_info("upgrade", "Database at version 18"); - $config->set_bool("in_upgrade", false); - } - } public function get_priority(): int From b2193cb6f16da7cc83591de4342218c4150fc5e3 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 27 Jun 2019 19:49:58 -0500 Subject: [PATCH 08/20] Adjusted rating array usage --- ext/comment/main.php | 2 +- ext/featured/main.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/comment/main.php b/ext/comment/main.php index ad2e9507..fb35da18 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -414,7 +414,7 @@ class CommentList extends Extension $image = Image::by_id($row["image_id"]); if ( ext_is_live("Ratings") && !is_null($image) && - strpos($user_ratings, $image->rating) === false + !in_array($image->rating, $user_ratings) ) { $image = null; // this is "clever", I may live to regret it } diff --git a/ext/featured/main.php b/ext/featured/main.php index eac762a5..5b7a4845 100644 --- a/ext/featured/main.php +++ b/ext/featured/main.php @@ -74,7 +74,7 @@ class Featured extends Extension } if (!is_null($image)) { if (ext_is_live("Ratings")) { - if (strpos(Ratings::get_user_class_privs($user), $image->rating) === false) { + if (!in_array($image->rating, Ratings::get_user_class_privs($user))) { return; } } From 3560a19f79061073951ced123f7d0f53ddd2e21d Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 27 Jun 2019 20:02:21 -0500 Subject: [PATCH 09/20] Updated tests to generate user config --- tests/bootstrap.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 40166c21..672a085a 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -16,6 +16,9 @@ if (is_null(User::by_name("demo"))) { $userPage->onUserCreation(new UserCreationEvent("test", "test", "")); } +global $user; +send_event(new InitUserConfigEvent($user)); + abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase { private $images = []; @@ -132,6 +135,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase { global $user; $user = User::by_name('demo'); + send_event(new InitUserConfigEvent($user)); $this->assertNotNull($user); } @@ -139,6 +143,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase { global $user; $user = User::by_name('test'); + send_event(new InitUserConfigEvent($user)); $this->assertNotNull($user); } From aa5a04fbd326eadf91f2723ea166b0161b2c1304 Mon Sep 17 00:00:00 2001 From: matthew Date: Thu, 27 Jun 2019 20:56:50 -0500 Subject: [PATCH 10/20] Further adjusting tests for user config Renamed Rating to ImageRating to prevent test system from trying to make an extension of it --- ext/rating/main.php | 14 +++++++------- tests/bootstrap.php | 8 +++----- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/ext/rating/main.php b/ext/rating/main.php index 63a859b7..52035bdc 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -20,12 +20,12 @@ */ /** - * @global Rating[] $_shm_ratings + * @global ImageRating[] $_shm_ratings */ global $_shm_ratings; $_shm_ratings = []; -class Rating { +class ImageRating { /** * @var string */ @@ -71,7 +71,7 @@ function clear_ratings() { } } -function add_rating(Rating $rating) { +function add_rating(ImageRating $rating) { global $_shm_ratings; if($rating->code=="?"&&array_key_exists("?",$_shm_ratings)) { @@ -80,11 +80,11 @@ function add_rating(Rating $rating) { $_shm_ratings[$rating->code] = $rating; } -add_rating(new Rating("?", "Unrated", "unrated", 99999)); +add_rating(new ImageRating("?", "Unrated", "unrated", 99999)); -add_rating(new Rating("s", "Safe", "safe", 0)); -add_rating(new Rating("q", "Questionable", "questionable", 500)); -add_rating(new Rating("e", "Explicit", "explicit", 1000)); +add_rating(new ImageRating("s", "Safe", "safe", 0)); +add_rating(new ImageRating("q", "Questionable", "questionable", 500)); +add_rating(new ImageRating("e", "Explicit", "explicit", 1000)); @include_once "data/config/ratings.conf.php"; class RatingSetEvent extends Event diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 672a085a..f40f5800 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -16,9 +16,6 @@ if (is_null(User::by_name("demo"))) { $userPage->onUserCreation(new UserCreationEvent("test", "test", "")); } -global $user; -send_event(new InitUserConfigEvent($user)); - abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase { private $images = []; @@ -135,16 +132,16 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase { global $user; $user = User::by_name('demo'); - send_event(new InitUserConfigEvent($user)); $this->assertNotNull($user); + send_event(new InitUserConfigEvent($user)); } protected function log_in_as_user() { global $user; $user = User::by_name('test'); - send_event(new InitUserConfigEvent($user)); $this->assertNotNull($user); + send_event(new InitUserConfigEvent($user)); } protected function log_out() @@ -152,6 +149,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase global $user, $config; $user = User::by_id($config->get_int("anon_id", 0)); $this->assertNotNull($user); + send_event(new InitUserConfigEvent($user)); } // post things From e065c8b789079b23117fef7b16e9a77407771cc2 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 27 Jun 2019 21:58:24 -0500 Subject: [PATCH 11/20] Set column defaults --- ext/rating/main.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ext/rating/main.php b/ext/rating/main.php index 52035bdc..6ec70326 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -532,7 +532,7 @@ class Ratings extends Extension global $database, $config; if ($config->get_int(RatingsConfig::VERSION) < 1) { - $database->Execute("ALTER TABLE images ADD COLUMN rating CHAR(1) NOT NULL DEFAULT 'u'"); + $database->Execute("ALTER TABLE images ADD COLUMN rating CHAR(1) NOT NULL DEFAULT '?'"); $database->Execute("CREATE INDEX images__rating ON images(rating)"); $config->set_int(RatingsConfig::VERSION, 3); } @@ -565,9 +565,19 @@ class Ratings extends Extension $config->set_array("ext_rating_admin_privs", str_split($value)); + switch ($database->get_driver_name()) { + case DatabaseDriver::MYSQL: + $database->Execute("ALTER TABLE images CHANGE rating rating CHAR(1) NOT NULL DEFAULT '?'"); + break; + case DatabaseDriver::PGSQL: + $database->Execute("ALTER TABLE images ALTER COLUMN rating SET DEFAULT '?'"); + break; + } + if ($database->get_driver_name()==DatabaseDriver::PGSQL) { // These updates can take a little bit $database->execute("SET statement_timeout TO 300000;"); } + $database->execute("UPDATE images SET rating = :new WHERE rating = :old", ["new"=>'?', "old"=>'u' ]); $config->set_int(RatingsConfig::VERSION, 4); From ee3f53e108b2b7b3435ab796e18283745be48bc6 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Mon, 24 Jun 2019 17:32:09 -0500 Subject: [PATCH 12/20] Changed related tags queries to run more efficiently, filter out the starting tags, and filter out any tags starting with tagme, rather than just tagme. --- ext/tag_list/main.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/ext/tag_list/main.php b/ext/tag_list/main.php index 9d0e8a53..33efe3b8 100644 --- a/ext/tag_list/main.php +++ b/ext/tag_list/main.php @@ -419,20 +419,16 @@ class TagList extends Extension $query = " SELECT t3.tag AS tag, t3.count AS calc_count, it3.tag_id - FROM - image_tags AS it1, - image_tags AS it2, - image_tags AS it3, - tags AS t1, - tags AS t3 + FROM image_tags AS it1 -- Starting image's tags + INNER JOIN tags AS t1 ON t1.id = it1.tag_id AND t1.tag NOT LIKE 'tagme%' + -- Get images with the same tags as the starting image + INNER JOIN image_tags AS it2 ON it1.tag_id=it2.tag_id + -- Get the tags from those other images except the same as the starting tags + INNER JOIN image_tags AS it3 ON it2.image_id=it3.image_id + LEFT JOIN image_tags it4 ON it4.image_id = it1.image_id AND it4.tag_id = it3.tag_id + INNER JOIN tags AS t3 ON t3.id = it3.tag_id AND t3.tag NOT LIKE 'tagme%' WHERE it1.image_id=:image_id - AND it1.tag_id=it2.tag_id - AND it2.image_id=it3.image_id - AND t1.tag != 'tagme' - AND t3.tag != 'tagme' - AND t1.id = it1.tag_id - AND t3.id = it3.tag_id GROUP BY it3.tag_id, t3.tag, t3.count ORDER BY calc_count DESC LIMIT :tag_list_length @@ -527,6 +523,9 @@ class TagList extends Extension $tag_id_array = []; $tags_ok = true; foreach ($wild_tags as $tag) { + if ($tag[0] == "-" || strpos($tag, "tagme")===0) { + continue; + } $tag = str_replace("*", "%", $tag); $tag = str_replace("?", "_", $tag); $tag_ids = $database->get_col("SELECT id FROM tags WHERE tag LIKE :tag AND count < 25000", ["tag" => $tag]); From 0fa2adfdd5d538135ca98d503558e8f57fea9a20 Mon Sep 17 00:00:00 2001 From: matthew Date: Thu, 27 Jun 2019 08:12:15 -0500 Subject: [PATCH 13/20] Added $user_config global based on existing config object for storing user-specific settings. Added event to the user page so that extensions can hook into it, providing user-specific setting controls --- core/_bootstrap.php | 5 +++ core/config.php | 83 +++++++++++++++++++++++++++++++++++++++----- ext/upgrade/main.php | 17 +++++++++ ext/user/main.php | 22 ++++++++++++ ext/user/theme.php | 21 ++++++----- index.php | 1 - 6 files changed, 129 insertions(+), 20 deletions(-) diff --git a/core/_bootstrap.php b/core/_bootstrap.php index a003ed6e..c9869a36 100644 --- a/core/_bootstrap.php +++ b/core/_bootstrap.php @@ -52,6 +52,11 @@ unset($themelet); $page = class_exists("CustomPage") ? new CustomPage() : new Page(); $_tracer->end(); +$_shm_ctx->log_start("Loading user information"); +$user = _get_user(); +$user_config = new DatabaseConfig($database, "user_config","user_id", $user->id); +$_shm_ctx->log_endok(); + // hook up event handlers $_tracer->begin("Loading extensions"); _load_event_listeners(); diff --git a/core/config.php b/core/config.php index c9fb225b..6b1ac6fa 100644 --- a/core/config.php +++ b/core/config.php @@ -20,6 +20,11 @@ interface Config */ public function set_int(string $name, ?string $value): void; + /** + * Set a configuration option to a new value, regardless of what the value is at the moment. + */ + public function set_float(string $name, ?string $value): void; + /** * Set a configuration option to a new value, regardless of what the value is at the moment. */ @@ -48,6 +53,16 @@ interface Config */ public function set_default_int(string $name, int $value): void; + /** + * Set a configuration option to a new value, if there is no value currently. + * + * Extensions should generally call these from their InitExtEvent handlers. + * This has the advantage that the values will show up in the "advanced" setup + * page where they can be modified, while calling get_* with a "default" + * parameter won't show up. + */ + public function set_default_float(string $name, float $value): void; + /** * Set a configuration option to a new value, if there is no value currently. * @@ -85,6 +100,11 @@ interface Config */ public function get_int(string $name, ?int $default=null): ?int; + /** + * Pick a value out of the table by name, cast to the appropriate data type. + */ + public function get_float(string $name, ?float $default=null): ?float; + /** * Pick a value out of the table by name, cast to the appropriate data type. */ @@ -119,6 +139,12 @@ abstract class BaseConfig implements Config $this->save($name); } + public function set_float(string $name, ?string $value): void + { + $this->values[$name] = $value; + $this->save($name); + } + public function set_string(string $name, ?string $value): void { $this->values[$name] = $value; @@ -131,9 +157,13 @@ abstract class BaseConfig implements Config $this->save($name); } - public function set_array(string $name, array $value): void + public function set_array(string $name, ?array $value): void { - $this->values[$name] = implode(",", $value); + if($value!=null) { + $this->values[$name] = implode(",", $value); + } else { + $this->values[$name] = null; + } $this->save($name); } @@ -279,19 +309,41 @@ class DatabaseConfig extends BaseConfig /** @var Database */ private $database = null; - public function __construct(Database $database) + private $table_name; + private $sub_column; + private $sub_value; + + public function __construct(Database $database, string $table_name = "config", + string $sub_column = null, string $sub_value = null) { $this->database = $database; + $this->table_name = $table_name; + $this->sub_value = $sub_value; + $this->sub_column = $sub_column; - $cached = $this->database->cache->get("config"); + $cache_name = "config"; + if(!empty($sub_value)) { + $cache_name .= "_".$sub_value; + } + + $cached = $this->database->cache->get($cache_name); if ($cached) { $this->values = $cached; } else { $this->values = []; - foreach ($this->database->get_all("SELECT name, value FROM config") as $row) { + + $query = "SELECT name, value FROM {$this->table_name}"; + $args = []; + + if(!empty($sub_column)&&!empty($sub_value)) { + $query .= " WHERE $sub_column = :sub_value"; + $args["sub_value"] = $sub_value; + } + + foreach ($this->database->get_all($query, $args ) as $row) { $this->values[$row["name"]] = $row["value"]; } - $this->database->cache->set("config", $this->values); + $this->database->cache->set($cache_name, $this->values); } } @@ -303,8 +355,23 @@ class DatabaseConfig extends BaseConfig $this->save($name); } } else { - $this->database->Execute("DELETE FROM config WHERE name = :name", ["name"=>$name]); - $this->database->Execute("INSERT INTO config VALUES (:name, :value)", ["name"=>$name, "value"=>$this->values[$name]]); + $query = "DELETE FROM {$this->table_name} WHERE name = :name"; + $args = ["name"=>$name]; + $cols = ["name","value"]; + $params = [":name",":value"]; + if(!empty($this->sub_column)&&!empty($this->sub_value)) { + $query .= " AND $this->sub_column = :sub_value"; + $args["sub_value"] = $this->sub_value; + $cols[] = $this->sub_column; + $params[] = ":sub_value"; + } + + $this->database->Execute($query, $args); + + $args["value"] =$this->values[$name]; + $this->database->Execute( + "INSERT INTO {$this->table_name} (".join(",",$cols).") VALUES (".join(",",$params).")", + $args); } // rather than deleting and having some other request(s) do a thundering // herd of race-conditioned updates, just save the updated version once here diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php index 3bad2d52..2ea12cb9 100644 --- a/ext/upgrade/main.php +++ b/ext/upgrade/main.php @@ -224,7 +224,24 @@ class Upgrade extends Extension $config->set_bool("in_upgrade", false); } + if ($config->get_int("db_version") < 18) { + $config->set_bool("in_upgrade", true); + $config->set_int("db_version", 18); + log_info("upgrade", "Adding user config table"); + + $database->create_table("user_config", " + user_id INTEGER NOT NULL, + name VARCHAR(128) NOT NULL, + value TEXT, + PRIMARY KEY (user_id, name), + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + "); + $database->execute("CREATE INDEX user_config_user_id_idx ON user_config(user_id)"); + + log_info("upgrade", "Database at version 18"); + $config->set_bool("in_upgrade", false); + } } diff --git a/ext/user/main.php b/ext/user/main.php index 4bb726d0..d6a59f8d 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -19,6 +19,17 @@ class UserBlockBuildingEvent extends Event } } +class UserOptionsBuildingEvent extends Event +{ + /** @var array */ + public $parts = []; + + public function add__html(string $html) + { + $this->parts[] = $html; + } +} + class UserPageBuildingEvent extends Event { /** @var User */ @@ -254,6 +265,17 @@ class UserPage extends Extension ksort($event->stats); $this->theme->display_user_page($event->display_user, $event->stats); + + if (!$user->is_anonymous()) { + if ($user->id == $event->display_user->id || $user->can("edit_user_info")) { + $uobe = new UserOptionsBuildingEvent(); + send_event($uobe); + + $page->add_block(new Block("Options", $this->theme->build_options($event->display_user, $uobe), "main", 60)); + } + } + + if ($user->id == $event->display_user->id) { $ubbe = new UserBlockBuildingEvent(); send_event($ubbe); diff --git a/ext/user/theme.php b/ext/user/theme.php index dab26e8c..a1650db4 100644 --- a/ext/user/theme.php +++ b/ext/user/theme.php @@ -243,14 +243,9 @@ class UserPageTheme extends Themelet $page->add_block(new NavBlock()); $page->add_block(new Block("Stats", join("
", $stats), "main", 10)); - if (!$user->is_anonymous()) { - if ($user->id == $duser->id || $user->can("edit_user_info")) { - $page->add_block(new Block("Options", $this->build_options($duser), "main", 60)); - } - } } - protected function build_options(User $duser) + public function build_options(User $duser, UserOptionsBuildingEvent $event) { global $config, $user; $html = ""; @@ -266,7 +261,7 @@ class UserPageTheme extends Themelet - "; +

"; } $html .= " @@ -285,7 +280,7 @@ class UserPageTheme extends Themelet - +

".make_form(make_link("user_admin/change_email"))." @@ -294,7 +289,7 @@ class UserPageTheme extends Themelet
- "; +

"; $i_user_id = int_escape($duser->id); @@ -316,7 +311,7 @@ class UserPageTheme extends Themelet - "; +

"; } if ($user->can(Permissions::DELETE_USER)) { @@ -337,8 +332,12 @@ class UserPageTheme extends Themelet - "; +

"; } + foreach ($event->parts as $part) { + $html .= $part; + } + } return $html; } diff --git a/index.php b/index.php index fdde0b16..95300516 100644 --- a/index.php +++ b/index.php @@ -90,7 +90,6 @@ $_tracer->begin($_SERVER["REQUEST_URI"] ?? "No Request"); try { // start the page generation waterfall - $user = _get_user(); if (PHP_SAPI === 'cli' || PHP_SAPI == 'phpdbg') { send_event(new CommandEvent($argv)); } else { From 85b883ed7a7e3796a273c7af028bf02e84207b66 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 27 Jun 2019 09:47:22 -0500 Subject: [PATCH 14/20] Moved user config stuff into an extension --- core/_bootstrap.php | 5 --- core/sys_config.php | 2 +- ext/upgrade/main.php | 19 ----------- ext/user_config/main.php | 69 ++++++++++++++++++++++++++++++++++++++++ index.php | 3 ++ 5 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 ext/user_config/main.php diff --git a/core/_bootstrap.php b/core/_bootstrap.php index c9869a36..a003ed6e 100644 --- a/core/_bootstrap.php +++ b/core/_bootstrap.php @@ -52,11 +52,6 @@ unset($themelet); $page = class_exists("CustomPage") ? new CustomPage() : new Page(); $_tracer->end(); -$_shm_ctx->log_start("Loading user information"); -$user = _get_user(); -$user_config = new DatabaseConfig($database, "user_config","user_id", $user->id); -$_shm_ctx->log_endok(); - // hook up event handlers $_tracer->begin("Loading extensions"); _load_event_listeners(); diff --git a/core/sys_config.php b/core/sys_config.php index 5dc80459..13de59a4 100644 --- a/core/sys_config.php +++ b/core/sys_config.php @@ -40,7 +40,7 @@ _d("SEARCH_ACCEL", false); // boolean use search accelerator _d("WH_SPLITS", 1); // int how many levels of subfolders to put in the warehouse _d("VERSION", '2.7-beta'); // string shimmie version _d("TIMEZONE", null); // string timezone -_d("CORE_EXTS", "bbcode,user,mail,upload,image,view,handle_pixel,ext_manager,setup,upgrade,handle_404,handle_static,comment,tag_list,index,tag_edit,alias_editor,media,help_pages,system"); // extensions to always enable +_d("CORE_EXTS", "bbcode,user,mail,upload,image,view,handle_pixel,ext_manager,setup,upgrade,handle_404,handle_static,comment,tag_list,index,tag_edit,alias_editor,media,help_pages,system,user_config"); // extensions to always enable _d("EXTRA_EXTS", ""); // string optional extra extensions _d("BASE_URL", null); // string force a specific base URL (default is auto-detect) _d("MIN_PHP_VERSION", '7.1');// string minimum supported PHP version diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php index 2ea12cb9..c818a526 100644 --- a/ext/upgrade/main.php +++ b/ext/upgrade/main.php @@ -224,25 +224,6 @@ class Upgrade extends Extension $config->set_bool("in_upgrade", false); } - if ($config->get_int("db_version") < 18) { - $config->set_bool("in_upgrade", true); - $config->set_int("db_version", 18); - - log_info("upgrade", "Adding user config table"); - - $database->create_table("user_config", " - user_id INTEGER NOT NULL, - name VARCHAR(128) NOT NULL, - value TEXT, - PRIMARY KEY (user_id, name), - FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE - "); - $database->execute("CREATE INDEX user_config_user_id_idx ON user_config(user_id)"); - - log_info("upgrade", "Database at version 18"); - $config->set_bool("in_upgrade", false); - } - } public function get_priority(): int diff --git a/ext/user_config/main.php b/ext/user_config/main.php new file mode 100644 index 00000000..ed728101 --- /dev/null +++ b/ext/user_config/main.php @@ -0,0 +1,69 @@ + + * Description: Provides system-wide support for user-specific settings + * Visibility: admin + */ + +// The user object doesn't exist until after database setup operations and the first wave of InitExtEvents, +// so we can't reliably access this data until then. This event is triggered by the system after all of that is done. +class InitUserConfigEvent extends Event +{ + public $user; + public $user_config; + + public function __construct(User $user) + { + $this->user = $user; + } +} + +class UserConfig extends Extension +{ + private const VERSION = "ext_user_config_version"; + + public function onInitExt(InitExtEvent $event) + { + global $config; + + if ($config->get_int(self::VERSION,0)<1) { + $this->install(); + } + } + + public function onInitUserConfig(InitUserConfigEvent $event) { + global $database, $user_config; + + $user_config = new DatabaseConfig($database, "user_config", "user_id", $event->user->id); + $event->user_config = $user_config; + } + + private function install(): void + { + global $config, $database; + + if ($config->get_int(self::VERSION,0) < 1) { + + log_info("upgrade", "Adding user config table"); + + $database->create_table("user_config", " + user_id INTEGER NOT NULL, + name VARCHAR(128) NOT NULL, + value TEXT, + PRIMARY KEY (user_id, name), + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + "); + $database->execute("CREATE INDEX user_config_user_id_idx ON user_config(user_id)"); + + $config->set_int(self::VERSION, 1); + } + } + + + // This needs to happen before any other events, but after db upgrade + public function get_priority(): int + { + return 6; + } +} diff --git a/index.php b/index.php index 95300516..67855129 100644 --- a/index.php +++ b/index.php @@ -89,7 +89,10 @@ $_tracer->begin($_SERVER["REQUEST_URI"] ?? "No Request"); try { + // start the page generation waterfall + $user = _get_user(); + send_event(new InitUserConfigEvent($user)); if (PHP_SAPI === 'cli' || PHP_SAPI == 'phpdbg') { send_event(new CommandEvent($argv)); } else { From 9bc5bb33745b273a4fb1647cc2f6ae6d72e60684 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 27 Jun 2019 20:02:21 -0500 Subject: [PATCH 15/20] Updated tests to generate user config --- tests/bootstrap.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 40166c21..f40f5800 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -133,6 +133,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase global $user; $user = User::by_name('demo'); $this->assertNotNull($user); + send_event(new InitUserConfigEvent($user)); } protected function log_in_as_user() @@ -140,6 +141,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase global $user; $user = User::by_name('test'); $this->assertNotNull($user); + send_event(new InitUserConfigEvent($user)); } protected function log_out() @@ -147,6 +149,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase global $user, $config; $user = User::by_id($config->get_int("anon_id", 0)); $this->assertNotNull($user); + send_event(new InitUserConfigEvent($user)); } // post things From 0506adbf30b924d6eb824f304870e4d268acbdca Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Thu, 27 Jun 2019 23:30:00 -0500 Subject: [PATCH 16/20] Adjusted rating setting migration --- ext/rating/main.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ext/rating/main.php b/ext/rating/main.php index 6ec70326..b56cb73c 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -156,7 +156,7 @@ class Ratings extends Extension public function onInitUserConfig(InitUserConfigEvent $event) { $event->user_config->set_default_array(RatingsConfig::USER_DEFAULTS, self::get_user_class_privs($event->user)); } - + public function onUserOptionsBuilding(UserOptionsBuildingEvent $event) { global $user, $user_config; @@ -558,11 +558,17 @@ class Ratings extends Extension if ($config->get_int(RatingsConfig::VERSION) < 4) { $value = $config->get_string("ext_rating_anon_privs"); - $config->set_array("ext_rating_anonymous_privs", str_split($value)); + if(!empty($value)) { + $config->set_array("ext_rating_anonymous_privs", str_split($value)); + } $value = $config->get_string("ext_rating_user_privs"); - $config->set_array("ext_rating_user_privs", str_split($value)); + if(!empty($value)) { + $config->set_array("ext_rating_user_privs", str_split($value)); + } $value = $config->get_string("ext_rating_admin_privs"); - $config->set_array("ext_rating_admin_privs", str_split($value)); + if(!empty($value)) { + $config->set_array("ext_rating_admin_privs", str_split($value)); + } switch ($database->get_driver_name()) { From 1e60c8720cd14d9a7f572fec2520e8f14bb28dae Mon Sep 17 00:00:00 2001 From: matthew Date: Fri, 5 Jul 2019 10:32:34 -0500 Subject: [PATCH 17/20] Set unrated and unknown as reserved ratings --- ext/rating/main.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/ext/rating/main.php b/ext/rating/main.php index b56cb73c..98daa256 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -77,6 +77,11 @@ function add_rating(ImageRating $rating) { if($rating->code=="?"&&array_key_exists("?",$_shm_ratings)) { throw new Exception("? is a reserved rating code that cannot be overridden"); } + + if($rating->code!="?"&&in_array(strtolower($rating->search_term), Ratings::UNRATED_KEYWORDS)) { + throw new Exception("$rating->search_term is a reserved search term"); + } + $_shm_ratings[$rating->code] = $rating; } @@ -114,6 +119,8 @@ class Ratings extends Extension { protected $db_support = [DatabaseDriver::MYSQL, DatabaseDriver::PGSQL]; + public const UNRATED_KEYWORDS = ["unknown","unrated"]; + private $search_regexp; @@ -129,7 +136,7 @@ class Ratings extends Extension array_push($search_terms, $rating->search_term); } $this->search_regexp = "/^rating[=|:](?:(\*|[" . $codes . "]+)|(" . - implode("|", $search_terms) . "|unknown))$/D"; + implode("|", $search_terms) . "|".implode("|",self::UNRATED_KEYWORDS)."))$/D"; } public function get_priority(): int @@ -156,7 +163,7 @@ class Ratings extends Extension public function onInitUserConfig(InitUserConfigEvent $event) { $event->user_config->set_default_array(RatingsConfig::USER_DEFAULTS, self::get_user_class_privs($event->user)); } - + public function onUserOptionsBuilding(UserOptionsBuildingEvent $event) { global $user, $user_config; @@ -260,7 +267,7 @@ class Ratings extends Extension public function onSearchTermParse(SearchTermParseEvent $event) { - global $user, $_shm_ratings; + global $user; $matches = []; if (is_null($event->term) && $this->no_rating_query($event->context)) { @@ -272,6 +279,10 @@ class Ratings extends Extension if (preg_match($this->search_regexp, strtolower($event->term), $matches)) { $ratings = $matches[1] ? $matches[1] : $matches[2][0]; + if(count($matches)>2&&in_array($matches[2], self::UNRATED_KEYWORDS)) { + $ratings = "?"; + } + if ($ratings == '*') { $ratings = Ratings::get_user_class_privs($user); } else { @@ -291,6 +302,10 @@ class Ratings extends Extension if (preg_match($this->search_regexp, strtolower($event->term), $matches) && $event->parse) { $ratings = $matches[1] ? $matches[1] : $matches[2][0]; + if(count($matches)>2&&in_array($matches[2], self::UNRATED_KEYWORDS)) { + $ratings = "?"; + } + $ratings = array_intersect(str_split($ratings), Ratings::get_user_class_privs($user)); $rating = $ratings[0]; From d62386474d59d8ca2851bbbabe32ef976a14787e Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Wed, 14 Aug 2019 08:44:29 -0500 Subject: [PATCH 18/20] travis adjustment --- .travis.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index da63e972..2b479dfd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,10 @@ language: php php: - - 7.1 + - 7.3 + +services: + - mysql + - postgresql sudo: false @@ -31,7 +35,7 @@ install: if [[ "$DB" == "mysql" ]]; then mysql -e "SET GLOBAL general_log = 'ON';" -uroot ; mysql -e "CREATE DATABASE shimmie;" -uroot ; - echo ' data/config/auto_install.conf.php ; + echo ' data/config/auto_install.conf.php ; fi - if [[ "$DB" == "sqlite" ]]; then echo ' data/config/auto_install.conf.php ; From 4dce3a2f07d84763986f32c416af4ad36cd8cae4 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Wed, 14 Aug 2019 09:07:45 -0500 Subject: [PATCH 19/20] Update theme.php --- ext/rating/theme.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ext/rating/theme.php b/ext/rating/theme.php index 48facc11..c16b15dd 100644 --- a/ext/rating/theme.php +++ b/ext/rating/theme.php @@ -109,6 +109,8 @@ class RatingsTheme extends Themelet } $output .= ""; return $output; + } + public function get_user_options(User $user, array $selected_ratings, array $available_ratings): string { $html = " From f5119b20a3b5f2acfe835bf6a27d61e8de232d75 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 29 Sep 2019 14:32:51 +0100 Subject: [PATCH 20/20] formatting --- core/cacheengine.php | 6 +- core/config.php | 24 +++--- core/database.php | 14 ++-- core/dbengine.php | 3 +- core/imageboard/image.php | 33 ++++---- core/imageboard/misc.php | 16 ++-- core/page.php | 23 +++--- core/permissions.php | 3 +- core/polyfills.php | 12 +-- core/send_event.php | 20 +++-- core/tests/polyfills.test.php | 10 +-- core/tests/util.test.php | 46 ++++++----- core/util.php | 4 +- ext/admin/main.php | 2 +- ext/admin/theme.php | 5 +- ext/alias_editor/main.php | 2 +- ext/artists/main.php | 2 +- ext/autocomplete/main.php | 4 +- ext/blocks/main.php | 2 +- ext/blotter/main.php | 2 +- ext/bulk_actions/main.php | 16 ++-- ext/comment/main.php | 4 +- ext/comment/theme.php | 1 - ext/et/main.php | 2 +- ext/ext_manager/main.php | 2 +- ext/favorites/main.php | 6 +- ext/favorites/theme.php | 1 - ext/handle_flash/main.php | 1 - ext/handle_ico/main.php | 2 +- ext/handle_mp3/main.php | 1 - ext/handle_pixel/main.php | 9 +-- ext/handle_svg/main.php | 1 - ext/handle_video/main.php | 18 ++--- ext/handle_video/theme.php | 4 +- ext/help_pages/main.php | 12 +-- ext/help_pages/theme.php | 2 - ext/image/main.php | 16 ++-- ext/image_hash_ban/main.php | 2 +- ext/index/main.php | 13 ++-- ext/index/theme.php | 1 - ext/ipban/main.php | 2 +- ext/log_db/main.php | 2 +- ext/media/main.php | 78 ++++++++++--------- ext/media/theme.php | 1 - ext/not_a_tag/main.php | 2 +- ext/notes/main.php | 2 +- ext/notes/theme.php | 2 - ext/numeric_score/main.php | 5 +- ext/numeric_score/theme.php | 1 - ext/pm/main.php | 2 +- ext/pools/main.php | 22 +++--- ext/pools/theme.php | 3 +- ext/post_titles/config.php | 2 +- .../events/post_title_set_event.php | 2 +- ext/post_titles/main.php | 10 +-- ext/post_titles/theme.php | 2 - ext/random_image/main.php | 2 +- ext/random_list/main.php | 2 +- ext/rating/main.php | 70 +++++++++-------- ext/rating/theme.php | 47 +++++------ ext/regen_thumb/main.php | 2 +- ext/relatationships/main.php | 21 +++-- ext/relatationships/test.php | 12 +-- ext/relatationships/theme.php | 1 - ext/report_image/main.php | 2 +- ext/resize/main.php | 4 +- ext/rss_comments/main.php | 3 +- ext/rss_images/main.php | 2 +- ext/setup/config.php | 2 +- ext/setup/main.php | 32 +++++--- ext/source_history/main.php | 2 +- ext/system/main.php | 2 - ext/tag_categories/config.php | 2 +- ext/tag_categories/main.php | 5 +- ext/tag_categories/theme.php | 1 - ext/tag_edit/main.php | 2 +- ext/tag_history/main.php | 2 +- ext/tag_list/main.php | 2 +- ext/tips/main.php | 2 +- ext/transcode/main.php | 8 +- ext/transcode/theme.php | 9 ++- ext/trash/main.php | 28 ++++--- ext/trash/theme.php | 7 +- ext/upgrade/main.php | 3 +- ext/upload/main.php | 4 +- ext/user/main.php | 4 +- ext/user/theme.php | 2 - ext/user_config/main.php | 8 +- ext/view/events/displaying_image_event.php | 5 +- .../image_admin_block_building_event.php | 2 +- .../events/image_info_box_building_event.php | 2 +- ext/view/events/image_info_set_event.php | 2 +- ext/wiki/main.php | 4 +- index.php | 8 +- themes/danbooru/layout.class.php | 2 +- themes/danbooru/view.theme.php | 2 +- themes/danbooru2/layout.class.php | 2 +- themes/danbooru2/view.theme.php | 2 +- themes/lite/layout.class.php | 2 +- themes/lite/view.theme.php | 2 +- 100 files changed, 406 insertions(+), 409 deletions(-) diff --git a/core/cacheengine.php b/core/cacheengine.php index 002b10b4..4d5fc05f 100644 --- a/core/cacheengine.php +++ b/core/cacheengine.php @@ -192,14 +192,14 @@ class Cache $_tracer->begin("Cache Query", ["key"=>$key]); $val = $this->engine->get($key); if ($val !== false) { - $res = "hit"; + $res = "hit"; $this->hits++; } else { - $res = "miss"; + $res = "miss"; $this->misses++; } $_tracer->end(null, ["result"=>$res]); - return $val; + return $val; } public function set(string $key, $val, int $time=0) diff --git a/core/config.php b/core/config.php index 6b1ac6fa..06047a77 100644 --- a/core/config.php +++ b/core/config.php @@ -159,7 +159,7 @@ abstract class BaseConfig implements Config public function set_array(string $name, ?array $value): void { - if($value!=null) { + if ($value!=null) { $this->values[$name] = implode(",", $value); } else { $this->values[$name] = null; @@ -313,16 +313,19 @@ class DatabaseConfig extends BaseConfig private $sub_column; private $sub_value; - public function __construct(Database $database, string $table_name = "config", - string $sub_column = null, string $sub_value = null) - { + public function __construct( + Database $database, + string $table_name = "config", + string $sub_column = null, + string $sub_value = null + ) { $this->database = $database; $this->table_name = $table_name; $this->sub_value = $sub_value; $this->sub_column = $sub_column; $cache_name = "config"; - if(!empty($sub_value)) { + if (!empty($sub_value)) { $cache_name .= "_".$sub_value; } @@ -335,12 +338,12 @@ class DatabaseConfig extends BaseConfig $query = "SELECT name, value FROM {$this->table_name}"; $args = []; - if(!empty($sub_column)&&!empty($sub_value)) { + if (!empty($sub_column)&&!empty($sub_value)) { $query .= " WHERE $sub_column = :sub_value"; $args["sub_value"] = $sub_value; } - foreach ($this->database->get_all($query, $args ) as $row) { + foreach ($this->database->get_all($query, $args) as $row) { $this->values[$row["name"]] = $row["value"]; } $this->database->cache->set($cache_name, $this->values); @@ -359,7 +362,7 @@ class DatabaseConfig extends BaseConfig $args = ["name"=>$name]; $cols = ["name","value"]; $params = [":name",":value"]; - if(!empty($this->sub_column)&&!empty($this->sub_value)) { + if (!empty($this->sub_column)&&!empty($this->sub_value)) { $query .= " AND $this->sub_column = :sub_value"; $args["sub_value"] = $this->sub_value; $cols[] = $this->sub_column; @@ -370,8 +373,9 @@ class DatabaseConfig extends BaseConfig $args["value"] =$this->values[$name]; $this->database->Execute( - "INSERT INTO {$this->table_name} (".join(",",$cols).") VALUES (".join(",",$params).")", - $args); + "INSERT INTO {$this->table_name} (".join(",", $cols).") VALUES (".join(",", $params).")", + $args + ); } // rather than deleting and having some other request(s) do a thundering // herd of race-conditioned updates, just save the updated version once here diff --git a/core/database.php b/core/database.php index 2cbf077f..3bead690 100644 --- a/core/database.php +++ b/core/database.php @@ -172,9 +172,9 @@ class Database if (is_null($this->engine)) { $this->connect_engine(); } - if($input===true) { + if ($input===true) { return $this->engine->BOOL_Y; - } else if ($input===false) { + } elseif ($input===false) { return $this->engine->BOOL_N; } return $input; @@ -190,13 +190,13 @@ class Database private function count_time(string $method, float $start, string $query, ?array $args): void { - global $_tracer, $tracer_enabled; - $dur = microtime(true) - $start; - if($tracer_enabled) { + global $_tracer, $tracer_enabled; + $dur = microtime(true) - $start; + if ($tracer_enabled) { $query = trim(preg_replace('/^[\t ]+/m', '', $query)); // trim leading whitespace $_tracer->complete($start * 1000000, $dur * 1000000, "DB Query", ["query"=>$query, "args"=>$args, "method"=>$method]); } - $this->query_count++; + $this->query_count++; $this->dbtime += $dur; } @@ -226,7 +226,7 @@ class Database return $stmt; } catch (PDOException $pdoe) { throw new SCoreException($pdoe->getMessage()."

Query: ".$query); - } + } } /** diff --git a/core/dbengine.php b/core/dbengine.php index 5286a8ad..7c5112c0 100644 --- a/core/dbengine.php +++ b/core/dbengine.php @@ -1,5 +1,6 @@ append(new Querylet(" ORDER BY ".(Image::$order_sql ?: "images.".$config->get_string("index_order")))); - if($limit!=null) { + if ($limit!=null) { $querylet->append(new Querylet(" LIMIT :limit ", ["limit" => $limit])); } $querylet->append(new Querylet(" OFFSET :offset ", ["offset"=>$start])); @@ -228,7 +228,7 @@ class Image $response = Image::query_accelerator($req); if ($response) { - $list = implode(",", $response); + $list = implode(",", $response); $result = $database->execute("SELECT * FROM images WHERE id IN ($list) ORDER BY images.id DESC"); } else { $result = $database->execute("SELECT * FROM images WHERE 1=0 ORDER BY images.id DESC"); @@ -253,19 +253,19 @@ class Image public static function query_accelerator($req) { - global $_tracer; + global $_tracer; $fp = @fsockopen("127.0.0.1", 21212); if (!$fp) { return null; } - $req_str = json_encode($req); - $_tracer->begin("Accelerator Query", ["req"=>$req_str]); + $req_str = json_encode($req); + $_tracer->begin("Accelerator Query", ["req"=>$req_str]); fwrite($fp, $req_str); $data = ""; while (($buffer = fgets($fp, 4096)) !== false) { $data .= $buffer; } - $_tracer->end(); + $_tracer->end(); if (!feof($fp)) { die("Error: unexpected fgets() fail in query_accelerator($req_str)\n"); } @@ -299,7 +299,7 @@ class Image ["tag"=>$tags[0]] ); } else { - if(ext_is_live("Ratings")) { + if (ext_is_live("Ratings")) { $tags[] = "rating:*"; } list($tag_conditions, $img_conditions) = self::terms_to_conditions($tags); @@ -833,10 +833,9 @@ class Image $opts = $matches[2]; $post = $matches[3]; - if(isset($flexihashes[$opts])) { + if (isset($flexihashes[$opts])) { $flexihash = $flexihashes[$opts]; - } - else { + } else { $flexihash = new Flexihash\Flexihash(); foreach (explode(",", $opts) as $opt) { $parts = explode("=", $opt); @@ -964,7 +963,7 @@ class Image FROM images WHERE 1=0 "); - } elseif($tag_count==1) { + } elseif ($tag_count==1) { // All wildcard terms that qualify for a single tag can be treated the same as non-wildcards $positive_tag_id_array[] = $tag_ids[0]; } else { @@ -981,14 +980,14 @@ class Image $sql = ""; assert($positive_tag_id_array || $positive_wildcard_id_array || $negative_tag_id_array, @$_GET['q']); - if(!empty($positive_tag_id_array) || !empty($positive_wildcard_id_array)) { + if (!empty($positive_tag_id_array) || !empty($positive_wildcard_id_array)) { $inner_joins = []; if (!empty($positive_tag_id_array)) { - foreach($positive_tag_id_array as $tag) { + foreach ($positive_tag_id_array as $tag) { $inner_joins[] = "= $tag"; } } - if(!empty($positive_wildcard_id_array)) { + if (!empty($positive_wildcard_id_array)) { foreach ($positive_wildcard_id_array as $tags) { $positive_tag_id_list = join(', ', $tags); $inner_joins[] = "IN ($positive_tag_id_list)"; @@ -1002,12 +1001,12 @@ class Image $i++; $sub_query .= " INNER JOIN image_tags it$i ON it$i.image_id = it.image_id AND it$i.tag_id $inner_join "; } - if(!empty($negative_tag_id_array)) { + if (!empty($negative_tag_id_array)) { $negative_tag_id_list = join(', ', $negative_tag_id_array); $sub_query .= " LEFT JOIN image_tags negative ON negative.image_id = it.image_id AND negative.tag_id IN ($negative_tag_id_list) "; } $sub_query .= "WHERE it.tag_id $first "; - if(!empty($negative_tag_id_array)) { + if (!empty($negative_tag_id_array)) { $sub_query .= " AND negative.image_id IS NULL"; } $sub_query .= " GROUP BY it.image_id "; @@ -1018,7 +1017,7 @@ class Image $sub_query ) a on a.image_id = images.id "; - } elseif(!empty($negative_tag_id_array)) { + } elseif (!empty($negative_tag_id_array)) { $negative_tag_id_list = join(', ', $negative_tag_id_array); $sql = " SELECT images.* diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index c82d9f98..bead57bc 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -124,7 +124,7 @@ function get_thumbnail_size(int $orig_width, int $orig_height, bool $use_dpi_sca } - if($use_dpi_scaling) { + if ($use_dpi_scaling) { list($max_width, $max_height) = get_thumbnail_max_size_scaled(); } else { $max_width = $config->get_int(ImageConfig::THUMB_WIDTH); @@ -138,7 +138,6 @@ function get_thumbnail_size(int $orig_width, int $orig_height, bool $use_dpi_sca } else { return $output; } - } function get_scaled_by_aspect_ratio(int $original_width, int $original_height, int $max_width, int $max_height) : array @@ -167,19 +166,20 @@ function get_thumbnail_max_size_scaled(): array } -function create_image_thumb(string $hash, string $type, string $engine = null) { +function create_image_thumb(string $hash, string $type, string $engine = null) +{ global $config; $inname = warehouse_path(Image::IMAGE_DIR, $hash); $outname = warehouse_path(Image::THUMBNAIL_DIR, $hash); $tsize = get_thumbnail_max_size_scaled(); - if(empty($engine)) { + if (empty($engine)) { $engine = $config->get_string(ImageConfig::THUMB_ENGINE); } $output_format = $config->get_string(ImageConfig::THUMB_TYPE); - if($output_format=="webp") { + if ($output_format=="webp") { $output_format = Media::WEBP_LOSSY; } @@ -206,14 +206,14 @@ function format_milliseconds(int $input): string $remainder = floor($input / 1000); - foreach (TIME_UNITS AS $unit=>$conversion) { + foreach (TIME_UNITS as $unit=>$conversion) { $count = $remainder % $conversion; $remainder = floor($remainder / $conversion); - if($count==0&&$remainder<1) { + if ($count==0&&$remainder<1) { break; } $output = "$count".$unit." ".$output; } return trim($output); -} \ No newline at end of file +} diff --git a/core/page.php b/core/page.php index 7efa2833..fb99f20d 100644 --- a/core/page.php +++ b/core/page.php @@ -307,14 +307,14 @@ class Page $active_link = null; // To save on event calls, we check if one of the top-level links has already been marked as active foreach ($nav_links as $link) { - if($link->active===true) { + if ($link->active===true) { $active_link = $link; break; } } $sub_links = null; // If one is, we just query for sub-menu options under that one tab - if($active_link!==null) { + if ($active_link!==null) { $psnbe = new PageSubNavBuildingEvent($active_link->name); send_event($psnbe); $sub_links = $psnbe->links; @@ -326,13 +326,13 @@ class Page // Now we check for a current link so we can identify the sub-links to show foreach ($psnbe->links as $sub_link) { - if($sub_link->active===true) { + if ($sub_link->active===true) { $sub_links = $psnbe->links; break; } } // If the active link has been detected, we break out - if($sub_links!==null) { + if ($sub_links!==null) { $link->active = true; break; } @@ -372,7 +372,6 @@ class Page header('Accept-Ranges: bytes'); if (isset($_SERVER['HTTP_RANGE'])) { - $c_start = $start; $c_end = $end; list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); @@ -418,8 +417,9 @@ class Page // After flush, we can tell if the client browser has disconnected. // This means we can start sending a large file, and if we detect they disappeared // then we can just stop and not waste any more resources or bandwidth. - if (connection_status() != 0) + if (connection_status() != 0) { break; + } } } finally { fclose($fp); @@ -541,7 +541,7 @@ class PageSubNavBuildingEvent extends Event public function add_nav_link(string $name, Link $link, string $desc, ?bool $active = null, int $order = 50) { - $this->links[] = new NavLink($name, $link, $desc, $active,$order); + $this->links[] = new NavLink($name, $link, $desc, $active, $order); } } @@ -553,7 +553,7 @@ class NavLink public $order; public $active = false; - public function __construct(String $name, Link $link, String $description, ?bool $active = null, int $order = 50) + public function __construct(String $name, Link $link, String $description, ?bool $active = null, int $order = 50) { global $config; @@ -561,7 +561,7 @@ class NavLink $this->link = $link; $this->description = $description; $this->order = $order; - if($active==null) { + if ($active==null) { $query = ltrim(_get_query(), "/"); if ($query === "") { // This indicates the front page, so we check what's set as the front page @@ -572,15 +572,14 @@ class NavLink } else { $this->active = self::is_active([$link->page], $front_page); } - } elseif($query===$link->page) { + } elseif ($query===$link->page) { $this->active = true; - }else { + } else { $this->active = self::is_active([$link->page]); } } else { $this->active = $active; } - } public static function is_active(array $pages_matched, string $url = null): bool diff --git a/core/permissions.php b/core/permissions.php index 4378a9c0..717bd5fd 100644 --- a/core/permissions.php +++ b/core/permissions.php @@ -66,5 +66,4 @@ abstract class Permissions public const VIEW_TRASH = "view_trash"; public const PERFORM_BULK_ACTIONS = "perform_bulk_actions"; - -} \ No newline at end of file +} diff --git a/core/polyfills.php b/core/polyfills.php index 680879f4..951df919 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -759,7 +759,7 @@ function validate_input(array $inputs): array */ function sanitize_path(string $path): string { - return preg_replace('|[\\\\/]+|S',DIRECTORY_SEPARATOR,$path); + return preg_replace('|[\\\\/]+|S', DIRECTORY_SEPARATOR, $path); } /** @@ -770,11 +770,11 @@ function join_path(string ...$paths): string { $output = ""; foreach ($paths as $path) { - if(empty($path)) { + if (empty($path)) { continue; } $path = sanitize_path($path); - if(empty($output)) { + if (empty($output)) { $output = $path; } else { $output = rtrim($output, DIRECTORY_SEPARATOR); @@ -790,8 +790,8 @@ function join_path(string ...$paths): string */ function iterator_map(callable $callback, iterator $iter): Generator { - foreach($iter as $i) { - yield call_user_func($callback,$i); + foreach ($iter as $i) { + yield call_user_func($callback, $i); } } @@ -801,4 +801,4 @@ function iterator_map(callable $callback, iterator $iter): Generator function iterator_map_to_array(callable $callback, iterator $iter): array { return iterator_to_array(iterator_map($callback, $iter)); -} \ No newline at end of file +} diff --git a/core/send_event.php b/core/send_event.php index 24897658..d6c92e01 100644 --- a/core/send_event.php +++ b/core/send_event.php @@ -26,7 +26,7 @@ function _load_event_listeners(): void function _clear_cached_event_listeners(): void { if (file_exists(data_path("cache/shm_event_listeners.php"))) { - unlink(data_path("cache/shm_event_listeners.php")); + unlink(data_path("cache/shm_event_listeners.php")); } } @@ -118,21 +118,29 @@ function send_event(Event $event): void // send_event() is performance sensitive, and with the number // of times tracer gets called the time starts to add up - if ($tracer_enabled) $_tracer->begin(get_class($event)); + if ($tracer_enabled) { + $_tracer->begin(get_class($event)); + } // SHIT: http://bugs.php.net/bug.php?id=35106 $my_event_listeners = $_shm_event_listeners[get_class($event)]; ksort($my_event_listeners); foreach ($my_event_listeners as $listener) { - if ($tracer_enabled) $_tracer->begin(get_class($listener)); + if ($tracer_enabled) { + $_tracer->begin(get_class($listener)); + } if (method_exists($listener, $method_name)) { $listener->$method_name($event); } - if ($tracer_enabled) $_tracer->end(); - if($event->stop_processing===true) { + if ($tracer_enabled) { + $_tracer->end(); + } + if ($event->stop_processing===true) { break; } } $_shm_event_count++; - if ($tracer_enabled) $_tracer->end(); + if ($tracer_enabled) { + $_tracer->end(); + } } diff --git a/core/tests/polyfills.test.php b/core/tests/polyfills.test.php index 68e0d10a..e5899f0a 100644 --- a/core/tests/polyfills.test.php +++ b/core/tests/polyfills.test.php @@ -48,7 +48,6 @@ class PolyfillsTest extends \PHPUnit\Framework\TestCase public function test_sanitize_path() { - $this->assertEquals( "one", sanitize_path("one") @@ -88,7 +87,6 @@ class PolyfillsTest extends \PHPUnit\Framework\TestCase DIRECTORY_SEPARATOR."one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR, sanitize_path("\\/one/\\/\\/two\\/") ); - } public function test_join_path() @@ -100,22 +98,22 @@ class PolyfillsTest extends \PHPUnit\Framework\TestCase $this->assertEquals( "one".DIRECTORY_SEPARATOR."two", - join_path("one","two") + join_path("one", "two") ); $this->assertEquals( "one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three", - join_path("one","two","three") + join_path("one", "two", "three") ); $this->assertEquals( "one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three", - join_path("one/two","three") + join_path("one/two", "three") ); $this->assertEquals( DIRECTORY_SEPARATOR."one".DIRECTORY_SEPARATOR."two".DIRECTORY_SEPARATOR."three".DIRECTORY_SEPARATOR, - join_path("\\/////\\\\one/\///"."\\//two\/\\//\\//","//\/\\\/three/\\/\/") + join_path("\\/////\\\\one/\///"."\\//two\/\\//\\//", "//\/\\\/three/\\/\/") ); } } diff --git a/core/tests/util.test.php b/core/tests/util.test.php index 3b3381f7..e04b01ae 100644 --- a/core/tests/util.test.php +++ b/core/tests/util.test.php @@ -3,65 +3,63 @@ require_once "core/util.php"; class UtilTest extends \PHPUnit\Framework\TestCase { - public function test_warehouse_path() { $hash = "7ac19c10d6859415"; $this->assertEquals( - join_path(DATA_DIR,"base",$hash), - warehouse_path("base",$hash,false, 0) + join_path(DATA_DIR, "base", $hash), + warehouse_path("base", $hash, false, 0) ); $this->assertEquals( - join_path(DATA_DIR,"base","7a",$hash), - warehouse_path("base",$hash,false, 1) + join_path(DATA_DIR, "base", "7a", $hash), + warehouse_path("base", $hash, false, 1) ); $this->assertEquals( - join_path(DATA_DIR,"base","7a","c1",$hash), - warehouse_path("base",$hash,false, 2) + join_path(DATA_DIR, "base", "7a", "c1", $hash), + warehouse_path("base", $hash, false, 2) ); $this->assertEquals( - join_path(DATA_DIR,"base","7a","c1","9c",$hash), - warehouse_path("base",$hash,false, 3) + join_path(DATA_DIR, "base", "7a", "c1", "9c", $hash), + warehouse_path("base", $hash, false, 3) ); $this->assertEquals( - join_path(DATA_DIR,"base","7a","c1","9c","10",$hash), - warehouse_path("base",$hash,false, 4) + join_path(DATA_DIR, "base", "7a", "c1", "9c", "10", $hash), + warehouse_path("base", $hash, false, 4) ); $this->assertEquals( - join_path(DATA_DIR,"base","7a","c1","9c","10","d6",$hash), - warehouse_path("base",$hash,false, 5) + join_path(DATA_DIR, "base", "7a", "c1", "9c", "10", "d6", $hash), + warehouse_path("base", $hash, false, 5) ); $this->assertEquals( - join_path(DATA_DIR,"base","7a","c1","9c","10","d6","85",$hash), - warehouse_path("base",$hash,false, 6) + join_path(DATA_DIR, "base", "7a", "c1", "9c", "10", "d6", "85", $hash), + warehouse_path("base", $hash, false, 6) ); $this->assertEquals( - join_path(DATA_DIR,"base","7a","c1","9c","10","d6","85","94",$hash), - warehouse_path("base",$hash,false, 7) + join_path(DATA_DIR, "base", "7a", "c1", "9c", "10", "d6", "85", "94", $hash), + warehouse_path("base", $hash, false, 7) ); $this->assertEquals( - join_path(DATA_DIR,"base","7a","c1","9c","10","d6","85","94","15",$hash), - warehouse_path("base",$hash,false, 8) + join_path(DATA_DIR, "base", "7a", "c1", "9c", "10", "d6", "85", "94", "15", $hash), + warehouse_path("base", $hash, false, 8) ); $this->assertEquals( - join_path(DATA_DIR,"base","7a","c1","9c","10","d6","85","94","15",$hash), - warehouse_path("base",$hash,false, 9) + join_path(DATA_DIR, "base", "7a", "c1", "9c", "10", "d6", "85", "94", "15", $hash), + warehouse_path("base", $hash, false, 9) ); $this->assertEquals( - join_path(DATA_DIR,"base","7a","c1","9c","10","d6","85","94","15",$hash), - warehouse_path("base",$hash,false, 10) + join_path(DATA_DIR, "base", "7a", "c1", "9c", "10", "d6", "85", "94", "15", $hash), + warehouse_path("base", $hash, false, 10) ); - } } diff --git a/core/util.php b/core/util.php index 7017078d..ecec96e1 100644 --- a/core/util.php +++ b/core/util.php @@ -174,7 +174,7 @@ function warehouse_path(string $base, string $hash, bool $create=true, int $spli { $dirs =[DATA_DIR, $base]; $splits = min($splits, strlen($hash) / 2); - for($i = 0; $i < $splits; $i++) { + for ($i = 0; $i < $splits; $i++) { $dirs[] = substr($hash, $i * 2, 2); } $dirs[] = $hash; @@ -343,7 +343,7 @@ function join_url(string $base, string ...$paths) { $output = $base; foreach ($paths as $path) { - $output = rtrim($output,"/"); + $output = rtrim($output, "/"); $path = ltrim($path, "/"); $output .= "/".$path; } diff --git a/ext/admin/main.php b/ext/admin/main.php index 423460c8..038d40ed 100644 --- a/ext/admin/main.php +++ b/ext/admin/main.php @@ -111,7 +111,7 @@ class AdminPage extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::MANAGE_ADMINTOOLS)) { $event->add_nav_link("admin", new Link('admin'), "Board Admin"); } diff --git a/ext/admin/theme.php b/ext/admin/theme.php index 1abe6542..d6a60e6c 100644 --- a/ext/admin/theme.php +++ b/ext/admin/theme.php @@ -55,12 +55,11 @@ class AdminPageTheme extends Themelet $html .= ""; $html .= "\n"; $page->add_block(new Block("Set Tag Case", $html)); - } public function dbq_html($terms) { - if(ext_is_live("Trash")) { + if (ext_is_live("Trash")) { $warning = "This delete method will bypass the trash
"; } if (class_exists("ImageBan")) { @@ -75,6 +74,4 @@ class AdminPageTheme extends Themelet "; return $html; } - - } diff --git a/ext/alias_editor/main.php b/ext/alias_editor/main.php index 36edbfbb..d759d6c9 100644 --- a/ext/alias_editor/main.php +++ b/ext/alias_editor/main.php @@ -119,7 +119,7 @@ class AliasEditor extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="tags") { + if ($event->parent=="tags") { $event->add_nav_link("aliases", new Link('alias/list'), "Aliases", NavLink::is_active(["alias"])); } } diff --git a/ext/artists/main.php b/ext/artists/main.php index b1f6efcc..2a98e268 100644 --- a/ext/artists/main.php +++ b/ext/artists/main.php @@ -55,7 +55,7 @@ class Artists extends Extension public function onHelpPageBuilding(HelpPageBuildingEvent $event) { - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "Artist"; $block->body = $this->theme->get_help_html(); diff --git a/ext/autocomplete/main.php b/ext/autocomplete/main.php index 187f45e5..2bd18217 100644 --- a/ext/autocomplete/main.php +++ b/ext/autocomplete/main.php @@ -38,8 +38,8 @@ class AutoComplete extends Extension //$limit = 0; $cache_key = "autocomplete-$s"; $limitSQL = ""; - $s = str_replace('_','\_', $s); - $s = str_replace('%','\%', $s); + $s = str_replace('_', '\_', $s); + $s = str_replace('%', '\%', $s); $SQLarr = ["search"=>"$s%"]; #, "cat_search"=>"%:$s%"]; if (isset($_GET["limit"]) && $_GET["limit"] !== 0) { $limitSQL = "LIMIT :limit"; diff --git a/ext/blocks/main.php b/ext/blocks/main.php index 197b5d9f..e0f283fa 100644 --- a/ext/blocks/main.php +++ b/ext/blocks/main.php @@ -29,7 +29,7 @@ class Blocks extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::MANAGE_BLOCKS)) { $event->add_nav_link("blocks", new Link('blocks/list'), "Blocks Editor"); } diff --git a/ext/blotter/main.php b/ext/blotter/main.php index 3e18b1a9..528f028e 100644 --- a/ext/blotter/main.php +++ b/ext/blotter/main.php @@ -59,7 +59,7 @@ class Blotter extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->is_admin()) { $event->add_nav_link("blotter", new Link('blotter/editor'), "Blotter Editor"); } diff --git a/ext/bulk_actions/main.php b/ext/bulk_actions/main.php index 0bfaf1da..60654701 100644 --- a/ext/bulk_actions/main.php +++ b/ext/bulk_actions/main.php @@ -22,10 +22,10 @@ class BulkActionBlockBuildingEvent extends Event $block = ""; } - if(!empty($access_key)) { + if (!empty($access_key)) { assert(strlen($access_key)==1); foreach ($this->actions as $existing) { - if($existing["access_key"]==$access_key) { + if ($existing["access_key"]==$access_key) { throw new SCoreException("Access key $access_key is already in use"); } } @@ -90,18 +90,18 @@ class BulkActions extends Extension } if ($user->can(Permissions::BULK_EDIT_IMAGE_TAG)) { - $event->add_action( "bulk_tag", "Tag", "t", "", $this->theme->render_tag_input(), - 10); + 10 + ); } if ($user->can(Permissions::BULK_EDIT_IMAGE_SOURCE)) { - $event->add_action("bulk_source", "Set (S)ource", "s","", $this->theme->render_source_input(), 10); + $event->add_action("bulk_source", "Set (S)ource", "s", "", $this->theme->render_source_input(), 10); } } @@ -186,7 +186,7 @@ class BulkActions extends Extension foreach ($data as $id) { if (is_numeric($id)) { $image = Image::by_id($id); - if($image!=null) { + if ($image!=null) { yield $image; } } @@ -246,10 +246,10 @@ class BulkActions extends Extension } } else { foreach ($items as $image) { - $img_tags = array_map("strtolower",$image->get_tag_array()); + $img_tags = array_map("strtolower", $image->get_tag_array()); if (!empty($neg_tag_array)) { - $neg_tag_array = array_map("strtolower",$neg_tag_array); + $neg_tag_array = array_map("strtolower", $neg_tag_array); $img_tags = array_merge($pos_tag_array, $img_tags); $img_tags = array_diff($img_tags, $neg_tag_array); diff --git a/ext/comment/main.php b/ext/comment/main.php index fb35da18..9f6404cd 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -166,7 +166,7 @@ class CommentList extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="comment") { + if ($event->parent=="comment") { $event->add_nav_link("comment_list", new Link('comment/list'), "All"); $event->add_nav_link("comment_help", new Link('ext_doc/comment'), "Help"); } @@ -368,7 +368,7 @@ class CommentList extends Extension public function onHelpPageBuilding(HelpPageBuildingEvent $event) { - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "Comments"; $block->body = $this->theme->get_help_html(); diff --git a/ext/comment/theme.php b/ext/comment/theme.php index 7e62da6b..31b77a7d 100644 --- a/ext/comment/theme.php +++ b/ext/comment/theme.php @@ -312,6 +312,5 @@ class CommentListTheme extends Themelet

Returns images that have been commented on by user 123.

'; - } } diff --git a/ext/et/main.php b/ext/et/main.php index 576765dd..cecea577 100644 --- a/ext/et/main.php +++ b/ext/et/main.php @@ -28,7 +28,7 @@ class ET extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::VIEW_SYSINTO)) { $event->add_nav_link("system_info", new Link('system_info'), "System Info", null, 10); } diff --git a/ext/ext_manager/main.php b/ext/ext_manager/main.php index 39c7f572..cc057fd8 100644 --- a/ext/ext_manager/main.php +++ b/ext/ext_manager/main.php @@ -165,7 +165,7 @@ class ExtManager extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::MANAGE_EXTENSION_LIST)) { $event->add_nav_link("ext_manager", new Link('ext_manager'), "Extension Manager"); } else { diff --git a/ext/favorites/main.php b/ext/favorites/main.php index 950d6178..380fefa2 100644 --- a/ext/favorites/main.php +++ b/ext/favorites/main.php @@ -157,7 +157,7 @@ class Favorites extends Extension public function onHelpPageBuilding(HelpPageBuildingEvent $event) { - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "Favorites"; $block->body = $this->theme->get_help_html(); @@ -168,11 +168,11 @@ class Favorites extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent=="posts") { + if ($event->parent=="posts") { $event->add_nav_link("posts_favorites", new Link("post/list/favorited_by={$user->name}/1"), "My Favorites"); } - if($event->parent==="user") { + if ($event->parent==="user") { if ($user->can(Permissions::MANAGE_ADMINTOOLS)) { $username = url_escape($user->name); $event->add_nav_link("favorites", new Link("post/list/favorited_by=$username/1"), "My Favorites"); diff --git a/ext/favorites/theme.php b/ext/favorites/theme.php index 22367347..cd03afbb 100644 --- a/ext/favorites/theme.php +++ b/ext/favorites/theme.php @@ -56,6 +56,5 @@ class FavoritesTheme extends Themelet

Returns images that have been favorited by user 123.

'; - } } diff --git a/ext/handle_flash/main.php b/ext/handle_flash/main.php index 3c53622d..a17abba9 100644 --- a/ext/handle_flash/main.php +++ b/ext/handle_flash/main.php @@ -8,7 +8,6 @@ class FlashFileHandler extends DataHandlerExtension { - public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) { switch ($event->ext) { diff --git a/ext/handle_ico/main.php b/ext/handle_ico/main.php index f67937b6..97e3c4c6 100644 --- a/ext/handle_ico/main.php +++ b/ext/handle_ico/main.php @@ -12,7 +12,7 @@ class IcoFileHandler extends DataHandlerExtension public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) { - if(in_array($event->ext, self::SUPPORTED_EXTENSIONS)) { + if (in_array($event->ext, self::SUPPORTED_EXTENSIONS)) { $event->lossless = true; $event->video = false; $event->audio = false; diff --git a/ext/handle_mp3/main.php b/ext/handle_mp3/main.php index 81fbee49..4cd943fa 100644 --- a/ext/handle_mp3/main.php +++ b/ext/handle_mp3/main.php @@ -17,7 +17,6 @@ class MP3FileHandler extends DataHandlerExtension break; } // TODO: Buff out audio format support, length scanning - } protected function create_thumb(string $hash, string $type): bool diff --git a/ext/handle_pixel/main.php b/ext/handle_pixel/main.php index 3b73bdfb..aeaa5779 100644 --- a/ext/handle_pixel/main.php +++ b/ext/handle_pixel/main.php @@ -13,14 +13,14 @@ class PixelFileHandler extends DataHandlerExtension public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) { - if(in_array($event->ext, Media::LOSSLESS_FORMATS)) { + if (in_array($event->ext, Media::LOSSLESS_FORMATS)) { $event->lossless = true; - } elseif($event->ext=="webp") { + } elseif ($event->ext=="webp") { $event->lossless = Media::is_lossless_webp($event->file_name); } - if(in_array($event->ext,self::SUPPORTED_EXTENSIONS)) { - if($event->lossless==null) { + if (in_array($event->ext, self::SUPPORTED_EXTENSIONS)) { + if ($event->lossless==null) { $event->lossless = false; } $event->audio = false; @@ -117,5 +117,4 @@ class PixelFileHandler extends DataHandlerExtension ", 20); } - } diff --git a/ext/handle_svg/main.php b/ext/handle_svg/main.php index 79fd6613..4dd31926 100644 --- a/ext/handle_svg/main.php +++ b/ext/handle_svg/main.php @@ -10,7 +10,6 @@ use enshrined\svgSanitize\Sanitizer; class SVGFileHandler extends DataHandlerExtension { - public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) { switch ($event->ext) { diff --git a/ext/handle_video/main.php b/ext/handle_video/main.php index 5c090faf..675134b5 100644 --- a/ext/handle_video/main.php +++ b/ext/handle_video/main.php @@ -50,19 +50,19 @@ class VideoFileHandler extends DataHandlerExtension public function onMediaCheckProperties(MediaCheckPropertiesEvent $event) { - if(in_array($event->ext, self::SUPPORTED_EXT)) { + if (in_array($event->ext, self::SUPPORTED_EXT)) { $event->video = true; try { $data = Media::get_ffprobe_data($event->file_name); - if(is_array($data)) { - if(array_key_exists("streams", $data)) { + if (is_array($data)) { + if (array_key_exists("streams", $data)) { $video = false; $audio = true; $streams = $data["streams"]; if (is_array($streams)) { foreach ($streams as $stream) { - if(is_array($stream)) { + if (is_array($stream)) { if (array_key_exists("codec_type", $stream)) { $type = $stream["codec_type"]; switch ($type) { @@ -82,22 +82,20 @@ class VideoFileHandler extends DataHandlerExtension && is_numeric($stream["height"]) && intval($stream["height"]) > ($event->height) ?? 0) { $event->height = intval($stream["height"]); } - } } $event->video = $video; $event->audio = $audio; } } - if(array_key_exists("format", $data)&& is_array($data["format"])) { + if (array_key_exists("format", $data)&& is_array($data["format"])) { $format = $data["format"]; - if(array_key_exists("duration", $format) && is_numeric($format["duration"])) { - $event->length = floor(floatval($format["duration"]) * 1000); + if (array_key_exists("duration", $format) && is_numeric($format["duration"])) { + $event->length = floor(floatval($format["duration"]) * 1000); } } } - } catch(MediaException $e) { - + } catch (MediaException $e) { } } } diff --git a/ext/handle_video/theme.php b/ext/handle_video/theme.php index c304eff7..eb4a8664 100644 --- a/ext/handle_video/theme.php +++ b/ext/handle_video/theme.php @@ -14,11 +14,11 @@ class VideoFileHandlerTheme extends Themelet $player = make_link('vendor/bower-asset/mediaelement/build/flashmediaelement.swf'); $width="auto"; - if($image->width>1) { + if ($image->width>1) { $width = $image->width."px"; } $height="auto"; - if($image->height>1) { + if ($image->height>1) { $height = $image->height."px"; } diff --git a/ext/help_pages/main.php b/ext/help_pages/main.php index d2821b00..f5f3ff41 100644 --- a/ext/help_pages/main.php +++ b/ext/help_pages/main.php @@ -14,7 +14,6 @@ class HelpPageListBuildingEvent extends Event { $this->pages[$key] = $name; } - } class HelpPageBuildingEvent extends Event @@ -27,10 +26,9 @@ class HelpPageBuildingEvent extends Event $this->key = $key; } - function add_block(Block $block, int $position = 50) + public function add_block(Block $block, int $position = 50) { - if(!array_key_exists("$position",$this->blocks)) - { + if (!array_key_exists("$position", $this->blocks)) { $this->blocks["$position"] = []; } $this->blocks["$position"][] = $block; @@ -55,7 +53,7 @@ class HelpPages extends Extension } else { $name = $event->get_arg(0); $title = $name; - if(array_key_exists($name, $e->pages)) { + if (array_key_exists($name, $e->pages)) { $title = $e->pages[$name]; } @@ -66,7 +64,7 @@ class HelpPages extends Extension asort($hpbe->blocks); foreach ($hpbe->blocks as $key=>$value) { - foreach($value as $block) { + foreach ($value as $block) { $page->add_block($block); } } @@ -89,6 +87,4 @@ class HelpPages extends Extension global $user; $event->add_link("Help", make_link("help")); } - - } diff --git a/ext/help_pages/theme.php b/ext/help_pages/theme.php index 2b3b7818..15892fb0 100644 --- a/ext/help_pages/theme.php +++ b/ext/help_pages/theme.php @@ -2,7 +2,6 @@ class HelpPagesTheme extends Themelet { - public function display_list_page(array $pages) { global $page; @@ -27,5 +26,4 @@ class HelpPagesTheme extends Themelet $page->set_title("Help - $title"); $page->set_heading("Help - $title"); } - } diff --git a/ext/image/main.php b/ext/image/main.php index 9969c3e8..fabf30fe 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -9,7 +9,8 @@ */ -abstract class ImageConfig { +abstract class ImageConfig +{ const THUMB_ENGINE = 'thumb_engine'; const THUMB_WIDTH = 'thumb_width'; const THUMB_HEIGHT = 'thumb_height'; @@ -26,7 +27,6 @@ abstract class ImageConfig { const COLLISION_MERGE = 'merge'; const COLLISION_ERROR = 'error'; - } /** @@ -34,7 +34,6 @@ abstract class ImageConfig { */ class ImageIO extends Extension { - const COLLISION_OPTIONS = ['Error'=>ImageConfig::COLLISION_ERROR, 'Merge'=>ImageConfig::COLLISION_MERGE]; const EXIF_READ_FUNCTION = "exif_read_data"; @@ -267,10 +266,9 @@ class ImageIO extends Extension try { Media::update_image_media_properties($image->hash, strtolower($image->ext)); - } catch(MediaException $e) { - log_warning("add_image","Error while running update_image_media_properties: ".$e->getMessage()); + } catch (MediaException $e) { + log_warning("add_image", "Error while running update_image_media_properties: ".$e->getMessage()); } - } // }}} end add @@ -349,7 +347,7 @@ class ImageIO extends Extension $duplicate = Image::by_hash($image->hash); - if(!is_null($duplicate) && $duplicate->id!=$id) { + if (!is_null($duplicate) && $duplicate->id!=$id) { $error = "Image {$duplicate->id} " . "already has hash {$image->hash}:

" . $this->theme->build_thumb_html($duplicate); throw new ImageReplaceException($error); @@ -388,8 +386,8 @@ class ImageIO extends Extension try { Media::update_image_media_properties($image->hash, $image->ext); - } catch(MediaException $e) { - log_warning("image_replace","Error while running update_image_media_properties: ".$e->getMessage()); + } catch (MediaException $e) { + log_warning("image_replace", "Error while running update_image_media_properties: ".$e->getMessage()); } /* Generate new thumbnail */ diff --git a/ext/image_hash_ban/main.php b/ext/image_hash_ban/main.php index cc3a7ca1..40ef6efc 100644 --- a/ext/image_hash_ban/main.php +++ b/ext/image_hash_ban/main.php @@ -106,7 +106,7 @@ class ImageBan extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::BAN_IMAGE)) { $event->add_nav_link("image_bans", new Link('image_hash_ban/list/1'), "Image Bans", NavLink::is_active(["image_hash_ban"])); } diff --git a/ext/index/main.php b/ext/index/main.php index 9bf22091..59a878c9 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -262,10 +262,13 @@ class Index extends Extension if (SPEED_HAX) { if (!$user->can("big_search")) { $fast_page_limit = 500; - if ($total_pages > $fast_page_limit) $total_pages = $fast_page_limit; + if ($total_pages > $fast_page_limit) { + $total_pages = $fast_page_limit; + } if ($page_number > $fast_page_limit) { $this->theme->display_error( - 404, "Search limit hit", + 404, + "Search limit hit", "Only $fast_page_limit pages of results are searchable - " . "if you want to find older results, use more specific search terms" ); @@ -334,19 +337,19 @@ class Index extends Extension public function onPageNavBuilding(PageNavBuildingEvent $event) { - $event->add_nav_link("posts", new Link('post/list'), "Posts", NavLink::is_active(["post","view"]),20); + $event->add_nav_link("posts", new Link('post/list'), "Posts", NavLink::is_active(["post","view"]), 20); } public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="posts") { + if ($event->parent=="posts") { $event->add_nav_link("posts_all", new Link('post/list'), "All"); } } public function onHelpPageBuilding(HelpPageBuildingEvent $event) { - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "General"; $block->body = $this->theme->get_help_html(); diff --git a/ext/index/theme.php b/ext/index/theme.php index e216ea06..8c881afd 100644 --- a/ext/index/theme.php +++ b/ext/index/theme.php @@ -339,6 +339,5 @@ and of course start organising your images :-)

'; - } } diff --git a/ext/ipban/main.php b/ext/ipban/main.php index 90eb198a..6d62e546 100644 --- a/ext/ipban/main.php +++ b/ext/ipban/main.php @@ -108,7 +108,7 @@ class IPBan extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::BAN_IP)) { $event->add_nav_link("ip_bans", new Link('ip_ban/list'), "IP Bans", NavLink::is_active(["ip_ban"])); } diff --git a/ext/log_db/main.php b/ext/log_db/main.php index 4002b68c..796118e4 100644 --- a/ext/log_db/main.php +++ b/ext/log_db/main.php @@ -123,7 +123,7 @@ class LogDatabase extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::VIEW_EVENTLOG)) { $event->add_nav_link("event_log", new Link('log/view'), "Event Log"); } diff --git a/ext/media/main.php b/ext/media/main.php index 012507b3..a9853e61 100644 --- a/ext/media/main.php +++ b/ext/media/main.php @@ -18,7 +18,6 @@ abstract class MediaConfig const CONVERT_PATH = "media_convert_path"; const VERSION = "ext_media_version"; const MEM_LIMIT = 'media_mem_limit'; - } abstract class MediaEngine @@ -105,14 +104,19 @@ class MediaResizeEvent extends Event public $ignore_aspect_ratio; public $allow_upscale; - public function __construct(String $engine, string $input_path, string $input_type, string $output_path, - int $target_width, int $target_height, - bool $ignore_aspect_ratio = false, - string $target_format = null, - int $target_quality = 80, - bool $minimize = false, - bool $allow_upscale = true) - { + public function __construct( + String $engine, + string $input_path, + string $input_type, + string $output_path, + int $target_width, + int $target_height, + bool $ignore_aspect_ratio = false, + string $target_format = null, + int $target_quality = 80, + bool $minimize = false, + bool $allow_upscale = true + ) { assert(in_array($engine, MediaEngine::ALL)); $this->engine = $engine; $this->input_path = $input_path; @@ -144,7 +148,6 @@ class MediaCheckPropertiesEvent extends Event $this->file_name = $file_name; $this->ext = $ext; } - } @@ -193,7 +196,7 @@ class Media extends Extension [0x52, 0x49, 0x46, 0x46, null, null, null, null, 0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x4C]; - static function imagick_available(): bool + public static function imagick_available(): bool { return extension_loaded("imagick"); } @@ -293,7 +296,6 @@ class Media extends Extension $sb->end_table(); $event->panel->add_block($sb); - } public function onAdminBuilding(AdminBuildingEvent $event) @@ -375,7 +377,8 @@ class Media extends Extension $event->target_format, $event->ignore_aspect_ratio, $event->target_quality, - $event->allow_upscale); + $event->allow_upscale + ); break; case MediaEngine::IMAGICK: @@ -391,7 +394,8 @@ class Media extends Extension $event->ignore_aspect_ratio, $event->target_quality, $event->minimize, - $event->allow_upscale); + $event->allow_upscale + ); //} break; default: @@ -421,7 +425,7 @@ class Media extends Extension public function onHelpPageBuilding(HelpPageBuildingEvent $event) { - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "Media"; $block->body = $this->theme->get_help_html(); @@ -480,7 +484,8 @@ class Media extends Extension "video" => $database->scoresql_value_prepare($mcpe->video), "audio" => $database->scoresql_value_prepare($mcpe->audio), "length" => $mcpe->length - ]); + ] + ); } public function get_images(String $ext = null) @@ -736,11 +741,12 @@ class Media extends Extension // } // } - public static function is_lossless(string $filename, string $format) { - if(in_array($format, self::LOSSLESS_FORMATS)) { + public static function is_lossless(string $filename, string $format) + { + if (in_array($format, self::LOSSLESS_FORMATS)) { return true; } - switch($format) { + switch ($format) { case "webp": return self::is_lossless_webp($filename); break; @@ -759,8 +765,7 @@ class Media extends Extension int $output_quality = 80, bool $minimize = false, bool $allow_upscale = true - ): void - { + ): void { global $config; $convert = $config->get_string(MediaConfig::CONVERT_PATH); @@ -773,7 +778,7 @@ class Media extends Extension $output_type = $input_type; } - if($output_type=="webp" && self::is_lossless($input_path, $input_type)) { + if ($output_type=="webp" && self::is_lossless($input_path, $input_type)) { $output_type = self::WEBP_LOSSLESS; } @@ -847,8 +852,7 @@ class Media extends Extension bool $ignore_aspect_ratio = false, int $output_quality = 80, bool $allow_upscale = true - ) - { + ) { $width = $info[0]; $height = $info[1]; @@ -949,16 +953,16 @@ class Media extends Extension // Actually resize the image. if (imagecopyresampled( - $image_resized, - $image, - 0, - 0, - 0, - 0, - $new_width, - $new_height, - $width, - $height + $image_resized, + $image, + 0, + 0, + 0, + 0, + $new_width, + $new_height, + $width, + $height ) === false) { throw new MediaException("Unable to copy resized image data to new image"); } @@ -1047,7 +1051,6 @@ class Media extends Extension } else { throw new MediaException("Unable to open file for byte check: $file_name"); } - } public static function is_animated_webp(String $image_filename): bool @@ -1091,7 +1094,7 @@ class Media extends Extension * @param $format * @return string|null The format name that the media extension will recognize. */ - static public function normalize_format(string $format, ?bool $lossless = null): ?string + public static function normalize_format(string $format, ?bool $lossless = null): ?string { if ($format == "webp") { if ($lossless === true) { @@ -1114,7 +1117,7 @@ class Media extends Extension * @param string $filename * @return array [width, height] */ - static public function video_size(string $filename): array + public static function video_size(string $filename): array { global $config; $ffmpeg = $config->get_string(MediaConfig::FFMPEG_PATH); @@ -1139,5 +1142,4 @@ class Media extends Extension log_debug('Media', "Getting video size with `$cmd`, returns $output -- $size[0], $size[1]"); return $size; } - } diff --git a/ext/media/theme.php b/ext/media/theme.php index 7871ee69..15c3e03e 100644 --- a/ext/media/theme.php +++ b/ext/media/theme.php @@ -42,6 +42,5 @@ class MediaTheme extends Themelet

These search terms depend on the items being scanned for media content. Automatic scanning was implemented in mid-2019, so items uploaded before, or items uploaded on a system without ffmpeg, will require additional scanning before this will work.

'; - } } diff --git a/ext/not_a_tag/main.php b/ext/not_a_tag/main.php index 369ab0ff..9c44ec6a 100644 --- a/ext/not_a_tag/main.php +++ b/ext/not_a_tag/main.php @@ -61,7 +61,7 @@ class NotATag extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="tags") { + if ($event->parent==="tags") { if ($user->can(Permissions::BAN_IMAGE)) { $event->add_nav_link("untags", new Link('untag/list/1'), "UnTags"); } diff --git a/ext/notes/main.php b/ext/notes/main.php index 18c45f82..11c07729 100644 --- a/ext/notes/main.php +++ b/ext/notes/main.php @@ -218,7 +218,7 @@ class Notes extends Extension public function onHelpPageBuilding(HelpPageBuildingEvent $event) { - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "Notes"; $block->body = $this->theme->get_help_html(); diff --git a/ext/notes/theme.php b/ext/notes/theme.php index 6878c9c2..5d4d016b 100644 --- a/ext/notes/theme.php +++ b/ext/notes/theme.php @@ -269,7 +269,5 @@ class NotesTheme extends Themelet

Returns images with note(s) by user 123.

'; - } - } diff --git a/ext/numeric_score/main.php b/ext/numeric_score/main.php index 0b93c369..30394e14 100644 --- a/ext/numeric_score/main.php +++ b/ext/numeric_score/main.php @@ -230,7 +230,7 @@ class NumericScore extends Extension public function onHelpPageBuilding(HelpPageBuildingEvent $event) { - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "Numeric Score"; $block->body = $this->theme->get_help_html(); @@ -306,11 +306,10 @@ class NumericScore extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="posts") { + if ($event->parent=="posts") { $event->add_nav_link("numeric_score_day", new Link('popular_by_day'), "Popular by Day"); $event->add_nav_link("numeric_score_month", new Link('popular_by_month'), "Popular by Month"); $event->add_nav_link("numeric_score_year", new Link('popular_by_year'), "Popular by Year"); - } } diff --git a/ext/numeric_score/theme.php b/ext/numeric_score/theme.php index e6dbe7fa..1d427322 100644 --- a/ext/numeric_score/theme.php +++ b/ext/numeric_score/theme.php @@ -132,6 +132,5 @@ class NumericScoreTheme extends Themelet

Sorts the search results by score, ascending.

'; - } } diff --git a/ext/pm/main.php b/ext/pm/main.php index d2c7f44d..6d989c14 100644 --- a/ext/pm/main.php +++ b/ext/pm/main.php @@ -96,7 +96,7 @@ class PrivMsg extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="user") { + if ($event->parent==="user") { if (!$user->is_anonymous()) { $count = $this->count_pms($user); $h_count = $count > 0 ? " ($count)" : ""; diff --git a/ext/pools/main.php b/ext/pools/main.php index 2fefbf73..fa8cd5d5 100644 --- a/ext/pools/main.php +++ b/ext/pools/main.php @@ -46,7 +46,6 @@ class PoolAddPostsEvent extends Event $this->pool_id = $pool_id; $this->posts = $posts; } - } class PoolCreationEvent extends Event @@ -67,7 +66,6 @@ class PoolCreationEvent extends Event $this->public = $public; $this->description = $description; } - } class Pools extends Extension @@ -152,7 +150,7 @@ class Pools extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="pool") { + if ($event->parent=="pool") { $event->add_nav_link("pool_list", new Link('pool/list'), "List"); $event->add_nav_link("pool_new", new Link('pool/new'), "Create"); $event->add_nav_link("pool_updated", new Link('pool/updated'), "Changes"); @@ -198,7 +196,8 @@ class Pools extends Extension $title, $user, $_POST["public"] === "Y", - $_POST["description"]); + $_POST["description"] + ); send_event($event); $page->set_mode(PageMode::REDIRECT); @@ -374,7 +373,7 @@ class Pools extends Extension public function onHelpPageBuilding(HelpPageBuildingEvent $event) { - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "Pools"; $block->body = $this->theme->get_help_html(); @@ -443,8 +442,8 @@ class Pools extends Extension $pools = $database->get_all("SELECT * FROM pools ORDER BY title "); - $event->add_action("bulk_pool_add_existing", "Add To (P)ool", "p","", $this->theme->get_bulk_pool_selector($pools)); - $event->add_action("bulk_pool_add_new", "Create Pool", "","", $this->theme->get_bulk_pool_input($event->search_terms)); + $event->add_action("bulk_pool_add_existing", "Add To (P)ool", "p", "", $this->theme->get_bulk_pool_selector($pools)); + $event->add_action("bulk_pool_add_new", "Create Pool", "", "", $this->theme->get_bulk_pool_input($event->search_terms)); } public function onBulkAction(BulkActionEvent $event) @@ -461,7 +460,8 @@ class Pools extends Extension if ($this->have_permission($user, $pool)) { send_event( - new PoolAddPostsEvent($pool_id,iterator_map_to_array("image_to_id", $event->items))); + new PoolAddPostsEvent($pool_id, iterator_map_to_array("image_to_id", $event->items)) + ); } break; case "bulk_pool_add_new": @@ -809,7 +809,7 @@ class Pools extends Extension if (ext_is_live("Ratings")) { $query .= "AND i.rating IN (".Ratings::privs_to_sql(Ratings::get_user_class_privs($user)).")"; } - if(ext_is_live("trash")) { + if (ext_is_live("trash")) { $query .= $database->scoreql_to_sql(" AND trash = SCORE_BOOL_N "); } @@ -823,10 +823,10 @@ class Pools extends Extension ); $totalPages = ceil($database->get_one( - " + " SELECT COUNT(*) FROM pool_images p $query", - ["pid" => $poolID] + ["pid" => $poolID] ) / $imagesPerPage); diff --git a/ext/pools/theme.php b/ext/pools/theme.php index 86a2406c..bfaacb30 100644 --- a/ext/pools/theme.php +++ b/ext/pools/theme.php @@ -401,7 +401,7 @@ class PoolsTheme extends Themelet public function get_bulk_pool_input(array $search_terms) { - return ""; + return ""; } @@ -429,6 +429,5 @@ class PoolsTheme extends Themelet

Returns images in the "swimming pool" pool. Note that the underscore becomes a space

'; - } } diff --git a/ext/post_titles/config.php b/ext/post_titles/config.php index 5499bfad..cd51aa58 100644 --- a/ext/post_titles/config.php +++ b/ext/post_titles/config.php @@ -6,4 +6,4 @@ abstract class PostTitlesConfig public const VERSION = "ext_post_titles_version"; public const DEFAULT_TO_FILENAME = "post_titles_default_to_filename"; public const SHOW_IN_WINDOW_TITLE = "post_titles_show_in_window_title"; -} \ No newline at end of file +} diff --git a/ext/post_titles/events/post_title_set_event.php b/ext/post_titles/events/post_title_set_event.php index 57f942f7..94ca7476 100644 --- a/ext/post_titles/events/post_title_set_event.php +++ b/ext/post_titles/events/post_title_set_event.php @@ -10,4 +10,4 @@ class PostTitleSetEvent extends Event $this->image = $image; $this->title = $title; } -} \ No newline at end of file +} diff --git a/ext/post_titles/main.php b/ext/post_titles/main.php index 50f550c3..a5bdde2f 100644 --- a/ext/post_titles/main.php +++ b/ext/post_titles/main.php @@ -42,7 +42,7 @@ class PostTitles extends Extension { global $config; - if($config->get_bool(PostTitlesConfig::SHOW_IN_WINDOW_TITLE)) { + if ($config->get_bool(PostTitlesConfig::SHOW_IN_WINDOW_TITLE)) { $event->set_title(self::get_title($event->get_image())); } } @@ -73,8 +73,8 @@ class PostTitles extends Extension { $sb = new SetupBlock("Post Titles"); $sb->start_table(); - $sb->add_bool_option(PostTitlesConfig::DEFAULT_TO_FILENAME,"Default to filename", true); - $sb->add_bool_option(PostTitlesConfig::SHOW_IN_WINDOW_TITLE,"Show in window title", true); + $sb->add_bool_option(PostTitlesConfig::DEFAULT_TO_FILENAME, "Default to filename", true); + $sb->add_bool_option(PostTitlesConfig::SHOW_IN_WINDOW_TITLE, "Show in window title", true); $sb->end_table(); $event->panel->add_block($sb); @@ -94,9 +94,9 @@ class PostTitles extends Extension global $config; $title = $image->title??""; - if(empty($title) && $config->get_bool(PostTitlesConfig::DEFAULT_TO_FILENAME)) { + if (empty($title) && $config->get_bool(PostTitlesConfig::DEFAULT_TO_FILENAME)) { $info = pathinfo($image->filename); - if(array_key_exists("extension",$info)) { + if (array_key_exists("extension", $info)) { $title = basename($image->filename, '.' . $info['extension']); } else { $title = $image->filename; diff --git a/ext/post_titles/theme.php b/ext/post_titles/theme.php index 53a6c1b5..9776389a 100644 --- a/ext/post_titles/theme.php +++ b/ext/post_titles/theme.php @@ -3,8 +3,6 @@ class PostTitlesTheme extends Themelet { public function get_title_set_html(string $title, bool $can_set): string { - - $html = " Title diff --git a/ext/random_image/main.php b/ext/random_image/main.php index b03d21e4..08c04c19 100644 --- a/ext/random_image/main.php +++ b/ext/random_image/main.php @@ -78,7 +78,7 @@ class RandomImage extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="posts") { + if ($event->parent=="posts") { $event->add_nav_link("posts_random", new Link('random_image/view'), "Random Image"); } } diff --git a/ext/random_list/main.php b/ext/random_list/main.php index 47c55b9c..259edd78 100644 --- a/ext/random_list/main.php +++ b/ext/random_list/main.php @@ -77,7 +77,7 @@ class RandomList extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="posts") { + if ($event->parent=="posts") { $event->add_nav_link("posts_random", new Link('random'), "Shuffle"); } } diff --git a/ext/rating/main.php b/ext/rating/main.php index 98daa256..a245d18f 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -25,7 +25,8 @@ global $_shm_ratings; $_shm_ratings = []; -class ImageRating { +class ImageRating +{ /** * @var string */ @@ -46,9 +47,9 @@ class ImageRating { */ public $order = 0; - public function __construct( string $code, string $name, string $search_term, int $order) + public function __construct(string $code, string $name, string $search_term, int $order) { - if(strlen($code)!=1) { + if (strlen($code)!=1) { throw new Exception("Rating code must be exactly one character"); } @@ -56,29 +57,30 @@ class ImageRating { $this->code = $code; $this->search_term = $search_term; $this->order = $order; - } } -function clear_ratings() { +function clear_ratings() +{ global $_shm_ratings; $keys = array_keys($_shm_ratings); - foreach ($keys as $key){ - if($key=="?") { + foreach ($keys as $key) { + if ($key=="?") { continue; } unset($_shm_ratings[$key]); } } -function add_rating(ImageRating $rating) { +function add_rating(ImageRating $rating) +{ global $_shm_ratings; - if($rating->code=="?"&&array_key_exists("?",$_shm_ratings)) { + if ($rating->code=="?"&&array_key_exists("?", $_shm_ratings)) { throw new Exception("? is a reserved rating code that cannot be overridden"); } - if($rating->code!="?"&&in_array(strtolower($rating->search_term), Ratings::UNRATED_KEYWORDS)) { + if ($rating->code!="?"&&in_array(strtolower($rating->search_term), Ratings::UNRATED_KEYWORDS)) { throw new Exception("$rating->search_term is a reserved search term"); } @@ -110,7 +112,8 @@ class RatingSetEvent extends Event } } -abstract class RatingsConfig { +abstract class RatingsConfig +{ const VERSION = "ext_ratings2_version"; const USER_DEFAULTS = "ratings_default"; } @@ -136,7 +139,7 @@ class Ratings extends Extension array_push($search_terms, $rating->search_term); } $this->search_regexp = "/^rating[=|:](?:(\*|[" . $codes . "]+)|(" . - implode("|", $search_terms) . "|".implode("|",self::UNRATED_KEYWORDS)."))$/D"; + implode("|", $search_terms) . "|".implode("|", self::UNRATED_KEYWORDS)."))$/D"; } public function get_priority(): int @@ -160,7 +163,8 @@ class Ratings extends Extension } } - public function onInitUserConfig(InitUserConfigEvent $event) { + public function onInitUserConfig(InitUserConfigEvent $event) + { $event->user_config->set_default_array(RatingsConfig::USER_DEFAULTS, self::get_user_class_privs($event->user)); } @@ -169,9 +173,12 @@ class Ratings extends Extension global $user, $user_config; $event->add__html( - $this->theme->get_user_options($user, + $this->theme->get_user_options( + $user, self::get_user_default_ratings($user), - self::get_user_class_privs($user))); + self::get_user_class_privs($user) + ) + ); } public function onSetupBuilding(SetupBuildingEvent $event) @@ -254,7 +261,7 @@ class Ratings extends Extension { global $user; - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "Ratings"; @@ -279,7 +286,7 @@ class Ratings extends Extension if (preg_match($this->search_regexp, strtolower($event->term), $matches)) { $ratings = $matches[1] ? $matches[1] : $matches[2][0]; - if(count($matches)>2&&in_array($matches[2], self::UNRATED_KEYWORDS)) { + if (count($matches)>2&&in_array($matches[2], self::UNRATED_KEYWORDS)) { $ratings = "?"; } @@ -302,7 +309,7 @@ class Ratings extends Extension if (preg_match($this->search_regexp, strtolower($event->term), $matches) && $event->parse) { $ratings = $matches[1] ? $matches[1] : $matches[2][0]; - if(count($matches)>2&&in_array($matches[2], self::UNRATED_KEYWORDS)) { + if (count($matches)>2&&in_array($matches[2], self::UNRATED_KEYWORDS)) { $ratings = "?"; } @@ -348,16 +355,16 @@ class Ratings extends Extension switch ($action) { case "update_ratings": $event->redirect = true; - if(!array_key_exists("rating_old", $_POST) || empty($_POST["rating_old"])) { + if (!array_key_exists("rating_old", $_POST) || empty($_POST["rating_old"])) { return; } - if(!array_key_exists("rating_new", $_POST) || empty($_POST["rating_new"])) { + if (!array_key_exists("rating_new", $_POST) || empty($_POST["rating_new"])) { return; } $old = $_POST["rating_old"]; $new = $_POST["rating_new"]; - if($user->can("bulk_edit_image_rating")) { + if ($user->can("bulk_edit_image_rating")) { $database->execute("UPDATE images SET rating = :new WHERE rating = :old", ["new"=>$new, "old"=>$old ]); } @@ -371,7 +378,7 @@ class Ratings extends Extension global $user; if ($user->can(Permissions::BULK_EDIT_IMAGE_RATING)) { - $event->add_action("bulk_rate", "Set (R)ating", "r","", $this->theme->get_selection_rater_html(["?"])); + $event->add_action("bulk_rate", "Set (R)ating", "r", "", $this->theme->get_selection_rater_html(["?"])); } } @@ -455,7 +462,6 @@ class Ratings extends Extension break; } } - } public static function get_sorted_ratings(): array @@ -490,10 +496,10 @@ class Ratings extends Extension public static function privs_to_sql(array $privs): string { $arr = []; - foreach($privs as $i) { + foreach ($privs as $i) { $arr[] = "'" . $i . "'"; } - if(sizeof($arr)==0) { + if (sizeof($arr)==0) { return "' '"; } $set = join(', ', $arr); @@ -504,7 +510,7 @@ class Ratings extends Extension { global $_shm_ratings; - if(array_key_exists($rating, $_shm_ratings)) { + if (array_key_exists($rating, $_shm_ratings)) { return $_shm_ratings[$rating]->name; } return "Unknown"; @@ -557,7 +563,7 @@ class Ratings extends Extension $config->set_int(RatingsConfig::VERSION, 2); } - if($config->get_int(RatingsConfig::VERSION) < 3) { + if ($config->get_int(RatingsConfig::VERSION) < 3) { $database->Execute("UPDATE images SET rating = 'u' WHERE rating is null"); switch ($database->get_driver_name()) { case DatabaseDriver::MYSQL: @@ -568,20 +574,20 @@ class Ratings extends Extension $database->Execute("ALTER TABLE images ALTER COLUMN rating SET NOT NULL"); break; } - $config->set_int(RatingsConfig::VERSION, 3); - } + $config->set_int(RatingsConfig::VERSION, 3); + } if ($config->get_int(RatingsConfig::VERSION) < 4) { $value = $config->get_string("ext_rating_anon_privs"); - if(!empty($value)) { + if (!empty($value)) { $config->set_array("ext_rating_anonymous_privs", str_split($value)); } $value = $config->get_string("ext_rating_user_privs"); - if(!empty($value)) { + if (!empty($value)) { $config->set_array("ext_rating_user_privs", str_split($value)); } $value = $config->get_string("ext_rating_admin_privs"); - if(!empty($value)) { + if (!empty($value)) { $config->set_array("ext_rating_admin_privs", str_split($value)); } diff --git a/ext/rating/theme.php b/ext/rating/theme.php index c16b15dd..19ef6354 100644 --- a/ext/rating/theme.php +++ b/ext/rating/theme.php @@ -30,9 +30,9 @@ class RatingsTheme extends Themelet $html = make_form(make_link("admin/update_ratings"))."
Change
To - // - // - // - // "; + // ".make_form(make_link("admin/bulk_rate"))." + // + // + // + // + // "; // $page->add_block(new Block("List Controls", $html, "left")); // } - public function get_selection_rater_html(array $selected_options, bool $multiple = false, array $available_options = null) { + public function get_selection_rater_html(array $selected_options, bool $multiple = false, array $available_options = null) + { global $_shm_ratings; - $output = ""; $options = Ratings::get_sorted_ratings(); - foreach($options as $option) { - if($available_options!=null && !in_array($option->code, $available_options)) { - continue; + foreach ($options as $option) { + if ($available_options!=null && !in_array($option->code, $available_options)) { + continue; } - $output .= ""; - } - return $output.""; + } + return $output.""; } public function get_help_html(array $ratings) diff --git a/ext/regen_thumb/main.php b/ext/regen_thumb/main.php index 62e80664..4c252be1 100644 --- a/ext/regen_thumb/main.php +++ b/ext/regen_thumb/main.php @@ -69,7 +69,7 @@ class RegenThumb extends Extension global $user; if ($user->can(Permissions::DELETE_IMAGE)) { - $event->add_action("bulk_regen", "Regen Thumbnails", "","", $this->theme->bulk_html()); + $event->add_action("bulk_regen", "Regen Thumbnails", "", "", $this->theme->bulk_html()); } } diff --git a/ext/relatationships/main.php b/ext/relatationships/main.php index 905bc1e8..443a5098 100644 --- a/ext/relatationships/main.php +++ b/ext/relatationships/main.php @@ -12,7 +12,7 @@ class ImageRelationshipSetEvent extends Event public $parent_id; - public function __construct(int $child_id, int $parent_id) + public function __construct(int $child_id, int $parent_id) { $this->child_id = $child_id; $this->parent_id = $parent_id; @@ -51,7 +51,7 @@ class Relationships extends Extension { if (isset($_POST['tag_edit__tags']) ? !preg_match('/parent[=|:]/', $_POST["tag_edit__tags"]) : true) { //Ignore tag_edit__parent if tags contain parent metatag if (isset($_POST["tag_edit__parent"]) ? ctype_digit($_POST["tag_edit__parent"]) : false) { - send_event(new ImageRelationshipSetEvent($event->image->id,(int) $_POST["tag_edit__parent"])); + send_event(new ImageRelationshipSetEvent($event->image->id, (int) $_POST["tag_edit__parent"])); } else { $this->remove_parent($event->image->id); } @@ -83,7 +83,7 @@ class Relationships extends Extension public function onHelpPageBuilding(HelpPageBuildingEvent $event) { - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "Relationships"; $block->body = $this->theme->get_help_html(); @@ -139,15 +139,14 @@ class Relationships extends Extension $old_parent = $database->get_one("SELECT parent_id FROM images WHERE id = :cid", ["cid"=>$event->child_id]); - if($old_parent!=$event->parent_id) { + if ($old_parent!=$event->parent_id) { if ($database->get_row("SELECT 1 FROM images WHERE id = :pid", ["pid" => $event->parent_id])) { - $result = $database->execute("UPDATE images SET parent_id = :pid WHERE id = :cid", ["pid" => $event->parent_id, "cid" => $event->child_id]); if ($result->rowCount() > 0) { $database->execute("UPDATE images SET has_children = TRUE WHERE id = :pid", ["pid" => $event->parent_id]); - if($old_parent!=null) { + if ($old_parent!=null) { $this->set_has_children($old_parent); } } @@ -162,7 +161,7 @@ class Relationships extends Extension $results = $database->get_all_iterable("SELECT * FROM images WHERE parent_id = :pid ", ["pid"=>$image->id]); $output = []; foreach ($results as $result) { - if($result["id"]==$omit) { + if ($result["id"]==$omit) { continue; } $output[] = new Image($result); @@ -187,13 +186,11 @@ class Relationships extends Extension // Doesn't work on pgsql // $database->execute("UPDATE images SET has_children = (SELECT * FROM (SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END FROM images WHERE parent_id = :pid) AS sub) -// WHERE id = :pid", ["pid"=>$parentID]); + // WHERE id = :pid", ["pid"=>$parentID]); $database->execute( "UPDATE images SET has_children = EXISTS (SELECT 1 FROM images WHERE parent_id = :pid) WHERE id = :pid", - ["pid"=>$parent_id]); + ["pid"=>$parent_id] + ); } - - - } diff --git a/ext/relatationships/test.php b/ext/relatationships/test.php index b486378a..69c1bbd1 100644 --- a/ext/relatationships/test.php +++ b/ext/relatationships/test.php @@ -34,7 +34,7 @@ class RelationshipTest extends ShimmiePHPUnitTestCase $image_3 = Image::by_id($image_id_3); $this->assertNull($image_1->parent_id); - $this->assertEquals($image_id_1,$image_2->parent_id); + $this->assertEquals($image_id_1, $image_2->parent_id); $this->assertNull($image_3->parent_id); $this->assertTrue($image_1->has_children); $this->assertFalse($image_2->has_children); @@ -56,7 +56,7 @@ class RelationshipTest extends ShimmiePHPUnitTestCase $image_3 = Image::by_id($image_id_3); $this->assertNull($image_1->parent_id); - $this->assertEquals($image_id_3,$image_2->parent_id); + $this->assertEquals($image_id_3, $image_2->parent_id); $this->assertNull($image_3->parent_id); $this->assertFalse($image_2->has_children); $this->assertFalse($image_2->has_children); @@ -129,7 +129,7 @@ class RelationshipTest extends ShimmiePHPUnitTestCase $image_3 = Image::by_id($image_id_3); $this->assertNull($image_1->parent_id); - $this->assertEquals($image_id_1,$image_2->parent_id); + $this->assertEquals($image_id_1, $image_2->parent_id); $this->assertNull($image_3->parent_id); $this->assertTrue($image_1->has_children); $this->assertFalse($image_2->has_children); @@ -151,8 +151,8 @@ class RelationshipTest extends ShimmiePHPUnitTestCase $image_2 = Image::by_id($image_id_2); $image_3 = Image::by_id($image_id_3); - $this->assertEquals($image_id_3,$image_1->parent_id); - $this->assertEquals($image_id_1,$image_2->parent_id); + $this->assertEquals($image_id_3, $image_1->parent_id); + $this->assertEquals($image_id_1, $image_2->parent_id); $this->assertNull($image_3->parent_id); $this->assertTrue($image_1->has_children); $this->assertFalse($image_2->has_children); @@ -175,7 +175,7 @@ class RelationshipTest extends ShimmiePHPUnitTestCase $image_3 = Image::by_id($image_id_3); $this->assertNull($image_1->parent_id); - $this->assertEquals($image_id_1,$image_2->parent_id); + $this->assertEquals($image_id_1, $image_2->parent_id); $this->assertNull($image_3->parent_id); $this->assertTrue($image_1->has_children); $this->assertFalse($image_2->has_children); diff --git a/ext/relatationships/theme.php b/ext/relatationships/theme.php index 40c0fe8c..b1af3843 100644 --- a/ext/relatationships/theme.php +++ b/ext/relatationships/theme.php @@ -72,6 +72,5 @@ class RelationshipsTheme extends Themelet

Returns images that have no children.

'; - } } diff --git a/ext/report_image/main.php b/ext/report_image/main.php index d5436e6b..5045edd1 100644 --- a/ext/report_image/main.php +++ b/ext/report_image/main.php @@ -136,7 +136,7 @@ class ReportImage extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::VIEW_IMAGE_REPORT)) { $count = $this->count_reported_images(); $h_count = $count > 0 ? " ($count)" : ""; diff --git a/ext/resize/main.php b/ext/resize/main.php index 212890fe..d66beb91 100644 --- a/ext/resize/main.php +++ b/ext/resize/main.php @@ -59,7 +59,7 @@ class ResizeImage extends Extension { $sb = new SetupBlock("Image Resize"); $sb->start_table(); - $sb->add_choice_option(ResizeConfig::ENGINE, Media::IMAGE_MEDIA_ENGINES, "Engine: ", true); + $sb->add_choice_option(ResizeConfig::ENGINE, Media::IMAGE_MEDIA_ENGINES, "Engine: ", true); $sb->add_bool_option(ResizeConfig::ENABLED, "Allow resizing images: ", true); $sb->add_bool_option(ResizeConfig::UPLOAD, "Resize on upload: ", true); $sb->end_table(); @@ -192,7 +192,7 @@ class ResizeImage extends Extension $engine = $config->get_string(ResizeConfig::ENGINE); - if(!$this->can_resize_format($image_obj->ext, $image_obj->lossless)) { + if (!$this->can_resize_format($image_obj->ext, $image_obj->lossless)) { throw new ImageResizeException("Engine $engine cannot resize selected image"); } diff --git a/ext/rss_comments/main.php b/ext/rss_comments/main.php index f64869e1..d580b468 100644 --- a/ext/rss_comments/main.php +++ b/ext/rss_comments/main.php @@ -82,9 +82,8 @@ EOD; public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="comment") { + if ($event->parent=="comment") { $event->add_nav_link("comment_rss", new Link('rss/comments'), "Feed"); } } - } diff --git a/ext/rss_images/main.php b/ext/rss_images/main.php index 6e5f9026..874555a0 100644 --- a/ext/rss_images/main.php +++ b/ext/rss_images/main.php @@ -119,7 +119,7 @@ class RSS_Images extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="posts") { + if ($event->parent=="posts") { $event->add_nav_link("posts_rss", new Link('rss/images'), "Feed"); } } diff --git a/ext/setup/config.php b/ext/setup/config.php index 2b5be64e..ab512925 100644 --- a/ext/setup/config.php +++ b/ext/setup/config.php @@ -9,4 +9,4 @@ class SetupConfig public const THEME = "theme"; public const WORD_WRAP = "word_wrap"; public const COMMENT_CAPTCHA = "comment_captcha"; -} \ No newline at end of file +} diff --git a/ext/setup/main.php b/ext/setup/main.php index b7bf6181..ac9d9b5b 100644 --- a/ext/setup/main.php +++ b/ext/setup/main.php @@ -142,20 +142,33 @@ class SetupBlock extends Block - private function format_option(string $name, $html, ?string $label, bool $table_row) { + private function format_option(string $name, $html, ?string $label, bool $table_row) + { global $config; - if($table_row) $this->start_table_row(); - if($table_row) $this->start_table_header_cell(); + if ($table_row) { + $this->start_table_row(); + } + if ($table_row) { + $this->start_table_header_cell(); + } if (!is_null($label)) { $this->body .= ""; } - if($table_row) $this->end_table_header_cell(); + if ($table_row) { + $this->end_table_header_cell(); + } - if($table_row) $this->start_table_cell(); + if ($table_row) { + $this->start_table_cell(); + } $this->body .= $html; - if($table_row) $this->end_table_cell(); - if($table_row) $this->end_table_row(); + if ($table_row) { + $this->end_table_cell(); + } + if ($table_row) { + $this->end_table_row(); + } } public function add_text_option(string $name, string $label=null, bool $table_row = false) @@ -187,7 +200,7 @@ class SetupBlock extends Block $checked = $config->get_bool($name) ? " checked" : ""; $html = ""; - if(!$table_row&&!is_null($label)) { + if (!$table_row&&!is_null($label)) { $html .= ""; } @@ -216,7 +229,6 @@ class SetupBlock extends Block $html .= "\n"; $this->format_option($name, $html, $label, $table_row); - } public function add_shorthand_int_option(string $name, string $label=null, bool $table_row = false) @@ -414,7 +426,7 @@ class Setup extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::CHANGE_SETTING)) { $event->add_nav_link("setup", new Link('setup'), "Board Config", null, 0); } diff --git a/ext/source_history/main.php b/ext/source_history/main.php index 3fd47cab..ee907642 100644 --- a/ext/source_history/main.php +++ b/ext/source_history/main.php @@ -85,7 +85,7 @@ class Source_History extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::BULK_EDIT_IMAGE_TAG)) { $event->add_nav_link("source_history", new Link('source_history/all/1'), "Source Changes", NavLink::is_active(["source_history"])); } diff --git a/ext/system/main.php b/ext/system/main.php index 2cce82c1..58baff4b 100644 --- a/ext/system/main.php +++ b/ext/system/main.php @@ -26,6 +26,4 @@ class System extends Extension { $event->add_nav_link("system", new Link('system'), "System"); } - - } diff --git a/ext/tag_categories/config.php b/ext/tag_categories/config.php index ee6b6637..f23f2a82 100644 --- a/ext/tag_categories/config.php +++ b/ext/tag_categories/config.php @@ -5,4 +5,4 @@ abstract class TagCategoriesConfig public const VERSION = "ext_tag_categories_version"; public const SPLIT_ON_VIEW = "tag_categories_split_on_view"; -} \ No newline at end of file +} diff --git a/ext/tag_categories/main.php b/ext/tag_categories/main.php index 95e1caeb..6a4ee7a5 100644 --- a/ext/tag_categories/main.php +++ b/ext/tag_categories/main.php @@ -75,7 +75,8 @@ class TagCategories extends Extension $count = $matches[3]; $types = $database->get_col( - $database->scoreql_to_sql('SELECT SCORE_STRNORM(category) FROM image_tag_categories')); + $database->scoreql_to_sql('SELECT SCORE_STRNORM(category) FROM image_tag_categories') + ); if (in_array($type, $types)) { $event->add_querylet( new Querylet($database->scoreql_to_sql("EXISTS ( @@ -93,7 +94,7 @@ class TagCategories extends Extension public function onHelpPageBuilding(HelpPageBuildingEvent $event) { - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "Tag Categories"; $block->body = $this->theme->get_help_html(); diff --git a/ext/tag_categories/theme.php b/ext/tag_categories/theme.php index c1894a27..e0229151 100644 --- a/ext/tag_categories/theme.php +++ b/ext/tag_categories/theme.php @@ -113,6 +113,5 @@ class TagCategoriesTheme extends Themelet

Can use <, <=, >, >=, or =.

Category name is not case sensitive, category must exist for search to work.

'; - } } diff --git a/ext/tag_edit/main.php b/ext/tag_edit/main.php index df6d8b43..9bc356c1 100644 --- a/ext/tag_edit/main.php +++ b/ext/tag_edit/main.php @@ -258,7 +258,7 @@ class TagEdit extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="tags") { + if ($event->parent=="tags") { $event->add_nav_link("tags_help", new Link('ext_doc/tag_edit'), "Help"); } } diff --git a/ext/tag_history/main.php b/ext/tag_history/main.php index b317abd3..419cc466 100644 --- a/ext/tag_history/main.php +++ b/ext/tag_history/main.php @@ -85,7 +85,7 @@ class Tag_History extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::BULK_EDIT_IMAGE_TAG)) { $event->add_nav_link("tag_history", new Link('tag_history/all/1'), "Tag Changes", NavLink::is_active(["tag_history"])); } diff --git a/ext/tag_list/main.php b/ext/tag_list/main.php index 33efe3b8..baa81840 100644 --- a/ext/tag_list/main.php +++ b/ext/tag_list/main.php @@ -100,7 +100,7 @@ class TagList extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="tags") { + if ($event->parent=="tags") { $event->add_nav_link("tags_map", new Link('tags/map'), "Map"); $event->add_nav_link("tags_alphabetic", new Link('tags/alphabetic'), "Alphabetic"); $event->add_nav_link("tags_popularity", new Link('tags/popularity'), "Popularity"); diff --git a/ext/tips/main.php b/ext/tips/main.php index 60564028..7f1dbad4 100644 --- a/ext/tips/main.php +++ b/ext/tips/main.php @@ -76,7 +76,7 @@ class Tips extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->is_admin()) { $event->add_nav_link("tips", new Link('tips/list'), "Tips Editor"); } diff --git a/ext/transcode/main.php b/ext/transcode/main.php index 17fef4ad..6cf121a8 100644 --- a/ext/transcode/main.php +++ b/ext/transcode/main.php @@ -98,7 +98,7 @@ class TranscodeImage extends Extension $sb->start_table(); $sb->add_bool_option(TranscodeConfig::ENABLED, "Allow transcoding images: ", true); $sb->add_bool_option(TranscodeConfig::UPLOAD, "Transcode on upload: ", true); - $sb->add_choice_option(TranscodeConfig::ENGINE, Media::IMAGE_MEDIA_ENGINES, "Engine", true); + $sb->add_choice_option(TranscodeConfig::ENGINE, Media::IMAGE_MEDIA_ENGINES, "Engine", true); foreach (self::INPUT_FORMATS as $display=>$format) { if (in_array($format, MediaEngine::INPUT_SUPPORT[$engine])) { $outputs = $this->get_supported_output_formats($engine, $format); @@ -181,7 +181,7 @@ class TranscodeImage extends Extension $engine = $config->get_string(TranscodeConfig::ENGINE); if ($user->is_admin()) { - $event->add_action(self::ACTION_BULK_TRANSCODE, "Transcode", null,"", $this->theme->get_transcode_picker_html($this->get_supported_output_formats($engine))); + $event->add_action(self::ACTION_BULK_TRANSCODE, "Transcode", null, "", $this->theme->get_transcode_picker_html($this->get_supported_output_formats($engine))); } } @@ -230,7 +230,7 @@ class TranscodeImage extends Extension private function get_supported_output_formats($engine, ?String $omit_format = null, ?bool $lossless = null): array { - if($omit_format!=null) { + if ($omit_format!=null) { $omit_format = Media::normalize_format($omit_format, $lossless); } $output = []; @@ -241,7 +241,7 @@ class TranscodeImage extends Extension $output[$key] = $value; continue; } - if(Media::is_output_supported($engine, $value) + if (Media::is_output_supported($engine, $value) &&(empty($omit_format)||$omit_format!=$value)) { $output[$key] = $value; } diff --git a/ext/transcode/theme.php b/ext/transcode/theme.php index bee29cc8..c52953b4 100644 --- a/ext/transcode/theme.php +++ b/ext/transcode/theme.php @@ -10,8 +10,13 @@ class TranscodeImageTheme extends Themelet global $config; $html = " - ".make_form(make_link("transcode/{$image->id}"), 'POST', false, "", - "return transcodeSubmit()")." + ".make_form( + make_link("transcode/{$image->id}"), + 'POST', + false, + "", + "return transcodeSubmit()" + )." ".$this->get_transcode_picker_html($options)." diff --git a/ext/trash/main.php b/ext/trash/main.php index e8732c64..a7a8dde4 100644 --- a/ext/trash/main.php +++ b/ext/trash/main.php @@ -14,7 +14,6 @@ abstract class TrashConfig class Trash extends Extension { - protected $db_support = [DatabaseDriver::MYSQL, DatabaseDriver::PGSQL]; public function get_priority(): int @@ -59,7 +58,7 @@ class Trash extends Extension { global $user, $page; - if($event->image->trash===true && !$user->can(Permissions::VIEW_TRASH)) { + if ($event->image->trash===true && !$user->can(Permissions::VIEW_TRASH)) { $page->set_mode(PageMode::REDIRECT); $page->set_redirect(make_link("post/list")); } @@ -67,7 +66,7 @@ class Trash extends Extension public function onImageDeletion(ImageDeletionEvent $event) { - if($event->force!==true && $event->image->trash!==true) { + if ($event->force!==true && $event->image->trash!==true) { self::set_trash($event->image->id, true); $event->stop_processing = true; } @@ -87,7 +86,7 @@ class Trash extends Extension if (preg_match(self::SEARCH_REGEXP, strtolower($event->term), $matches)) { - if($user->can(Permissions::VIEW_TRASH)) { + if ($user->can(Permissions::VIEW_TRASH)) { $event->add_querylet(new Querylet($database->scoreql_to_sql("trash = SCORE_BOOL_Y "))); } } @@ -96,8 +95,8 @@ class Trash extends Extension public function onHelpPageBuilding(HelpPageBuildingEvent $event) { global $user; - if($event->key===HelpPages::SEARCH) { - if($user->can(Permissions::VIEW_TRASH)) { + if ($event->key===HelpPages::SEARCH) { + if ($user->can(Permissions::VIEW_TRASH)) { $block = new Block(); $block->header = "Trash"; $block->body = $this->theme->get_help_html(); @@ -117,18 +116,19 @@ class Trash extends Extension return true; } - public static function set_trash($image_id, $trash) { + public static function set_trash($image_id, $trash) + { global $database; - $database->execute("UPDATE images SET trash = :trash WHERE id = :id", - ["trash"=>$database->scoresql_value_prepare($trash),"id"=>$image_id]); - - + $database->execute( + "UPDATE images SET trash = :trash WHERE id = :id", + ["trash"=>$database->scoresql_value_prepare($trash),"id"=>$image_id] + ); } public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event) { global $config, $database, $user; - if($event->image->trash===true && $user->can(Permissions::VIEW_TRASH)) { + if ($event->image->trash===true && $user->can(Permissions::VIEW_TRASH)) { $event->add_part($this->theme->get_image_admin_html($event->image->id)); } } @@ -138,7 +138,7 @@ class Trash extends Extension global $user; if ($user->can(Permissions::VIEW_TRASH)&&in_array("in:trash", $event->search_terms)) { - $event->add_action("bulk_trash_restore","(U)ndelete", "u"); + $event->add_action("bulk_trash_restore", "(U)ndelete", "u"); } } @@ -172,7 +172,5 @@ class Trash extends Extension $database->Execute("CREATE INDEX images_trash_idx ON images(trash)"); $config->set_int(TrashConfig::VERSION, 1); } - } - } diff --git a/ext/trash/theme.php b/ext/trash/theme.php index 3211aa1f..49c3fd15 100644 --- a/ext/trash/theme.php +++ b/ext/trash/theme.php @@ -2,7 +2,8 @@ class TrashTheme extends Themelet { - function get_image_admin_html(int $image_id) { + public function get_image_admin_html(int $image_id) + { $html = " ".make_form(make_link('trash_restore/'.$image_id), 'POST')." @@ -10,7 +11,8 @@ class TrashTheme extends Themelet "; - return $html; } + return $html; + } public function get_help_html() @@ -21,6 +23,5 @@ class TrashTheme extends Themelet

Returns images that are in the trash.

'; - } } diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php index c818a526..0ee743f8 100644 --- a/ext/upgrade/main.php +++ b/ext/upgrade/main.php @@ -187,7 +187,7 @@ class Upgrade extends Extension $database->execute("ALTER TABLE images ADD COLUMN length INTEGER NULL "); log_info("upgrade", "Setting indexes for media columns"); - switch($database->get_driver_name()) { + switch ($database->get_driver_name()) { case DatabaseDriver::PGSQL: case DatabaseDriver::SQLITE: $database->execute('CREATE INDEX images_video_idx ON images(video) WHERE video IS NOT NULL'); @@ -223,7 +223,6 @@ class Upgrade extends Extension log_info("upgrade", "Database at version 17"); $config->set_bool("in_upgrade", false); } - } public function get_priority(): int diff --git a/ext/upload/main.php b/ext/upload/main.php index 8c9d313f..2fd330d1 100644 --- a/ext/upload/main.php +++ b/ext/upload/main.php @@ -141,12 +141,12 @@ class Upload extends Extension public function onPageNavBuilding(PageNavBuildingEvent $event) { - $event->add_nav_link("upload",new Link('upload'), "Upload"); + $event->add_nav_link("upload", new Link('upload'), "Upload"); } public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="upload") { + if ($event->parent=="upload") { if (class_exists("Wiki")) { $event->add_nav_link("upload_guidelines", new Link('wiki/upload_guidelines'), "Guidelines"); } diff --git a/ext/user/main.php b/ext/user/main.php index d6a59f8d..5ef42f64 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -341,7 +341,7 @@ class UserPage extends Extension public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { global $user; - if($event->parent==="system") { + if ($event->parent==="system") { if ($user->can(Permissions::EDIT_USER_CLASS)) { $event->add_nav_link("user_admin", new Link('user_admin/list'), "User List", NavLink::is_active(["user_admin"])); } @@ -388,7 +388,7 @@ class UserPage extends Extension public function onHelpPageBuilding(HelpPageBuildingEvent $event) { - if($event->key===HelpPages::SEARCH) { + if ($event->key===HelpPages::SEARCH) { $block = new Block(); $block->header = "Users"; $block->body = $this->theme->get_help_html(); diff --git a/ext/user/theme.php b/ext/user/theme.php index a1650db4..fbed0d82 100644 --- a/ext/user/theme.php +++ b/ext/user/theme.php @@ -242,7 +242,6 @@ class UserPageTheme extends Themelet $page->set_heading(html_escape($duser->name)."'s Page"); $page->add_block(new NavBlock()); $page->add_block(new Block("Stats", join("
", $stats), "main", 10)); - } public function build_options(User $duser, UserOptionsBuildingEvent $event) @@ -337,7 +336,6 @@ class UserPageTheme extends Themelet foreach ($event->parts as $part) { $html .= $part; } - } return $html; } diff --git a/ext/user_config/main.php b/ext/user_config/main.php index ed728101..e5d06992 100644 --- a/ext/user_config/main.php +++ b/ext/user_config/main.php @@ -27,12 +27,13 @@ class UserConfig extends Extension { global $config; - if ($config->get_int(self::VERSION,0)<1) { + if ($config->get_int(self::VERSION, 0)<1) { $this->install(); } } - public function onInitUserConfig(InitUserConfigEvent $event) { + public function onInitUserConfig(InitUserConfigEvent $event) + { global $database, $user_config; $user_config = new DatabaseConfig($database, "user_config", "user_id", $event->user->id); @@ -43,8 +44,7 @@ class UserConfig extends Extension { global $config, $database; - if ($config->get_int(self::VERSION,0) < 1) { - + if ($config->get_int(self::VERSION, 0) < 1) { log_info("upgrade", "Adding user config table"); $database->create_table("user_config", " diff --git a/ext/view/events/displaying_image_event.php b/ext/view/events/displaying_image_event.php index 3ac3090f..38bc3a45 100644 --- a/ext/view/events/displaying_image_event.php +++ b/ext/view/events/displaying_image_event.php @@ -17,7 +17,8 @@ class DisplayingImageEvent extends Event return $this->image; } - public function set_title(String $title) { + public function set_title(String $title) + { $this->title = $title; } -} \ No newline at end of file +} diff --git a/ext/view/events/image_admin_block_building_event.php b/ext/view/events/image_admin_block_building_event.php index 0211065a..971fe97e 100644 --- a/ext/view/events/image_admin_block_building_event.php +++ b/ext/view/events/image_admin_block_building_event.php @@ -22,4 +22,4 @@ class ImageAdminBlockBuildingEvent extends Event } $this->parts[$position] = $html; } -} \ No newline at end of file +} diff --git a/ext/view/events/image_info_box_building_event.php b/ext/view/events/image_info_box_building_event.php index cb626349..61577490 100644 --- a/ext/view/events/image_info_box_building_event.php +++ b/ext/view/events/image_info_box_building_event.php @@ -22,4 +22,4 @@ class ImageInfoBoxBuildingEvent extends Event } $this->parts[$position] = $html; } -} \ No newline at end of file +} diff --git a/ext/view/events/image_info_set_event.php b/ext/view/events/image_info_set_event.php index e6d77d59..a870f328 100644 --- a/ext/view/events/image_info_set_event.php +++ b/ext/view/events/image_info_set_event.php @@ -9,4 +9,4 @@ class ImageInfoSetEvent extends Event { $this->image = $image; } -} \ No newline at end of file +} diff --git a/ext/wiki/main.php b/ext/wiki/main.php index 2bb04b0a..ac6bb9da 100644 --- a/ext/wiki/main.php +++ b/ext/wiki/main.php @@ -179,13 +179,13 @@ class Wiki extends Extension public function onPageNavBuilding(PageNavBuildingEvent $event) { - $event->add_nav_link("wiki",new Link('wiki'), "Wiki"); + $event->add_nav_link("wiki", new Link('wiki'), "Wiki"); } public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) { - if($event->parent=="wiki") { + if ($event->parent=="wiki") { $event->add_nav_link("wiki_rules", new Link('wiki/rules'), "Rules"); $event->add_nav_link("wiki_help", new Link('ext_doc/wiki'), "Help"); } diff --git a/index.php b/index.php index 67855129..7b28355e 100644 --- a/index.php +++ b/index.php @@ -100,7 +100,7 @@ try { $page->display(); } - if($database->transaction===true) { + if ($database->transaction===true) { $database->commit(); } @@ -117,7 +117,7 @@ try { $_tracer->end(); if (TRACE_FILE) { - if((microtime(true) - $_shm_load_start) > TRACE_THRESHOLD) { - $_tracer->flush(TRACE_FILE); - } + if ((microtime(true) - $_shm_load_start) > TRACE_THRESHOLD) { + $_tracer->flush(TRACE_FILE); + } } diff --git a/themes/danbooru/layout.class.php b/themes/danbooru/layout.class.php index 6be406a4..68c315f1 100644 --- a/themes/danbooru/layout.class.php +++ b/themes/danbooru/layout.class.php @@ -101,7 +101,7 @@ class Layout } $custom_sublinks = ""; - if(!empty($sub_links)) { + if (!empty($sub_links)) { $custom_sublinks = "
"; foreach ($sub_links as $nav_link) { $custom_sublinks .= "
  • ".$this->navlinks($nav_link->link, $nav_link->description, $nav_link->active)."
  • "; diff --git a/themes/danbooru/view.theme.php b/themes/danbooru/view.theme.php index 0886f297..79d5c9e5 100644 --- a/themes/danbooru/view.theme.php +++ b/themes/danbooru/view.theme.php @@ -33,7 +33,7 @@ class CustomViewImageTheme extends ViewImageTheme
    Filesize: $h_filesize
    Type: $h_type"; - if($image->length!=null) { + if ($image->length!=null) { $h_length = format_milliseconds($image->length); $html .= "
    Length: $h_length"; } diff --git a/themes/danbooru2/layout.class.php b/themes/danbooru2/layout.class.php index 440d0c88..e5c9c3e5 100644 --- a/themes/danbooru2/layout.class.php +++ b/themes/danbooru2/layout.class.php @@ -101,7 +101,7 @@ class Layout } $custom_sublinks = ""; - if(!empty($sub_links)) { + if (!empty($sub_links)) { $custom_sublinks = "
    "; foreach ($sub_links as $nav_link) { $custom_sublinks .= "
  • ".$this->navlinks($nav_link->link, $nav_link->description, $nav_link->active)."
  • "; diff --git a/themes/danbooru2/view.theme.php b/themes/danbooru2/view.theme.php index ff9c4ce0..93e442f5 100644 --- a/themes/danbooru2/view.theme.php +++ b/themes/danbooru2/view.theme.php @@ -33,7 +33,7 @@ class CustomViewImageTheme extends ViewImageTheme
    Type: $h_type "; - if($image->length!=null) { + if ($image->length!=null) { $h_length = format_milliseconds($image->length); $html .= "
    Length: $h_length"; } diff --git a/themes/lite/layout.class.php b/themes/lite/layout.class.php index f11cbe70..ac011d57 100644 --- a/themes/lite/layout.class.php +++ b/themes/lite/layout.class.php @@ -57,7 +57,7 @@ class Layout } $custom_sublinks = ""; - if(!empty($sub_links)) { + if (!empty($sub_links)) { $custom_sublinks = "
    "; foreach ($sub_links as $nav_link) { $custom_sublinks .= $this->navlinks($nav_link->link, $nav_link->description, $nav_link->active); diff --git a/themes/lite/view.theme.php b/themes/lite/view.theme.php index 91767252..7bd0cf59 100644 --- a/themes/lite/view.theme.php +++ b/themes/lite/view.theme.php @@ -33,7 +33,7 @@ class CustomViewImageTheme extends ViewImageTheme
    Filesize: $h_filesize
    Type: ".$h_type." "; - if($image->length!=null) { + if ($image->length!=null) { $h_length = format_milliseconds($image->length); $html .= "
    Length: $h_length"; }