2020-01-26 13:19:35 +00:00
|
|
|
<?php declare(strict_types=1);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class ImageViewCounter extends Extension
|
|
|
|
{
|
2020-05-15 07:22:54 +00:00
|
|
|
protected $theme;
|
2019-05-28 16:59:38 +00:00
|
|
|
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
|
|
|
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
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// don't add view if person already viewed recently
|
2020-10-24 21:33:29 +00:00
|
|
|
if ($recent_from_ip > 0) {
|
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
|
|
|
|
2020-10-24 21:33:29 +00:00
|
|
|
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
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
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-18 18:26:56 +00:00
|
|
|
|
2020-10-24 21:33:29 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
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);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-24 21:33:29 +00:00
|
|
|
|
2020-05-15 07:22:54 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
2020-10-24 21:33:29 +00:00
|
|
|
global $database;
|
2020-05-15 07:22:54 +00:00
|
|
|
|
|
|
|
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 = [];
|
2020-05-15 07:22:54 +00:00
|
|
|
foreach ($result as $id) {
|
2020-10-24 21:33:29 +00:00
|
|
|
$images[] = Image::by_id(intval($id));
|
2020-05-15 07:22:54 +00:00
|
|
|
}
|
|
|
|
$this->theme->view_popular($images);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
|
|
|
|
{
|
|
|
|
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");
|
2020-05-15 07:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-23 05:21:00 +00:00
|
|
|
}
|