Merge branch 'custom_ratings' into develop
This commit is contained in:
commit
e6a402cd4e
16 changed files with 444 additions and 130 deletions
|
@ -302,6 +302,9 @@ class Image
|
||||||
["tag"=>$tags[0]]
|
["tag"=>$tags[0]]
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
if (ext_is_live("Ratings")) {
|
||||||
|
$tags[] = "rating:*";
|
||||||
|
}
|
||||||
list($tag_conditions, $img_conditions) = self::terms_to_conditions($tags);
|
list($tag_conditions, $img_conditions) = self::terms_to_conditions($tags);
|
||||||
$total = Image::get_accelerated_count($tag_conditions, $img_conditions);
|
$total = Image::get_accelerated_count($tag_conditions, $img_conditions);
|
||||||
if (is_null($total)) {
|
if (is_null($total)) {
|
||||||
|
|
|
@ -803,7 +803,6 @@ function iterator_map_to_array(callable $callback, iterator $iter): array
|
||||||
return iterator_to_array(iterator_map($callback, $iter));
|
return iterator_to_array(iterator_map($callback, $iter));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function get_class_from_file(string $file): string
|
function get_class_from_file(string $file): string
|
||||||
{
|
{
|
||||||
$fp = fopen($file, 'r');
|
$fp = fopen($file, 'r');
|
||||||
|
|
|
@ -405,7 +405,7 @@ class CommentList extends Extension
|
||||||
$image = Image::by_id($row["image_id"]);
|
$image = Image::by_id($row["image_id"]);
|
||||||
if (
|
if (
|
||||||
Extension::is_enabled(RatingsInfo::KEY) && !is_null($image) &&
|
Extension::is_enabled(RatingsInfo::KEY) && !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
|
$image = null; // this is "clever", I may live to regret it
|
||||||
}
|
}
|
||||||
|
|
|
@ -200,7 +200,7 @@ class DanbooruApi extends Extension
|
||||||
"preview_url" => $img->get_thumb_link(),
|
"preview_url" => $img->get_thumb_link(),
|
||||||
"preview_height" => $previewsize[1],
|
"preview_height" => $previewsize[1],
|
||||||
"preview_width" => $previewsize[0],
|
"preview_width" => $previewsize[0],
|
||||||
"rating" => "u",
|
"rating" => "?",
|
||||||
"date" => $img->posted,
|
"date" => $img->posted,
|
||||||
"is_warehoused" => false,
|
"is_warehoused" => false,
|
||||||
"tags" => $taglist,
|
"tags" => $taglist,
|
||||||
|
|
|
@ -55,7 +55,7 @@ class Featured extends Extension
|
||||||
}
|
}
|
||||||
if (!is_null($image)) {
|
if (!is_null($image)) {
|
||||||
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
||||||
if (strpos(Ratings::get_user_privs($user), $image->rating) === false) {
|
if (!in_array($image->rating, Ratings::get_user_class_privs($user))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -798,7 +798,7 @@ class Pools extends Extension
|
||||||
// WE CHECK IF THE EXTENSION RATING IS INSTALLED, WHICH VERSION AND IF IT
|
// 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
|
// WORKS TO SHOW/HIDE SAFE, QUESTIONABLE, EXPLICIT AND UNRATED IMAGES FROM USER
|
||||||
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
||||||
$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 (Extension::is_enabled(TrashInfo::KEY)) {
|
if (Extension::is_enabled(TrashInfo::KEY)) {
|
||||||
$query .= $database->scoreql_to_sql(" AND trash = SCORE_BOOL_N ");
|
$query .= $database->scoreql_to_sql(" AND trash = SCORE_BOOL_N ");
|
||||||
|
|
|
@ -1,5 +1,80 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @global ImageRating[] $_shm_ratings
|
||||||
|
*/
|
||||||
|
global $_shm_ratings;
|
||||||
|
$_shm_ratings = [];
|
||||||
|
|
||||||
|
class ImageRating
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @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)
|
||||||
|
{
|
||||||
|
if (strlen($code)!=1) {
|
||||||
|
throw new Exception("Rating code must be exactly one character");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->name = $name;
|
||||||
|
$this->code = $code;
|
||||||
|
$this->search_term = $search_term;
|
||||||
|
$this->order = $order;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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(ImageRating $rating)
|
||||||
|
{
|
||||||
|
global $_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)) {
|
||||||
|
throw new Exception("$rating->search_term is a reserved search term");
|
||||||
|
}
|
||||||
|
|
||||||
|
$_shm_ratings[$rating->code] = $rating;
|
||||||
|
}
|
||||||
|
|
||||||
|
add_rating(new ImageRating("?", "Unrated", "unrated", 99999));
|
||||||
|
|
||||||
|
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
|
class RatingSetEvent extends Event
|
||||||
{
|
{
|
||||||
/** @var Image */
|
/** @var Image */
|
||||||
|
@ -9,15 +84,45 @@ class RatingSetEvent extends Event
|
||||||
|
|
||||||
public function __construct(Image $image, string $rating)
|
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->image = $image;
|
||||||
$this->rating = $rating;
|
$this->rating = $rating;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract class RatingsConfig
|
||||||
|
{
|
||||||
|
const VERSION = "ext_ratings2_version";
|
||||||
|
const USER_DEFAULTS = "ratings_default";
|
||||||
|
}
|
||||||
|
|
||||||
class Ratings extends Extension
|
class Ratings extends Extension
|
||||||
{
|
{
|
||||||
|
protected $db_support = [DatabaseDriver::MYSQL, DatabaseDriver::PGSQL];
|
||||||
|
|
||||||
|
public const UNRATED_KEYWORDS = ["unknown","unrated"];
|
||||||
|
|
||||||
|
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) . "|".implode("|", self::UNRATED_KEYWORDS)."))$/D";
|
||||||
|
}
|
||||||
|
|
||||||
public function get_priority(): int
|
public function get_priority(): int
|
||||||
{
|
{
|
||||||
return 50;
|
return 50;
|
||||||
|
@ -25,30 +130,59 @@ class Ratings extends Extension
|
||||||
|
|
||||||
public function onInitExt(InitExtEvent $event)
|
public function onInitExt(InitExtEvent $event)
|
||||||
{
|
{
|
||||||
global $config;
|
global $user, $config, $_shm_user_classes, $_shm_ratings;
|
||||||
|
|
||||||
if ($config->get_int("ext_ratings2_version") < 2) {
|
if ($config->get_int(RatingsConfig::VERSION) < 4) {
|
||||||
$this->install();
|
$this->install();
|
||||||
}
|
}
|
||||||
|
|
||||||
$config->set_default_string("ext_rating_anon_privs", 'squ');
|
foreach (array_keys($_shm_user_classes) as $key) {
|
||||||
$config->set_default_string("ext_rating_user_privs", 'sqeu');
|
if ($key == "base" || $key == "hellbanned") {
|
||||||
$config->set_default_string("ext_rating_admin_privs", 'sqeu');
|
continue;
|
||||||
|
}
|
||||||
|
$config->set_default_array("ext_rating_" . $key . "_privs", array_keys($_shm_ratings));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
$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)
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
||||||
{
|
{
|
||||||
$privs = [];
|
global $config, $_shm_user_classes, $_shm_ratings;
|
||||||
$privs['Safe Only'] = 's';
|
|
||||||
$privs['Safe and Unknown'] = 'su';
|
$ratings = self::get_sorted_ratings();
|
||||||
$privs['Safe and Questionable'] = 'sq';
|
|
||||||
$privs['Safe, Questionable, Unknown'] = 'squ';
|
$options = [];
|
||||||
$privs['All'] = 'sqeu';
|
foreach ($ratings as $key => $rating) {
|
||||||
|
$options[$rating->name] = $rating->code;
|
||||||
|
}
|
||||||
|
|
||||||
$sb = new SetupBlock("Image Ratings");
|
$sb = new SetupBlock("Image Ratings");
|
||||||
$sb->add_choice_option("ext_rating_anon_privs", $privs, "Anonymous: ");
|
$sb->start_table();
|
||||||
$sb->add_choice_option("ext_rating_user_privs", $privs, "<br>Users: ");
|
foreach (array_keys($_shm_user_classes) as $key) {
|
||||||
$sb->add_choice_option("ext_rating_admin_privs", $privs, "<br>Admins: ");
|
if ($key == "base" || $key == "hellbanned") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$sb->add_multichoice_option("ext_rating_" . $key . "_privs", $options, $key, true);
|
||||||
|
}
|
||||||
|
$sb->end_table();
|
||||||
|
|
||||||
$event->panel->add_block($sb);
|
$event->panel->add_block($sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,8 +201,7 @@ class Ratings extends Extension
|
||||||
/**
|
/**
|
||||||
* Deny images upon insufficient permissions.
|
* Deny images upon insufficient permissions.
|
||||||
**/
|
**/
|
||||||
$user_view_level = Ratings::get_user_privs($user);
|
$user_view_level = Ratings::get_user_class_privs($user);
|
||||||
$user_view_level = preg_split('//', $user_view_level, -1);
|
|
||||||
if (!in_array($event->image->rating, $user_view_level)) {
|
if (!in_array($event->image->rating, $user_view_level)) {
|
||||||
$page->set_mode(PageMode::REDIRECT);
|
$page->set_mode(PageMode::REDIRECT);
|
||||||
$page->set_redirect(make_link("post/list"));
|
$page->set_redirect(make_link("post/list"));
|
||||||
|
@ -126,12 +259,24 @@ class Ratings extends Extension
|
||||||
|
|
||||||
$matches = [];
|
$matches = [];
|
||||||
if (is_null($event->term) && $this->no_rating_query($event->context)) {
|
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)"));
|
$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 = $matches[1] ? $matches[1] : $matches[2][0];
|
||||||
$ratings = array_intersect(str_split($ratings), str_split(Ratings::get_user_privs($user)));
|
|
||||||
|
if (count($matches)>2&&in_array($matches[2], self::UNRATED_KEYWORDS)) {
|
||||||
|
$ratings = "?";
|
||||||
|
}
|
||||||
|
|
||||||
|
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) . "'";
|
$set = "'" . join("', '", $ratings) . "'";
|
||||||
$event->add_querylet(new Querylet("rating IN ($set)"));
|
$event->add_querylet(new Querylet("rating IN ($set)"));
|
||||||
}
|
}
|
||||||
|
@ -139,11 +284,17 @@ class Ratings extends Extension
|
||||||
|
|
||||||
public function onTagTermParse(TagTermParseEvent $event)
|
public function onTagTermParse(TagTermParseEvent $event)
|
||||||
{
|
{
|
||||||
|
global $user;
|
||||||
$matches = [];
|
$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 = $matches[1] ? $matches[1] : $matches[2][0];
|
||||||
$ratings = array_intersect(str_split($ratings), str_split(Ratings::get_user_privs($user)));
|
|
||||||
|
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];
|
$rating = $ratings[0];
|
||||||
|
|
||||||
|
@ -159,12 +310,56 @@ class Ratings extends Extension
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event)
|
||||||
{
|
{
|
||||||
global $user;
|
global $user;
|
||||||
|
|
||||||
if ($user->can(Permissions::BULK_EDIT_IMAGE_RATING)) {
|
if ($user->can(Permissions::BULK_EDIT_IMAGE_RATING)) {
|
||||||
$event->add_action("bulk_rate", "Set (R)ating", "r", "", $this->theme->get_selection_rater_html("u", "bulk_rating"));
|
$event->add_action("bulk_rate", "Set (R)ating", "r", "", $this->theme->get_selection_rater_html(["?"]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,11 +369,11 @@ class Ratings extends Extension
|
||||||
|
|
||||||
switch ($event->action) {
|
switch ($event->action) {
|
||||||
case "bulk_rate":
|
case "bulk_rate":
|
||||||
if (!isset($_POST['bulk_rating'])) {
|
if (!isset($_POST['rating'])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ($user->can(Permissions::BULK_EDIT_IMAGE_RATING)) {
|
if ($user->can(Permissions::BULK_EDIT_IMAGE_RATING)) {
|
||||||
$rating = $_POST['bulk_rating'];
|
$rating = $_POST['rating'];
|
||||||
$total = 0;
|
$total = 0;
|
||||||
foreach ($event->items as $image) {
|
foreach ($event->items as $image) {
|
||||||
send_event(new RatingSetEvent($image, $rating));
|
send_event(new RatingSetEvent($image, $rating));
|
||||||
|
@ -192,7 +387,7 @@ class Ratings extends Extension
|
||||||
|
|
||||||
public function onPageRequest(PageRequestEvent $event)
|
public function onPageRequest(PageRequestEvent $event)
|
||||||
{
|
{
|
||||||
global $user, $page;
|
global $user, $page, $user_config;
|
||||||
|
|
||||||
if ($event->page_matches("admin/bulk_rate")) {
|
if ($event->page_matches("admin/bulk_rate")) {
|
||||||
if (!$user->can(Permissions::BULK_EDIT_IMAGE_RATING)) {
|
if (!$user->can(Permissions::BULK_EDIT_IMAGE_RATING)) {
|
||||||
|
@ -221,28 +416,72 @@ class Ratings extends Extension
|
||||||
$page->set_redirect(make_link("post/list"));
|
$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): string
|
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;
|
global $config;
|
||||||
|
|
||||||
if ($user->is_anonymous()) {
|
return $config->get_array("ext_rating_".$user->class->name."_privs");
|
||||||
$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 ?? "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function privs_to_sql(string $sqes): string
|
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 = [];
|
$arr = [];
|
||||||
$length = strlen($sqes);
|
foreach ($privs as $i) {
|
||||||
for ($i=0; $i<$length; $i++) {
|
$arr[] = "'" . $i . "'";
|
||||||
$arr[] = "'" . $sqes[$i] . "'";
|
}
|
||||||
|
if (sizeof($arr)==0) {
|
||||||
|
return "' '";
|
||||||
}
|
}
|
||||||
$set = join(', ', $arr);
|
$set = join(', ', $arr);
|
||||||
return $set;
|
return $set;
|
||||||
|
@ -250,25 +489,19 @@ class Ratings extends Extension
|
||||||
|
|
||||||
public static function rating_to_human(string $rating): string
|
public static function rating_to_human(string $rating): string
|
||||||
{
|
{
|
||||||
switch ($rating) {
|
global $_shm_ratings;
|
||||||
case "s": return "Safe";
|
|
||||||
case "q": return "Questionable";
|
if (array_key_exists($rating, $_shm_ratings)) {
|
||||||
case "e": return "Explicit";
|
return $_shm_ratings[$rating]->name;
|
||||||
default: return "Unknown";
|
|
||||||
}
|
}
|
||||||
|
return "Unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function rating_is_valid(string $rating): bool
|
public static function rating_is_valid(string $rating): bool
|
||||||
{
|
{
|
||||||
switch ($rating) {
|
global $_shm_ratings;
|
||||||
case "s":
|
|
||||||
case "q":
|
return in_array($rating, array_keys($_shm_ratings));
|
||||||
case "e":
|
|
||||||
case "u":
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -277,13 +510,7 @@ class Ratings extends Extension
|
||||||
private function can_rate(): bool
|
private function can_rate(): bool
|
||||||
{
|
{
|
||||||
global $config, $user;
|
global $config, $user;
|
||||||
if ($user->is_anonymous() && $config->get_string("ext_rating_anon_privs") == "sqeu") {
|
if ($user->can("edit_image_rating")) {
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if ($user->is_admin()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!$user->is_anonymous() && $config->get_string("ext_rating_user_privs") == "sqeu") {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -306,18 +533,18 @@ class Ratings extends Extension
|
||||||
{
|
{
|
||||||
global $database, $config;
|
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("ALTER TABLE images ADD COLUMN rating CHAR(1) NOT NULL DEFAULT '?'");
|
||||||
$database->Execute("CREATE INDEX images__rating ON images(rating)");
|
$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)");
|
$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");
|
$database->Execute("UPDATE images SET rating = 'u' WHERE rating is null");
|
||||||
switch ($database->get_driver_name()) {
|
switch ($database->get_driver_name()) {
|
||||||
case DatabaseDriver::MYSQL:
|
case DatabaseDriver::MYSQL:
|
||||||
|
@ -328,7 +555,40 @@ class Ratings extends Extension
|
||||||
$database->Execute("ALTER TABLE images ALTER COLUMN rating SET NOT NULL");
|
$database->Execute("ALTER TABLE images ALTER COLUMN rating SET NOT NULL");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$config->set_int("ext_ratings2_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)) {
|
||||||
|
$config->set_array("ext_rating_anonymous_privs", str_split($value));
|
||||||
|
}
|
||||||
|
$value = $config->get_string("ext_rating_user_privs");
|
||||||
|
if (!empty($value)) {
|
||||||
|
$config->set_array("ext_rating_user_privs", str_split($value));
|
||||||
|
}
|
||||||
|
$value = $config->get_string("ext_rating_admin_privs");
|
||||||
|
if (!empty($value)) {
|
||||||
|
$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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,6 @@ class RatingsTheme extends Themelet
|
||||||
{
|
{
|
||||||
public function get_rater_html(int $image_id, string $rating, bool $can_rate): string
|
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);
|
$human_rating = Ratings::rating_to_human($rating);
|
||||||
$html = "
|
$html = "
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -15,9 +12,7 @@ class RatingsTheme extends Themelet
|
||||||
".($can_rate ? "
|
".($can_rate ? "
|
||||||
<span class='view'>$human_rating</span>
|
<span class='view'>$human_rating</span>
|
||||||
<span class='edit'>
|
<span class='edit'>
|
||||||
<input type='radio' name='rating' value='s' id='s'$s_checked><label for='s'>Safe</label>
|
".$this->get_selection_rater_html([$rating])."
|
||||||
<input type='radio' name='rating' value='q' id='q'$q_checked><label for='q'>Questionable</label>
|
|
||||||
<input type='radio' name='rating' value='e' id='e'$e_checked><label for='e'>Explicit</label>
|
|
||||||
</span>
|
</span>
|
||||||
" : "
|
" : "
|
||||||
$human_rating
|
$human_rating
|
||||||
|
@ -28,32 +23,65 @@ class RatingsTheme extends Themelet
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function display_bulk_rater(string $terms)
|
|
||||||
|
public function display_form(array $current_ratings, array $available_ratings)
|
||||||
{
|
{
|
||||||
global $page;
|
global $page, $database;
|
||||||
$html = "
|
|
||||||
".make_form(make_link("admin/bulk_rate"))."
|
$html = make_form(make_link("admin/update_ratings"))."<table class='form'><tr>
|
||||||
<input type='hidden' name='query' value='".html_escape($terms)."'>
|
<th>Change</th><td><select name='rating_old' required='required'><option></option>";
|
||||||
<select name='rating'>
|
foreach ($current_ratings as $key=>$value) {
|
||||||
<option value='s'>Safe</option>
|
$html .= "<option value='$key'>$value</option>";
|
||||||
<option value='q'>Questionable</option>
|
}
|
||||||
<option value='e'>Explicit</option>
|
$html .= "</select></td></tr>
|
||||||
<option value='u'>Unrated</option>
|
<tr><th>To</th><td><select name='rating_new' required='required'><option></option>";
|
||||||
</select>
|
foreach ($available_ratings as $value) {
|
||||||
<input type='submit' value='Go'>
|
$html .= "<option value='$value->code'>$value->name</option>";
|
||||||
</form>
|
}
|
||||||
";
|
$html .= "</select></td></tr>
|
||||||
$page->add_block(new Block("List Controls", $html, "left"));
|
<tr><td colspan='2'><input type='submit' value='Update'></td></tr></table>
|
||||||
|
</form>\n";
|
||||||
|
$page->add_block(new Block("Update Ratings", $html));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_selection_rater_html(String $id = "select_rating")
|
|
||||||
|
|
||||||
|
// public function display_bulk_rater(string $terms)
|
||||||
|
// {
|
||||||
|
// global $page;
|
||||||
|
// $html = "
|
||||||
|
// ".make_form(make_link("admin/bulk_rate"))."
|
||||||
|
// <input type='hidden' name='query' value='".html_escape($terms)."'>
|
||||||
|
// <select name='rating'>
|
||||||
|
// <option value='s'>Safe</option>
|
||||||
|
// <option value='q'>Questionable</option>
|
||||||
|
// <option value='e'>Explicit</option>
|
||||||
|
// <option value='u'>Unrated</option>
|
||||||
|
// </select>
|
||||||
|
// <input type='submit' value='Go'>
|
||||||
|
// </form>
|
||||||
|
// ";
|
||||||
|
// $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)
|
||||||
{
|
{
|
||||||
return "<select name='".$id."'>
|
global $_shm_ratings;
|
||||||
<option value='s'>Safe</option>
|
|
||||||
<option value='q'>Questionable</option>
|
$output = "<select name='rating".($multiple ? "[]' multiple='multiple'" : "' ")." >";
|
||||||
<option value='e'>Explicit</option>
|
|
||||||
<option value='u'>Unrated</option>
|
$options = Ratings::get_sorted_ratings();
|
||||||
</select>";
|
|
||||||
|
foreach ($options as $option) {
|
||||||
|
if ($available_options!=null && !in_array($option->code, $available_options)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$output .= "<option value='".$option->code."' ".
|
||||||
|
(in_array($option->code, $selected_options) ? "selected='selected'": "")
|
||||||
|
.">".$option->name."</option>";
|
||||||
|
}
|
||||||
|
return $output."</select>";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_help_html(array $ratings)
|
public function get_help_html(array $ratings)
|
||||||
|
@ -83,4 +111,28 @@ class RatingsTheme extends Themelet
|
||||||
$output .= "</table>";
|
$output .= "</table>";
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function get_user_options(User $user, array $selected_ratings, array $available_ratings): string
|
||||||
|
{
|
||||||
|
$html = "
|
||||||
|
<p>".make_form(make_link("user_admin/default_ratings"))."
|
||||||
|
<input type='hidden' name='id' value='$user->id'>
|
||||||
|
<table style='width: 300px;'>
|
||||||
|
<thead>
|
||||||
|
<tr><th colspan='2'>Default Rating Filter</th></tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr><td>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.</td></tr>
|
||||||
|
<tr><td>
|
||||||
|
".$this->get_selection_rater_html($selected_ratings, true, $available_ratings)."
|
||||||
|
</td></tr>
|
||||||
|
</tbody>
|
||||||
|
<tfoot>
|
||||||
|
<tr><td><input type='submit' value='Save'></td></tr>
|
||||||
|
</tfoot>
|
||||||
|
</table>
|
||||||
|
</form>
|
||||||
|
";
|
||||||
|
return $html;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -602,12 +602,10 @@ class TagList extends Extension
|
||||||
$starting_tags = [];
|
$starting_tags = [];
|
||||||
$tags_ok = true;
|
$tags_ok = true;
|
||||||
foreach ($wild_tags as $tag) {
|
foreach ($wild_tags as $tag) {
|
||||||
if ($tag[0] == "-") {
|
if ($tag[0] == "-" || strpos($tag, "tagme")===0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$tag = Tag::sqlify($tag);
|
$tag = Tag::sqlify($tag);
|
||||||
|
|
||||||
$tag_ids = $database->get_col("SELECT id FROM tags WHERE tag LIKE :tag AND count < 25000", ["tag" => $tag]);
|
$tag_ids = $database->get_col("SELECT id FROM tags WHERE tag LIKE :tag AND count < 25000", ["tag" => $tag]);
|
||||||
// $search_tags = array_merge($search_tags,
|
// $search_tags = array_merge($search_tags,
|
||||||
// $database->get_col("SELECT tag FROM tags WHERE tag LIKE :tag", array("tag"=>$tag)));
|
// $database->get_col("SELECT tag FROM tags WHERE tag LIKE :tag", array("tag"=>$tag)));
|
||||||
|
|
|
@ -7,6 +7,8 @@ abstract class TrashConfig
|
||||||
|
|
||||||
class Trash extends Extension
|
class Trash extends Extension
|
||||||
{
|
{
|
||||||
|
protected $db_support = [DatabaseDriver::MYSQL, DatabaseDriver::PGSQL];
|
||||||
|
|
||||||
public function get_priority(): int
|
public function get_priority(): int
|
||||||
{
|
{
|
||||||
// Needs to be early to intercept delete events
|
// Needs to be early to intercept delete events
|
||||||
|
|
|
@ -31,12 +31,12 @@ class UserConfig extends Extension
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onUserLogin(UserLoginEvent $event)
|
public function onInitUserConfig(InitUserConfigEvent $event)
|
||||||
{
|
{
|
||||||
global $database, $user_config;
|
global $database, $user_config;
|
||||||
|
|
||||||
$user_config = new DatabaseConfig($database, "user_config", "user_id", $event->user->id);
|
$user_config = new DatabaseConfig($database, "user_config", "user_id", $event->user->id);
|
||||||
send_event(new InitUserConfigEvent($event->user, $user_config));
|
$event->user_config = $user_config;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function install(): void
|
private function install(): void
|
||||||
|
|
|
@ -88,11 +88,10 @@ require_once "core/_bootstrap.php";
|
||||||
$_tracer->begin($_SERVER["REQUEST_URI"] ?? "No Request");
|
$_tracer->begin($_SERVER["REQUEST_URI"] ?? "No Request");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
|
||||||
// start the page generation waterfall
|
// start the page generation waterfall
|
||||||
$user = _get_user();
|
$user = _get_user();
|
||||||
send_event(new UserLoginEvent($user));
|
send_event(new UserLoginEvent($user));
|
||||||
|
send_event(new InitUserConfigEvent($user));
|
||||||
if (PHP_SAPI === 'cli' || PHP_SAPI == 'phpdbg') {
|
if (PHP_SAPI === 'cli' || PHP_SAPI == 'phpdbg') {
|
||||||
send_event(new CommandEvent($argv));
|
send_event(new CommandEvent($argv));
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -134,6 +134,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase
|
||||||
$user = User::by_name('demo');
|
$user = User::by_name('demo');
|
||||||
$this->assertNotNull($user);
|
$this->assertNotNull($user);
|
||||||
send_event(new UserLoginEvent($user));
|
send_event(new UserLoginEvent($user));
|
||||||
|
send_event(new InitUserConfigEvent($user));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function log_in_as_user()
|
protected function log_in_as_user()
|
||||||
|
@ -142,6 +143,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase
|
||||||
$user = User::by_name('test');
|
$user = User::by_name('test');
|
||||||
$this->assertNotNull($user);
|
$this->assertNotNull($user);
|
||||||
send_event(new UserLoginEvent($user));
|
send_event(new UserLoginEvent($user));
|
||||||
|
send_event(new InitUserConfigEvent($user));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function log_out()
|
protected function log_out()
|
||||||
|
@ -150,6 +152,7 @@ abstract class ShimmiePHPUnitTestCase extends \PHPUnit\Framework\TestCase
|
||||||
$user = User::by_id($config->get_int("anon_id", 0));
|
$user = User::by_id($config->get_int("anon_id", 0));
|
||||||
$this->assertNotNull($user);
|
$this->assertNotNull($user);
|
||||||
send_event(new UserLoginEvent($user));
|
send_event(new UserLoginEvent($user));
|
||||||
|
send_event(new InitUserConfigEvent($user));
|
||||||
}
|
}
|
||||||
|
|
||||||
// post things
|
// post things
|
||||||
|
|
|
@ -47,8 +47,8 @@ class CustomViewImageTheme extends ViewImageTheme
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
||||||
if ($image->rating == null || $image->rating == "u") {
|
if ($image->rating == null || $image->rating == "?") {
|
||||||
$image->rating = "u";
|
$image->rating = "?";
|
||||||
}
|
}
|
||||||
$h_rating = Ratings::rating_to_human($image->rating);
|
$h_rating = Ratings::rating_to_human($image->rating);
|
||||||
$html .= "<br>Rating: $h_rating";
|
$html .= "<br>Rating: $h_rating";
|
||||||
|
|
|
@ -48,14 +48,12 @@ class CustomViewImageTheme extends ViewImageTheme
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
||||||
if ($image->rating == null || $image->rating == "u") {
|
if ($image->rating == null || $image->rating == "?") {
|
||||||
$image->rating = "u";
|
$image->rating = "?";
|
||||||
}
|
}
|
||||||
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
|
||||||
$h_rating = Ratings::rating_to_human($image->rating);
|
$h_rating = Ratings::rating_to_human($image->rating);
|
||||||
$html .= "<br>Rating: $h_rating";
|
$html .= "<br>Rating: $h_rating";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,8 +48,8 @@ class CustomViewImageTheme extends ViewImageTheme
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
||||||
if ($image->rating == null || $image->rating == "u") {
|
if ($image->rating == null || $image->rating == "?") {
|
||||||
$image->rating = "u";
|
$image->rating = "?";
|
||||||
}
|
}
|
||||||
$h_rating = Ratings::rating_to_human($image->rating);
|
$h_rating = Ratings::rating_to_human($image->rating);
|
||||||
$html .= "<br>Rating: $h_rating";
|
$html .= "<br>Rating: $h_rating";
|
||||||
|
|
Reference in a new issue