Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce string query and mode search params #10462

Merged
merged 5 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function opensearch()

public function quickSearch()
{
$quickSearch = new QuickSearch(request(), ['user' => auth()->user()]);
$quickSearch = new QuickSearch(Request::all(), ['user' => auth()->user()]);
$searches = $quickSearch->searches();

$result = [];
Expand Down Expand Up @@ -177,18 +177,20 @@ public function quickSearch()
*/
public function search()
{
if (request('mode') === 'beatmapset') {
return ujs_redirect(route('beatmapsets.index', ['q' => request('query')]));
$currentUser = Auth::user();
$allSearch = new AllSearch(Request::all(), ['user' => $currentUser]);

if ($allSearch->getMode() === 'beatmapset') {
return ujs_redirect(route('beatmapsets.index', ['q' => $allSearch->getRawQuery()]));
}

$allSearch = new AllSearch(request(), ['user' => Auth::user()]);
$isSearchPage = true;

if (is_api_request()) {
return response()->json($allSearch->toJson());
}

$fields = Auth::user()?->isModerator() ?? false ? [] : ['includeDeleted' => null];
$fields = $currentUser?->isModerator() ?? false ? [] : ['includeDeleted' => null];

return ext_view('home.search', compact('allSearch', 'fields', 'isSearchPage'));
}
Expand Down
4 changes: 2 additions & 2 deletions app/Libraries/Search/ForumSearchRequestParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public function __construct(array $request, ?User $user)
{
parent::__construct();

$this->queryString = presence(trim($request['query'] ?? null));
$this->queryString = presence(trim(get_string($request['query'] ?? null) ?? ''));
$this->page = get_int($request['page'] ?? null);
$this->from = $this->pageAsFrom($this->page);
$this->includeSubforums = get_bool($request['forum_children'] ?? false);
$this->username = presence(trim($request['username'] ?? null));
$this->username = presence(trim(get_string($request['username'] ?? null) ?? ''));
$this->forumId = get_int($request['forum_id'] ?? null);
$this->topicId = get_int($request['topic_id'] ?? null);
$this->parseSort(get_string($request['sort'] ?? null));
Expand Down
26 changes: 18 additions & 8 deletions app/Libraries/Search/MultiSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

namespace App\Libraries\Search;

use Illuminate\Http\Request;

class MultiSearch
{
const MODES = [
Expand Down Expand Up @@ -36,24 +34,36 @@ class MultiSearch
private $options;
private $query;
private $searches;
private array $request;

public function __construct(Request $request, array $options = [])
public function __construct(private array $request, array $options = [])
{
$this->request = $request->all();
$this->query = trim(get_string($this->request['query'] ?? null) ?? '');
if (isset($this->request['mode'])) {
$this->request['mode'] = presence(get_string($this->request['mode']));
}
if (isset($this->request['query'])) {
$this->request['query'] = get_string($this->request['query']);
}
if (isset($this->request['username'])) {
$this->request['username'] = presence(get_string($this->request['username']));
}
$this->query = trim($this->request['query'] ?? '');
$this->options = $options;
}

public function getMode()
{
return presence($this->request['mode'] ?? null) ?? 'all';
return $this->request['mode'] ?? 'all';
}

public function getRawQuery(): ?string
{
return $this->request['query'] ?? null;
}

public function hasQuery()
{
return present($this->query)
|| ($this->getMode() === 'forum_post' && present(get_string($this->request['username'] ?? null)));
|| ($this->getMode() === 'forum_post' && isset($this->request['username']));
}

public function searches()
Expand Down
2 changes: 1 addition & 1 deletion app/Libraries/Search/UserSearchRequestParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(array $request)
{
parent::__construct();

$this->queryString = presence(trim($request['query'] ?? null));
$this->queryString = presence(trim(get_string($request['query'] ?? null) ?? ''));
$this->page = get_int($request['page'] ?? null);
$this->from = $this->pageAsFrom($this->page);
$this->recentOnly = get_bool($request['recent_only'] ?? null);
Expand Down
4 changes: 2 additions & 2 deletions app/Libraries/Search/WikiSearchRequestParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public function __construct(array $request)
{
parent::__construct();

$this->queryString = trim($request['query'] ?? null);
$this->locale = $request['locale'] ?? null;
$this->queryString = trim(get_string($request['query'] ?? null) ?? '');
$this->locale = get_string($request['locale'] ?? null);
$this->page = get_int($request['page'] ?? null);
$this->from = $this->pageAsFrom($this->page);
}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/home/_search_page_tabs.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
@endphp
<a
href="{{ route('search', ['mode' => $mode, 'query' => request('query')]) }}"
href="{{ route('search', ['mode' => $mode, 'query' => $allSearch->getRawQuery()]) }}"
class="{{ $cssClasses }}"
>
<span class="fake-bold" data-content="{{ osu_trans("home.search.mode.{$mode}") }}">
Expand Down
6 changes: 3 additions & 3 deletions resources/views/home/_search_results.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<a
class="search-result__more-button {{ $showMore ? '' : 'search-result__more-button--hidden' }}"
href="{{ route('search', ['mode' => $mode, 'query' => request('query')]) }}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember this being done intentionally to preserve the query as-is instead of changing foo to foo but I suppose it should be fine as long as the input box's text itself isn't changing 🤔

href="{{ route('search', ['mode' => $mode, 'query' => $allSearch->getRawQuery()]) }}"
>
<span class="fas fa-angle-right"></span>
</a>
Expand All @@ -34,12 +34,12 @@ class="search-result__more-button {{ $showMore ? '' : 'search-result__more-butto
@if ($showMore)
<a
class="search-result__row search-result__row--more"
href="{{ route('search', ['mode' => $mode, 'query' => request('query')]) }}"
href="{{ route('search', ['mode' => $mode, 'query' => $allSearch->getRawQuery()]) }}"
>
{{ osu_trans("home.search.{$mode}.more_simple") }}
</a>
@else
@if (request('mode') === 'user' && $search->overLimit())
@if ($allSearch->getMode() === 'user' && $search->overLimit())
<div class="search-result__row search-result__row--notice">
{{ osu_trans("home.search.user.more_hidden", ['max' => config("osu.search.max.user")]) }}
</div>
Expand Down
6 changes: 3 additions & 3 deletions resources/views/home/search.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
class="js-search osu-page"
autocomplete="off"
>
<input type="hidden" name="mode" value="{{ request('mode') }}">
<input type="hidden" name="mode" value="{{ $allSearch->getMode() }}">

<div class="search-header js-search--header">
<div class="search-header__box">
<input
autofocus
class="search-header__input js-search--input"
data-search-current="{{ request('query') }}"
data-search-current="{{ $allSearch->getRawQuery() }}"
data-turbolinks-permanent
id="search-input"
name="query"
placeholder="{{ osu_trans('home.search.placeholder') }}"
type="search"
value="{{ request('query') }}"
value="{{ $allSearch->getRawQuery() }}"
/>

<button class="search-header__icon search-header__icon--normal">
Expand Down