1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Security\Protections\Throttle; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use Psr\Container\ContainerExceptionInterface; |
19
|
|
|
use Psr\Container\ContainerInterface; |
20
|
|
|
use O2System\Psr\NotFoundExceptionInterface; |
|
|
|
|
21
|
|
|
use Traversable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Repository |
25
|
|
|
* @package O2System\Kernel\Containers |
26
|
|
|
*/ |
27
|
|
|
class Repository implements |
28
|
|
|
\ArrayAccess, |
29
|
|
|
\IteratorAggregate, |
30
|
|
|
\Countable, |
31
|
|
|
\Serializable, |
32
|
|
|
\JsonSerializable, |
33
|
|
|
ContainerInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Repository::__construct |
37
|
|
|
*/ |
38
|
|
|
public function __construct() |
39
|
|
|
{ |
40
|
|
|
if (empty($_SESSION[ 'throttle' ])) { |
41
|
|
|
$_SESSION[ 'throttle' ] = []; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// ------------------------------------------------------------------------ |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retrieve an external iterator |
49
|
|
|
* |
50
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
51
|
|
|
* @return Traversable An instance of an object implementing <b>Iterator</b> or |
52
|
|
|
* <b>Traversable</b> |
53
|
|
|
* @since 5.0.0 |
54
|
|
|
*/ |
55
|
|
|
public function getIterator() |
56
|
|
|
{ |
57
|
|
|
return new \ArrayIterator($_SESSION[ 'throttle' ]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// ------------------------------------------------------------------------ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Globals::exists |
64
|
|
|
* |
65
|
|
|
* Checks if the data exists on the storage. |
66
|
|
|
* An alias of Globals::__isset method. |
67
|
|
|
* |
68
|
|
|
* @param string $offset The object offset key. |
69
|
|
|
* |
70
|
|
|
* @return bool Returns TRUE on success or FALSE on failure. |
71
|
|
|
*/ |
72
|
|
|
public function exists($offset) |
73
|
|
|
{ |
74
|
|
|
return $this->__isset($offset); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// ------------------------------------------------------------------------ |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Globals__isset |
81
|
|
|
* |
82
|
|
|
* Implementing magic method __isset to simplify when checks if offset exists on PHP native session variable, |
83
|
|
|
* just simply calling isset( $globals[ 'offset' ] ). |
84
|
|
|
* |
85
|
|
|
* @param mixed $offset PHP native GLOBALS offset. |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function __isset($offset) |
90
|
|
|
{ |
91
|
|
|
return $this->offsetExists($offset); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// ------------------------------------------------------------------------ |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Whether a offset exists |
98
|
|
|
* |
99
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
100
|
|
|
* |
101
|
|
|
* @param mixed $offset <p> |
102
|
|
|
* An offset to check for. |
103
|
|
|
* </p> |
104
|
|
|
* |
105
|
|
|
* @return boolean true on success or false on failure. |
106
|
|
|
* </p> |
107
|
|
|
* <p> |
108
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
109
|
|
|
* @since 5.0.0 |
110
|
|
|
*/ |
111
|
|
|
public function offsetExists($offset) |
112
|
|
|
{ |
113
|
|
|
return isset($_SESSION[ 'throttle' ][ $offset ]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// ------------------------------------------------------------------------ |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Session::__get |
120
|
|
|
* |
121
|
|
|
* Implementing magic method __get to simplify gets PHP native session variable by requested offset, |
122
|
|
|
* just simply calling isset( $session[ 'offset' ] ). |
123
|
|
|
* |
124
|
|
|
* @param $offset |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function &__get($offset) |
129
|
|
|
{ |
130
|
|
|
return $_SESSION[ 'throttle' ][ $offset ]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// ------------------------------------------------------------------------ |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Globals__set |
137
|
|
|
* |
138
|
|
|
* Implementing magic method __set to simplify set PHP native GLOBALS variable, |
139
|
|
|
* just simply calling $globals->offset = 'foo'. |
140
|
|
|
* |
141
|
|
|
* @param mixed $offset PHP native GLOBALS offset. |
142
|
|
|
* @param mixed $value PHP native GLOBALS offset value to set. |
143
|
|
|
*/ |
144
|
|
|
public function __set($offset, $value) |
145
|
|
|
{ |
146
|
|
|
$this->offsetSet($offset, $value); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// ------------------------------------------------------------------------ |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Globals::store |
153
|
|
|
* |
154
|
|
|
* Store the data into the storage. |
155
|
|
|
* An alias of Globals::__set method. |
156
|
|
|
* |
157
|
|
|
* @param string $offset The data offset key. |
158
|
|
|
* @param mixed $value The data to be stored. |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function store($offset, $value) |
163
|
|
|
{ |
164
|
|
|
$this->__set($offset, $value); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// ------------------------------------------------------------------------ |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Offset to set |
171
|
|
|
* |
172
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
173
|
|
|
* |
174
|
|
|
* @param mixed $offset <p> |
175
|
|
|
* The offset to assign the value to. |
176
|
|
|
* </p> |
177
|
|
|
* @param mixed $value <p> |
178
|
|
|
* The value to set. |
179
|
|
|
* </p> |
180
|
|
|
* |
181
|
|
|
* @return void |
182
|
|
|
* @since 5.0.0 |
183
|
|
|
*/ |
184
|
|
|
public function offsetSet($offset, $value) |
185
|
|
|
{ |
186
|
|
|
$_SESSION[ 'throttle' ][ $offset ] = $value; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// ------------------------------------------------------------------------ |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Globals::remove |
193
|
|
|
* |
194
|
|
|
* Removes a data from the storage. |
195
|
|
|
* An alias of Globals::__unset method. |
196
|
|
|
* |
197
|
|
|
* @param string $offset The object offset key. |
198
|
|
|
* |
199
|
|
|
* @return void |
200
|
|
|
*/ |
201
|
|
|
public function remove($offset) |
202
|
|
|
{ |
203
|
|
|
$this->__unset($offset); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// ------------------------------------------------------------------------ |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Globals__unset |
210
|
|
|
* |
211
|
|
|
* Implementing magic method __unset to simplify unset method, just simply calling |
212
|
|
|
* unset( $globals[ 'offset' ] ). |
213
|
|
|
* |
214
|
|
|
* @param mixed $offset PHP Native GLOBALS offset |
215
|
|
|
* |
216
|
|
|
* @return void |
217
|
|
|
*/ |
218
|
|
|
public function __unset($offset) |
219
|
|
|
{ |
220
|
|
|
$this->offsetUnset($offset); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// ------------------------------------------------------------------------ |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Offset to unset |
227
|
|
|
* |
228
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
229
|
|
|
* |
230
|
|
|
* @param mixed $offset <p> |
231
|
|
|
* The offset to unset. |
232
|
|
|
* </p> |
233
|
|
|
* |
234
|
|
|
* @return void |
235
|
|
|
* @since 5.0.0 |
236
|
|
|
*/ |
237
|
|
|
public function offsetUnset($offset) |
238
|
|
|
{ |
239
|
|
|
if (isset($_SESSION[ 'throttle' ][ $offset ])) { |
240
|
|
|
unset($_SESSION[ 'throttle' ][ $offset ]); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// ------------------------------------------------------------------------ |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Globals::merge |
248
|
|
|
* |
249
|
|
|
* Merge new array of data into the data storage. |
250
|
|
|
* |
251
|
|
|
* @param array $data New array of data. |
252
|
|
|
* |
253
|
|
|
* @return array The old array of data storage. |
254
|
|
|
*/ |
255
|
|
|
public function merge(array $data) |
256
|
|
|
{ |
257
|
|
|
$oldData = $_SESSION[ 'throttle' ]; |
258
|
|
|
$_SESSION[ 'throttle' ] = array_merge($_SESSION[ 'throttle' ], $data); |
259
|
|
|
|
260
|
|
|
return $oldData; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
// ------------------------------------------------------------------------ |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Globals::exchange |
267
|
|
|
* |
268
|
|
|
* Exchange the array of data storage into the new array of data. |
269
|
|
|
* |
270
|
|
|
* @param array $data New array of data. |
271
|
|
|
* |
272
|
|
|
* @return array The old array of data storage. |
273
|
|
|
*/ |
274
|
|
|
public function exchange(array $data) |
275
|
|
|
{ |
276
|
|
|
$oldData = $_SESSION[ 'throttle' ]; |
277
|
|
|
$_SESSION[ 'throttle' ] = $data; |
278
|
|
|
|
279
|
|
|
return $oldData; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
// ------------------------------------------------------------------------ |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Globals::destroy |
286
|
|
|
* |
287
|
|
|
* Removes all object from the container and perform each object destruction. |
288
|
|
|
* |
289
|
|
|
* @return array Array of old storage items. |
290
|
|
|
*/ |
291
|
|
|
public function destroy() |
292
|
|
|
{ |
293
|
|
|
$storage = $_SESSION[ 'throttle' ]; |
294
|
|
|
|
295
|
|
|
$_SESSION[ 'throttle' ] = []; |
296
|
|
|
|
297
|
|
|
return $storage; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
// ------------------------------------------------------------------------ |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Globals::count |
304
|
|
|
* |
305
|
|
|
* Application of Countable::count method to count the numbers of contained objects. |
306
|
|
|
* |
307
|
|
|
* @see http://php.net/manual/en/countable.count.php |
308
|
|
|
* @return int The numbers of data on the storage. |
309
|
|
|
*/ |
310
|
|
|
public function count() |
311
|
|
|
{ |
312
|
|
|
return (int)count($_SESSION[ 'throttle' ]); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
// ------------------------------------------------------------------------ |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Globals::serialize |
319
|
|
|
* |
320
|
|
|
* Application of Serializable::serialize method to serialize the data storage. |
321
|
|
|
* |
322
|
|
|
* @see http://php.net/manual/en/serializable.serialize.php |
323
|
|
|
* |
324
|
|
|
* @return string The string representation of the serialized data storage. |
325
|
|
|
*/ |
326
|
|
|
public function serialize() |
327
|
|
|
{ |
328
|
|
|
return serialize($_SESSION[ 'throttle' ]); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
// ------------------------------------------------------------------------ |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Globals::unserialize |
335
|
|
|
* |
336
|
|
|
* Application of Serializable::unserialize method to unserialize and construct the data storage. |
337
|
|
|
* |
338
|
|
|
* @see http://php.net/manual/en/serializable.unserialize.php |
339
|
|
|
* |
340
|
|
|
* @param string $serialized The string representation of the serialized data storage. |
341
|
|
|
* |
342
|
|
|
* @return void |
343
|
|
|
*/ |
344
|
|
|
public function unserialize($serialized) |
345
|
|
|
{ |
346
|
|
|
$_SESSION[ 'throttle' ] = unserialize($serialized); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
// ------------------------------------------------------------------------ |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Globals::jsonSerialize |
353
|
|
|
* |
354
|
|
|
* Specify data which should be serialized to JSON |
355
|
|
|
* |
356
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
357
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
358
|
|
|
* which is a value of any type other than a resource. |
359
|
|
|
* @since 5.4.0 |
360
|
|
|
*/ |
361
|
|
|
public function jsonSerialize() |
362
|
|
|
{ |
363
|
|
|
return $_SESSION[ 'throttle' ]; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
// ------------------------------------------------------------------------ |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Globals::getArrayCopy |
370
|
|
|
* |
371
|
|
|
* Gets a copy of the data storage. |
372
|
|
|
* |
373
|
|
|
* @return array Returns a copy of the data storage. |
374
|
|
|
*/ |
375
|
|
|
public function getArrayCopy() |
376
|
|
|
{ |
377
|
|
|
return $_SESSION[ 'throttle' ]; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
// ------------------------------------------------------------------------ |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Finds an entry of the container by its identifier and returns it. |
384
|
|
|
* |
385
|
|
|
* @param string $id Identifier of the entry to look for. |
386
|
|
|
* |
387
|
|
|
* @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
388
|
|
|
* @throws ContainerExceptionInterface Error while retrieving the entry. |
389
|
|
|
* |
390
|
|
|
* @return mixed Entry. |
391
|
|
|
*/ |
392
|
|
|
public function get($id) |
393
|
|
|
{ |
394
|
|
|
if ($this->has($id)) { |
395
|
|
|
return $this->offsetGet($id); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
// @todo throw exception |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
// ------------------------------------------------------------------------ |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Returns true if the container can return an entry for the given identifier. |
405
|
|
|
* Returns false otherwise. |
406
|
|
|
* |
407
|
|
|
* `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
408
|
|
|
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
409
|
|
|
* |
410
|
|
|
* @param string $id Identifier of the entry to look for. |
411
|
|
|
* |
412
|
|
|
* @return bool |
413
|
|
|
*/ |
414
|
|
|
public function has($id) |
415
|
|
|
{ |
416
|
|
|
return (bool)$this->offsetExists($id); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
// ------------------------------------------------------------------------ |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Offset to retrieve |
423
|
|
|
* |
424
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
425
|
|
|
* |
426
|
|
|
* @param mixed $offset <p> |
427
|
|
|
* The offset to retrieve. |
428
|
|
|
* </p> |
429
|
|
|
* |
430
|
|
|
* @return mixed Can return all value types. |
431
|
|
|
* @since 5.0.0 |
432
|
|
|
*/ |
433
|
|
|
public function offsetGet($offset) |
434
|
|
|
{ |
435
|
|
|
return (isset($_SESSION[ 'throttle' ][ $offset ])) ? $_SESSION[ 'throttle' ][ $offset ] : false; |
436
|
|
|
} |
437
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths