panel->create_new_block("Post View Counter"); $sb->add_bool_option("image_viewcounter_adminonly", "Display view counter only to admin"); } # Adds view to database if needed public function onDisplayingImage(DisplayingImageEvent $event) { global $database, $user; $imgid = $event->image->id; // counts views from current IP in the last hour $recent_from_ip = (int)$database->get_one( " SELECT COUNT(*) FROM image_views WHERE ipaddress=:ipaddress AND timestamp >:lasthour AND image_id =:image_id ", [ "ipaddress" => $_SERVER['REMOTE_ADDR'], "lasthour" => time() - $this->view_interval, "image_id" => $imgid ] ); // don't add view if person already viewed recently if ($recent_from_ip > 0) { return; } // Add view for current IP $database->execute( " INSERT INTO image_views (image_id, user_id, timestamp, ipaddress) VALUES (:image_id, :user_id, :timestamp, :ipaddress) ", [ "image_id" => $imgid, "user_id" => $user->id, "timestamp" => time(), "ipaddress" => $_SERVER['REMOTE_ADDR'], ] ); } public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) { global $user, $database; if ($user->can(Permissions::SEE_IMAGE_VIEW_COUNTS)) { $view_count = (int)$database->get_one( "SELECT COUNT(*) FROM image_views WHERE image_id =:image_id", ["image_id" => $event->image->id] ); $event->add_part( "