2024-01-16 10:56:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shimmie2;
|
|
|
|
|
|
|
|
class LinkScan extends Extension
|
|
|
|
{
|
2024-01-16 12:08:03 +00:00
|
|
|
public function get_priority(): int
|
2024-01-16 10:56:53 +00:00
|
|
|
{
|
2024-01-16 12:08:03 +00:00
|
|
|
return 10; // be able to intercept post/list
|
2024-01-16 10:56:53 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 12:08:03 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2024-01-16 10:56:53 +00:00
|
|
|
{
|
2024-01-16 12:08:03 +00:00
|
|
|
global $config, $page;
|
2024-01-16 10:56:53 +00:00
|
|
|
|
2024-02-09 16:36:57 +00:00
|
|
|
$search = $event->get_GET('search') ?? $event->get_POST('search') ?? "";
|
2024-02-09 14:46:50 +00:00
|
|
|
if ($event->page_matches("post/list") && !empty($search)) {
|
2024-01-16 12:08:03 +00:00
|
|
|
$trigger = $config->get_string("link_scan_trigger", "https?://");
|
2024-02-09 14:46:50 +00:00
|
|
|
if (preg_match("#.*{$trigger}.*#", $search)) {
|
|
|
|
$ids = $this->scan($search);
|
2024-01-16 12:08:03 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(search_link(["id=".implode(",", $ids)]));
|
|
|
|
$event->stop_processing = true;
|
2024-01-16 10:56:53 +00:00
|
|
|
}
|
2024-01-16 12:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-16 10:56:53 +00:00
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @return int[]
|
|
|
|
*/
|
2024-01-16 12:16:18 +00:00
|
|
|
private function scan(string $text): array
|
2024-01-16 12:08:03 +00:00
|
|
|
{
|
|
|
|
$ids = [];
|
|
|
|
$matches = [];
|
|
|
|
preg_match_all("/post\/view\/(\d+)/", $text, $matches);
|
2024-08-31 16:05:18 +00:00
|
|
|
foreach ($matches[1] as $match) {
|
2024-01-16 12:47:32 +00:00
|
|
|
$img = Image::by_id((int)$match);
|
|
|
|
if ($img) {
|
|
|
|
$ids[] = $img->id;
|
|
|
|
}
|
2024-01-16 12:08:03 +00:00
|
|
|
}
|
|
|
|
preg_match_all("/\b([0-9a-fA-F]{32})\b/", $text, $matches);
|
2024-08-31 16:05:18 +00:00
|
|
|
foreach ($matches[1] as $match) {
|
2024-01-16 12:47:32 +00:00
|
|
|
$img = Image::by_hash($match);
|
|
|
|
if ($img) {
|
|
|
|
$ids[] = $img->id;
|
|
|
|
}
|
2024-01-16 10:56:53 +00:00
|
|
|
}
|
2024-01-16 12:08:03 +00:00
|
|
|
return array_unique($ids);
|
2024-01-16 10:56:53 +00:00
|
|
|
}
|
|
|
|
}
|