2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2015-01-24 16:54:18 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class VarnishPurger extends Extension
|
|
|
|
{
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2022-01-17 17:06:20 +00:00
|
|
|
{
|
2022-07-09 22:37:43 +00:00
|
|
|
global $config;
|
|
|
|
$config->set_default_string('varnish_host', '127.0.0.1');
|
|
|
|
$config->set_default_int('varnish_port', 80);
|
|
|
|
$config->set_default_string('varnish_protocol', 'http');
|
2022-01-17 17:06:20 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function curl_purge(string $path): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
// waiting for curl timeout adds ~5 minutes to unit tests
|
|
|
|
if (defined("UNITTEST")) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-20 22:10:33 +00:00
|
|
|
|
2022-01-17 17:06:20 +00:00
|
|
|
global $config;
|
2022-07-09 22:37:43 +00:00
|
|
|
$host = $config->get_string('varnish_host');
|
2022-01-17 17:06:20 +00:00
|
|
|
$port = $config->get_int('varnish_port');
|
|
|
|
$protocol = $config->get_string('varnish_protocol');
|
|
|
|
$url = $protocol . '://'. $host . '/' . $path;
|
2019-05-28 16:59:38 +00:00
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
2022-01-17 17:06:20 +00:00
|
|
|
curl_setopt($ch, CURLOPT_PORT, $port);
|
2019-05-28 16:59:38 +00:00
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PURGE");
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
|
|
$result = curl_exec($ch);
|
|
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
2022-01-17 17:06:20 +00:00
|
|
|
if ($httpCode != 200) {
|
2024-02-11 15:47:40 +00:00
|
|
|
throw new ServerError('PURGE ' . $url . ' unsuccessful (HTTP '. $httpCode . ')');
|
2022-01-17 17:06:20 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
curl_close($ch);
|
|
|
|
}
|
2015-01-24 16:54:18 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onCommentPosting(CommentPostingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->curl_purge("post/view/{$event->image_id}");
|
|
|
|
}
|
2015-01-24 16:54:18 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageInfoSet(ImageInfoSetEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->curl_purge("post/view/{$event->image->id}");
|
|
|
|
}
|
2015-01-24 16:54:18 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageDeletion(ImageDeletionEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->curl_purge("post/view/{$event->image->id}");
|
|
|
|
}
|
2015-01-24 16:54:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 99;
|
|
|
|
}
|
2015-01-24 16:54:18 +00:00
|
|
|
}
|