<?php
declare(strict_types=1);
namespace Shimmie2;
class LinkScan extends Extension
{
public function get_priority(): int
return 10; // be able to intercept post/list
}
public function onPageRequest(PageRequestEvent $event): void
global $config, $page;
if ($event->page_matches("post/list") && isset($_GET['search'])) {
$trigger = $config->get_string("link_scan_trigger", "https?://");
if (preg_match("#.*{$trigger}.*#", $_GET['search'])) {
$ids = $this->scan($_GET['search']);
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(search_link(["id=".implode(",", $ids)]));
$event->stop_processing = true;
private function scan(string $text): array
$ids = [];
$matches = [];
preg_match_all("/post\/view\/(\d+)/", $text, $matches);
foreach($matches[1] as $match) {
$ids[] = $match;
preg_match_all("/\b([0-9a-fA-F]{32})\b/", $text, $matches);
$ids[] = Image::by_hash($match)->id;
return array_unique($ids);