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 {
|
|
|
|
var $image_id, $user, $rating;
|
|
|
|
|
|
|
|
public function RatingSetEvent($image_id, $user, $rating) {
|
|
|
|
$this->image_id = $image_id;
|
|
|
|
$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) {
|
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
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof InitExtEvent) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $config;
|
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
|
|
|
|
|
|
|
global $config;
|
|
|
|
$config->set_default_string("ext_rating_anon_privs", 'sq');
|
|
|
|
$config->set_default_string("ext_rating_user_privs", 'sq');
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof RatingSetEvent) {
|
2007-10-18 02:04:22 +00:00
|
|
|
$this->set_rating($event->image_id, $event->rating);
|
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof ImageInfoBoxBuildingEvent) {
|
2008-03-24 04:03:34 +00:00
|
|
|
global $user;
|
|
|
|
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
|
|
|
global $user;
|
|
|
|
if($user->is_admin()) {
|
2008-03-24 04:03:34 +00:00
|
|
|
send_event(new RatingSetEvent($event->image_id, $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';
|
|
|
|
$privs['Safe and Questionable'] = 'sq';
|
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: ");
|
|
|
|
$sb->add_choice_option("ext_rating_user_privs", $privs, "<br>Logged in: ");
|
|
|
|
$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)) {
|
|
|
|
global $user, $config;
|
|
|
|
if($user->is_anonymous()) {
|
|
|
|
$sqes = $config->get_string("ext_rating_anon_privs");
|
|
|
|
}
|
|
|
|
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)"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function no_rating_query($context) {
|
|
|
|
foreach($context as $term) {
|
2009-01-22 07:21:56 +00:00
|
|
|
if(preg_match("/^rating=([sqeu]+)$/", $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());
|
|
|
|
?>
|