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
|
|
|
|
2007-10-18 02:04:22 +00:00
|
|
|
class RatingSetEvent extends Event {
|
2014-04-28 07:26:35 +00:00
|
|
|
/** @var \Image */
|
|
|
|
public $image;
|
|
|
|
/** @var string */
|
|
|
|
public $rating;
|
2007-10-18 02:04:22 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(Image $image, string $rating) {
|
2009-08-13 19:10:48 +00:00
|
|
|
assert(in_array($rating, array("s", "q", "e", "u")));
|
2014-04-28 07:26:35 +00:00
|
|
|
|
2009-08-02 07:19:43 +00:00
|
|
|
$this->image = $image;
|
2007-10-18 02:04:22 +00:00
|
|
|
$this->rating = $rating;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 05:40:01 +00:00
|
|
|
class Ratings extends Extension {
|
2015-09-20 17:37:36 +00:00
|
|
|
protected $db_support = ['mysql']; // ?
|
2007-10-02 21:59:20 +00:00
|
|
|
|
2014-04-28 07:26:35 +00:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_priority(): int {return 50;}
|
2012-01-27 18:16:46 +00:00
|
|
|
|
2012-02-12 17:08:25 +00:00
|
|
|
public function onInitExt(InitExtEvent $event) {
|
2012-02-09 05:40:01 +00:00
|
|
|
global $config;
|
|
|
|
|
|
|
|
if($config->get_int("ext_ratings2_version") < 2) {
|
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event) {
|
|
|
|
$privs = array();
|
|
|
|
$privs['Safe Only'] = 's';
|
|
|
|
$privs['Safe and Unknown'] = 'su';
|
|
|
|
$privs['Safe and Questionable'] = 'sq';
|
|
|
|
$privs['Safe, Questionable, Unknown'] = 'squ';
|
|
|
|
$privs['All'] = 'sqeu';
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2012-03-19 20:16:40 +00:00
|
|
|
public function onPostListBuilding(PostListBuildingEvent $event) {
|
|
|
|
global $user;
|
|
|
|
if($user->is_admin() && !empty($event->search_terms)) {
|
2019-02-02 12:07:33 +00:00
|
|
|
$this->theme->display_bulk_rater(Tag::implode($event->search_terms));
|
2012-03-19 20:16:40 +00:00
|
|
|
}
|
2012-02-09 05:40:01 +00:00
|
|
|
}
|
2012-03-19 20:16:40 +00:00
|
|
|
|
2012-02-09 05:40:01 +00:00
|
|
|
|
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event) {
|
2014-04-28 07:26:35 +00:00
|
|
|
global $user, $page;
|
2012-02-09 05:40:01 +00:00
|
|
|
/**
|
|
|
|
* 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"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2016-09-13 06:10:48 +00:00
|
|
|
$event->add_part($this->theme->get_rater_html($event->image->id, $event->image->rating, $this->can_rate()), 80);
|
2012-02-09 05:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onImageInfoSet(ImageInfoSetEvent $event) {
|
|
|
|
if($this->can_rate() && isset($_POST["rating"])) {
|
2016-09-25 19:17:29 +00:00
|
|
|
$rating = $_POST["rating"];
|
|
|
|
if (Ratings::rating_is_valid($rating)) {
|
|
|
|
send_event(new RatingSetEvent($event->image, $rating));
|
|
|
|
}
|
2012-02-09 05:40:01 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-02 21:59:20 +00:00
|
|
|
|
2012-02-09 05:40:01 +00:00
|
|
|
public function onParseLinkTemplate(ParseLinkTemplateEvent $event) {
|
2014-06-01 20:56:33 +00:00
|
|
|
$event->replace('$rating', $this->rating_to_human($event->image->rating));
|
2012-02-09 05:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onSearchTermParse(SearchTermParseEvent $event) {
|
|
|
|
global $user;
|
|
|
|
|
|
|
|
$matches = array();
|
|
|
|
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)"));
|
2009-08-02 07:19:43 +00:00
|
|
|
}
|
2014-01-02 14:00:24 +00:00
|
|
|
if(preg_match("/^rating[=|:](?:([sqeu]+)|(safe|questionable|explicit|unknown))$/D", strtolower($event->term), $matches)) {
|
2014-12-13 01:23:44 +00:00
|
|
|
$ratings = $matches[1] ? $matches[1] : $matches[2][0];
|
2013-11-05 22:11:17 +00:00
|
|
|
$ratings = array_intersect(str_split($ratings), str_split(Ratings::get_user_privs($user)));
|
2013-10-06 11:34:26 +00:00
|
|
|
$set = "'" . join("', '", $ratings) . "'";
|
2012-02-09 05:40:01 +00:00
|
|
|
$event->add_querylet(new Querylet("rating IN ($set)"));
|
|
|
|
}
|
|
|
|
}
|
2009-08-02 07:19:43 +00:00
|
|
|
|
2012-02-09 05:40:01 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
2014-04-28 07:26:35 +00:00
|
|
|
global $user, $page;
|
2012-02-09 05:40:01 +00:00
|
|
|
|
|
|
|
if ($event->page_matches("admin/bulk_rate")) {
|
2009-08-02 07:19:43 +00:00
|
|
|
if(!$user->is_admin()) {
|
2014-04-28 07:26:35 +00:00
|
|
|
throw new PermissionDeniedException();
|
2009-08-02 07:19:43 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$n = 0;
|
|
|
|
while(true) {
|
|
|
|
$images = Image::find_images($n, 100, Tag::explode($_POST["query"]));
|
|
|
|
if(count($images) == 0) break;
|
2012-02-04 20:35:21 +00:00
|
|
|
|
|
|
|
reset($images); // rewind to first element in array.
|
|
|
|
|
2009-08-02 07:19:43 +00:00
|
|
|
foreach($images as $image) {
|
2012-08-18 18:45:39 +00:00
|
|
|
send_event(new RatingSetEvent($image, $_POST['rating']));
|
2009-08-02 07:19:43 +00:00
|
|
|
}
|
|
|
|
$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"]));
|
|
|
|
$page->set_mode("redirect");
|
2012-03-19 20:16:40 +00:00
|
|
|
$page->set_redirect(make_link("post/list"));
|
2009-08-02 07:19:43 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-17 20:18:38 +00:00
|
|
|
}
|
2014-04-28 07:26:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \User $user
|
2015-09-20 17:37:36 +00:00
|
|
|
* @return string
|
2014-04-28 07:26:35 +00:00
|
|
|
*/
|
|
|
|
public static function get_user_privs(User $user) {
|
2009-11-15 05:45:50 +00:00
|
|
|
global $config;
|
2014-04-28 07:26:35 +00:00
|
|
|
|
2009-11-15 05:45:50 +00:00
|
|
|
if($user->is_anonymous()) {
|
|
|
|
$sqes = $config->get_string("ext_rating_anon_privs");
|
|
|
|
}
|
|
|
|
else if($user->is_admin()) {
|
|
|
|
$sqes = $config->get_string("ext_rating_admin_privs");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$sqes = $config->get_string("ext_rating_user_privs");
|
|
|
|
}
|
|
|
|
return $sqes;
|
|
|
|
}
|
|
|
|
|
2014-04-28 07:26:35 +00:00
|
|
|
/**
|
|
|
|
* @param string $sqes
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public static function privs_to_sql(string $sqes) {
|
2009-11-15 05:45:50 +00:00
|
|
|
$arr = array();
|
2012-01-16 05:07:04 +00:00
|
|
|
$length = strlen($sqes);
|
|
|
|
for($i=0; $i<$length; $i++) {
|
2009-11-15 05:45:50 +00:00
|
|
|
$arr[] = "'" . $sqes[$i] . "'";
|
|
|
|
}
|
|
|
|
$set = join(', ', $arr);
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2014-04-28 07:26:35 +00:00
|
|
|
/**
|
|
|
|
* @param string $rating
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public static function rating_to_human(string $rating) {
|
2010-01-03 08:55:43 +00:00
|
|
|
switch($rating) {
|
|
|
|
case "s": return "Safe";
|
|
|
|
case "q": return "Questionable";
|
|
|
|
case "e": return "Explicit";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 19:17:29 +00:00
|
|
|
/**
|
|
|
|
* @param string $rating
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public static function rating_is_valid(string $rating) {
|
2016-09-25 19:17:29 +00:00
|
|
|
switch($rating) {
|
|
|
|
case "s":
|
|
|
|
case "q":
|
|
|
|
case "e":
|
|
|
|
case "u":
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 07:26:35 +00:00
|
|
|
/**
|
|
|
|
* FIXME: this is a bit ugly and guessey, should have proper options
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-08-13 19:10:48 +00:00
|
|
|
private function can_rate() {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-28 07:26:35 +00:00
|
|
|
/**
|
2017-03-13 00:13:32 +00:00
|
|
|
* @param string[] $context
|
2014-04-28 07:26:35 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2008-10-17 20:18:38 +00:00
|
|
|
private function no_rating_query($context) {
|
|
|
|
foreach($context as $term) {
|
2014-01-02 14:00:24 +00:00
|
|
|
if(preg_match("/^rating[=|:]/", $term)) {
|
2008-10-17 20:18:38 +00:00
|
|
|
return false;
|
2008-02-07 20:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-17 20:18:38 +00:00
|
|
|
return true;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2007-10-02 21:59:20 +00:00
|
|
|
private function install() {
|
2014-04-28 07:26:35 +00:00
|
|
|
global $database, $config;
|
2007-10-02 22:29:32 +00:00
|
|
|
|
2007-10-02 23:37:04 +00:00
|
|
|
if($config->get_int("ext_ratings2_version") < 1) {
|
2009-01-22 07:21:56 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2007-10-02 23:37:04 +00:00
|
|
|
if($config->get_int("ext_ratings2_version") < 2) {
|
2007-10-02 22:29:32 +00:00
|
|
|
$database->Execute("CREATE INDEX images__rating ON images(rating)");
|
2007-10-02 23:37:04 +00:00
|
|
|
$config->set_int("ext_ratings2_version", 2);
|
2007-10-02 22:29:32 +00:00
|
|
|
}
|
2009-01-22 07:21:56 +00:00
|
|
|
|
|
|
|
if($config->get_int("ext_ratings2_version") < 3) {
|
|
|
|
$database->Execute("ALTER TABLE images CHANGE rating rating CHAR(1) NOT NULL DEFAULT 'u'");
|
|
|
|
$config->set_int("ext_ratings2_version", 3);
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 07:26:35 +00:00
|
|
|
/**
|
|
|
|
* @param int $image_id
|
|
|
|
* @param string $rating
|
|
|
|
* @param string $old_rating
|
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
private function set_rating(int $image_id, string $rating, string $old_rating) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $database;
|
2012-01-21 00:17:07 +00:00
|
|
|
if($old_rating != $rating){
|
|
|
|
$database->Execute("UPDATE images SET rating=? WHERE id=?", array($rating, $image_id));
|
2014-06-01 20:56:33 +00:00
|
|
|
log_info("rating", "Rating for Image #{$image_id} set to: ".$this->rating_to_human($rating));
|
2012-01-21 00:17:07 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-26 02:54:51 +00:00
|
|
|
|