paste.chapril.org-privatebin/lib/Filter.php

72 lines
1.8 KiB
PHP
Raw Normal View History

<?php
/**
2016-07-11 11:58:15 +02:00
* PrivateBin
*
* a zero-knowledge paste bin
*
2016-07-11 11:58:15 +02:00
* @link https://github.com/PrivateBin/PrivateBin
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
2023-12-04 21:07:17 +01:00
* @version 1.6.1
*/
2016-12-12 18:43:23 +01:00
2016-12-12 18:50:00 +01:00
namespace PrivateBin;
2016-07-21 17:09:48 +02:00
use Exception;
/**
* Filter
*
* Provides data filtering functions.
*/
class Filter
{
/**
* format a given time string into a human readable label (localized)
*
* accepts times in the format "[integer][time unit]"
*
* @access public
* @static
* @param string $time
* @throws Exception
* @return string
*/
public static function formatHumanReadableTime($time)
{
if (preg_match('/^(\d+) *(\w+)$/', $time, $matches) !== 1) {
throw new Exception("Error parsing time format '$time'", 30);
}
switch ($matches[2]) {
case 'sec':
$unit = 'second';
break;
case 'min':
$unit = 'minute';
break;
default:
$unit = rtrim($matches[2], 's');
}
return I18n::_(array('%d ' . $unit, '%d ' . $unit . 's'), (int) $matches[1]);
}
/**
* format a given number of bytes in IEC 80000-13:2008 notation (localized)
*
* @access public
* @static
* @param int $size
* @return string
*/
public static function formatHumanReadableSize($size)
{
$iec = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
$i = 0;
while (($size / 1024) >= 1) {
$size = $size / 1024;
2017-11-13 22:15:14 +01:00
++$i;
}
2023-06-18 12:54:22 +02:00
return number_format($size, $i ? 2 : 0, '.', ' ') . ' ' . I18n::_($iec[$i]);
}
}