1
0
mirror of https://github.com/e107inc/e107.git synced 2025-04-22 13:41:52 +02:00

file_size_decode() moved from upload to file handler

This commit is contained in:
secretr 2010-02-09 15:24:55 +00:00
parent ab26b37476
commit 0ef179fe57
2 changed files with 63 additions and 44 deletions

@ -9,8 +9,8 @@
*
*
* $Source: /cvs_backup/e107_0.8/e107_handlers/file_class.php,v $
* $Revision: 1.10 $
* $Date: 2010-02-05 11:13:38 $
* $Revision: 1.11 $
* $Date: 2010-02-09 15:24:55 $
* $Author: secretr $
*/
@ -298,5 +298,60 @@ class e_file
}
return false;
}
/**
* Parse a file size string (e.g. 16M) and compute the simple numeric value.
*
* @param string $source - input string which may include 'multiplier' characters such as 'M' or 'G'. Converted to 'decoded value'
* @param int $compare - a 'compare' value
* @param string $action - values (gt|lt)
*
* @return int file size value.
* If the decoded value evaluates to zero, returns the value of $compare
* If $action == 'gt', return the larger of the decoded value and $compare
* If $action == 'lt', return the smaller of the decoded value and $compare
*/
function file_size_decode($source, $compare = 0, $action = '')
{
$source = trim($source);
if (strtolower(substr($source, -1, 1)) == 'b')
$source = substr($source, 0, -1); // Trim a trailing byte indicator
$mult = 1;
if (strlen($source) && (strtoupper(substr($source, -1, 1)) == 'B'))
$source = substr($source, 0, -1);
if (!$source || is_numeric($source))
{
$val = $source;
}
else
{
$val = substr($source, 0, -1);
switch (substr($source, -1, 1))
{
case 'T':
$val = $val * 1024;
case 'G':
$val = $val * 1024;
case 'M':
$val = $val * 1024;
case 'K':
case 'k':
$val = $val * 1024;
break;
}
}
if ($val == 0)
return $compare;
switch ($action)
{
case 'lt':
return min($val, $compare);
case 'gt':
return max($val, $compare);
default:
return $val;
}
return 0;
}
}

@ -9,8 +9,8 @@
* File Upload Handler
*
* $Source: /cvs_backup/e107_0.8/e107_handlers/upload_handler.php,v $
* $Revision: 1.29 $
* $Date: 2010-01-12 09:56:57 $
* $Revision: 1.30 $
* $Date: 2010-02-09 15:24:53 $
* $Author: secretr $
*/
@ -20,7 +20,7 @@
*
* @package e107
* @subpackage e107_handlers
* @version $Id: upload_handler.php,v 1.29 2010-01-12 09:56:57 secretr Exp $;
* @version $Id: upload_handler.php,v 1.30 2010-02-09 15:24:53 secretr Exp $;
*
* @todo - option to restrict by total size irrespective of number of uploads
*/
@ -652,7 +652,9 @@ function vet_file($filename, $target_name, $allowed_filetypes = '', $unknown = F
/**
* Parse a file size string (e.g. 16M) and compute the simple numeric value.
* Proxy to e_file::file_size_decode().
*
* @see e_file::file_size_decode()
* @param string $source - input string which may include 'multiplier' characters such as 'M' or 'G'. Converted to 'decoded value'
* @param int $compare - a 'compare' value
* @param string $action - values (gt|lt)
@ -664,45 +666,7 @@ function vet_file($filename, $target_name, $allowed_filetypes = '', $unknown = F
*/
function file_size_decode($source, $compare = 0, $action = '')
{
$source = trim($source);
if (strtolower(substr($source, -1, 1)) == 'b')
$source = substr($source, 0, -1); // Trim a trailing byte indicator
$mult = 1;
if (strlen($source) && (strtoupper(substr($source, -1, 1)) == 'B'))
$source = substr($source, 0, -1);
if (!$source || is_numeric($source))
{
$val = $source;
}
else
{
$val = substr($source, 0, -1);
switch (substr($source, -1, 1))
{
case 'T':
$val = $val * 1024;
case 'G':
$val = $val * 1024;
case 'M':
$val = $val * 1024;
case 'K':
case 'k':
$val = $val * 1024;
break;
}
}
if ($val == 0)
return $compare;
switch ($action)
{
case 'lt':
return min($val, $compare);
case 'gt':
return max($val, $compare);
default:
return $val;
}
return 0;
return e107::getFile(true)->file_size_decode($source, $compare, $action);
}