PluginProbe
WebTotem Security / 2.2.1
WebTotem Security v2.2.1
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.1, at routes.php

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