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

109 lines
3.3 KiB
PHP
Raw Normal View History

2020-01-26 13:19:35 +00:00
<?php declare(strict_types=1);
class ImageViewCounter extends Extension
{
protected $theme;
private $view_interval = 3600; # allows views to be added each hour
public function onDisplayingImage(DisplayingImageEvent $event)
{
2020-10-24 21:33:29 +00:00
global $database, $user;
2016-06-18 18:26:56 +00:00
2020-10-24 21:33:29 +00:00
$imgid = $event->image->id;
2016-06-18 18:26:56 +00:00
2020-10-24 21:33:29 +00:00
// 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
]
);
2016-06-18 18:26:56 +00:00
// don't add view if person already viewed recently
2020-10-24 21:33:29 +00:00
if ($recent_from_ip > 0) {
return;
}
2016-06-18 18:26:56 +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)
",
[
"image_id" => $imgid,
"user_id" => $user->id,
"timestamp" => time(),
"ipaddress" => $_SERVER['REMOTE_ADDR'],
]
);
}
2016-06-18 18:26:56 +00:00
2020-10-24 21:33:29 +00:00
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event)
{
2020-10-24 21:33:29 +00:00
global $user, $database;
2016-06-18 18:26:56 +00:00
2020-10-24 21:33:29 +00:00
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]
);
2016-06-18 18:26:56 +00:00
2020-10-24 21:33:29 +00:00
$event->add_part(
"<tr><th>Views:</th><td>$view_count</td></tr>",
38
);
}
}
2016-06-18 18:26:56 +00:00
2020-10-24 21:33:29 +00:00
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
{
2020-10-24 21:33:29 +00:00
global $database, $config;
2016-06-18 18:26:56 +00:00
2020-10-24 21:33:29 +00:00
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);
}
}
2020-10-24 21:33:29 +00:00
public function onPageRequest(PageRequestEvent $event)
{
2020-10-24 21:33:29 +00:00
global $database;
if ($event->page_matches("popular_images")) {
2020-10-24 21:33:29 +00:00
$sql = "
SELECT image_id, count(*) AS total_views
FROM image_views, images
WHERE image_views.image_id = image_views.image_id
AND image_views.image_id = images.id
GROUP BY image_views.image_id
ORDER BY total_views DESC
";
$result = $database->get_col($sql);
$images = [];
foreach ($result as $id) {
2020-10-24 21:33:29 +00:00
$images[] = Image::by_id(intval($id));
}
$this->theme->view_popular($images);
}
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
if ($event->parent=="posts") {
$event->add_nav_link("sort_by_visits", new Link('popular_images'), "Popular Images");
}
}
}