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

118 lines
3.5 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
use function MicroHTML\{TD,TH,TR};
class ImageViewCounter extends Extension
{
2023-02-03 20:03:04 +00:00
/** @var ImageViewCounterTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
private int $view_interval = 3600; # allows views to be added each hour
# Add Setup Block with options for view counter
public function onSetupBuilding(SetupBuildingEvent $event)
{
$sb = $event->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)
{
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" => get_real_ip(),
2020-10-24 21:33:29 +00:00
"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" => get_real_ip(),
]
);
}
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 = (string)$database->get_one(
2020-10-24 21:33:29 +00:00
"SELECT COUNT(*) FROM image_views WHERE image_id =:image_id",
["image_id" => $event->image->id]
);
2016-06-18 18:26:56 +00:00
$event->add_part(SHM_POST_INFO("Views", $view_count), 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")) {
$popular_ids = $database->get_col("
2020-10-24 21:33:29 +00:00
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
LIMIT 100
");
$images = Search::get_images($popular_ids);
$this->theme->view_popular($images);
}
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
2023-11-11 21:49:12 +00:00
if ($event->parent == "posts") {
2020-10-26 15:13:28 +00:00
$event->add_nav_link("sort_by_visits", new Link('popular_images'), "Popular Posts");
}
}
}