From f28aa5f3ecbd729b53894d7e091db0dab1368ddc Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Thu, 7 Dec 2006 17:02:25 +0000 Subject: - Patch #92059 by Steven et al: added a memory check to garland/color.module. --- includes/common.inc | 21 +++++++++++++++++++++ includes/file.inc | 24 ++---------------------- 2 files changed, 23 insertions(+), 22 deletions(-) (limited to 'includes') diff --git a/includes/common.inc b/includes/common.inc index 6d4ece2c0..703ca698a 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -959,6 +959,27 @@ function format_plural($count, $singular, $plural) { } } +/** + * Parse a given byte count. + * + * @param $size + * The size expressed as a number of bytes with optional SI size and unit + * suffix (e.g. 2, 3K, 5MB, 10G). + * @return + * An integer representation of the size. + */ +function parse_size($size) { + $suffixes = array( + '' => 1, + 'k' => 1024, + 'm' => 1048576, // 1024 * 1024 + 'g' => 1073741824, // 1024 * 1024 * 1024 + ); + if (preg_match('/([0-9]+)\s*(k|m|g)?(b?(ytes?)?)/i', $size, $match)) { + return $match[1] * $suffixes[drupal_strtolower($match[2])]; + } +} + /** * Generate a string representation for the given byte count. * diff --git a/includes/file.inc b/includes/file.inc index 8df67ec65..a6702cac8 100644 --- a/includes/file.inc +++ b/includes/file.inc @@ -712,26 +712,6 @@ function file_directory_path() { return variable_get('file_directory_path', 'files'); } -/** - * Helper function for file_upload_max_size(). - */ -function _file_convert_to_mb($val){ - $val = trim($val); - $last = strtolower($val[strlen($val) - 1]); - switch ($last) { - // The 'G' modifier is available since PHP 5.1.0 - case 'g': - $size = $val * 1024; - break; - case 'k': - $size = $val / 1024; - break; - default: - $size = (int) $val; - } - return $size; -} - /** * Determine the maximum file upload size by querying the PHP settings. * @@ -742,9 +722,9 @@ function file_upload_max_size() { static $max_size = -1; if ($max_size < 0) { - $upload_max = _file_convert_to_mb(ini_get('upload_max_filesize')); + $upload_max = parse_size(ini_get('upload_max_filesize')); // sanity check- a single upload should not be more than 50% the size limit of the total post - $post_max = _file_convert_to_mb(ini_get('post_max_size')) / 2; + $post_max = parse_size(ini_get('post_max_size')) / 2; $max_size = ($upload_max < $post_max) ? $upload_max : $post_max; } return $max_size; -- cgit v1.2.3