diff options
-rw-r--r-- | phpBB/docs/CHANGELOG.html | 1 | ||||
-rw-r--r-- | phpBB/includes/acp/acp_attachments.php | 10 | ||||
-rw-r--r-- | phpBB/includes/functions.php | 71 | ||||
-rw-r--r-- | phpBB/includes/functions_content.php | 8 | ||||
-rw-r--r-- | phpBB/includes/functions_upload.php | 9 |
5 files changed, 76 insertions, 23 deletions
diff --git a/phpBB/docs/CHANGELOG.html b/phpBB/docs/CHANGELOG.html index 6be303d534..8aed44a6d8 100644 --- a/phpBB/docs/CHANGELOG.html +++ b/phpBB/docs/CHANGELOG.html @@ -151,6 +151,7 @@ <li>[Fix] Fix database updater and db tools to support multiple column changes/additions/removals with SQLite</li> <li>[Fix] Correctly detect GZIP status in debug mode. (Bug #24075 - Patch by rxu)</li> <li>[Fix] Posting smilies in view more smilies now work again in IE (Bug #46025 - Patch by leviatan21)</li> + <li>[Fix] Properly convert and show filesize information (Bug #47775 - Patch by bantu)</li> <li>[Change] Change the data format of the default file ACM to be more secure from tampering and have better performance.</li> <li>[Change] Add index on log_time to the log table to prevent slowdown on boards with many log entries. (Bug #44665 - Patch by bantu)</li> <li>[Change] Template engine now permits to a limited extent variable includes.</li> diff --git a/phpBB/includes/acp/acp_attachments.php b/phpBB/includes/acp/acp_attachments.php index de4092036e..0fc0fe0848 100644 --- a/phpBB/includes/acp/acp_attachments.php +++ b/phpBB/includes/acp/acp_attachments.php @@ -684,8 +684,9 @@ class acp_attachments $ext_group_row['max_filesize'] = (int) $config['max_filesize']; } - $size_format = ($ext_group_row['max_filesize'] >= 1048576) ? 'mb' : (($ext_group_row['max_filesize'] >= 1024) ? 'kb' : 'b'); - $ext_group_row['max_filesize'] = get_formatted_filesize($ext_group_row['max_filesize'], false); + $max_filesize = get_formatted_filesize($ext_group_row['max_filesize'], false, array('mb', 'kb', 'b')); + $size_format = $max_filesize['si_identifier']; + $ext_group_row['max_filesize'] = $max_filesize['value']; $img_path = $config['upload_icons_path']; @@ -1429,8 +1430,9 @@ class acp_attachments function max_filesize($value, $key = '') { // Determine size var and adjust the value accordingly - $size_var = ($value >= 1048576) ? 'mb' : (($value >= 1024) ? 'kb' : 'b'); - $value = get_formatted_filesize($value, false); + $filesize = get_formatted_filesize($value, false, array('mb', 'kb', 'b')); + $size_var = $filesize['si_identifier']; + $value = $filesize['value']; return '<input type="text" id="' . $key . '" size="8" maxlength="15" name="config[' . $key . ']" value="' . $value . '" /> <select name="' . $key . '">' . size_select_options($size_var) . '</select>'; } diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 60fe6a454f..84bbb964de 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -232,27 +232,82 @@ function unique_id($extra = 'c') /** * Return formatted string for filesizes +* +* @param int $value filesize in bytes +* @param bool $string_only true if language string should be returned +* @param array $allowed_units only allow these units (data array indexes) +* +* @return mixed data array if $string_only is false +* @author bantu */ -function get_formatted_filesize($bytes, $add_size_lang = true) +function get_formatted_filesize($value, $string_only = true, $allowed_units = false) { global $user; - if ($bytes >= pow(2, 30)) + $available_units = array( + 'gb' => array( + 'min' => 1073741824, // pow(2, 30) + 'index' => 3, + 'si_unit' => 'GB', + 'iec_unit' => 'GIB', + ), + 'mb' => array( + 'min' => 1048576, // pow(2, 20) + 'index' => 2, + 'si_unit' => 'MB', + 'iec_unit' => 'MIB', + ), + 'kb' => array( + 'min' => 1024, // pow(2, 10) + 'index' => 1, + 'si_unit' => 'KB', + 'iec_unit' => 'KIB', + ), + 'b' => array( + 'min' => 0, + 'index' => 0, + 'si_unit' => 'BYTES', // Language index + 'iec_unit' => 'BYTES', // Language index + ), + ); + + foreach ($available_units as $si_identifier => $unit_info) { - return ($add_size_lang) ? round($bytes / 1024 / 1024 / 1024, 2) . ' ' . $user->lang['GIB'] : round($bytes / 1024 / 1024 / 1024, 2); + if (!empty($allowed_units) && $si_identifier != 'b' && !in_array($si_identifier, $allowed_units)) + { + continue; + } + + if ($value >= $unit_info['min']) + { + $unit_info['si_identifier'] = $si_identifier; + + break; + } } + unset($available_units); - if ($bytes >= pow(2, 20)) + for ($i = 0; $i < $unit_info['index']; $i++) { - return ($add_size_lang) ? round($bytes / 1024 / 1024, 2) . ' ' . $user->lang['MIB'] : round($bytes / 1024 / 1024, 2); + $value /= 1024; } + $value = round($value, 2); - if ($bytes >= pow(2, 10)) + // Lookup units in language dictionary + $unit_info['si_unit'] = (isset($user->lang[$unit_info['si_unit']])) ? $user->lang[$unit_info['si_unit']] : $unit_info['si_unit']; + $unit_info['iec_unit'] = (isset($user->lang[$unit_info['iec_unit']])) ? $user->lang[$unit_info['iec_unit']] : $unit_info['iec_unit']; + + // Default to IEC + $unit_info['unit'] = $unit_info['iec_unit']; + + if (!$string_only) { - return ($add_size_lang) ? round($bytes / 1024, 2) . ' ' . $user->lang['KIB'] : round($bytes / 1024, 2); + $unit_info['value'] = $value; + + return $unit_info; } - return ($add_size_lang) ? ($bytes) . ' ' . $user->lang['BYTES'] : ($bytes); + return $value . ' ' . $unit_info['unit']; } /** diff --git a/phpBB/includes/functions_content.php b/phpBB/includes/functions_content.php index 3107177137..630f4105d0 100644 --- a/phpBB/includes/functions_content.php +++ b/phpBB/includes/functions_content.php @@ -853,16 +853,14 @@ function parse_attachments($forum_id, &$message, &$attachments, &$update_count, } } - $filesize = $attachment['filesize']; - $size_lang = ($filesize >= 1048576) ? $user->lang['MIB'] : (($filesize >= 1024) ? $user->lang['KIB'] : $user->lang['BYTES']); - $filesize = get_formatted_filesize($filesize, false); + $filesize = get_formatted_filesize($attachment['filesize'], false); $comment = bbcode_nl2br(censor_text($attachment['attach_comment'])); $block_array += array( 'UPLOAD_ICON' => $upload_icon, - 'FILESIZE' => $filesize, - 'SIZE_LANG' => $size_lang, + 'FILESIZE' => $filesize['value'], + 'SIZE_LANG' => $filesize['unit'], 'DOWNLOAD_NAME' => basename($attachment['real_filename']), 'COMMENT' => $comment, ); diff --git a/phpBB/includes/functions_upload.php b/phpBB/includes/functions_upload.php index 1ad6223aa1..c65d732984 100644 --- a/phpBB/includes/functions_upload.php +++ b/phpBB/includes/functions_upload.php @@ -417,10 +417,9 @@ class filespec // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form if ($this->upload->max_filesize && ($this->get('filesize') > $this->upload->max_filesize || $this->filesize == 0)) { - $size_lang = ($this->upload->max_filesize >= 1048576) ? $user->lang['MIB'] : (($this->upload->max_filesize >= 1024) ? $user->lang['KIB'] : $user->lang['BYTES'] ); $max_filesize = get_formatted_filesize($this->upload->max_filesize, false); - $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang); + $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); return false; } @@ -855,10 +854,9 @@ class fileupload break; case 2: - $size_lang = ($this->max_filesize >= 1048576) ? $user->lang['MIB'] : (($this->max_filesize >= 1024) ? $user->lang['KIB'] : $user->lang['BYTES']); $max_filesize = get_formatted_filesize($this->max_filesize, false); - $error = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang); + $error = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); break; case 3: @@ -891,10 +889,9 @@ class fileupload // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0)) { - $size_lang = ($this->max_filesize >= 1048576) ? $user->lang['MIB'] : (($this->max_filesize >= 1024) ? $user->lang['KIB'] : $user->lang['BYTES']); $max_filesize = get_formatted_filesize($this->max_filesize, false); - $file->error[] = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize, $size_lang); + $file->error[] = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']); } // check Filename |