modxcms / SimpleSearch

A simple search component for MODx Revolution
http://rtfm.modx.com/extras/revo/simplesearch
9 stars 19 forks source link

Fatal error related to strpos usage #57

Open Mark-H opened 1 year ago

Mark-H commented 1 year ago

Found this on a client MODX 2.8.4 / PHP 8.0 / SimpleSearch 2.1.2 site's php error log going back many months:

PHP Fatal error:  Uncaught ValueError: strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack) in /www/core/components/simplesearch/model/simplesearch/simplesearch.class.php:437
Stack trace:
#0 /www/core/components/simplesearch/model/simplesearch/simplesearch.class.php(437): strpos()
#1 /www/core/cache/includes/elements/modsnippet/2.include.cache.php(91): SimpleSearch->createExtract()
#2 /www/core/model/modx/modscript.class.php(76): include('...')
#3 /www/core/model/modx/modparser.class.php(537): modScript->process()
#4 /www/core/components/pdotools/model/pdotools/pdoparser.class.php(273): modParser->processTag()
#5 /www/core/model/modx/modparser.class.php(251): pdoParser->processTag()
#6 /www/core/components/pdotools/model/pdotools/pdoparser.class.php(64): modParser->processElementTags()
#7 /www/core/model/modx/modresource.class.php(546): pdoParser->processElementTags()
#8 /www/core/model/modx/modresource.class.php(498): modResource->parseContent()
#9 /www/core/model/modx/modresponse.class.php(62): modResource->prepare()
#10 /www/core/model/modx/modrequest.class.php(138): modResponse->outputContent()
#11 /www/core/model/modx/modrequest.class.php(122): modRequest->prepareResponse()
#12 /www/core/model/modx/modx.class.php(1461): modRequest->handleRequest()
#13 /www/index.php(54): modX->handleRequest()
#14 {main}
  thrown in /www/core/components/simplesearch/model/simplesearch/simplesearch.class.php on line 437

Not quite sure what triggers it, but the trace suggests a front-end action.

halftrainedharry commented 9 months ago

This issue occurs only if the system setting use_multibyte isn't set to Yes and the length of the extract to generate is bigger than the length of the text itself.

The error can be avoided by adding this line

if (strlen($text) <= $length) return $text;

before the line that throws the error:

https://github.com/modxcms/SimpleSearch/blob/fe709df019c3447733057e57d8940a95573b96d8/core/components/simplesearch/model/simplesearch/simplesearch.class.php#L437