2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2009-08-20 22:37:17 +00:00
|
|
|
/*
|
2007-07-08 20:21:21 +00:00
|
|
|
* Name: Image Ratings
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
2012-02-08 02:52:11 +00:00
|
|
|
* Link: http://code.shishnet.org/shimmie2/
|
2007-07-08 20:21:21 +00:00
|
|
|
* License: GPLv2
|
2009-01-16 08:18:41 +00:00
|
|
|
* Description: Allow users to rate images "safe", "questionable" or "explicit"
|
2013-11-05 22:32:27 +00:00
|
|
|
* Documentation:
|
|
|
|
* This shimmie extension provides filter:
|
|
|
|
* <ul>
|
|
|
|
* <li>rating = (safe|questionable|explicit|unknown)
|
|
|
|
* <ul>
|
|
|
|
* <li>rating=s -- safe images
|
|
|
|
* <li>rating=q -- questionable images
|
|
|
|
* <li>rating=e -- explicit images
|
|
|
|
* <li>rating=u -- Unknown rating
|
|
|
|
* <li>rating=sq -- safe and questionable images
|
|
|
|
* </ul>
|
|
|
|
* </ul>
|
2007-07-08 20:21:21 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class RatingSetEvent extends Event
|
|
|
|
{
|
2019-05-28 19:27:23 +00:00
|
|
|
/** @var Image */
|
2019-05-28 16:59:38 +00:00
|
|
|
public $image;
|
|
|
|
/** @var string */
|
|
|
|
public $rating;
|
2007-10-18 02:04:22 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function __construct(Image $image, string $rating)
|
|
|
|
{
|
|
|
|
assert(in_array($rating, ["s", "q", "e", "u"]));
|
2014-04-28 07:26:35 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->image = $image;
|
|
|
|
$this->rating = $rating;
|
|
|
|
}
|
2007-10-18 02:04:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class Ratings extends Extension
|
|
|
|
{
|
2019-06-20 15:42:32 +00:00
|
|
|
protected $db_support = [DatabaseDriver::MYSQL, DatabaseDriver::PGSQL];
|
2007-10-02 21:59:20 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 50;
|
|
|
|
}
|
2012-01-27 18:16:46 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
if ($config->get_int("ext_ratings2_version") < 2) {
|
|
|
|
$this->install();
|
|
|
|
}
|
2012-02-09 05:40:01 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
|
|
|
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';
|
2012-02-09 05:40:01 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb = new SetupBlock("Image Ratings");
|
|
|
|
$sb->add_choice_option("ext_rating_anon_privs", $privs, "Anonymous: ");
|
|
|
|
$sb->add_choice_option("ext_rating_user_privs", $privs, "<br>Users: ");
|
|
|
|
$sb->add_choice_option("ext_rating_admin_privs", $privs, "<br>Admins: ");
|
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
|
|
|
|
2019-06-05 23:03:22 +00:00
|
|
|
// public function onPostListBuilding(PostListBuildingEvent $event)
|
|
|
|
// {
|
|
|
|
// global $user;
|
|
|
|
// if ($user->is_admin() && !empty($event->search_terms)) {
|
|
|
|
// $this->theme->display_bulk_rater(Tag::implode($event->search_terms));
|
|
|
|
// }
|
|
|
|
// }
|
2012-03-19 20:16:40 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event)
|
|
|
|
{
|
|
|
|
global $user, $page;
|
|
|
|
/**
|
|
|
|
* 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)) {
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("post/list"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onRatingSet(RatingSetEvent $event)
|
|
|
|
{
|
|
|
|
if (empty($event->image->rating)) {
|
|
|
|
$old_rating = "";
|
|
|
|
} else {
|
|
|
|
$old_rating = $event->image->rating;
|
|
|
|
}
|
|
|
|
$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"])) {
|
|
|
|
$rating = $_POST["rating"];
|
|
|
|
if (Ratings::rating_is_valid($rating)) {
|
|
|
|
send_event(new RatingSetEvent($event->image, $rating));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-02 21:59:20 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onParseLinkTemplate(ParseLinkTemplateEvent $event)
|
|
|
|
{
|
|
|
|
$event->replace('$rating', $this->rating_to_human($event->image->rating));
|
|
|
|
}
|
2012-02-09 05:40:01 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onSearchTermParse(SearchTermParseEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
|
|
|
|
$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)) {
|
|
|
|
$ratings = $matches[1] ? $matches[1] : $matches[2][0];
|
|
|
|
$ratings = array_intersect(str_split($ratings), str_split(Ratings::get_user_privs($user)));
|
|
|
|
$set = "'" . join("', '", $ratings) . "'";
|
|
|
|
$event->add_querylet(new Querylet("rating IN ($set)"));
|
|
|
|
}
|
|
|
|
}
|
2009-08-02 07:19:43 +00:00
|
|
|
|
2019-06-11 14:59:06 +00:00
|
|
|
public function onTagTermParse(TagTermParseEvent $event)
|
|
|
|
{
|
|
|
|
$matches = [];
|
|
|
|
|
|
|
|
if (preg_match("/^rating[=|:](?:([sqeu]+)|(safe|questionable|explicit|unknown))$/D", 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)));
|
|
|
|
|
|
|
|
$rating = $ratings[0];
|
|
|
|
|
|
|
|
$image = Image::by_id($event->id);
|
|
|
|
|
|
|
|
$re = new RatingSetEvent($image, $rating);
|
|
|
|
|
|
|
|
send_event($re);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($matches)) {
|
|
|
|
$event->metatag = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 23:03:22 +00:00
|
|
|
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
|
2019-06-27 03:41:42 +00:00
|
|
|
if ($user->can("bulk_edit_image_rating")) {
|
|
|
|
$event->add_action("bulk_rate","Set (R)ating", "r","",$this->theme->get_selection_rater_html("u","bulk_rating"));
|
2019-06-05 23:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onBulkAction(BulkActionEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
|
2019-06-14 12:47:50 +00:00
|
|
|
switch ($event->action) {
|
2019-06-12 22:44:25 +00:00
|
|
|
case "bulk_rate":
|
2019-06-05 23:03:22 +00:00
|
|
|
if (!isset($_POST['bulk_rating'])) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-27 03:41:42 +00:00
|
|
|
if ($user->can("bulk_edit_image_rating")) {
|
2019-06-05 23:03:22 +00:00
|
|
|
$rating = $_POST['bulk_rating'];
|
2019-06-12 22:44:25 +00:00
|
|
|
$total = 0;
|
|
|
|
foreach ($event->items as $id) {
|
|
|
|
$image = Image::by_id($id);
|
2019-06-14 12:47:50 +00:00
|
|
|
if ($image==null) {
|
2019-06-12 22:44:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-06-20 04:37:33 +00:00
|
|
|
|
2019-06-05 23:03:22 +00:00
|
|
|
send_event(new RatingSetEvent($image, $rating));
|
2019-06-14 12:47:50 +00:00
|
|
|
$total++;
|
2019-06-05 23:03:22 +00:00
|
|
|
}
|
2019-06-12 22:44:25 +00:00
|
|
|
flash_message("Rating set for $total items");
|
2019-06-05 23:03:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
|
|
|
global $user, $page;
|
|
|
|
|
|
|
|
if ($event->page_matches("admin/bulk_rate")) {
|
2019-06-27 03:41:42 +00:00
|
|
|
if (!$user->can("bulk_edit_image_rating")) {
|
2019-05-28 16:59:38 +00:00
|
|
|
throw new PermissionDeniedException();
|
|
|
|
} else {
|
|
|
|
$n = 0;
|
|
|
|
while (true) {
|
|
|
|
$images = Image::find_images($n, 100, Tag::explode($_POST["query"]));
|
|
|
|
if (count($images) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
reset($images); // rewind to first element in array.
|
|
|
|
|
|
|
|
foreach ($images as $image) {
|
|
|
|
send_event(new RatingSetEvent($image, $_POST['rating']));
|
|
|
|
}
|
|
|
|
$n += 100;
|
|
|
|
}
|
|
|
|
#$database->execute("
|
|
|
|
# update images set rating=? where images.id in (
|
|
|
|
# select image_id from image_tags join tags
|
|
|
|
# on image_tags.tag_id = tags.id where tags.tag = ?);
|
|
|
|
# ", array($_POST["rating"], $_POST["tag"]));
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("post/list"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-28 07:26:35 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public static function get_user_privs(User $user): string
|
|
|
|
{
|
|
|
|
global $config;
|
2014-04-28 07:26:35 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
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;
|
|
|
|
}
|
2009-11-15 05:45:50 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public static function privs_to_sql(string $sqes): string
|
|
|
|
{
|
|
|
|
$arr = [];
|
|
|
|
$length = strlen($sqes);
|
|
|
|
for ($i=0; $i<$length; $i++) {
|
|
|
|
$arr[] = "'" . $sqes[$i] . "'";
|
|
|
|
}
|
|
|
|
$set = join(', ', $arr);
|
|
|
|
return $set;
|
|
|
|
}
|
2009-11-15 05:45:50 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
2010-01-03 08:55:43 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public static function rating_is_valid(string $rating): bool
|
|
|
|
{
|
|
|
|
switch ($rating) {
|
|
|
|
case "s":
|
|
|
|
case "q":
|
|
|
|
case "e":
|
|
|
|
case "u":
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-09-25 19:17:29 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* FIXME: this is a bit ugly and guessey, should have proper options
|
|
|
|
*/
|
|
|
|
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") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-13 19:10:48 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* #param string[] $context
|
|
|
|
*/
|
|
|
|
private function no_rating_query(array $context): bool
|
|
|
|
{
|
|
|
|
foreach ($context as $term) {
|
|
|
|
if (preg_match("/^rating[=|:]/", $term)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
private function install()
|
|
|
|
{
|
|
|
|
global $database, $config;
|
2007-10-02 22:29:32 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($config->get_int("ext_ratings2_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);
|
|
|
|
}
|
2007-10-02 22:29:32 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($config->get_int("ext_ratings2_version") < 2) {
|
|
|
|
$database->Execute("CREATE INDEX images__rating ON images(rating)");
|
|
|
|
$config->set_int("ext_ratings2_version", 2);
|
|
|
|
}
|
2009-01-22 07:21:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($config->get_int("ext_ratings2_version") < 3) {
|
2019-06-11 14:59:06 +00:00
|
|
|
$database->Execute("UPDATE images SET rating = 'u' WHERE rating is null");
|
2019-06-14 12:47:50 +00:00
|
|
|
switch ($database->get_driver_name()) {
|
2019-06-20 15:42:32 +00:00
|
|
|
case DatabaseDriver::MYSQL:
|
2019-06-11 14:59:06 +00:00
|
|
|
$database->Execute("ALTER TABLE images CHANGE rating rating CHAR(1) NOT NULL DEFAULT 'u'");
|
|
|
|
break;
|
2019-06-20 15:42:32 +00:00
|
|
|
case DatabaseDriver::PGSQL:
|
2019-06-11 14:59:06 +00:00
|
|
|
$database->Execute("ALTER TABLE images ALTER COLUMN rating SET DEFAULT 'u'");
|
|
|
|
$database->Execute("ALTER TABLE images ALTER COLUMN rating SET NOT NULL");
|
|
|
|
break;
|
|
|
|
}
|
2019-06-14 12:47:50 +00:00
|
|
|
$config->set_int("ext_ratings2_version", 3);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
private function set_rating(int $image_id, string $rating, string $old_rating)
|
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
if ($old_rating != $rating) {
|
|
|
|
$database->Execute("UPDATE images SET rating=? WHERE id=?", [$rating, $image_id]);
|
|
|
|
log_info("rating", "Rating for Image #{$image_id} set to: ".$this->rating_to_human($rating));
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|