Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

Php Warning: Strpos(): Rate Topic   - - - - -

 
  • The Tool
  • Been Here Way Too Long Member
  • Members
  • Join Date: 30-Mar 07
  • 3732 posts

Posted 08 April 2018 - 11:28 AM #1

I posted this issue in bug tracker but it's not getting much attention and nobody else seems to have the issue so I thought I would pick some brains in the forum. 

[07-Apr-2018 14:41:40 America/Chicago] PHP Warning:  strpos(): Offset not contained in string in /home/user/public_html/app/functions/fn.common.php on line 2145

I have been getting these warning for some time now and I have been able to trace it down to only certain product pages but I cannot figure out exactly what is causing it.  Can anyone tell me what this function truncates?

function fn_truncate_chars($text, $limit, $ellipsis = '...')
{
    if (strlen($text) > $limit) {
        $pos_end = strpos(str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $text), ' ', $limit);

        if($pos_end !== false)
            $text = trim(substr($text, 0, $pos_end)) . $ellipsis;
    }

    return $text;
}


 
  • poppedweb
  • Authorized Reseller
  • Members
  • Join Date: 02-Aug 16
  • 476 posts

Posted 08 April 2018 - 12:52 PM #2

Hello,

 

Could you try matching the timestamp from your warning with the one in the access log to see what dispatch has triggered this warning?

 

Kind regards,


PoppedWeb | sales@poppedweb.com | https://poppedweb.com
TurnKey Website Design | Add-Ons | Performance Audits | Dedicated Server Management
24/7 Support | Response within an hour (during working hours).

 
  • The Tool
  • Been Here Way Too Long Member
  • Members
  • Join Date: 30-Mar 07
  • 3732 posts

Posted 08 April 2018 - 04:44 PM #3

Like I stated, the warnings occur on various product pages in the frontend.



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11194 posts

Posted 08 April 2018 - 08:32 PM #4

You'll probably have to add some debugging and look for when the strpos returns false and error_get_last() and look at the "message" element.  It should contain 'strpos'.  When it does, you can do a debug_backtrace() to see how you got there.

 

The actual error is that $limit -1 is more than the length of the string.  Ie. strpos("a", "abc", 3) would return that error since "abc"[3] does is beyond the length of "abc".   So you'll have to see what is calling the function with an invalid $limit.


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • The Tool
  • Been Here Way Too Long Member
  • Members
  • Join Date: 30-Mar 07
  • 3732 posts

Posted 09 April 2018 - 07:20 AM #5

Okay, apparently this has something to do with the pinterest button from the social buttons addon when truncating the description.
 
 
PHP Warning
Message

strpos(): Offset not contained in string
Error at

app/functions/fn.common.php, line: 2145
Backtrace
File: app/functions/fn.common.php
Line: 2145
Function: {closure}
File: app/addons/social_buttons/func.php
Line: 61
Function: fn_truncate_chars
File: app/addons/social_buttons/func.php
Line: 32
Function: fn_pinterest_prepare_settings
File: app/addons/social_buttons/controllers/frontend/products.post.php
Line: 32
Function: fn_get_sb_provider_settings
File: app/functions/fn.control.php
Line: 702
Function: include
File: app/functions/fn.control.php
Line: 460
Function: fn_run_controller
File: index.php
Line: 25
Function: fn_dispatch

    $pinterest_params = array(
        'url' => urlencode(fn_sb_get_url()),
        'media' => urlencode(fn_get_sb_image_url($params)),
        'description' => rawurlencode(fn_truncate_chars(htmlspecialchars(fn_get_sb_description($params)), 200)),
        'params' => '',
    );
This is the description for the product.
 

<p><strong>EZ Red XLM500-GR EZR-XLM500-GR Xtreme Magnetic Light, Green</strong></p>
<ul>
<li>Includes 360 degree magnetic base</li>
<li>Logo COB LED</li>
<li>Micro USB recharcheable rotating stand and handle</li>
<li>Rugged bumper protectors</li>
<li>On/Off Button</li>
</ul>



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11194 posts

Posted 09 April 2018 - 05:22 PM #6

Suggest you look at line 61 on your site of the social_buttons/func.php file.  On my site it has:

'description' => rawurlencode(fn_truncate_chars(htmlspecialchars(fn_get_sb_description($params)), 200)),

The 200 parameter is supposed to be a max limit.  However, there's a bug in that function where the comparison is done BEFORE the str_replace.  And the string length is ending up less than $limit because 2 chars can be replaced by 1 "\r\n' replaced with ' '. This alters the length of the string.

 

Suggest you submit a report to bugtracker with this info.

You can probably fix it by changing

. if (strlen($text) > $limit) {
        $pos_end = strpos(str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $text), ' ', $limit);

 
       if($pos_end !== false)
            $text = trim(substr($text, 0, $pos_end)) . $ellipsis;
    }


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • The Tool
  • Been Here Way Too Long Member
  • Members
  • Join Date: 30-Mar 07
  • 3732 posts

Posted 09 April 2018 - 08:19 PM #7

Suggest you look at line 61 on your site of the social_buttons/func.php file.


I posted that piece/line of code above and you have what I have.
 

Suggest you submit a report to bugtracker with this info.


I did a month ago and it's been under review for as long. That's why I am picking your brain. Do you reckon we'll get compensated for fixing it? LMAO

What exactly does ". " do? It's the only thing I see different in your code and the original.

 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11194 posts

Posted 09 April 2018 - 08:54 PM #8

Looks like a chunk of my posting got dropped.  There should have been a 

 

to

    $stripped = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $text),;
    if (strlen($stripped) > $limit) {
        $pos_end = strpos($stripped, $limit);

 
       if($pos_end !== false)
            $text = trim(substr($text, 0, $pos_end)) . $ellipsis;
    }

UNTESTED

 

Note that in the above, it's possible for the returned text to be less than 200 chars given that any "\r\n" chars (2) will be evaluated against the 200 as a single char..


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • The Tool
  • Been Here Way Too Long Member
  • Members
  • Join Date: 30-Mar 07
  • 3732 posts

Posted 10 April 2018 - 08:16 AM #9

It looks like it fixed it.  You left a comma after $text).  The working function is:

function fn_truncate_chars($text, $limit, $ellipsis = '...')
{
    $stripped = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $text);
    if (strlen($stripped) > $limit) {
        $pos_end = strpos($stripped, $limit);

        if($pos_end !== false)
            $text = trim(substr($text, 0, $pos_end)) . $ellipsis;
    }

    return $text;
}

Just a friendly note to others...if you use cpanel, do not use the file editor to edit the /apps/functions/fn.common.php.  It will not only change the charset within the code in the file but it will save it as gb18030 even if you select utf-8.



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11194 posts

Posted 10 April 2018 - 07:49 PM #10

Glad it works for you.  As I said, UNTESTED.


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.