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>
|
|
|
|
* License: GPLv2
|
2009-01-16 08:18:41 +00:00
|
|
|
* Description: Allow users to rate images "safe", "questionable" or "explicit"
|
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 {
|
2009-08-02 07:19:43 +00:00
|
|
|
var $image, $user, $rating;
|
2007-10-18 02:04:22 +00:00
|
|
|
|
2009-08-02 07:19:43 +00:00
|
|
|
public function RatingSetEvent(Image $image, User $user, $rating) {
|
2009-08-13 19:10:48 +00:00
|
|
|
assert(in_array($rating, array("s", "q", "e", "u")));
|
2009-08-02 07:19:43 +00:00
|
|
|
$this->image = $image;
|
2007-10-18 02:04:22 +00:00
|
|
|
$this->user = $user;
|
|
|
|
$this->rating = $rating;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
class Ratings implements Extension {
|
2007-10-02 21:59:20 +00:00
|
|
|
var $theme;
|
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
public function receive_event(Event $event) {
|
2009-05-11 14:04:33 +00:00
|
|
|
global $config, $database, $page, $user;
|
2008-09-06 16:59:02 +00:00
|
|
|
if(is_null($this->theme)) $this->theme = get_theme_object($this);
|
2007-10-02 21:59:20 +00:00
|
|
|
|
2009-08-02 07:19:43 +00:00
|
|
|
if($event instanceof AdminBuildingEvent) {
|
|
|
|
$this->theme->display_bulk_rater();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(($event instanceof PageRequestEvent) && $event->page_matches("admin/bulk_rate")) {
|
|
|
|
global $database, $user, $page;
|
|
|
|
if(!$user->is_admin()) {
|
|
|
|
throw PermissionDeniedException();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$n = 0;
|
|
|
|
while(true) {
|
|
|
|
$images = Image::find_images($n, 100, Tag::explode($_POST["query"]));
|
|
|
|
if(count($images) == 0) break;
|
|
|
|
foreach($images as $image) {
|
|
|
|
send_event(new RatingSetEvent($image, $user, $_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"]));
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("admin"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof InitExtEvent) {
|
2007-10-02 23:37:04 +00:00
|
|
|
if($config->get_int("ext_ratings2_version") < 2) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->install();
|
|
|
|
}
|
2007-10-02 23:20:23 +00:00
|
|
|
|
2009-07-19 03:48:25 +00:00
|
|
|
$config->set_default_string("ext_rating_anon_privs", 'squ');
|
2009-08-13 19:10:48 +00:00
|
|
|
$config->set_default_string("ext_rating_user_privs", 'sqeu');
|
2009-07-19 00:29:48 +00:00
|
|
|
$config->set_default_string("ext_rating_admin_privs", 'sqeu');
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof RatingSetEvent) {
|
2009-08-02 07:19:43 +00:00
|
|
|
$this->set_rating($event->image->id, $event->rating);
|
2007-10-18 02:04:22 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof ImageInfoBoxBuildingEvent) {
|
2009-08-13 19:10:48 +00:00
|
|
|
if($this->can_rate()) {
|
2008-03-24 04:03:34 +00:00
|
|
|
$event->add_part($this->theme->get_rater_html($event->image->id, $event->image->rating), 80);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof ImageInfoSetEvent) {
|
2009-08-13 19:10:48 +00:00
|
|
|
if($this->can_rate() && isset($_POST["rating"])) {
|
2009-08-02 07:19:43 +00:00
|
|
|
send_event(new RatingSetEvent($event->image, $user, $_POST['rating']));
|
2007-10-02 21:59:20 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof SetupBuildingEvent) {
|
2007-10-02 23:20:23 +00:00
|
|
|
$privs = array();
|
|
|
|
$privs['Safe Only'] = 's';
|
2009-07-19 00:29:48 +00:00
|
|
|
$privs['Safe and Unknown'] = 'su';
|
2007-10-02 23:20:23 +00:00
|
|
|
$privs['Safe and Questionable'] = 'sq';
|
2009-07-19 00:29:48 +00:00
|
|
|
$privs['Safe, Questionable, Unknown'] = 'squ';
|
2009-01-22 07:21:56 +00:00
|
|
|
$privs['All'] = 'sqeu';
|
2007-10-02 23:20:23 +00:00
|
|
|
|
|
|
|
$sb = new SetupBlock("Image Ratings");
|
|
|
|
$sb->add_choice_option("ext_rating_anon_privs", $privs, "Anonymous: ");
|
2009-07-19 00:29:48 +00:00
|
|
|
$sb->add_choice_option("ext_rating_user_privs", $privs, "<br>Users: ");
|
|
|
|
$sb->add_choice_option("ext_rating_admin_privs", $privs, "<br>Admins: ");
|
2007-10-02 23:20:23 +00:00
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
2007-11-04 08:16:41 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof ParseLinkTemplateEvent) {
|
2007-11-04 08:16:41 +00:00
|
|
|
$event->replace('$rating', $this->theme->rating_to_name($event->image->rating));
|
|
|
|
}
|
2008-02-07 20:28:07 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof SearchTermParseEvent) {
|
2008-02-07 20:28:07 +00:00
|
|
|
$matches = array();
|
2008-10-17 20:18:38 +00:00
|
|
|
if(is_null($event->term) && $this->no_rating_query($event->context)) {
|
2009-11-15 05:45:50 +00:00
|
|
|
$set = Ratings::privs_to_sql(Ratings::get_user_privs($user));
|
2008-10-17 20:18:38 +00:00
|
|
|
$event->add_querylet(new Querylet("rating IN ($set)"));
|
|
|
|
}
|
2009-01-22 07:04:29 +00:00
|
|
|
if(preg_match("/^rating=([sqeu]+)$/", $event->term, $matches)) {
|
2008-02-07 20:28:07 +00:00
|
|
|
$sqes = $matches[1];
|
|
|
|
$arr = array();
|
|
|
|
for($i=0; $i<strlen($sqes); $i++) {
|
|
|
|
$arr[] = "'" . $sqes[$i] . "'";
|
|
|
|
}
|
|
|
|
$set = join(', ', $arr);
|
2008-10-17 20:18:38 +00:00
|
|
|
$event->add_querylet(new Querylet("rating IN ($set)"));
|
|
|
|
}
|
2009-08-02 07:43:00 +00:00
|
|
|
if(preg_match("/^rating=(safe|questionable|explicit|unknown)$/", strtolower($event->term), $matches)) {
|
|
|
|
$text = $matches[1];
|
|
|
|
$char = $text[0];
|
|
|
|
$event->add_querylet(new Querylet("rating = ?", array($char)));
|
|
|
|
}
|
2008-10-17 20:18:38 +00:00
|
|
|
}
|
2010-03-24 04:09:51 +00:00
|
|
|
|
|
|
|
if($event instanceof DisplayingImageEvent) {
|
|
|
|
/**
|
|
|
|
* Deny images upon insufficient permissions.
|
|
|
|
**/
|
|
|
|
global $user, $database, $page;
|
|
|
|
$user_view_level = Ratings::get_user_privs($user);
|
|
|
|
$user_view_level = preg_split('//', $user_view_level, -1);
|
|
|
|
$image_level = $database->get_row("SELECT `rating` FROM `images` WHERE id =?",$event->image->id);
|
|
|
|
$image_level = $image_level["rating"];
|
|
|
|
if(!in_array($image_level, $user_view_level)) {
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("post/list"));
|
|
|
|
}
|
|
|
|
}
|
2008-10-17 20:18:38 +00:00
|
|
|
}
|
|
|
|
|
2009-11-15 05:45:50 +00:00
|
|
|
public static function get_user_privs($user) {
|
|
|
|
global $config;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function privs_to_sql($sqes) {
|
|
|
|
$arr = array();
|
|
|
|
for($i=0; $i<strlen($sqes); $i++) {
|
|
|
|
$arr[] = "'" . $sqes[$i] . "'";
|
|
|
|
}
|
|
|
|
$set = join(', ', $arr);
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2010-01-03 08:55:43 +00:00
|
|
|
public static function rating_to_human($rating) {
|
|
|
|
switch($rating) {
|
|
|
|
case "s": return "Safe";
|
|
|
|
case "q": return "Questionable";
|
|
|
|
case "e": return "Explicit";
|
|
|
|
default: return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-13 19:10:48 +00:00
|
|
|
// FIXME: this is a bit ugly and guessey, should have proper options
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-17 20:18:38 +00:00
|
|
|
private function no_rating_query($context) {
|
|
|
|
foreach($context as $term) {
|
2009-08-02 08:24:37 +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() {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $database;
|
|
|
|
global $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
|
|
|
}
|
|
|
|
|
2007-10-02 21:59:20 +00:00
|
|
|
private function set_rating($image_id, $rating) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $database;
|
2007-10-02 21:59:20 +00:00
|
|
|
$database->Execute("UPDATE images SET rating=? WHERE id=?", array($rating, $image_id));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
add_event_listener(new Ratings());
|
|
|
|
?>
|