splittingred / SimpleSearch

A simple search component for MODx Revolution
http://docs.modxcms.com/display/ADDON/SimpleSearch
36 stars 36 forks source link

PHP warning: strpos(): Offset not contained in string #55

Closed ghost closed 12 years ago

ghost commented 13 years ago

My error log has been filling up with these PHP warnings from SimpleSearch. It seems that it's possible for the offset passed to strpos() to be greater than the actual length of the extract, causing PHP to throw this warning. It doesn't affect the search results, but it can fill up the error log on busy sites.

Proposed fix:

Add this code above line 314 of simplesearch.class.php (at the start of the else{} condition):

                $pos_end = 0;

                if( strlen( $text ) >= $length - 1 ) {
                    $pos_end = min(strpos($text, ' ', $length - 1), strpos($text, '.', $length - 1));
                }

It simply checks to make sure that the offset is not greater than the length of the extract text.

ghost commented 13 years ago

I forgot to mention, this also seems to happen with the strpos() on line 249 of simplesearch.class.php (and presumably on line 247 with mb_strpos()) - similar checks can be added to prevent warnings from appearing in the error logs.

splittingred commented 12 years ago

The code has changed quite a bit since this issue was reported. Are you still experiencing it? If so, the line numbers are different than the lines you referenced - could you point out the new line references?

ghost commented 12 years ago

Sorry for the delay in getting back to this. After upgrading to the latest version, the problem persists, and I'm still seeing these errors in my log each day. The lines that I see most frequently referenced are 269 and 334, although I suppose this could also occur with the mb_strpos function calls as well.

Again, it doesn't seem to cause any harm, but dozens of these errors get written to the log each day. It's more of an annoyance than anything, but it'd be nice to get rid of them once and for all.

Thanks!

splittingred commented 12 years ago

Can you post the errors please? (As well as your SimpleSearch call)

ghost commented 12 years ago

After updating to the latest version, I'm still seeing the following errors in my log:

[2011-12-01 07:26:00] (ERROR @ /path/to/core/components/simplesearch/model/simplesearch/simplesearch.class.php : 337) PHP warning: strpos() [<a href='function.strpos'>function.strpos</a>]: Offset not contained in string

The fix for the error on line 269 (now line 272) seems to have worked, though, as I haven't seen that line referenced in the log.

My Simplesearch page is as follows:

[[!SimpleSearchForm?
  &tpl=`siteSearchForm`
  &toPlaceholder=`search.form`]]

[[+search.form]]

[[!SimpleSearch?
  &highlightClass=`searchHighlight`
  &extractEllipsis=`…`
  &pagingSeparator=` `
  &offsetIndex=`offset`
  &_perPage=`100`
  &extractLength=`300`
  &searchStyle=`match`
  &fieldPotency=`pagetitle:20,longtitle:18,alias:12`
  &tpl=`siteSearchResult`
  &containerTpl=`siteSearchResults`
  &currentPageTpl=`searchResultsButtonCurrent`
  &pageTpl=`searchResultsButton`]]