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

Use PHP 8 string functions #8207

Merged
merged 9 commits into from
May 16, 2024
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
14 changes: 7 additions & 7 deletions Sources/Actions/Admin/ACP.php
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ public static function prepareDBSettingContext(array &$config_vars): void
// Finally allow overrides - and some final cleanups.
foreach ($config_var as $k => $v) {
if (!is_numeric($k)) {
if (substr($k, 0, 2) == 'on') {
if (str_starts_with($k, 'on')) {
Utils::$context['config_vars'][$config_var[1]]['javascript'] .= ' ' . $k . '="' . $v . '"';
} else {
Utils::$context['config_vars'][$config_var[1]][$k] = $v;
Expand Down Expand Up @@ -1052,13 +1052,13 @@ public static function saveSettings(array &$config_vars): void

// Fix the forum's URL if necessary.
if (isset($_POST['boardurl'])) {
if (substr($_POST['boardurl'], -10) == '/index.php') {
if (str_ends_with($_POST['boardurl'], '/index.php')) {
$_POST['boardurl'] = substr($_POST['boardurl'], 0, -10);
} elseif (substr($_POST['boardurl'], -1) == '/') {
} elseif (str_ends_with($_POST['boardurl'], '/')) {
$_POST['boardurl'] = substr($_POST['boardurl'], 0, -1);
}

if (substr($_POST['boardurl'], 0, 7) != 'http://' && substr($_POST['boardurl'], 0, 7) != 'file://' && substr($_POST['boardurl'], 0, 8) != 'https://') {
if (!str_starts_with($_POST['boardurl'], 'http://') && !str_starts_with($_POST['boardurl'], 'file://') && !str_starts_with($_POST['boardurl'], 'https://')) {
$_POST['boardurl'] = 'http://' . $_POST['boardurl'];
}

Expand Down Expand Up @@ -1562,7 +1562,7 @@ public static function getFileVersions(array &$versionOptions): array
$tasks_dir = dir(Config::$tasksdir);

while ($entry = $tasks_dir->read()) {
if (substr($entry, -4) === '.php' && !is_dir(Config::$tasksdir . '/' . $entry) && $entry !== 'index.php') {
if (str_ends_with($entry, '.php') && !is_dir(Config::$tasksdir . '/' . $entry) && $entry !== 'index.php') {
// Read the first 4k from the file.... enough for the header.
$fp = fopen(Config::$tasksdir . '/' . $entry, 'rb');
$header = fread($fp, 4096);
Expand Down Expand Up @@ -1592,7 +1592,7 @@ public static function getFileVersions(array &$versionOptions): array
$this_dir = dir($dirname);

while ($entry = $this_dir->read()) {
if (substr($entry, -12) == 'template.php' && !is_dir($dirname . '/' . $entry)) {
if (str_ends_with($entry, 'template.php') && !is_dir($dirname . '/' . $entry)) {
// Read the first 768 bytes from the file.... enough for the header.
$fp = fopen($dirname . '/' . $entry, 'rb');
$header = fread($fp, 768);
Expand All @@ -1615,7 +1615,7 @@ public static function getFileVersions(array &$versionOptions): array
$this_dir = dir($lang_dir);

while ($entry = $this_dir->read()) {
if (substr($entry, -4) == '.php' && $entry != 'index.php' && !is_dir($lang_dir . '/' . $entry)) {
if (str_ends_with($entry, '.php') && $entry != 'index.php' && !is_dir($lang_dir . '/' . $entry)) {
// Read the first 768 bytes from the file.... enough for the header.
$fp = fopen($lang_dir . '/' . $entry, 'rb');
$header = fread($fp, 768);
Expand Down
14 changes: 7 additions & 7 deletions Sources/Actions/Admin/Attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -1229,14 +1229,14 @@ public function repair(): void

if ($files_checked <= $current_check) {
// Temporary file, get rid of it!
if (strpos($file, 'post_tmp_') !== false) {
if (str_contains($file, 'post_tmp_')) {
// Temp file is more than 5 hours old!
if (filemtime($attach_dir . '/' . $file) < time() - 18000) {
@unlink($attach_dir . '/' . $file);
}
}
// That should be an attachment, let's check if we have it in the database
elseif (strpos($file, '_') !== false) {
elseif (str_contains($file, '_')) {
$attachID = (int) substr($file, 0, strpos($file, '_'));

if (!empty($attachID)) {
Expand Down Expand Up @@ -1414,7 +1414,7 @@ public function paths(): void
if (!empty(Config::$modSettings['attachment_basedirectories'])) {
// Count any sub-folders.
foreach (Config::$modSettings['attachmentUploadDir'] as $sub) {
if (strpos($sub, $path . DIRECTORY_SEPARATOR) !== false) {
if (str_contains($sub, $path . DIRECTORY_SEPARATOR)) {
$num_attach++;
}
}
Expand Down Expand Up @@ -1497,14 +1497,14 @@ public function paths(): void

if (!empty(Config::$modSettings['attachment_basedirectories'])) {
foreach (Config::$modSettings['attachment_basedirectories'] as $bid => $base) {
if (strpos(Config::$modSettings['attachmentUploadDir'][$_POST['current_dir']], $base . DIRECTORY_SEPARATOR) !== false) {
if (str_contains(Config::$modSettings['attachmentUploadDir'][$_POST['current_dir']], $base . DIRECTORY_SEPARATOR)) {
$use_subdirectories_for_attachments = 1;
break;
}
}
}

if ($use_subdirectories_for_attachments == 0 && strpos(Config::$modSettings['attachmentUploadDir'][$_POST['current_dir']], Config::$boarddir . DIRECTORY_SEPARATOR) !== false) {
if ($use_subdirectories_for_attachments == 0 && str_contains(Config::$modSettings['attachmentUploadDir'][$_POST['current_dir']], Config::$boarddir . DIRECTORY_SEPARATOR)) {
$bid = 0;
}

Expand Down Expand Up @@ -2428,7 +2428,7 @@ public static function list_getAttachDirs(): array

// Count any sub-folders.
foreach (Config::$modSettings['attachmentUploadDir'] as $sid => $sub) {
if (strpos($sub, $dir . DIRECTORY_SEPARATOR) !== false) {
if (str_contains($sub, $dir . DIRECTORY_SEPARATOR)) {
$expected_files[$id]++;
$sub_dirs++;
}
Expand Down Expand Up @@ -2481,7 +2481,7 @@ public static function list_getBaseDirs(): array
$expected_dirs = 0;

foreach (Config::$modSettings['attachmentUploadDir'] as $sid => $sub) {
if (strpos($sub, $dir . DIRECTORY_SEPARATOR) !== false) {
if (str_contains($sub, $dir . DIRECTORY_SEPARATOR)) {
$expected_dirs++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Actions/Admin/Bans.php
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ public static function updateBanMembers(): void

if ($row['email_address']) {
// Does it have a wildcard - if so we can't do a IN on it.
if (strpos($row['email_address'], '%') !== false) {
if (str_contains($row['email_address'], '%')) {
$memberEmailWild[$row['email_address']] = $row['email_address'];
} else {
$memberEmails[$row['email_address']] = $row['email_address'];
Expand Down Expand Up @@ -2419,7 +2419,7 @@ protected static function removeBanTriggers(array|int $items_ids = [], ?int $gro
$ban_items[$row['id_ban']]['type'] = 'ip';
$ban_items[$row['id_ban']]['ip'] = IP::range2ip($row['ip_low'], $row['ip_high']);

$is_range = (strpos($ban_items[$row['id_ban']]['ip'], '-') !== false || strpos($ban_items[$row['id_ban']]['ip'], '*') !== false);
$is_range = (str_contains($ban_items[$row['id_ban']]['ip'], '-') || str_contains($ban_items[$row['id_ban']]['ip'], '*'));

$log_info[] = [
'bantype' => ($is_range ? 'ip_range' : 'main_ip'),
Expand Down
2 changes: 1 addition & 1 deletion Sources/Actions/Admin/EndSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function execute(): void

// Clean any admin tokens as well.
foreach ($_SESSION['token'] as $key => $token) {
if (strpos($key, '-admin') !== false) {
if (str_contains($key, '-admin')) {
unset($_SESSION['token'][$key]);
}
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/Actions/Admin/ErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function view(): void
'time' => Time::create('@' . $row['log_time'])->format(),
'timestamp' => $row['log_time'],
'url' => [
'html' => Utils::htmlspecialchars(strpos($row['url'], 'cron.php') === false ? (substr($row['url'], 0, 1) == '?' ? Config::$scripturl : '') . $row['url'] : $row['url']),
'html' => Utils::htmlspecialchars(!str_contains($row['url'], 'cron.php') ? (str_starts_with($row['url'], '?') ? Config::$scripturl : '') . $row['url'] : $row['url']),
'href' => base64_encode(Db::$db->escape_wildcard_string($row['url'])),
],
'message' => [
Expand All @@ -250,7 +250,7 @@ public function view(): void
if (!empty($row['file']) && !empty($row['line'])) {
// Eval'd files rarely point to the right location and cause
// havoc for linking, so don't link them.
$linkfile = strpos($row['file'], 'eval') !== false && strpos($row['file'], '?') !== false;
$linkfile = str_contains($row['file'], 'eval') && str_contains($row['file'], '?');

Utils::$context['errors'][$row['id_error']]['file'] = [
'file' => $row['file'],
Expand Down Expand Up @@ -316,7 +316,7 @@ public function view(): void

Utils::$context['filter']['value']['html'] = '<a href="' . Config::$scripturl . '?action=profile;u=' . $id . '">' . (isset(User::$loaded[$id]) ? User::$loaded[$id]->name : Lang::$txt['guest']) . '</a>';
} elseif ($this->filter['variable'] == 'url') {
Utils::$context['filter']['value']['html'] = '\'' . strtr(Utils::htmlspecialchars((substr($this->filter['value']['sql'], 0, 1) == '?' ? Config::$scripturl : '') . $this->filter['value']['sql']), ['\\_' => '_']) . '\'';
Utils::$context['filter']['value']['html'] = '\'' . strtr(Utils::htmlspecialchars((str_starts_with($this->filter['value']['sql'], '?') ? Config::$scripturl : '') . $this->filter['value']['sql']), ['\\_' => '_']) . '\'';
} elseif ($this->filter['variable'] == 'message') {
Utils::$context['filter']['value']['html'] = '\'' . strtr(Utils::htmlspecialchars($this->filter['value']['sql']), ["\n" => '<br>', '&lt;br /&gt;' => '<br>', "\t" => '&nbsp;&nbsp;&nbsp;', '\\_' => '_', '\\%' => '%', '\\\\' => '\\']) . '\'';

Expand Down Expand Up @@ -410,12 +410,12 @@ public function viewFile(): void
if (
$ext != 'php'
|| (
strpos($file, $real_board) === false
&& strpos($file, $real_source) === false
!str_contains($file, $real_board)
&& !str_contains($file, $real_source)
)
|| $basename == strtolower(basename(SMF_SETTINGS_FILE))
|| $basename == strtolower(basename(SMF_SETTINGS_BACKUP_FILE))
|| strpos($file, $real_cache) !== false
|| str_contains($file, $real_cache)
|| !is_readable($file)
) {
ErrorHandler::fatalLang('error_bad_file', true, [Utils::htmlspecialchars($file)]);
Expand Down
2 changes: 1 addition & 1 deletion Sources/Actions/Admin/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ public function profileEdit(): void
'private' => $row['private'],
'can_search' => $row['can_search'],
'mask' => $row['mask'],
'regex' => substr($row['mask'], 0, 5) == 'regex' ? substr($row['mask'], 5) : '',
'regex' => str_starts_with($row['mask'], 'regex') ? substr($row['mask'], 5) : '',
'enclose' => $row['enclose'],
'placement' => $row['placement'],
];
Expand Down
2 changes: 1 addition & 1 deletion Sources/Actions/Admin/Find.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function internal(): void
$name = preg_replace('~<(?:div|span)\sclass="smalltext">.+?</(?:div|span)>~', '', $name);

Utils::$context['search_results'][] = [
'url' => (substr($item[1], 0, 4) == 'area' ? Config::$scripturl . '?action=admin;' . $item[1] : $item[1]) . ';' . Utils::$context['session_var'] . '=' . Utils::$context['session_id'] . ((substr($item[1], 0, 4) == 'area' && $section == 'settings' ? '#' . $item[0][0] : '')),
'url' => (str_starts_with($item[1], 'area') ? Config::$scripturl . '?action=admin;' . $item[1] : $item[1]) . ';' . Utils::$context['session_var'] . '=' . Utils::$context['session_id'] . ((str_starts_with($item[1], 'area') && $section == 'settings' ? '#' . $item[0][0] : '')),
'name' => $name,
'type' => $section,
'help' => Utils::shorten(isset($item[2]) ? strip_tags(Lang::$helptxt[$item[2]]) : (isset(Lang::$helptxt[$found]) ? strip_tags(Lang::$helptxt[$found]) : ''), 255),
Expand Down
10 changes: 5 additions & 5 deletions Sources/Actions/Admin/Languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function download(): void
// Check writable status.
foreach ($_POST['copy_file'] as $file) {
// Check it's not very bad.
if (strpos($file, '..') !== false || (strpos($file, 'Themes') !== 0 && !preg_match('~agreement\.[A-Za-z-_0-9]+\.txt$~', $file))) {
if (str_contains($file, '..') || (!str_starts_with($file, 'Themes') && !preg_match('~agreement\.[A-Za-z-_0-9]+\.txt$~', $file))) {
ErrorHandler::fatal(Lang::$txt['languages_download_illegal_paths']);
}

Expand Down Expand Up @@ -663,7 +663,7 @@ public function editEntries()
Utils::$context['sub_template'] = 'modify_language_entries';

$lang_id = $_GET['lid'];
list($theme_id, $file_id) = empty($_REQUEST['tfid']) || strpos($_REQUEST['tfid'], '+') === false ? [1, ''] : explode('+', $_REQUEST['tfid']);
list($theme_id, $file_id) = empty($_REQUEST['tfid']) || !str_contains($_REQUEST['tfid'], '+') ? [1, ''] : explode('+', $_REQUEST['tfid']);

// Clean the ID - just in case.
preg_match('~([A-Za-z0-9_-]+)~', $lang_id, $matches);
Expand Down Expand Up @@ -1123,9 +1123,9 @@ function ($val1, $val2) {
}

// Clean up some bits.
if (strpos($subValue, '\'') === 0) {
if (str_starts_with($subValue, '\'')) {
$subValue = trim($subValue, '\'');
} elseif (strpos($subValue, '"') === 0) {
} elseif (str_starts_with($subValue, '"')) {
$subValue = trim($subValue, '"');
}

Expand Down Expand Up @@ -1493,7 +1493,7 @@ public static function list_getLanguagesList(): array

foreach ($lang_files as $file) {
// Were we searching?
if (!empty(Utils::$context['smf_search_term']) && strpos($file->fetch('name'), Utils::strtolower(Utils::$context['smf_search_term'])) === false) {
if (!empty(Utils::$context['smf_search_term']) && !str_contains($file->fetch('name'), Utils::strtolower(Utils::$context['smf_search_term']))) {
continue;
}

Expand Down
16 changes: 8 additions & 8 deletions Sources/Actions/Admin/Maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ public function entitiesToUnicode(): void
}

while ($column_info = Db::$db->fetch_assoc($request)) {
if (strpos($column_info['Type'], 'text') !== false || strpos($column_info['Type'], 'char') !== false) {
if (str_contains($column_info['Type'], 'text') || str_contains($column_info['Type'], 'char')) {
$columns[] = strtolower($column_info['Field']);
}
}
Expand Down Expand Up @@ -1205,7 +1205,7 @@ public function entitiesToUnicode(): void
$changes = [];

foreach ($row as $column_name => $column_value) {
if ($column_name !== $primary_key && strpos($column_value, '&#') !== false) {
if ($column_name !== $primary_key && str_contains($column_value, '&#')) {
$changes[] = $column_name . ' = {string:changes_' . $column_name . '}';
$insertion_variables['changes_' . $column_name] = Utils::entityDecode($column_value);
}
Expand Down Expand Up @@ -2113,7 +2113,7 @@ public static function getIntegrationHooksData($start, $per_page, $sort, $filter
$function_list += self::getDefinedFunctionsInFile($absPath_clean);
}

$hook_exists = isset($function_list[$hookParsedData['call']]) || (substr($hook, -8) === '_include' && isset($files[$absPath_clean]));
$hook_exists = isset($function_list[$hookParsedData['call']]) || (str_ends_with($hook, '_include') && isset($files[$absPath_clean]));
$hook_temp = !empty(Utils::$context['integration_hooks_temporary'][$hook][$hookParsedData['rawData']]);
$temp = [
'hook_name' => $hook,
Expand Down Expand Up @@ -2403,30 +2403,30 @@ protected static function parseIntegrationHook(string $hook, string $rawData): a
$modFunc = $rawData;

// Any files?
if (substr($hook, -8) === '_include') {
if (str_ends_with($hook, '_include')) {
$modFunc = $modFunc . '|';
}

if (strpos($modFunc, '|') !== false) {
if (str_contains($modFunc, '|')) {
list($hookData['hookFile'], $modFunc) = explode('|', $modFunc);
$hookData['absPath'] = strtr(strtr(trim($hookData['hookFile']), ['$boarddir' => Config::$boarddir, '$sourcedir' => Config::$sourcedir, '$themedir' => Theme::$current->settings['theme_dir'] ?? '']), '\\', '/');
}

// Hook is an instance.
if (strpos($modFunc, '#') !== false) {
if (str_contains($modFunc, '#')) {
$modFunc = str_replace('#', '', $modFunc);
$hookData['object'] = true;
}

// Hook is "disabled"
// May need to inspect $rawData here for includes...
if ((strpos($modFunc, '!') !== false) || (empty($modFunc) && (strpos($rawData, '!') !== false))) {
if ((str_contains($modFunc, '!')) || (empty($modFunc) && (str_contains($rawData, '!')))) {
$modFunc = str_replace('!', '', $modFunc);
$hookData['enabled'] = false;
}

// Handling methods?
if (strpos($modFunc, '::') !== false) {
if (str_contains($modFunc, '::')) {
list($hookData['class'], $hookData['method']) = explode('::', $modFunc);
$hookData['pureFunc'] = $hookData['method'];
$hookData['call'] = $modFunc;
Expand Down
4 changes: 2 additions & 2 deletions Sources/Actions/Admin/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ public function send(bool $clean_only = false): void
if (!Utils::$context['send_pm']) {
$include_unsubscribe = true;

if (strpos($_POST['message'], '{$member.unsubscribe}') === false) {
if (!str_contains($_POST['message'], '{$member.unsubscribe}')) {
$_POST['message'] .= "\n\n" . '{$member.unsubscribe}';
}
}
Expand Down Expand Up @@ -1242,7 +1242,7 @@ protected function __construct()
$this->subaction = isset($_REQUEST['sa']) && isset(self::$subactions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : (User::$me->allowedTo('edit_news') ? 'editnews' : (User::$me->allowedTo('send_mail') ? 'mailingmembers' : 'settings'));

// Force the right area...
if (substr($this->subaction, 0, 7) == 'mailing') {
if (str_starts_with($this->subaction, 'mailing')) {
Menu::$loaded['admin']['current_subsection'] = 'mailingmembers';
}

Expand Down
Loading
Loading