From 19a6b39c703da49265574c9d9380d9a9de2f7175 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 25 Oct 2020 19:31:58 +0000 Subject: [PATCH] make use of str_starts_with / str_ends_with / str_contains --- core/basepage.php | 4 ++-- core/extension.php | 2 +- core/imageboard/image.php | 2 +- core/polyfills.php | 16 +++++++++++----- core/sanitize_php.php | 2 +- core/urls.php | 4 ++-- core/user.php | 2 +- core/util.php | 6 +++--- ext/artists/main.php | 4 ++-- ext/ban_words/main.php | 2 +- ext/bulk_actions/main.php | 2 +- ext/custom_html_headers/main.php | 2 +- ext/handle_cbz/main.php | 2 +- ext/image/main.php | 2 +- ext/index/main.php | 8 ++++---- ext/ipban/main.php | 2 +- ext/rule34/main.php | 4 ++-- ext/setup/theme.php | 2 +- ext/tag_edit/main.php | 2 +- ext/tag_list/main.php | 6 +++--- ext/upload/theme.php | 2 +- 21 files changed, 42 insertions(+), 36 deletions(-) diff --git a/core/basepage.php b/core/basepage.php index 3bd5f550..e3fb982c 100644 --- a/core/basepage.php +++ b/core/basepage.php @@ -298,7 +298,7 @@ class BasePage if (isset($_SERVER['HTTP_RANGE'])) { list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); - if (strpos($range, ',') !== false) { + if (str_contains($range, ',')) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes $start-$end/$size"); break; @@ -335,7 +335,7 @@ class BasePage break; case PageMode::REDIRECT: if ($this->flash) { - $this->redirect .= (strpos($this->redirect, "?") === false) ? "?" : "&"; + $this->redirect .= str_contains($this->redirect, "?") ? "&" : "?"; $this->redirect .= "flash=" . url_escape(implode("\n", $this->flash)); } header('Location: ' . $this->redirect); diff --git a/core/extension.php b/core/extension.php index 54074b13..6409267e 100644 --- a/core/extension.php +++ b/core/extension.php @@ -413,7 +413,7 @@ abstract class DataHandlerExtension extends Extension $image->filesize = $metadata['size']; $image->hash = $metadata['hash']; - $image->filename = (($pos = strpos($metadata['filename'], '?')) !== false) ? substr($metadata['filename'], 0, $pos) : $metadata['filename']; + $image->filename = ($pos = str_contains($metadata['filename'], '?')) ? substr($metadata['filename'], 0, $pos) : $metadata['filename']; if (array_key_exists("extension", $metadata)) { $image->set_mime(MimeType::get_for_file($filename, $metadata["extension"])); diff --git a/core/imageboard/image.php b/core/imageboard/image.php index a958403e..e5f2b156 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -533,7 +533,7 @@ class Image $image_link = $config->get_string($template); if (!empty($image_link)) { - if (!(strpos($image_link, "://") > 0) && !str_starts_with($image_link, "/")) { + if (!str_contains($image_link, "://") && !str_starts_with($image_link, "/")) { $image_link = make_link($image_link); } $chosen = $image_link; diff --git a/core/polyfills.php b/core/polyfills.php index 1bdf0d7b..04d0e793 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -345,15 +345,21 @@ function unparse_url($parsed_url) if (!function_exists('str_starts_with')) { function str_starts_with(string $haystack, string $needle): bool { - $length = strlen($needle); - return (substr($haystack, 0, $length) === $needle); + return \strncmp($haystack, $needle, \strlen($needle)) === 0; } +} +if (!function_exists('str_ends_with')) { function str_ends_with(string $haystack, string $needle): bool { - $length = strlen($needle); - $start = $length * -1; //negative - return (substr($haystack, $start) === $needle); + return $needle === '' || $needle === \substr($haystack, - \strlen($needle)); + } +} + +if (!function_exists('str_contains')) { + function str_contains(string $haystack, string $needle): bool + { + return '' === $needle || false !== strpos($haystack, $needle); } } diff --git a/core/sanitize_php.php b/core/sanitize_php.php index 5f36cddb..56e44b34 100644 --- a/core/sanitize_php.php +++ b/core/sanitize_php.php @@ -42,7 +42,7 @@ ini_set('assert.exception', '1'); // throw exceptions when failed set_error_handler(function ($errNo, $errStr) { // Should we turn ALL notices into errors? PHP allows a lot of // terrible things to happen by default... - if (strpos($errStr, 'Use of undefined constant ') === 0) { + if (str_starts_with($errStr, 'Use of undefined constant ')) { throw new Exception("PHP Error#$errNo: $errStr"); } else { return false; diff --git a/core/urls.php b/core/urls.php index 1dd6a7ca..1ca3192d 100644 --- a/core/urls.php +++ b/core/urls.php @@ -79,7 +79,7 @@ function modify_url(string $url, array $changes): string */ function make_http(string $link): string { - if (strpos($link, "://") > 0) { + if (str_contains($link, "://")) { return $link; } @@ -105,7 +105,7 @@ function referer_or(string $dest, ?array $blacklist=null): string } if ($blacklist) { foreach ($blacklist as $b) { - if (strstr($_SERVER['HTTP_REFERER'], $b)) { + if (str_contains($_SERVER['HTTP_REFERER'], $b)) { return $dest; } } diff --git a/core/user.php b/core/user.php index 1496c208..c1ddf9e8 100644 --- a/core/user.php +++ b/core/user.php @@ -118,7 +118,7 @@ class User $my_user = User::by_name($name); // If user tried to log in as "foo bar" and failed, try "foo_bar" - if (!$my_user && strpos($name, " ") !== false) { + if (!$my_user && str_contains($name, " ")) { $my_user = User::by_name(str_replace(" ", "_", $name)); } diff --git a/core/util.php b/core/util.php index d9b197a3..23149cdf 100644 --- a/core/util.php +++ b/core/util.php @@ -53,11 +53,11 @@ function contact_link(): ?string return $text; } - if (strpos($text, "@")) { + if (str_contains($text, "@")) { return "mailto:$text"; } - if (strpos($text, "/")) { + if (str_contains($text, "/")) { return "http://$text"; } @@ -348,7 +348,7 @@ function path_to_tags(string $path): string // which is for inheriting to tags on the subfolder $category_to_inherit = $tag; } else { - if ($category!=""&&strpos($tag, ":") === false) { + if ($category!="" && !str_contains($tag, ":")) { // This indicates that category inheritance is active, // and we've encountered a tag that does not specify a category. // So we attach the inherited category to the tag. diff --git a/ext/artists/main.php b/ext/artists/main.php index df49771a..77f2de0d 100644 --- a/ext/artists/main.php +++ b/ext/artists/main.php @@ -553,7 +553,7 @@ class Artists extends Extension $urlsAsString = $inputs["urls"]; $urlsIDsAsString = $inputs["urlsIDs"]; - if (strpos($name, " ")) { + if (str_contains($name, " ")) { return; } @@ -695,7 +695,7 @@ class Artists extends Extension ]); $name = $inputs["name"]; - if (strpos($name, " ")) { + if (str_contains($name, " ")) { return -1; } diff --git a/ext/ban_words/main.php b/ext/ban_words/main.php index 44a9f48e..3ed7d253 100644 --- a/ext/ban_words/main.php +++ b/ext/ban_words/main.php @@ -87,7 +87,7 @@ xanax } } else { // other words are literal - if (strpos($comment, $word) !== false) { + if (str_contains($comment, $word)) { throw $ex; } } diff --git a/ext/bulk_actions/main.php b/ext/bulk_actions/main.php index 35455f48..a81adb31 100644 --- a/ext/bulk_actions/main.php +++ b/ext/bulk_actions/main.php @@ -255,7 +255,7 @@ class BulkActions extends Extension $pos_tag_array = []; $neg_tag_array = []; foreach ($tags as $new_tag) { - if (strpos($new_tag, '-') === 0) { + if (str_starts_with($new_tag, '-')) { $neg_tag_array[] = substr($new_tag, 1); } else { $pos_tag_array[] = $new_tag; diff --git a/ext/custom_html_headers/main.php b/ext/custom_html_headers/main.php index ffeb938e..52a09687 100644 --- a/ext/custom_html_headers/main.php +++ b/ext/custom_html_headers/main.php @@ -55,7 +55,7 @@ class CustomHtmlHeaders extends Extension $sitename_in_title = $config->get_string("sitename_in_title"); // sitename is already in title (can occur on index & other pages) - if (strstr($page->title, $site_title)) { + if (str_contains($page->title, $site_title)) { return; } diff --git a/ext/handle_cbz/main.php b/ext/handle_cbz/main.php index eaf7503d..40e6e4c5 100644 --- a/ext/handle_cbz/main.php +++ b/ext/handle_cbz/main.php @@ -55,7 +55,7 @@ class CBZFileHandler extends DataHandlerExtension sort($names); $cover = $names[0]; foreach ($names as $name) { - if (strpos(strtolower($name), "cover") !== false) { + if (str_contains(strtolower($name), "cover")) { $cover = $name; break; } diff --git a/ext/image/main.php b/ext/image/main.php index a7e3812d..c5b0902b 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -301,7 +301,7 @@ class ImageIO extends Extension public function onParseLinkTemplate(ParseLinkTemplateEvent $event) { $fname = $event->image->get_filename(); - $base_fname = strpos($fname, '.') ? substr($fname, 0, strrpos($fname, '.')) : $fname; + $base_fname = str_contains($fname, '.') ? substr($fname, 0, strrpos($fname, '.')) : $fname; $event->replace('$id', (string)$event->image->id); $event->replace('$hash_ab', substr($event->image->hash, 0, 2)); diff --git a/ext/index/main.php b/ext/index/main.php index b0a30c81..d85da537 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -46,10 +46,10 @@ class Index extends Extension if ( SPEED_HAX && ( - strstr($ua, "Googlebot") !== false - || strstr($ua, "YandexBot") !== false - || strstr($ua, "bingbot") !== false - || strstr($ua, "msnbot") !== false + str_contains($ua, "Googlebot") + || str_contains($ua, "YandexBot") + || str_contains($ua, "bingbot") + || str_contains($ua, "msnbot") ) && ( $count_search_terms > 1 diff --git a/ext/ipban/main.php b/ext/ipban/main.php index 7dd8c337..633f1acf 100644 --- a/ext/ipban/main.php +++ b/ext/ipban/main.php @@ -113,7 +113,7 @@ class IPBan extends Extension $ips = []; # "0.0.0.0" => 123; $networks = []; # "0.0.0.0/32" => 456; foreach ($rows as $ip => $id) { - if (strstr($ip, '/')) { + if (str_contains($ip, '/')) { $networks[$ip] = $id; } else { $ips[$ip] = $id; diff --git a/ext/rule34/main.php b/ext/rule34/main.php index baa14f74..ffb466e9 100644 --- a/ext/rule34/main.php +++ b/ext/rule34/main.php @@ -7,8 +7,8 @@ use function MicroHTML\A; if ( // kill these glitched requests immediately !empty($_SERVER["REQUEST_URI"]) - && strpos(@$_SERVER["REQUEST_URI"], "/http") !== false - && strpos(@$_SERVER["REQUEST_URI"], "paheal.net") !== false + && str_contains(@$_SERVER["REQUEST_URI"], "/http") + && str_contains(@$_SERVER["REQUEST_URI"], "paheal.net") ) { die("No"); } diff --git a/ext/setup/theme.php b/ext/setup/theme.php index f19a6b83..71aa0d9f 100644 --- a/ext/setup/theme.php +++ b/ext/setup/theme.php @@ -52,7 +52,7 @@ class SetupTheme extends Themelet $h_value = html_escape((string)$value); $h_box = ""; - if (is_string($value) && strpos($value, "\n") > 0) { + if (is_string($value) && str_contains($value, "\n")) { $h_box .= ""; } else { $h_box .= ""; diff --git a/ext/tag_edit/main.php b/ext/tag_edit/main.php index a080da62..4613c357 100644 --- a/ext/tag_edit/main.php +++ b/ext/tag_edit/main.php @@ -68,7 +68,7 @@ class TagSetEvent extends Event $this->metatags = []; foreach ($tags as $tag) { - if ((strpos($tag, ':') === false) && (strpos($tag, '=') === false)) { + if ((!str_contains($tag, ':')) && (!str_contains($tag, '='))) { //Tag doesn't contain : or =, meaning it can't possibly be a metatag. //This should help speed wise, as it avoids running every single tag through a bunch of preg_match instead. array_push($this->tags, $tag); diff --git a/ext/tag_list/main.php b/ext/tag_list/main.php index be669b33..7ff990c5 100644 --- a/ext/tag_list/main.php +++ b/ext/tag_list/main.php @@ -198,8 +198,8 @@ class TagList extends Extension $i++; $arg = "tag$i"; $args[$arg] = Tag::sqlify($tag); - if (strpos($tag, '*') === false - && strpos($tag, '?') === false) { + if (!str_contains($tag, '*') + && !str_contains($tag, '?')) { $where[] = " tag = :$arg "; } else { $where[] = " tag LIKE :$arg "; @@ -587,7 +587,7 @@ class TagList extends Extension $starting_tags = []; $tags_ok = true; foreach ($wild_tags as $tag) { - if ($tag[0] == "-" || strpos($tag, "tagme")===0) { + if ($tag[0] == "-" || str_starts_with($tag, "tagme")) { continue; } $tag = Tag::sqlify($tag); diff --git a/ext/upload/theme.php b/ext/upload/theme.php index 10d4bcc2..8f476bd1 100644 --- a/ext/upload/theme.php +++ b/ext/upload/theme.php @@ -205,7 +205,7 @@ class UploadTheme extends Themelet public function display_upload_error(Page $page, string $title, string $message) { // this message has intentional HTML in it... - $message = strpos($message, "already has hash") ? $message : html_escape($message); + $message = str_contains($message, "already has hash") ? $message : html_escape($message); $page->add_block(new Block($title, $message)); }