PluginProbe
WebTotem Security / 2.4.2
WebTotem Security v2.4.2
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.2, at lib/Ajax.php

1,465 lines 38.9 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 $response['notifications'] = self::notifications();
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 wp_send_json([
35 'link' => $link,
36 'success' => true,
37 'user' => WebTotemAPI::getEmail(),
38 ], 200);
39 } else {
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 $allSites = WebTotemAPI::getSites();
1138 $has_next_page = $allSites['pageInfo']['hasNextPage'];
1139
1140 WebTotemOption::setSessionOptions([
1141 'sites_cursor' => $allSites['pageInfo']['endCursor'],
1142 ]);
1143
1144 // Sites list.
1145 $build[] = [
1146 'variables' => [
1147 'sites' => WebTotem::allSitesData($allSites),
1148 'has_next_page' => $has_next_page,
1149 ],
1150 'template' => 'multisite_list'
1151 ];
1152
1153 $response['content'] = $template->arrayRender($build);
1154
1155 break;
1156
1157 case 'add_site':
1158
1159 $new_site = WebTotemRequest::post('site_name');
1160 WebTotemAPI::addMultiSiteNewSites([$new_site]);
1161
1162 $allSites = WebTotemAPI::getSites();
1163 $has_next_page = $allSites['pageInfo']['hasNextPage'];
1164
1165 WebTotemOption::setSessionOptions([
1166 'sites_cursor' => $allSites['pageInfo']['endCursor'],
1167 ]);
1168
1169 // Sites list.
1170 $build[] = [
1171 'variables' => [
1172 'sites' => WebTotem::allSitesData($allSites),
1173 'has_next_page' => $has_next_page,
1174 ],
1175 'template' => 'multisite_list'
1176 ];
1177
1178 $response['content'] = $template->arrayRender($build);
1179
1180 break;
1181
1182 }
1183
1184 $response['success'] = true;
1185 $response['notifications'] = self::notifications();
1186 wp_send_json($response);
1187 }
1188
1189 /**
1190 * Changing the theme mode.
1191 *
1192 * @return void
1193 */
1194 public static function changeThemeMode() {
1195
1196 if (WebTotemRequest::post('ajax_action') !== 'theme_mode') {
1197 return;
1198 }
1199
1200 $theme_mode = WebTotemOption::getSessionOption('theme_mode');
1201
1202 if ($theme_mode == 'dark') {
1203 WebTotemOption::setSessionOptions(['theme_mode' => 'light']);
1204 $response = 'light';
1205 }
1206 else {
1207 WebTotemOption::setSessionOptions(['theme_mode' => 'dark']);
1208 $response = 'dark';
1209 }
1210
1211 wp_send_json($response);
1212 }
1213
1214 /**
1215 * Set user time zone offset.
1216 *
1217 * @return void
1218 */
1219 public static function userTimeZone() {
1220
1221 if (WebTotemRequest::post('ajax_action') !== 'set_time_zone') {
1222 return;
1223 }
1224
1225 $time_zone_offset = WebTotemRequest::post('offset');
1226 $now = strtotime('now');
1227 $check = WebTotemOption::getOption('time_zone_check') ?: 0;
1228
1229 // Checking whether an hour has elapsed since the previous request.
1230 if ($now >= $check) {
1231 $time_zone = WebTotemAPI::getTimeZone();
1232 if ($time_zone) {
1233 $time_zone_offset = timezone_offset_get(new \DateTimeZone($time_zone), new \DateTime('now', new \DateTimeZone('Europe/London'))) / 3600;
1234 WebTotemOption::setOptions(['time_zone_check' => $now + 3600]);
1235 }
1236 WebTotemOption::setOptions(['time_zone_offset' => $time_zone_offset]);
1237 }
1238
1239 wp_send_json([
1240 'success' => true,
1241 'time_zone_offset' => $time_zone_offset
1242 ]);
1243
1244 }
1245
1246 /**
1247 * Updating the page data in the specified time interval.
1248 *
1249 * @return void
1250 */
1251 public static function reloadPage() {
1252
1253 if (WebTotemRequest::post('ajax_action') !== 'reload_page') {
1254 return;
1255 }
1256
1257 $page = WebTotemRequest::post('page');
1258
1259 $template = new WebTotemTemplate();
1260
1261 // Get data from WebTotem API.
1262 $host = WebTotemAPI::siteInfo();
1263
1264 switch ($page) {
1265 case 'dashboard':
1266
1267 $data = WebTotemAPI::getAllData($host['id']);
1268
1269 // Start build array for rendering.
1270 // Scoring block.
1271 $service_data = $data['scoring']['result'];
1272 $total_score = round($data['scoring']['score']);
1273 $score_grading = WebTotem::scoreGrading($total_score);
1274 $build['scoring'] = [
1275 'variables' => [
1276 "host_id" => $host['id'],
1277 "total_score" => $total_score . "%",
1278 "tested_on" => $data['scoring']['lastTest']['time'],
1279 "server_ip" => $service_data['ip'] ?: ' - ',
1280 "location" => WebTotem::getCountryName($service_data['country']) ?: ' - ',
1281 "is_higher_than" => $service_data['isHigherThan'] . '%',
1282 "grade" => $score_grading['grade'],
1283 "color" => $score_grading['color'],
1284 ],
1285 'template' => 'score',
1286 ];
1287
1288 // Firewall stats.
1289 $period = WebTotemOption::getSessionOption('firewall_period');
1290 $service_data = $period ? WebTotemAPI::getFirewall($host['id'], 10, NULL, $period) : $data;
1291 $service_data = $service_data['firewall'];
1292
1293 $chart = WebTotem::generateWafChart($service_data['chart']);
1294 $build['firewall_stats'] = [
1295 'variables' => [
1296 "is_waf_training" => $data['agentManager'] && WebTotem::isWafTraining( $data['agentManager']['createdAt'] ),
1297 "most_attacks" => WebTotem::getMostAttacksData($service_data['map']),
1298 "all_attacks" => $chart['count_attacks'],
1299 "blocking" => $chart['count_blocks'],
1300 "not_blocking" => (int) $chart['count_attacks'] - (int) $chart['count_blocks'],
1301 ],
1302 'template' => 'firewall_stats',
1303 ];
1304
1305 $build['chart_periods'] = [
1306 'variables' => [
1307 "service" => 'waf',
1308 "days" => is_array($period) ? 7 : $period,
1309 ],
1310 'template' => 'chart_periods',
1311 ];
1312
1313 // Firewall blocks.
1314 $build['firewall_data'] = [
1315 'variables' => [
1316 "chart" => $chart['chart'],
1317 "days" => $chart['days'],
1318 "logs" => WebTotem::wafLogs($service_data['logs']['edges']),
1319 ],
1320 'template' => 'firewall',
1321 ];
1322
1323 // Server Status RAM.
1324 $period = WebTotemOption::getSessionOption('ram_period') ?: 7;
1325 $service_data = $period ? WebTotemAPI::getServerStatusData($host['id'], $period) : $data['serverStatus'];
1326
1327 $build['server_status_ram'] = [
1328 'variables' => [
1329 "info" => $service_data['info'],
1330 "ram_chart" => WebTotem::generateChart($service_data['ramChart']),
1331 "days" => $period,
1332 ],
1333 'template' => 'server_status_ram',
1334 ];
1335
1336 // Server Status CPU.
1337 $period = WebTotemOption::getSessionOption('cpu_period') ?: 7;
1338 $service_data = $period ? WebTotemAPI::getServerStatusData($host['id'], $period) : $data['serverStatus'];
1339 $build['server_status_cpu'] = [
1340 'variables' => [
1341 "cpu_chart" => WebTotem::generateChart($service_data['cpuChart']),
1342 "days" => $period,
1343 ],
1344
1345 'template' => 'server_status_cpu',
1346 ];
1347
1348 // Antivirus stats blocks.
1349 $antivirus_stats = $data['antivirus']['stats'];
1350 $build['antivirus_stats'] = [
1351 'variables' => [
1352 "changes" => $antivirus_stats['changed'] ?: 0,
1353 "scanned" => $antivirus_stats['scanned'] ?: 0,
1354 "deleted" => $antivirus_stats['deleted'] ?: 0,
1355 "infected" => $antivirus_stats["infected"] ?: 0,
1356 ],
1357
1358 'template' => 'antivirus_stats',
1359 ];
1360
1361 // Monitoring blocks.
1362 $build['monitoring'] = [
1363 'variables' => [
1364 "ssl" => [
1365 'status' => WebTotem::getStatusData($data['ssl']['status']),
1366 'days_left' => WebTotem::daysLeft($data['ssl']['expiryDate']),
1367 'issue_date' => WebTotem::dateFormatter($data['ssl']['issueDate']),
1368 'expiry_date' => WebTotem::dateFormatter($data['ssl']['expiryDate']),
1369 ],
1370 "availability" => [
1371 'status' => WebTotem::getStatusData($data['availability']['status']),
1372 "percent" => $data['availability']['percent'],
1373 "response_time" => ceil($data['availability']['responseTime'] / 1000000) . ' ' . __('ms.', 'wtotem'),
1374 "downtime" => ceil($data['availability']['downTime'] / 1000000) . ' ' . __('ms.', 'wtotem'),
1375 "last_test" => WebTotem::dateFormatter($data['availability']['lastTest']['time']),
1376 ],
1377 'reputation' => [
1378 "status" => WebTotem::getStatusData($data['reputation']['status']),
1379 "blacklists_entries" => WebTotem::blacklistsEntries(
1380 $data['reputation']['status'],
1381 $data['reputation']['virusList']),
1382 "info" => WebTotem::getReputationInfo($data['reputation']['status']),
1383 "last_test" => WebTotem::dateFormatter($data['reputation']['lastTest']['time']),
1384 ],
1385 ],
1386 'template' => 'monitoring',
1387 ];
1388
1389
1390 // Scanning blocks.
1391 $disc_usage_data = $data['serverStatus']['discUsage'];
1392 $disc_usage = [
1393 'total' => $disc_usage_data['total'],
1394 'free' => $disc_usage_data['free'],
1395 'used' => $disc_usage_data['total'] - $disc_usage_data['free'],
1396 ];
1397
1398 $build['scanning'] = [
1399 'variables' => [
1400 "ports" => [
1401 'status' => WebTotem::getStatusData($data['ports']['status']),
1402 "ip" => $data['ports']['ip'],
1403 "number" => count($data['ports']['tcp']),
1404 "tcp" => $data['ports']['tcp'],
1405 "ignore_ports" => $data['ports']['ignorePorts'],
1406 "last_test" => WebTotem::dateFormatter($data['ports']['lastTest']['time']),
1407 ],
1408 "deface" => [
1409 'status' => WebTotem::getStatusData($data['deface']['status']),
1410 "number" => $data['deface']['count'],
1411 "words" => !empty($data['deface']['words']) ? implode(",", $data['deface']['words']) : '',
1412 "last_test" => WebTotem::dateFormatter($data['deface']['lastTest']['time']),
1413 ],
1414 "disc_usage" => $disc_usage,
1415 "disc_chart" => json_encode($disc_usage),
1416 ],
1417 'template' => 'scanning',
1418 ];
1419
1420 $response['content'][] = ['selector' => '#scoring', 'content' => $template->arrayRender($build['scoring'])];
1421 $response['content'][] = ['selector' => '#firewall_stats', 'content' => $template->arrayRender($build['firewall_stats'])];
1422 $response['content'][] = ['selector' => '#waf_chart_period', 'content' => $template->arrayRender($build['chart_periods'])];
1423 $response['content'][] = ['selector' => '#firewall_data', 'content' => $template->arrayRender($build['firewall_data'])];
1424 $response['content'][] = ['selector' => '#server_status_cpu', 'content' => $template->arrayRender($build['server_status_cpu'])];
1425 $response['content'][] = ['selector' => '#server_status_ram', 'content' => $template->arrayRender($build['server_status_ram'])];
1426 $response['content'][] = ['selector' => '#antivirus_stats', 'content' => $template->arrayRender($build['antivirus_stats'])];
1427 $response['content'][] = ['selector' => '#monitoring', 'content' => $template->arrayRender($build['monitoring'])];
1428 $response['content'][] = ['selector' => '#scanning', 'content' => $template->arrayRender($build['scanning'])];
1429
1430 break;
1431 }
1432
1433 $response['success'] = true;
1434 $response['notifications'] = self::notifications();
1435 wp_send_json($response);
1436 }
1437
1438 /**
1439 * Notification output.
1440 *
1441 * @return string
1442 */
1443 public static function notifications() {
1444
1445 $notifications = WebTotem::getNotifications();
1446
1447 if($notifications){
1448 $build[] = [
1449 'variables' => [
1450 'notifications' => $notifications,
1451 ],
1452
1453 'template' => 'notifications',
1454 ];
1455
1456 $template = new WebTotemTemplate();
1457 return $template->arrayRender($build);
1458 }
1459 return false;
1460
1461
1462 }
1463
1464 }
1465