Skip to content

Commit

Permalink
[speed_hax] allow post/prev and post/next to add some tags internally…
Browse files Browse the repository at this point in the history
… without penalising the user
  • Loading branch information
shish committed Feb 10, 2025
1 parent 3aa4c42 commit 5604bc5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
6 changes: 5 additions & 1 deletion core/imageboard/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ private static function find_images_internal(int $start = 0, ?int $limit = null,

if (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_int(SpeedHaxConfig::BIG_SEARCH) > 0) {
$anon_limit = $config->get_int(SpeedHaxConfig::BIG_SEARCH);
if (!$user->can(Permissions::BIG_SEARCH) and count($tags) > $anon_limit) {
$counted_tags = $tags;
// exclude tags which start with "id>", "id<", or "order:id_"
// because those are added internally for post/next and post/prev
$counted_tags = array_filter($counted_tags, fn ($tag) => !\Safe\preg_match("/^id[><]|^order:id_/", $tag));
if (!$user->can(Permissions::BIG_SEARCH) and count($counted_tags) > $anon_limit) {
throw new PermissionDenied("Anonymous users may only search for up to $anon_limit tags at a time");
}
}
Expand Down
56 changes: 56 additions & 0 deletions ext/speed_hax/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Shimmie2;

class SpeedHaxTest extends ShimmiePHPUnitTestCase
{
public function testUserNoTagLimit(): void
{
global $config;
$config->set_int(SpeedHaxConfig::BIG_SEARCH, 1);

$this->log_in_as_user();
$image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "asdf post1");
$image_id_2 = $this->post_image("tests/favicon.png", "asdf post2");

// default user isn't limited
$this->assert_search_results(["asdf"], [$image_id_2, $image_id_1], "User can search for one tag");
$this->assert_search_results(["asdf", "post1"], [$image_id_1], "User can search for two tags");
}

public function testAnonTagLimit(): void
{
global $config;
$config->set_int(SpeedHaxConfig::BIG_SEARCH, 1);

$this->log_in_as_user();
$image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "asdf post1");
$image_id_2 = $this->post_image("tests/favicon.png", "asdf post2");
$this->log_out();

// default anon is limited
$this->assert_search_results(["asdf"], [$image_id_2, $image_id_1], "Anon can search for one tag");
$this->assertException(PermissionDenied::class, function () use ($image_id_1) {
$this->assert_search_results(["asdf", "post1"], [$image_id_1]);
});
}

public function testAnonPostNext(): void
{
global $config;
$config->set_int(SpeedHaxConfig::BIG_SEARCH, 1);

$this->log_in_as_user();
$image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "asdf post1");
$image_id_2 = $this->post_image("tests/favicon.png", "asdf post2");
$this->log_out();

// post/next and post/prev use additional tags internally,
// but those ones shouldn't count towards the limit
$page = $this->get_page("post/next/$image_id_2", ["search" => "asdf"]);
$this->assertEquals(PageMode::REDIRECT, $page->mode);
$this->assertEquals($page->redirect, make_link("post/view/$image_id_1?#search=asdf"));
}
}
4 changes: 0 additions & 4 deletions ext/view/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
require_once "events/image_info_set_event.php";
require_once "events/image_admin_block_building_event.php";

use function MicroHTML\TR;
use function MicroHTML\TH;
use function MicroHTML\TD;

class ViewPost extends Extension
{
/** @var ViewPostTheme */
Expand Down

0 comments on commit 5604bc5

Please sign in to comment.