Passed
Push — master ( 3f508b...939a62 )
by El
05:26
created

Filter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 67
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A slowEquals() 0 7 3
A formatHumanReadableSize() 0 9 3
A formatHumanReadableTime() 0 16 4
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.1
11
 */
12
13
namespace PrivateBin;
14
15
use Exception;
16
17
/**
18
 * Filter
19
 *
20
 * Provides data filtering functions.
21
 */
22
class Filter
23
{
24
    /**
25
     * format a given time string into a human readable label (localized)
26
     *
27
     * accepts times in the format "[integer][time unit]"
28
     *
29
     * @access public
30
     * @static
31
     * @param  string $time
32
     * @throws Exception
33
     * @return string
34
     */
35 38
    public static function formatHumanReadableTime($time)
36
    {
37 38
        if (preg_match('/^(\d+) *(\w+)$/', $time, $matches) !== 1) {
38 1
            throw new Exception("Error parsing time format '$time'", 30);
39
        }
40 37
        switch ($matches[2]) {
41 37
            case 'sec':
42 1
                $unit = 'second';
43 1
                break;
44 37
            case 'min':
45 37
                $unit = 'minute';
46 37
                break;
47
            default:
48 37
                $unit = rtrim($matches[2], 's');
49
        }
50 37
        return I18n::_(array('%d ' . $unit, '%d ' . $unit . 's'), (int) $matches[1]);
51
    }
52
53
    /**
54
     * format a given number of bytes in IEC 80000-13:2008 notation (localized)
55
     *
56
     * @access public
57
     * @static
58
     * @param  int $size
59
     * @return string
60
     */
61 3
    public static function formatHumanReadableSize($size)
62
    {
63 3
        $iec = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
64 3
        $i   = 0;
65 3
        while (($size / 1024) >= 1) {
66 1
            $size = $size / 1024;
67 1
            ++$i;
68
        }
69 3
        return number_format($size, ($i ? 2 : 0), '.', ' ') . ' ' . I18n::_($iec[$i]);
70
    }
71
72
    /**
73
     * fixed time string comparison operation to prevent timing attacks
74
     * https://crackstation.net/hashing-security.htm?=rd#slowequals
75
     *
76
     * @access public
77
     * @static
78
     * @param  string $a
79
     * @param  string $b
80
     * @return bool
81
     */
82 11
    public static function slowEquals($a, $b)
83
    {
84 11
        $diff = strlen($a) ^ strlen($b);
85 11
        for ($i = 0; $i < strlen($a) && $i < strlen($b); ++$i) {
86 11
            $diff |= ord($a[$i]) ^ ord($b[$i]);
87
        }
88 11
        return $diff === 0;
89
    }
90
}
91