From 6cf981cf263f309d977590cd459e61fb161a0c75 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 8 Nov 2023 18:15:08 +1300 Subject: [PATCH] Simplify CRM_Utils_Number::formatUnitSize, move check to form --- CRM/Admin/Form/Setting/Miscellaneous.php | 14 +++++++++----- CRM/Core/BAO/File.php | 3 +-- CRM/Import/DataSource/CSV.php | 6 +++--- CRM/Import/Form/DataSource.php | 2 -- CRM/Utils/Number.php | 18 ++---------------- 5 files changed, 15 insertions(+), 28 deletions(-) diff --git a/CRM/Admin/Form/Setting/Miscellaneous.php b/CRM/Admin/Form/Setting/Miscellaneous.php index cf07535068..cc221644ae 100644 --- a/CRM/Admin/Form/Setting/Miscellaneous.php +++ b/CRM/Admin/Form/Setting/Miscellaneous.php @@ -48,9 +48,13 @@ class CRM_Admin_Form_Setting_Miscellaneous extends CRM_Admin_Form_Setting { /** * Basic setup. */ - public function preProcess() { - // check for post max size - CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE); + public function preProcess(): void { + $maxImportFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize')); + $postMaxSize = CRM_Utils_Number::formatUnitSize(ini_get('post_max_size')); + if ($maxImportFileSize > $postMaxSize) { + CRM_Core_Session::setStatus(ts("Note: Upload max filesize ('upload_max_filesize') should not exceed Post max size ('post_max_size') as defined in PHP.ini, please check with your system administrator."), ts("Warning"), "alert"); + } + // This is a temp hack for the fact we really don't need to hard-code each setting in the tpl but // we haven't worked through NOT doing that. These settings have been un-hardcoded. $this->assign('pure_config_settings', [ @@ -100,8 +104,8 @@ class CRM_Admin_Form_Setting_Miscellaneous extends CRM_Admin_Form_Setting { $errors = []; // validate max file size - $iniBytes = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), FALSE); - $inputBytes = CRM_Utils_Number::formatUnitSize($fields['maxFileSize'] . 'M', FALSE); + $iniBytes = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize')); + $inputBytes = ((int) $fields['maxFileSize']) * 1024 * 1024; if ($inputBytes > $iniBytes) { $errors['maxFileSize'] = ts("Maximum file size cannot exceed limit defined in \"php.ini\" (\"upload_max_filesize=%1\").", [ diff --git a/CRM/Core/BAO/File.php b/CRM/Core/BAO/File.php index d0ad6492a3..0745e4a4a2 100644 --- a/CRM/Core/BAO/File.php +++ b/CRM/Core/BAO/File.php @@ -442,9 +442,8 @@ AND CEF.entity_id = %2"; // Assign maxAttachments count to template for help message $form->assign('maxAttachments', $numAttachments); - $config = CRM_Core_Config::singleton(); // set default max file size as 2MB - $maxFileSize = $config->maxFileSize ? $config->maxFileSize : 2; + $maxFileSize = \Civi::settings()->get('maxFileSize') ?: 2; $currentAttachmentInfo = self::getEntityFile($entityTable, $entityID, TRUE); $totalAttachments = $currentAttachmentInfo ? count($currentAttachmentInfo) : 0; diff --git a/CRM/Import/DataSource/CSV.php b/CRM/Import/DataSource/CSV.php index 9885ceab93..fbf9c5d37d 100644 --- a/CRM/Import/DataSource/CSV.php +++ b/CRM/Import/DataSource/CSV.php @@ -48,12 +48,12 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource { public function buildQuickForm(&$form) { $form->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_CSV'); - $uploadFileSize = CRM_Utils_Number::formatUnitSize(Civi::settings()->get('maxFileSize') . 'm', TRUE); + $uploadFileSize = $uploadSize = \Civi::settings()->get('maxFileSize'); //Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit". if (empty($uploadFileSize)) { - $uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), TRUE); + $uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize')); + $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2); } - $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2); $form->assign('uploadSize', $uploadSize); $form->add('File', 'uploadFile', ts('Import Data File'), NULL, TRUE); $form->add('text', 'fieldSeparator', ts('Import Field Separator'), ['size' => 2], TRUE); diff --git a/CRM/Import/Form/DataSource.php b/CRM/Import/Form/DataSource.php index 75149c026e..c9ab949279 100644 --- a/CRM/Import/Form/DataSource.php +++ b/CRM/Import/Form/DataSource.php @@ -37,8 +37,6 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms { */ public function preProcess(): void { $this->pushUrlToUserContext(); - // check for post max size - CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE); $this->assign('importEntity', $this->getTranslatedEntity()); $this->assign('importEntities', $this->getTranslatedEntities()); } diff --git a/CRM/Utils/Number.php b/CRM/Utils/Number.php index da5db035c5..e8212c8e31 100644 --- a/CRM/Utils/Number.php +++ b/CRM/Utils/Number.php @@ -66,14 +66,13 @@ class CRM_Utils_Number { } /** - * Some kind of numbery-looky-printy thing. + * Convert a file size value from the formats allowed in php_ini to the number of bytes. * * @param string $size - * @param bool $checkForPostMax * * @return int */ - public static function formatUnitSize($size, $checkForPostMax = FALSE) { + public static function formatUnitSize($size): int { if ($size) { $last = strtolower($size[strlen($size) - 1]); $size = (int) $size; @@ -87,19 +86,6 @@ class CRM_Utils_Number { case 'k': $size *= 1024; } - - if ($checkForPostMax) { - $maxImportFileSize = self::formatUnitSize(ini_get('upload_max_filesize')); - $postMaxSize = self::formatUnitSize(ini_get('post_max_size')); - if ($maxImportFileSize > $postMaxSize && $postMaxSize == $size) { - CRM_Core_Session::setStatus(ts("Note: Upload max filesize ('upload_max_filesize') should not exceed Post max size ('post_max_size') as defined in PHP.ini, please check with your system administrator."), ts("Warning"), "alert"); - } - // respect php.ini upload_max_filesize - if ($size > $maxImportFileSize && $size !== $postMaxSize) { - $size = $maxImportFileSize; - CRM_Core_Session::setStatus(ts("Note: Please verify your configuration for Maximum File Size (in MB) Administrator >> System Settings >> Misc. It should support 'upload_max_size' as defined in PHP.ini.Please check with your system administrator.", [1 => CRM_Utils_System::url('civicrm/admin/setting/misc', 'reset=1')]), ts("Warning"), "alert"); - } - } return $size; } } -- 2.25.1