Bug fixes and consolidation of various thumbnail and resize functionality Changed resize/rotate extensions to use replace image event Added content-disposition header to image responses to provide a human-friendly filename when saving Added more bulk thumbnail regeneration tools Tweaks to bulk actions to correct totals when batching items
		
			
				
	
	
		
			318 lines
		
	
	
		
			9.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			318 lines
		
	
	
		
			9.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
/*
 | 
						|
 * Name: Image Ratings
 | 
						|
 * Author: Shish <webmaster@shishnet.org>
 | 
						|
 * Link: http://code.shishnet.org/shimmie2/
 | 
						|
 * License: GPLv2
 | 
						|
 * Description: Allow users to rate images "safe", "questionable" or "explicit"
 | 
						|
 * 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>
 | 
						|
 */
 | 
						|
 | 
						|
class RatingSetEvent extends Event
 | 
						|
{
 | 
						|
    /** @var Image */
 | 
						|
    public $image;
 | 
						|
    /** @var string  */
 | 
						|
    public $rating;
 | 
						|
 | 
						|
    public function __construct(Image $image, string $rating)
 | 
						|
    {
 | 
						|
        assert(in_array($rating, ["s", "q", "e", "u"]));
 | 
						|
 | 
						|
        $this->image = $image;
 | 
						|
        $this->rating = $rating;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
class Ratings extends Extension
 | 
						|
{
 | 
						|
    protected $db_support = ['mysql','pgsql'];  // ?
 | 
						|
 | 
						|
    public function get_priority(): int
 | 
						|
    {
 | 
						|
        return 50;
 | 
						|
    }
 | 
						|
 | 
						|
    public function onInitExt(InitExtEvent $event)
 | 
						|
    {
 | 
						|
        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 = [];
 | 
						|
        $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);
 | 
						|
    }
 | 
						|
    
 | 
						|
    // 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));
 | 
						|
    //     }
 | 
						|
    // }
 | 
						|
 | 
						|
    
 | 
						|
    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)) {
 | 
						|
            $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)
 | 
						|
    {
 | 
						|
        $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));
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public function onParseLinkTemplate(ParseLinkTemplateEvent $event)
 | 
						|
    {
 | 
						|
        $event->replace('$rating', $this->rating_to_human($event->image->rating));
 | 
						|
    }
 | 
						|
 | 
						|
    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)"));
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event)
 | 
						|
    {
 | 
						|
        global $user;
 | 
						|
 | 
						|
        if ($user->is_admin()) {
 | 
						|
            $event->add_action("Set Rating","",$this->theme->get_selection_rater_html("bulk_rating"));
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    public function onBulkAction(BulkActionEvent $event)
 | 
						|
    {
 | 
						|
        global $user;
 | 
						|
 | 
						|
        switch($event->action) {
 | 
						|
            case "Set Rating":
 | 
						|
                if (!isset($_POST['bulk_rating'])) {
 | 
						|
                    return;
 | 
						|
                }
 | 
						|
                if ($user->is_admin()) {
 | 
						|
                    $rating = $_POST['bulk_rating'];
 | 
						|
                    foreach ($event->items as $image) {
 | 
						|
                        send_event(new RatingSetEvent($image, $rating));
 | 
						|
						$event->running_total++;
 | 
						|
                    }
 | 
						|
                }
 | 
						|
                break;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public function onPageRequest(PageRequestEvent $event)
 | 
						|
    {
 | 
						|
        global $user, $page;
 | 
						|
        
 | 
						|
        if ($event->page_matches("admin/bulk_rate")) {
 | 
						|
            if (!$user->is_admin()) {
 | 
						|
                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"]));
 | 
						|
                $page->set_mode("redirect");
 | 
						|
                $page->set_redirect(make_link("post/list"));
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public static function get_user_privs(User $user): string
 | 
						|
    {
 | 
						|
        global $config;
 | 
						|
 | 
						|
        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;
 | 
						|
    }
 | 
						|
 | 
						|
    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;
 | 
						|
    }
 | 
						|
 | 
						|
    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";
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public static function rating_is_valid(string $rating): bool
 | 
						|
    {
 | 
						|
        switch ($rating) {
 | 
						|
            case "s":
 | 
						|
            case "q":
 | 
						|
            case "e":
 | 
						|
            case "u":
 | 
						|
                return true;
 | 
						|
            default:
 | 
						|
                return false;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 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;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * #param string[] $context
 | 
						|
     */
 | 
						|
    private function no_rating_query(array $context): bool
 | 
						|
    {
 | 
						|
        foreach ($context as $term) {
 | 
						|
            if (preg_match("/^rating[=|:]/", $term)) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    private function install()
 | 
						|
    {
 | 
						|
        global $database, $config;
 | 
						|
 | 
						|
        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);
 | 
						|
        }
 | 
						|
 | 
						|
        if ($config->get_int("ext_ratings2_version") < 2) {
 | 
						|
            $database->Execute("CREATE INDEX images__rating ON images(rating)");
 | 
						|
            $config->set_int("ext_ratings2_version", 2);
 | 
						|
        }
 | 
						|
 | 
						|
        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);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    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));
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |