Renamed classes for full PSR-2 compliance, some cleanup
This commit is contained in:
106
lib/Filter.php
Normal file
106
lib/Filter.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* PrivateBin
|
||||
*
|
||||
* a zero-knowledge paste bin
|
||||
*
|
||||
* @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
|
||||
* @version 0.22
|
||||
*/
|
||||
|
||||
namespace PrivateBin;
|
||||
|
||||
use PrivateBin\I18n;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Filter
|
||||
*
|
||||
* Provides data filtering functions.
|
||||
*/
|
||||
class Filter
|
||||
{
|
||||
/**
|
||||
* strips slashes deeply
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
public static function stripslashesDeep($value)
|
||||
{
|
||||
return is_array($value) ?
|
||||
array_map('self::stripslashesDeep', $value) :
|
||||
stripslashes($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
$i++;
|
||||
}
|
||||
return number_format($size, ($i ? 2 : 0), '.', ' ') . ' ' . I18n::_($iec[$i]);
|
||||
}
|
||||
|
||||
/**
|
||||
* fixed time string comparison operation to prevent timing attacks
|
||||
* https://crackstation.net/hashing-security.htm?=rd#slowequals
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
* @param string $a
|
||||
* @param string $b
|
||||
* @return bool
|
||||
*/
|
||||
public static function slowEquals($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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user