seblucas / cops

Calibre OPDS (and HTML) PHP Server : web-based light alternative to Calibre content server / Calibre2OPDS to serve ebooks (epub, mobi, pdf, ...)
http://blog.slucas.fr/en/oss/calibre-opds-php-server
GNU General Public License v2.0
1.43k stars 229 forks source link

php fatal error with php 8.0.0 #492

Open Ponce opened 3 years ago

Ponce commented 3 years ago

cops' index.php with php 8.0.0 produces a blank page and I got this in my php errors log:

28-Nov-2020 15:43:13 Europe/Paris] PHP Fatal error:  Uncaught Error: Call to undefined function create_function() in /var/www/htdocs/cops/vendor/seblucas/dot-php/doT.php:94
Stack trace:
#0 /var/www/htdocs/cops/index.php(53): doT->template()
#1 {main}
  thrown in /var/www/htdocs/cops/vendor/seblucas/dot-php/doT.php on line 94

seems like create_function has been deprecated with php-7.2 and removed in 8.0.0 together with much other deprecated stuff.

ph1lt0r commented 3 years ago

Same issues. Any way to fix this? or is this a major issue?

marioscube commented 3 years ago

The (1st?) error is a deprecated function in php 8 "create_function" on line 94 of doT.php.

The fix should be something like this: Old: return @create_function ('$it', $func);

New:

return @function ($it) use ($func) {
        eval($func);
        };

However on php 7 (don't have 8 yet) this does not work (it should?) and there is no error (in my setup), so I do not know what goes wrong (not much of a programmer myself).

Anyone with php knowledge who can fix this for us?

kounch commented 3 years ago

Tried

The (1st?) error is a deprecated function in php 8 "create_function" on line 94 of doT.php.

The fix should be something like this: Old: return @create_function ('$it', $func);

New:

return @function ($it) use ($func) {
        eval($func);
        };

However on php 7 (don't have 8 yet) this does not work (it should?) and there is no error (in my setup), so I do not know what goes wrong (not much of a programmer myself).

Tried on php 8. There's also no error, and no page.

timtoo commented 3 years ago

I fiddled with it (I don't actually know php), and the following replacement code worked for me with PHP 8:

return function ($it) use ($func) {
        return eval($func);
        };

Apparently the security implications of using eval() was the reason create_function() was deprecated (it used it internally, and it's a bit astonishing it took php this long to get rid of such a thing), so seemingly re-creating create_function() using eval() above doesn't feel great! But here we are. Good to have my COPS alive again.

marioscube commented 3 years ago

@timtoo

What a difference a RETURN makes! And works (for me so far) without the @! Maybe needs more testing......

Must have overlooked that permutation (no clue about php either). ;-)

Thank you!

It also seems to work with php 7.4 for me (don't have 8 installed).

mikespub commented 1 year ago

Included in #522 and release 1.2.0 at https://github.com/mikespub-org/seblucas-cops