2012-04-29 19:15:06 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 11:58:15 +02:00
|
|
|
* PrivateBin
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 11:58:15 +02:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2012-04-29 19:15:06 +02:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 13:56:52 +02:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2017-10-04 20:05:46 +02:00
|
|
|
* @version 1.1.1
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
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;
|
|
|
|
|
2012-04-29 19:15:06 +02:00
|
|
|
/**
|
2016-08-09 11:54:42 +02:00
|
|
|
* Filter
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
|
|
|
* Provides data filtering functions.
|
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
class Filter
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2012-04-30 22:58:08 +02:00
|
|
|
/**
|
2015-09-06 19:21:17 +02:00
|
|
|
* 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
|
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
public static function formatHumanReadableTime($time)
|
2015-09-06 19:21:17 +02:00
|
|
|
{
|
|
|
|
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');
|
|
|
|
}
|
2016-08-09 11:54:42 +02:00
|
|
|
return I18n::_(array('%d ' . $unit, '%d ' . $unit . 's'), (int) $matches[1]);
|
2015-09-06 19:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* format a given number of bytes in IEC 80000-13:2008 notation (localized)
|
2012-04-30 22:58:08 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param int $size
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
public static function formatHumanReadableSize($size)
|
2012-04-30 22:58:08 +02:00
|
|
|
{
|
2015-09-06 19:21:17 +02:00
|
|
|
$iec = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
|
2016-08-15 16:45:47 +02:00
|
|
|
$i = 0;
|
2016-07-26 08:19:35 +02:00
|
|
|
while (($size / 1024) >= 1) {
|
|
|
|
$size = $size / 1024;
|
2017-11-13 22:15:14 +01:00
|
|
|
++$i;
|
2012-04-30 22:58:08 +02:00
|
|
|
}
|
2016-08-09 11:54:42 +02:00
|
|
|
return number_format($size, ($i ? 2 : 0), '.', ' ') . ' ' . I18n::_($iec[$i]);
|
2012-04-30 22:58:08 +02:00
|
|
|
}
|
2014-02-06 22:52:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* fixed time string comparison operation to prevent timing attacks
|
|
|
|
* https://crackstation.net/hashing-security.htm?=rd#slowequals
|
|
|
|
*
|
|
|
|
* @access public
|
2015-08-16 15:55:31 +02:00
|
|
|
* @static
|
2014-02-06 22:52:17 +01:00
|
|
|
* @param string $a
|
|
|
|
* @param string $b
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
public static function slowEquals($a, $b)
|
2014-02-06 22:52:17 +01:00
|
|
|
{
|
|
|
|
$diff = strlen($a) ^ strlen($b);
|
2017-11-13 22:15:14 +01:00
|
|
|
for ($i = 0; $i < strlen($a) && $i < strlen($b); ++$i) {
|
2014-02-06 22:52:17 +01:00
|
|
|
$diff |= ord($a[$i]) ^ ord($b[$i]);
|
|
|
|
}
|
|
|
|
return $diff === 0;
|
2016-07-26 08:19:35 +02:00
|
|
|
}
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|