From b9a07330621b8c09339e33779798af46c517b7cd Mon Sep 17 00:00:00 2001 From: Miguel Dantas Date: Sat, 15 Jun 2019 15:21:05 +0100 Subject: [PATCH] [MEDIA][CORE] Add common function for converting a string with a size unit to an int and MediaFile uses file_quota --- lib/default.php | 2 +- lib/framework.php | 2 +- lib/mediafile.php | 30 ++--------------------------- lib/util.php | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 30 deletions(-) diff --git a/lib/default.php b/lib/default.php index c73f8fccd2..754d03aa6f 100644 --- a/lib/default.php +++ b/lib/default.php @@ -266,7 +266,7 @@ $default = 'video/quicktime' => 'mov', 'video/webm' => 'webm', ), - 'file_quota' => 5000000, + 'file_quota' => common_get_preferred_php_upload_limit(), 'user_quota' => 50000000, 'monthly_quota' => 15000000, 'uploads' => true, diff --git a/lib/framework.php b/lib/framework.php index bd7693f5d7..77e1b19076 100644 --- a/lib/framework.php +++ b/lib/framework.php @@ -32,7 +32,7 @@ defined('GNUSOCIAL') || die(); define('GNUSOCIAL_ENGINE', 'GNU social'); define('GNUSOCIAL_ENGINE_URL', 'https://www.gnu.org/software/social/'); -define('GNUSOCIAL_BASE_VERSION', '1.21.2'); +define('GNUSOCIAL_BASE_VERSION', '1.21.3'); define('GNUSOCIAL_LIFECYCLE', 'dev'); // 'dev', 'alpha[0-9]+', 'beta[0-9]+', 'rc[0-9]+', 'release' define('GNUSOCIAL_VERSION', GNUSOCIAL_BASE_VERSION . '-' . GNUSOCIAL_LIFECYCLE); diff --git a/lib/mediafile.php b/lib/mediafile.php index 4dc75a71a9..e429ec7f91 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -268,35 +268,9 @@ class MediaFile /** * The maximum allowed file size, as an int */ - public static function maxFileSizeInt() + public static function maxFileSizeInt() : int { - return min( - self::sizeStrToInt(ini_get('post_max_size')), - self::sizeStrToInt(ini_get('upload_max_filesize')), - self::sizeStrToInt(ini_get('memory_limit')) - ); - } - - /** - * Convert a string representing a file size (with units), to an int - * @param $str - * @return bool|int|string - */ - public static function sizeStrToInt($str) - { - $unit = substr($str, -1); - $num = substr($str, 0, -1); - switch (strtoupper($unit)) { - case 'G': - $num *= 1024; - // no break - case 'M': - $num *= 1024; - // no break - case 'K': - $num *= 1024; - } - return $num; + return common_config('attachments', 'file_quota'); } /** diff --git a/lib/util.php b/lib/util.php index 45d5b2b8f2..d0131be2ae 100644 --- a/lib/util.php +++ b/lib/util.php @@ -2649,6 +2649,54 @@ function common_strip_html($html, $trim=true, $save_whitespace=false) return $trim ? trim($text) : $text; } +/** + * An internal helper function that converts a $size from php.ini for + * file size limit from the 'human-readable' shorthand into a int. If + * $size is empty (the value is not set in php.ini), returns a default + * value (5000000) + * + * @param string|bool $size + * @return int the php.ini upload limit in machine-readable format + */ +function _common_size_str_to_int($size) : int +{ + if (empty($size)) { + return 5000000; + } + + $suffix = substr($size, -1); + $size = substr($size, 0, -1); + switch (strtoupper($suffix)) { + case 'P': + $size *= 1024; + // no break + case 'T': + $size *= 1024; + // no break + case 'G': + $size *= 1024; + // no break + case 'M': + $size *= 1024; + // no break + case 'K': + $size *= 1024; + break; + } + return $size; +} + +/** + * Uses `_common_size_str_to_int()` to find the smallest value for uploads in php.ini + * + * @returns int + */ +function common_get_preferred_php_upload_limit() { + return min(_common_size_str_to_int(ini_get('post_max_size')), + _common_size_str_to_int(ini_get('upload_max_filesize')), + _common_size_str_to_int(ini_get('memory_limit'))); +} + function html_sprintf() { $args = func_get_args(); -- 2.39.5