Skip to content

Commit

Permalink
add settings to disable post-specific tags and ratings (to prevent ov…
Browse files Browse the repository at this point in the history
…erriding tag-based ratings
  • Loading branch information
Giraffaman committed Apr 13, 2024
1 parent 2f69aa9 commit 43384fc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
14 changes: 12 additions & 2 deletions ext/rating/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,12 @@ public function onRatingSet(RatingSetEvent $event): void
{
if (empty($event->image['rating'])) {
$old_rating = "";
error_log("ratings: no old rating.");
} else {
$old_rating = $event->image['rating'];
error_log("ratings: old rating is ".$old_rating);
}
error_log("ratings: calling set_rating for image ".$event->image->id.", new rating ".$event->rating.", old rating ".$old_rating);
$this->set_rating($event->image->id, $event->rating, $old_rating);
}

Expand Down Expand Up @@ -323,6 +326,7 @@ public function onTagTermCheck(TagTermCheckEvent $event): void
{
if (preg_match($this->search_regexp, $event->term)) {
$event->metatag = true;
error_log("ratings: ".$event->term." is a ratings tag!");
}
}

Expand All @@ -336,12 +340,14 @@ public function onTagTermParse(TagTermParseEvent $event): void

if (count($matches) > 2 && in_array($matches[2], self::UNRATED_KEYWORDS)) {
$ratings = "?";
error_log("ratings: don't know what rating ".$event->term." is.");
}

$ratings = array_intersect(str_split($ratings), Ratings::get_user_class_privs($user));
$rating = $ratings[0];
$image = Image::by_id_ex($event->image_id);
send_event(new RatingSetEvent($image, $rating));
error_log("ratings: ".$event->term." is rating ".$rating);
}
}

Expand Down Expand Up @@ -524,7 +530,10 @@ public function onUploadHeaderBuilding(UploadHeaderBuildingEvent $event): void

public function onUploadSpecificBuilding(UploadSpecificBuildingEvent $event): void
{
$event->add_part($this->theme->get_upload_specific_rater_html($event->suffix));
global $config;
if($config->get_bool(UploadConfig::ALLOW_SPECIFIC_RATINGS)) {
$event->add_part($this->theme->get_upload_specific_rater_html($event->suffix));
}
}

/**
Expand Down Expand Up @@ -692,7 +701,8 @@ private function set_rating(int $image_id, string $rating, string $old_rating):
global $database;
if ($old_rating != $rating) {
$database->execute("UPDATE images SET rating=:rating WHERE id=:id", ['rating' => $rating, 'id' => $image_id]);
log_info("rating", "Rating for >>{$image_id} set to: ".$this->rating_to_human($rating));
#log_info("rating", "Rating for >>{$image_id} set to: ".$this->rating_to_human($rating));
error_log("Rating for >>{$image_id} set to: ".$this->rating_to_human($rating));
}
}
}
2 changes: 2 additions & 0 deletions ext/upload/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class UploadConfig
{
public const COUNT = "upload_count";
public const FORM_COUNT = "form_count";
public const ALLOW_SPECIFIC_TAGS = "allow_specific_tags";
public const ALLOW_SPECIFIC_RATINGS = "allow_specific_ratings";
public const SIZE = "upload_size";
public const MIN_FREE_SPACE = "upload_min_free_space";
public const TLSOURCE = "upload_tlsource";
Expand Down
6 changes: 6 additions & 0 deletions ext/upload/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public function onInitExt(InitExtEvent $event): void
global $config;
$config->set_default_int(UploadConfig::COUNT, 3);
$config->set_default_int(UploadConfig::FORM_COUNT, 5);
$config->set_default_bool(UploadConfig::ALLOW_SPECIFIC_TAGS, true);
# disabling post-specific ratings selector since we set ratings through tags and this would override it
# e.g. if tag "nsfw" (rating=e) present but this is left "unrated", image ends up with rating=?
$config->set_default_bool(UploadConfig::ALLOW_SPECIFIC_RATINGS, false);
$config->set_default_int(UploadConfig::SIZE, parse_shorthand_int('1MB'));
$config->set_default_int(UploadConfig::MIN_FREE_SPACE, parse_shorthand_int('100MB'));
$config->set_default_bool(UploadConfig::TLSOURCE, true);
Expand Down Expand Up @@ -158,6 +162,8 @@ public function onSetupBuilding(SetupBuildingEvent $event): void
$sb->add_label("<i>PHP Limit = " . ini_get('upload_max_filesize') . "</i>");
$sb->add_choice_option(UploadConfig::TRANSLOAD_ENGINE, $tes, "<br/>Transload: ");
$sb->add_bool_option(UploadConfig::TLSOURCE, "<br/>Use transloaded URL as source if none is provided: ");
$sb->add_bool_option(UploadConfig::ALLOW_SPECIFIC_TAGS, "<br/>Support post-specific tags:");
$sb->add_bool_option(UploadConfig::ALLOW_SPECIFIC_RATINGS, "<br/>Support post-specific tags:");

$sb->start_table();
$sb->add_bool_option(UploadConfig::MIME_CHECK_ENABLED, "Enable upload MIME checks", true);
Expand Down
9 changes: 6 additions & 3 deletions ext/upload/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ protected function build_upload_list(): HTMLElement
}
for ($i = 0; $i < $form_count; $i++) {
$specific_fields = emptyHTML();
$usfbe = send_event(new UploadSpecificBuildingEvent((string)$i));
foreach ($usfbe->get_parts() as $part) {
$specific_fields->appendChild($part);
if($config->get_bool(UploadConfig::ALLOW_SPECIFIC_TAGS)) {
$usfbe = send_event(new UploadSpecificBuildingEvent((string)$i));

foreach ($usfbe->get_parts() as $part) {
$specific_fields->appendChild($part);
}
}

$upload_list->appendChild(
Expand Down

0 comments on commit 43384fc

Please sign in to comment.