Filter.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.1
  11. */
  12. namespace PrivateBin;
  13. use Exception;
  14. /**
  15. * Filter
  16. *
  17. * Provides data filtering functions.
  18. */
  19. class Filter
  20. {
  21. /**
  22. * strips slashes deeply
  23. *
  24. * @access public
  25. * @static
  26. * @param mixed $value
  27. * @return mixed
  28. */
  29. public static function stripslashesDeep($value)
  30. {
  31. return is_array($value) ?
  32. array_map('self::stripslashesDeep', $value) :
  33. stripslashes($value);
  34. }
  35. /**
  36. * format a given time string into a human readable label (localized)
  37. *
  38. * accepts times in the format "[integer][time unit]", valid time units are:
  39. * sec, second, seconds, min, minute, minutes, hour, hours, day, days, week,
  40. * weeks, month, months, year, years
  41. *
  42. * @access public
  43. * @static
  44. * @param string $time
  45. * @throws Exception
  46. * @return string
  47. */
  48. public static function formatHumanReadableTime($time)
  49. {
  50. if (preg_match('/^(\d+) *(\w+)$/', $time, $matches) !== 1) {
  51. throw new Exception("Error parsing time format '$time'", 30);
  52. }
  53. switch ($matches[2]) {
  54. case 'sec':
  55. $unit = 'second';
  56. break;
  57. case 'min':
  58. $unit = 'minute';
  59. break;
  60. default:
  61. $unit = rtrim($matches[2], 's');
  62. }
  63. return I18n::_(array('%d ' . $unit, '%d ' . $unit . 's'), (int) $matches[1]);
  64. }
  65. /**
  66. * format a given number of bytes in IEC 80000-13:2008 notation (localized)
  67. *
  68. * @access public
  69. * @static
  70. * @param int $size
  71. * @return string
  72. */
  73. public static function formatHumanReadableSize($size)
  74. {
  75. $iec = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
  76. $i = 0;
  77. while (($size / 1024) >= 1) {
  78. $size = $size / 1024;
  79. $i++;
  80. }
  81. return number_format($size, ($i ? 2 : 0), '.', ' ') . ' ' . I18n::_($iec[$i]);
  82. }
  83. /**
  84. * fixed time string comparison operation to prevent timing attacks
  85. * https://crackstation.net/hashing-security.htm?=rd#slowequals
  86. *
  87. * @access public
  88. * @static
  89. * @param string $a
  90. * @param string $b
  91. * @return bool
  92. */
  93. public static function slowEquals($a, $b)
  94. {
  95. $diff = strlen($a) ^ strlen($b);
  96. for ($i = 0; $i < strlen($a) && $i < strlen($b); $i++) {
  97. $diff |= ord($a[$i]) ^ ord($b[$i]);
  98. }
  99. return $diff === 0;
  100. }
  101. }