"Admin => Misc" - Fix validation of "Maximum File Size"
authorTim Otten <totten@civicrm.org>
Thu, 14 Jan 2021 03:15:11 +0000 (19:15 -0800)
committerTim Otten <totten@civicrm.org>
Thu, 14 Jan 2021 03:25:17 +0000 (19:25 -0800)
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

index 94fce1edafdff58da5ca2cb6993643475da581ea..4d34e060d8600d2c1cf87b9dba5bdf5725c43db4 100644 (file)
@@ -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