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/link_scan/main.php

54 lines
1.5 KiB
PHP
Raw Normal View History

2024-01-16 10:56:53 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class LinkScan extends Extension
{
public function get_priority(): int
2024-01-16 10:56:53 +00:00
{
return 10; // be able to intercept post/list
2024-01-16 10:56:53 +00:00
}
public function onPageRequest(PageRequestEvent $event): void
2024-01-16 10:56:53 +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)) {
$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);
$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 10:56:53 +00:00
/**
* @return int[]
*/
2024-01-16 12:16:18 +00:00
private function scan(string $text): array
{
$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;
}
}
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
}
return array_unique($ids);
2024-01-16 10:56:53 +00:00
}
}