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