Topic: Function strpos() is deprecated in PHP 8.1

I am still on version 4.5.2M of LuxCal and after upgrading to PHP 8.1, I am getting the following message for every calendar entry being displayed:
Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated  in /www/seidenphp/htdocs/luxcal/common/retrieve.php on line 187

Here is the code beginning at the function ProcessEvent:

function processEvent($from, $till, $eStart, $eEnd, &$row) {
    global $evtList, $usr, $set;
   
    $sTs = mktime(12,0,0,substr($from,5,2),substr($from,8,2),substr($from,0,4));
    $eTs = mktime(14,0,0,substr($till,5,2),substr($till,8,2),substr($till,0,4));
    for($i=$sTs;$i<=$eTs;$i+=86400) { //increment 1 day
        $evt = array();
        $curD = date('Y-m-d', $i);
        if (strpos($row['xda'], $curD) === false) { //no exceptions

The above line is line 187 within the retrieve.php.  I've reviewed 5.2.0M code as shown below:

        if (strpos($row['xda'], $curD) !== false) { continue; } //exception: skip

which is a bit different but still gets the produces the same deprecated warning on the page.

Please advise on a code fix for this.
Thanks,
Mark

Re: Function strpos() is deprecated in PHP 8.1

Hi Mark,

The strpos() function is one of the most common string search functions in PHP and in the PHP reference manual which is up-to-date with PHP 8 it is NOT indicated as deprecated. Currently in the PHP 8 reference manual it is advised to use the strpos() function for simple text searches.
So don't worry.
If it is really deprecated, I'm sure it will take at least take a few years before it will be completely dropped. Once it is 100% sure this function is deprecated, I will take care that in a next LuxCal release this function will be replaced by a replacement function.

Roel

3 (edited by JanC 2022-04-11 23:17:35)

Re: Function strpos() is deprecated in PHP 8.1

hey,
I'm getting a similar issue when opening an event details:

Deprecated: preg_replace_callback(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /common/toolbox.php on line 536

The problem is not the function itself but the parameter being null.

By debugging I found out this method is called from the remUrlImgEmlTags function. The 3rd parameter is the value from the DB and can be coming either the columns text1, text2 or text3

By opening directly the sql DB I see that all of my events have text2 and text3 set to NULL. So I fixed it by updating directly my DB:

UPDATE lux_events
SET text2 = ''
WHERE text2 IS NULL;

UPDATE lux_events
SET text3 = ''
WHERE text3 IS NULL;

However that's a workaround. Those field should have been set to the correct empty value by the migration script I guess (I updated from 2.7)

As far as the  strpos() is deprecated goes, I can also reproduce it simply by creating a new event. Again, I see this is coming from the event column "xDates" being nil just after its creation. I believe the default value of that field should be an empty string and not a NULL value

Another fix could be to pass a default value to the strpos() function if the xda is null:

if (strpos($row['xda'] ?? "", $curD) !== false) { continue; } //exception: skip

(note the '?? ""')
However I'm not sure how much compatible that is with different versions of PHP

Re: Function strpos() is deprecated in PHP 8.1

Hi Jan,

Yes, you are right and that is what I expected. I will fix this in a new calendar version. In the mean time i will see if I can create a work around and post the result in the forum under Known Issues and Fixes.

Roel

Re: Function strpos() is deprecated in PHP 8.1

Hi,
thanks for your answer.
As far as the preg_replace_callback goes the fix is similar to the strpos function

just add

  $html = $html ?? "";

at the top of the addUrlImgEmlTags and remUrlImgEmlTags functions

btw, is this project opensource? I'd be happy to contribute with those fixes

Re: Function strpos() is deprecated in PHP 8.1

Hi JanC,
    Thanks very much reviewing and coming up with some work-arounds!  I will implement and wait for the upcoming result under Known Issues and Fixes by Roel!
Mark

Re: Function strpos() is deprecated in PHP 8.1

Hi Jan,

No this project is not open source. But suggestions for improvements or fixes are always welcome. We will validate all suggestions and adopt them, if they are good.

For example the null coalescing operator (??) in your post above is a very good solution, but we cannot adopt it (yet) for the LuxCal calendar because it was introduced in PHP 7 and not all calendar users are running the calendar with PHP 7 or 8.

Roel

Re: Function strpos() is deprecated in PHP 8.1

Roel

Further to comments below, i am being made to upgrade to php 8 and getting similar error messages.

my units : Lux 5.2; PHP 8.1.3

I'm getting the following error message when loading the calendar :

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in 'my_web_address'/common/retrieve.php on line 202.

The message appears for every calendar entry, then the calendar is rendered.

Doss

Re: Function strpos() is deprecated in PHP 8.1

Hi Doss,

This known problem, occurring as of PHP 8.1, will be solved in the next calendar version, which is planned for end of May.
In the mean time you could apply the following fix:
Edit the file "common/retrieve.php" and replace line 202 . . .

if (strpos($row['xda'], $curD) !== false) { continue; } //exception: skip

by . . .

if (strpos($row['xda'] ?? "", $curD) !== false) { continue; } //exception: skip

(In other words: add ?? "" after $row['xda']).

Roel

Re: Function strpos() is deprecated in PHP 8.1

Roel,

Done - all works perfectly.

Many thanks for the swift response.