PluginProbe
WebTotem Security / 2.4.31
WebTotem Security v2.4.31
3.0.2 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 All 110 releases
wt-security / lib / Ajax.php

Ajax.php in WebTotem Security 2.4.31, at lib/Ajax.php

2,433 lines 68.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) {
3 if (!headers_sent()) {
4 header('HTTP/1.1 403 Forbidden');
5 }
6 die('Protected By WebTotem!');
7 }
8
9 class WebTotemAjax
10 {
11
12 /**
13 * Activation plugin.
14 *
15 * @return void
16 */
17 public static function activation()
18 {
19
20 if (WebTotemRequest::post('ajax_action') !== 'activation') {
21 return;
22 }
23
24 if ($api_key = WebTotemRequest::post('api_key')) {
25
26 $result = WebTotemAPI::auth($api_key);
27
28 if ($result == 'success') {
29 if (WebTotem::isMultiSite()) {
30 $link = WebTotem::adminURL('admin.php?page=wtotem_all_sites');
31 } else {
32 $link = WebTotem::adminURL('admin.php?page=wtotem');
33 }
34 $email = WebTotemAPI::getEmail();
35 WebTotemOption::setOptions(['user_email' => $email]);
36 wp_send_json([
37 'link' => $link,
38 'success' => true,
39 'user' => $email,
40 ], 200);
41 } else {
42
43 wp_send_json([
44 'notifications' => self::notifications(),
45 'success' => false,
46 ], 200);
47 }
48 }
49
50 }
51
52 /**
53 * The process of installing agents (WAF, AV) on the main page.
54 *
55 * @return void
56 */
57 public static function agentsInstallation()
58 {
59
60 if (WebTotemRequest::post('ajax_action') !== 'agents_installation') {
61 return;
62 }
63
64 $av_installed = WebTotemOption::getOption('av_installed');
65 $waf_installed = WebTotemOption::getOption('waf_installed');
66
67 // Check if the agents are installed.
68 if ($av_installed and $waf_installed) {
69 $agents_statuses = [
70 'process_statuses' => [
71 'av' => 'installed',
72 'waf' => 'installed',
73 ],
74 ];
75 } else {
76 // If not installed, then request statuses from the WebTotem API.
77 $host = WebTotemAPI::siteInfo();
78 $data = WebTotemAPI::getAgentsStatusesFromAPI($host['id']);
79
80 $agents_statuses = [
81 'av' => $data['av']['status'],
82 'waf' => $data['waf']['status'],
83 ];
84
85 $agents_statuses = WebTotem::getAgentsStatuses($agents_statuses);
86 }
87
88 $build[] = [
89 'variables' => [
90 'process_status' => $agents_statuses['process_statuses'],
91 ],
92 'template' => 'agents_installation',
93 ];
94
95 $status = [
96 'av' => $agents_statuses['process_statuses']['av'] == 'installed',
97 'waf' => $agents_statuses['process_statuses']['waf'] == 'installed',
98 ];
99
100 WebTotemOption::setOptions([
101 'av_installed' => $status['av'],
102 'waf_installed' => $status['waf'],
103 ]);
104
105 $template = new WebTotemTemplate();
106 $agents = $template->arrayRender($build);
107
108 wp_send_json([
109 'success' => true,
110 'notifications' => self::notifications(),
111 'agents' => $agents,
112 'agents_statuses' => $status['av'] && $status['waf'],
113 ]);
114 }
115
116 /**
117 * Reinstall agents.
118 *
119 * @return void
120 */
121 public static function reinstallAgents()
122 {
123
124 if (WebTotemRequest::post('ajax_action') !== 'reinstall_agents') {
125 return;
126 }
127
128 WebTotemAgentManager::amInstall();
129
130 $response['success'] = true;
131 $response['redirect_link'] = WebTotem::adminURL('admin.php?page=wtotem');
132 wp_send_json($response);
133
134 }
135
136 /**
137 * Deleting plugin activation data and redirecting to the activation page.
138 *
139 * @return void
140 */
141 public static function logout()
142 {
143
144 if (WebTotemRequest::post('ajax_action') !== 'logout') {
145 return;
146 }
147
148 WebTotemOption::logout();
149
150 $response['success'] = true;
151 $response['redirect_link'] = WebTotem::adminURL('admin.php?page=wtotem_activation');
152 wp_send_json($response);
153
154 }
155
156 /**
157 * Creating a modal window.
158 *
159 * @return void
160 */
161 public static function popup()
162 {
163
164 if (WebTotemRequest::post('ajax_action') !== 'popup') {
165 return;
166 }
167
168 $action = WebTotemRequest::post('popup_action');
169 $template = new WebTotemTemplate();
170
171 if ($action) {
172 switch ($action) {
173 case 'reinstall_agents':
174 $build[] = [
175 'variables' => [
176 'message' => sprintf(__('Some scanning data for %s may be deleted.', 'wtotem'), WEBTOTEM_SITE_DOMAIN),
177 'action' => 'reinstall_agents',
178 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
179 ],
180 'template' => 'popup',
181 ];
182 break;
183
184 case 'logout':
185 $build[] = [
186 'variables' => [
187 'message' => __('Are you sure you want to change the API key?', 'wtotem'),
188 'action' => 'logout',
189 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
190 ],
191 'template' => 'popup',
192 ];
193 break;
194
195
196 case 'update_plugin':
197 $build[] = [
198 'variables' => [
199 'message' => __('Do you really want to update the plugin?', 'wtotem'),
200 'action' => 'update_plugin',
201 'slug' => WebTotemRequest::post('slug'),
202 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
203 ],
204 'template' => 'popup',
205 ];
206 break;
207
208 }
209
210 wp_send_json([
211 'success' => true,
212 'content' => $template->arrayRender($build),
213 ]);
214 }
215
216 wp_send_json([
217 'success' => false,
218 ]);
219
220 }
221
222 /**
223 * Request to update charts with parameters.
224 *
225 * @return void
226 */
227 public static function chart()
228 {
229
230 if (WebTotemRequest::post('ajax_action') !== 'chart') {
231 return;
232 }
233
234 $template = new WebTotemTemplate();
235
236 $days = (integer)WebTotemRequest::post('days');
237 $service = WebTotemRequest::post('service');
238
239 $host = WebTotemAPI::siteInfo();
240
241 switch ($service) {
242 case 'waf':
243
244 WebTotemOption::setSessionOptions(['firewall_period' => $days]);
245
246 // Firewall chart.
247 $data = WebTotemAPI::getFirewallChart($host['id'], $days);
248 $chart = WebTotem::generateWafChart($data['chart']);
249
250 $_chart[] = [
251 'variables' => [
252 'days' => $days,
253 'chart' => $chart['chart'],
254 ],
255 'template' => 'firewall_chart',
256 ];
257
258 // Firewall logs.
259 $data = WebTotemAPI::getFirewall($host['id'], 10, NULL, $days);
260 $firewall = $data['firewall'];
261
262 $waf_logs[] = [
263 'variables' => [
264 'logs' => WebTotem::wafLogs($firewall['logs']['edges']),
265 ],
266 'template' => 'firewall_logs',
267 ];
268
269 // Firewall stats.
270 $waf_stats[] = [
271 'variables' => [
272 'is_waf_training' => WebTotem::isWafTraining($data['agentManager']['createdAt']),
273 'all_attacks' => $chart['count_attacks'],
274 'blocking' => $chart['count_blocks'],
275 'not_blocking' => $chart['count_attacks'] - $chart['count_blocks'],
276 'most_attacks' => WebTotem::getMostAttacksData($firewall['map']),
277 ],
278 'template' => 'firewall_stats',
279 ];
280
281 WebTotemOption::setSessionOptions([
282 'firewall_cursor' => $firewall['logs']['pageInfo']['endCursor'],
283 ]);
284
285 $has_next_page = $firewall['logs']['pageInfo']['hasNextPage'];
286
287 $response = [
288 'chart' => $template->arrayRender($_chart),
289 'waf_logs' => $template->arrayRender($waf_logs),
290 'waf_stats' => $template->arrayRender($waf_stats),
291 'has_next_page' => $has_next_page,
292 'service' => 'waf',
293 ];
294
295 break;
296
297 case 'cpu':
298 WebTotemOption::setSessionOptions(['cpu_period' => $days]);
299
300 $data = WebTotemAPI::getServerStatusData($host['id'], $days);
301 $chart = WebTotem::generateChart($data['cpuChart'], $days);
302
303 $_chart[] = [
304 'variables' => [
305 'days' => $days,
306 'chart' => $chart,
307 ],
308 'template' => 'cpu_chart',
309 ];
310
311 $response = [
312 'chart' => $template->arrayRender($_chart),
313 'service' => 'cpu',
314 ];
315
316 break;
317
318 case 'ram':
319 WebTotemOption::setSessionOptions(['ram_period' => $days]);
320
321 $data = WebTotemAPI::getServerStatusData($host['id'], $days);
322 $chart = WebTotem::generateChart($data['ramChart'], $days);
323
324 $_chart[] = [
325 'variables' => [
326 'days' => $days,
327 'chart' => $chart,
328 ],
329 'template' => 'ram_chart',
330 ];
331
332 $response = [
333 'chart' => $template->arrayRender($_chart),
334 'service' => 'ram',
335 ];
336
337 break;
338
339 case 'map':
340 $data = WebTotemAPI::getFirewallChart($host['id'], $days);
341 $chart = WebTotem::generateAttacksMapChart($data['map']);
342 $world_map_json = WEBTOTEM_URL . '/includes/js/world_map.json';
343
344 $_chart[] = [
345 'variables' => [
346 'attacks_map' => $chart,
347 'world_map_json' => $world_map_json,
348 ],
349 'template' => 'map_chart',
350 ];
351
352 $response = [
353 'chart' => $template->arrayRender($_chart),
354 'service' => 'map',
355 ];
356
357 break;
358
359 }
360
361 if ($service) {
362 $response['success'] = true;
363 $response['notifications'] = self::notifications();
364 wp_send_json($response);
365 }
366
367 }
368
369 /**
370 * Data lazy load.
371 * @return void
372 */
373 public static function lazyLoad()
374 {
375
376 if (WebTotemRequest::post('ajax_action') !== 'lazy_load') {
377 return;
378 }
379
380 $template = new WebTotemTemplate();
381
382 $service = WebTotemRequest::post('service');
383
384 $host = WebTotemAPI::siteInfo();
385
386 switch ($service) {
387 case 'all_sites':
388 $cursor = WebTotemOption::getSessionOption('sites_cursor') ?: NULL;
389 $allSites = WebTotemAPI::getSites($cursor);
390
391 $has_next_page = $allSites['pageInfo']['hasNextPage'];
392
393 WebTotemOption::setSessionOptions([
394 'sites_cursor' => $allSites['pageInfo']['endCursor'],
395 ]);
396
397 // Sites list.
398 $build[] = [
399 'variables' => [
400 'sites' => WebTotem::allSitesData($allSites),
401 'has_next_page' => $has_next_page,
402 ],
403 'template' => 'multisite_list'
404 ];
405
406 break;
407
408 case 'firewall':
409 $cursor = WebTotemOption::getSessionOption('firewall_cursor') ?: NULL;
410 $period = WebTotemOption::getSessionOption('firewall_period') ?: 365;
411 $data = WebTotemAPI::getFirewall($host['id'], 10, $cursor, $period);
412 $service_data = $data['firewall'];
413 $has_next_page = $service_data['logs']['pageInfo']['hasNextPage'];
414
415 WebTotemOption::setSessionOptions([
416 'firewall_cursor' => $service_data['logs']['pageInfo']['endCursor'],
417 ]);
418
419 // Firewall logs.
420 $build[] = [
421 'variables' => [
422 'logs' => WebTotem::wafLogs($service_data['logs']['edges']),
423 ],
424 'template' => 'firewall_logs',
425 ];
426
427 break;
428
429 case 'antivirus':
430 $cursor = WebTotemOption::getSessionOption('antivirus_cursor') ?: NULL;
431 $event = WebTotemOption::getSessionOption('antivirus_event') ?: NULL;
432 $permissions = WebTotemOption::getSessionOption('antivirus_permissions') ?: NULL;
433
434 $params = [
435 'host_id' => $host['id'],
436 'limit' => 10,
437 'days' => 365,
438 'cursor' => $cursor,
439 'event' => $event,
440 'permissions' => $permissions,
441 ];
442
443 $data = WebTotemAPI::getAntivirus($params);
444 $has_next_page = $data['log']['pageInfo']['hasNextPage'];
445
446 WebTotemOption::setSessionOptions([
447 'antivirus_cursor' => $data['log']['pageInfo']['endCursor'],
448 ]);
449
450 // Antivirus logs.
451 $build[] = [
452 'variables' => [
453 'logs' => WebTotem::getAntivirusLogs($data['log']['edges']),
454 ],
455 'template' => 'antivirus_logs',
456 ];
457
458 break;
459
460 case 'reports':
461 $cursor = WebTotemOption::getSessionOption('reports_cursor') ?: NULL;
462
463 $data = WebTotemAPI::getAllReports($host['id'], 10, $cursor);
464 $has_next_page = $data['pageInfo']['hasNextPage'];
465
466 WebTotemOption::setSessionOptions([
467 'reports_cursor' => $data['pageInfo']['endCursor'],
468 ]);
469
470 // Reports.
471 $build[] = [
472 'variables' => [
473 "reports" => WebTotem::getReports($data['edges']),
474 "has_next_page" => $data['pageInfo']['hasNextPage'],
475 ],
476 'template' => 'reports_list',
477 ];
478
479 break;
480
481 case 'reports_m':
482 $cursor = WebTotemOption::getSessionOption('reports_m_cursor') ?: NULL;
483
484 $data = WebTotemAPI::getAllReports($host['id'], 10, $cursor);
485 $has_next_page = $data['pageInfo']['hasNextPage'];
486
487 WebTotemOption::setSessionOptions([
488 'reports_m_cursor' => $data['pageInfo']['endCursor'],
489 ]);
490
491 // Reports mobile.
492 $build[] = [
493 'variables' => [
494 "reports" => WebTotem::getReports($data['edges']),
495 "has_next_page" => $data['pageInfo']['hasNextPage'],
496 ],
497 'template' => 'reports_list_mobile',
498 ];
499
500 break;
501 }
502
503 if ($service) {
504
505 wp_send_json([
506 'success' => true,
507 'content' => $template->arrayRender($build),
508 'has_next_page' => $has_next_page,
509 'notifications' => self::notifications(),
510 ]);
511 }
512 }
513
514 /**
515 * Data lazy load.
516 * @return void
517 */
518 public static function logs()
519 {
520 if (WebTotemRequest::post('ajax_action') !== 'logs') {
521 return;
522 }
523
524 $template = new WebTotemTemplate();
525 $logs_action = WebTotemRequest::post('logs_action');
526
527 switch ($logs_action) {
528 case 'audit_logs_pagination':
529 $order = WebTotemRequest::post('order') === 'ascending' ? 'ASC' : 'DESC';
530 $current_page = (int)WebTotemRequest::post('current_page');
531 $event = WebTotemRequest::post('event');
532 $filter = $event === 'All' ? [] : ['LIKE', ['event' => $event . '%']];
533
534 $audit_logs = WebTotemDB::getRows(
535 $filter,
536 'audit_logs',
537 false,
538 ['limit' => 10, 'page' => $current_page],
539 ['order_by' => 'created_at', 'direction' => $order]
540 );
541
542 $build[] = [
543 'variables' => [
544 "audit_logs" => WebTotem::getAuditLogs($audit_logs['data'], $audit_logs['dates_count']),
545 ],
546 'template' => 'scan_audit_logs',
547 ];
548
549 $response = [
550 'success' => true,
551 'content' => $template->arrayRender($build),
552 "pagination" => WebTotem::paginationBuild(10, $audit_logs['count'], $current_page),
553 'notifications' => self::notifications(),
554 ];
555
556 break;
557 case 'cve_logs_pagination':
558 $current_page = (int)WebTotemRequest::post('current_page');
559
560 $plugins_cve_list = WebTotemDB::getRows([], 'plugins_cve_list', false, ['limit' => 8, 'page' => $current_page]);
561
562 $build[] = [
563 'variables' => [
564 "logs" => WebTotem::preparePluginsCveList($plugins_cve_list['data']),
565 ],
566 'template' => 'scan_logs_cve',
567 ];
568
569 $response = [
570 'success' => true,
571 'content' => $template->arrayRender($build),
572 "pagination" => WebTotem::paginationBuild(8, $plugins_cve_list['count'], $current_page),
573 'notifications' => self::notifications(),
574 ];
575
576 break;
577
578 case 'audit_logs_sort_filter':
579 $order = WebTotemRequest::post('order') === 'ascending' ? 'ASC' : 'DESC';
580 $event = WebTotemRequest::post('event');
581 $filter = $event === 'All' ? [] : ['LIKE', ['event' => $event . '%']];
582 $audit_logs = WebTotemDB::getRows(
583 $filter,
584 'audit_logs',
585 false,
586 ['limit' => 10, 'page' => 1],
587 ['order_by' => 'created_at', 'direction' => $order]
588 );
589
590 $build[] = [
591 'variables' => [
592 "audit_logs" => WebTotem::getAuditLogs($audit_logs['data'], $audit_logs['dates_count']),
593 ],
594 'template' => 'scan_audit_logs',
595 ];
596
597 $response = [
598 'success' => true,
599 'content' => $template->arrayRender($build),
600 "pagination" => WebTotem::paginationBuild(10, $audit_logs['count'], 1),
601 'notifications' => self::notifications(),
602 ];
603
604 break;
605
606 case 'confidential_files':
607 $id = WebTotemRequest::post('id') ?? false;
608 if ($id) {
609 $file = WebTotemDB::getData(['id' => $id], 'confidential_files');
610 if ($file['path']) {
611 $path = urldecode($file['path']);
612 if(file_exists($path)) unlink($path);
613 WebTotemDB::deleteData(['id' => $id], 'confidential_files');
614 WebTotemOption::setNotification('info', sprintf(__('File %s was deleted', 'wtotem'), json_decode($file['name'])));
615 }
616 }
617 $order_by = WebTotemRequest::post('order');
618 $direction = WebTotemRequest::post('direction') === 'ascending' ? 'ASC' : 'DESC';
619 $current_page = (int)WebTotemRequest::post('current_page') ?: 1;
620
621 $confidential_files = WebTotemDB::getRows(
622 [],
623 'confidential_files',
624 false,
625 ['limit' => 10, 'page' => $current_page],
626 $order_by ? ['order_by' => $order_by, 'direction' => $direction] : ['order_by' => 'id', 'direction' => 'DESC']
627 );
628
629 $build[] = [
630 'variables' => [
631 "confidential_files" => WebTotem::getConfidentialFiles($confidential_files['data']),
632 ],
633 'template' => 'scan_confidential_files',
634 ];
635
636 $response = [
637 'success' => true,
638 'content' => $template->arrayRender($build),
639 'count' => $confidential_files['count'],
640 "pagination" => WebTotem::paginationBuild(10, $confidential_files['count'], $current_page),
641 'notifications' => self::notifications(),
642 ];
643
644 break;
645
646 case 'logs_pagination':
647 $current_page = (int)WebTotemRequest::post('current_page');
648 $type = WebTotemRequest::post('type');
649 $direction = WebTotemRequest::post('direction') === 'ascending' ? 'ASC' : 'DESC';
650
651 $scan_logs = WebTotemDB::getRows(
652 ['AND', ['data_type' => $type]],
653 'scan_logs',
654 'content',
655 ['limit' => 10, 'page' => $current_page],
656 !empty($direction) ? ['order_by' => 'is_internal', 'direction' => $direction] : ['order_by' => 'id', 'direction' => 'DESC']
657 );
658
659 $build[] = [
660 'variables' => [
661 "logs" => WebTotem::prepareLinksData($scan_logs['data']),
662 "data_type" => $type
663 ],
664 'template' => 'scan_logs_items',
665 ];
666
667 $response = [
668 'success' => true,
669 'content' => $template->arrayRender($build),
670 "pagination" => WebTotem::paginationBuild(10, $scan_logs['count'], $current_page),
671 'notifications' => self::notifications(),
672 ];
673
674 break;
675
676 case 'rescan':
677 WebTotem::updateCveData();
678 WebTotemOption::setOptions(['scan_init' => 1]);
679 WebTotemScan::initialize();
680 $response = [
681 'success' => true,
682 'notifications' => self::notifications(),
683 ];
684
685 break;
686
687 case 'check_scan':
688
689 if(WebTotemOption::getOption('scan_init')){
690 WebTotemScan::initialize();
691 $response = [
692 'success' => true,
693 'scan_finished' => false,
694 'notifications' => self::notifications(),
695 ];
696 } else {
697 $content = [];
698 $pagination = [];
699 $count = [];
700 $types = ['links', 'scripts', 'iframes'];
701
702 foreach ($types as $type) {
703 $scan_logs = WebTotemDB::getRows(
704 ['AND', ['data_type' => $type]],
705 'scan_logs',
706 'content'
707 );
708
709 $build[$type][] = [
710 'variables' => [
711 "logs" => $scan_logs['data'],
712 "data_type" => $type
713 ],
714 'template' => 'scan_logs_items',
715 ];
716 $content[$type] = $template->arrayRender($build[$type]);
717 $pagination[$type] = WebTotem::paginationBuild(10, $scan_logs['count']);
718 $count[$type] = $scan_logs['count'];
719 }
720
721 $plugins_cve_list = WebTotemDB::getRows([], 'plugins_cve_list', false, ['limit' => 8, 'page' => 1]);
722 $content['cve'] = $template->arrayRender([
723 'variables' => [
724 "logs" => WebTotem::preparePluginsCveList($plugins_cve_list['data']),
725 ],
726 'template' => 'scan_logs_cve',
727 ]);
728 $count['cve'] = $plugins_cve_list['count'];
729 $pagination['cve'] = WebTotem::paginationBuild(8, $plugins_cve_list['count']);
730
731 $confidential_files = WebTotemDB::getRows([], 'confidential_files');
732 $content['confidential_files'] = $template->arrayRender([
733 'variables' => [
734 "confidential_files" => WebTotem::getConfidentialFiles($confidential_files['data']),
735 ],
736 'template' => 'scan_confidential_files',
737 ]);
738 $pagination['confidential_files'] = WebTotem::paginationBuild(10, $confidential_files['count']);
739 $count['confidential_files'] = $confidential_files['count'];
740
741 // Resetting the task in the cron.
742 wp_clear_scheduled_hook('webtotem_daily_cron');
743 wp_schedule_event(time() + 86395, 'daily', 'webtotem_daily_cron');
744
745 $until_next_scan = wp_next_scheduled('webtotem_daily_cron') - time();
746
747 $hr = floor($until_next_scan / 3600);
748 $min = floor(($until_next_scan % 3600) / 60);
749
750 $response = [
751 'success' => true,
752 'scan_finished' => true,
753 'content' => $content,
754 "pagination" => $pagination,
755 "next_scan" => sprintf(__('%dh %dm', 'wtotem'), $hr, $min),
756 "count" => $count,
757 'notifications' => self::notifications(),
758 ];
759 }
760
761 break;
762 }
763
764 wp_send_json($response ?? []);
765 }
766
767 /**
768 * Add date filter.
769 *
770 * @return void
771 */
772 public static function wafDateFilter()
773 {
774
775 if (WebTotemRequest::post('ajax_action') !== 'waf_date_filter') {
776 return;
777 }
778
779 $template = new WebTotemTemplate();
780
781 $period = [WebTotemRequest::post('date_from'), WebTotemRequest::post('date_to')];
782 WebTotemOption::setSessionOptions(['firewall_period' => $period]);
783
784 $host = WebTotemAPI::siteInfo();
785
786 // Firewall logs.
787 $data = WebTotemAPI::getFirewall($host['id'], 10, NULL, $period);
788 $firewall = $data['firewall'];
789
790 $waf_logs[] = [
791 'variables' => [
792 'logs' => WebTotem::wafLogs($firewall['logs']['edges']),
793 ],
794 'template' => 'firewall_logs',
795 ];
796
797 // Firewall chart.
798 $data = WebTotemAPI::getFirewallChart($host['id'], $period);
799 $chart = WebTotem::generateWafChart($data['chart']);
800
801 $_chart[] = [
802 'variables' => [
803 'days' => $chart['days'],
804 'chart' => $chart['chart'],
805 ],
806 'template' => 'firewall_chart',
807 ];
808
809 // Firewall stats.
810 $waf_stats[] = [
811 'variables' => [
812 'is_waf_training' => WebTotem::isWafTraining($data['agentManager']['createdAt']),
813 'all_attacks' => $chart['count_attacks'],
814 'blocking' => $chart['count_blocks'],
815 'not_blocking' => $chart['count_attacks'] - $chart['count_blocks'],
816 'most_attacks' => WebTotem::getMostAttacksData($firewall['map']),
817 ],
818 'template' => 'firewall_stats',
819 ];
820
821 WebTotemOption::setSessionOptions([
822 'firewall_cursor' => $firewall['logs']['pageInfo']['endCursor'],
823 ]);
824
825 $has_next_page = $firewall['logs']['pageInfo']['hasNextPage'];
826
827 $response = [
828 'success' => true,
829 'chart' => $template->arrayRender($_chart),
830 'waf_logs' => $template->arrayRender($waf_logs),
831 'waf_stats' => $template->arrayRender($waf_stats),
832 'has_next_page' => $has_next_page,
833 'notifications' => self::notifications(),
834 ];
835
836 wp_send_json($response);
837 }
838
839
840 /**
841 * Request to restart re-scan and receive antivirus data.
842 *
843 * @return void
844 */
845 public static function antivirus()
846 {
847
848 if (WebTotemRequest::post('ajax_action') !== 'antivirus') {
849 return;
850 }
851
852 $action = WebTotemRequest::post('av_action');
853
854 $host = WebTotemAPI::siteInfo();
855
856 switch ($action) {
857 case 'rescan':
858 $response = WebTotemAPI::forceCheck($host['id'], 'av');
859
860 if (!isset($response['errors'])) {
861 $data = WebTotemAPI::getAntivirusLastTest($host['id']);
862 $response['last_scan'] = WebTotem::dateFormatter($data['lastTest']['time']);
863 }
864 break;
865
866 case 'download_report':
867 $response = WebTotemAPI::avExport($host['id']);
868 if (!isset($response['errors'])) {
869 $response['doc_link'] = $response['data']['auth']['sites']['av']['export'];
870 }
871 break;
872
873 case 'filter':
874
875 $file_status = WebTotemRequest::post('file_status');
876 $permission = filter_var(WebTotemRequest::post('permission'), FILTER_VALIDATE_BOOLEAN);
877
878 WebTotemOption::setSessionOptions([
879 'antivirus_permissions' => $permission,
880 'antivirus_event' => $file_status,
881 ]);
882
883 $params = [
884 'host_id' => $host['id'],
885 'limit' => 10,
886 'days' => 365,
887 'cursor' => NULL,
888 'event' => $file_status,
889 'permissions' => $permission,
890 ];
891
892 $data = WebTotemAPI::getAntivirus($params);
893 $has_next_page = $data['log']['pageInfo']['hasNextPage'];
894
895 WebTotemOption::setSessionOptions([
896 'antivirus_cursor' => $data['log']['pageInfo']['endCursor'],
897 ]);
898
899 // Antivirus logs.
900 $build[] = [
901 'variables' => [
902 'logs' => WebTotem::getAntivirusLogs($data['log']['edges']),
903 ],
904 'template' => 'antivirus_logs',
905 ];
906
907 $template = new WebTotemTemplate();
908 $response = [
909 'logs' => $template->arrayRender($build),
910 'has_next_page' => $has_next_page,
911 ];
912
913 break;
914 }
915
916 $response['success'] = true;
917 $response['notifications'] = self::notifications();
918
919 wp_send_json($response);
920 }
921
922 /**
923 * Request to add a file to quarantine.
924 *
925 * @return void
926 */
927 public static function quarantine()
928 {
929 if (WebTotemRequest::post('ajax_action') !== 'quarantine') {
930 return;
931 }
932
933 $action = WebTotemRequest::post('quarantine_action');
934 $id_or_path = WebTotemRequest::post('id_or_path');
935
936 $host = WebTotemAPI::siteInfo();
937 $response = [];
938
939 switch ($action) {
940 case 'add':
941 $api_response = WebTotemAPI::moveToQuarantine($host['id'], $id_or_path);
942 break;
943
944 case 'remove':
945 $api_response = WebTotemAPI::moveFromQuarantine($id_or_path);
946 break;
947 }
948
949 if (!isset($api_response['errors'])) {
950
951 $quarantine_logs = WebTotemAPI::getQuarantineList($host['id']);
952 $quarantine_count = count($quarantine_logs);
953
954 // Quarantine logs.
955 $quarantine[] = [
956 'variables' => [
957 "logs" => WebTotem::getQuarantineLogs($quarantine_logs),
958 "count" => $quarantine_count,
959 ],
960 'template' => 'quarantine',
961 ];
962
963 $cursor = WebTotemOption::getSessionOption('antivirus_cursor') ?: NULL;
964 $event = WebTotemOption::getSessionOption('antivirus_event') ?: NULL;
965 $permissions = WebTotemOption::getSessionOption('antivirus_permissions') ?: NULL;
966
967 $params = [
968 'host_id' => $host['id'],
969 'limit' => 10,
970 'days' => 365,
971 'cursor' => $cursor,
972 'event' => $event,
973 'permissions' => $permissions,
974 ];
975
976 $data = WebTotemAPI::getAntivirus($params);
977 WebTotemCache::setData(['getAntivirus' => $data], $host['id']);
978
979 $has_next_page = $data['log']['pageInfo']['hasNextPage'];
980
981 WebTotemOption::setSessionOptions([
982 'antivirus_cursor' => $data['log']['pageInfo']['endCursor'],
983 ]);
984
985 // Antivirus logs.
986 $antivirus_logs[] = [
987 'variables' => [
988 'logs' => WebTotem::getAntivirusLogs($data['log']['edges']),
989 ],
990 'template' => 'antivirus_logs',
991 ];
992
993
994 $template = new WebTotemTemplate();
995 $response = [
996 'antivirus_logs' => $template->arrayRender($antivirus_logs),
997 'quarantine' => $template->arrayRender($quarantine),
998 'has_next_page' => $has_next_page,
999 ];
1000
1001 }
1002
1003 $response['success'] = true;
1004 $response['notifications'] = self::notifications();
1005
1006 wp_send_json($response);
1007
1008 }
1009
1010 /**
1011 * Request to add or remove a port to the ignore list.
1012 *
1013 * @return void
1014 */
1015 public static function ignorePorts()
1016 {
1017
1018 if (WebTotemRequest::post('ajax_action') !== 'ignore_ports') {
1019 return;
1020 }
1021
1022 $template = new WebTotemTemplate();
1023
1024 $action = WebTotemRequest::post('port_action');
1025 $port = (int)WebTotemRequest::post('port');
1026
1027 $host = WebTotemAPI::siteInfo();
1028
1029 switch ($action) {
1030 case 'add':
1031 $response = WebTotemAPI::addIgnorePort($host['id'], $port);
1032 break;
1033
1034 case 'remove':
1035 $response = WebTotemAPI::removeIgnorePort($host['id'], $port);
1036 break;
1037 }
1038
1039 if (!isset($response['errors'])) {
1040
1041 $ports = WebTotemAPI::getAllPortsList($host['id']);
1042 $open_ports[] = [
1043 'variables' => [
1044 "ports" => $ports['TCPResults'] ? WebTotem::getOpenPortsData(array_slice($ports['TCPResults'], 0, 3)) : [],
1045 'more' => true,
1046 ],
1047 'template' => 'open_ports',
1048 ];
1049
1050 $open_ports_modal[] = [
1051 'variables' => [
1052 "ports" => WebTotem::getOpenPortsData($ports['TCPResults']),
1053 ],
1054 'template' => 'open_ports',
1055 ];
1056
1057 $ignore_ports[] = [
1058 'variables' => [
1059 "ports" => $ports,
1060 ],
1061 'template' => 'ignore_ports',
1062 ];
1063 $response = [
1064 'open_ports' => $template->arrayRender($open_ports),
1065 'open_ports_modal' => $template->arrayRender($open_ports_modal),
1066 'ignore_ports' => $template->arrayRender($ignore_ports),
1067 ];
1068
1069 }
1070
1071 $response['success'] = true;
1072 $response['notifications'] = self::notifications();
1073
1074 wp_send_json($response);
1075 }
1076
1077 /**
1078 * Request for a report link.
1079 *
1080 * @return void
1081 */
1082 public static function reports()
1083 {
1084
1085 if (WebTotemRequest::post('ajax_action') !== 'reports') {
1086 return;
1087 }
1088
1089 $template = new WebTotemTemplate();
1090
1091 $action = WebTotemRequest::post('report_action');
1092
1093 switch ($action) {
1094 case 'download':
1095 $id = WebTotemRequest::post('id');
1096 $link = WebTotemAPI::downloadReport($id);
1097 if ($link) {
1098 $response['link'] = $link;
1099 }
1100 break;
1101 case 'report_form':
1102
1103 $period = [WebTotemRequest::post('date_from'), WebTotemRequest::post('date_to')];
1104 $modules_data = WebTotemRequest::post('modules');
1105
1106 $modules = [
1107 'wa' => 'false',
1108 'dc' => 'false',
1109 'ps' => 'false',
1110 'rc' => 'false',
1111 'sc' => 'false',
1112 'av' => 'false',
1113 'waf' => 'false'
1114 ];
1115
1116 foreach ($modules_data as $module => $value) {
1117 $modules[$module] = 'true';
1118 }
1119
1120 $host = WebTotemAPI::siteInfo();
1121 $api_response = WebTotemAPI::generateReport($host['id'], $period, $modules);
1122
1123 if (!$api_response) {
1124 $massage = '<div class="message error_message">' . __('Report generation error', 'wtotem') . '</div>';
1125 } else {
1126 $data = WebTotemAPI::getAllReports($host['id']);
1127 WebTotemCache::setData(['getAllReports' => $data], $host['id']);
1128
1129 // Reports.
1130 $build[] = [
1131 'variables' => [
1132 "reports" => WebTotem::getReports($data['edges']),
1133 "has_next_page" => $data['pageInfo']['hasNextPage'],
1134 ],
1135 'template' => 'reports_list',
1136 ];
1137
1138 // Reports mobile.
1139 $build_mobile[] = [
1140 'variables' => [
1141 "reports" => WebTotem::getReports($data['edges']),
1142 "has_next_page" => $data['pageInfo']['hasNextPage'],
1143 ],
1144 'template' => 'reports_list_mobile',
1145 ];
1146
1147 $response = [
1148 'reports' => $template->arrayRender($build),
1149 'reports_m' => $template->arrayRender($build_mobile),
1150 'link' => $api_response,
1151 ];
1152
1153 $massage = '<div class="message success_message">' . __('The report was successfully generated', 'wtotem') . '</div>';
1154 }
1155
1156 $response['massage'] = $massage;
1157
1158 break;
1159 }
1160
1161 $response['success'] = true;
1162 $response['notifications'] = self::notifications();
1163 wp_send_json($response);
1164 }
1165
1166 /**
1167 * Request for a report link.
1168 *
1169 * @return void
1170 */
1171 public static function settings()
1172 {
1173
1174 if (WebTotemRequest::post('ajax_action') !== 'settings') {
1175 return;
1176 }
1177
1178 $av_installed = WebTotemOption::getOption('av_installed');
1179 $waf_installed = WebTotemOption::getOption('waf_installed');
1180 $action = WebTotemRequest::post('settings_action');
1181
1182 if (in_array($action, ['module_toggle', 'module_notifications', 'waf_settings', 'add_allow_ip', 'add_deny_ip', 'add_allow_url', 'add_ip_list', 'country_blocking'])) {
1183 if (!$av_installed && !$waf_installed) {
1184 WebTotemOption::setNotification('warning', __('It is not possible to make changes because the agents are not installed.', 'wtotem'));
1185
1186 wp_send_json([
1187 'success' => false,
1188 'notifications' => self::notifications()
1189 ]);
1190 return;
1191 }
1192 }
1193
1194 $host = WebTotemAPI::siteInfo();
1195 $template = new WebTotemTemplate();
1196
1197 switch ($action) {
1198
1199 case 'module_toggle':
1200 $config = WebTotemAPI::toggleConfigs(WebTotemRequest::post('value'));
1201
1202 $configs_data = WebTotemAPI::getConfigs($host['id']);
1203 WebTotemCache::setData(['getConfigs' => $configs_data], $host['id']);
1204
1205 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1206 $response['isActive'] = $config['isActive'];
1207 $response['success'] = true;
1208 break;
1209
1210 case 'country_blocking':
1211 $countries = WebTotemRequest::post('checked_countries');
1212
1213 if (WebTotemAPI::syncBlockedCountries($host['id'], $countries)) {
1214 $waf_data = WebTotemAPI::getBlockedCountries($host['id']);
1215 WebTotemCache::setData(['getBlockedCountries' => $waf_data], $host['id']);
1216
1217 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1218 $response['blocked_countries_list'] = $waf_data['blockedCountries'];
1219 $response['success'] = true;
1220 } else {
1221 WebTotemOption::setNotification('success', __('Your changes have not been applied.', 'wtotem'));
1222 $response['success'] = false;
1223 }
1224
1225 break;
1226
1227 case 'module_notifications':
1228 $config = WebTotemAPI::toggleNotifications($host['id'], WebTotemRequest::post('value'));
1229
1230 $configs_data = WebTotemAPI::getConfigs($host['id']);
1231 WebTotemCache::setData(['getConfigs' => $configs_data], $host['id']);
1232
1233 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1234 $response['isActive'] = $config;
1235 $response['success'] = true;
1236 break;
1237
1238 case 'waf_settings':
1239
1240 $response['success'] = true;
1241
1242 $dos = WebTotemRequest::post('dos');
1243 $dos_limit = WebTotemRequest::post('dos_limit');
1244 // $login_attempt = WebTotemRequest::post('login_attempt');
1245 // $login_attempt_limit = WebTotemRequest::post('login_attempt_limit');
1246
1247 if ($dos) {
1248 if (empty($dos_limit)) {
1249 $response['errors']['dos_limit'] = __('The field is required.', 'wtotem');
1250 } else if ($dos_limit < 500 or $dos_limit > 10000) {
1251 $response['success'] = false;
1252 $response['errors']['dos_limit'] = sprintf(__('Please specify a value from %s to %s.', 'wtotem'), '500', '10 000');
1253 }
1254 }
1255
1256 // if ($login_attempt) {
1257 // if (empty($login_attempt_limit)) {
1258 // $response['errors']['login_attempt_limit'] = __('The field is required.', 'wtotem');
1259 // } else if ($login_attempt_limit < 5 or $login_attempt_limit > 30) {
1260 // $response['success'] = false;
1261 // $response['errors']['login_attempt_limit'] = sprintf(__('Please specify a value from %s to %s.', 'wtotem'), '5', '30');
1262 // }
1263 // }
1264
1265 if (!$response['success']) {
1266 break;
1267 } else {
1268 $response['errors'] = false;
1269 }
1270
1271 $settings = [
1272 'gdn' => WebTotemRequest::post('gdn'),
1273 'dosProtection' => $dos,
1274 'dosLimit' => $dos_limit,
1275 'loginAttemptsProtection' => 'false',
1276 'loginAttemptsLimit' => 20,
1277 ];
1278
1279 $host = WebTotemAPI::siteInfo();
1280 $api_response = WebTotemAPI::setFirewallSettings($host['id'], $settings);
1281
1282 if (!$api_response['errors']) {
1283
1284 $data = WebTotemAPI::getIpLists($host['id']);
1285 WebTotemCache::setData(['getIpLists' => $data], $host['id']);
1286
1287 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1288 }
1289
1290 break;
1291
1292 case 'recaptcha_settings':
1293
1294 $recaptcha_v3_site_key = WebTotemRequest::post('recaptcha_v3_site_key');
1295 $recaptcha_v3_secret = WebTotemRequest::post('recaptcha_v3_secret');
1296 $recaptcha_token = WebTotemRequest::post('recaptcha_token');
1297 $recaptcha = filter_var(WebTotemRequest::post('recaptcha'), FILTER_VALIDATE_BOOLEAN) ?: false;
1298
1299 if ($recaptcha) {
1300 if (empty($recaptcha_v3_site_key) or empty($recaptcha_v3_secret) or strlen($recaptcha_v3_site_key) != 40 or strlen($recaptcha_v3_secret) != 40) {
1301 $response['success'] = false;
1302
1303 $response['errors'] = ['recaptcha_v3_site_key' => '', 'recaptcha_v3_secret' => ''];
1304
1305 if (empty($recaptcha_v3_site_key)) {
1306 $response['errors']['recaptcha_v3_site_key'] = __('The field is required.', 'wtotem');
1307 } else if (strlen($recaptcha_v3_site_key) != 40) {
1308 $response['errors']['recaptcha_v3_site_key'] = __('Invalid field length.', 'wtotem');
1309 }
1310 if (empty($recaptcha_v3_secret)) {
1311 $response['errors']['recaptcha_v3_secret'] = __('The field is required.', 'wtotem');
1312 } else if (strlen($recaptcha_v3_secret) != 40) {
1313 $response['errors']['recaptcha_v3_secret'] = __('Invalid field length.', 'wtotem');
1314 }
1315
1316 break;
1317 }
1318
1319 $score = WebTotemCaptcha::score($recaptcha_token, $recaptcha_v3_secret);
1320
1321 if ($score == 0) {
1322 $response['success'] = false;
1323 $response['errors']['recaptcha_v3_secret'] = __('Please check your keys and try again.', 'wtotem');
1324 $response['errors']['recaptcha_v3_site_key'] = __('Please check your keys and try again.', 'wtotem');
1325 break;
1326 }
1327 }
1328
1329 if ($recaptcha) {
1330 $settings = [
1331 'recaptcha_v3_site_key' => $recaptcha_v3_site_key,
1332 'recaptcha_v3_secret' => $recaptcha_v3_secret,
1333 ];
1334 }
1335 $settings['recaptcha'] = $recaptcha;
1336
1337 if ($settings['hide_wp_version']) {
1338 WebTotemOption::hideReadme();
1339 } else {
1340 WebTotemOption::restoreReadme();
1341 }
1342
1343 WebTotemOption::setPluginSettings($settings);
1344
1345 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1346 WebTotemOption::setNotification('warning', __('Please make sure that no other recaptcha is used on your site. Otherwise, there may be a conflict that will cause problems when logging into the admin panel.', 'wtotem'));
1347
1348 $response['success'] = true;
1349
1350
1351 break;
1352
1353 case 'two_factor_settings':
1354 $two_factor = filter_var(WebTotemRequest::post('two_factor'), FILTER_VALIDATE_BOOLEAN) ?: false;
1355
1356 WebTotemOption::setPluginSettings([
1357 'two_factor' => $two_factor,
1358 ]);
1359
1360 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1361 if ($two_factor) {
1362 WebTotemOption::setNotification('warning', __('Please make sure that no other 2FA is used on your site. Otherwise, there may be a conflict that will cause problems when logging into the admin panel.', 'wtotem'));
1363 }
1364
1365 $response['success'] = true;
1366
1367 break;
1368
1369
1370 case 'other_settings':
1371
1372 $settings = [
1373 'hide_wp_version' => filter_var(WebTotemRequest::post('hide_wp_version'), FILTER_VALIDATE_BOOLEAN) ?: false,
1374 'disable_user_enumeration' => filter_var(WebTotemRequest::post('disable_user_enumeration'), FILTER_VALIDATE_BOOLEAN) ?: false,
1375 ];
1376
1377 if ($settings['hide_wp_version']) {
1378 WebTotemOption::hideReadme();
1379
1380 } else {
1381 WebTotemOption::restoreReadme();
1382 }
1383
1384 WebTotemOption::setPluginSettings($settings);
1385
1386 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1387
1388 $response['success'] = true;
1389
1390 break;
1391
1392 case 'bruteforce_protection_settings':
1393
1394 $data = WebTotemRequest::post('data');
1395 $response['success'] = true;
1396
1397 $login_attempts = filter_var($data['login_attempts'], FILTER_VALIDATE_BOOLEAN) ?: false;
1398 $password_reset = filter_var($data['password_reset'], FILTER_VALIDATE_BOOLEAN) ?: false;
1399
1400 if ($login_attempts) {
1401 $response['errors'] = ['login_number_of_attempts' => '', 'login_minutes_of_ban' => ''];
1402
1403 if (empty($data['login_number_of_attempts']) or empty($data['login_minutes_of_ban'])) {
1404 $response['success'] = false;
1405
1406 if (empty($data['login_number_of_attempts'])) {
1407 $response['errors']['login_number_of_attempts'] = __('The field is required.', 'wtotem');
1408 }
1409 if (empty($data['login_minutes_of_ban'])) {
1410 $response['errors']['login_minutes_of_ban'] = __('The field is required.', 'wtotem');
1411 }
1412 } else if ($data['login_number_of_attempts'] <= 0 or $data['login_number_of_attempts'] > 1000000) {
1413 $response['success'] = false;
1414 $response['errors']['login_number_of_attempts'] = sprintf(__('Please specify a value from %s to %s.', 'wtotem'), '1', '1000000');
1415 }
1416 }
1417
1418 if ($password_reset) {
1419 if (empty($data['password_reset_number_of_attempts']) or empty($data['password_reset_minutes_of_ban'])) {
1420 $response['success'] = false;
1421
1422 $response['errors']['password_reset_number_of_attempts'] = '';
1423 $response['errors']['password_reset_minutes_of_ban'] = '';
1424
1425 if (empty($data['password_reset_number_of_attempts'])) {
1426 $response['errors']['password_reset_number_of_attempts'] = __('The field is required.', 'wtotem');
1427 }
1428 if (empty($data['password_reset_minutes_of_ban'])) {
1429 $response['errors']['password_reset_minutes_of_ban'] = __('The field is required.', 'wtotem');
1430 }
1431 } else if ($data['password_reset_number_of_attempts'] <= 0 or $data['password_reset_number_of_attempts'] > 1000000) {
1432 $response['success'] = false;
1433 $response['errors']['password_reset_number_of_attempts'] = sprintf(__('Please specify a value from %s to %s.', 'wtotem'), '1', '1000000');
1434 }
1435 }
1436 if (!$response['success']) {
1437 break;
1438 } else {
1439 $response['errors'] = false;
1440 }
1441
1442 $settings = [
1443 'login_attempts' => $login_attempts,
1444 'password_reset' => $password_reset,
1445 ];
1446
1447 if ($login_attempts) {
1448 $settings['login_number_of_attempts'] = $data['login_number_of_attempts'];
1449 $settings['login_minutes_of_ban'] = $data['login_minutes_of_ban'];
1450 }
1451 if ($password_reset) {
1452 $settings['password_reset_number_of_attempts'] = $data['password_reset_number_of_attempts'];
1453 $settings['password_reset_minutes_of_ban'] = $data['password_reset_minutes_of_ban'];
1454 }
1455
1456 WebTotemOption::setPluginSettings($settings);
1457
1458 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1459
1460 break;
1461
1462 case 'add_allow_ip':
1463 $api_response = WebTotemAPI::addIpToList($host['id'], WebTotemRequest::post('value'), 'white');
1464 if ($api_response) {
1465 $data = WebTotemAPI::getIpLists($host['id']);
1466 WebTotemCache::setData(['getIpLists' => $data], $host['id']);
1467 $build[] = [
1468 'variables' => [
1469 "list" => WebTotem::getIpList($data['whiteList'], 'ip_allow'),
1470 ],
1471 'template' => 'allow_deny_list',
1472 ];
1473
1474 $response['content'] = $template->arrayRender($build);
1475 }
1476
1477 $response['success'] = true;
1478 break;
1479
1480 case 'add_deny_ip':
1481 $api_response = WebTotemAPI::addIpToList($host['id'], WebTotemRequest::post('value'), 'black');
1482 if ($api_response) {
1483 $data = WebTotemAPI::getIpLists($host['id']);
1484 WebTotemCache::setData(['getIpLists' => $data], $host['id']);
1485 $build[] = [
1486 'variables' => [
1487 "list" => WebTotem::getIpList($data['blackList'], 'ip_deny'),
1488 ],
1489 'template' => 'allow_deny_list',
1490 ];
1491
1492 $response['content'] = $template->arrayRender($build);
1493 }
1494
1495 $response['success'] = true;
1496 break;
1497
1498 case 'add_allow_url':
1499 $api_response = WebTotemAPI::addUrlToAllowList($host['id'], WebTotemRequest::post('value'));
1500 if ($api_response) {
1501 $data = WebTotemAPI::getAllowUrlList($host['id']);
1502 $build[] = [
1503 'variables' => [
1504 "list" => WebTotem::getUrlAllowList($data),
1505 ],
1506 'template' => 'allow_url_list',
1507 ];
1508
1509 $response['content'] = $template->arrayRender($build);
1510 }
1511
1512 $response['success'] = true;
1513 break;
1514
1515 case 'add_ip_list':
1516 $ips = WebTotemRequest::post('ips');
1517 $list_name = WebTotemRequest::post('list');
1518
1519 $host = WebTotemAPI::siteInfo();
1520 $api_response = WebTotemAPI::addIpToList($host['id'], $ips, $list_name);
1521
1522 if ($api_response) {
1523 $data = WebTotemAPI::getIpLists($host['id']);
1524
1525 $data_list = ($list_name == 'white') ? $data['whiteList'] : $data['blackList'];
1526 $ip_list = ($list_name == 'white') ? 'ip_allow' : 'ip_deny';
1527
1528 $build[] = [
1529 'variables' => [
1530 "list" => WebTotem::getIpList($data_list, $ip_list),
1531 ],
1532 'template' => 'allow_deny_list',
1533 ];
1534
1535 if ($api_response['status'] != 0) {
1536 $response['invalidIPs'] = implode("\n", $api_response['invalidIPs']);
1537 }
1538
1539 $response['wrap'] = ($list_name == 'white') ? '#wtotem_ip_allow_list' : '#wtotem_ip_deny_list';
1540 $response['content'] = $template->arrayRender($build);
1541 }
1542 $response['success'] = true;
1543
1544 break;
1545 }
1546
1547 $response['notifications'] = self::notifications();
1548 wp_send_json($response);
1549 }
1550
1551 /**
1552 * Request to remove from the list of deny/allowed ip or url addresses.
1553 *
1554 * @return void
1555 */
1556 public static function remove()
1557 {
1558
1559 if (WebTotemRequest::post('ajax_action') !== 'remove') {
1560 return;
1561 }
1562
1563 $av_installed = WebTotemOption::getOption('av_installed');
1564 $waf_installed = WebTotemOption::getOption('waf_installed');
1565
1566 if (!$av_installed && !$waf_installed) {
1567 WebTotemOption::setNotification('warning', __('It is not possible to make changes because the agents are not installed.', 'wtotem'));
1568
1569 wp_send_json([
1570 'success' => false,
1571 'notifications' => self::notifications()
1572 ]);
1573 }
1574
1575 $action = WebTotemRequest::post('remove_action');
1576 $host = WebTotemAPI::siteInfo();
1577 $template = new WebTotemTemplate();
1578
1579 switch ($action) {
1580 case 'ip_allow':
1581 $api_response = WebTotemAPI::removeIpFromList(WebTotemRequest::post('id'));
1582
1583 if ($api_response) {
1584 $data = WebTotemAPI::getIpLists($host['id']);
1585
1586 $build[] = [
1587 'variables' => [
1588 "list" => WebTotem::getIpList($data['whiteList'], 'ip_allow'),
1589 ],
1590 'template' => 'allow_deny_list',
1591 ];
1592
1593 $response['content'] = $template->arrayRender($build);
1594 $response['wrap'] = '#wtotem_ip_allow_list';
1595 }
1596 break;
1597
1598 case 'ip_deny':
1599 $api_response = WebTotemAPI::removeIpFromList(WebTotemRequest::post('id'));
1600
1601 if ($api_response) {
1602 $data = WebTotemAPI::getIpLists($host['id']);
1603
1604 $build[] = [
1605 'variables' => [
1606 "list" => WebTotem::getIpList($data['blackList'], 'ip_deny'),
1607 ],
1608 'template' => 'allow_deny_list',
1609 ];
1610
1611 $response['content'] = $template->arrayRender($build);
1612 $response['wrap'] = '#wtotem_ip_deny_list';
1613 }
1614 break;
1615
1616 case 'url_allow':
1617 $api_response = WebTotemAPI::removeUrlFromAllowList(WebTotemRequest::post('id'));
1618
1619 if ($api_response) {
1620 $data = WebTotemAPI::getAllowUrlList($host['id']);
1621
1622 $build[] = [
1623 'variables' => [
1624 "list" => WebTotem::getUrlAllowList($data),
1625 ],
1626 'template' => 'allow_url_list',
1627 ];
1628
1629 $response['content'] = $template->arrayRender($build);
1630 $response['wrap'] = '#wtotem_allow_url';
1631 }
1632 break;
1633 }
1634
1635 $response['success'] = true;
1636 $response['notifications'] = self::notifications();
1637 wp_send_json($response);
1638 }
1639
1640 /**
1641 * Request to remove site from WebTotem.
1642 *
1643 * @return void
1644 */
1645 public static function multisite()
1646 {
1647
1648 if (WebTotemRequest::post('ajax_action') !== 'multisite') {
1649 return;
1650 }
1651
1652 $action = WebTotemRequest::post('multisite_action');
1653 $template = new WebTotemTemplate();
1654
1655 switch ($action) {
1656 case 'remove_site':
1657
1658 $host_id = WebTotemRequest::post('hid');
1659 $main_host = WebTotemOption::getMainHost();
1660
1661 if ($host_id == $main_host['id']) {
1662 WebTotemOption::setNotification('error', __('You cannot delete the primary domain.', 'wtotem'));
1663 break;
1664 }
1665 WebTotemAPI::removeMultiSiteHost($host_id);
1666
1667 break;
1668
1669 case 'add_site':
1670
1671 $new_site = WebTotemRequest::post('site_name');
1672 WebTotemAPI::addMultiSiteNewSites([$new_site]);
1673
1674 break;
1675 }
1676
1677 $allSites = WebTotemAPI::getSites();
1678 $has_next_page = $allSites['pageInfo']['hasNextPage'];
1679
1680 WebTotemOption::setSessionOptions([
1681 'sites_cursor' => $allSites['pageInfo']['endCursor'],
1682 ]);
1683
1684 // Sites list.
1685 $build[] = [
1686 'variables' => [
1687 'sites' => WebTotem::allSitesData($allSites),
1688 'has_next_page' => $has_next_page,
1689 ],
1690 'template' => 'multisite_list'
1691 ];
1692
1693 $response['content'] = $template->arrayRender($build);
1694
1695 $response['success'] = true;
1696 $response['notifications'] = self::notifications();
1697 wp_send_json($response);
1698 }
1699
1700 /**
1701 * Request to remove site from WebTotem.
1702 *
1703 * @return void
1704 */
1705 public static function twoFactorAuth()
1706 {
1707
1708 if (WebTotemRequest::post('ajax_action') !== 'two_factor_auth') {
1709 return;
1710 }
1711
1712 $action = WebTotemRequest::post('case_action');
1713 $template = new WebTotemTemplate();
1714 $current_user = wp_get_current_user();
1715
1716 if ($user_id = (int)WebTotemRequest::post('user_id')) {
1717 if ($current_user->ID !== $user_id and !current_user_can('manage_options')) {
1718
1719 WebTotemOption::setNotification('info', '$current_user->ID: ' . $current_user->ID . ', $user_id: ' . $user_id);
1720 WebTotemOption::setNotification('error', __('You cannot edit this user.', 'wtotem'));
1721 return;
1722 }
1723 $user = get_user_by('id', $user_id);
1724 } else {
1725 $user = $current_user;
1726 }
1727
1728 switch ($action) {
1729 case 'activate':
1730
1731 $g = new WebTotemGoogleAuthenticator();
1732
1733 $secret = WebTotemRequest::post('secret');
1734 $recovery = WebTotemRequest::post('recovery');
1735 $code = WebTotemRequest::post('code');
1736
1737 if ($g->checkCode($secret, $code)) {
1738 WebTotemLogin::saveData($user->ID, $recovery, $secret);
1739 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1740 $response['success'] = true;
1741 } else {
1742 WebTotemOption::setNotification('error', __('You have entered an incorrect activation code.', 'wtotem'));
1743 $response['success'] = false;
1744 }
1745
1746 break;
1747
1748 case 'deactivate':
1749
1750 WebTotemLogin::delete($user->ID);
1751
1752 $response['success'] = true;
1753
1754 break;
1755 }
1756
1757 $build[] = [
1758 'variables' => [
1759 'two_factor' => WebTotemLogin::getTwoFactorData($user),
1760 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
1761 'user_id' => $user->ID,
1762 ],
1763 'template' => 'two_factor_auth'
1764 ];
1765
1766 $response['content'] = $template->arrayRender($build);
1767
1768 $response['notifications'] = self::notifications();
1769 wp_send_json($response);
1770 }
1771
1772 /**
1773 * Changing the theme mode.
1774 *
1775 * @return void
1776 */
1777 public static function changeThemeMode()
1778 {
1779
1780 if (WebTotemRequest::post('ajax_action') !== 'theme_mode') {
1781 return;
1782 }
1783
1784 $theme_mode = WebTotemOption::getSessionOption('theme_mode');
1785
1786 if ($theme_mode == 'dark') {
1787 WebTotemOption::setSessionOptions(['theme_mode' => 'light']);
1788 $response = 'light';
1789 } else {
1790 WebTotemOption::setSessionOptions(['theme_mode' => 'dark']);
1791 $response = 'dark';
1792 }
1793
1794 wp_send_json($response);
1795 }
1796
1797 /**
1798 * Set user time zone offset.
1799 *
1800 * @return void
1801 */
1802 public static function userTimeZone()
1803 {
1804
1805 if (WebTotemRequest::post('ajax_action') !== 'set_time_zone') {
1806 return;
1807 }
1808
1809 $time_zone_offset = WebTotemRequest::post('offset');
1810 $now = strtotime('now');
1811 $check = WebTotemOption::getOption('time_zone_check') ?: 0;
1812
1813 // Checking whether an hour has elapsed since the previous request.
1814 if ($now >= $check) {
1815 $time_zone = WebTotemAPI::getTimeZone();
1816 if ($time_zone) {
1817 $time_zone_offset = timezone_offset_get(new \DateTimeZone($time_zone), new \DateTime('now', new \DateTimeZone('Europe/London'))) / 3600;
1818 WebTotemOption::setOptions(['time_zone_check' => $now + 3600]);
1819 }
1820 WebTotemOption::setOptions(['time_zone_offset' => $time_zone_offset]);
1821 }
1822
1823 wp_send_json([
1824 'success' => true,
1825 'time_zone_offset' => $time_zone_offset
1826 ]);
1827
1828 }
1829
1830 /**
1831 * Forced checking of services.
1832 *
1833 * @return void
1834 */
1835 public static function force_check()
1836 {
1837 if (WebTotemRequest::post('ajax_action') !== 'force_check') {
1838 return;
1839 }
1840
1841 $service = WebTotemRequest::post('service');
1842 $host = WebTotemAPI::siteInfo();
1843 $template = new WebTotemTemplate();
1844
1845 $response['success'] = false;
1846
1847 if($service){
1848 // force check service
1849 $_response = WebTotemAPI::forceCheck($host['id'], $service);
1850
1851 if (!isset($_response['errors'])) {
1852
1853
1854 switch ($service) {
1855
1856 case 'ssl':
1857 $data = WebTotemAPI::getMonitoring($host['id']);
1858
1859 $ssl = [
1860 'status' => WebTotem::getStatusData($data['sslResults']['results'][0]['certStatus']),
1861 'cert_name' => $data['sslResults']['results'][0]['certIssuerName'],
1862 'days_left' => WebTotem::daysLeft($data['sslResults']['results'][0]['certExpiryDate']),
1863 'issue_date' => WebTotem::dateFormatter($data['sslResults']['results'][0]['certIssueDate']),
1864 'expiry_date' => WebTotem::dateFormatter($data['sslResults']['results'][0]['certExpiryDate']),
1865 ];
1866
1867 $build[] = [
1868 'variables' => [
1869 'ssl' => $ssl,
1870 ],
1871 'template' => 'monitoring_ssl',
1872 ];
1873
1874 $response['content'] = $template->arrayRender($build);
1875 $response['success'] = true;
1876 break;
1877
1878 case 'dec':
1879 $data = WebTotemAPI::getMonitoring($host['id']);
1880
1881 $domain = [
1882 'status' => WebTotem::getStatusData($data['domain']['lastScanResult']['status']),
1883 "redirect_link" => $data['domain']['lastScanResult']['redirectLink'],
1884 "is_created_at" => (bool)$data['domain']['lastScanResult']['time'],
1885 "created_at" => WebTotem::dateFormatter($data['domain']['lastScanResult']['time']),
1886 "is_taken" => $data['domain']['lastScanResult']['isTaken'],
1887 "ips" => $data['domain']['lastScanResult']['ips'],
1888 "protection" => $data['domain']['lastScanResult']['protection'],
1889 ];
1890
1891 $build[] = [
1892 'variables' => [
1893 'domain' => $domain,
1894 ],
1895 'template' => 'monitoring_domain',
1896 ];
1897
1898 $response['content'] = $template->arrayRender($build);
1899 $response['success'] = true;
1900 break;
1901
1902 case 'rc':
1903 $data = WebTotemAPI::getMonitoring($host['id']);
1904
1905 $reputation = [
1906 "status" => WebTotem::getStatusData($data['reputation']['status']),
1907 "blacklists_entries" => WebTotem::blacklistsEntries($data['reputation']['status'], $data['reputation']['virusList']),
1908 "info" => WebTotem::getReputationInfo($data['reputation']['status']),
1909 "last_test" => WebTotem::dateFormatter($data['reputation']['lastTest']['time']),
1910 ];
1911
1912 $build[] = [
1913 'variables' => [
1914 'reputation' => $reputation,
1915 ],
1916 'template' => 'monitoring_reputation',
1917 ];
1918
1919 $response['content'] = $template->arrayRender($build);
1920 $response['success'] = true;
1921 break;
1922
1923 case 'ps':
1924 $ports = WebTotemAPI::getAllPortsList($host['id']);
1925
1926 if($ports['TCPResults']){
1927 $open_ports[] = [
1928 'variables' => [
1929 "ports" => WebTotem::getOpenPortsData($ports['TCPResults']),
1930 ],
1931 'template' => 'open_ports',
1932 ];
1933
1934 $open_ports_few[] = [
1935 'variables' => [
1936 "ports" => $ports['TCPResults'] ? WebTotem::getOpenPortsData(array_slice($ports['TCPResults'], 0, 3)) : [],
1937 "more" => true,
1938 ],
1939 'template' => 'open_ports',
1940 ];
1941 }
1942
1943 $ignore_ports[] = [
1944 'variables' => [
1945 "ports" => $ports,
1946 ],
1947 'template' => 'ignore_ports',
1948 ];
1949 $response = [
1950 'status' => WebTotem::getStatusData($ports['status']),
1951 'last_test' => WebTotem::dateFormatter($ports['lastTest']['time']),
1952 'open_ports' => (isset($open_ports)) ? $template->arrayRender($open_ports) : '',
1953 'open_ports_few' => (isset($open_ports_few)) ? $template->arrayRender($open_ports_few) : '',
1954 'ignore_ports' => $template->arrayRender($ignore_ports),
1955 ];
1956
1957 $response['success'] = true;
1958
1959 break;
1960
1961 case 'ops':
1962
1963 $open_path_data = WebTotemAPI::getOpenPaths($host['id']);
1964 $open_path[] = [
1965 'variables' => [
1966 "paths" => $open_path_data['paths'],
1967 ],
1968 'template' => 'open_paths',
1969 ];
1970
1971 $response = [
1972 'status' => WebTotem::getStatusData(($open_path_data['paths']) ? 'warning' : 'clean'),
1973 "last_test" => WebTotem::dateFormatter($open_path_data['time']),
1974 'open_paths' => $template->arrayRender($open_path),
1975 ];
1976
1977 $response['success'] = true;
1978 break;
1979 }
1980
1981
1982
1983 }
1984 }
1985
1986 $response['notifications'] = self::notifications();
1987
1988
1989 wp_send_json($response);
1990 }
1991
1992 /**
1993 * Initialization scanning and checking the current status.
1994 *
1995 * @return void
1996 */
1997 public static function wtotem_scan()
1998 {
1999
2000 if (WebTotemRequest::get('ajax_action') !== 'wtotem_scan') {
2001 return;
2002 }
2003
2004 $logs_action = WebTotemRequest::get('scan_action');
2005
2006 switch ($logs_action) {
2007 case 'init':
2008 if(!WebTotemOption::getOption('scan_init')){
2009 WebTotemOption::setOptions(['scan_init' => 1]);
2010 WebTotemScan::initialize();
2011 $response = [
2012 'success' => true,
2013 'scan_start' => 'success',
2014 ];
2015 } else {
2016 $response = [
2017 'success' => false,
2018 'error' => 'The scan is already running',
2019 ];
2020 }
2021
2022 break;
2023
2024 case 'push':
2025 if(WebTotemOption::getOption('scan_init')) {
2026 WebTotemScan::initialize();
2027 $response = [
2028 'success' => true,
2029 'scan_finished' => false,
2030 'push' => 'success',
2031 ];
2032 } else {
2033 $response = [
2034 'success' => false,
2035 'scan_finished' => true,
2036 'push' => 'fail',
2037 'error' => 'Scan completed',
2038 ];
2039 }
2040
2041 break;
2042 }
2043
2044
2045 wp_send_json($response ?? ['success' => false, 'error' => 'No action found']);
2046 }
2047
2048 /**
2049 * Forced checking of services.
2050 *
2051 * @return void
2052 */
2053 public static function user_feedback() {
2054 if (WebTotemRequest::post('ajax_action') !== 'user_feedback') {
2055 return;
2056 }
2057
2058 $data = [
2059 'score' => (int)WebTotemRequest::post('score'),
2060 'feedback' => WebTotemRequest::post('feedback') ?? "",
2061 ];
2062
2063 $response_data = WebTotemAPI::setFeedback($data);
2064 if($response_data['message'] == 'Score added'){
2065 $response['content'] = '<div style="text-align: center;"><img src="'.WebTotem::getImagePath('').'popup_success_icon.svg" style="width: 85px;"><p class="user-feedback__title" style="margin-bottom: 20px">'.__('Thank you for feedback', 'wtotem').'</p><button id="user-feedback-ok" class="wtotem_control__btn">Okay</button></div>';
2066 $response['success'] = true;
2067 WebTotemOption::setNotification('success', __('Your reply has been sent successfully.', 'wtotem'));
2068 } else {
2069 WebTotemOption::setNotification('error', __('There were difficulties. Your reply has not been sent.', 'wtotem'));
2070 $response['success'] = false;
2071 }
2072
2073 $response['notifications'] = self::notifications();
2074
2075 wp_send_json($response);
2076 }
2077
2078
2079
2080 /**
2081 * Updating the page data in the specified time interval.
2082 *
2083 * @return void
2084 */
2085 public static function reloadPage()
2086 {
2087
2088 if (WebTotemRequest::post('ajax_action') !== 'reload_page') {
2089 return;
2090 }
2091
2092 $page = WebTotemRequest::post('page');
2093
2094 $template = new WebTotemTemplate();
2095
2096 // Get data from WebTotem API.
2097 $host = WebTotemAPI::siteInfo();
2098
2099 switch ($page) {
2100 case 'dashboard':
2101
2102 $data = WebTotemAPI::getAllData($host['id']);
2103 WebTotemCache::setData(['getAllData' => $data], $host['id']);
2104
2105 // Start build array for rendering.
2106 // Scoring block.
2107 $service_data = $data['scoring']['result'];
2108 $total_score = round($data['scoring']['score']);
2109 $score_grading = WebTotem::scoreGrading($total_score);
2110 $build['scoring'] = [
2111 'variables' => [
2112 "host_id" => $host['id'],
2113 "total_score" => $total_score . "%",
2114 "tested_on" => WebTotem::dateFormatter($data['scoring']['lastTest']['time']),
2115 "server_ip" => $service_data['ip'] ?: ' - ',
2116 "location" => WebTotem::getCountryName($service_data['country']) ?: ' - ',
2117 "is_higher_than" => $service_data['isHigherThan'] . '%',
2118 "grade" => $score_grading['grade'],
2119 "color" => $score_grading['color'],
2120 ],
2121 'template' => 'score',
2122 ];
2123
2124 $is_period_available = WebTotem::isPeriodAvailable($data['agentManager']['createdAt']);
2125
2126 // Firewall stats.
2127 $period = WebTotemOption::getSessionOption('firewall_period');
2128 $service_data = $period ? WebTotemAPI::getFirewall($host['id'], 10, NULL, $period) : $data;
2129 $service_data = $service_data['firewall'];
2130
2131 $chart = WebTotem::generateWafChart($service_data['chart']);
2132 $build['firewall_stats'] = [
2133 'variables' => [
2134 "is_waf_training" => $data['agentManager'] && WebTotem::isWafTraining($data['agentManager']['createdAt']),
2135 "is_period_available" => $is_period_available,
2136 "most_attacks" => WebTotem::getMostAttacksData($service_data['map']),
2137 "all_attacks" => $chart['count_attacks'],
2138 "blocking" => $chart['count_blocks'],
2139 "not_blocking" => (int)$chart['count_attacks'] - (int)$chart['count_blocks'],
2140 ],
2141 'template' => 'firewall_stats',
2142 ];
2143
2144 $build['chart_periods'] = [
2145 'variables' => [
2146 "service" => 'waf',
2147 "days" => is_array($period) ? 7 : $period,
2148 ],
2149 'template' => 'chart_periods',
2150 ];
2151
2152 // Firewall blocks.
2153 $build['firewall_data'] = [
2154 'variables' => [
2155 "is_period_available" => $is_period_available,
2156 "chart" => $chart['chart'],
2157 "days" => $chart['days'],
2158 "logs" => WebTotem::wafLogs($service_data['logs']['edges']),
2159 ],
2160 'template' => 'firewall',
2161 ];
2162
2163 // Server Status RAM.
2164 $period = WebTotemOption::getSessionOption('ram_period') ?: 7;
2165 $service_data = $period ? WebTotemAPI::getServerStatusData($host['id'], $period) : $data['serverStatus'];
2166
2167 $build['server_status_ram'] = [
2168 'variables' => [
2169 "is_period_available" => $is_period_available,
2170 "info" => $service_data['info'],
2171 "ram_chart" => WebTotem::generateChart($service_data['ramChart']),
2172 "days" => $period,
2173 ],
2174 'template' => 'server_status_ram',
2175 ];
2176
2177 // Server Status CPU.
2178 $period = WebTotemOption::getSessionOption('cpu_period') ?: 7;
2179 $service_data = $period ? WebTotemAPI::getServerStatusData($host['id'], $period) : $data['serverStatus'];
2180 $build['server_status_cpu'] = [
2181 'variables' => [
2182 "is_period_available" => $is_period_available,
2183 "cpu_chart" => WebTotem::generateChart($service_data['cpuChart']),
2184 "days" => $period,
2185 ],
2186
2187 'template' => 'server_status_cpu',
2188 ];
2189
2190 // Antivirus stats blocks.
2191 $antivirus_stats = $data['antivirus']['stats'];
2192 $build['antivirus_stats'] = [
2193 'variables' => [
2194 "changes" => $antivirus_stats['changed'] ?: 0,
2195 "scanned" => $antivirus_stats['scanned'] ?: 0,
2196 "deleted" => $antivirus_stats['deleted'] ?: 0,
2197 "infected" => $antivirus_stats["infected"] ?: 0,
2198 ],
2199
2200 'template' => 'antivirus_stats',
2201 ];
2202
2203 // Monitoring blocks.
2204 $ssl= false;
2205 if($data['sslResults']['results']){
2206 $ssl = [
2207 'status' => WebTotem::getStatusData($data['sslResults']['results'][0]['certStatus']),
2208 'cert_name' => $data['sslResults']['results'][0]['certIssuerName'],
2209 'days_left' => WebTotem::daysLeft($data['sslResults']['results'][0]['certExpiryDate']),
2210 'issue_date' => WebTotem::dateFormatter($data['sslResults']['results'][0]['certIssueDate']),
2211 'expiry_date' => WebTotem::dateFormatter($data['sslResults']['results'][0]['certExpiryDate']),
2212 ];
2213 }
2214
2215 $domain = false;
2216 if(WebTotem::isKz()){
2217 $domain = [
2218 'status' => WebTotem::getStatusData($data['domain']['lastScanResult']['status']),
2219 "redirect_link" => $data['domain']['lastScanResult']['redirectLink'],
2220 "is_created_at" => (bool)$data['domain']['lastScanResult']['time'],
2221 "created_at" => WebTotem::dateFormatter($data['domain']['lastScanResult']['time']),
2222 "is_taken" => $data['domain']['lastScanResult']['isTaken'],
2223 "ips" => $data['domain']['lastScanResult']['ips'],
2224 "protection" => $data['domain']['lastScanResult']['protection'],
2225 ];
2226 }
2227
2228 $build['monitoring'] = [
2229 'variables' => [
2230 "ssl" => $ssl,
2231 "domain" => $domain,
2232 'reputation' => [
2233 "status" => WebTotem::getStatusData($data['reputation']['status']),
2234 "blacklists_entries" => WebTotem::blacklistsEntries(
2235 $data['reputation']['status'],
2236 $data['reputation']['virusList']),
2237 "info" => WebTotem::getReputationInfo($data['reputation']['status']),
2238 "last_test" => WebTotem::dateFormatter($data['reputation']['lastTest']['time']),
2239 ],
2240 ],
2241 'template' => 'monitoring',
2242 ];
2243
2244 $ports = WebTotemAPI::getAllPortsList($host['id']);
2245
2246 // Scanning blocks.
2247 $build['scanning'] = [
2248 'variables' => [
2249 "ports" => [
2250 "status" => WebTotem::getStatusData($data['ports']['status']),
2251 "TCPResults" => WebTotem::getOpenPortsData($data['ports']['TCPResults']),
2252 "more" => true,
2253 "last_test" => WebTotem::dateFormatter($data['ports']['lastTest']['time']),
2254 ],
2255 "open_path" => [
2256 "status" => WebTotem::getStatusData(($data['openPathSearch']['paths']) ? 'warning' : 'clean'),
2257 "last_test" => WebTotem::dateFormatter($data['openPathSearch']['time']),
2258 "paths" => $data['openPathSearch']['paths'],
2259 ],
2260 ],
2261 'template' => 'scanning',
2262 ];
2263
2264 $build['ports_form'] = [
2265 'variables' => [
2266 "ports" => [
2267 "TCPResults" => WebTotem::getOpenPortsData($ports['TCPResults']),
2268 "ignorePorts" => $ports['ignorePorts'],
2269 ],
2270 ],
2271 'template' => 'ports_form',
2272 ];
2273
2274 $response['content'][] = ['selector' => '#scoring', 'content' => $template->arrayRender($build['scoring'])];
2275 $response['content'][] = ['selector' => '#firewall_stats', 'content' => $template->arrayRender($build['firewall_stats'])];
2276 $response['content'][] = ['selector' => '#waf_chart_period', 'content' => $template->arrayRender($build['chart_periods'])];
2277 $response['content'][] = ['selector' => '#firewall_data', 'content' => $template->arrayRender($build['firewall_data'])];
2278 $response['content'][] = ['selector' => '#server_status_cpu', 'content' => $template->arrayRender($build['server_status_cpu'])];
2279 $response['content'][] = ['selector' => '#server_status_ram', 'content' => $template->arrayRender($build['server_status_ram'])];
2280 $response['content'][] = ['selector' => '#antivirus_stats', 'content' => $template->arrayRender($build['antivirus_stats'])];
2281 $response['content'][] = ['selector' => '#monitoring', 'content' => $template->arrayRender($build['monitoring'])];
2282 $response['content'][] = ['selector' => '#scanning', 'content' => $template->arrayRender($build['scanning'])];
2283 $response['content'][] = ['selector' => '#ports_form', 'content' => $template->arrayRender($build['ports_form'])];
2284
2285 break;
2286 }
2287
2288 $response['success'] = true;
2289 $response['notifications'] = self::notifications();
2290 wp_send_json($response);
2291 }
2292
2293 public static function update_plugin() {
2294
2295 if (WebTotemRequest::post('ajax_action') !== 'update_plugin') {
2296 return;
2297 }
2298 $plugin_slug = WebTotemRequest::post('slug');
2299
2300 include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
2301
2302 $plugin = $plugin_slug . '/' . $plugin_slug . '.php';
2303
2304 $upgrader = new Plugin_Upgrader();
2305 $upgrader->upgrade($plugin);
2306
2307 WebTotemOption::setNotification('success', __('The plugin has been updated', 'wtotem'));
2308 die();
2309 }
2310
2311 public static function after_plugin_update(){
2312
2313 if (WebTotemRequest::post('ajax_action') !== 'after_plugin_update') {
2314 return;
2315 }
2316 $plugin_slug = WebTotemRequest::post('slug');
2317
2318 $plugin_info = WebTotem::get_plugin_info($plugin_slug);
2319
2320 if( WebTotem::updateCveDataByPluginName($plugin_info)){
2321
2322 $template = new WebTotemTemplate();
2323 $plugins_cve_list = WebTotemDB::getRows([], 'plugins_cve_list', false, ['limit' => 8, 'page' => 1]);
2324 $response['content'] = $template->arrayRender([
2325 'variables' => [
2326 "logs" => WebTotem::preparePluginsCveList($plugins_cve_list['data']),
2327 ],
2328 'template' => 'scan_logs_cve',
2329 ]);
2330 $response['count'] = $plugins_cve_list['count'];
2331 $response['pagination'] = WebTotem::paginationBuild(8, $plugins_cve_list['count']);
2332 }
2333
2334 $response['notifications'] = self::notifications();
2335
2336 wp_send_json($response);
2337 }
2338
2339 public static function authenticate()
2340 {
2341
2342 if (WebTotemRequest::post('ajax_action') !== 'authenticate') {
2343 return;
2344 }
2345
2346 $credentials = array(
2347 'log' => 'pwd',
2348 'username' => 'password'
2349 );
2350 $username = null;
2351 $password = null;
2352 foreach ($credentials as $usernameKey => $passwordKey) {
2353 if (array_key_exists($usernameKey, $_POST) &&
2354 array_key_exists($passwordKey, $_POST) &&
2355 is_string($_POST[$usernameKey]) &&
2356 is_string($_POST[$passwordKey])) {
2357 $username = $_POST[$usernameKey];
2358 $password = $_POST[$passwordKey];
2359 break;
2360 }
2361 }
2362 if (empty($username) || empty($password)) {
2363 $response['error'] = wp_kses(sprintf(__('<strong>ERROR</strong>: A username and password must be provided. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), wp_lostpassword_url()), array('strong' => array(), 'a' => array('href' => array(), 'title' => array())));
2364 }
2365
2366 do_action_ref_array('wp_authenticate', array(&$username, &$password));
2367
2368 $user = wp_authenticate($username, $password);
2369 $user = WebTotemBFProtection::checkBruteForceAttempts($user, $username);
2370
2371 if (is_object($user) && ($user instanceof \WP_User)) {
2372
2373 $response['login'] = true;
2374
2375 if (WebTotemLogin::hasUser2faActivated($user)) {
2376
2377 $template = new WebTotemTemplate();
2378
2379 $response['2fa'] = true;
2380 $response['content'] = $template->getHtml('login_auth_form');
2381
2382 }
2383 } else if (is_wp_error($user)) {
2384 $errors = array();
2385 foreach ($user->get_error_codes() as $code) {
2386 if ($code == 'invalid_username' || $code == 'invalid_email' || $code == 'incorrect_password' || $code == 'authentication_failed') {
2387 $errors[] = wp_kses(sprintf(__('<strong>ERROR</strong>: The username or password you entered is incorrect. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), wp_lostpassword_url()), array('strong' => array(), 'a' => array('href' => array(), 'title' => array())));
2388 } else {
2389 foreach ($user->get_error_messages($code) as $error_message) {
2390 $errors[] = $error_message;
2391 }
2392 }
2393 }
2394
2395 if (!empty($errors)) {
2396 $errors = implode('<br>', $errors);
2397 $response['error'] = apply_filters('login_errors', $errors);
2398 }
2399
2400 }
2401
2402 wp_send_json($response);
2403 }
2404
2405 /**
2406 * Notification output.
2407 *
2408 * @return string
2409 */
2410 public static function notifications()
2411 {
2412
2413 $notifications = WebTotem::getNotifications();
2414
2415 if ($notifications) {
2416 $build[] = [
2417 'variables' => [
2418 'notifications' => $notifications,
2419 ],
2420
2421 'template' => 'notifications',
2422 ];
2423
2424 $template = new WebTotemTemplate();
2425 return $template->arrayRender($build);
2426 }
2427 return false;
2428
2429 }
2430
2431
2432 }
2433