These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Persistence; |
||
14 | |||
15 | use PrivateBin\Configuration; |
||
16 | |||
17 | /** |
||
18 | * PurgeLimiter |
||
19 | * |
||
20 | * Handles purge limiting, so purging is not triggered too frequently. |
||
21 | */ |
||
22 | class PurgeLimiter extends AbstractPersistence |
||
23 | { |
||
24 | /** |
||
25 | * time limit in seconds, defaults to 300s |
||
26 | * |
||
27 | * @access private |
||
28 | * @static |
||
29 | * @var int |
||
30 | */ |
||
31 | private static $_limit = 300; |
||
32 | |||
33 | /** |
||
34 | * set the time limit in seconds |
||
35 | * |
||
36 | * @access public |
||
37 | * @static |
||
38 | * @param int $limit |
||
39 | */ |
||
40 | 30 | public static function setLimit($limit) |
|
41 | { |
||
42 | 30 | self::$_limit = $limit; |
|
43 | 30 | } |
|
44 | |||
45 | /** |
||
46 | * set configuration options of the traffic limiter |
||
47 | * |
||
48 | * @access public |
||
49 | * @static |
||
50 | * @param Configuration $conf |
||
51 | */ |
||
52 | 29 | public static function setConfiguration(Configuration $conf) |
|
53 | { |
||
54 | 29 | self::setLimit($conf->getKey('limit', 'purge')); |
|
55 | 29 | self::setPath($conf->getKey('dir', 'purge')); |
|
56 | 29 | } |
|
57 | |||
58 | /** |
||
59 | * check if the purge can be performed |
||
60 | * |
||
61 | * @access public |
||
62 | * @static |
||
63 | * @throws Exception |
||
64 | * @return bool |
||
65 | */ |
||
66 | 30 | public static function canPurge() |
|
0 ignored issues
–
show
|
|||
67 | { |
||
68 | // disable limits if set to less then 1 |
||
69 | 30 | if (self::$_limit < 1) { |
|
70 | 2 | return true; |
|
71 | } |
||
72 | |||
73 | 29 | $file = 'purge_limiter.php'; |
|
74 | 29 | $now = time(); |
|
75 | 29 | $content = '<?php' . PHP_EOL . '$GLOBALS[\'purge_limiter\'] = ' . $now . ';' . PHP_EOL; |
|
76 | 29 | if (!self::_exists($file)) { |
|
77 | 29 | self::_store($file, $content); |
|
78 | } |
||
79 | |||
80 | 29 | $path = self::getPath($file); |
|
81 | 29 | require $path; |
|
82 | 29 | $pl = $GLOBALS['purge_limiter']; |
|
83 | |||
84 | 29 | if ($pl + self::$_limit >= $now) { |
|
85 | 29 | $result = false; |
|
86 | } else { |
||
87 | 1 | $result = true; |
|
88 | 1 | self::_store($file, $content); |
|
89 | } |
||
90 | 29 | return $result; |
|
91 | } |
||
92 | } |
||
93 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: