2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
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) {
|
|
|
|
$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-07-19 00:29:48 +00:00
|
|
|
$config->set_default_string("ext_rating_user_privs", 'squ');
|
|
|
|
$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) {
|
2008-03-24 04:03:34 +00:00
|
|
|
if($user->is_admin()) {
|
|
|
|
$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) {
|
2007-10-02 21:59:20 +00:00
|
|
|
if($user->is_admin()) {
|
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)) {
|
|
|
|
if($user->is_anonymous()) {
|
|
|
|
$sqes = $config->get_string("ext_rating_anon_privs");
|
|
|
|
}
|
2009-07-19 00:29:48 +00:00
|
|
|
else if($user->is_admin()) {
|
|
|
|
$sqes = $config->get_string("ext_rating_admin_privs");
|
|
|
|
}
|
2008-10-17 20:18:38 +00:00
|
|
|
else {
|
|
|
|
$sqes = $config->get_string("ext_rating_user_privs");
|
|
|
|
}
|
|
|
|
$arr = array();
|
|
|
|
for($i=0; $i<strlen($sqes); $i++) {
|
|
|
|
$arr[] = "'" . $sqes[$i] . "'";
|
|
|
|
}
|
|
|
|
$set = join(', ', $arr);
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
?>
|