PluginProbe
WebTotem Security / 2.4.9
WebTotem Security v2.4.9
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.9, at lib/Ajax.php

1,446 lines 38.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) {
4 if (!headers_sent()) {
5 header('HTTP/1.1 403 Forbidden');
6 }
7 die('Protected By WebTotem!');
8 }
9
10 class WebTotemAjax {
11
12 /**
13 * Activation plugin.
14 *
15 * @return void
16 */
17 public static function activation() {
18
19 if (WebTotemRequest::post('ajax_action') !== 'activation') {
20 return;
21 }
22
23 if($api_key = WebTotemRequest::post('api_key')) {
24
25 $result = WebTotemAPI::auth($api_key);
26
27 if($result == 'success') {
28 if(WebTotem::isMultiSite()) {
29 $link = WebTotem::adminURL('admin.php?page=wtotem_all_sites');
30 } else {
31 $link = WebTotem::adminURL('admin.php?page=wtotem');
32 }
33 wp_send_json([
34 'link' => $link,
35 'success' => true,
36 'user' => WebTotemAPI::getEmail(),
37 ], 200);
38 } else {
39
40 wp_send_json([
41 'notifications' => self::notifications(),
42 'success' => false,
43 ], 200);
44 }
45 }
46
47 }
48
49 /**
50 * The process of installing agents (WAF, AV) on the main page.
51 *
52 * @return void
53 */
54 public static function agentsInstallation() {
55
56 if (WebTotemRequest::post('ajax_action') !== 'agents_installation') {
57 return;
58 }
59
60 $av_installed = WebTotemOption::getOption('av_installed');
61 $waf_installed = WebTotemOption::getOption('waf_installed');
62
63 // Check if the agents are installed.
64 if ($av_installed and $waf_installed) {
65 $agents_statuses = [
66 'process_statuses' => [
67 'av' => 'installed',
68 'waf' => 'installed',
69 ],
70 ];
71 }
72 else {
73 // If not installed, then request statuses from the WebTotem API.
74 $host = WebTotemAPI::siteInfo();
75 $data = WebTotemAPI::getAgentsStatusesFromAPI($host['id']);
76
77 $agents_statuses = [
78 'av' => $data['av']['status'],
79 'waf' => $data['waf']['status'],
80 ];
81
82 $agents_statuses = WebTotem::getAgentsStatuses($agents_statuses);
83 }
84
85 $build[] = [
86 'variables' => [
87 'process_status' => $agents_statuses['process_statuses'],
88 ],
89 'template' => 'agents_installation',
90 ];
91
92 $status = [
93 'av' => $agents_statuses['process_statuses']['av'] == 'installed',
94 'waf' => $agents_statuses['process_statuses']['waf'] == 'installed',
95 ];
96
97 WebTotemOption::setOptions([
98 'av_installed' => $status['av'],
99 'waf_installed' => $status['waf'],
100 ]);
101
102 $template = new WebTotemTemplate();
103 $agents = $template->arrayRender($build);
104
105 wp_send_json([
106 'success' => true,
107 'notifications' => self::notifications(),
108 'agents' => $agents,
109 'agents_statuses' => $status['av'] && $status['waf'],
110 ]);
111 }
112
113
114 /**
115 * Reinstall agents.
116 *
117 * @return void
118 */
119 public static function reinstallAgents() {
120
121 if (WebTotemRequest::post('ajax_action') !== 'reinstall_agents') {
122 return;
123 }
124
125 if (WebTotemAgentManager::removeAgents()) {
126 WebTotemAgentManager::amInstall();
127 }
128 $response['success'] = true;
129 $response['redirect_link'] = WebTotem::adminURL('admin.php?page=wtotem');
130 wp_send_json($response);
131
132 }
133
134 /**
135 * Deleting plugin activation data and redirecting to the activation page.
136 *
137 * @return void
138 */
139 public static function logout() {
140
141 if (WebTotemRequest::post('ajax_action') !== 'logout') {
142 return;
143 }
144
145 WebTotemOption::logout();
146
147 $response['success'] = true;
148 $response['redirect_link'] = WebTotem::adminURL('admin.php?page=wtotem_activation');
149 wp_send_json($response);
150
151 }
152
153 /**
154 * Creating a modal window.
155 *
156 * @return void
157 */
158 public static function popup() {
159
160 if (WebTotemRequest::post('ajax_action') !== 'popup') {
161 return;
162 }
163
164 $action = WebTotemRequest::post('popup_action');
165 $template = new WebTotemTemplate();
166
167 if($action){
168 switch ($action) {
169 case 'reinstall_agents':
170 $build[] = [
171 'variables' => [
172 'message' => sprintf(__('Some scanning data for %s may be deleted.', 'wtotem'), WEBTOTEM_SITE_DOMAIN),
173 'action' => 'reinstall_agents',
174 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
175 ],
176 'template' => 'popup',
177 ];
178 break;
179
180 case 'logout':
181 $build[] = [
182 'variables' => [
183 'message' => __('Are you sure you want to change the API key?', 'wtotem'),
184 'action' => 'logout',
185 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
186 ],
187 'template' => 'popup',
188 ];
189 break;
190 }
191
192 wp_send_json([
193 'success' => true,
194 'content' => $template->arrayRender($build),
195 ]);
196 }
197
198 wp_send_json([
199 'success' => false,
200 ]);
201
202 }
203
204 /**
205 * Request to update charts with parameters.
206 *
207 * @return void
208 */
209 public static function chart() {
210
211 if (WebTotemRequest::post('ajax_action') !== 'chart') {
212 return;
213 }
214
215 $template = new WebTotemTemplate();
216
217 $days = (integer) WebTotemRequest::post('days');
218 $service = WebTotemRequest::post('service');
219
220 $host = WebTotemAPI::siteInfo();
221
222 switch ($service) {
223 case 'waf':
224
225 WebTotemOption::setSessionOptions(['firewall_period' => $days]);
226
227 // Firewall chart.
228 $data = WebTotemAPI::getFirewallChart($host['id'], $days);
229 $chart = WebTotem::generateWafChart($data['chart']);
230
231 $_chart[] = [
232 'variables' => [
233 'days' => $days,
234 'chart' => $chart['chart'],
235 ],
236 'template' => 'firewall_chart',
237 ];
238
239 // Firewall logs.
240 $data = WebTotemAPI::getFirewall($host['id'], 10, NULL, $days);
241 $firewall = $data['firewall'];
242
243 $waf_logs[] = [
244 'variables' => [
245 'logs' => WebTotem::wafLogs($firewall['logs']['edges']),
246 ],
247 'template' => 'firewall_logs',
248 ];
249
250 // Firewall stats.
251 $waf_stats[] = [
252 'variables' => [
253 'is_waf_training' => WebTotem::isWafTraining($data['agentManager']['createdAt']),
254 'all_attacks' => $chart['count_attacks'],
255 'blocking' => $chart['count_blocks'],
256 'not_blocking' => $chart['count_attacks'] - $chart['count_blocks'],
257 'most_attacks' => WebTotem::getMostAttacksData($firewall['map']),
258 ],
259 'template' => 'firewall_stats',
260 ];
261
262 WebTotemOption::setSessionOptions([
263 'firewall_cursor' => $firewall['logs']['pageInfo']['endCursor'],
264 ]);
265
266 $has_next_page = $firewall['logs']['pageInfo']['hasNextPage'];
267
268 $response = [
269 'chart' => $template->arrayRender($_chart),
270 'waf_logs' => $template->arrayRender($waf_logs),
271 'waf_stats' => $template->arrayRender($waf_stats),
272 'has_next_page' => $has_next_page,
273 'service' => 'waf',
274 ];
275
276 break;
277
278 case 'cpu':
279 WebTotemOption::setSessionOptions(['cpu_period' => $days]);
280
281 $data = WebTotemAPI::getServerStatusData($host['id'], $days);
282 $chart = WebTotem::generateChart($data['cpuChart'], $days);
283
284 $_chart[] = [
285 'variables' => [
286 'days' => $days,
287 'chart' => $chart,
288 ],
289 'template' => 'cpu_chart',
290 ];
291
292 $response = [
293 'chart' => $template->arrayRender($_chart),
294 'service' => 'cpu',
295 ];
296
297 break;
298
299 case 'ram':
300 WebTotemOption::setSessionOptions(['ram_period' => $days]);
301
302 $data = WebTotemAPI::getServerStatusData($host['id'], $days);
303 $chart = WebTotem::generateChart($data['ramChart'], $days);
304
305 $_chart[] = [
306 'variables' => [
307 'days' => $days,
308 'chart' => $chart,
309 ],
310 'template' => 'ram_chart',
311 ];
312
313 $response = [
314 'chart' => $template->arrayRender($_chart),
315 'service' => 'ram',
316 ];
317
318 break;
319
320 case 'map':
321 $data = WebTotemAPI::getFirewallChart($host['id'], $days);
322 $chart = WebTotem::generateAttacksMapChart($data['map']);
323 $world_map_json = WEBTOTEM_URL . '/includes/js/world_map.json';
324
325 $_chart[] = [
326 'variables' => [
327 'attacks_map' => $chart,
328 'world_map_json' => $world_map_json,
329 ],
330 'template' => 'map_chart',
331 ];
332
333 $response = [
334 'chart' => $template->arrayRender($_chart),
335 'service' => 'map',
336 ];
337
338 break;
339
340 }
341
342 if ($service) {
343 $response['success'] = true;
344 $response['notifications'] = self::notifications();
345 wp_send_json($response);
346 }
347
348 }
349
350 /**
351 * Data lazy load.
352
353 * @return void
354 */
355 public static function lazyLoad() {
356
357
358 if (WebTotemRequest::post('ajax_action') !== 'lazy_load') {
359 return;
360 }
361
362 $template = new WebTotemTemplate();
363
364 $service = WebTotemRequest::post('service');
365
366 $host = WebTotemAPI::siteInfo();
367
368 switch ($service) {
369 case 'all_sites':
370 $cursor = WebTotemOption::getSessionOption('sites_cursor') ?: NULL;
371 $allSites = WebTotemAPI::getSites($cursor);
372
373 $has_next_page = $allSites['pageInfo']['hasNextPage'];
374
375 WebTotemOption::setSessionOptions([
376 'sites_cursor' => $allSites['pageInfo']['endCursor'],
377 ]);
378
379 // Sites list.
380 $build[] = [
381 'variables' => [
382 'sites' => WebTotem::allSitesData($allSites),
383 'has_next_page' => $has_next_page,
384 ],
385 'template' => 'multisite_list'
386 ];
387
388 break;
389
390 case 'firewall':
391 $cursor = WebTotemOption::getSessionOption('firewall_cursor') ?: NULL;
392 $period = WebTotemOption::getSessionOption('firewall_period') ?: 365;
393 $data = WebTotemAPI::getFirewall($host['id'], 10, $cursor, $period);
394 $service_data = $data['firewall'];
395 $has_next_page = $service_data['logs']['pageInfo']['hasNextPage'];
396
397 WebTotemOption::setSessionOptions([
398 'firewall_cursor' => $service_data['logs']['pageInfo']['endCursor'],
399 ]);
400
401 // Firewall logs.
402 $build[] = [
403 'variables' => [
404 'logs' => WebTotem::wafLogs($service_data['logs']['edges']),
405 ],
406 'template' => 'firewall_logs',
407 ];
408
409 break;
410
411 case 'antivirus':
412 $cursor = WebTotemOption::getSessionOption('antivirus_cursor') ?: NULL;
413 $event = WebTotemOption::getSessionOption('antivirus_event') ?: NULL;
414 $permissions = WebTotemOption::getSessionOption('antivirus_permissions') ?: NULL;
415
416 $params = [
417 'host_id' => $host['id'],
418 'limit' => 10,
419 'days' => 365,
420 'cursor' => $cursor,
421 'event' => $event,
422 'permissions' => $permissions,
423 ];
424
425 $data = WebTotemAPI::getAntivirus($params);
426 $has_next_page = $data['log']['pageInfo']['hasNextPage'];
427
428 WebTotemOption::setSessionOptions([
429 'antivirus_cursor' => $data['log']['pageInfo']['endCursor'],
430 ]);
431
432 // Antivirus logs.
433 $build[] = [
434 'variables' => [
435 'logs' =>WebTotem::getAntivirusLogs($data['log']['edges']),
436 ],
437 'template' => 'antivirus_logs',
438 ];
439
440 break;
441
442 case 'reports':
443 $cursor = WebTotemOption::getSessionOption('reports_cursor') ?: NULL;
444
445 $data = WebTotemAPI::getAllReports($host['id'], 10, $cursor);
446 $has_next_page = $data['pageInfo']['hasNextPage'];
447
448 WebTotemOption::setSessionOptions([
449 'reports_cursor' => $data['pageInfo']['endCursor'],
450 ]);
451
452 // Reports.
453 $build[] = [
454 'variables' => [
455 "reports" => WebTotem::getReports($data['edges']),
456 "has_next_page" => $data['pageInfo']['hasNextPage'],
457 ],
458 'template' => 'reports_list',
459 ];
460
461 break;
462
463 case 'reports_m':
464 $cursor = WebTotemOption::getSessionOption('reports_m_cursor') ?: NULL;
465
466 $data = WebTotemAPI::getAllReports($host['id'], 10, $cursor);
467 $has_next_page = $data['pageInfo']['hasNextPage'];
468
469 WebTotemOption::setSessionOptions([
470 'reports_m_cursor' => $data['pageInfo']['endCursor'],
471 ]);
472
473 // Reports mobile.
474 $build[] = [
475 'variables' => [
476 "reports" => WebTotem::getReports($data['edges']),
477 "has_next_page" => $data['pageInfo']['hasNextPage'],
478 ],
479 'template' => 'reports_list_mobile',
480 ];
481
482 break;
483 }
484
485 if ($service) {
486
487 wp_send_json([
488 'success' => true,
489 'content' => $template->arrayRender($build),
490 'has_next_page' => $has_next_page,
491 'notifications' => self::notifications(),
492 ]);
493 }
494 }
495
496
497 /**
498 * Add date filter.
499 *
500 * @return void
501 */
502 public static function wafDateFilter() {
503
504 if (WebTotemRequest::post('ajax_action') !== 'waf_date_filter') {
505 return;
506 }
507
508 $template = new WebTotemTemplate();
509
510 $date_from = WebTotemRequest::post('date_from');
511
512 $period = explode(" to ", $date_from);
513 WebTotemOption::setSessionOptions(['firewall_period' => $period]);
514
515 $host = WebTotemAPI::siteInfo();
516
517 // Firewall logs.
518 $data = WebTotemAPI::getFirewall($host['id'], 10, NULL, $period);
519 $firewall = $data['firewall'];
520
521 $waf_logs[] = [
522 'variables' => [
523 'logs' => WebTotem::wafLogs($firewall['logs']['edges']),
524 ],
525 'template' => 'firewall_logs',
526 ];
527
528 // Firewall chart.
529 $data = WebTotemAPI::getFirewallChart($host['id'], $period);
530 $chart = WebTotem::generateWafChart($data['chart']);
531
532 $_chart[] = [
533 'variables' => [
534 'days' => $chart['days'],
535 'chart' => $chart['chart'],
536 ],
537 'template' => 'firewall_chart',
538 ];
539
540 // Firewall stats.
541 $waf_stats[] = [
542 'variables' => [
543 'is_waf_training' => WebTotem::isWafTraining($data['agentManager']['createdAt']),
544 'all_attacks' => $chart['count_attacks'],
545 'blocking' => $chart['count_blocks'],
546 'not_blocking' => $chart['count_attacks'] - $chart['count_blocks'],
547 'most_attacks' => WebTotem::getMostAttacksData($firewall['map']),
548 ],
549 'template' => 'firewall_stats',
550 ];
551
552 WebTotemOption::setSessionOptions([
553 'firewall_cursor' => $firewall['logs']['pageInfo']['endCursor'],
554 ]);
555
556 $has_next_page = $firewall['logs']['pageInfo']['hasNextPage'];
557
558 $response = [
559 'success' => true,
560 'chart' => $template->arrayRender($_chart),
561 'waf_logs' => $template->arrayRender($waf_logs),
562 'waf_stats' => $template->arrayRender($waf_stats),
563 'has_next_page' => $has_next_page,
564 'notifications' => self::notifications(),
565 ];
566
567 wp_send_json($response);
568 }
569
570
571 /**
572 * Request to restart re-scan and receive antivirus data.
573 *
574 * @return void
575 */
576 public static function antivirus() {
577
578 if (WebTotemRequest::post('ajax_action') !== 'antivirus') {
579 return;
580 }
581
582 $action = WebTotemRequest::post('av_action');
583
584 $host = WebTotemAPI::siteInfo();
585
586 switch ($action) {
587 case 'rescan':
588 $response = WebTotemAPI::forceCheck($host['id'], 'av');
589
590 if (!isset($response['errors'])) {
591 $data = WebTotemAPI::getAntivirusLastTest($host['id']);
592 $response['last_scan'] = WebTotem::dateFormatter($data['lastTest']['time']);
593
594 }
595 break;
596
597 case 'download_report':
598 $response = WebTotemAPI::avExport($host['id']);
599 if (!isset($response['errors'])) {
600 $response['doc_link'] = $response['data']['auth']['sites']['av']['export'];
601 }
602 break;
603
604 case 'filter':
605
606 $file_status = WebTotemRequest::post('file_status');
607 $permission = filter_var( WebTotemRequest::post('permission'), FILTER_VALIDATE_BOOLEAN);
608
609 WebTotemOption::setSessionOptions([
610 'antivirus_permissions' => $permission,
611 'antivirus_event' => $file_status,
612 ]);
613
614 $params = [
615 'host_id' => $host['id'],
616 'limit' => 10,
617 'days' => 365,
618 'cursor' => NULL,
619 'event' => $file_status,
620 'permissions' => $permission,
621 ];
622
623 $data = WebTotemAPI::getAntivirus($params);
624 $has_next_page = $data['log']['pageInfo']['hasNextPage'];
625
626 WebTotemOption::setSessionOptions([
627 'antivirus_cursor' => $data['log']['pageInfo']['endCursor'],
628 ]);
629
630 // Antivirus logs.
631 $build[] = [
632 'variables' => [
633 'logs' =>WebTotem::getAntivirusLogs($data['log']['edges']),
634 ],
635 'template' => 'antivirus_logs',
636 ];
637
638 $template = new WebTotemTemplate();
639 $response = [
640 'logs' => $template->arrayRender($build),
641 'has_next_page' => $has_next_page,
642 ];
643
644 break;
645 }
646
647 $response['success'] = true;
648 $response['notifications'] = self::notifications();
649
650 wp_send_json($response);
651 }
652
653 /**
654 * Request to add a file to quarantine.
655 *
656 * @return void
657 */
658 public static function quarantine() {
659 if (WebTotemRequest::post('ajax_action') !== 'quarantine') {
660 return;
661 }
662
663 $action = WebTotemRequest::post('quarantine_action');
664 $id_or_path = WebTotemRequest::post('id_or_path');
665
666 $host = WebTotemAPI::siteInfo();
667 $response = [];
668
669 switch ($action) {
670 case 'add':
671 $api_response = WebTotemAPI::moveToQuarantine($host['id'], $id_or_path);
672 break;
673
674 case 'remove':
675 $api_response = WebTotemAPI::moveFromQuarantine($id_or_path);
676 break;
677 }
678
679 if (!isset($api_response['errors'])) {
680
681 $quarantine_logs = WebTotemAPI::getQuarantineList($host['id']);
682 $quarantine_count = count($quarantine_logs);
683
684 // Quarantine logs.
685 $quarantine[] = [
686 'variables' => [
687 "logs" => WebTotem::getQuarantineLogs($quarantine_logs),
688 "count" => $quarantine_count,
689 ],
690 'template' => 'quarantine',
691 ];
692
693
694 $cursor = WebTotemOption::getSessionOption('antivirus_cursor') ?: NULL;
695 $event = WebTotemOption::getSessionOption('antivirus_event') ?: NULL;
696 $permissions = WebTotemOption::getSessionOption('antivirus_permissions') ?: NULL;
697
698 $params = [
699 'host_id' => $host['id'],
700 'limit' => 10,
701 'days' => 365,
702 'cursor' => $cursor,
703 'event' => $event,
704 'permissions' => $permissions,
705 ];
706
707 $data = WebTotemAPI::getAntivirus($params);
708 $has_next_page = $data['log']['pageInfo']['hasNextPage'];
709
710 WebTotemOption::setSessionOptions([
711 'antivirus_cursor' => $data['log']['pageInfo']['endCursor'],
712 ]);
713
714 // Antivirus logs.
715 $antivirus_logs[] = [
716 'variables' => [
717 'logs' =>WebTotem::getAntivirusLogs($data['log']['edges']),
718 ],
719 'template' => 'antivirus_logs',
720 ];
721
722
723 $template = new WebTotemTemplate();
724 $response = [
725 'antivirus_logs' => $template->arrayRender($antivirus_logs),
726 'quarantine' => $template->arrayRender($quarantine),
727 'has_next_page' => $has_next_page,
728 ];
729
730 }
731
732 $response['success'] = true;
733 $response['notifications'] = self::notifications();
734
735 wp_send_json($response);
736
737 }
738
739 /**
740 * Request to add or remove a port to the ignore list.
741 *
742 * @return void
743 */
744 public static function ignorePorts() {
745
746 if (WebTotemRequest::post('ajax_action') !== 'ignore_ports') {
747 return;
748 }
749
750 $template = new WebTotemTemplate();
751
752 $action = WebTotemRequest::post('port_action');
753 $port = (int) WebTotemRequest::post('port');
754
755 $host = WebTotemAPI::siteInfo();
756
757 switch ($action) {
758 case 'add':
759 $response = WebTotemAPI::addIgnorePort($host['id'], $port);
760 break;
761
762 case 'remove':
763 $response = WebTotemAPI::removeIgnorePort($host['id'], $port);
764 break;
765 }
766
767 if (!isset($response['errors'])) {
768
769 $ports = WebTotemAPI::getAllPortsList($host['id']);
770 $open_ports[] = [
771 'variables' => [
772 "ports" => $ports,
773 ],
774 'template' => 'open_ports',
775 ];
776
777 $ignore_ports[] = [
778 'variables' => [
779 "ports" => $ports,
780 ],
781 'template' => 'ignore_ports',
782 ];
783 $response = [
784 'open_ports' => $template->arrayRender($open_ports),
785 'ignore_ports' => $template->arrayRender($ignore_ports),
786 'notifications' => self::notifications(),
787 ];
788
789 }
790
791 $response['success'] = true;
792 $response['notifications'] = self::notifications();
793
794 wp_send_json($response);
795 }
796
797 /**
798 * Request for a report link.
799 *
800 * @return void
801 */
802 public static function reports() {
803
804 if (WebTotemRequest::post('ajax_action') !== 'reports') {
805 return;
806 }
807
808 $template = new WebTotemTemplate();
809
810 $action = WebTotemRequest::post('report_action');
811
812 switch ($action) {
813 case 'download':
814 $id = WebTotemRequest::post('id');
815 $link = WebTotemAPI::downloadReport($id);
816 if ($link) {
817 $response['link'] = $link;
818 }
819 break;
820 case 'report_form':
821
822 $period = explode(" to ", WebTotemRequest::post('date_period'));
823 $modules_data = WebTotemRequest::post('modules');
824
825 $modules = [
826 'wa' => 'false',
827 'dc' => 'false',
828 'ps' => 'false',
829 'rc' => 'false',
830 'sc' => 'false',
831 'av' => 'false',
832 'waf' => 'false'
833 ];
834
835 foreach ($modules_data as $module => $value){
836 $modules[$module] = 'true';
837 }
838
839 $host = WebTotemAPI::siteInfo();
840 $api_response = WebTotemAPI::generateReport($host['id'], $period, $modules);
841
842 if (!$api_response) {
843 $massage = '<div class="message error_message">' . __('Report generation error', 'wtotem') . '</div>';
844 }
845 else {
846 $data = WebTotemAPI::getAllReports($host['id']);
847
848 // Reports.
849 $build[] = [
850 'variables' => [
851 "reports" => WebTotem::getReports($data['edges']),
852 "has_next_page" => $data['pageInfo']['hasNextPage'],
853 ],
854 'template' => 'reports_list',
855 ];
856
857 // Reports mobile.
858 $build_mobile[] = [
859 'variables' => [
860 "reports" => WebTotem::getReports($data['edges']),
861 "has_next_page" => $data['pageInfo']['hasNextPage'],
862 ],
863 'template' => 'reports_list_mobile',
864 ];
865
866 $response = [
867 'reports' => $template->arrayRender($build),
868 'reports_m' => $template->arrayRender($build_mobile),
869 'link' => $api_response,
870 ];
871
872 $massage = '<div class="message success_message">' . __('The report was successfully generated', 'wtotem') . '</div>';
873 }
874
875 $response['massage'] = $massage;
876
877 break;
878 }
879
880 $response['success'] = true;
881 $response['notifications'] = self::notifications();
882 wp_send_json($response);
883 }
884
885 /**
886 * Request for a report link.
887 *
888 * @return void
889 */
890 public static function settings() {
891
892 if (WebTotemRequest::post('ajax_action') !== 'settings') {
893 return;
894 }
895
896 $av_installed = WebTotemOption::getOption('av_installed');
897 $waf_installed = WebTotemOption::getOption('waf_installed');
898
899 if(!$av_installed && !$waf_installed) {
900 WebTotemOption::setNotification('warning', __('It is not possible to make changes because the agents are not installed.', 'wtotem'));
901
902 wp_send_json([
903 'success' => false,
904 'notifications' => self::notifications()
905 ]);
906 }
907
908 $action = WebTotemRequest::post('settings_action');
909 $host = WebTotemAPI::siteInfo();
910 $template = new WebTotemTemplate();
911
912 switch ($action) {
913
914 case 'module_toggle':
915 $config = WebTotemAPI::toggleConfigs(WebTotemRequest::post('value'));
916 $response['isActive'] = $config['isActive'];
917 break;
918
919 case 'module_notifications':
920 $config = WebTotemAPI::toggleNotifications($host['id'], WebTotemRequest::post('value'));
921 $response['isActive'] = $config;
922 break;
923
924 case 'waf_settings':
925
926 $settings = [
927 'gdn' => WebTotemRequest::post('gdn'),
928 'dosProtection' => WebTotemRequest::post('dos'),
929 'dosLimit' => WebTotemRequest::post('dos_limit'),
930 'loginAttemptsProtection' => WebTotemRequest::post('login_attempt'),
931 'loginAttemptsLimit' => WebTotemRequest::post('login_attempt_limit'),
932 ];
933
934 $host = WebTotemAPI::siteInfo();
935 $api_response = WebTotemAPI::setFirewallSettings($host['id'], $settings);
936
937 if (!$api_response['errors']) {
938 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
939 }
940
941 break;
942
943 case 'add_allow_ip':
944 $api_response = WebTotemAPI::addIpToList($host['id'], WebTotemRequest::post('value'), 'white');
945 if ($api_response) {
946 $data = WebTotemAPI::getIpLists($host['id']);
947 $build[] = [
948 'variables' => [
949 "list" => WebTotem::getIpList($data['whiteList'], 'ip_allow'),
950 ],
951 'template' => 'allow_deny_list',
952 ];
953
954 $response['content'] = $template->arrayRender($build);
955 }
956 break;
957
958 case 'add_deny_ip':
959 $api_response = WebTotemAPI::addIpToList($host['id'], WebTotemRequest::post('value'), 'black');
960 if ($api_response) {
961 $data = WebTotemAPI::getIpLists($host['id']);
962 $build[] = [
963 'variables' => [
964 "list" => WebTotem::getIpList($data['blackList'], 'ip_deny'),
965 ],
966 'template' => 'allow_deny_list',
967 ];
968
969 $response['content'] = $template->arrayRender($build);
970 }
971 break;
972
973 case 'add_allow_url':
974 $api_response = WebTotemAPI::addUrlToAllowList($host['id'], WebTotemRequest::post('value'));
975 if ($api_response) {
976 $data = WebTotemAPI::getAllowUrlList($host['id']);
977 $build[] = [
978 'variables' => [
979 "list" => WebTotem::getUrlAllowList($data),
980 ],
981 'template' => 'allow_url_list',
982 ];
983
984 $response['content'] = $template->arrayRender($build);
985 }
986 break;
987
988 case 'add_ip_list':
989 $ips = WebTotemRequest::post('ips');
990 $list_name = WebTotemRequest::post('list');
991
992 $host = WebTotemAPI::siteInfo();
993 $api_response = WebTotemAPI::addIpToList($host['id'], $ips, $list_name);
994
995 if ($api_response) {
996 $data = WebTotemAPI::getIpLists($host['id']);
997
998 $data_list = ($list_name == 'white') ? $data['whiteList'] : $data['blackList'];
999 $ip_list = ($list_name == 'white') ? 'ip_allow' : 'ip_deny';
1000
1001 $build[] = [
1002 'variables' => [
1003 "list" => WebTotem::getIpList($data_list, $ip_list),
1004 ],
1005 'template' => 'allow_deny_list',
1006 ];
1007
1008 if ($api_response['status'] != 0) {
1009 $response['invalidIPs'] = implode("\n", $api_response['invalidIPs']);
1010 }
1011
1012 $response['wrap'] = ($list_name == 'white') ? '#wtotem_ip_allow_list' : '#wtotem_ip_deny_list';
1013 $response['content'] = $template->arrayRender($build);
1014 }
1015 break;
1016 }
1017
1018 $response['success'] = true;
1019 $response['notifications'] = self::notifications();
1020 wp_send_json($response);
1021 }
1022
1023 /**
1024 * Request to remove from the list of deny/allowed ip or url addresses.
1025 *
1026 * @return void
1027 */
1028 public static function remove() {
1029
1030 if (WebTotemRequest::post('ajax_action') !== 'remove') {
1031 return;
1032 }
1033
1034 $av_installed = WebTotemOption::getOption('av_installed');
1035 $waf_installed = WebTotemOption::getOption('waf_installed');
1036
1037 if(!$av_installed && !$waf_installed) {
1038 WebTotemOption::setNotification('warning', __('It is not possible to make changes because the agents are not installed.', 'wtotem'));
1039
1040 wp_send_json([
1041 'success' => false,
1042 'notifications' => self::notifications()
1043 ]);
1044 }
1045
1046 $action = WebTotemRequest::post('remove_action');
1047 $host = WebTotemAPI::siteInfo();
1048 $template = new WebTotemTemplate();
1049
1050 switch ($action) {
1051 case 'ip_allow':
1052 $api_response = WebTotemAPI::removeIpFromList( WebTotemRequest::post('id') );
1053
1054 if ($api_response) {
1055 $data = WebTotemAPI::getIpLists($host['id']);
1056
1057 $build[] = [
1058 'variables' => [
1059 "list" => WebTotem::getIpList($data['whiteList'], 'ip_allow'),
1060 ],
1061 'template' => 'allow_deny_list',
1062 ];
1063
1064 $response['content'] = $template->arrayRender($build);
1065 $response['wrap'] = '#wtotem_ip_allow_list';
1066 }
1067 break;
1068
1069 case 'ip_deny':
1070 $api_response = WebTotemAPI::removeIpFromList( WebTotemRequest::post('id') );
1071
1072 if ($api_response) {
1073 $data = WebTotemAPI::getIpLists($host['id']);
1074
1075 $build[] = [
1076 'variables' => [
1077 "list" => WebTotem::getIpList($data['blackList'], 'ip_deny'),
1078 ],
1079 'template' => 'allow_deny_list',
1080 ];
1081
1082 $response['content'] = $template->arrayRender($build);
1083 $response['wrap'] = '#wtotem_ip_deny_list';
1084 }
1085 break;
1086
1087 case 'url_allow':
1088 $api_response = WebTotemAPI::removeUrlFromAllowList( WebTotemRequest::post('id') );
1089
1090 if ($api_response) {
1091 $data = WebTotemAPI::getAllowUrlList($host['id']);
1092
1093 $build[] = [
1094 'variables' => [
1095 "list" => WebTotem::getUrlAllowList($data),
1096 ],
1097 'template' => 'allow_url_list',
1098 ];
1099
1100 $response['content'] = $template->arrayRender($build);
1101 $response['wrap'] = '#wtotem_allow_url';
1102 }
1103 break;
1104 }
1105
1106 $response['success'] = true;
1107 $response['notifications'] = self::notifications();
1108 wp_send_json($response);
1109 }
1110
1111 /**
1112 * Request to remove site from WebTotem.
1113 *
1114 * @return void
1115 */
1116 public static function multisite() {
1117
1118 if (WebTotemRequest::post('ajax_action') !== 'multisite') {
1119 return;
1120 }
1121
1122 $action = WebTotemRequest::post('multisite_action');
1123 $template = new WebTotemTemplate();
1124
1125 switch ($action) {
1126 case 'remove_site':
1127
1128 $host_id = WebTotemRequest::post('hid');
1129 $main_host = WebTotemOption::getMainHost();
1130
1131 if($host_id == $main_host['id']){
1132 WebTotemOption::setNotification('error', __('You cannot delete the primary domain.', 'wtotem'));
1133 break;
1134 }
1135 WebTotemAPI::removeMultiSiteHost($host_id);
1136
1137 break;
1138
1139 case 'add_site':
1140
1141 $new_site = WebTotemRequest::post('site_name');
1142 WebTotemAPI::addMultiSiteNewSites([$new_site]);
1143
1144 break;
1145 }
1146
1147 $allSites = WebTotemAPI::getSites();
1148 $has_next_page = $allSites['pageInfo']['hasNextPage'];
1149
1150 WebTotemOption::setSessionOptions([
1151 'sites_cursor' => $allSites['pageInfo']['endCursor'],
1152 ]);
1153
1154 // Sites list.
1155 $build[] = [
1156 'variables' => [
1157 'sites' => WebTotem::allSitesData($allSites),
1158 'has_next_page' => $has_next_page,
1159 ],
1160 'template' => 'multisite_list'
1161 ];
1162
1163 $response['content'] = $template->arrayRender($build);
1164
1165 $response['success'] = true;
1166 $response['notifications'] = self::notifications();
1167 wp_send_json($response);
1168 }
1169
1170 /**
1171 * Changing the theme mode.
1172 *
1173 * @return void
1174 */
1175 public static function changeThemeMode() {
1176
1177 if (WebTotemRequest::post('ajax_action') !== 'theme_mode') {
1178 return;
1179 }
1180
1181 $theme_mode = WebTotemOption::getSessionOption('theme_mode');
1182
1183 if ($theme_mode == 'dark') {
1184 WebTotemOption::setSessionOptions(['theme_mode' => 'light']);
1185 $response = 'light';
1186 }
1187 else {
1188 WebTotemOption::setSessionOptions(['theme_mode' => 'dark']);
1189 $response = 'dark';
1190 }
1191
1192 wp_send_json($response);
1193 }
1194
1195 /**
1196 * Set user time zone offset.
1197 *
1198 * @return void
1199 */
1200 public static function userTimeZone() {
1201
1202 if (WebTotemRequest::post('ajax_action') !== 'set_time_zone') {
1203 return;
1204 }
1205
1206 $time_zone_offset = WebTotemRequest::post('offset');
1207 $now = strtotime('now');
1208 $check = WebTotemOption::getOption('time_zone_check') ?: 0;
1209
1210 // Checking whether an hour has elapsed since the previous request.
1211 if ($now >= $check) {
1212 $time_zone = WebTotemAPI::getTimeZone();
1213 if ($time_zone) {
1214 $time_zone_offset = timezone_offset_get(new \DateTimeZone($time_zone), new \DateTime('now', new \DateTimeZone('Europe/London'))) / 3600;
1215 WebTotemOption::setOptions(['time_zone_check' => $now + 3600]);
1216 }
1217 WebTotemOption::setOptions(['time_zone_offset' => $time_zone_offset]);
1218 }
1219
1220 wp_send_json([
1221 'success' => true,
1222 'time_zone_offset' => $time_zone_offset
1223 ]);
1224
1225 }
1226
1227 /**
1228 * Updating the page data in the specified time interval.
1229 *
1230 * @return void
1231 */
1232 public static function reloadPage() {
1233
1234 if (WebTotemRequest::post('ajax_action') !== 'reload_page') {
1235 return;
1236 }
1237
1238 $page = WebTotemRequest::post('page');
1239
1240 $template = new WebTotemTemplate();
1241
1242 // Get data from WebTotem API.
1243 $host = WebTotemAPI::siteInfo();
1244
1245 switch ($page) {
1246 case 'dashboard':
1247
1248 $data = WebTotemAPI::getAllData($host['id']);
1249
1250 // Start build array for rendering.
1251 // Scoring block.
1252 $service_data = $data['scoring']['result'];
1253 $total_score = round($data['scoring']['score']);
1254 $score_grading = WebTotem::scoreGrading($total_score);
1255 $build['scoring'] = [
1256 'variables' => [
1257 "host_id" => $host['id'],
1258 "total_score" => $total_score . "%",
1259 "tested_on" => WebTotem::dateFormatter($data['scoring']['lastTest']['time']),
1260 "server_ip" => $service_data['ip'] ?: ' - ',
1261 "location" => WebTotem::getCountryName($service_data['country']) ?: ' - ',
1262 "is_higher_than" => $service_data['isHigherThan'] . '%',
1263 "grade" => $score_grading['grade'],
1264 "color" => $score_grading['color'],
1265 ],
1266 'template' => 'score',
1267 ];
1268
1269 // Firewall stats.
1270 $period = WebTotemOption::getSessionOption('firewall_period');
1271 $service_data = $period ? WebTotemAPI::getFirewall($host['id'], 10, NULL, $period) : $data;
1272 $service_data = $service_data['firewall'];
1273
1274 $chart = WebTotem::generateWafChart($service_data['chart']);
1275 $build['firewall_stats'] = [
1276 'variables' => [
1277 "is_waf_training" => $data['agentManager'] && WebTotem::isWafTraining( $data['agentManager']['createdAt'] ),
1278 "most_attacks" => WebTotem::getMostAttacksData($service_data['map']),
1279 "all_attacks" => $chart['count_attacks'],
1280 "blocking" => $chart['count_blocks'],
1281 "not_blocking" => (int) $chart['count_attacks'] - (int) $chart['count_blocks'],
1282 ],
1283 'template' => 'firewall_stats',
1284 ];
1285
1286 $build['chart_periods'] = [
1287 'variables' => [
1288 "service" => 'waf',
1289 "days" => is_array($period) ? 7 : $period,
1290 ],
1291 'template' => 'chart_periods',
1292 ];
1293
1294 // Firewall blocks.
1295 $build['firewall_data'] = [
1296 'variables' => [
1297 "chart" => $chart['chart'],
1298 "days" => $chart['days'],
1299 "logs" => WebTotem::wafLogs($service_data['logs']['edges']),
1300 ],
1301 'template' => 'firewall',
1302 ];
1303
1304 // Server Status RAM.
1305 $period = WebTotemOption::getSessionOption('ram_period') ?: 7;
1306 $service_data = $period ? WebTotemAPI::getServerStatusData($host['id'], $period) : $data['serverStatus'];
1307
1308 $build['server_status_ram'] = [
1309 'variables' => [
1310 "info" => $service_data['info'],
1311 "ram_chart" => WebTotem::generateChart($service_data['ramChart']),
1312 "days" => $period,
1313 ],
1314 'template' => 'server_status_ram',
1315 ];
1316
1317 // Server Status CPU.
1318 $period = WebTotemOption::getSessionOption('cpu_period') ?: 7;
1319 $service_data = $period ? WebTotemAPI::getServerStatusData($host['id'], $period) : $data['serverStatus'];
1320 $build['server_status_cpu'] = [
1321 'variables' => [
1322 "cpu_chart" => WebTotem::generateChart($service_data['cpuChart']),
1323 "days" => $period,
1324 ],
1325
1326 'template' => 'server_status_cpu',
1327 ];
1328
1329 // Antivirus stats blocks.
1330 $antivirus_stats = $data['antivirus']['stats'];
1331 $build['antivirus_stats'] = [
1332 'variables' => [
1333 "changes" => $antivirus_stats['changed'] ?: 0,
1334 "scanned" => $antivirus_stats['scanned'] ?: 0,
1335 "deleted" => $antivirus_stats['deleted'] ?: 0,
1336 "infected" => $antivirus_stats["infected"] ?: 0,
1337 ],
1338
1339 'template' => 'antivirus_stats',
1340 ];
1341
1342 // Monitoring blocks.
1343 $build['monitoring'] = [
1344 'variables' => [
1345 "ssl" => [
1346 'status' => WebTotem::getStatusData($data['ssl']['status']),
1347 'days_left' => WebTotem::daysLeft($data['ssl']['expiryDate']),
1348 'issue_date' => WebTotem::dateFormatter($data['ssl']['issueDate']),
1349 'expiry_date' => WebTotem::dateFormatter($data['ssl']['expiryDate']),
1350 ],
1351 "availability" => [
1352 'status' => WebTotem::getStatusData($data['availability']['status']),
1353 "percent" => $data['availability']['percent'],
1354 "response_time" => ceil($data['availability']['responseTime'] / 1000000) . ' ' . __('ms.', 'wtotem'),
1355 "downtime" => ceil($data['availability']['downTime'] / 1000000) . ' ' . __('ms.', 'wtotem'),
1356 "last_test" => WebTotem::dateFormatter($data['availability']['lastTest']['time']),
1357 ],
1358 'reputation' => [
1359 "status" => WebTotem::getStatusData($data['reputation']['status']),
1360 "blacklists_entries" => WebTotem::blacklistsEntries(
1361 $data['reputation']['status'],
1362 $data['reputation']['virusList']),
1363 "info" => WebTotem::getReputationInfo($data['reputation']['status']),
1364 "last_test" => WebTotem::dateFormatter($data['reputation']['lastTest']['time']),
1365 ],
1366 ],
1367 'template' => 'monitoring',
1368 ];
1369
1370
1371 // Scanning blocks.
1372 $disc_usage_data = $data['serverStatus']['discUsage'];
1373 $disc_usage = [
1374 'total' => $disc_usage_data['total'],
1375 'free' => $disc_usage_data['free'],
1376 'used' => $disc_usage_data['total'] - $disc_usage_data['free'],
1377 ];
1378
1379 $build['scanning'] = [
1380 'variables' => [
1381 "ports" => [
1382 'status' => WebTotem::getStatusData($data['ports']['status']),
1383 "ip" => $data['ports']['ip'],
1384 "number" => count($data['ports']['tcp']),
1385 "tcp" => $data['ports']['tcp'],
1386 "ignore_ports" => $data['ports']['ignorePorts'],
1387 "last_test" => WebTotem::dateFormatter($data['ports']['lastTest']['time']),
1388 ],
1389 "deface" => [
1390 'status' => WebTotem::getStatusData($data['deface']['status']),
1391 "number" => $data['deface']['count'],
1392 "words" => !empty($data['deface']['words']) ? implode(",", $data['deface']['words']) : '',
1393 "last_test" => WebTotem::dateFormatter($data['deface']['lastTest']['time']),
1394 ],
1395 "disc_usage" => $disc_usage,
1396 "disc_chart" => json_encode($disc_usage),
1397 ],
1398 'template' => 'scanning',
1399 ];
1400
1401 $response['content'][] = ['selector' => '#scoring', 'content' => $template->arrayRender($build['scoring'])];
1402 $response['content'][] = ['selector' => '#firewall_stats', 'content' => $template->arrayRender($build['firewall_stats'])];
1403 $response['content'][] = ['selector' => '#waf_chart_period', 'content' => $template->arrayRender($build['chart_periods'])];
1404 $response['content'][] = ['selector' => '#firewall_data', 'content' => $template->arrayRender($build['firewall_data'])];
1405 $response['content'][] = ['selector' => '#server_status_cpu', 'content' => $template->arrayRender($build['server_status_cpu'])];
1406 $response['content'][] = ['selector' => '#server_status_ram', 'content' => $template->arrayRender($build['server_status_ram'])];
1407 $response['content'][] = ['selector' => '#antivirus_stats', 'content' => $template->arrayRender($build['antivirus_stats'])];
1408 $response['content'][] = ['selector' => '#monitoring', 'content' => $template->arrayRender($build['monitoring'])];
1409 $response['content'][] = ['selector' => '#scanning', 'content' => $template->arrayRender($build['scanning'])];
1410
1411 break;
1412 }
1413
1414 $response['success'] = true;
1415 $response['notifications'] = self::notifications();
1416 wp_send_json($response);
1417 }
1418
1419 /**
1420 * Notification output.
1421 *
1422 * @return string
1423 */
1424 public static function notifications() {
1425
1426 $notifications = WebTotem::getNotifications();
1427
1428 if($notifications){
1429 $build[] = [
1430 'variables' => [
1431 'notifications' => $notifications,
1432 ],
1433
1434 'template' => 'notifications',
1435 ];
1436
1437 $template = new WebTotemTemplate();
1438 return $template->arrayRender($build);
1439 }
1440 return false;
1441
1442
1443 }
1444
1445 }
1446