diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index 09def27ee..f7bc1c62a 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -197,7 +197,7 @@ function redirect_to_next_image(Image $image): void $target_image = $image->get_next($search_terms); if ($target_image === null) { - $redirect_target = referer_or(make_link("post/list"), ['post/view']); + $redirect_target = referer_or(search_link(), ['post/view']); } else { $redirect_target = make_link("post/view/{$target_image->id}", null, $query); } diff --git a/core/tests/urls.test.php b/core/tests/urls.test.php index fb34d4ab8..e607726a1 100644 --- a/core/tests/urls.test.php +++ b/core/tests/urls.test.php @@ -10,6 +10,18 @@ class UrlsTest extends TestCase { + public function test_search_link() + { + $this->assertEquals( + "/test/post/list/bar+foo/1", + search_link(["foo", "bar"]) + ); + $this->assertEquals( + "/test/post/list/cat%2A+rating%3D%5Eq/1", + search_link(["rating=?", "cat*"]) + ); + } + public function test_make_link() { // basic diff --git a/core/urls.php b/core/urls.php index 0950f4bca..40261458b 100644 --- a/core/urls.php +++ b/core/urls.php @@ -23,6 +23,20 @@ public function make_link(): string } } +/** + * Build a link to a search page for given terms, + * with all the appropriate escaping + */ +function search_link(array $terms = [], int $page = 1): string +{ + if($terms) { + $q = urlencode(Tag::caret(Tag::implode($terms))); + return make_link("post/list/$q/$page"); + } else { + return make_link("post/list/$page"); + } +} + /** * Figure out the correct way to link to a page, taking into account * things like the nice URLs setting. diff --git a/ext/approval/main.php b/ext/approval/main.php index 1f62ed5f6..8c4080e54 100644 --- a/ext/approval/main.php +++ b/ext/approval/main.php @@ -134,7 +134,7 @@ public function onUserBlockBuilding(UserBlockBuildingEvent $event) { global $user; if ($user->can(Permissions::APPROVE_IMAGE)) { - $event->add_link("Pending Approval", make_link("/post/list/approved%3Ano/1"), 60); + $event->add_link("Pending Approval", search_link(["approved:no"]), 60); } } diff --git a/ext/artists/theme.php b/ext/artists/theme.php index be5016364..ed36eb3f9 100644 --- a/ext/artists/theme.php +++ b/ext/artists/theme.php @@ -353,7 +353,7 @@ public function show_artist($artist, $aliases, $members, $urls, $images, $userIs { global $page; - $artist_link = "".str_replace("_", " ", $artist['name']).""; + $artist_link = "".str_replace("_", " ", $artist['name']).""; $html = " diff --git a/ext/browser_search/main.php b/ext/browser_search/main.php index eb71905a0..3487d78bc 100644 --- a/ext/browser_search/main.php +++ b/ext/browser_search/main.php @@ -26,7 +26,7 @@ public function onPageRequest(PageRequestEvent $event) if ($event->page_matches("browser_search.xml")) { // First, we need to build all the variables we'll need $search_title = $config->get_string(SetupConfig::TITLE); - $search_form_url = make_link('post/list/{searchTerms}'); + $search_form_url = search_link(['{searchTerms}']); $suggenton_url = make_link('browser_search/')."{searchTerms}"; $icon_b64 = base64_encode(file_get_contents("ext/static_files/static/favicon.ico")); diff --git a/ext/favorites/main.php b/ext/favorites/main.php index eb94d62cf..fb0c0f462 100644 --- a/ext/favorites/main.php +++ b/ext/favorites/main.php @@ -75,7 +75,7 @@ public function onUserPageBuilding(UserPageBuildingEvent $event) $i_favorites_count = Image::count_images(["favorited_by={$event->display_user->name}"]); $i_days_old = ((time() - strtotime($event->display_user->join_date)) / 86400) + 1; $h_favorites_rate = sprintf("%.1f", ($i_favorites_count / $i_days_old)); - $favorites_link = make_link("post/list/favorited_by={$event->display_user->name}/1"); + $favorites_link = search_link(["favorited_by={$event->display_user->name}"]); $event->add_stats("Posts favorited: $i_favorites_count, $h_favorites_rate per day"); } @@ -115,7 +115,7 @@ public function onUserBlockBuilding(UserBlockBuildingEvent $event) global $user; $username = url_escape($user->name); - $event->add_link("My Favorites", make_link("post/list/favorited_by=$username/1"), 20); + $event->add_link("My Favorites", search_link(["favorited_by=$username"]), 20); } public function onSearchTermParse(SearchTermParseEvent $event) diff --git a/ext/home/theme.php b/ext/home/theme.php index a0b37efe5..b72218cb9 100644 --- a/ext/home/theme.php +++ b/ext/home/theme.php @@ -37,7 +37,7 @@ public function build_body(string $sitename, string $main_links, string $main_te $contact_link = empty($contact_link) ? "" : "
Contact –"; $search_html = "