filter.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * ZeroBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 0.20
  11. */
  12. /**
  13. * filter
  14. *
  15. * Provides data filtering functions.
  16. */
  17. class filter
  18. {
  19. /**
  20. * strips slashes deeply
  21. *
  22. * @access public
  23. * @static
  24. * @param mixed $value
  25. * @return mixed
  26. */
  27. public static function stripslashes_deep($value)
  28. {
  29. return is_array($value) ?
  30. array_map('filter::stripslashes_deep', $value) :
  31. stripslashes($value);
  32. }
  33. /**
  34. * format a given number of bytes
  35. *
  36. * @access public
  37. * @static
  38. * @param int $size
  39. * @return string
  40. */
  41. public static function size_humanreadable($size)
  42. {
  43. $iec = array('B', 'kiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
  44. $i = 0;
  45. while ( ( $size / 1024 ) >= 1 ) {
  46. $size = $size / 1024;
  47. $i++;
  48. }
  49. return number_format($size, ($i ? 2 : 0), '.', ' ') . ' ' . $iec[$i];
  50. }
  51. /**
  52. * validate paste ID
  53. *
  54. * @access public
  55. * @static
  56. * @param string $dataid
  57. * @return bool
  58. */
  59. public static function is_valid_paste_id($dataid)
  60. {
  61. return (bool) preg_match('#\A[a-f\d]{16}\z#', $dataid);
  62. }
  63. /**
  64. * fixed time string comparison operation to prevent timing attacks
  65. * https://crackstation.net/hashing-security.htm?=rd#slowequals
  66. *
  67. * @access public
  68. * @static
  69. * @param string $a
  70. * @param string $b
  71. * @return bool
  72. */
  73. public static function slow_equals($a, $b)
  74. {
  75. $diff = strlen($a) ^ strlen($b);
  76. for($i = 0; $i < strlen($a) && $i < strlen($b); $i++)
  77. {
  78. $diff |= ord($a[$i]) ^ ord($b[$i]);
  79. }
  80. return $diff === 0;
  81. }
  82. }