2012-04-29 13:15:06 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ZeroBin
|
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
|
|
|
* @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
|
|
|
* @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2015-09-03 16:22:59 -04:00
|
|
|
* @version 0.20
|
2012-04-29 13:15:06 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* filter
|
|
|
|
*
|
|
|
|
* Provides data filtering functions.
|
|
|
|
*/
|
|
|
|
class filter
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* strips slashes deeply
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param mixed $value
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function stripslashes_deep($value)
|
|
|
|
{
|
|
|
|
return is_array($value) ?
|
|
|
|
array_map('filter::stripslashes_deep', $value) :
|
|
|
|
stripslashes($value);
|
|
|
|
}
|
2012-04-30 16:58:08 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* format a given number of bytes
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param int $size
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function size_humanreadable($size)
|
|
|
|
{
|
|
|
|
$iec = array('B', 'kiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
|
2012-08-25 18:49:11 -04:00
|
|
|
$i = 0;
|
|
|
|
while ( ( $size / 1024 ) >= 1 ) {
|
2012-04-30 16:58:08 -04:00
|
|
|
$size = $size / 1024;
|
|
|
|
$i++;
|
|
|
|
}
|
2012-08-25 18:49:11 -04:00
|
|
|
return number_format($size, ($i ? 2 : 0), '.', ' ') . ' ' . $iec[$i];
|
2012-04-30 16:58:08 -04:00
|
|
|
}
|
2014-02-06 16:52:17 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* validate paste ID
|
|
|
|
*
|
|
|
|
* @access public
|
2015-08-16 09:55:31 -04:00
|
|
|
* @static
|
2014-02-06 16:52:17 -05:00
|
|
|
* @param string $dataid
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function is_valid_paste_id($dataid)
|
|
|
|
{
|
|
|
|
return (bool) preg_match('#\A[a-f\d]{16}\z#', $dataid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public static function slow_equals($a, $b)
|
|
|
|
{
|
|
|
|
$diff = strlen($a) ^ strlen($b);
|
|
|
|
for($i = 0; $i < strlen($a) && $i < strlen($b); $i++)
|
|
|
|
{
|
|
|
|
$diff |= ord($a[$i]) ^ ord($b[$i]);
|
|
|
|
}
|
|
|
|
return $diff === 0;
|
|
|
|
}
|
2012-04-29 13:15:06 -04:00
|
|
|
}
|