PluginProbe
WebTotem Security / 2.2.3
WebTotem Security v2.2.3
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / routes.php

routes.php in WebTotem Security 2.2.3, at routes.php

1,086 lines 39.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php defined('ABSPATH') or die("Protected By WT!");
2
3 add_action('admin_menu', 'wtsec_add_menu_link');
4 add_action('wp_ajax_change_status', 'wtsec_ajax_changeStatus');
5 add_action('admin_post_change_status', 'wtsec_changeStatus');
6 add_action('admin_post_login_form', 'wtsec_login_form');
7 add_action('wp_ajax_cmd', 'wtsec_cmd_ajax');
8 add_action('admin_post_ajax_cmd', 'wtsec_cmd_ajax');
9 add_action('wp_ajax_ajax_firewall', 'wtsec_ajax_firewall');
10 add_action('wp_ajax_ajax_antivirus', 'wtsec_ajax_antivirus');
11 add_action('wp_ajax_chart_filter', 'wtsec_chart_filter');
12 add_action('wp_ajax_map_filter', 'wtsec_map_filter');
13 add_action('wp_ajax_color_scheme_toggle', 'wtsec_color_scheme');
14 add_action('wp_ajax_configs_toggle', 'wtsec_configs_toggle');
15
16
17 function wtsec_add_menu_link()
18 {
19 add_menu_page(
20 WTSEC_PAGE_TITLE,
21 WTSEC_MENU_TITLE,
22 'manage_options',
23 wtsec_getRoute('dashboard'),
24 'wtsec_index_page',
25 '',
26 '1'
27 );
28 $parent = wtsec_getRoute('dashboard');
29 if (WTSEC_LIBRARY_App::authorized()) {
30 $capability = 'manage_options';
31 foreach (wtsec_pages() as $page => $arguments) {
32 add_submenu_page($parent, $arguments['page_title'], $arguments['menu_title'], $capability, wtsec_getRoute($page), $arguments['function']);
33 }
34 add_submenu_page(null, WTSEC_LIBRARY_Localization::lmsg('sign_in'), WTSEC_LIBRARY_Localization::lmsg('sign_in'), 'manage_options', wtsec_getRoute('login'), 'wtsec_login');
35 add_submenu_page($parent, WTSEC_LIBRARY_Localization::lmsg('logout'), WTSEC_LIBRARY_Localization::lmsg('logout'), 'manage_options', wtsec_getRoute('logout'), 'wtsec_logout');
36 } else {
37 $capability = 'manage_options';
38 foreach (wtsec_pages() as $page => $arguments) {
39 add_submenu_page(null, $arguments['page_title'], $arguments['menu_title'], $capability, wtsec_getRoute($page), function () {
40 wp_safe_redirect(wtsec_getUrl('login'));
41 });
42 }
43 add_submenu_page($parent, WTSEC_LIBRARY_Localization::lmsg('sign_in'), WTSEC_LIBRARY_Localization::lmsg('sign_in'), 'manage_options', wtsec_getRoute('login'), 'wtsec_login');
44 add_submenu_page(null, WTSEC_LIBRARY_Localization::lmsg('logout'), WTSEC_LIBRARY_Localization::lmsg('logout'), 'manage_options', wtsec_getRoute('logout'), 'wtsec_logout');
45 }
46 }
47
48 function dd($vl)
49 {
50 echo '<pre>';
51 print_r($vl);
52 echo '</pre>';
53 die();
54 }
55
56 function myGrading($score)
57 {
58 if ($score < 0 || $score > 100) return ['grade' => '','color' => ''];
59 $scores = [100 => 'A+', 90 => 'A', 80 => 'A-', 70 => 'B+', 60 => 'B', 50 => 'B-', 35 => 'C+', 20 => 'C', 0 => 'C-' ];
60 foreach ($scores as $key => $value){
61 if($score >= $key){
62 $grade = $value;
63 break;
64 }
65 }
66
67 $color = ($score >= 80) ? 'green' : ($score >= 50) ? 'orange' : 'red';
68
69 return ['grade' => $grade,'color' => $color];
70 }
71
72
73 function convertTimeToReadable($date)
74 {
75 if(!$date){
76 return WTSEC_LIBRARY_Localization::lmsg('unknown');
77 }
78
79 $lang = get_locale();
80 //enable russian language
81 if ($lang === "ru_RU") {
82 $lang .= ".UTF-8";
83 setlocale(LC_ALL, $lang);
84 }
85 if(strpos($date, ".") !== false){
86 $date = substr($date, 0, strpos($date, "."));
87 }
88 return strftime("%B %e, %Y / %R", strtotime($date." UTC"));
89 }
90
91
92 function convertToCurrentTime($date)
93 {
94 $lang = get_locale();
95 //enable russian language
96 if ($lang === "ru_RU") {
97 $lang .= ".UTF-8";
98 setlocale(LC_ALL, $lang);
99 }
100 if(strpos($date, ".") !== false){
101 $date = substr($date, 0, strpos($date, "."));
102 }
103 $date .= " UTC";
104 $current_time = strtotime($date);
105 return ['date' => date("Y-m-d", $current_time),'time' => date("H:i:s",$current_time)];
106 }
107
108 function wtsec_index_page()
109 {
110 $host = WTSEC_LIBRARY_WT::getOwnSite();
111
112 $services = [];
113 if (!empty($host)) {
114 $services = [
115 "settings" => [
116 "token" => WTSEC_LIBRARY_App::getToken(),
117 "site_id" => $host['id'],
118 ],
119 "score" => [
120 "total_score" => 0,
121 "tested_on" => "",
122 "server_ip" => "",
123 "location" => "",
124 "description" => wtsec_locale("score.description"),
125 "information" => "",
126 "grade" => "",
127 ],
128 ];
129
130 $score = json_decode(WTSEC_LIBRARY_WT::getScore(), true);
131 if (!empty($score)) {
132 $total_score = round($score['totalScore']);
133 $my_grading = myGrading($total_score);
134 $services["score"] = [
135 "total_score" => $total_score."%",
136 "tested_on" => convertTimeToReadable($score['date']),
137 "server_ip" => $score['ip'],
138 "location" => $score['country'],
139 "description" => wtsec_locale("score.description"),
140 "information" => wtsec_locale("score.higher_than_percent", ['percent' => $score['ishigherthan']]),
141 "grade" => $my_grading['grade'],
142 "color" => $my_grading['color'],
143 ];
144 }
145
146 $checks = WTSEC_LIBRARY_WT::getAllChecks($host['id']);
147 foreach ($checks as $service => $site) {
148 if (empty($site) || !is_array($site)) {
149 continue;
150 }
151 switch ($service) {
152 case "availability":
153 $services["wa"] = [
154 // "pause" => wtsec_getStatusStartPauseIcon($site['config']['isActive'], $site['config']['id'], $host['id']),
155 "pause" => "",
156 "status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']),
157 "response_time" => ceil($site['responseTime'] / 1000000) . ' ' . WTSEC_LIBRARY_Localization::lmsg('ms'),
158 "availability" => $site['percent'] . '%',
159 "availability_percent" => $site['percent'],
160 "last_test" => convertTimeToReadable($site['lastTest']['time']),
161 "downtime" => ceil($site['downTime'] / 1000000) . ' ' . WTSEC_LIBRARY_Localization::lmsg('ms'),
162 "description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.availability')
163 ];
164 break;
165 case "ssl":
166 if ((int)$site['expiryDate'] === 0) {
167 $days_left = 0;
168 } else {
169 $earlier = new DateTime();
170 $later = new DateTime($site['expiryDate']);
171 $days_left = $later->diff($earlier)->format("%a");
172 }
173 $services["ssl"] = [
174 "pause" => "",
175 "information" => wtsec_getInformation($service, $site['status']),
176 "status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']),
177 "days_left" => $days_left,
178 "issue_date" => convertTimeToReadable($site['issueDate']),
179 "expiry_date" => convertTimeToReadable($site['expiryDate']),
180 "description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.ssl'),
181 "solve_it" => in_array($site['status'],['clean','pending']) ? '' : wtsec_locale('solve_it'),
182 ];
183 break;
184 case "domain":
185 if ((int)$site['expiredDate'] === 0) {
186 $days_left = 0;
187 } else {
188 $earlier = new DateTime();
189 $later = new DateTime($site['expiredDate']);
190 $days_left = $later->diff($earlier)->format("%a");
191 }
192
193 $services["dec"] = [
194 "pause" => "",
195 "registrar" => $site['registrar'],
196 "owner" => $site['owner'],
197 "information" => wtsec_getInformation($service, $site['status']),
198 "email" => $site['email'],
199 "status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']),
200 "days_left" => $days_left,
201 "created" => convertTimeToReadable($site['createdDate']),
202 "expiry_date" => convertTimeToReadable($site['expiredDate']),
203 "description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.domain')
204 ];
205 break;
206 case "reputation":
207 $count = 0;
208 if ($site['status'] != "clean") {
209 foreach ($site['virusList'] as &$list) {
210 if (!empty($list['virus']['type'])) {
211 $count++;
212 }
213 }
214 }
215 $services["av"] = [
216 "pause" => "",
217 "status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']),
218 "time_of_the_last_test" => convertTimeToReadable($site['lastTest']['time']),
219 "blacklists_entries" => $count,
220 "description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.reputation'),
221 "reputation" => $site['status'] === "infected" ? wtsec_locale('bad_reputation') : wtsec_locale('good_reputation'),
222 ];
223 break;
224 case "maliciousScript":
225 $services["cms"] = [
226 "pause" => "",
227 "status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']),
228 "time_of_the_last_test" => $site['lastTest']['time'],
229 "description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.malicious')
230 ];
231 break;
232 case "deface":
233 $services["dc"] = [
234 "pause" => "",
235 "status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']),
236 "time_of_the_last_test" => convertTimeToReadable($site['lastTest']['time']),
237 "number" => $site['count'],
238 "description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.deface')
239 ];
240 break;
241 case "ports":
242 $services["ps"] = [
243 "pause" => "",
244 "status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']),
245 "ip" => $site['ip'],
246 "last_test" => convertTimeToReadable($site['lastTest']['time']),
247 "number" => count($site['tcp']),
248 "tcp" => !empty($site['tcp']) ? implode(",", $site['tcp']) : '',
249 "udp" => "",
250 "description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.port')
251 ];
252 break;
253 case "firewall":
254 $service = "WAF";
255 $_service = strtolower($service);
256 $domain = $host['hostname'];
257 $uid = $host['id'];
258
259 $chart = generateChart($site['chart'], 30);
260 // $status = wtsec_checkStatus($uid, $service);
261
262 $data = WTSEC_LIBRARY_WT::getFirewall($host['id']);
263 $data = $data['data']['auth']['viewer']['sites']['one']['firewall'];
264
265 $countryAttacks = getCountryAttacks($data['map'], $chart['count_attacks']);
266 $edges = $data['logs']['edges'];
267
268 $services["waf"] = [
269 "pause" => "",
270 "site_address" => $domain,
271 "status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']),
272 "_status" => $site['status'],
273 "installed_status" => in_array($site['status'], ["not_installed", "error", "down", "not_supported", "not_registered"]) ? false : true,
274 "time_of_the_last_check" => convertTimeToReadable($site['lastTest']['time']),
275 "attacks" => $chart['count_attacks'],
276 "blocking" => $chart['count_blocks'],
277 "non-blocking" => $chart['count_attacks'] - $chart['count_blocks'],
278 "description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.firewall'),
279 "actions" => wtsec_generateButtons($uid, $_service, $service, $site['status'], WTSEC_SITE_URL),
280 "chart" => json_encode($chart['chart'], true),
281 "edges" => $edges,
282 "countryAttacks" => $countryAttacks,
283 ];
284 break;
285 case "antivirus":
286 $service = "AV";
287 $_service = strtolower($service);
288 $domain = $host['hostname'];
289 $uid = $host['id'];
290 // $status = wtsec_checkStatus($uid, $service);
291 $services["vc"] = [
292 "pause" => "",
293 "site_address" => $domain,
294 "status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']),
295 "installed_status" => in_array($site['status'], ["not_installed", "error", "down", "not_supported", "not_registered"]) ? false : true,
296 "_status" => $site['status'],
297 "signatures" => (int)$site['stats']['infected'],
298 "changes" => (int)$site['stats']['changed'],
299 "scanned" => (int)$site['stats']['scaned'],
300 "deleted" => (int)$site['stats']['deleted'],
301 "list" => $site['stats']["infected"],
302 "actions" => wtsec_generateButtons($uid, $_service, $service, $site['status'], WTSEC_SITE_URL),
303 "description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.antivirus')
304 ];
305 break;
306 }
307 }
308
309 //check AM, if not installed , run install
310 $process = "installing";
311 $check_am_file = wtsec_checkInstalledFile("am");
312 $am_installed = $check_am_file["status"];
313 $am_installed_status = wtsec_app()->get('am_installed');
314
315 if (!$am_installed || !$am_installed_status) {
316 $am_is_installed = wtsec_cmd("am_install", "AM", "am", $host['id'], [], WTSEC_SITE_URL);
317 if (!$am_is_installed) {
318 $process = "failed";
319 wtsec_app()->set('am_installed', false);
320 } else {
321 wtsec_app()->set('am_installed', true);
322 }
323 } elseif ($services["vc"]["_status"] === "not_installed" || $services["waf"]["_status"] === "not_installed") {
324 $process = "installing";
325 } else {
326 $process = "installed";
327 }
328
329 $services["am"] = [
330 "status" => WTSEC_LIBRARY_WT::getStatusIcon($am_installed ? "working" : "not_installed"),
331 "actions" => wtsec_generateButtons($host['id'], "am", "AM", ["active" => $check_am_file], WTSEC_SITE_URL),
332 "process_status" => $process,
333 "installed" => $am_installed,
334 ];
335 }
336
337 wtsec_layout("dashboard", $services);
338 }
339
340 function getCountryAttacks($map, $countAttacks){
341
342 if($map){
343 $attackKey = array_search(max(array_column($map, 'attacks')), array_column($map, 'attacks'));
344 $map[$attackKey]['percent'] = ($countAttacks) ? round($map[$attackKey]['attacks'] / $countAttacks * 100) : 0;
345 $map[$attackKey]['country'] = wtsec_getCountryName($map[$attackKey]['country']);
346 return $map[$attackKey];
347 }
348
349 return ['percent' => 0, 'country' => false];
350
351 }
352 function getAttacksMap($map){
353 $attacks = [];
354 $countries = [];
355 foreach ($map as $value){
356 $attacks[] = $value['attacks'];
357 $countries[] = wtsec_getCountryName($value['country']);
358 }
359 return ['attacks' => $attacks, 'countries' => $countries];
360 }
361
362
363 function generateChart($charts, $days = 7)
364 {
365
366 if(is_array($days)) {
367 $dateDiff = strtotime($days['end'] - strtotime($days['begin']));
368 $days = floor($dateDiff / (60 * 60 * 24));
369 }
370
371 if($days <= 1){
372 $begin = new DateTime( date('Y-m-d 00:00:00') );
373 $end = new DateTime( date('Y-m-d 23:59:59') );
374 $step = 'hour';
375 } elseif($days <= 31){
376 $begin = new DateTime( date('Y-m-d', strtotime('-'.($days - 1).' day')) );
377 $end = new DateTime( date('Y-m-d') );
378 $step = 'day';
379 } else {
380 $begin = new DateTime( date('Y-m-01', strtotime('- 11 month')) );
381 $end = new DateTime( date('Y-m-01', time()) );
382 $step = 'month';
383 }
384
385
386 $count_attacks = 0;
387 $count_blocks = 0;
388 $c = [];
389
390 for($i = $begin; $i <= $end; $i->modify('+1 '.$step)){
391
392 $c[$i->format('U')] = [
393 'date' => ($days <= 1) ? $i->format("Y-m-d H:00:00") : $i->format("Y-m-d"),
394 'count' => 0,
395 'attacks' => 0,
396 'blocked' => 0,
397 ];
398 }
399
400 foreach ($charts as $chart) {
401 $d = strtotime($chart['time']);
402 $c[$d]['count'] = $chart['blocked'];
403 $c[$d]['attacks'] = $chart['attacks'];
404 $c[$d]['blocked'] = $chart['blocked'];
405 $count_attacks += $chart['attacks'];
406 $count_blocks += $chart['blocked'];
407 }
408
409
410 //unset keys, because the js parser sorting by key in integer
411 $result = [];
412 foreach ($c as $d) {
413 if(isset($d['date'])){
414 $result[] = $d;
415 }
416 }
417
418 return ['chart' => $result, 'count_attacks' => $count_attacks, 'count_blocks' => $count_blocks];
419 }
420
421 function wtsec_getInformation($service, $status)
422 {
423 $information = [
424 "ssl" => [
425 WTSEC_LIBRARY_Localization::lmsg("statuses.ok"),
426 WTSEC_LIBRARY_Localization::lmsg("statuses.invalid"),
427 WTSEC_LIBRARY_Localization::lmsg("statuses.expired"),
428 WTSEC_LIBRARY_Localization::lmsg("statuses.expires"),
429 "unknown_status" => WTSEC_LIBRARY_Localization::lmsg("statuses.missing"),
430 ],
431 "domain" => [
432 WTSEC_LIBRARY_Localization::lmsg("statuses.ok"),
433 WTSEC_LIBRARY_Localization::lmsg("statuses.expires"),
434 "unknown_status" => WTSEC_LIBRARY_Localization::lmsg("statuses.error"),
435 ]
436 ];
437 return isset($information[$service][$status]) ? $information[$service][$status] : $information[$service]["unknown_status"];
438 }
439
440 function wtsec_getStatusStartPauseIcon($status, $config_id, $host_id)
441 {
442 $icon = '';
443 switch ($status) {
444 case "1":
445 $icon = '<div class="v-pause ww-icon ww-icon--pause" style="border:none" data-config_id="' . $config_id . '" data-host_id="' . $host_id . '"></div>';
446 break;
447 case "0":
448 $icon = '<div class="v-pause ww-icon ww-icon--play" style="border:none" data-config_id="' . $config_id . '" data-host_id="' . $host_id . '"></div>';
449 break;
450 }
451 return $icon;
452 }
453
454 function wtsec_options_page()
455 {
456 $host = WTSEC_LIBRARY_WT::getOwnSite();
457 $options = wtsec_getConfigs($host['id']);
458 wtsec_layout("options", $options);
459 }
460
461 function wtsec_services_page()
462 {
463 wtsec_layout("services");
464 }
465
466 function wtsec_antivirus_page()
467 {
468 $host = WTSEC_LIBRARY_WT::getOwnSite();
469 $services = [];
470 if (!empty($host)) {
471 $site = WTSEC_LIBRARY_WT::getAntivirus($host['id']);
472 if (isset($site['data']['auth']['viewer']['sites']['one']['antivirus']['log'])) {
473 $site = $site['data']['auth']['viewer']['sites']['one']['antivirus'];
474
475 $_SESSION['antivirus_endCursor'] = $site['log']['pageInfo']['endCursor'];
476 $_SESSION['antivirus_hasNextPage'] = $site['log']['pageInfo']['hasNextPage'];
477
478 $service = "AV";
479 $_service = strtolower($service);
480 $domain = $host['hostname'];
481 $uid = $host['id'];
482 $status = wtsec_checkStatus($uid, $service);
483 $services["vc"] = [
484 "pause" => "",
485 "site_address" => $domain,
486 "status" => WTSEC_LIBRARY_WT::getStatusIcon($site['status']),
487 "signatures" => (int)$site['stats']['infected'],
488 "changes" => (int)$site['stats']['changed'],
489 "scanned" => (int)$site['stats']['scaned'],
490 "deleted" => (int)$site['stats']['deleted'],
491 "list" => $site["log"]['edges'],
492 "actions" => $buttons = wtsec_generateButtons($uid, $_service, $service, $status, WTSEC_SITE_URL),
493 "description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.antivirus')
494 ];
495 }
496 }
497 wtsec_layout("antivirus", $services);
498 }
499
500
501 function wtsec_firewall_page()
502 {
503 $host = WTSEC_LIBRARY_WT::getOwnSite();
504 $services = [];
505
506 if (!empty($host)) {
507 $site = WTSEC_LIBRARY_WT::getFirewall($host['id'],10,null,30);
508 $chart = WTSEC_LIBRARY_WT::getFirewallChart($host['id'], 30);
509 if (isset($site['data']['auth']['viewer']['sites']['one']['firewall']['logs'])) {
510 $logs = $site['data']['auth']['viewer']['sites']['one']['firewall']['logs'];
511 $firewall = $site['data']['auth']['viewer']['sites']['one']['firewall'];
512 $edges = $logs['edges'];
513
514 $chart = $chart['data']['auth']['viewer']['sites']['one']['firewall']['chart'];
515
516 $chart = generateChart($chart, 30);
517
518 $domain = $host['hostname'];
519
520 $_SESSION['firewall_endCursor'] = $logs['pageInfo']['endCursor'];
521 $_SESSION['firewall_hasNextPage'] = $logs['pageInfo']['hasNextPage'];
522
523 $countryAttacks = getCountryAttacks($firewall['map'], $chart['count_attacks']);
524 $attacksMap = getAttacksMap($firewall['map']);
525
526 $services["waf"] = [
527 "pause" => "",
528 "site_address" => $domain,
529 "status" => WTSEC_LIBRARY_WT::getStatusIcon($firewall['status']),
530 "_status" => $firewall['status'],
531 "installed_status" => in_array($firewall['status'], ["not_installed", "error", "down", "not_supported", "not_registered"]) ? false : true,
532 "time_of_the_last_check" => convertTimeToReadable($firewall['lastTest']['time']),
533 "attacks" => $chart['count_attacks'],
534 "blocking" => $chart['count_blocks'],
535 "non-blocking" => $chart['count_attacks'] - $chart['count_blocks'],
536 "description" => WTSEC_LIBRARY_Localization::lmsg('descriptions.firewall'),
537 "chart" => json_encode($chart['chart'], true),
538 "edges" => $edges,
539 "countryAttacks" => $countryAttacks,
540 "attacksMap" => $attacksMap,
541 ];
542 }
543 }
544 wtsec_layout("firewall", $services);
545 }
546
547 /**
548 * ajax load firewall
549 */
550 function wtsec_ajax_firewall()
551 {
552 $host = WTSEC_LIBRARY_WT::getOwnSite();
553 $services = [];
554
555 $post = ($_POST) ? wtsec_clean($_POST):[];
556 $days = ($post['period']) ? wtsec_period_as_time($post['period']) : 7;
557
558 if (!empty($host)) {
559
560 if(!$_SESSION['firewall_endCursor'] || $post['filter'] == 'period_filter') $_SESSION['firewall_endCursor'] = null;
561
562 $site = WTSEC_LIBRARY_WT::getFirewall($host['id'],10, $_SESSION['firewall_endCursor'], $days);
563 if (isset($site['data']['auth']['viewer']['sites']['one']['firewall']['logs'])) {
564 $logs = $site['data']['auth']['viewer']['sites']['one']['firewall']['logs'];
565
566 $services["waf"]['edges'] = $logs['edges'];
567
568 $_SESSION['firewall_endCursor'] = $logs['pageInfo']['endCursor'];
569 $_SESSION['firewall_hasNextPage'] = $logs['pageInfo']['hasNextPage'];
570 }
571
572 }
573
574 wtsec_layout_part("_firewall_lazy", $services);
575 wp_die();
576 }
577
578 /**
579 * ajax load antivirus
580 */
581 function wtsec_ajax_antivirus()
582 {
583 $host = WTSEC_LIBRARY_WT::getOwnSite();
584 $services = [];
585
586 $post = ($_POST) ? wtsec_clean($_POST):[];
587 $days = ($post['period']) ? wtsec_period_as_time($post['period']) : 7;
588
589 if (!empty($host)) {
590
591 if(!$_SESSION['antivirus_endCursor'] || $post['filter'] == 'period_filter') $_SESSION['antivirus_endCursor'] = null;
592 if($post['event'] || $_SESSION['antivirus_event']){
593 if($post['event']){
594 $_SESSION['antivirus_event'] = $post['event'];
595 }
596 $site = WTSEC_LIBRARY_WT::getAntivirus($host['id'],10, $_SESSION['antivirus_endCursor'],$days, $_SESSION['antivirus_event']);
597 } else {
598 $site = WTSEC_LIBRARY_WT::getAntivirus($host['id'],10, $_SESSION['antivirus_endCursor'],$days);
599 }
600
601 if (isset($site['data']['auth']['viewer']['sites']['one']['antivirus']['log'])) {
602 $logs = $site['data']['auth']['viewer']['sites']['one']['antivirus']['log'];
603
604 $services["vc"]['list'] = $logs['edges'];
605
606 $_SESSION['antivirus_endCursor'] = $logs['pageInfo']['endCursor'];
607 $_SESSION['antivirus_hasNextPage'] = $logs['pageInfo']['hasNextPage'];
608
609 }
610 }
611 wtsec_layout_part("_antivirus_lazy", $services);
612
613 wp_die();
614 }
615
616 /**
617 * ajax load chart
618 */
619 function wtsec_chart_filter()
620 {
621 $days = (int)$_POST['days'];
622 $days = $days ?: 7;
623
624 $host = WTSEC_LIBRARY_WT::getOwnSite();
625 $chart = WTSEC_LIBRARY_WT::getFirewallChart($host['id'], $days);
626 $chart = $chart['data']['auth']['viewer']['sites']['one']['firewall']['chart'];
627 $chart = generateChart($chart, $days);
628 $res['chart'] = json_encode($chart['chart'], true);
629 $res['days'] = $days;
630
631 wtsec_layout_part("_chart_ajax", $res);
632
633 wp_die();
634 }
635
636 /**
637 * ajax load attack map
638 */
639 function wtsec_map_filter()
640 {
641 $days = (int)$_POST['days'];
642 $days = $days ?: 7;
643
644 $host = WTSEC_LIBRARY_WT::getOwnSite();
645 $site = WTSEC_LIBRARY_WT::getFirewall($host['id'],10,null,$days);
646 $firewall = $site['data']['auth']['viewer']['sites']['one']['firewall'];
647
648 $attacksMap = getAttacksMap($firewall['map']);
649 $res["waf"]['attacksMap'] = $attacksMap;
650 $res['days'] = $days;
651
652 wtsec_layout_part("_map_view", $res);
653
654 wp_die();
655 }
656
657 function wtsec_color_scheme()
658 {
659 $checked = (bool)$_POST['checked'];
660
661 if($checked){
662 WTSEC_LIBRARY_App::_set('color_scheme', 'dark');
663 echo 'dark';
664 } else {
665 WTSEC_LIBRARY_App::_set('color_scheme', 'light');
666 echo 'light';
667 }
668
669 wp_die();
670 }
671
672 function wtsec_configs_toggle()
673 {
674 $post = ($_POST) ? wtsec_clean($_POST):[];
675 $service_id = $post['service'];
676 $config = WTSEC_LIBRARY_WT::toggleConfigs($service_id);
677 echo json_encode($config['data']['auth']['configs']['toggle']);
678
679 wp_die();
680 }
681
682
683 function wtsec_login_form()
684 {
685 if (wtsec_request()->method === "POST") {
686 $result = WTSEC_LIBRARY_WT::auth(wtsec_request()->key);
687 if (isset($result['data']['guest']['apiKeys']['auth']['token']['value'])) {
688 WTSEC_LIBRARY_Session::setNotification("success", WTSEC_LIBRARY_Localization::lmsg('successfully_activated'));
689 $token = $result['data']['guest']['apiKeys']['auth']['token']['value'];
690 WTSEC_LIBRARY_App::login($token);
691 wtsec_app()->set("api_key", wtsec_request()->key);
692 wp_safe_redirect(wtsec_getUrl('dashboard'));
693 exit;
694 } else {
695 WTSEC_LIBRARY_Session::setNotification("error", WTSEC_LIBRARY_Localization::lmsg("form.incorrect"));
696 }
697 }
698 wp_safe_redirect(wtsec_getUrl('login'));
699 }
700
701 function wtsec_cmd_ajax()
702 {
703 $service = strtoupper(wtsec_request()->service);
704 $_service = strtolower(wtsec_request()->service);
705 $uid = wtsec_request()->uid;
706 $cmd = wtsec_request()->cmd;
707 $status = wtsec_checkStatus($uid, $service);
708
709 if (stripos($cmd, "install") === false) {
710 wtsec_cmd($cmd, $service, $_service, $uid, $status, WTSEC_SITE_URL);
711 wp_safe_redirect(wp_get_referer());
712 } else {
713 $redirect = wtsec_getUrl("dashboard");
714 $form_url = wp_nonce_url(admin_url('admin-post.php?cmd=' . $cmd . '&action=ajax_cmd&service=' . $service . '&uid=' . $uid), "ajax_cmd");
715 if (wtsec_filesystem_init($form_url, '', false, false)) {
716 wtsec_cmd($cmd, $service, $_service, $uid, $status, WTSEC_SITE_URL);
717 wp_safe_redirect($redirect);
718 }
719 }
720 }
721
722 function wtsec_login()
723 {
724 require_once WTSEC_PLUGIN_PATH . "includes/login.php";
725 }
726
727 function wtsec_logout()
728 {
729 WTSEC_LIBRARY_App::logout();
730 }
731
732 function wtsec_layout_part($template, $arguments = [])
733 {
734 wp_register_script( 'ajaxHandle', get_template_directory() . 'PATH TO YOUR JS FILE', array(), false, true );
735 wp_enqueue_script( 'ajaxHandle' );
736 wp_localize_script( 'ajaxHandle', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
737
738 require WTSEC_PLUGIN_PATH . "includes/components/" . $template . ".php";
739
740 }
741
742 function wtsec_layout($template, $arguments = [], $faq = "faq")
743 {
744 $body = WTSEC_PLUGIN_PATH . "includes/" . $template . ".php";
745 $faq = WTSEC_PLUGIN_PATH . "includes/" . $faq . ".php";
746 require_once WTSEC_PLUGIN_PATH . "includes/layout.php";
747 }
748
749
750 function wtsec_ajax_changeStatus()
751 {
752 if (wtsec_request()->method === "POST") {
753 $host_id = wtsec_request()->host_id;
754 $config_id = wtsec_request()->config_id;
755 $result = WTSEC_LIBRARY_WT::changeStatus($config_id, $host_id);
756 echo json_encode($result);
757 }
758 wp_die();
759 }
760
761 function wtsec_changeStatus()
762 {
763 if (wtsec_request()->method === "POST") {
764 $host_id = wtsec_request()->host_id;
765 $config_id = wtsec_request()->config_id;
766 WTSEC_LIBRARY_WT::changeStatus($config_id, $host_id);
767 }
768 wp_safe_redirect(wp_get_referer());
769 }
770
771 function wtsec_page($page)
772 {
773 return wtsec_pages()[$page];
774 }
775
776 function wtsec_clean($data)
777 {
778 if (is_array($data)) {
779 foreach ($data as $key => $value) {
780 unset($data[$key]);
781 $data[wtsec_clean($key)] = wtsec_clean($value);
782 }
783 } else {
784 $data = htmlspecialchars(strip_tags($data), ENT_COMPAT, 'UTF-8');
785 }
786 return $data;
787 }
788
789 function wtsec_period_as_time($period){
790 $end = $period[1] ?: $period[0];
791 $array['begin'] = strtotime(date('d-m-Y 00:00:00',strtotime($period[0])));
792 $array['end'] = strtotime(date('d-m-Y 23:59:59',strtotime($end)));
793
794 return $array;
795 }
796
797 function wtsec_getUrl($page)
798 {
799 return admin_url('admin.php?page=' . WTSEC_PAGE_PREFIX . $page);
800 }
801
802 function wtsec_getRoute($page)
803 {
804 return WTSEC_PAGE_PREFIX . $page;
805 }
806
807 function wtsec_pages()
808 {
809 return [
810 "options" => [
811 "page_title" => WTSEC_LIBRARY_Localization::lmsg('options'),
812 "menu_title" => WTSEC_LIBRARY_Localization::lmsg('options'),
813 "function" => "wtsec_options_page",
814 ],
815 "services" => [
816 "page_title" => WTSEC_LIBRARY_Localization::lmsg('services'),
817 "menu_title" => WTSEC_LIBRARY_Localization::lmsg('services'),
818 "function" => "wtsec_services_page",
819 ],
820 "vc" => [
821 "page_title" => WTSEC_LIBRARY_Localization::lmsg('remote_antivirus'),
822 "menu_title" => WTSEC_LIBRARY_Localization::lmsg('remote_antivirus'),
823 "function" => "wtsec_antivirus_page",
824 "vc_action" => "vc-action",
825 "vc_function" => "wtsec_vc_function"
826 ],
827 "waf" => [
828 "page_title" => WTSEC_LIBRARY_Localization::lmsg('firewall'),
829 "menu_title" => WTSEC_LIBRARY_Localization::lmsg('firewall'),
830 "function" => "wtsec_firewall_page",
831 "vc_action" => "firewall-action",
832 "vc_function" => "wtsec_firewall_function"
833 ],
834 ];
835 }
836
837 function wtsec_getCountryName($key){
838 $countries = [
839 'AD' => 'Andorra',
840 'AE' => 'United Arab Emirates',
841 'AF' => 'Afghanistan',
842 'AG' => 'Antigua and Barbuda',
843 'AI' => 'Anguilla',
844 'AL' => 'Albania',
845 'AM' => 'Armenia',
846 'AN' => 'Netherlands Antilles',
847 'AO' => 'Angola',
848 'AP' => 'Asia/Pacific Region',
849 'AQ' => 'Antarctica',
850 'AR' => 'Argentina',
851 'AS' => 'American Samoa',
852 'AT' => 'Austria',
853 'AU' => 'Australia',
854 'AW' => 'Aruba',
855 'AZ' => 'Azerbaijan',
856 'BA' => 'Bosnia and Herz.',
857 'BB' => 'Barbados',
858 'BD' => 'Bangladesh',
859 'BE' => 'Belgium',
860 'BF' => 'Burkina Faso',
861 'BG' => 'Bulgaria',
862 'BH' => 'Brunei',
863 'BI' => 'Burundi',
864 'BJ' => 'Benin',
865 'BM' => 'Bermuda',
866 'BN' => 'Brunei Darussalam',
867 'BO' => 'Bolivia',
868 'BR' => 'Brazil',
869 'BS' => 'Bahamas',
870 'BT' => 'Bhutan',
871 'BV' => 'Bouvet Island',
872 'BW' => 'Botswana',
873 'BY' => 'Belarus',
874 'BZ' => 'Belize',
875 'CA' => 'Canada',
876 'CC' => 'Cocos (Keeling) Islands',
877 'CD' => 'Dem. Rep. Congo',
878 'CF' => 'Central African Rep.',
879 'CG' => 'Congo',
880 'CH' => 'Switzerland',
881 'CI' => 'Côte d\'Ivoire',
882 'CK' => 'Cook Islands',
883 'CL' => 'Chile',
884 'CM' => 'Cameroon',
885 'CN' => 'China',
886 'CO' => 'Colombia',
887 'CR' => 'Costa Rica',
888 'CS' => 'Serbia',
889 'CU' => 'Cuba',
890 'CV' => 'Cape Verde',
891 'CX' => 'Christmas Island',
892 'CY' => 'Cyprus',
893 'CZ' => 'Czech Rep.',
894 'DE' => 'Germany',
895 'DJ' => 'Djibouti',
896 'DK' => 'Denmark',
897 'DM' => 'Dominica',
898 'DO' => 'Dominican Rep.',
899 'DZ' => 'Algeria',
900 'EC' => 'Ecuador',
901 'EE' => 'Estonia',
902 'EG' => 'Egypt',
903 'EH' => 'W. Sahara',
904 'ER' => 'Eritrea',
905 'ES' => 'Spain',
906 'ET' => 'Ethiopia',
907 'EU' => 'Europe',
908 'FI' => 'Finland',
909 'FJ' => 'Fiji',
910 'FK' => 'Falkland Is.',
911 'FM' => 'Micronesia, Federated States of',
912 'FO' => 'Faroe Islands',
913 'FR' => 'France',
914 'FX' => 'France, Metropolitan',
915 'GA' => 'Gabon',
916 'GB' => 'United Kingdom',
917 'GD' => 'Grenada',
918 'GE' => 'Georgia',
919 'GF' => 'French Guiana',
920 'GH' => 'Ghana',
921 'GI' => 'Gibraltar',
922 'GL' => 'Greenland',
923 'GM' => 'Gambia',
924 'GN' => 'Guinea',
925 'GP' => 'Guadeloupe',
926 'GQ' => 'Eq. Guinea',
927 'GR' => 'Greece',
928 'GS' => 'South Georgia and the South Sandwich Islands',
929 'GT' => 'Guatemala',
930 'GU' => 'Guam',
931 'GW' => 'Guinea-Bissau',
932 'GY' => 'Guyana',
933 'HK' => 'Hong Kong',
934 'HM' => 'Heard Island and McDonald Islands',
935 'HN' => 'Honduras',
936 'HR' => 'Croatia',
937 'HT' => 'Haiti',
938 'HU' => 'Hungary',
939 'ID' => 'Indonesia',
940 'IE' => 'Ireland',
941 'IL' => 'Israel',
942 'IN' => 'India',
943 'IO' => 'British Indian Ocean Territory',
944 'IQ' => 'Iraq',
945 'IR' => 'Iran',
946 'IS' => 'Iceland',
947 'IT' => 'Italy',
948 'JM' => 'Jamaica',
949 'JO' => 'Jordan',
950 'JP' => 'Japan',
951 'KE' => 'Kenya',
952 'KG' => 'Kyrgyzstan',
953 'KH' => 'Cambodia',
954 'KI' => 'Kiribati',
955 'KM' => 'Comoros',
956 'KN' => 'Saint Kitts and Nevis',
957 'KP' => 'Dem. Rep. Korea',
958 'KR' => 'Korea',
959 'KW' => 'Kuwait',
960 'KY' => 'Cayman Islands',
961 'KZ' => 'Kazakhstan',
962 'LA' => 'Lao PDR',
963 'LB' => 'Lebanon',
964 'LC' => 'Saint Lucia',
965 'LI' => 'Liechtenstein',
966 'LK' => 'Sri Lanka',
967 'LR' => 'Liberia',
968 'LS' => 'Lesotho',
969 'LT' => 'Lithuania',
970 'LU' => 'Luxembourg',
971 'LV' => 'Latvia',
972 'LY' => 'Libya',
973 'MA' => 'Morocco',
974 'MC' => 'Monaco',
975 'MD' => 'Moldova',
976 'MG' => 'Madagascar',
977 'MH' => 'Marshall Islands',
978 'MK' => 'Macedonia',
979 'ML' => 'Mali',
980 'MM' => 'Myanmar',
981 'MN' => 'Mongolia',
982 'MO' => 'Macau',
983 'MP' => 'Northern Mariana Islands',
984 'MQ' => 'Martinique',
985 'MR' => 'Mauritania',
986 'MS' => 'Montserrat',
987 'MT' => 'Malta',
988 'MU' => 'Mauritius',
989 'MV' => 'Maldives',
990 'MW' => 'Malawi',
991 'MX' => 'Mexico',
992 'MY' => 'Malaysia',
993 'MZ' => 'Mozambique',
994 'NA' => 'Namibia',
995 'NC' => 'New Caledonia',
996 'NE' => 'Niger',
997 'NF' => 'Norfolk Island',
998 'NG' => 'Nigeria',
999 'NI' => 'Nicaragua',
1000 'NL' => 'Netherlands',
1001 'NO' => 'Norway',
1002 'NP' => 'Nepal',
1003 'NR' => 'Nauru',
1004 'NU' => 'Niue',
1005 'NZ' => 'New Zealand',
1006 'OM' => 'Oman',
1007 'PA' => 'Panama',
1008 'PE' => 'Peru',
1009 'PF' => 'French Polynesia',
1010 'PG' => 'Papua New Guinea',
1011 'PH' => 'Philippines',
1012 'PK' => 'Pakistan',
1013 'PL' => 'Poland',
1014 'PM' => 'Saint Pierre and Miquelon',
1015 'PN' => 'Pitcairn Islands',
1016 'PR' => 'Puerto Rico',
1017 'PS' => 'Palestine',
1018 'PT' => 'Portugal',
1019 'PW' => 'Palau',
1020 'PY' => 'Paraguay',
1021 'QA' => 'Qatar',
1022 'RE' => 'Reunion',
1023 'RO' => 'Romania',
1024 'RU' => 'Russia',
1025 'RW' => 'Rwanda',
1026 'SA' => 'Saudi Arabia',
1027 'SB' => 'Solomon Is.',
1028 'SC' => 'Seychelles',
1029 'SD' => 'Sudan',
1030 'SE' => 'Sweden',
1031 'SG' => 'Singapore',
1032 'SH' => 'Saint Helena',
1033 'SI' => 'Slovenia',
1034 'SJ' => 'Svalbard and Jan Mayen',
1035 'SK' => 'Slovakia',
1036 'SL' => 'Sierra Leone',
1037 'SM' => 'San Marino',
1038 'SN' => 'Senegal',
1039 'SO' => 'Somalia',
1040 'SR' => 'Suriname',
1041 'ST' => 'Sao Tome and Principe',
1042 'SV' => 'El Salvador',
1043 'SY' => 'Syria',
1044 'SZ' => 'Swaziland',
1045 'TC' => 'Turks and Caicos Islands',
1046 'TD' => 'Chad',
1047 'TF' => 'French Southern Territories',
1048 'TG' => 'Togo',
1049 'TH' => 'Thailand',
1050 'TJ' => 'Tajikistan',
1051 'TK' => 'Tokelau',
1052 'TM' => 'Turkmenistan',
1053 'TN' => 'Tunisia',
1054 'TO' => 'Tonga',
1055 'TP' => 'East Timor',
1056 'TR' => 'Turkey',
1057 'TT' => 'Trinidad and Tobago',
1058 'TV' => 'Tuvalu',
1059 'TW' => 'Taiwan',
1060 'TZ' => 'Tanzania',
1061 'UA' => 'Ukraine',
1062 'UG' => 'Uganda',
1063 'UM' => 'United States Minor Outlying Islands',
1064 'US' => 'United States',
1065 'UY' => 'Uruguay',
1066 'UZ' => 'Uzbekistan',
1067 'VA' => 'Holy See (Vatican City State)',
1068 'VC' => 'Saint Vincent and the Grenadines',
1069 'VE' => 'Venezuela',
1070 'VG' => 'Virgin Islands, British',
1071 'VI' => 'Virgin Islands, U.S.',
1072 'VN' => 'Vietnam',
1073 'VU' => 'Vanuatu',
1074 'WF' => 'Wallis and Futuna',
1075 'WS' => 'Samoa',
1076 'YE' => 'Yemen',
1077 'YT' => 'Mayotte',
1078 'ZA' => 'South Africa',
1079 'ZM' => 'Zambia',
1080 'ZR' => 'Zaire',
1081 'ZW' => 'Zimbabwe',
1082 ];
1083
1084 return $countries[$key];
1085 }
1086