gkrid / dokuwiki-plugin-pdfjs

MIT License
6 stars 5 forks source link

Undefined array key 1 in /var/www/html/dokuwiki/lib/plugins/pdfjs/syntax.php #32

Open rogerw-gh opened 4 months ago

rogerw-gh commented 4 months ago

This is happening on a server running php8.2, patched to $ php --version PHP 8.2.18 (cli) (built: Apr 11 2024 22:07:45) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.18, Copyright (c) Zend Technologies with Zend OPcache v8.2.18, Copyright (c), by Zend Technologies

This site is internet facing, so patching to latest version is not optional, there was a nasty security hole in the previous version.

Page surrounding the pdfjs plugin renders OK, the browser displays connection rejected in the iFrame [on Vivaldi], on Firefox the error is 'Firefox can't open this page'.

In the nginx error logs there is are three entries per instance of pdfjs - there are multiple PDF's on this page.

FastCGI sent in stderr: “PHP message: PHP Warning: Undefined array key 1 in /var/www/html/dokuwiki/lib/plugins/pdfjs/syntax.php on line 64; PHP message: PHP Warning: Undefined array key 1 in /var/www/html/dokuwiki/lib/plugins/pdfjs/syntax.php on line 66; PHP message: PHP Warning: Undefined array key 1 in /var/www/html/dokuwiki/lib/plugins/pdfjs/syntax.php on line 78;

Tracing /var/www/html/dokuwiki/lib/plugins/pdfjs/syntax.php, the errors occur in lines using lines list ( ) = explode ();

    list($link, $title) = explode('|', $media, 2);

    list($idzoom, $display) = explode('?disp=', $link, 2);

According to https://www.php.net/manual/en/function.explode.php a ValueError is returned, php7.4 returned false - this code works on php7.4 just fine.

Stack Exchange has this.. https://stackoverflow.com/questions/73133701/php-8-1-explode-causes-error-when-result-is-assigned-to-list

The php developers have changed the behaviour of php8 and up for these null results, conversation here: https://wiki.php.net/rfc/null_coercion_consistency#notes

So this issue isn't going away without looking at lines 66,68 and 78 to fix the returned values from the explode function.

Using declare(strict_types=0); in syntax.php doesn't resolve the problem in lines 68 and 80, support for php 8.2 and up will need a fix which is beyond my level of understanding :-(.

Phildrim commented 4 months ago

Hey, I was running into the same problem. I'm not very versed in php - the idea is to use the null coalescing operator ?? to assign values for otherwise empty elements. The following changes seemed to work: I changed line 66 to

$media_parts = explode('|', $media, 2);
$link = $media_parts[0];
$title = $media_parts[1] ?? 'Default Title';

line 68 to

$link_parts = explode('?disp=', $link, 2);
$idzoom = $link_parts[0];
$display = trim($link_parts[1] ?? '');

and line 78 to

$idzoom_parts = explode('?', $idzoom, 2);
$id = $idzoom_parts[0];
$zoom = $idzoom_parts[1] ?? '';

Let me know if it helped (or if there is an easier solution..)

VA1DER commented 2 weeks ago

Any update on when this might get fixed in the plugin release?