PluginProbe
WebTotem Security / 2.4.14
WebTotem Security v2.4.14
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.14, at lib/Ajax.php

1,748 lines 69.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 * Activation plugin.
13 *
14 * @return void
15 */
16 public static function activation() {
17
18 if (WebTotemRequest::post('ajax_action') !== 'activation') {
19 return;
20 }
21
22 if($api_key = WebTotemRequest::post('api_key')) {
23
24 $result = WebTotemAPI::auth($api_key);
25
26 if($result == 'success') {
27 if(WebTotem::isMultiSite()) {
28 $link = WebTotem::adminURL('admin.php?page=wtotem_all_sites');
29 } else {
30 $link = WebTotem::adminURL('admin.php?page=wtotem');
31 }
32 wp_send_json([
33 'link' => $link,
34 'success' => true,
35 'user' => WebTotemAPI::getEmail(),
36 ], 200);
37 } else {
38
39 wp_send_json([
40 'notifications' => self::notifications(),
41 'success' => false,
42 ], 200);
43 }
44 }
45
46 }
47
48 /**
49 * The process of installing agents (WAF, AV) on the main page.
50 *
51 * @return void
52 */
53 public static function agentsInstallation() {
54
55 if (WebTotemRequest::post('ajax_action') !== 'agents_installation') {
56 return;
57 }
58
59 $av_installed = WebTotemOption::getOption('av_installed');
60 $waf_installed = WebTotemOption::getOption('waf_installed');
61
62 // Check if the agents are installed.
63 if ($av_installed and $waf_installed) {
64 $agents_statuses = [
65 'process_statuses' => [
66 'av' => 'installed',
67 'waf' => 'installed',
68 ],
69 ];
70 }
71 else {
72 // If not installed, then request statuses from the WebTotem API.
73 $host = WebTotemAPI::siteInfo();
74 $data = WebTotemAPI::getAgentsStatusesFromAPI($host['id']);
75
76 $agents_statuses = [
77 'av' => $data['av']['status'],
78 'waf' => $data['waf']['status'],
79 ];
80
81 $agents_statuses = WebTotem::getAgentsStatuses($agents_statuses);
82 }
83
84 $build[] = [
85 'variables' => [
86 'process_status' => $agents_statuses['process_statuses'],
87 ],
88 'template' => 'agents_installation',
89 ];
90
91 $status = [
92 'av' => $agents_statuses['process_statuses']['av'] == 'installed',
93 'waf' => $agents_statuses['process_statuses']['waf'] == 'installed',
94 ];
95
96 WebTotemOption::setOptions([
97 'av_installed' => $status['av'],
98 'waf_installed' => $status['waf'],
99 ]);
100
101 $template = new WebTotemTemplate();
102 $agents = $template->arrayRender($build);
103
104 wp_send_json([
105 'success' => true,
106 'notifications' => self::notifications(),
107 'agents' => $agents,
108 'agents_statuses' => $status['av'] && $status['waf'],
109 ]);
110 }
111
112
113 /**
114 * Reinstall agents.
115 *
116 * @return void
117 */
118 public static function reinstallAgents() {
119
120 if (WebTotemRequest::post('ajax_action') !== 'reinstall_agents') {
121 return;
122 }
123
124 if (WebTotemAgentManager::removeAgents()) {
125 WebTotemAgentManager::amInstall();
126 }
127 $response['success'] = true;
128 $response['redirect_link'] = WebTotem::adminURL('admin.php?page=wtotem');
129 wp_send_json($response);
130
131 }
132
133 /**
134 * Deleting plugin activation data and redirecting to the activation page.
135 *
136 * @return void
137 */
138 public static function logout() {
139
140 if (WebTotemRequest::post('ajax_action') !== 'logout') {
141 return;
142 }
143
144 WebTotemOption::logout();
145
146 $response['success'] = true;
147 $response['redirect_link'] = WebTotem::adminURL('admin.php?page=wtotem_activation');
148 wp_send_json($response);
149
150 }
151
152 /**
153 * Creating a modal window.
154 *
155 * @return void
156 */
157 public static function popup() {
158
159 if (WebTotemRequest::post('ajax_action') !== 'popup') {
160 return;
161 }
162
163 $action = WebTotemRequest::post('popup_action');
164 $template = new WebTotemTemplate();
165
166 if($action){
167 switch ($action) {
168 case 'reinstall_agents':
169 $build[] = [
170 'variables' => [
171 'message' => sprintf(__('Some scanning data for %s may be deleted.', 'wtotem'), WEBTOTEM_SITE_DOMAIN),
172 'action' => 'reinstall_agents',
173 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
174 ],
175 'template' => 'popup',
176 ];
177 break;
178
179 case 'logout':
180 $build[] = [
181 'variables' => [
182 'message' => __('Are you sure you want to change the API key?', 'wtotem'),
183 'action' => 'logout',
184 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
185 ],
186 'template' => 'popup',
187 ];
188 break;
189 }
190
191 wp_send_json([
192 'success' => true,
193 'content' => $template->arrayRender($build),
194 ]);
195 }
196
197 wp_send_json([
198 'success' => false,
199 ]);
200
201 }
202
203 /**
204 * Request to update charts with parameters.
205 *
206 * @return void
207 */
208 public static function chart() {
209
210 if (WebTotemRequest::post('ajax_action') !== 'chart') {
211 return;
212 }
213
214 $template = new WebTotemTemplate();
215
216 $days = (integer) WebTotemRequest::post('days');
217 $service = WebTotemRequest::post('service');
218
219 $host = WebTotemAPI::siteInfo();
220
221 switch ($service) {
222 case 'waf':
223
224 WebTotemOption::setSessionOptions(['firewall_period' => $days]);
225
226 // Firewall chart.
227 $data = WebTotemAPI::getFirewallChart($host['id'], $days);
228 $chart = WebTotem::generateWafChart($data['chart']);
229
230 $_chart[] = [
231 'variables' => [
232 'days' => $days,
233 'chart' => $chart['chart'],
234 ],
235 'template' => 'firewall_chart',
236 ];
237
238 // Firewall logs.
239 $data = WebTotemAPI::getFirewall($host['id'], 10, NULL, $days);
240 $firewall = $data['firewall'];
241
242 $waf_logs[] = [
243 'variables' => [
244 'logs' => WebTotem::wafLogs($firewall['logs']['edges']),
245 ],
246 'template' => 'firewall_logs',
247 ];
248
249 // Firewall stats.
250 $waf_stats[] = [
251 'variables' => [
252 'is_waf_training' => WebTotem::isWafTraining($data['agentManager']['createdAt']),
253 'all_attacks' => $chart['count_attacks'],
254 'blocking' => $chart['count_blocks'],
255 'not_blocking' => $chart['count_attacks'] - $chart['count_blocks'],
256 'most_attacks' => WebTotem::getMostAttacksData($firewall['map']),
257 ],
258 'template' => 'firewall_stats',
259 ];
260
261 WebTotemOption::setSessionOptions([
262 'firewall_cursor' => $firewall['logs']['pageInfo']['endCursor'],
263 ]);
264
265 $has_next_page = $firewall['logs']['pageInfo']['hasNextPage'];
266
267 $response = [
268 'chart' => $template->arrayRender($_chart),
269 'waf_logs' => $template->arrayRender($waf_logs),
270 'waf_stats' => $template->arrayRender($waf_stats),
271 'has_next_page' => $has_next_page,
272 'service' => 'waf',
273 ];
274
275 break;
276
277 case 'cpu':
278 WebTotemOption::setSessionOptions(['cpu_period' => $days]);
279
280 $data = WebTotemAPI::getServerStatusData($host['id'], $days);
281 $chart = WebTotem::generateChart($data['cpuChart'], $days);
282
283 $_chart[] = [
284 'variables' => [
285 'days' => $days,
286 'chart' => $chart,
287 ],
288 'template' => 'cpu_chart',
289 ];
290
291 $response = [
292 'chart' => $template->arrayRender($_chart),
293 'service' => 'cpu',
294 ];
295
296 break;
297
298 case 'ram':
299 WebTotemOption::setSessionOptions(['ram_period' => $days]);
300
301 $data = WebTotemAPI::getServerStatusData($host['id'], $days);
302 $chart = WebTotem::generateChart($data['ramChart'], $days);
303
304 $_chart[] = [
305 'variables' => [
306 'days' => $days,
307 'chart' => $chart,
308 ],
309 'template' => 'ram_chart',
310 ];
311
312 $response = [
313 'chart' => $template->arrayRender($_chart),
314 'service' => 'ram',
315 ];
316
317 break;
318
319 case 'map':
320 $data = WebTotemAPI::getFirewallChart($host['id'], $days);
321 $chart = WebTotem::generateAttacksMapChart($data['map']);
322 $world_map_json = WEBTOTEM_URL . '/includes/js/world_map.json';
323
324 $_chart[] = [
325 'variables' => [
326 'attacks_map' => $chart,
327 'world_map_json' => $world_map_json,
328 ],
329 'template' => 'map_chart',
330 ];
331
332 $response = [
333 'chart' => $template->arrayRender($_chart),
334 'service' => 'map',
335 ];
336
337 break;
338
339 }
340
341 if ($service) {
342 $response['success'] = true;
343 $response['notifications'] = self::notifications();
344 wp_send_json($response);
345 }
346
347 }
348
349 /**
350 * Data lazy load.
351
352 * @return void
353 */
354 public static function lazyLoad() {
355
356
357 if (WebTotemRequest::post('ajax_action') !== 'lazy_load') {
358 return;
359 }
360
361 $template = new WebTotemTemplate();
362
363 $service = WebTotemRequest::post('service');
364
365 $host = WebTotemAPI::siteInfo();
366
367 switch ($service) {
368 case 'all_sites':
369 $cursor = WebTotemOption::getSessionOption('sites_cursor') ?: NULL;
370 $allSites = WebTotemAPI::getSites($cursor);
371
372 $has_next_page = $allSites['pageInfo']['hasNextPage'];
373
374 WebTotemOption::setSessionOptions([
375 'sites_cursor' => $allSites['pageInfo']['endCursor'],
376 ]);
377
378 // Sites list.
379 $build[] = [
380 'variables' => [
381 'sites' => WebTotem::allSitesData($allSites),
382 'has_next_page' => $has_next_page,
383 ],
384 'template' => 'multisite_list'
385 ];
386
387 break;
388
389 case 'firewall':
390 $cursor = WebTotemOption::getSessionOption('firewall_cursor') ?: NULL;
391 $period = WebTotemOption::getSessionOption('firewall_period') ?: 365;
392 $data = WebTotemAPI::getFirewall($host['id'], 10, $cursor, $period);
393 $service_data = $data['firewall'];
394 $has_next_page = $service_data['logs']['pageInfo']['hasNextPage'];
395
396 WebTotemOption::setSessionOptions([
397 'firewall_cursor' => $service_data['logs']['pageInfo']['endCursor'],
398 ]);
399
400 // Firewall logs.
401 $build[] = [
402 'variables' => [
403 'logs' => WebTotem::wafLogs($service_data['logs']['edges']),
404 ],
405 'template' => 'firewall_logs',
406 ];
407
408 break;
409
410 case 'antivirus':
411 $cursor = WebTotemOption::getSessionOption('antivirus_cursor') ?: NULL;
412 $event = WebTotemOption::getSessionOption('antivirus_event') ?: NULL;
413 $permissions = WebTotemOption::getSessionOption('antivirus_permissions') ?: NULL;
414
415 $params = [
416 'host_id' => $host['id'],
417 'limit' => 10,
418 'days' => 365,
419 'cursor' => $cursor,
420 'event' => $event,
421 'permissions' => $permissions,
422 ];
423
424 $data = WebTotemAPI::getAntivirus($params);
425 $has_next_page = $data['log']['pageInfo']['hasNextPage'];
426
427 WebTotemOption::setSessionOptions([
428 'antivirus_cursor' => $data['log']['pageInfo']['endCursor'],
429 ]);
430
431 // Antivirus logs.
432 $build[] = [
433 'variables' => [
434 'logs' =>WebTotem::getAntivirusLogs($data['log']['edges']),
435 ],
436 'template' => 'antivirus_logs',
437 ];
438
439 break;
440
441 case 'reports':
442 $cursor = WebTotemOption::getSessionOption('reports_cursor') ?: NULL;
443
444 $data = WebTotemAPI::getAllReports($host['id'], 10, $cursor);
445 $has_next_page = $data['pageInfo']['hasNextPage'];
446
447 WebTotemOption::setSessionOptions([
448 'reports_cursor' => $data['pageInfo']['endCursor'],
449 ]);
450
451 // Reports.
452 $build[] = [
453 'variables' => [
454 "reports" => WebTotem::getReports($data['edges']),
455 "has_next_page" => $data['pageInfo']['hasNextPage'],
456 ],
457 'template' => 'reports_list',
458 ];
459
460 break;
461
462 case 'reports_m':
463 $cursor = WebTotemOption::getSessionOption('reports_m_cursor') ?: NULL;
464
465 $data = WebTotemAPI::getAllReports($host['id'], 10, $cursor);
466 $has_next_page = $data['pageInfo']['hasNextPage'];
467
468 WebTotemOption::setSessionOptions([
469 'reports_m_cursor' => $data['pageInfo']['endCursor'],
470 ]);
471
472 // Reports mobile.
473 $build[] = [
474 'variables' => [
475 "reports" => WebTotem::getReports($data['edges']),
476 "has_next_page" => $data['pageInfo']['hasNextPage'],
477 ],
478 'template' => 'reports_list_mobile',
479 ];
480
481 break;
482 }
483
484 if ($service) {
485
486 wp_send_json([
487 'success' => true,
488 'content' => $template->arrayRender($build),
489 'has_next_page' => $has_next_page,
490 'notifications' => self::notifications(),
491 ]);
492 }
493 }
494
495
496 /**
497 * Add date filter.
498 *
499 * @return void
500 */
501 public static function wafDateFilter() {
502
503 if (WebTotemRequest::post('ajax_action') !== 'waf_date_filter') {
504 return;
505 }
506
507 $template = new WebTotemTemplate();
508
509 $date_from = WebTotemRequest::post('date_from');
510
511 $period = explode(" to ", $date_from);
512 WebTotemOption::setSessionOptions(['firewall_period' => $period]);
513
514 $host = WebTotemAPI::siteInfo();
515
516 // Firewall logs.
517 $data = WebTotemAPI::getFirewall($host['id'], 10, NULL, $period);
518 $firewall = $data['firewall'];
519
520 $waf_logs[] = [
521 'variables' => [
522 'logs' => WebTotem::wafLogs($firewall['logs']['edges']),
523 ],
524 'template' => 'firewall_logs',
525 ];
526
527 // Firewall chart.
528 $data = WebTotemAPI::getFirewallChart($host['id'], $period);
529 $chart = WebTotem::generateWafChart($data['chart']);
530
531 $_chart[] = [
532 'variables' => [
533 'days' => $chart['days'],
534 'chart' => $chart['chart'],
535 ],
536 'template' => 'firewall_chart',
537 ];
538
539 // Firewall stats.
540 $waf_stats[] = [
541 'variables' => [
542 'is_waf_training' => WebTotem::isWafTraining($data['agentManager']['createdAt']),
543 'all_attacks' => $chart['count_attacks'],
544 'blocking' => $chart['count_blocks'],
545 'not_blocking' => $chart['count_attacks'] - $chart['count_blocks'],
546 'most_attacks' => WebTotem::getMostAttacksData($firewall['map']),
547 ],
548 'template' => 'firewall_stats',
549 ];
550
551 WebTotemOption::setSessionOptions([
552 'firewall_cursor' => $firewall['logs']['pageInfo']['endCursor'],
553 ]);
554
555 $has_next_page = $firewall['logs']['pageInfo']['hasNextPage'];
556
557 $response = [
558 'success' => true,
559 'chart' => $template->arrayRender($_chart),
560 'waf_logs' => $template->arrayRender($waf_logs),
561 'waf_stats' => $template->arrayRender($waf_stats),
562 'has_next_page' => $has_next_page,
563 'notifications' => self::notifications(),
564 ];
565
566 wp_send_json($response);
567 }
568
569
570 /**
571 * Request to restart re-scan and receive antivirus data.
572 *
573 * @return void
574 */
575 public static function antivirus() {
576
577 if (WebTotemRequest::post('ajax_action') !== 'antivirus') {
578 return;
579 }
580
581 $action = WebTotemRequest::post('av_action');
582
583 $host = WebTotemAPI::siteInfo();
584
585 switch ($action) {
586 case 'rescan':
587 $response = WebTotemAPI::forceCheck($host['id'], 'av');
588
589 if (!isset($response['errors'])) {
590 $data = WebTotemAPI::getAntivirusLastTest($host['id']);
591 $response['last_scan'] = WebTotem::dateFormatter($data['lastTest']['time']);
592
593 }
594 break;
595
596 case 'download_report':
597 $response = WebTotemAPI::avExport($host['id']);
598 if (!isset($response['errors'])) {
599 $response['doc_link'] = $response['data']['auth']['sites']['av']['export'];
600 }
601 break;
602
603 case 'filter':
604
605 $file_status = WebTotemRequest::post('file_status');
606 $permission = filter_var( WebTotemRequest::post('permission'), FILTER_VALIDATE_BOOLEAN);
607
608 WebTotemOption::setSessionOptions([
609 'antivirus_permissions' => $permission,
610 'antivirus_event' => $file_status,
611 ]);
612
613 $params = [
614 'host_id' => $host['id'],
615 'limit' => 10,
616 'days' => 365,
617 'cursor' => NULL,
618 'event' => $file_status,
619 'permissions' => $permission,
620 ];
621
622 $data = WebTotemAPI::getAntivirus($params);
623 $has_next_page = $data['log']['pageInfo']['hasNextPage'];
624
625 WebTotemOption::setSessionOptions([
626 'antivirus_cursor' => $data['log']['pageInfo']['endCursor'],
627 ]);
628
629 // Antivirus logs.
630 $build[] = [
631 'variables' => [
632 'logs' =>WebTotem::getAntivirusLogs($data['log']['edges']),
633 ],
634 'template' => 'antivirus_logs',
635 ];
636
637 $template = new WebTotemTemplate();
638 $response = [
639 'logs' => $template->arrayRender($build),
640 'has_next_page' => $has_next_page,
641 ];
642
643 break;
644 }
645
646 $response['success'] = true;
647 $response['notifications'] = self::notifications();
648
649 wp_send_json($response);
650 }
651
652 /**
653 * Request to add a file to quarantine.
654 *
655 * @return void
656 */
657 public static function quarantine() {
658 if (WebTotemRequest::post('ajax_action') !== 'quarantine') {
659 return;
660 }
661
662 $action = WebTotemRequest::post('quarantine_action');
663 $id_or_path = WebTotemRequest::post('id_or_path');
664
665 $host = WebTotemAPI::siteInfo();
666 $response = [];
667
668 switch ($action) {
669 case 'add':
670 $api_response = WebTotemAPI::moveToQuarantine($host['id'], $id_or_path);
671 break;
672
673 case 'remove':
674 $api_response = WebTotemAPI::moveFromQuarantine($id_or_path);
675 break;
676 }
677
678 if (!isset($api_response['errors'])) {
679
680 $quarantine_logs = WebTotemAPI::getQuarantineList($host['id']);
681 $quarantine_count = count($quarantine_logs);
682
683 // Quarantine logs.
684 $quarantine[] = [
685 'variables' => [
686 "logs" => WebTotem::getQuarantineLogs($quarantine_logs),
687 "count" => $quarantine_count,
688 ],
689 'template' => 'quarantine',
690 ];
691
692 $cursor = WebTotemOption::getSessionOption('antivirus_cursor') ?: NULL;
693 $event = WebTotemOption::getSessionOption('antivirus_event') ?: NULL;
694 $permissions = WebTotemOption::getSessionOption('antivirus_permissions') ?: NULL;
695
696 $params = [
697 'host_id' => $host['id'],
698 'limit' => 10,
699 'days' => 365,
700 'cursor' => $cursor,
701 'event' => $event,
702 'permissions' => $permissions,
703 ];
704
705 $data = WebTotemAPI::getAntivirus($params);
706 WebTotemCache::setData(['getAntivirus' => $data], $host['id']);
707
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 ];
787
788 }
789
790 $response['success'] = true;
791 $response['notifications'] = self::notifications();
792
793 wp_send_json($response);
794 }
795
796 /**
797 * Request for a report link.
798 *
799 * @return void
800 */
801 public static function reports() {
802
803 if (WebTotemRequest::post('ajax_action') !== 'reports') {
804 return;
805 }
806
807 $template = new WebTotemTemplate();
808
809 $action = WebTotemRequest::post('report_action');
810
811 switch ($action) {
812 case 'download':
813 $id = WebTotemRequest::post('id');
814 $link = WebTotemAPI::downloadReport($id);
815 if ($link) {
816 $response['link'] = $link;
817 }
818 break;
819 case 'report_form':
820
821 $period = explode(" to ", WebTotemRequest::post('date_period'));
822 $modules_data = WebTotemRequest::post('modules');
823
824 $modules = [
825 'wa' => 'false',
826 'dc' => 'false',
827 'ps' => 'false',
828 'rc' => 'false',
829 'sc' => 'false',
830 'av' => 'false',
831 'waf' => 'false'
832 ];
833
834 foreach ($modules_data as $module => $value){
835 $modules[$module] = 'true';
836 }
837
838 $host = WebTotemAPI::siteInfo();
839 $api_response = WebTotemAPI::generateReport($host['id'], $period, $modules);
840
841 if (!$api_response) {
842 $massage = '<div class="message error_message">' . __('Report generation error', 'wtotem') . '</div>';
843 }
844 else {
845 $data = WebTotemAPI::getAllReports($host['id']);
846 WebTotemCache::setData(['getAllReports' => $data], $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
917 $configs_data = WebTotemAPI::getConfigs($host['id']);
918 WebTotemCache::setData(['getConfigs' => $configs_data], $host['id']);
919
920 $response['isActive'] = $config['isActive'];
921 break;
922
923 case 'module_notifications':
924 $config = WebTotemAPI::toggleNotifications($host['id'], WebTotemRequest::post('value'));
925
926 $configs_data = WebTotemAPI::getConfigs($host['id']);
927 WebTotemCache::setData(['getConfigs' => $configs_data], $host['id']);
928
929 $response['isActive'] = $config;
930 $response['success'] = true;
931 break;
932
933 case 'waf_settings':
934
935 $settings = [
936 'gdn' => WebTotemRequest::post('gdn'),
937 'dosProtection' => WebTotemRequest::post('dos'),
938 'dosLimit' => WebTotemRequest::post('dos_limit'),
939 'loginAttemptsProtection' => WebTotemRequest::post('login_attempt'),
940 'loginAttemptsLimit' => WebTotemRequest::post('login_attempt_limit'),
941 ];
942
943 $host = WebTotemAPI::siteInfo();
944 $api_response = WebTotemAPI::setFirewallSettings($host['id'], $settings);
945
946 if (!$api_response['errors']) {
947
948 $data = WebTotemAPI::getIpLists($host['id']);
949 WebTotemCache::setData(['getIpLists' => $data], $host['id']);
950
951 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
952 }
953
954 $response['success'] = true;
955 break;
956
957 case 'recaptcha_settings':
958
959 $recaptcha_v3_site_key = WebTotemRequest::post('recaptcha_v3_site_key');
960 $recaptcha_v3_secret = WebTotemRequest::post('recaptcha_v3_secret');
961 $recaptcha_token = WebTotemRequest::post('recaptcha_token');
962 $recaptcha = filter_var(WebTotemRequest::post('recaptcha'), FILTER_VALIDATE_BOOLEAN) ?: false;
963
964 if($recaptcha){
965 if(empty($recaptcha_v3_site_key) or empty($recaptcha_v3_secret) or strlen($recaptcha_v3_site_key) != 40 or strlen($recaptcha_v3_secret) != 40 ){
966 $response['success'] = false;
967
968 $response['errors'] = ['recaptcha_v3_site_key' => '', 'recaptcha_v3_secret' => ''];
969
970 if(empty($recaptcha_v3_site_key)){
971 $response['errors']['recaptcha_v3_site_key'] = __('The field is required.', 'wtotem');
972 } else if(strlen($recaptcha_v3_site_key) != 40){
973 $response['errors']['recaptcha_v3_site_key'] = __('Invalid field length.', 'wtotem');
974 }
975 if(empty($recaptcha_v3_secret)){
976 $response['errors']['recaptcha_v3_secret'] = __('The field is required.', 'wtotem');
977 } else if(strlen($recaptcha_v3_secret) != 40){
978 $response['errors']['recaptcha_v3_secret'] = __('Invalid field length.', 'wtotem');
979 }
980
981 break;
982 }
983
984 $score = WebTotemCaptcha::score($recaptcha_token, $recaptcha_v3_secret);
985
986 if( $score == 0 ){
987 $response['success'] = false;
988 $response['errors']['recaptcha_v3_secret'] = __('Make sure that you have filled in the field correctly.', 'wtotem');
989 $response['errors']['recaptcha_v3_site_key'] = __('Make sure that you have filled in the field correctly.', 'wtotem');
990 break;
991 }
992 }
993
994
995 if($recaptcha){
996 $settings = [
997 'recaptcha_v3_site_key' => $recaptcha_v3_site_key,
998 'recaptcha_v3_secret' => $recaptcha_v3_secret,
999 ];
1000 }
1001 $settings['recaptcha'] = $recaptcha;
1002
1003 if($settings['hide_wp_version']){
1004 WebTotemOption::hideReadme();
1005 } else {
1006 WebTotemOption::restoreReadme();
1007 }
1008
1009 WebTotemOption::setPluginSettings($settings);
1010
1011 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1012 $response['success'] = true;
1013
1014
1015 break;
1016
1017 case 'other_settings':
1018
1019 $settings = [
1020 'hide_wp_version' => filter_var(WebTotemRequest::post('hide_wp_version'), FILTER_VALIDATE_BOOLEAN) ?: false,
1021 ];
1022
1023 if($settings['hide_wp_version']){
1024 WebTotemOption::hideReadme();
1025
1026 } else {
1027 WebTotemOption::restoreReadme();
1028 }
1029
1030 WebTotemOption::setPluginSettings($settings);
1031
1032 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1033 $response['success'] = true;
1034
1035
1036 break;
1037
1038 case 'bruteforce_protection_settings':
1039
1040 $data = WebTotemRequest::post('data');
1041 $response['success'] = true;
1042
1043 $login_attempts = filter_var($data['login_attempts'], FILTER_VALIDATE_BOOLEAN) ?: false;
1044 $password_reset = filter_var($data['password_reset'], FILTER_VALIDATE_BOOLEAN) ?: false;
1045
1046 if($login_attempts){
1047 $response['errors'] = ['login_number_of_attempts' => '', 'login_minutes_of_ban' => ''];
1048
1049 if(empty($data['login_number_of_attempts']) or empty($data['login_minutes_of_ban'])){
1050 $response['success'] = false;
1051
1052 if(empty($data['login_number_of_attempts'])){
1053 $response['errors']['login_number_of_attempts'] = __('The field is required.', 'wtotem');
1054 }
1055 if(empty($data['login_minutes_of_ban'])){
1056 $response['errors']['login_minutes_of_ban'] = __('The field is required.', 'wtotem');
1057 }
1058 }
1059 if(isset($data['login_number_of_attempts']) and
1060 ($data['login_number_of_attempts'] <= 0 or $data['login_number_of_attempts'] > 1000000)) {
1061 $response['success'] = false;
1062 $response['errors']['login_number_of_attempts'] = __('Please specify a value from 1 to 1000000.', 'wtotem');
1063 }
1064 }
1065
1066 if($password_reset){
1067 if(empty($data['password_reset_number_of_attempts']) or empty($data['password_reset_minutes_of_ban'])){
1068 $response['success'] = false;
1069
1070 $response['errors']['password_reset_number_of_attempts'] = '';
1071 $response['errors']['password_reset_minutes_of_ban'] = '';
1072
1073 if(empty($data['password_reset_number_of_attempts'])){
1074 $response['errors']['password_reset_number_of_attempts'] = __('The field is required.', 'wtotem');
1075 }
1076 if(empty($data['password_reset_minutes_of_ban'])){
1077 $response['errors']['password_reset_minutes_of_ban'] = __('The field is required.', 'wtotem');
1078 }
1079 }
1080
1081 if(isset($data['password_reset_number_of_attempts']) and
1082 ($data['password_reset_number_of_attempts'] <= 0 or $data['password_reset_number_of_attempts'] > 1000000)) {
1083 $response['success'] = false;
1084 $response['errors']['password_reset_number_of_attempts'] = __('Please specify a value from 1 to 1000000.', 'wtotem');
1085 }
1086 }
1087 if(!$response['success']){
1088 break;
1089 }
1090
1091 $settings = [
1092 'login_attempts' => $login_attempts,
1093 'password_reset' => $password_reset,
1094 ];
1095
1096 if($login_attempts){
1097 $settings['login_number_of_attempts'] = $data['login_number_of_attempts'];
1098 $settings['login_minutes_of_ban'] = $data['login_minutes_of_ban'];
1099 }
1100 if($password_reset){
1101 $settings['password_reset_number_of_attempts'] = $data['password_reset_number_of_attempts'];
1102 $settings['password_reset_minutes_of_ban'] = $data['password_reset_minutes_of_ban'];
1103 }
1104
1105 WebTotemOption::setPluginSettings($settings);
1106
1107 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1108
1109 break;
1110
1111 case 'add_allow_ip':
1112 $api_response = WebTotemAPI::addIpToList($host['id'], WebTotemRequest::post('value'), 'white');
1113 if ($api_response) {
1114 $data = WebTotemAPI::getIpLists($host['id']);
1115 WebTotemCache::setData(['getIpLists' => $data], $host['id']);
1116 $build[] = [
1117 'variables' => [
1118 "list" => WebTotem::getIpList($data['whiteList'], 'ip_allow'),
1119 ],
1120 'template' => 'allow_deny_list',
1121 ];
1122
1123 $response['content'] = $template->arrayRender($build);
1124 }
1125
1126 $response['success'] = true;
1127 break;
1128
1129 case 'add_deny_ip':
1130 $api_response = WebTotemAPI::addIpToList($host['id'], WebTotemRequest::post('value'), 'black');
1131 if ($api_response) {
1132 $data = WebTotemAPI::getIpLists($host['id']);
1133 WebTotemCache::setData(['getIpLists' => $data], $host['id']);
1134 $build[] = [
1135 'variables' => [
1136 "list" => WebTotem::getIpList($data['blackList'], 'ip_deny'),
1137 ],
1138 'template' => 'allow_deny_list',
1139 ];
1140
1141 $response['content'] = $template->arrayRender($build);
1142 }
1143
1144 $response['success'] = true;
1145 break;
1146
1147 case 'add_allow_url':
1148 $api_response = WebTotemAPI::addUrlToAllowList($host['id'], WebTotemRequest::post('value'));
1149 if ($api_response) {
1150 $data = WebTotemAPI::getAllowUrlList($host['id']);
1151 $build[] = [
1152 'variables' => [
1153 "list" => WebTotem::getUrlAllowList($data),
1154 ],
1155 'template' => 'allow_url_list',
1156 ];
1157
1158 $response['content'] = $template->arrayRender($build);
1159 }
1160
1161 $response['success'] = true;
1162 break;
1163
1164 case 'add_ip_list':
1165 $ips = WebTotemRequest::post('ips');
1166 $list_name = WebTotemRequest::post('list');
1167
1168 $host = WebTotemAPI::siteInfo();
1169 $api_response = WebTotemAPI::addIpToList($host['id'], $ips, $list_name);
1170
1171 if ($api_response) {
1172 $data = WebTotemAPI::getIpLists($host['id']);
1173
1174 $data_list = ($list_name == 'white') ? $data['whiteList'] : $data['blackList'];
1175 $ip_list = ($list_name == 'white') ? 'ip_allow' : 'ip_deny';
1176
1177 $build[] = [
1178 'variables' => [
1179 "list" => WebTotem::getIpList($data_list, $ip_list),
1180 ],
1181 'template' => 'allow_deny_list',
1182 ];
1183
1184 if ($api_response['status'] != 0) {
1185 $response['invalidIPs'] = implode("\n", $api_response['invalidIPs']);
1186 }
1187
1188 $response['wrap'] = ($list_name == 'white') ? '#wtotem_ip_allow_list' : '#wtotem_ip_deny_list';
1189 $response['content'] = $template->arrayRender($build);
1190 }
1191 $response['success'] = true;
1192
1193 break;
1194 }
1195
1196 $response['notifications'] = self::notifications();
1197 wp_send_json($response);
1198 }
1199
1200 /**
1201 * Request to remove from the list of deny/allowed ip or url addresses.
1202 *
1203 * @return void
1204 */
1205 public static function remove() {
1206
1207 if (WebTotemRequest::post('ajax_action') !== 'remove') {
1208 return;
1209 }
1210
1211 $av_installed = WebTotemOption::getOption('av_installed');
1212 $waf_installed = WebTotemOption::getOption('waf_installed');
1213
1214 if(!$av_installed && !$waf_installed) {
1215 WebTotemOption::setNotification('warning', __('It is not possible to make changes because the agents are not installed.', 'wtotem'));
1216
1217 wp_send_json([
1218 'success' => false,
1219 'notifications' => self::notifications()
1220 ]);
1221 }
1222
1223 $action = WebTotemRequest::post('remove_action');
1224 $host = WebTotemAPI::siteInfo();
1225 $template = new WebTotemTemplate();
1226
1227 switch ($action) {
1228 case 'ip_allow':
1229 $api_response = WebTotemAPI::removeIpFromList( WebTotemRequest::post('id') );
1230
1231 if ($api_response) {
1232 $data = WebTotemAPI::getIpLists($host['id']);
1233
1234 $build[] = [
1235 'variables' => [
1236 "list" => WebTotem::getIpList($data['whiteList'], 'ip_allow'),
1237 ],
1238 'template' => 'allow_deny_list',
1239 ];
1240
1241 $response['content'] = $template->arrayRender($build);
1242 $response['wrap'] = '#wtotem_ip_allow_list';
1243 }
1244 break;
1245
1246 case 'ip_deny':
1247 $api_response = WebTotemAPI::removeIpFromList( WebTotemRequest::post('id') );
1248
1249 if ($api_response) {
1250 $data = WebTotemAPI::getIpLists($host['id']);
1251
1252 $build[] = [
1253 'variables' => [
1254 "list" => WebTotem::getIpList($data['blackList'], 'ip_deny'),
1255 ],
1256 'template' => 'allow_deny_list',
1257 ];
1258
1259 $response['content'] = $template->arrayRender($build);
1260 $response['wrap'] = '#wtotem_ip_deny_list';
1261 }
1262 break;
1263
1264 case 'url_allow':
1265 $api_response = WebTotemAPI::removeUrlFromAllowList( WebTotemRequest::post('id') );
1266
1267 if ($api_response) {
1268 $data = WebTotemAPI::getAllowUrlList($host['id']);
1269
1270 $build[] = [
1271 'variables' => [
1272 "list" => WebTotem::getUrlAllowList($data),
1273 ],
1274 'template' => 'allow_url_list',
1275 ];
1276
1277 $response['content'] = $template->arrayRender($build);
1278 $response['wrap'] = '#wtotem_allow_url';
1279 }
1280 break;
1281 }
1282
1283 $response['success'] = true;
1284 $response['notifications'] = self::notifications();
1285 wp_send_json($response);
1286 }
1287
1288 /**
1289 * Request to remove site from WebTotem.
1290 *
1291 * @return void
1292 */
1293 public static function multisite() {
1294
1295 if (WebTotemRequest::post('ajax_action') !== 'multisite') {
1296 return;
1297 }
1298
1299 $action = WebTotemRequest::post('multisite_action');
1300 $template = new WebTotemTemplate();
1301
1302 switch ($action) {
1303 case 'remove_site':
1304
1305 $host_id = WebTotemRequest::post('hid');
1306 $main_host = WebTotemOption::getMainHost();
1307
1308 if($host_id == $main_host['id']){
1309 WebTotemOption::setNotification('error', __('You cannot delete the primary domain.', 'wtotem'));
1310 break;
1311 }
1312 WebTotemAPI::removeMultiSiteHost($host_id);
1313
1314 break;
1315
1316 case 'add_site':
1317
1318 $new_site = WebTotemRequest::post('site_name');
1319 WebTotemAPI::addMultiSiteNewSites([$new_site]);
1320
1321 break;
1322 }
1323
1324 $allSites = WebTotemAPI::getSites();
1325 $has_next_page = $allSites['pageInfo']['hasNextPage'];
1326
1327 WebTotemOption::setSessionOptions([
1328 'sites_cursor' => $allSites['pageInfo']['endCursor'],
1329 ]);
1330
1331 // Sites list.
1332 $build[] = [
1333 'variables' => [
1334 'sites' => WebTotem::allSitesData($allSites),
1335 'has_next_page' => $has_next_page,
1336 ],
1337 'template' => 'multisite_list'
1338 ];
1339
1340 $response['content'] = $template->arrayRender($build);
1341
1342 $response['success'] = true;
1343 $response['notifications'] = self::notifications();
1344 wp_send_json($response);
1345 }
1346
1347 /**
1348 * Request to remove site from WebTotem.
1349 *
1350 * @return void
1351 */
1352 public static function twoFactorAuth() {
1353
1354 if (WebTotemRequest::post('ajax_action') !== 'two_factor_auth') {
1355 return;
1356 }
1357
1358 $action = WebTotemRequest::post('case_action');
1359 $template = new WebTotemTemplate();
1360
1361 switch ($action) {
1362 case 'activate':
1363
1364 $g = new GoogleAuthenticator();
1365
1366 $user = wp_get_current_user();
1367 $secret = WebTotemRequest::post('secret');
1368 $recovery = WebTotemRequest::post('recovery');
1369 $code = WebTotemRequest::post('code');
1370
1371 if($g->checkCode($secret, $code)){
1372 WebTotemLogin::saveData($user->ID, $recovery, $secret);
1373 $response['success'] = true;
1374 } else {
1375 WebTotemOption::setNotification('error', 'You have entered an incorrect activation code.');
1376 $response['success'] = false;
1377 }
1378
1379 break;
1380
1381 case 'deactivate':
1382
1383 $user = wp_get_current_user();
1384 WebTotemLogin::delete($user->ID);
1385
1386 $response['success'] = true;
1387
1388 break;
1389
1390 }
1391
1392 $build[] = [
1393 'variables' => [
1394 'two_factor' => WebTotemLogin::getTwoFactorData(),
1395 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
1396 ],
1397 'template' => 'two_factor_auth'
1398 ];
1399
1400 $response['content'] = $template->arrayRender($build);
1401
1402 $response['notifications'] = self::notifications();
1403 wp_send_json($response);
1404 }
1405
1406 /**
1407 * Changing the theme mode.
1408 *
1409 * @return void
1410 */
1411 public static function changeThemeMode() {
1412
1413 if (WebTotemRequest::post('ajax_action') !== 'theme_mode') {
1414 return;
1415 }
1416
1417 $theme_mode = WebTotemOption::getSessionOption('theme_mode');
1418
1419 if ($theme_mode == 'dark') {
1420 WebTotemOption::setSessionOptions(['theme_mode' => 'light']);
1421 $response = 'light';
1422 }
1423 else {
1424 WebTotemOption::setSessionOptions(['theme_mode' => 'dark']);
1425 $response = 'dark';
1426 }
1427
1428 wp_send_json($response);
1429 }
1430
1431 /**
1432 * Set user time zone offset.
1433 *
1434 * @return void
1435 */
1436 public static function userTimeZone() {
1437
1438 if (WebTotemRequest::post('ajax_action') !== 'set_time_zone') {
1439 return;
1440 }
1441
1442 $time_zone_offset = WebTotemRequest::post('offset');
1443 $now = strtotime('now');
1444 $check = WebTotemOption::getOption('time_zone_check') ?: 0;
1445
1446 // Checking whether an hour has elapsed since the previous request.
1447 if ($now >= $check) {
1448 $time_zone = WebTotemAPI::getTimeZone();
1449 if ($time_zone) {
1450 $time_zone_offset = timezone_offset_get(new \DateTimeZone($time_zone), new \DateTime('now', new \DateTimeZone('Europe/London'))) / 3600;
1451 WebTotemOption::setOptions(['time_zone_check' => $now + 3600]);
1452 }
1453 WebTotemOption::setOptions(['time_zone_offset' => $time_zone_offset]);
1454 }
1455
1456 wp_send_json([
1457 'success' => true,
1458 'time_zone_offset' => $time_zone_offset
1459 ]);
1460
1461 }
1462
1463 /**
1464 * Updating the page data in the specified time interval.
1465 *
1466 * @return void
1467 */
1468 public static function reloadPage() {
1469
1470 if (WebTotemRequest::post('ajax_action') !== 'reload_page') {
1471 return;
1472 }
1473
1474 $page = WebTotemRequest::post('page');
1475
1476 $template = new WebTotemTemplate();
1477
1478 // Get data from WebTotem API.
1479 $host = WebTotemAPI::siteInfo();
1480
1481 switch ($page) {
1482 case 'dashboard':
1483
1484 $data = WebTotemAPI::getAllData($host['id']);
1485
1486 // Start build array for rendering.
1487 // Scoring block.
1488 $service_data = $data['scoring']['result'];
1489 $total_score = round($data['scoring']['score']);
1490 $score_grading = WebTotem::scoreGrading($total_score);
1491 $build['scoring'] = [
1492 'variables' => [
1493 "host_id" => $host['id'],
1494 "total_score" => $total_score . "%",
1495 "tested_on" => WebTotem::dateFormatter($data['scoring']['lastTest']['time']),
1496 "server_ip" => $service_data['ip'] ?: ' - ',
1497 "location" => WebTotem::getCountryName($service_data['country']) ?: ' - ',
1498 "is_higher_than" => $service_data['isHigherThan'] . '%',
1499 "grade" => $score_grading['grade'],
1500 "color" => $score_grading['color'],
1501 ],
1502 'template' => 'score',
1503 ];
1504
1505 // Firewall stats.
1506 $period = WebTotemOption::getSessionOption('firewall_period');
1507 $service_data = $period ? WebTotemAPI::getFirewall($host['id'], 10, NULL, $period) : $data;
1508 $service_data = $service_data['firewall'];
1509
1510 $chart = WebTotem::generateWafChart($service_data['chart']);
1511 $build['firewall_stats'] = [
1512 'variables' => [
1513 "is_waf_training" => $data['agentManager'] && WebTotem::isWafTraining( $data['agentManager']['createdAt'] ),
1514 "most_attacks" => WebTotem::getMostAttacksData($service_data['map']),
1515 "all_attacks" => $chart['count_attacks'],
1516 "blocking" => $chart['count_blocks'],
1517 "not_blocking" => (int) $chart['count_attacks'] - (int) $chart['count_blocks'],
1518 ],
1519 'template' => 'firewall_stats',
1520 ];
1521
1522 $build['chart_periods'] = [
1523 'variables' => [
1524 "service" => 'waf',
1525 "days" => is_array($period) ? 7 : $period,
1526 ],
1527 'template' => 'chart_periods',
1528 ];
1529
1530 // Firewall blocks.
1531 $build['firewall_data'] = [
1532 'variables' => [
1533 "chart" => $chart['chart'],
1534 "days" => $chart['days'],
1535 "logs" => WebTotem::wafLogs($service_data['logs']['edges']),
1536 ],
1537 'template' => 'firewall',
1538 ];
1539
1540 // Server Status RAM.
1541 $period = WebTotemOption::getSessionOption('ram_period') ?: 7;
1542 $service_data = $period ? WebTotemAPI::getServerStatusData($host['id'], $period) : $data['serverStatus'];
1543
1544 $build['server_status_ram'] = [
1545 'variables' => [
1546 "info" => $service_data['info'],
1547 "ram_chart" => WebTotem::generateChart($service_data['ramChart']),
1548 "days" => $period,
1549 ],
1550 'template' => 'server_status_ram',
1551 ];
1552
1553 // Server Status CPU.
1554 $period = WebTotemOption::getSessionOption('cpu_period') ?: 7;
1555 $service_data = $period ? WebTotemAPI::getServerStatusData($host['id'], $period) : $data['serverStatus'];
1556 $build['server_status_cpu'] = [
1557 'variables' => [
1558 "cpu_chart" => WebTotem::generateChart($service_data['cpuChart']),
1559 "days" => $period,
1560 ],
1561
1562 'template' => 'server_status_cpu',
1563 ];
1564
1565 // Antivirus stats blocks.
1566 $antivirus_stats = $data['antivirus']['stats'];
1567 $build['antivirus_stats'] = [
1568 'variables' => [
1569 "changes" => $antivirus_stats['changed'] ?: 0,
1570 "scanned" => $antivirus_stats['scanned'] ?: 0,
1571 "deleted" => $antivirus_stats['deleted'] ?: 0,
1572 "infected" => $antivirus_stats["infected"] ?: 0,
1573 ],
1574
1575 'template' => 'antivirus_stats',
1576 ];
1577
1578 // Monitoring blocks.
1579 $build['monitoring'] = [
1580 'variables' => [
1581 "ssl" => [
1582 'status' => WebTotem::getStatusData($data['ssl']['status']),
1583 'days_left' => WebTotem::daysLeft($data['ssl']['expiryDate']),
1584 'issue_date' => WebTotem::dateFormatter($data['ssl']['issueDate']),
1585 'expiry_date' => WebTotem::dateFormatter($data['ssl']['expiryDate']),
1586 ],
1587 "availability" => [
1588 'status' => WebTotem::getStatusData($data['availability']['status']),
1589 "percent" => $data['availability']['percent'],
1590 "response_time" => ceil($data['availability']['responseTime'] / 1000000) . ' ' . __('ms.', 'wtotem'),
1591 "downtime" => ceil($data['availability']['downTime'] / 1000000) . ' ' . __('ms.', 'wtotem'),
1592 "last_test" => WebTotem::dateFormatter($data['availability']['lastTest']['time']),
1593 ],
1594 'reputation' => [
1595 "status" => WebTotem::getStatusData($data['reputation']['status']),
1596 "blacklists_entries" => WebTotem::blacklistsEntries(
1597 $data['reputation']['status'],
1598 $data['reputation']['virusList']),
1599 "info" => WebTotem::getReputationInfo($data['reputation']['status']),
1600 "last_test" => WebTotem::dateFormatter($data['reputation']['lastTest']['time']),
1601 ],
1602 ],
1603 'template' => 'monitoring',
1604 ];
1605
1606 // Scanning blocks.
1607 $disc_usage_data = $data['serverStatus']['discUsage'];
1608 $disc_usage = [
1609 'total' => $disc_usage_data['total'],
1610 'free' => $disc_usage_data['free'],
1611 'used' => $disc_usage_data['total'] - $disc_usage_data['free'],
1612 ];
1613
1614 $build['scanning'] = [
1615 'variables' => [
1616 "ports" => [
1617 'status' => WebTotem::getStatusData($data['ports']['status']),
1618 "ip" => $data['ports']['ip'],
1619 "number" => count($data['ports']['tcp']),
1620 "tcp" => $data['ports']['tcp'],
1621 "ignore_ports" => $data['ports']['ignorePorts'],
1622 "last_test" => WebTotem::dateFormatter($data['ports']['lastTest']['time']),
1623 ],
1624 "deface" => [
1625 'status' => WebTotem::getStatusData($data['deface']['status']),
1626 "number" => $data['deface']['count'],
1627 "words" => !empty($data['deface']['words']) ? implode(",", $data['deface']['words']) : '',
1628 "last_test" => WebTotem::dateFormatter($data['deface']['lastTest']['time']),
1629 ],
1630 "disc_usage" => $disc_usage,
1631 "disc_chart" => json_encode($disc_usage),
1632 ],
1633 'template' => 'scanning',
1634 ];
1635
1636 $response['content'][] = ['selector' => '#scoring', 'content' => $template->arrayRender($build['scoring'])];
1637 $response['content'][] = ['selector' => '#firewall_stats', 'content' => $template->arrayRender($build['firewall_stats'])];
1638 $response['content'][] = ['selector' => '#waf_chart_period', 'content' => $template->arrayRender($build['chart_periods'])];
1639 $response['content'][] = ['selector' => '#firewall_data', 'content' => $template->arrayRender($build['firewall_data'])];
1640 $response['content'][] = ['selector' => '#server_status_cpu', 'content' => $template->arrayRender($build['server_status_cpu'])];
1641 $response['content'][] = ['selector' => '#server_status_ram', 'content' => $template->arrayRender($build['server_status_ram'])];
1642 $response['content'][] = ['selector' => '#antivirus_stats', 'content' => $template->arrayRender($build['antivirus_stats'])];
1643 $response['content'][] = ['selector' => '#monitoring', 'content' => $template->arrayRender($build['monitoring'])];
1644 $response['content'][] = ['selector' => '#scanning', 'content' => $template->arrayRender($build['scanning'])];
1645
1646 break;
1647 }
1648
1649 $response['success'] = true;
1650 $response['notifications'] = self::notifications();
1651 wp_send_json($response);
1652 }
1653
1654
1655 public static function authenticate() {
1656
1657 if (WebTotemRequest::post('ajax_action') !== 'authenticate') {
1658 return;
1659 }
1660
1661 $credentials = array(
1662 'log' => 'pwd',
1663 'username' => 'password'
1664 );
1665 $username = null;
1666 $password = null;
1667 foreach ($credentials as $usernameKey => $passwordKey) {
1668 if (array_key_exists($usernameKey, $_POST) &&
1669 array_key_exists($passwordKey, $_POST) &&
1670 is_string($_POST[$usernameKey]) &&
1671 is_string($_POST[$passwordKey])) {
1672 $username = $_POST[$usernameKey];
1673 $password = $_POST[$passwordKey];
1674 break;
1675 }
1676 }
1677 if (empty($username) || empty($password)) {
1678 $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())));
1679 }
1680
1681 do_action_ref_array('wp_authenticate', array(&$username, &$password));
1682
1683 $user = wp_authenticate($username, $password);
1684 $user = WebTotemBFProtection::checkBruteForceAttempts($user);
1685
1686 if (is_object($user) && ($user instanceof \WP_User)) {
1687
1688 $response['login'] = true;
1689
1690 if(WebTotemLogin::hasUser2faActivated($user)){
1691
1692 $template = new WebTotemTemplate();
1693
1694 $response['2fa'] = true;
1695 $response['content'] = $template->getHtml( 'login_auth_form' );
1696
1697 }
1698 } else if (is_wp_error($user)) {
1699 $errors = array();
1700 foreach ($user->get_error_codes() as $code) {
1701 if ($code == 'invalid_username' || $code == 'invalid_email' || $code == 'incorrect_password' || $code == 'authentication_failed') {
1702 $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())));
1703 }
1704 else {
1705 foreach ($user->get_error_messages($code) as $error_message) {
1706 $errors[] = $error_message;
1707 }
1708 }
1709 }
1710
1711 if (!empty($errors)) {
1712 $errors = implode('<br>', $errors);
1713 $response['error'] = apply_filters('login_errors', $errors);
1714 }
1715
1716 }
1717
1718 wp_send_json($response);
1719 }
1720
1721 /**
1722 * Notification output.
1723 *
1724 * @return string
1725 */
1726 public static function notifications() {
1727
1728 $notifications = WebTotem::getNotifications();
1729
1730 if($notifications){
1731 $build[] = [
1732 'variables' => [
1733 'notifications' => $notifications,
1734 ],
1735
1736 'template' => 'notifications',
1737 ];
1738
1739 $template = new WebTotemTemplate();
1740 return $template->arrayRender($build);
1741 }
1742 return false;
1743
1744 }
1745
1746
1747 }
1748