Commit f5ec7087 authored by Wouter Wolters's avatar Wouter Wolters Committed by Christian Kuhn
Browse files

[TASK] Replace substr(...) with strpos(...)

strpos will use only CPU resources, substr also needs memory allocation.

Resolves: #85477
Releases: master
Change-Id: Ic25c99986f7e2e7237c10acaa75be8d0f6289f13
Reviewed-on: https://review.typo3.org/57466

Reviewed-by: Jigal van Hemert's avatarJigal van Hemert <jigal.van.hemert@typo3.org>
Tested-by: Jigal van Hemert's avatarJigal van Hemert <jigal.van.hemert@typo3.org>
Tested-by: default avatarTYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
parent c5f567c3
......@@ -723,7 +723,7 @@ class NewRecordController
$_EXTKEY = '';
if ($nameParts[0] === 'tx' || $nameParts[0] === 'tt') {
// Try to extract extension name
if (substr($v['ctrl']['title'], 0, 8) === 'LLL:EXT:') {
if (strpos($v['ctrl']['title'], 'LLL:EXT:') === 0) {
$_EXTKEY = substr($v['ctrl']['title'], 8);
$_EXTKEY = substr($_EXTKEY, 0, strpos($_EXTKEY, '/'));
if ($_EXTKEY !== '') {
......
......@@ -158,7 +158,7 @@ class AddController extends AbstractWizardController
is_array($record) ? $record : ['pid' => $this->P['pid']]
);
// Set [params][pid]
if (substr($this->P['params']['pid'], 0, 3) === '###' && substr($this->P['params']['pid'], -3) === '###') {
if (strpos($this->P['params']['pid'], '###') === 0 && substr($this->P['params']['pid'], -3) === '###') {
$keyword = substr($this->P['params']['pid'], 3, -3);
if (strpos($keyword, 'PAGE_TSCONFIG_') === 0) {
$this->pid = (int)$TSconfig[$this->P['field']][$keyword];
......
......@@ -123,7 +123,7 @@ class ListController extends AbstractWizardController
is_array($origRow) ? $origRow : ['pid' => $this->P['pid']]
);
// Set [params][pid]
if (substr($this->P['params']['pid'], 0, 3) === '###' && substr($this->P['params']['pid'], -3) === '###') {
if (strpos($this->P['params']['pid'], '###') === 0 && substr($this->P['params']['pid'], -3) === '###') {
$keyword = substr($this->P['params']['pid'], 3, -3);
if (strpos($keyword, 'PAGE_TSCONFIG_') === 0) {
$this->pid = (int)$TSconfig[$this->P['field']][$keyword];
......
......@@ -259,14 +259,14 @@ class TableManualRepository
if ($itemValue) {
$reference = GeneralUtility::trimExplode(':', $itemValue);
$referenceUrl = GeneralUtility::trimExplode('|', $itemValue);
if (substr($referenceUrl[1], 0, 4) === 'http') {
if (strpos($referenceUrl[1], 'http') === 0) {
// URL reference
$lines[] = [
'url' => $referenceUrl[1],
'title' => $referenceUrl[0],
'target' => '_blank'
];
} elseif (substr($referenceUrl[1], 0, 5) === 'FILE:') {
} elseif (strpos($referenceUrl[1], 'FILE:') === 0) {
// File reference
$fileName = GeneralUtility::getFileAbsFileName(substr($referenceUrl[1], 5));
if ($fileName && @is_file($fileName)) {
......
......@@ -50,7 +50,7 @@ class FullRecordContainer extends AbstractContainer
// Streamline the fields array
// First, make sure there is always a --div-- definition for the first element
if (substr($fieldsArray[0], 0, 7) !== '--div--') {
if (strpos($fieldsArray[0], '--div--') !== 0) {
array_unshift($fieldsArray, '--div--;LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.generalTab');
}
// If first tab has no label definition, add "general" label
......@@ -61,7 +61,7 @@ class FullRecordContainer extends AbstractContainer
// If there are at least two --div-- definitions, inner container will be a TabContainer, else a NoTabContainer
$tabCount = 0;
foreach ($fieldsArray as $field) {
if (substr($field, 0, 7) === '--div--') {
if (strpos($field, '--div--') === 0) {
$tabCount++;
}
}
......
......@@ -399,7 +399,7 @@ class InlineRecordContainer extends AbstractContainer
'localize' => '',
'locked' => '',
];
$isNewItem = substr($rec['uid'], 0, 3) === 'NEW';
$isNewItem = strpos($rec['uid'], 'NEW') === 0;
$isParentExisting = MathUtility::canBeInterpretedAsInteger($data['inlineParentUid']);
$tcaTableCtrl = $GLOBALS['TCA'][$foreignTable]['ctrl'];
$tcaTableCols = $GLOBALS['TCA'][$foreignTable]['columns'];
......
......@@ -252,7 +252,7 @@ class PagePositionMap
$code = '<ul class="list-tree">';
foreach ($lines as $line) {
if ((substr($line, 0, 3) === '<ul') || (substr($line, 0, 4) === '</ul')) {
if ((strpos($line, '<ul') === 0) || (strpos($line, '</ul') === 0)) {
$code .= $line;
} else {
$code .= '<li>' . $line . '</li>';
......
......@@ -558,7 +558,7 @@ class BackendUtility
}
$output = $fullOutput = '/';
$clause = trim($clause);
if ($clause !== '' && substr($clause, 0, 3) !== 'AND') {
if ($clause !== '' && strpos($clause, 'AND') !== 0) {
$clause = 'AND ' . $clause;
}
$data = self::BEgetRootLine($uid, $clause);
......
......@@ -807,7 +807,7 @@ class PageLayoutView implements LoggerAwareInterface
$theData[$field] = '&nbsp;<strong>ID</strong>';
break;
default:
if (substr($field, 0, 6) === 'table_') {
if (strpos($field, 'table_') === 0) {
$f2 = substr($field, 6);
if ($GLOBALS['TCA'][$f2]) {
$theData[$field] = '&nbsp;' .
......@@ -1816,7 +1816,7 @@ class PageLayoutView implements LoggerAwareInterface
}
break;
default:
if (substr($field, 0, 6) === 'table_') {
if (strpos($field, 'table_') === 0) {
$f2 = substr($field, 6);
if ($GLOBALS['TCA'][$f2]) {
$c = $this->numberOfRecords($f2, $row['uid']);
......
......@@ -134,7 +134,7 @@ class LogEntryRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
return;
}
// Constraint for a group
if (substr($userOrGroup, 0, 3) === 'gr-') {
if (strpos($userOrGroup, 'gr-') === 0) {
$groupId = (int)substr($userOrGroup, 3);
$userIds = [];
foreach ($this->beUserList as $userId => $userData) {
......@@ -148,7 +148,7 @@ class LogEntryRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
// If there are no group members -> use -1 as constraint to not find anything
$queryConstraints[] = $query->in('userid', [-1]);
}
} elseif (substr($userOrGroup, 0, 3) === 'us-') {
} elseif (strpos($userOrGroup, 'us-') === 0) {
$queryConstraints[] = $query->equals('userid', (int)substr($userOrGroup, 3));
} elseif ($userOrGroup === '-1') {
$queryConstraints[] = $query->equals('userid', (int)$GLOBALS['BE_USER']->user['uid']);
......
......@@ -632,7 +632,7 @@ abstract class AbstractUserAuthentication implements LoggerAwareInterface
}
// Refuse login for _CLI users, if not processing a CLI request type
// (although we shouldn't be here in case of a CLI request type)
if (strtoupper(substr($loginData['uname'], 0, 5)) === '_CLI_' && !Environment::isCli()) {
if (stripos($loginData['uname'], '_CLI_') === 0 && !Environment::isCli()) {
throw new \RuntimeException('TYPO3 Fatal Error: You have tried to login using a CLI user. Access prohibited!', 1270853931);
}
}
......
......@@ -165,11 +165,11 @@ class MemcachedBackend extends AbstractBackend implements TaggableBackendInterfa
$this->memcache = new $memcachedPlugin;
$defaultPort = $this->usedPeclModule === 'memcache' ? ini_get('memcache.default_port') : 11211;
foreach ($this->servers as $server) {
if (substr($server, 0, 7) === 'unix://') {
if (strpos($server, 'unix://') === 0) {
$host = $server;
$port = 0;
} else {
if (substr($server, 0, 6) === 'tcp://') {
if (strpos($server, 'tcp://') === 0) {
$server = substr($server, 6);
}
if (strpos($server, ':') !== false) {
......@@ -290,7 +290,7 @@ class MemcachedBackend extends AbstractBackend implements TaggableBackendInterfa
public function get($entryIdentifier)
{
$value = $this->memcache->get($this->identifierPrefix . $entryIdentifier);
if (is_string($value) && substr($value, 0, 14) === 'TYPO3*chunked:') {
if (is_string($value) && strpos($value, 'TYPO3*chunked:') === 0) {
list(, $chunkCount) = explode(':', $value);
$value = '';
for ($chunkNumber = 1; $chunkNumber < $chunkCount; $chunkNumber++) {
......
......@@ -256,7 +256,7 @@ class PdoBackend extends AbstractBackend implements TaggableBackendInterface
$this->databaseHandle = GeneralUtility::makeInstance(\PDO::class, $this->dataSourceName, $this->username, $this->password);
}
$this->databaseHandle->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
if (substr($this->pdoDriver, 0, 5) === 'mysql') {
if (strpos($this->pdoDriver, 'mysql') === 0) {
$this->databaseHandle->exec('SET SESSION sql_mode=\'ANSI\';');
}
} catch (\PDOException $e) {
......@@ -298,7 +298,7 @@ class PdoBackend extends AbstractBackend implements TaggableBackendInterface
{
$sql = file($pathAndFilename, FILE_IGNORE_NEW_LINES & FILE_SKIP_EMPTY_LINES);
// Remove MySQL style key length delimiters (yuck!) if we are not setting up a MySQL db
if (substr($pdoDriver, 0, 5) !== 'mysql') {
if (strpos($pdoDriver, 'mysql') !== 0) {
$sql = preg_replace('/"\\([0-9]+\\)/', '"', $sql);
}
$statement = '';
......
......@@ -164,7 +164,7 @@ class SimpleFileBackend extends AbstractBackend implements PhpCapableBackendInte
$documentRoot = '';
}
if (Environment::isWindows()) {
if (substr($cacheDirectory, 0, strlen($documentRoot)) === $documentRoot) {
if (strpos($cacheDirectory, $documentRoot) === 0) {
$documentRoot = '';
}
}
......
......@@ -69,7 +69,7 @@ class LoadedExtensionArrayElement implements \IteratorAggregate, \ArrayAccess, \
$pathSite = Environment::getPublicPath() . '/';
$pathSiteLength = strlen($pathSite);
$absolutePackagePath = $this->package->getPackagePath();
if (substr($absolutePackagePath, 0, $pathSiteLength) === $pathSite) {
if (strpos($absolutePackagePath, $pathSite) === 0) {
$relativePackagePathToPathSite = substr($absolutePackagePath, $pathSiteLength);
$relativePackagePathToPathSiteSegments = explode('/', $relativePackagePathToPathSite);
$packageType = null;
......
......@@ -151,7 +151,7 @@ class YamlFileLoader
*/
protected function isPlaceholder($value): bool
{
return is_string($value) && substr($value, 0, 1) === '%' && substr($value, -1) === '%';
return is_string($value) && strpos($value, '%') === 0 && substr($value, -1) === '%';
}
/**
......
......@@ -38,7 +38,7 @@ class PdoHelper
trigger_error('Class will be removed in TYPO3 v10', E_USER_DEPRECATED);
$sql = file($pathAndFilename, FILE_IGNORE_NEW_LINES & FILE_SKIP_EMPTY_LINES);
// Remove MySQL style key length delimiters (yuck!) if we are not setting up a MySQL db
if (substr($pdoDriver, 0, 5) !== 'mysql') {
if (strpos($pdoDriver, 'mysql') !== 0) {
$sql = preg_replace('/"\\([0-9]+\\)/', '"', $sql);
}
$statement = '';
......
......@@ -555,7 +555,7 @@ class QueryGenerator
// Traverse:
foreach ($queryConfig as $key => $conf) {
$fieldName = '';
if (substr($conf['type'], 0, 6) === 'FIELD_') {
if (strpos($conf['type'], 'FIELD_') === 0) {
$fieldName = substr($conf['type'], 6);
$fieldType = $this->fields[$fieldName]['type'];
} elseif ($conf['type'] === 'newlevel') {
......@@ -610,7 +610,7 @@ class QueryGenerator
$subscript = $parent . '[' . $key . ']';
$lineHTML = [];
$lineHTML[] = $this->mkOperatorSelect($this->name . $subscript, $conf['operator'], $c, $conf['type'] !== 'FIELD_');
if (substr($conf['type'], 0, 6) === 'FIELD_') {
if (strpos($conf['type'], 'FIELD_') === 0) {
$fieldName = substr($conf['type'], 6);
$this->fieldName = $fieldName;
$fieldType = $this->fields[$fieldName]['type'];
......@@ -811,7 +811,7 @@ class QueryGenerator
}
if ($fieldSetup['type'] === 'multiple') {
foreach ($fieldSetup['items'] as $key => $val) {
if (substr($val[0], 0, 4) === 'LLL:') {
if (strpos($val[0], 'LLL:') === 0) {
$value = $languageService->sL($val[0]);
} else {
$value = $val[0];
......@@ -825,7 +825,7 @@ class QueryGenerator
}
if ($fieldSetup['type'] === 'binary') {
foreach ($fieldSetup['items'] as $key => $val) {
if (substr($val[0], 0, 4) === 'LLL:') {
if (strpos($val[0], 'LLL:') === 0) {
$value = $languageService->sL($val[0]);
} else {
$value = $val[0];
......@@ -842,7 +842,7 @@ class QueryGenerator
$dontPrefixFirstTable = 0;
if ($fieldSetup['items']) {
foreach ($fieldSetup['items'] as $key => $val) {
if (substr($val[0], 0, 4) === 'LLL:') {
if (strpos($val[0], 'LLL:') === 0) {
$value = $languageService->sL($val[0]);
} else {
$value = $val[0];
......@@ -906,7 +906,7 @@ class QueryGenerator
$altLabelField = $GLOBALS['TCA'][$from_table]['ctrl']['label_alt'];
if ($GLOBALS['TCA'][$from_table]['columns'][$labelField]['config']['items']) {
foreach ($GLOBALS['TCA'][$from_table]['columns'][$labelField]['config']['items'] as $labelArray) {
if (substr($labelArray[0], 0, 4) === 'LLL:') {
if (strpos($labelArray[0], 'LLL:') === 0) {
$labelFieldSelect[$labelArray[1]] = $languageService->sL($labelArray[0]);
} else {
$labelFieldSelect[$labelArray[1]] = $labelArray[0];
......@@ -917,7 +917,7 @@ class QueryGenerator
$altLabelFieldSelect = [];
if ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items']) {
foreach ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items'] as $altLabelArray) {
if (substr($altLabelArray[0], 0, 4) === 'LLL:') {
if (strpos($altLabelArray[0], 'LLL:') === 0) {
$altLabelFieldSelect[$altLabelArray[1]] = $languageService->sL($altLabelArray[0]);
} else {
$altLabelFieldSelect[$altLabelArray[1]] = $altLabelArray[0];
......
......@@ -957,7 +957,7 @@ class QueryView
}
if ($fieldSetup['type'] === 'multiple') {
foreach ($fieldSetup['items'] as $key => $val) {
if (substr($val[0], 0, 4) === 'LLL:') {
if (strpos($val[0], 'LLL:') === 0) {
$value = $this->languageService->sL($val[0]);
} else {
$value = $val[0];
......@@ -972,7 +972,7 @@ class QueryView
}
if ($fieldSetup['type'] === 'binary') {
foreach ($fieldSetup['items'] as $Key => $val) {
if (substr($val[0], 0, 4) === 'LLL:') {
if (strpos($val[0], 'LLL:') === 0) {
$value = $this->languageService->sL($val[0]);
} else {
$value = $val[0];
......@@ -988,7 +988,7 @@ class QueryView
$useTablePrefix = 0;
if ($fieldSetup['items']) {
foreach ($fieldSetup['items'] as $key => $val) {
if (substr($val[0], 0, 4) === 'LLL:') {
if (strpos($val[0], 'LLL:') === 0) {
$value = $this->languageService->sL($val[0]);
} else {
$value = $val[0];
......@@ -1049,7 +1049,7 @@ class QueryView
if ($GLOBALS['TCA'][$from_table]['columns'][$labelField]['config']['items']) {
$items = $GLOBALS['TCA'][$from_table]['columns'][$labelField]['config']['items'];
foreach ($items as $labelArray) {
if (substr($labelArray[0], 0, 4) === 'LLL:') {
if (strpos($labelArray[0], 'LLL:') === 0) {
$labelFieldSelect[$labelArray[1]] = $this->languageService->sL($labelArray[0]);
} else {
$labelFieldSelect[$labelArray[1]] = $labelArray[0];
......@@ -1061,7 +1061,7 @@ class QueryView
if ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items']) {
$items = $GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items'];
foreach ($items as $altLabelArray) {
if (substr($altLabelArray[0], 0, 4) === 'LLL:') {
if (strpos($altLabelArray[0], 'LLL:') === 0) {
$altLabelFieldSelect[$altLabelArray[1]] = $this->languageService->sL($altLabelArray[0]);
} else {
$altLabelFieldSelect[$altLabelArray[1]] = $altLabelArray[0];
......
......@@ -514,7 +514,7 @@ class RteHtmlParser extends HtmlParser implements LoggerAwareInterface
list($attribArray) = $this->get_tag_attributes($v, true);
$absoluteUrl = trim($attribArray['src']);
// Transform the src attribute into an absolute url, if it not already
if (strtolower(substr($absoluteUrl, 0, 4)) !== 'http') {
if (stripos($absoluteUrl, 'http') !== 0) {
// If site is in a subpath (eg. /~user_jim/) this path needs to be removed because it will be added with $siteUrl
$attribArray['src'] = preg_replace('#^' . preg_quote($sitePath, '#') . '#', '', $attribArray['src']);
$attribArray['src'] = $siteUrl . $attribArray['src'];
......@@ -995,8 +995,8 @@ class RteHtmlParser extends HtmlParser implements LoggerAwareInterface
// Wrapping the line in <p> tags if not already wrapped and does not contain an hr tag
if (!preg_match('/<(hr)(\\s[^>\\/]*)?[[:space:]]*\\/?>/i', $parts[$k])) {
$testStr = strtolower(trim($parts[$k]));
if (substr($testStr, 0, 4) !== '<div' || substr($testStr, -6) !== '</div>') {
if (substr($testStr, 0, 2) !== '<p' || substr($testStr, -4) !== '</p>') {
if (strpos($testStr, '<div') !== 0 || substr($testStr, -6) !== '</div>') {
if (strpos($testStr, '<p') !== 0 || substr($testStr, -4) !== '</p>') {
// Only set p-tags if there is not already div or p tags:
$parts[$k] = '<p>' . $parts[$k] . '</p>';
}
......@@ -1107,7 +1107,7 @@ class RteHtmlParser extends HtmlParser implements LoggerAwareInterface
{
$info = [];
$url = trim($url);
if (substr(strtolower($url), 0, 7) === 'mailto:') {
if (strpos(strtolower($url), 'mailto:') === 0) {
$info['url'] = trim(substr($url, 7));
$info['type'] = 'email';
} elseif (strpos($url, '?file:') !== false) {
......@@ -1129,7 +1129,7 @@ class RteHtmlParser extends HtmlParser implements LoggerAwareInterface
$siteUrl_parts = parse_url($url);
$curUrl_parts = parse_url($curURL);
// Hosts should match
if ($siteUrl_parts['host'] == $curUrl_parts['host'] && (!$info['relScriptPath'] || defined('TYPO3_mainDir') && substr($info['relScriptPath'], 0, strlen(TYPO3_mainDir)) == TYPO3_mainDir)) {
if ($siteUrl_parts['host'] == $curUrl_parts['host'] && (!$info['relScriptPath'] || defined('TYPO3_mainDir') && strpos($info['relScriptPath'], TYPO3_mainDir) === 0)) {
// If the script path seems to match or is empty (FE-EDIT)
// New processing order 100502
$uP = parse_url($info['relUrl']);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment