-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathFunctionPatcher.php
195 lines (167 loc) · 3.71 KB
/
FunctionPatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* Part of CI PHPUnit Test
*
* @author Kenji Suzuki <https://github.com/kenjis>
* @license MIT License
* @copyright 2015 Kenji Suzuki
* @link https://github.com/kenjis/ci-phpunit-test
*/
namespace Kenjis\MonkeyPatch\Patcher;
require __DIR__ . '/FunctionPatcher/NodeVisitor.php';
require __DIR__ . '/FunctionPatcher/Proxy.php';
use LogicException;
use Kenjis\MonkeyPatch\Patcher\FunctionPatcher\NodeVisitor;
class FunctionPatcher extends AbstractPatcher
{
private static $lock_function_list = false;
/**
* @var array list of function names (in lower case) which you patch
*/
private static $whitelist = [
'mt_rand',
'rand',
'uniqid',
'hash_hmac',
'md5',
'sha1',
'hash',
'time',
'microtime',
'date',
'function_exists',
// Functions that have param called by reference
// Need to prepare method in FunctionPatcher\Proxy class
'openssl_random_pseudo_bytes',
];
/**
* @var array list of function names (in lower case) which can't be patched
*/
private static $blacklist = [
// Segmentation fault
'call_user_func_array',
'exit__',
// Error: Only variables should be assigned by reference
'get_instance',
'get_config',
'load_class',
'get_mimes',
'_get_validation_object',
// has reference param
'preg_replace',
'preg_match',
'preg_match_all',
'array_unshift',
'array_shift',
'sscanf',
'ksort',
'krsort',
'str_ireplace',
'str_replace',
'is_callable',
'flock',
'end',
'idn_to_ascii',
// Special functions for ci-phpunit-test
'show_404',
'show_error',
'redirect',
];
public static $replacement;
public function __construct()
{
$this->node_visitor = new NodeVisitor();
}
protected static function checkLock($error_msg)
{
if (self::$lock_function_list)
{
throw new LogicException($error_msg);
}
}
public static function addWhitelists(array $function_list)
{
self::checkLock("You can't add to whitelist after initialization");
foreach ($function_list as $function_name)
{
self::$whitelist[] = strtolower($function_name);
}
}
/**
* @return array
*/
public static function getFunctionWhitelist()
{
return self::$whitelist;
}
public static function addBlacklist($function_name)
{
self::checkLock("You can't add to blacklist after initialization");
self::$blacklist[] = strtolower($function_name);
}
public static function removeBlacklist($function_name)
{
self::checkLock("You can't remove from blacklist after initialization");
$key = array_search(strtolower($function_name), self::$blacklist);
array_splice(self::$blacklist, $key, 1);
}
public static function lockFunctionList()
{
self::$lock_function_list = true;
}
/**
* @param string $name function name
* @return boolean
*/
public static function isWhitelisted($name)
{
if (in_array(strtolower($name), self::$whitelist))
{
return true;
}
return false;
}
/**
* @param string $name function name
* @return boolean
*/
public static function isBlacklisted($name)
{
if (in_array(strtolower($name), self::$blacklist))
{
return true;
}
return false;
}
protected static function generateNewSource($source)
{
$tokens = token_get_all($source);
$new_source = '';
$i = -1;
ksort(self::$replacement);
reset(self::$replacement);
$replacement = each(self::$replacement);
foreach ($tokens as $token)
{
$i++;
if (is_string($token))
{
$new_source .= $token;
}
elseif ($i == $replacement['key'])
{
$new_source .= $replacement['value'];
$replacement = each(self::$replacement);
}
else
{
$new_source .= $token[1];
}
}
if ($replacement !== false)
{
throw new LogicException('Replacement data still remain');
}
return $new_source;
}
}