make use of str_starts_with / str_ends_with / str_contains

This commit is contained in:
Shish 2020-10-25 19:31:58 +00:00
parent c783ff0e8d
commit 19a6b39c70
21 changed files with 42 additions and 36 deletions

View File

@ -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);

View File

@ -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"]));

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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));
}

View File

@ -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.

View File

@ -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;
}

View File

@ -87,7 +87,7 @@ xanax
}
} else {
// other words are literal
if (strpos($comment, $word) !== false) {
if (str_contains($comment, $word)) {
throw $ex;
}
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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));

View File

@ -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

View File

@ -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;

View File

@ -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");
}

View File

@ -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 .= "<textarea cols='50' rows='4' name='_config_$h_name'>$h_value</textarea>";
} else {
$h_box .= "<input type='text' name='_config_$h_name' value='$h_value'>";

View File

@ -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);

View File

@ -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);

View File

@ -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));
}