Skip to content

Commit

Permalink
[BUGFIX] Use strpos() over strstr()
Browse files Browse the repository at this point in the history
This patch replaces all usages of `strstr()` and its friends where the
existence of a certain substring is checked, but the result isn't used at
all with `strpos()`, which uses less memory.

Resolves: #88922
Releases: master
Change-Id: Ie45589ac9410e22e1b48e82dd8086eadb6d74107
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61445
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
  • Loading branch information
andreaskienast authored and NeoBlack committed Aug 23, 2019
1 parent 895d737 commit cde9c8c
Show file tree
Hide file tree
Showing 34 changed files with 65 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ protected function isTableAllowedOnPage(string $table, array $page): bool
$allowedTableList = $GLOBALS['PAGES_TYPES']['default']['allowedTables'];
}
// If all tables or the table is listed as an allowed type, return TRUE
if (strstr($allowedTableList, '*') || GeneralUtility::inList($allowedTableList, $table)) {
if (strpos($allowedTableList, '*') !== false || GeneralUtility::inList($allowedTableList, $table)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ protected function processForeignTableClause(array $result, $foreignTableName, $
) {
$foreignTableClause = $result['processedTca']['columns'][$localFieldName]['config']['foreign_table_where'];
// Replace possible markers in query
if (strstr($foreignTableClause, '###REC_FIELD_')) {
if (strpos($foreignTableClause, '###REC_FIELD_') !== false) {
// " AND table.field='###REC_FIELD_field1###' AND ..." -> array(" AND table.field='", "field1###' AND ...")
$whereClauseParts = explode('###REC_FIELD_', $foreignTableClause);
foreach ($whereClauseParts as $key => $value) {
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/backend/Classes/Utility/BackendUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -2009,7 +2009,7 @@ public static function getProcessedValue(
}
}
// If this field is a password field, then hide the password by changing it to a random number of asterisk (*)
if (!empty($theColConf['eval']) && stristr($theColConf['eval'], 'password')) {
if (!empty($theColConf['eval']) && stripos($theColConf['eval'], 'password') !== false) {
$l = '';
$randomNumber = rand(5, 12);
for ($i = 0; $i < $randomNumber; $i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ public function modAccess($conf)
}
// If $conf['access'] is set but not with 'admin' then we return TRUE, if the module is found in the modList
$acs = false;
if (!strstr($conf['access'], 'admin') && $conf['name']) {
if (strpos($conf['access'], 'admin') === false && $conf['name']) {
$acs = $this->check('modules', $conf['name']);
}
if (!$acs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ protected static function initializeBasicErrorReporting()
protected static function getTypo3Os()
{
$typoOs = '';
if (!stristr(PHP_OS, 'darwin') && !stristr(PHP_OS, 'cygwin') && stristr(PHP_OS, 'win')) {
if (stripos(PHP_OS, 'darwin') === false && stripos(PHP_OS, 'cygwin') === false && stripos(PHP_OS, 'win') !== false) {
$typoOs = 'WIN';
}
return $typoOs;
Expand Down
8 changes: 4 additions & 4 deletions typo3/sysext/core/Classes/Database/QueryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ public function makeOptionList($fieldName, $conf, $table)
}
}
}
if (stristr($fieldSetup['allowed'], ',')) {
if (strpos($fieldSetup['allowed'], ',') !== false) {
$from_table_Arr = explode(',', $fieldSetup['allowed']);
$useTablePrefix = 1;
if (!$fieldSetup['prepend_tname']) {
Expand All @@ -835,16 +835,16 @@ public function makeOptionList($fieldName, $conf, $table)
->from($table)
->execute();
while ($row = $statement->fetch()) {
if (stristr($row[$fieldName], ',')) {
if (strpos($row[$fieldName], ',') !== false) {
$checkContent = explode(',', $row[$fieldName]);
foreach ($checkContent as $singleValue) {
if (!stristr($singleValue, '_')) {
if (strpos($singleValue, '_') === false) {
$dontPrefixFirstTable = 1;
}
}
} else {
$singleValue = $row[$fieldName];
if ($singleValue !== '' && !stristr($singleValue, '_')) {
if ($singleValue !== '' && strpos($singleValue, '_') === false) {
$dontPrefixFirstTable = 1;
}
}
Expand Down
8 changes: 4 additions & 4 deletions typo3/sysext/core/Classes/Database/QueryView.php
Original file line number Diff line number Diff line change
Expand Up @@ -981,24 +981,24 @@ public function makeValueList($fieldName, $fieldValue, $conf, $table, $splitStri
}
}
}
if (stristr($fieldSetup['allowed'], ',')) {
if (strpos($fieldSetup['allowed'], ',') !== false) {
$from_table_Arr = explode(',', $fieldSetup['allowed']);
$useTablePrefix = 1;
if (!$fieldSetup['prepend_tname']) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$statement = $queryBuilder->select($fieldName)->from($table)->execute();
while ($row = $statement->fetch()) {
if (stristr($row[$fieldName], ',')) {
if (strpos($row[$fieldName], ',') !== false) {
$checkContent = explode(',', $row[$fieldName]);
foreach ($checkContent as $singleValue) {
if (!stristr($singleValue, '_')) {
if (strpos($singleValue, '_') === false) {
$dontPrefixFirstTable = 1;
}
}
} else {
$singleValue = $row[$fieldName];
if ($singleValue !== '' && !stristr($singleValue, '_')) {
if ($singleValue !== '' && strpos($singleValue, '_') === false) {
$dontPrefixFirstTable = 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/core/Classes/Database/ReferenceIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ public function setReferenceValue_softreferences($refRec, $softref, $newValue, &
}
}
// Set in data array:
if (!strstr($softref['tokenizedContent'], '{softref:')) {
if (strpos($softref['tokenizedContent'], '{softref:') === false) {
if ($flexPointer) {
$flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
$dataArray[$refRec['tablename']][$refRec['recuid']][$refRec['field']]['data'] = [];
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/core/Classes/Database/SoftReferenceIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public function getTypoLinkParts($typolinkValue)
$pU = @parse_url($link_param);

// If it's a mail address:
if (strstr($link_param, '@') && !$pU['scheme']) {
if (strpos($link_param, '@') !== false && !$pU['scheme']) {
$link_param = preg_replace('/^mailto:/i', '', $link_param);
$finalTagParts['LINK_TYPE'] = 'mailto';
$finalTagParts['url'] = trim($link_param);
Expand Down
12 changes: 4 additions & 8 deletions typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1947,12 +1947,12 @@ public function convertColor($string)
$cParts = explode(':', $string, 2);
// Finding the RGB definitions of the color:
$string = $cParts[0];
if (strstr($string, '#')) {
if (strpos($string, '#') !== false) {
$string = preg_replace('/[^A-Fa-f0-9]*/', '', $string);
$col[] = hexdec(substr($string, 0, 2));
$col[] = hexdec(substr($string, 2, 2));
$col[] = hexdec(substr($string, 4, 2));
} elseif (strstr($string, ',')) {
} elseif (strpos($string, ',') !== false) {
$string = preg_replace('/[^,0-9]*/', '', $string);
$strArr = explode(',', $string);
$col[] = (int)$strArr[0];
Expand Down Expand Up @@ -2283,12 +2283,8 @@ protected function generateStatusHashForImageFile($filePath)
*/
public function getImageScale($info, $w, $h, $options)
{
if (strstr($w . $h, 'm')) {
$max = 1;
} else {
$max = 0;
}
if (strstr($w . $h, 'c')) {
$max = strpos($w . $h, 'm') !== false ? 1 : 0;
if (strpos($w . $h, 'c') !== false) {
$out['cropH'] = (int)substr(strstr($w, 'c'), 1);
$out['cropV'] = (int)substr(strstr($h, 'c'), 1);
$crs = true;
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/core/Classes/LinkHandling/LinkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function resolveByStringRepresentation(string $urn): array
if ($fragment) {
$result['fragment'] = $fragment;
}
} elseif (stripos($urn, '://') && $this->handlers[self::TYPE_URL]) {
} elseif (strpos($urn, '://') && $this->handlers[self::TYPE_URL]) {
$result = $this->handlers[self::TYPE_URL]->resolveHandlerData(['url' => $urn]);
$result['type'] = self::TYPE_URL;
} elseif (stripos($urn, 'mailto:') === 0 && $this->handlers[self::TYPE_EMAIL]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,15 +577,15 @@ public function ext_getSearchKeys($arr, $depth_in, $searchString, $keyArray)
}
} else {
// The value has matched
if (stristr($arr[$key], $searchString)) {
if (stripos($arr[$key], $searchString) !== false) {
$this->tsbrowser_searchKeys[$depth] += 2;
}
// The key has matches
if (stristr($key, $searchString)) {
if (stripos($key, $searchString) !== false) {
$this->tsbrowser_searchKeys[$depth] += 4;
}
// Just open this subtree if the parent key has matched the search
if (stristr($depth_in, $searchString)) {
if (stripos($depth_in, $searchString) !== false) {
$this->tsbrowser_searchKeys[$depth] = 1;
}
}
Expand Down Expand Up @@ -920,7 +920,7 @@ public function ext_compareFlatSetups($default)
}
$parts = explode(';', $line);
foreach ($parts as $par) {
if (strstr($par, '=')) {
if (strpos($par, '=') !== false) {
$keyValPair = explode('=', $par, 2);
switch (trim(strtolower($keyValPair[0]))) {
case 'type':
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/core/Classes/TypoScript/TypoScriptService.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function explodeConfigurationForOptionSplit(array $originalConfiguration,
}
} elseif (is_string($val)) {
// Splitting of all values on this level of the TypoScript object tree:
if ($cKey === 'noTrimWrap' || (!strstr($val, '|*|') && !strstr($val, '||'))) {
if ($cKey === 'noTrimWrap' || (strpos($val, '|*|') === false && strpos($val, '||') === false)) {
for ($aKey = 0; $aKey < $splitCount; $aKey++) {
$finalConfiguration[$aKey][$cKey] = $val;
}
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/core/Classes/Utility/VersionNumberUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function splitVersionRange($version)
trigger_error('Method ' . __METHOD__ . ' will be removed in TYPO3 11.0', E_USER_DEPRECATED);

$versionRange = [];
if (strstr($version, '-')) {
if (strpos($version, '-') !== false) {
$versionRange = explode('-', $version, 2);
} else {
$versionRange[0] = $version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ public function getImageSourceCollection($layoutKey, $conf, $file)
);
}
if ($dimension) {
if (strstr($dimension, 'c') !== false && ($dimensionKey === 'width' || $dimensionKey === 'height')) {
if (strpos($dimension, 'c') !== false && ($dimensionKey === 'width' || $dimensionKey === 'height')) {
$dimensionParts = explode('c', $dimension, 2);
$dimension = ((int)$dimensionParts[0] * $pixelDensity) . 'c';
if ($dimensionParts[1]) {
Expand Down Expand Up @@ -3850,7 +3850,7 @@ public function _parseFunc($theValue, $conf)
$newstring .= $pieces[0];
$match_len = strlen($data) - (strlen($pieces[0]) + strlen($pieces[1]));
$inTag = false;
if (strstr($pieces[0], '<') || strstr($pieces[0], '>')) {
if (strpos($pieces[0], '<') !== false || strpos($pieces[0], '>') !== false) {
// Returns TRUE, if a '<' is closer to the string-end than '>'.
// This is the case if we're INSIDE a tag (that could have been
// made by makelinks...) and we must secure, that the inside of a tag is
Expand Down Expand Up @@ -4130,14 +4130,14 @@ public function http_makelinks($data, $conf)
$keep = $conf['keep'];
$linkParts = parse_url($scheme . $parts[0]);
$linktxt = '';
if (strstr($keep, 'scheme')) {
if (strpos($keep, 'scheme') !== false) {
$linktxt = $scheme;
}
$linktxt .= $linkParts['host'];
if (strstr($keep, 'path')) {
if (strpos($keep, 'path') !== false) {
$linktxt .= $linkParts['path'];
// Added $linkParts['query'] 3/12
if (strstr($keep, 'query') && $linkParts['query']) {
if (strpos($keep, 'query') !== false && $linkParts['query']) {
$linktxt .= '?' . $linkParts['query'];
} elseif ($linkParts['path'] === '/') {
$linktxt = substr($linktxt, 0, -1);
Expand Down Expand Up @@ -4475,7 +4475,7 @@ protected function createCropAreaFromJsonString(string $cropSettings, string $cr
*/
public function getFieldVal($field)
{
if (!strstr($field, '//')) {
if (strpos($field, '//') === false) {
return $this->data[trim($field)] ?? null;
}
$sections = GeneralUtility::trimExplode('//', $field, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ public function render($conf = [])
// Getting marks
if (is_array($conf['marks.'])) {
foreach ($conf['marks.'] as $theKey => $theValue) {
if (!strstr($theKey, '.')) {
if (strpos($theKey, '.') === false) {
$content = str_replace($PRE . $theKey . $POST, $this->cObj->cObjGetSingle($theValue, $conf['marks.'][$theKey . '.'], 'marks.' . $theKey), $content);
}
}
}
// Getting subparts.
if (is_array($conf['subparts.'])) {
foreach ($conf['subparts.'] as $theKey => $theValue) {
if (!strstr($theKey, '.')) {
if (strpos($theKey, '.') === false) {
$subpart = $this->templateService->getSubpart($content, $PRE . $theKey . $POST);
if ($subpart) {
$this->cObj->setCurrentVal($subpart);
Expand All @@ -96,7 +96,7 @@ public function render($conf = [])
// Getting subpart wraps
if (is_array($conf['wraps.'])) {
foreach ($conf['wraps.'] as $theKey => $theValue) {
if (!strstr($theKey, '.')) {
if (strpos($theKey, '.') === false) {
$subpart = $this->templateService->getSubpart($content, $PRE . $theKey . $POST);
if ($subpart) {
$this->cObj->setCurrentVal($subpart);
Expand All @@ -110,7 +110,7 @@ public function render($conf = [])
// Getting subparts.
if (is_array($conf['subparts.'])) {
foreach ($conf['subparts.'] as $theKey => $theValue) {
if (!strstr($theKey, '.')) {
if (strpos($theKey, '.') === false) {
$subpart = $this->templateService->getSubpart($content, $PRE . $theKey . $POST);
if ($subpart) {
$GLOBALS['TSFE']->register['SUBPART_' . $theKey] = $subpart;
Expand All @@ -123,7 +123,7 @@ public function render($conf = [])
// Getting marks
if (is_array($conf['marks.'])) {
foreach ($conf['marks.'] as $theKey => $theValue) {
if (!strstr($theKey, '.')) {
if (strpos($theKey, '.') === false) {
$marks[$theKey]['name'] = $theValue;
$marks[$theKey]['conf'] = $conf['marks.'][$theKey . '.'];
}
Expand All @@ -132,7 +132,7 @@ public function render($conf = [])
// Getting subpart wraps
if (is_array($conf['wraps.'])) {
foreach ($conf['wraps.'] as $theKey => $theValue) {
if (!strstr($theKey, '.')) {
if (strpos($theKey, '.') === false) {
$wraps[$theKey]['name'] = $theValue;
$wraps[$theKey]['conf'] = $conf['wraps.'][$theKey . '.'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,7 @@ public function main()
*/
protected function processImage()
{
if (strstr($this->width . $this->height, 'm')) {
$max = 'm';
} else {
$max = '';
}
$max = strpos($this->width . $this->height, 'm') !== false ? 'm' : '';
$this->height = MathUtility::forceIntegerInRange($this->height, 0);
$this->width = MathUtility::forceIntegerInRange($this->width, 0) . $max;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2403,19 +2403,19 @@ protected function isAllowedLinkVarValue(string $haystack, string $needle): bool
if (@preg_match($needle, $haystack)) {
$isAllowed = true;
}
} elseif (strstr($needle, '-')) {
} elseif (strpos($needle, '-') !== false) {
// Range
if (MathUtility::canBeInterpretedAsInteger($haystack)) {
$range = explode('-', $needle);
if ($range[0] <= $haystack && $range[1] >= $haystack) {
$isAllowed = true;
}
}
} elseif (strstr($needle, '|')) {
} elseif (strpos($needle, '|') !== false) {
// List
// Trim the input
$haystack = str_replace(' ', '', $haystack);
if (strstr('|' . $needle . '|', '|' . $haystack . '|')) {
if (strpos('|' . $needle . '|', '|' . $haystack . '|') !== false) {
$isAllowed = true;
}
} elseif ((string)$needle === (string)$haystack) {
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/impexp/Classes/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ public function getNextFilePart($fd, $unserialize = false, $name = '')
return null;
}
$initStrDat = explode(':', $initStr);
if (strstr($initStrDat[0], 'Warning')) {
if (strpos($initStrDat[0], 'Warning') !== false) {
$this->error('File read error: Warning message in file. (' . $initStr . fgets($fd) . ')');
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/impexp/Classes/ImportExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ public function checkDokType($checkTable, $doktype)
$allowedTableList = $GLOBALS['PAGES_TYPES'][$doktype]['allowedTables'] ?? $GLOBALS['PAGES_TYPES']['default']['allowedTables'];
$allowedArray = GeneralUtility::trimExplode(',', $allowedTableList, true);
// If all tables or the table is listed as an allowed type, return TRUE
if (strstr($allowedTableList, '*') || in_array($checkTable, $allowedArray)) {
if (strpos($allowedTableList, '*') !== false || in_array($checkTable, $allowedArray)) {
return true;
}
return false;
Expand Down
Loading

0 comments on commit cde9c8c

Please sign in to comment.