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