stefangabos / Zebra_Form

A jQuery augmented PHP library for creating secure HTML forms and validating them easily
Other
98 stars 49 forks source link

Update for PHP 8 - unknown named parameter issue in file upload #44

Open njwgh opened 2 years ago

njwgh commented 2 years ago

In PHP 8 an unknown named parameter error occurs with file uploads The reason and fix:

in zebra-form.php, line 2434

actions array has this value (when uploading a file) 0 => string '_upload' (length=7) 1 => string 'desc_file_upload' (length=16) 2 => string 'move' (length=4) 3 => string 'f957_' (length=5) 'block' => string 'error' (length=5) 'message' => string 'Could not upload file' (length=21)

This array is being passed to the function _upload (via call_user_func_array)

PHP 8.0 requires that if you pass 'named' parameters - as in this case 'block' and 'message' are 'named', then the function you're passing them to has to have parameters of the same name.

_upload doesn't have these parameters named, but it actually doesn't need the 'block' and 'message' parameters, so you can leave them out Hence you can edit line 2434 from: if (!call_user_func_array(array(&$this,$actions[0]), array_slice($actions, 1))) { to if (!call_user_func_array(array(&$this,$actions[0]), array_slice($actions, 1, -2))) {

This will leave off the last two values from the end of the array.

line 2434 is used by other functions than _upload, and this fix will work for all of them EXCEPT _convert

The array sent to _convert has a lot of extra named parameters, but these aren't actually used anywhere as far as I can see so you can remove the names

so lines 3128-3131 in zebra-form.php are: 'extension' => $rule_attributes[0], // extension to convert to 'quality' => $rule_attributes[1], // quality (available only for JPEG files) 'preserve_original_file' => $rule_attributes[2], // preserve original file? 'overwrite' => $rule_attributes[3], // overwrite if file with new extension exists

they can be updated to

$rule_attributes[0], // extension to convert to $rule_attributes[1], // quality (available only for JPEG files) $rule_attributes[2], // preserve original file? $rule_attributes[3], // overwrite if file with new extension exists

Note that I have tested my suggested fix for line 2434, but I haven't tested my suggested fix for lines 3128-3131

stefangabos commented 2 years ago

hey, thanks for letting me know! i think the easiest fix is to change

if (!call_user_func_array(array(&$this,$actions[0]), array_slice($actions, 1))) {

to

if (!call_user_func_array(array(&$this,$actions[0]), array_values(array_slice($actions, 1)))) {

that array_values would take care of the named parameters