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