I am getting ‘Call to undefined function Kirby\Toolkit\fnmatch()’ error when uploading a file to a panel page on the remote server, but not on the local version of the same site.
I deploy via git post-receive hook, checkout -f.
kirby/src/toolkit is on the remote server and has 24 files, same as local.
‘media’ folder and ‘panel’ subfolder are 755, same as content folder. The only difference I can see is that on the local ‘media’ folder I got a ‘pages’ subfolder with the images I uploaded locally. But not on the remote because I can’t ul images, of course.
I checked the remote server for ImageMagik and GD:
I went and see if I can change php version. On the panel of this server there is nothing obvious about that, I can change “php mode” which was FastCGI(PHP 7.2)" and I changed it to “(PHP 7.3)” as it did not seem to match the version returned by “php -v”.
Still not working. It may be that this takes time. Meanwhile I will contact them just in case.
This is interesting and very rare, it seems, I haven’t seen this issue in all these years and the function was used in Kirby 2 as well.
I guess you could patch this in the source code but I can’t suggest something quickly but would have to look into this myself. Also, it would only make sense if this would be fixed in Kirby itself in the future, otherwise you wouldn’t be able to update without fixing every time.
And it seems that it’s unfortunately not POSIX compatible, which is a shame. I’m afraid this is such a rare edge case, that it doesn’t make a lot of sense for us to fix this in the core. But fortunately it seems like it’s patchable with a simple plugin. We are currently looking into that.
I am pestering them nevertheless for their unPOSIXness.
First time interacting with you Bastian, congratulations for Kirby, it is great, lovely… I reccomend it and use it as often as I can; and of course congratulations to the rest of the team for their work too.
Thanks for the kind words about Kirby despite the issue
BTW the fix is directly from the PHP docs. User comments there are not always 100% reliable and @lukasbestle wanted to give it a try as well, but if you need a quick fix, you could try to paste the following into a new plugin
in site/plugins/fnmatch/index.php:
if (!function_exists('fnmatch')) {
define('FNM_PATHNAME', 1);
define('FNM_NOESCAPE', 2);
define('FNM_PERIOD', 4);
define('FNM_CASEFOLD', 16);
function fnmatch($pattern, $string, $flags = 0) {
return pcre_fnmatch($pattern, $string, $flags);
}
}
function pcre_fnmatch($pattern, $string, $flags = 0) {
$modifiers = null;
$transforms = array(
'\*' => '.*',
'\?' => '.',
'\[\!' => '[^',
'\[' => '[',
'\]' => ']',
'\.' => '\.',
'\\' => '\\\\'
);
// Forward slash in string must be in pattern:
if ($flags & FNM_PATHNAME) {
$transforms['\*'] = '[^/]*';
}
// Back slash should not be escaped:
if ($flags & FNM_NOESCAPE) {
unset($transforms['\\']);
}
// Perform case insensitive match:
if ($flags & FNM_CASEFOLD) {
$modifiers .= 'i';
}
// Period at start must be the same as pattern:
if ($flags & FNM_PERIOD) {
if (strpos($string, '.') === 0 && strpos($pattern, '.') !== 0) return false;
}
$pattern = '#^'
. strtr(preg_quote($pattern, '#'), $transforms)
. '$#'
. $modifiers;
return (boolean)preg_match($pattern, $string);
}
Sorry to dig this up again but I’m still trying to get this to work.
I’ve tried the plugin solution and verified that the function setting fnmatch is actually called.
Yet when I try to open http://localhost:8000/panel I get the message ‘The JSON response from the API could not be parsed’ and the following stdout:
[Wed Oct 20 19:30:28 2021] 127.0.0.1:43169 [200]: GET /api/system?view=panel - Uncaught Error: Call to undefined function Kirby\Toolkit\fnmatch() in /data/data/com.termux/files/home/icc/httpdocs/kirby/src/Toolkit/Mime.php:256
This is on Android/Termux (Linux). The site itself works fine, it’s just the panel. Everything also works fine on Windows.