This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/image_view_counter/main.php

132 lines
3.7 KiB
PHP
Raw Normal View History

<?php
/**
* Name: Image View Counter
* Author: Drudex Software <support@drudexsoftware.com>
* Link: http://www.drudexsoftware.com/
* License: GPLv2
* Description: Tracks & displays how many times an image is viewed
* Documentation:
* Whenever anyone views an image, a view will be added to that image.
* This extension will also track any username & the IP adress.
* This is done to prevent duplicate views.
* A person can only count as a view again 1 hour after viewing the image initially.
*/
2016-06-18 18:26:56 +00:00
class ImageViewCounter extends Extension {
private $view_interval = 3600; # allows views to be added each hour
2016-06-18 18:26:56 +00:00
# Add Setup Block with options for view counter
public function onSetupBuilding(SetupBuildingEvent $event) {
$sb = new SetupBlock("Image View Counter");
$sb->add_bool_option("image_viewcounter_adminonly", "Display view counter only to admin");
2016-06-18 18:26:56 +00:00
$event->panel->add_block($sb);
}
2016-06-18 18:26:56 +00:00
# Adds view to database if needed
public function onDisplayingImage(DisplayingImageEvent $event) {
$imgid = $event->image->id; // determines image id
$this->addview($imgid); // adds a view
}
2016-06-18 18:26:56 +00:00
# display views to user or admin below image if allowed
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) {
global $user, $config;
2016-06-18 18:26:56 +00:00
$adminonly = $config->get_bool("image_viewcounter_adminonly"); // todo
if ($adminonly == false || ($adminonly && $user->is_admin()))
$event->add_part(
"<tr><th>Views:</th><td>".
$this->get_view_count($event->image->id) .
2016-09-03 18:24:51 +00:00
"</tr>", 38);
2016-06-18 18:26:56 +00:00
}
# Installs DB table
public function onInitExt(InitExtEvent $event) {
global $database, $config;
// if the sql table doesn't exist yet, create it
if($config->get_bool("image_viewcounter_installed") == false) { //todo
$database->create_table("image_views","
id SCORE_AIPK,
image_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
timestamp INTEGER NOT NULL,
ipaddress SCORE_INET NOT NULL");
$config->set_bool("image_viewcounter_installed", true);
}
}
/**
* Adds a view to the item if needed
*/
2019-05-28 16:31:20 +00:00
private function addview(int $imgid)
2016-06-18 18:26:56 +00:00
{
global $database, $user;
// don't add view if person already viewed recently
if ($this->can_add_view($imgid) == false) 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)
",
array(
"image_id" => $imgid,
"user_id" => $user->id,
"timestamp" => time(),
"ipaddress" => $_SERVER['REMOTE_ADDR'],
)
);
}
/**
* Returns true if this IP hasn't recently viewed this image
*/
2019-05-28 16:31:20 +00:00
private function can_add_view(int $imgid)
2016-06-18 18:26:56 +00:00
{
global $database;
// 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
",
array(
"ipaddress" => $_SERVER['REMOTE_ADDR'],
"lasthour" => time() - $this->view_interval,
"image_id" => $imgid
)
);
// if no views were found with the set criteria, return true
if($recent_from_ip == 0) return true;
else return false;
}
/**
* Returns the int of the view count from the given image id
*/
2019-05-28 16:31:20 +00:00
private function get_view_count(int $imgid = 0)
2016-06-18 18:26:56 +00:00
{
global $database;
if ($imgid == 0) // return view count of all images
$view_count = (int)$database->get_one(
"SELECT COUNT(*) FROM image_views"
);
else // return view count of specified image
$view_count = (int)$database->get_one(
"SELECT COUNT(*) FROM image_views WHERE image_id =:image_id",
array("image_id" => $imgid)
);
// returns the count as int
return $view_count;
}
}