1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Maslosoft\Whitelist; |
10
|
|
|
|
11
|
|
|
use function array_sum; |
12
|
|
|
use function count; |
13
|
|
|
use function is_array; |
14
|
|
|
use function is_string; |
15
|
|
|
use Maslosoft\EmbeDi\EmbeDi; |
16
|
|
|
use Maslosoft\EmbeDi\Traits\FlyTrait; |
17
|
|
|
use Maslosoft\Whitelist\Checkers\FunctionChecker; |
18
|
|
|
use Maslosoft\Whitelist\Checkers\MethodChecker; |
19
|
|
|
use Maslosoft\Whitelist\Checkers\StaticMethodChecker; |
20
|
|
|
use Maslosoft\Whitelist\Helpers\ErrorCollector; |
21
|
|
|
use Maslosoft\Whitelist\Interfaces\CheckerInterface; |
22
|
|
|
use Maslosoft\Whitelist\Tokenizer\Tokenizer; |
23
|
|
|
use Psr\Log\LoggerAwareInterface; |
24
|
|
|
use Psr\Log\LoggerAwareTrait; |
25
|
|
|
use Psr\Log\NullLogger; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Whitelist |
29
|
|
|
* |
30
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
31
|
|
|
*/ |
32
|
|
|
class Whitelist implements LoggerAwareInterface |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
use FlyTrait, |
36
|
|
|
LoggerAwareTrait; |
37
|
|
|
|
38
|
|
|
const DefaultInstanceId = 'whitelist'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string[]|CheckerInterface[] |
42
|
|
|
*/ |
43
|
|
|
public $checkers = [ |
44
|
|
|
FunctionChecker::class, |
45
|
|
|
StaticMethodChecker::class, |
46
|
|
|
MethodChecker::class |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Allowed code features |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
public $whitelist = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Required code features |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
public $required = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* List of aspects to check |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
private $aspects = [ |
66
|
|
|
'variables', |
67
|
|
|
'functions', |
68
|
|
|
'classes', |
69
|
|
|
'methods', |
70
|
|
|
'fields', |
71
|
|
|
'constants', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Version number holder |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
private static $_version = null; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* |
82
|
|
|
* @param mixed[]|string $config Configuration array or instance id |
83
|
|
|
*/ |
84
|
|
|
public function __construct($config = self::DefaultInstanceId) |
85
|
|
|
{ |
86
|
|
|
assert(!empty($config)); |
87
|
|
|
assert(is_string($config) || is_array($config)); |
88
|
|
|
$this->logger = new NullLogger; |
89
|
|
|
|
90
|
|
|
if (is_string($config)) |
91
|
|
|
{ |
92
|
|
|
EmbeDi::fly($config)->configure($this); |
93
|
|
|
} |
94
|
|
|
else |
95
|
|
|
{ |
96
|
|
|
EmbeDi::fly()->apply($config, $this); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Instantiate checkers if not already provided as instances |
100
|
|
|
$checkers = []; |
101
|
|
|
foreach($this->checkers as $checker) |
102
|
|
|
{ |
103
|
|
|
if(!$checker instanceof CheckerInterface) |
104
|
|
|
{ |
105
|
|
|
assert(is_string($checker) || is_array($checker)); |
106
|
|
|
$checkers[] = EmbeDi::fly()->apply($checker); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
$this->checkers = $checkers; |
110
|
|
|
|
111
|
|
|
// Ensure that all aspects have sane values |
112
|
|
|
foreach(['whitelist', 'require'] as $name) |
113
|
|
|
{ |
114
|
|
|
foreach($this->aspects as $aspect) |
115
|
|
|
{ |
116
|
|
|
if(empty($this->$name[$aspect]) || !is_array($this->$name[$aspect])) |
117
|
|
|
{ |
118
|
|
|
$this->$name[$aspect] = []; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function check($code, ErrorCollector $ec = null) |
125
|
|
|
{ |
126
|
|
|
$tokenizer = new Tokenizer($code); |
127
|
|
|
|
128
|
|
|
if(empty($ec)) |
129
|
|
|
{ |
130
|
|
|
$ec = new ErrorCollector; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$results = []; |
134
|
|
|
|
135
|
|
|
foreach($this->checkers as $checker) |
136
|
|
|
{ |
137
|
|
|
assert($checker instanceof CheckerInterface); |
138
|
|
|
if($checker instanceof LoggerAwareInterface) |
139
|
|
|
{ |
140
|
|
|
$checker->setLogger($this->logger); |
141
|
|
|
} |
142
|
|
|
$results[] = (int) $checker->check($this, $tokenizer, $ec); |
143
|
|
|
} |
144
|
|
|
return array_sum($results) === count($results); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function checkFile($fileName) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get whitelist version |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public function getVersion() |
157
|
|
|
{ |
158
|
|
|
if (null === self::$_version) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
self::$_version = require __DIR__ . '/version.php'; |
161
|
|
|
} |
162
|
|
|
return self::$_version; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.