[link scan] intercept search box inputs
This commit is contained in:
parent
5a9e6f2638
commit
b1940ca919
3 changed files with 44 additions and 26 deletions
|
@ -14,4 +14,5 @@ class LinkScanInfo extends ExtensionInfo
|
|||
public array $authors = self::SHISH_AUTHOR;
|
||||
public string $license = self::LICENSE_GPLV2;
|
||||
public string $description = "Find posts that are referenced in a block of text";
|
||||
public ?string $documentation = "With this extension enabled, you can paste a block of text into the search field, and the code will scan the text for URLs referencing the site, and show them as search results";
|
||||
}
|
||||
|
|
|
@ -6,34 +6,38 @@ namespace Shimmie2;
|
|||
|
||||
class LinkScan extends Extension
|
||||
{
|
||||
/** @var LinkScanTheme */
|
||||
protected Themelet $theme;
|
||||
|
||||
public function onAdminBuilding(AdminBuildingEvent $event): void
|
||||
public function get_priority(): int
|
||||
{
|
||||
$this->theme->display_form();
|
||||
return 10; // be able to intercept post/list
|
||||
}
|
||||
|
||||
public function onAdminAction(AdminActionEvent $event): void
|
||||
public function onPageRequest(PageRequestEvent $event): void
|
||||
{
|
||||
global $page;
|
||||
if($event->action == "link_scan") {
|
||||
$text = $_POST['text'];
|
||||
$ids = [];
|
||||
global $config, $page;
|
||||
|
||||
$matches = [];
|
||||
preg_match_all("/post\/view\/(\d+)/", $text, $matches);
|
||||
foreach($matches[1] as $match) {
|
||||
$ids[] = $match;
|
||||
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;
|
||||
}
|
||||
preg_match_all("/\b([0-9a-fA-F]{32})\b/", $text, $matches);
|
||||
foreach($matches[1] as $match) {
|
||||
$ids[] = Image::by_hash($match)->id;
|
||||
}
|
||||
|
||||
$event->redirect = false;
|
||||
$page->set_mode(PageMode::REDIRECT);
|
||||
$page->set_redirect(search_link(["id=".implode(",", $ids)]));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
foreach($matches[1] as $match) {
|
||||
$ids[] = Image::by_hash($match)->id;
|
||||
}
|
||||
return array_unique($ids);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,12 +12,12 @@ class LinkScanTest extends ShimmiePHPUnitTestCase
|
|||
$image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "TeStCase");
|
||||
$image_id_2 = $this->post_image("tests/favicon.png", "TeStCase");
|
||||
|
||||
$_POST['text'] = "
|
||||
$text = "
|
||||
Look at http://example.com/post/view/{$image_id_1} there is an image
|
||||
|
||||
http://example.com/post/view/{$image_id_2} is another one
|
||||
";
|
||||
send_event(new AdminActionEvent("link_scan"));
|
||||
$page = $this->get_page("post/list", ["search" => $text]);
|
||||
|
||||
$this->assertEquals(PageMode::REDIRECT, $page->mode);
|
||||
$this->assertEquals("/test/post/list/id%3D{$image_id_1}%2C{$image_id_2}/1", $page->redirect);
|
||||
|
@ -29,12 +29,25 @@ class LinkScanTest extends ShimmiePHPUnitTestCase
|
|||
$image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "TeStCase");
|
||||
$image_id_2 = $this->post_image("tests/favicon.png", "TeStCase");
|
||||
|
||||
$_POST['text'] = "
|
||||
$text = "
|
||||
Look at http://example.com/_images/feb01bab5698a11dd87416724c7a89e3/foobar.jpg
|
||||
there is an image or search for e106ea2983e1b77f11e00c0c54e53805";
|
||||
send_event(new AdminActionEvent("link_scan"));
|
||||
$page = $this->get_page("post/list", ["search" => $text]);
|
||||
|
||||
$this->assertEquals(PageMode::REDIRECT, $page->mode);
|
||||
$this->assertEquals("/test/post/list/id%3D{$image_id_1}%2C{$image_id_2}/1", $page->redirect);
|
||||
}
|
||||
|
||||
public function testNotTriggered(): void
|
||||
{
|
||||
global $page;
|
||||
$this->post_image("tests/pbx_screenshot.jpg", "TeStCase");
|
||||
$this->post_image("tests/favicon.png", "TeStCase");
|
||||
|
||||
$text = "Look at feb01bab5698a11dd87416724c7a89e3/foobar.jpg";
|
||||
$page = $this->get_page("post/list", ["search" => $text]);
|
||||
|
||||
$this->assertEquals(PageMode::REDIRECT, $page->mode);
|
||||
$this->assertEquals("/test/post/list/at%20feb01bab5698a11dd87416724c7a89e3%2Ffoobar.jpg%20Look/1", $page->redirect);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue