Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
70.59% |
12 / 17 |
|
37.50% |
3 / 8 |
CRAP | |
0.00% |
0 / 1 |
deactivated_super_global | |
70.59% |
12 / 17 |
|
37.50% |
3 / 8 |
11.06 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
error | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
offsetExists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
offsetGet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
offsetSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetUnset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
count | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getIterator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\request; |
15 | |
16 | /** |
17 | * Replacement for a superglobal (like $_GET or $_POST) which calls |
18 | * trigger_error on all operations but isset, overloads the [] operator with SPL. |
19 | */ |
20 | class deactivated_super_global implements \ArrayAccess, \Countable, \IteratorAggregate |
21 | { |
22 | /** |
23 | * @var string Holds the name of the superglobal this is replacing. |
24 | */ |
25 | private $name; |
26 | |
27 | /** |
28 | * @var int (\phpbb\request\request_interface::POST|GET|REQUEST|COOKIE) Super global constant. |
29 | */ |
30 | private $super_global; |
31 | |
32 | /** |
33 | * @var request_interface The request class instance holding the actual request data. |
34 | */ |
35 | private $request; |
36 | |
37 | /** |
38 | * Constructor generates an error message fitting the super global to be used within the other functions. |
39 | * |
40 | * @param request_interface $request A request class instance holding the real super global data. |
41 | * @param string $name Name of the super global this is a replacement for - e.g. '_GET'. |
42 | * @param int $super_global The variable's super global constant (\phpbb\request\request_interface::POST|GET|REQUEST|COOKIE). |
43 | */ |
44 | public function __construct(request_interface $request, string $name, int $super_global) |
45 | { |
46 | $this->request = $request; |
47 | $this->name = $name; |
48 | $this->super_global = $super_global; |
49 | } |
50 | |
51 | /** |
52 | * Calls trigger_error with the file and line number the super global was used in. |
53 | */ |
54 | private function error() |
55 | { |
56 | $file = ''; |
57 | $line = 0; |
58 | |
59 | $message = 'Illegal use of $' . $this->name . '. You must use the request class to access input data. Found in %s on line %d. This error message was generated by deactivated_super_global.'; |
60 | |
61 | $backtrace = debug_backtrace(); |
62 | if (isset($backtrace[1])) |
63 | { |
64 | $file = $backtrace[1]['file']; |
65 | $line = $backtrace[1]['line']; |
66 | } |
67 | trigger_error(sprintf($message, $file, $line), E_USER_ERROR); |
68 | } |
69 | |
70 | /** |
71 | * Redirects isset to the correct request class call. |
72 | * |
73 | * @param string $offset The key of the super global being accessed. |
74 | * |
75 | * @return bool Whether the key on the super global exists. |
76 | */ |
77 | public function offsetExists($offset): bool |
78 | { |
79 | return $this->request->is_set($offset, $this->super_global); |
80 | } |
81 | |
82 | /**#@+ |
83 | * Part of the \ArrayAccess implementation, will always result in a FATAL error. |
84 | */ |
85 | public function offsetGet($offset): void |
86 | { |
87 | $this->error(); |
88 | } |
89 | |
90 | public function offsetSet($offset, $value): void |
91 | { |
92 | $this->error(); |
93 | } |
94 | |
95 | public function offsetUnset($offset): void |
96 | { |
97 | $this->error(); |
98 | } |
99 | /**#@-*/ |
100 | |
101 | /** |
102 | * Part of the \Countable implementation, will always result in a FATAL error |
103 | * @return void |
104 | */ |
105 | #[\ReturnTypeWillChange] public function count(): void |
106 | { |
107 | $this->error(); |
108 | } |
109 | |
110 | /** |
111 | * Part of the Traversable/IteratorAggregate implementation, will always result in a FATAL error |
112 | * @return void |
113 | */ |
114 | #[\ReturnTypeWillChange] public function getIterator(): void |
115 | { |
116 | $this->error(); |
117 | } |
118 | } |