diff --git a/ext/approval/main.php b/ext/approval/main.php index c0023d71c..e4dc7bfda 100644 --- a/ext/approval/main.php +++ b/ext/approval/main.php @@ -123,14 +123,12 @@ public function onUserBlockBuilding(UserBlockBuildingEvent $event): void } } - public const SEARCH_REGEXP = "/^approved:(yes|no)/"; + public const SEARCH_REGEXP = "/^approved:(yes|no)/i"; public function onSearchTermParse(SearchTermParseEvent $event): void { global $user, $config; if ($config->get_bool(ApprovalConfig::IMAGES)) { - $matches = []; - if (is_null($event->term) && $this->no_approval_query($event->context)) { $event->add_querylet(new Querylet("approved = :true", ["true" => true])); } @@ -138,7 +136,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void if (is_null($event->term)) { return; } - if (preg_match(self::SEARCH_REGEXP, strtolower($event->term), $matches)) { + if ($matches = $event->matches(self::SEARCH_REGEXP)) { if ($user->can(Permissions::APPROVE_IMAGE) && $matches[1] == "no") { $event->add_querylet(new Querylet("approved != :true", ["true" => true])); } else { diff --git a/ext/artists/main.php b/ext/artists/main.php index 11ae7b74f..6805bad3b 100644 --- a/ext/artists/main.php +++ b/ext/artists/main.php @@ -61,8 +61,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - $matches = []; - if (preg_match("/^(author|artist)[=|:](.*)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^(author|artist)[=|:](.*)$/i")) { $char = $matches[2]; $event->add_querylet(new Querylet("author = :author_char", ["author_char" => $char])); } diff --git a/ext/comment/main.php b/ext/comment/main.php index 83adcbecf..212c6611d 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -381,15 +381,14 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - $matches = []; - if (preg_match("/^comments([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^comments([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i")) { $cmp = ltrim($matches[1], ":") ?: "="; $comments = $matches[2]; $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM comments GROUP BY image_id HAVING count(image_id) $cmp $comments)")); - } elseif (preg_match("/^commented_by[=|:](.*)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^commented_by[=|:](.*)$/i")) { $user_id = User::name_to_id($matches[1]); $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM comments WHERE owner_id = $user_id)")); - } elseif (preg_match("/^commented_by_userno[=|:]([0-9]+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^commented_by_userno[=|:]([0-9]+)$/i")) { $user_id = int_escape($matches[1]); $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM comments WHERE owner_id = $user_id)")); } diff --git a/ext/favorites/main.php b/ext/favorites/main.php index d6ad1f4c4..34f6f65aa 100644 --- a/ext/favorites/main.php +++ b/ext/favorites/main.php @@ -137,17 +137,17 @@ public function onSearchTermParse(SearchTermParseEvent $event): void } $matches = []; - if (preg_match("/^favorites([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^favorites([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i")) { $cmp = ltrim($matches[1], ":") ?: "="; $favorites = $matches[2]; $event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE favorites $cmp $favorites)")); - } elseif (preg_match("/^favorited_by[=|:](.*)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^favorited_by[=|:](.*)$/i")) { $user_id = User::name_to_id($matches[1]); $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM user_favorites WHERE user_id = $user_id)")); - } elseif (preg_match("/^favorited_by_userno[=|:](\d+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^favorited_by_userno[=|:](\d+)$/i")) { $user_id = int_escape($matches[1]); $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM user_favorites WHERE user_id = $user_id)")); - } elseif (preg_match("/^order[=|:](favorites)(?:_(desc|asc))?$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^order[=|:](favorites)(?:_(desc|asc))?$/i")) { $default_order_for_column = "DESC"; $sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column; $event->order = "images.favorites $sort"; diff --git a/ext/index/events.php b/ext/index/events.php index babc70b98..d3ebb3ddc 100644 --- a/ext/index/events.php +++ b/ext/index/events.php @@ -71,6 +71,21 @@ public function add_tag_condition(TagCondition $c): void { $this->tag_conditions[] = $c; } + + /** + * @return array|null + */ + public function matches(string $regex): ?array + { + $matches = []; + if (is_null($this->term)) { + return null; + } + if (\Safe\preg_match($regex, $this->term, $matches) === 1) { + return $matches; + } + return null; + } } class SearchTermParseException extends InvalidInput diff --git a/ext/index/main.php b/ext/index/main.php index 1b697c8c9..7e880e412 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -198,44 +198,43 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - $matches = []; - if (preg_match("/^filesize([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+[kmg]?b?)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^filesize([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+[kmg]?b?)$/i")) { $cmp = ltrim($matches[1], ":") ?: "="; $val = parse_shorthand_int($matches[2]); $event->add_querylet(new Querylet("images.filesize $cmp :val{$event->id}", ["val{$event->id}" => $val])); - } elseif (preg_match("/^id=([\d,]+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^id=([\d,]+)$/i")) { $val = array_map(fn ($x) => int_escape($x), explode(",", $matches[1])); $set = implode(",", $val); $event->add_querylet(new Querylet("images.id IN ($set)")); - } elseif (preg_match("/^id([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^id([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i")) { $cmp = ltrim($matches[1], ":") ?: "="; $val = int_escape($matches[2]); $event->add_querylet(new Querylet("images.id $cmp :val{$event->id}", ["val{$event->id}" => $val])); - } elseif (preg_match("/^(hash|md5)[=|:]([0-9a-fA-F]*)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^(hash|md5)[=|:]([0-9a-fA-F]*)$/i")) { $hash = strtolower($matches[2]); $event->add_querylet(new Querylet('images.hash = :hash', ["hash" => $hash])); - } elseif (preg_match("/^(phash)[=|:]([0-9a-fA-F]*)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^(phash)[=|:]([0-9a-fA-F]*)$/i")) { $phash = strtolower($matches[2]); $event->add_querylet(new Querylet('images.phash = :phash', ["phash" => $phash])); - } elseif (preg_match("/^(filename|name)[=|:](.+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^(filename|name)[=|:](.+)$/i")) { $filename = strtolower($matches[2]); $event->add_querylet(new Querylet("lower(images.filename) LIKE :filename{$event->id}", ["filename{$event->id}" => "%$filename%"])); - } elseif (preg_match("/^posted([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9-]*)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^posted([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9-]*)$/i")) { // TODO Make this able to search = without needing a time component. $cmp = ltrim($matches[1], ":") ?: "="; $val = $matches[2]; $event->add_querylet(new Querylet("images.posted $cmp :posted{$event->id}", ["posted{$event->id}" => $val])); - } elseif (preg_match("/^order[=|:](id|width|height|length|filesize|filename)[_]?(desc|asc)?$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^order[=|:](id|width|height|length|filesize|filename)[_]?(desc|asc)?$/i")) { $ord = strtolower($matches[1]); $default_order_for_column = preg_match("/^(id|filename)$/", $matches[1]) ? "ASC" : "DESC"; $sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column; $event->order = "images.$ord $sort"; - } elseif (preg_match("/^order[=|:]random[_]([0-9]{1,4})$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^order[=|:]random[_]([0-9]{1,4})$/i")) { // requires a seed to avoid duplicates // since the tag can't be changed during the parseevent, we instead generate the seed during submit using js $seed = (int)$matches[1]; $event->order = $database->seeded_random($seed, "images.id"); - } elseif (preg_match("/^order[=|:]dailyshuffle$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^order[=|:]dailyshuffle$/i")) { // will use today's date as seed, thus allowing for a dynamic randomized list without outside intervention. // This way the list will change every day, giving a more dynamic feel to the imageboard. // recommended to change homepage to "post/list/order:dailyshuffle/1" diff --git a/ext/media/main.php b/ext/media/main.php index f2cd2fc2f..e00d60e31 100644 --- a/ext/media/main.php +++ b/ext/media/main.php @@ -243,29 +243,28 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - $matches = []; - if (preg_match("/^content[=|:]((video)|(audio)|(image)|(unknown))$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^content[=|:]((video)|(audio)|(image)|(unknown))$/i")) { $field = $matches[1]; if ($field === "unknown") { $event->add_querylet(new Querylet("video IS NULL OR audio IS NULL OR image IS NULL")); } else { $event->add_querylet(new Querylet("$field = :true", ["true" => true])); } - } elseif (preg_match("/^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+):(\d+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+):(\d+)$/i")) { $cmp = preg_replace_ex('/^:/', '=', $matches[1]); $args = ["width{$event->id}" => int_escape($matches[2]), "height{$event->id}" => int_escape($matches[3])]; $event->add_querylet(new Querylet("width / :width{$event->id} $cmp height / :height{$event->id}", $args)); - } elseif (preg_match("/^size([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)x(\d+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^size([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)x(\d+)$/i")) { $cmp = ltrim($matches[1], ":") ?: "="; $args = ["width{$event->id}" => int_escape($matches[2]), "height{$event->id}" => int_escape($matches[3])]; $event->add_querylet(new Querylet("width $cmp :width{$event->id} AND height $cmp :height{$event->id}", $args)); - } elseif (preg_match("/^width([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^width([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i")) { $cmp = ltrim($matches[1], ":") ?: "="; $event->add_querylet(new Querylet("width $cmp :width{$event->id}", ["width{$event->id}" => int_escape($matches[2])])); - } elseif (preg_match("/^height([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^height([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i")) { $cmp = ltrim($matches[1], ":") ?: "="; $event->add_querylet(new Querylet("height $cmp :height{$event->id}", ["height{$event->id}" => int_escape($matches[2])])); - } elseif (preg_match("/^length([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(.+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^length([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(.+)$/i")) { $value = parse_to_milliseconds($matches[2]); $cmp = ltrim($matches[1], ":") ?: "="; $event->add_querylet(new Querylet("length $cmp :length{$event->id}", ["length{$event->id}" => $value])); diff --git a/ext/mime/main.php b/ext/mime/main.php index bb6d1d8f0..72dd143a2 100644 --- a/ext/mime/main.php +++ b/ext/mime/main.php @@ -74,12 +74,11 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - $matches = []; // check for tags first as tag based searches are more common. - if (preg_match("/^ext[=|:]([a-zA-Z0-9]+)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^ext[=|:]([a-zA-Z0-9]+)$/i")) { $ext = strtolower($matches[1]); $event->add_querylet(new Querylet('images.ext = :ext', ["ext" => $ext])); - } elseif (preg_match("/^mime[=|:](.+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^mime[=|:](.+)$/i")) { $mime = strtolower($matches[1]); $event->add_querylet(new Querylet("images.mime = :mime", ["mime" => $mime])); } diff --git a/ext/notes/main.php b/ext/notes/main.php index 9f0dec627..bbc2c0ac4 100644 --- a/ext/notes/main.php +++ b/ext/notes/main.php @@ -205,18 +205,17 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - $matches = []; - if (preg_match("/^note[=|:](.*)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^note[=|:](.*)$/i")) { $notes = int_escape($matches[1]); $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE note = $notes)")); - } elseif (preg_match("/^notes([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)%/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^notes([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)%/i")) { $cmp = ltrim($matches[1], ":") ?: "="; $notes = $matches[2]; $event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE notes $cmp $notes)")); - } elseif (preg_match("/^notes_by[=|:](.*)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^notes_by[=|:](.*)$/i")) { $user_id = User::name_to_id($matches[1]); $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = $user_id)")); - } elseif (preg_match("/^(notes_by_userno|notes_by_user_id)[=|:](\d+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^(notes_by_userno|notes_by_user_id)[=|:](\d+)$/i")) { $user_id = int_escape($matches[2]); $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = $user_id)")); } diff --git a/ext/numeric_score/main.php b/ext/numeric_score/main.php index 18f185d63..9194db7f4 100644 --- a/ext/numeric_score/main.php +++ b/ext/numeric_score/main.php @@ -319,36 +319,35 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - $matches = []; - if (preg_match("/^score([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(-?\d+)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^score([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(-?\d+)$/i")) { $cmp = ltrim($matches[1], ":") ?: "="; $score = $matches[2]; $event->add_querylet(new Querylet("numeric_score $cmp $score")); - } elseif (preg_match("/^upvoted_by[=|:](.*)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^upvoted_by[=|:](.*)$/i")) { $duser = User::by_name($matches[1]); $event->add_querylet(new Querylet( "images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=1)", ["ns_user_id" => $duser->id] )); - } elseif (preg_match("/^downvoted_by[=|:](.*)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^downvoted_by[=|:](.*)$/i")) { $duser = User::by_name($matches[1]); $event->add_querylet(new Querylet( "images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=-1)", ["ns_user_id" => $duser->id] )); - } elseif (preg_match("/^upvoted_by_id[=|:](\d+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^upvoted_by_id[=|:](\d+)$/i")) { $iid = int_escape($matches[1]); $event->add_querylet(new Querylet( "images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=1)", ["ns_user_id" => $iid] )); - } elseif (preg_match("/^downvoted_by_id[=|:](\d+)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^downvoted_by_id[=|:](\d+)$/i")) { $iid = int_escape($matches[1]); $event->add_querylet(new Querylet( "images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=-1)", ["ns_user_id" => $iid] )); - } elseif (preg_match("/^order[=|:](?:numeric_)?(score)(?:_(desc|asc))?$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^order[=|:](?:numeric_)?(score)(?:_(desc|asc))?$/i")) { $default_order_for_column = "DESC"; $sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column; $event->order = "images.numeric_score $sort"; @@ -366,7 +365,7 @@ public function onTagTermParse(TagTermParseEvent $event): void { $matches = []; - if (preg_match("/^vote[=|:](up|down|remove)$/", $event->term, $matches)) { + if ($matches = $event->matches("/^vote[=|:](up|down|remove)$/")) { global $user; $score = ($matches[1] == "up" ? 1 : ($matches[1] == "down" ? -1 : 0)); if ($user->can(Permissions::CREATE_VOTE)) { diff --git a/ext/pools/main.php b/ext/pools/main.php index 2c7983797..7f12e21d2 100644 --- a/ext/pools/main.php +++ b/ext/pools/main.php @@ -495,8 +495,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - $matches = []; - if (preg_match("/^pool[=|:]([0-9]+|any|none)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^pool[=|:]([0-9]+|any|none)$/i")) { $poolID = $matches[1]; if (preg_match("/^(any|none)$/", $poolID)) { @@ -505,7 +504,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void } else { $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM pool_images WHERE pool_id = $poolID)")); } - } elseif (preg_match("/^pool_by_name[=|:](.*)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^pool_by_name[=|:](.*)$/i")) { $poolTitle = str_replace("_", " ", $matches[1]); $pool = $this->get_single_pool_from_title($poolTitle); @@ -514,7 +513,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void $poolID = $pool->id; } $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM pool_images WHERE pool_id = $poolID)")); - } elseif (preg_match("/^pool_id[=|:](.*)$/i", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^pool_id[=|:](.*)$/i")) { $poolID = str_replace("_", " ", $matches[1]); $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM pool_images WHERE pool_id = $poolID)")); } @@ -531,7 +530,7 @@ public function onTagTermCheck(TagTermCheckEvent $event): void public function onTagTermParse(TagTermParseEvent $event): void { $matches = []; - if (preg_match("/^pool[=|:]([^:]*|lastcreated):?([0-9]*)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^pool[=|:]([^:]*|lastcreated):?([0-9]*)$/i")) { global $user; $poolTag = (string) str_replace("_", " ", $matches[1]); diff --git a/ext/post_source/main.php b/ext/post_source/main.php index 6cfe2c374..5ce41378b 100644 --- a/ext/post_source/main.php +++ b/ext/post_source/main.php @@ -67,7 +67,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - if (preg_match("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i")) { $cmp = ltrim($matches[1], ":") ?: "="; $count = $matches[2]; $event->add_querylet( @@ -92,7 +92,7 @@ public function onTagTermCheck(TagTermCheckEvent $event): void public function onTagTermParse(TagTermParseEvent $event): void { - if (preg_match("/^source[=|:](.*)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^source[=|:](.*)$/i")) { $source = ($matches[1] !== "none" ? $matches[1] : ""); send_event(new SourceSetEvent(Image::by_id_ex($event->image_id), $source)); } diff --git a/ext/post_tags/main.php b/ext/post_tags/main.php index ed6282947..6247efd19 100644 --- a/ext/post_tags/main.php +++ b/ext/post_tags/main.php @@ -73,6 +73,18 @@ public function __construct(string $term) parent::__construct(); $this->term = $term; } + + /** + * @return array|null + */ + public function matches(string $regex): ?array + { + $matches = []; + if (\Safe\preg_match($regex, $this->term, $matches) === 1) { + return $matches; + } + return null; + } } /** @@ -89,6 +101,18 @@ public function __construct(string $term, int $image_id) $this->term = $term; $this->image_id = $image_id; } + + /** + * @return array|null + */ + public function matches(string $regex): ?array + { + $matches = []; + if (\Safe\preg_match($regex, $this->term, $matches) === 1) { + return $matches; + } + return null; + } } class PostTags extends Extension @@ -154,7 +178,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - if (preg_match("/^(source)[=|:](.*)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^(source)[=|:](.*)$/i")) { $source = strtolower($matches[2]); if (preg_match("/^(any|none)$/i", $source)) { diff --git a/ext/private_image/main.php b/ext/private_image/main.php index 7d220a064..177a3030c 100644 --- a/ext/private_image/main.php +++ b/ext/private_image/main.php @@ -95,14 +95,12 @@ public function onDisplayingImage(DisplayingImageEvent $event): void } } - public const SEARCH_REGEXP = "/^private:(yes|no|any)/"; + public const SEARCH_REGEXP = "/^private:(yes|no|any)/i"; public function onSearchTermParse(SearchTermParseEvent $event): void { global $user, $user_config; $show_private = $user_config->get_bool(PrivateImageConfig::USER_VIEW_DEFAULT); - $matches = []; - if (is_null($event->term) && $this->no_private_query($event->context)) { if ($show_private) { $event->add_querylet( @@ -122,10 +120,10 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - if (preg_match(self::SEARCH_REGEXP, strtolower($event->term), $matches)) { + if ($matches = $event->matches(self::SEARCH_REGEXP)) { $params = []; $query = ""; - switch ($matches[1]) { + switch (strtolower($matches[1])) { case "no": $query .= "private != :true"; $params["true"] = true; diff --git a/ext/relationships/main.php b/ext/relationships/main.php index 6fb9930ce..ca1a69acf 100644 --- a/ext/relationships/main.php +++ b/ext/relationships/main.php @@ -79,8 +79,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - $matches = []; - if (preg_match("/^parent[=|:]([0-9]+|any|none)$/", $event->term, $matches)) { + if ($matches = $event->matches("/^parent[=|:]([0-9]+|any|none)$/")) { $parentID = $matches[1]; if (preg_match("/^(any|none)$/", $parentID)) { @@ -89,7 +88,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void } else { $event->add_querylet(new Querylet("images.parent_id = :pid", ["pid" => $parentID])); } - } elseif (preg_match("/^child[=|:](any|none)$/", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^child[=|:](any|none)$/")) { $not = ($matches[1] == "any" ? "=" : "!="); $event->add_querylet(new Querylet("images.has_children $not :true", ["true" => true])); } @@ -104,23 +103,21 @@ public function onHelpPageBuilding(HelpPageBuildingEvent $event): void public function onTagTermCheck(TagTermCheckEvent $event): void { - if (preg_match("/^(parent|child)[=|:](.*)$/i", $event->term)) { + if ($event->matches("/^(parent|child)[=|:](.*)$/i")) { $event->metatag = true; } } public function onTagTermParse(TagTermParseEvent $event): void { - $matches = []; - - if (preg_match("/^parent[=|:]([0-9]+|none)$/", $event->term, $matches)) { + if ($matches = $event->matches("/^parent[=|:]([0-9]+|none)$/")) { $parentID = $matches[1]; if ($parentID == "none" || $parentID == "0") { $this->remove_parent($event->image_id); } else { send_event(new ImageRelationshipSetEvent($event->image_id, (int)$parentID)); } - } elseif (preg_match("/^child[=|:]([0-9]+)$/", $event->term, $matches)) { + } elseif ($matches = $event->matches("/^child[=|:]([0-9]+)$/")) { $childID = $matches[1]; send_event(new ImageRelationshipSetEvent((int)$childID, $event->image_id)); } diff --git a/ext/tag_categories/main.php b/ext/tag_categories/main.php index f718aeeb8..d6e0b59bb 100644 --- a/ext/tag_categories/main.php +++ b/ext/tag_categories/main.php @@ -87,8 +87,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - $matches = []; - if (preg_match("/^(.+)tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9]+)$/i", $event->term, $matches)) { + if ($matches = $event->matches("/^(.+)tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9]+)$/i")) { global $database; $type = strtolower($matches[1]); $cmp = ltrim($matches[2], ":") ?: "="; diff --git a/ext/trash/main.php b/ext/trash/main.php index ca96c2f0e..66fa88ee8 100644 --- a/ext/trash/main.php +++ b/ext/trash/main.php @@ -95,7 +95,7 @@ public function onUserBlockBuilding(UserBlockBuildingEvent $event): void } } - public const SEARCH_REGEXP = "/^in:trash$/"; + public const SEARCH_REGEXP = "/^in:(trash)$/i"; public function onSearchTermParse(SearchTermParseEvent $event): void { global $user; @@ -109,7 +109,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void if (is_null($event->term)) { return; } - if (preg_match(self::SEARCH_REGEXP, strtolower($event->term), $matches)) { + if ($event->matches(self::SEARCH_REGEXP)) { if ($user->can(Permissions::VIEW_TRASH)) { $event->add_querylet(new Querylet("trash = :true", ["true" => true])); } diff --git a/ext/user/main.php b/ext/user/main.php index 15f26d72e..ee8170b1c 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -583,14 +583,13 @@ public function onSearchTermParse(SearchTermParseEvent $event): void return; } - $matches = []; - if (preg_match(self::USER_SEARCH_REGEX, $event->term, $matches)) { + if ($matches = $event->matches(self::USER_SEARCH_REGEX)) { $duser = User::by_name($matches[2]); $event->add_querylet(new Querylet("images.owner_id {$matches[1]}= {$duser->id}")); - } elseif (preg_match(self::USER_ID_SEARCH_REGEX, $event->term, $matches)) { + } elseif ($matches = $event->matches(self::USER_ID_SEARCH_REGEX)) { $user_id = int_escape($matches[2]); $event->add_querylet(new Querylet("images.owner_id {$matches[1]}= $user_id")); - } elseif ($user->can(Permissions::VIEW_IP) && preg_match("/^(?:poster|user)_ip[=|:]([0-9\.]+)$/i", $event->term, $matches)) { + } elseif ($user->can(Permissions::VIEW_IP) && $matches = $event->matches("/^(?:poster|user)_ip[=|:]([0-9\.]+)$/i")) { $user_ip = $matches[1]; // FIXME: ip_escape? $event->add_querylet(new Querylet("images.owner_ip = '$user_ip'")); }