From 05358826de3a09e116e86548eff315477b02129e Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 13 Jan 2021 19:15:11 -0800 Subject: [PATCH] "Admin => Misc" - Fix validation of "Maximum File Size" The form "Administer => System Settings => Miscellaneous" has a field for "Maximum File Size". I was on a workstation where the PHP-default and the Civi-default were disagreeable, so (by default) I couldn't submit the form. So I noticed some quirks -- fixed below. Before ------ * The error message tells you that the "Maximum File Size" disagrees with a setting in `php.ini`. Hopefully, you know what+where of "php.ini", otherwise you'll be at a loss for what value is acceptable. * The validation assumes that `upload_max_filesize` is expressed in unit-megabytes. But `php.ini` actually allows different units (`2m`, `2048k`, `1g`, `2097152`). The comparison is incorrect if any other unit is used. (ex: If `php.ini has `upload_max_filesize=1g`, and if the requested limit is `2` megabytes, then it should accept - but it rejects due to mismatched units.) After ----- * Error message tells you what you need to know. * Validator correctly interprets the units used in `php.ini`'s `upload_max_filesize`. --- CRM/Admin/Form/Setting/Miscellaneous.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/CRM/Admin/Form/Setting/Miscellaneous.php b/CRM/Admin/Form/Setting/Miscellaneous.php index 94fce1edaf..4d34e060d8 100644 --- a/CRM/Admin/Form/Setting/Miscellaneous.php +++ b/CRM/Admin/Form/Setting/Miscellaneous.php @@ -44,13 +44,10 @@ class CRM_Admin_Form_Setting_Miscellaneous extends CRM_Admin_Form_Setting { 'prevNextBackend' => CRM_Core_BAO_Setting::SEARCH_PREFERENCES_NAME, ]; - public $_uploadMaxSize; - /** * Basic setup. */ public function preProcess() { - $this->_uploadMaxSize = (int) ini_get('upload_max_filesize'); // check for post max size CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE); // This is a temp hack for the fact we really don't need to hard-code each setting in the tpl but @@ -101,8 +98,13 @@ class CRM_Admin_Form_Setting_Miscellaneous extends CRM_Admin_Form_Setting { $errors = []; // validate max file size - if ($fields['maxFileSize'] > $options->_uploadMaxSize) { - $errors['maxFileSize'] = ts("Maximum file size cannot exceed Upload max size ('upload_max_filesize') as defined in PHP.ini."); + $iniBytes = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), FALSE); + $inputBytes = CRM_Utils_Number::formatUnitSize($fields['maxFileSize'] . 'M', FALSE); + + if ($inputBytes > $iniBytes) { + $errors['maxFileSize'] = ts("Maximum file size cannot exceed limit defined in \"php.ini\" (\"upload_max_filesize=%1\") .", [ + 1 => ini_get('upload_max_filesize'), + ]); } // validate recent items stack size -- 2.25.1