2013-02-23 05:21:00 +00:00
|
|
|
<?php
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class ImageViewCounter extends Extension
|
|
|
|
{
|
|
|
|
private $view_interval = 3600; # allows views to be added each hour
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +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");
|
2013-02-23 05:21:00 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +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
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +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
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$adminonly = $config->get_bool("image_viewcounter_adminonly"); // todo
|
2019-09-29 18:00:51 +00:00
|
|
|
if ($adminonly == false || ($adminonly && $user->can(Permissions::SEE_IMAGE_VIEW_COUNTS))) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_part(
|
|
|
|
"<tr><th>Views:</th><td>".
|
|
|
|
$this->get_view_count($event->image->id) .
|
|
|
|
"</tr>",
|
|
|
|
38
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
# Installs DB table
|
2019-11-03 17:19:37 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database, $config;
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// if the sql table doesn't exist yet, create it
|
|
|
|
if ($config->get_bool("image_viewcounter_installed") == false) { //todo
|
|
|
|
$database->create_table("image_views", "
|
2016-06-18 18:26:56 +00:00
|
|
|
id SCORE_AIPK,
|
|
|
|
image_id INTEGER NOT NULL,
|
|
|
|
user_id INTEGER NOT NULL,
|
|
|
|
timestamp INTEGER NOT NULL,
|
|
|
|
ipaddress SCORE_INET NOT NULL");
|
2019-05-28 16:59:38 +00:00
|
|
|
$config->set_bool("image_viewcounter_installed", true);
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Adds a view to the item if needed
|
|
|
|
*/
|
|
|
|
private function addview(int $imgid)
|
|
|
|
{
|
|
|
|
global $database, $user;
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// don't add view if person already viewed recently
|
2019-10-02 09:10:47 +00:00
|
|
|
if ($this->can_add_view($imgid) === false) {
|
2019-05-28 16:59:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// Add view for current IP
|
|
|
|
$database->execute(
|
|
|
|
"
|
2016-06-18 18:26:56 +00:00
|
|
|
INSERT INTO image_views (image_id, user_id, timestamp, ipaddress)
|
|
|
|
VALUES (:image_id, :user_id, :timestamp, :ipaddress)
|
|
|
|
",
|
2019-05-28 16:59:38 +00:00
|
|
|
[
|
|
|
|
"image_id" => $imgid,
|
|
|
|
"user_id" => $user->id,
|
|
|
|
"timestamp" => time(),
|
|
|
|
"ipaddress" => $_SERVER['REMOTE_ADDR'],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Returns true if this IP hasn't recently viewed this image
|
|
|
|
*/
|
|
|
|
private function can_add_view(int $imgid)
|
|
|
|
{
|
|
|
|
global $database;
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// counts views from current IP in the last hour
|
|
|
|
$recent_from_ip = (int)$database->get_one(
|
|
|
|
"
|
2016-06-18 18:26:56 +00:00
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM image_views
|
|
|
|
WHERE ipaddress=:ipaddress AND timestamp >:lasthour AND image_id =:image_id
|
|
|
|
",
|
2019-05-28 16:59:38 +00:00
|
|
|
[
|
|
|
|
"ipaddress" => $_SERVER['REMOTE_ADDR'],
|
|
|
|
"lasthour" => time() - $this->view_interval,
|
|
|
|
"image_id" => $imgid
|
|
|
|
]
|
|
|
|
);
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// if no views were found with the set criteria, return true
|
|
|
|
if ($recent_from_ip == 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Returns the int of the view count from the given image id
|
|
|
|
*/
|
|
|
|
private function get_view_count(int $imgid = 0)
|
|
|
|
{
|
|
|
|
global $database;
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
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",
|
|
|
|
["image_id" => $imgid]
|
|
|
|
);
|
|
|
|
}
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// returns the count as int
|
|
|
|
return $view_count;
|
|
|
|
}
|
2013-02-23 05:21:00 +00:00
|
|
|
}
|