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

MarisaBot emotes search function #1

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Add commenting to explain new code.
sdleffler committed Jan 27, 2021
commit d825f9129c61a41e803d2d62394d536782829a7b
8 changes: 8 additions & 0 deletions emotes.php
Original file line number Diff line number Diff line change
@@ -154,15 +154,23 @@ function createThumbnail($image_name,$new_width,$new_height,$uploadDir,$moveToDi
$prev = '?';
echo 'total emotes: ' . count($files) . '</br>' . 'randoms: gun, shades, smug, stare, thumbsup (use !randemote)' . '</br></br>';

// Generate a form for the search box. This consists of a text box (which should contain the previous search term, if any),
// a "Search" button which submits the form and reloads the page, and a "Clear" button which returns to viewing all emotes.
echo '<form action="emotes.php" method="get">Search: <input type="text" name="search_query" ';
// We provide an initial value *only* if there is a previous search query and we did not just clear the search.
// Also, sanitize the search query string just in case it contains HTML.
if (isset($_GET["search_query"]) && !isset($_GET["clear_search"])) {
echo 'value=' . filter_var($_GET["search_query"], FILTER_SANITIZE_STRING);
}
echo '> <input type="submit" name="form_submit" value="Search"> <input type="submit" name="clear_search" value="Clear"><br></form>';

// If the search query string is set, filter $files for filenames which have substrings matching the search query, and
// then return those thumbnails only.
if (isset($_GET["search_query"]) && isset($_GET["form_submit"]) && $_GET["form_submit"] === "Search") {
$needle = filter_var($_GET["search_query"], FILTER_SANITIZE_STRING);

// If the search query is an empty string, we don't want to show anything; otherwise we could accidentally show all emotes,
// as the empty string is a substring of all strings, and that would be slow.
$filtered = [];
if ($needle !== "") {
$filtered = array_values(array_filter($files, function($image) use ($needle) {