PluginProbe
WebTotem Security / 2.4.15
WebTotem Security v2.4.15
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.15, at lib/Ajax.php

1,747 lines 69.9 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 WebTotemOption::setNotification('warning', __('Please make sure that no other recaptcha is used on your site. Otherwise, there may be a conflict that will cause problems when logging into the admin panel.', 'wtotem'));
1013
1014 $response['success'] = true;
1015
1016
1017 break;
1018
1019 case 'other_settings':
1020
1021 $settings = [
1022 'hide_wp_version' => filter_var(WebTotemRequest::post('hide_wp_version'), FILTER_VALIDATE_BOOLEAN) ?: false,
1023 ];
1024
1025 if($settings['hide_wp_version']){
1026 WebTotemOption::hideReadme();
1027
1028 } else {
1029 WebTotemOption::restoreReadme();
1030 }
1031
1032 WebTotemOption::setPluginSettings($settings);
1033
1034 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1035
1036 $response['success'] = true;
1037
1038 break;
1039
1040 case 'bruteforce_protection_settings':
1041
1042 $data = WebTotemRequest::post('data');
1043 $response['success'] = true;
1044
1045 $login_attempts = filter_var($data['login_attempts'], FILTER_VALIDATE_BOOLEAN) ?: false;
1046 $password_reset = filter_var($data['password_reset'], FILTER_VALIDATE_BOOLEAN) ?: false;
1047
1048 if($login_attempts){
1049 $response['errors'] = ['login_number_of_attempts' => '', 'login_minutes_of_ban' => ''];
1050
1051 if(empty($data['login_number_of_attempts']) or empty($data['login_minutes_of_ban'])){
1052 $response['success'] = false;
1053
1054 if(empty($data['login_number_of_attempts'])){
1055 $response['errors']['login_number_of_attempts'] = __('The field is required.', 'wtotem');
1056 }
1057 if(empty($data['login_minutes_of_ban'])){
1058 $response['errors']['login_minutes_of_ban'] = __('The field is required.', 'wtotem');
1059 }
1060 } else if($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 } else if($data['password_reset_number_of_attempts'] <= 0 or $data['password_reset_number_of_attempts'] > 1000000) {
1080 $response['success'] = false;
1081 $response['errors']['password_reset_number_of_attempts'] = __('Please specify a value from 1 to 1000000.', 'wtotem');
1082 }
1083 }
1084 if(!$response['success']){
1085 break;
1086 } else {
1087 $response['errors'] = false;
1088 }
1089
1090 $settings = [
1091 'login_attempts' => $login_attempts,
1092 'password_reset' => $password_reset,
1093 ];
1094
1095 if($login_attempts){
1096 $settings['login_number_of_attempts'] = $data['login_number_of_attempts'];
1097 $settings['login_minutes_of_ban'] = $data['login_minutes_of_ban'];
1098 }
1099 if($password_reset){
1100 $settings['password_reset_number_of_attempts'] = $data['password_reset_number_of_attempts'];
1101 $settings['password_reset_minutes_of_ban'] = $data['password_reset_minutes_of_ban'];
1102 }
1103
1104 WebTotemOption::setPluginSettings($settings);
1105
1106 WebTotemOption::setNotification('success', __('Your changes have been applied successfully.', 'wtotem'));
1107
1108 break;
1109
1110 case 'add_allow_ip':
1111 $api_response = WebTotemAPI::addIpToList($host['id'], WebTotemRequest::post('value'), 'white');
1112 if ($api_response) {
1113 $data = WebTotemAPI::getIpLists($host['id']);
1114 WebTotemCache::setData(['getIpLists' => $data], $host['id']);
1115 $build[] = [
1116 'variables' => [
1117 "list" => WebTotem::getIpList($data['whiteList'], 'ip_allow'),
1118 ],
1119 'template' => 'allow_deny_list',
1120 ];
1121
1122 $response['content'] = $template->arrayRender($build);
1123 }
1124
1125 $response['success'] = true;
1126 break;
1127
1128 case 'add_deny_ip':
1129 $api_response = WebTotemAPI::addIpToList($host['id'], WebTotemRequest::post('value'), 'black');
1130 if ($api_response) {
1131 $data = WebTotemAPI::getIpLists($host['id']);
1132 WebTotemCache::setData(['getIpLists' => $data], $host['id']);
1133 $build[] = [
1134 'variables' => [
1135 "list" => WebTotem::getIpList($data['blackList'], 'ip_deny'),
1136 ],
1137 'template' => 'allow_deny_list',
1138 ];
1139
1140 $response['content'] = $template->arrayRender($build);
1141 }
1142
1143 $response['success'] = true;
1144 break;
1145
1146 case 'add_allow_url':
1147 $api_response = WebTotemAPI::addUrlToAllowList($host['id'], WebTotemRequest::post('value'));
1148 if ($api_response) {
1149 $data = WebTotemAPI::getAllowUrlList($host['id']);
1150 $build[] = [
1151 'variables' => [
1152 "list" => WebTotem::getUrlAllowList($data),
1153 ],
1154 'template' => 'allow_url_list',
1155 ];
1156
1157 $response['content'] = $template->arrayRender($build);
1158 }
1159
1160 $response['success'] = true;
1161 break;
1162
1163 case 'add_ip_list':
1164 $ips = WebTotemRequest::post('ips');
1165 $list_name = WebTotemRequest::post('list');
1166
1167 $host = WebTotemAPI::siteInfo();
1168 $api_response = WebTotemAPI::addIpToList($host['id'], $ips, $list_name);
1169
1170 if ($api_response) {
1171 $data = WebTotemAPI::getIpLists($host['id']);
1172
1173 $data_list = ($list_name == 'white') ? $data['whiteList'] : $data['blackList'];
1174 $ip_list = ($list_name == 'white') ? 'ip_allow' : 'ip_deny';
1175
1176 $build[] = [
1177 'variables' => [
1178 "list" => WebTotem::getIpList($data_list, $ip_list),
1179 ],
1180 'template' => 'allow_deny_list',
1181 ];
1182
1183 if ($api_response['status'] != 0) {
1184 $response['invalidIPs'] = implode("\n", $api_response['invalidIPs']);
1185 }
1186
1187 $response['wrap'] = ($list_name == 'white') ? '#wtotem_ip_allow_list' : '#wtotem_ip_deny_list';
1188 $response['content'] = $template->arrayRender($build);
1189 }
1190 $response['success'] = true;
1191
1192 break;
1193 }
1194
1195 $response['notifications'] = self::notifications();
1196 wp_send_json($response);
1197 }
1198
1199 /**
1200 * Request to remove from the list of deny/allowed ip or url addresses.
1201 *
1202 * @return void
1203 */
1204 public static function remove() {
1205
1206 if (WebTotemRequest::post('ajax_action') !== 'remove') {
1207 return;
1208 }
1209
1210 $av_installed = WebTotemOption::getOption('av_installed');
1211 $waf_installed = WebTotemOption::getOption('waf_installed');
1212
1213 if(!$av_installed && !$waf_installed) {
1214 WebTotemOption::setNotification('warning', __('It is not possible to make changes because the agents are not installed.', 'wtotem'));
1215
1216 wp_send_json([
1217 'success' => false,
1218 'notifications' => self::notifications()
1219 ]);
1220 }
1221
1222 $action = WebTotemRequest::post('remove_action');
1223 $host = WebTotemAPI::siteInfo();
1224 $template = new WebTotemTemplate();
1225
1226 switch ($action) {
1227 case 'ip_allow':
1228 $api_response = WebTotemAPI::removeIpFromList( WebTotemRequest::post('id') );
1229
1230 if ($api_response) {
1231 $data = WebTotemAPI::getIpLists($host['id']);
1232
1233 $build[] = [
1234 'variables' => [
1235 "list" => WebTotem::getIpList($data['whiteList'], 'ip_allow'),
1236 ],
1237 'template' => 'allow_deny_list',
1238 ];
1239
1240 $response['content'] = $template->arrayRender($build);
1241 $response['wrap'] = '#wtotem_ip_allow_list';
1242 }
1243 break;
1244
1245 case 'ip_deny':
1246 $api_response = WebTotemAPI::removeIpFromList( WebTotemRequest::post('id') );
1247
1248 if ($api_response) {
1249 $data = WebTotemAPI::getIpLists($host['id']);
1250
1251 $build[] = [
1252 'variables' => [
1253 "list" => WebTotem::getIpList($data['blackList'], 'ip_deny'),
1254 ],
1255 'template' => 'allow_deny_list',
1256 ];
1257
1258 $response['content'] = $template->arrayRender($build);
1259 $response['wrap'] = '#wtotem_ip_deny_list';
1260 }
1261 break;
1262
1263 case 'url_allow':
1264 $api_response = WebTotemAPI::removeUrlFromAllowList( WebTotemRequest::post('id') );
1265
1266 if ($api_response) {
1267 $data = WebTotemAPI::getAllowUrlList($host['id']);
1268
1269 $build[] = [
1270 'variables' => [
1271 "list" => WebTotem::getUrlAllowList($data),
1272 ],
1273 'template' => 'allow_url_list',
1274 ];
1275
1276 $response['content'] = $template->arrayRender($build);
1277 $response['wrap'] = '#wtotem_allow_url';
1278 }
1279 break;
1280 }
1281
1282 $response['success'] = true;
1283 $response['notifications'] = self::notifications();
1284 wp_send_json($response);
1285 }
1286
1287 /**
1288 * Request to remove site from WebTotem.
1289 *
1290 * @return void
1291 */
1292 public static function multisite() {
1293
1294 if (WebTotemRequest::post('ajax_action') !== 'multisite') {
1295 return;
1296 }
1297
1298 $action = WebTotemRequest::post('multisite_action');
1299 $template = new WebTotemTemplate();
1300
1301 switch ($action) {
1302 case 'remove_site':
1303
1304 $host_id = WebTotemRequest::post('hid');
1305 $main_host = WebTotemOption::getMainHost();
1306
1307 if($host_id == $main_host['id']){
1308 WebTotemOption::setNotification('error', __('You cannot delete the primary domain.', 'wtotem'));
1309 break;
1310 }
1311 WebTotemAPI::removeMultiSiteHost($host_id);
1312
1313 break;
1314
1315 case 'add_site':
1316
1317 $new_site = WebTotemRequest::post('site_name');
1318 WebTotemAPI::addMultiSiteNewSites([$new_site]);
1319
1320 break;
1321 }
1322
1323 $allSites = WebTotemAPI::getSites();
1324 $has_next_page = $allSites['pageInfo']['hasNextPage'];
1325
1326 WebTotemOption::setSessionOptions([
1327 'sites_cursor' => $allSites['pageInfo']['endCursor'],
1328 ]);
1329
1330 // Sites list.
1331 $build[] = [
1332 'variables' => [
1333 'sites' => WebTotem::allSitesData($allSites),
1334 'has_next_page' => $has_next_page,
1335 ],
1336 'template' => 'multisite_list'
1337 ];
1338
1339 $response['content'] = $template->arrayRender($build);
1340
1341 $response['success'] = true;
1342 $response['notifications'] = self::notifications();
1343 wp_send_json($response);
1344 }
1345
1346 /**
1347 * Request to remove site from WebTotem.
1348 *
1349 * @return void
1350 */
1351 public static function twoFactorAuth() {
1352
1353 if (WebTotemRequest::post('ajax_action') !== 'two_factor_auth') {
1354 return;
1355 }
1356
1357 $action = WebTotemRequest::post('case_action');
1358 $template = new WebTotemTemplate();
1359
1360 switch ($action) {
1361 case 'activate':
1362
1363 $g = new WebTotemGoogleAuthenticator();
1364
1365 $user = wp_get_current_user();
1366 $secret = WebTotemRequest::post('secret');
1367 $recovery = WebTotemRequest::post('recovery');
1368 $code = WebTotemRequest::post('code');
1369
1370 if($g->checkCode($secret, $code)){
1371 WebTotemLogin::saveData($user->ID, $recovery, $secret);
1372 $response['success'] = true;
1373 } else {
1374 WebTotemOption::setNotification('error', 'You have entered an incorrect activation code.');
1375 $response['success'] = false;
1376 }
1377
1378 break;
1379
1380 case 'deactivate':
1381
1382 $user = wp_get_current_user();
1383 WebTotemLogin::delete($user->ID);
1384
1385 $response['success'] = true;
1386
1387 break;
1388
1389 }
1390
1391 $build[] = [
1392 'variables' => [
1393 'two_factor' => WebTotemLogin::getTwoFactorData(),
1394 'page_nonce' => wp_create_nonce('wtotem_page_nonce'),
1395 ],
1396 'template' => 'two_factor_auth'
1397 ];
1398
1399 $response['content'] = $template->arrayRender($build);
1400
1401 $response['notifications'] = self::notifications();
1402 wp_send_json($response);
1403 }
1404
1405 /**
1406 * Changing the theme mode.
1407 *
1408 * @return void
1409 */
1410 public static function changeThemeMode() {
1411
1412 if (WebTotemRequest::post('ajax_action') !== 'theme_mode') {
1413 return;
1414 }
1415
1416 $theme_mode = WebTotemOption::getSessionOption('theme_mode');
1417
1418 if ($theme_mode == 'dark') {
1419 WebTotemOption::setSessionOptions(['theme_mode' => 'light']);
1420 $response = 'light';
1421 }
1422 else {
1423 WebTotemOption::setSessionOptions(['theme_mode' => 'dark']);
1424 $response = 'dark';
1425 }
1426
1427 wp_send_json($response);
1428 }
1429
1430 /**
1431 * Set user time zone offset.
1432 *
1433 * @return void
1434 */
1435 public static function userTimeZone() {
1436
1437 if (WebTotemRequest::post('ajax_action') !== 'set_time_zone') {
1438 return;
1439 }
1440
1441 $time_zone_offset = WebTotemRequest::post('offset');
1442 $now = strtotime('now');
1443 $check = WebTotemOption::getOption('time_zone_check') ?: 0;
1444
1445 // Checking whether an hour has elapsed since the previous request.
1446 if ($now >= $check) {
1447 $time_zone = WebTotemAPI::getTimeZone();
1448 if ($time_zone) {
1449 $time_zone_offset = timezone_offset_get(new \DateTimeZone($time_zone), new \DateTime('now', new \DateTimeZone('Europe/London'))) / 3600;
1450 WebTotemOption::setOptions(['time_zone_check' => $now + 3600]);
1451 }
1452 WebTotemOption::setOptions(['time_zone_offset' => $time_zone_offset]);
1453 }
1454
1455 wp_send_json([
1456 'success' => true,
1457 'time_zone_offset' => $time_zone_offset
1458 ]);
1459
1460 }
1461
1462 /**
1463 * Updating the page data in the specified time interval.
1464 *
1465 * @return void
1466 */
1467 public static function reloadPage() {
1468
1469 if (WebTotemRequest::post('ajax_action') !== 'reload_page') {
1470 return;
1471 }
1472
1473 $page = WebTotemRequest::post('page');
1474
1475 $template = new WebTotemTemplate();
1476
1477 // Get data from WebTotem API.
1478 $host = WebTotemAPI::siteInfo();
1479
1480 switch ($page) {
1481 case 'dashboard':
1482
1483 $data = WebTotemAPI::getAllData($host['id']);
1484
1485 // Start build array for rendering.
1486 // Scoring block.
1487 $service_data = $data['scoring']['result'];
1488 $total_score = round($data['scoring']['score']);
1489 $score_grading = WebTotem::scoreGrading($total_score);
1490 $build['scoring'] = [
1491 'variables' => [
1492 "host_id" => $host['id'],
1493 "total_score" => $total_score . "%",
1494 "tested_on" => WebTotem::dateFormatter($data['scoring']['lastTest']['time']),
1495 "server_ip" => $service_data['ip'] ?: ' - ',
1496 "location" => WebTotem::getCountryName($service_data['country']) ?: ' - ',
1497 "is_higher_than" => $service_data['isHigherThan'] . '%',
1498 "grade" => $score_grading['grade'],
1499 "color" => $score_grading['color'],
1500 ],
1501 'template' => 'score',
1502 ];
1503
1504 // Firewall stats.
1505 $period = WebTotemOption::getSessionOption('firewall_period');
1506 $service_data = $period ? WebTotemAPI::getFirewall($host['id'], 10, NULL, $period) : $data;
1507 $service_data = $service_data['firewall'];
1508
1509 $chart = WebTotem::generateWafChart($service_data['chart']);
1510 $build['firewall_stats'] = [
1511 'variables' => [
1512 "is_waf_training" => $data['agentManager'] && WebTotem::isWafTraining( $data['agentManager']['createdAt'] ),
1513 "most_attacks" => WebTotem::getMostAttacksData($service_data['map']),
1514 "all_attacks" => $chart['count_attacks'],
1515 "blocking" => $chart['count_blocks'],
1516 "not_blocking" => (int) $chart['count_attacks'] - (int) $chart['count_blocks'],
1517 ],
1518 'template' => 'firewall_stats',
1519 ];
1520
1521 $build['chart_periods'] = [
1522 'variables' => [
1523 "service" => 'waf',
1524 "days" => is_array($period) ? 7 : $period,
1525 ],
1526 'template' => 'chart_periods',
1527 ];
1528
1529 // Firewall blocks.
1530 $build['firewall_data'] = [
1531 'variables' => [
1532 "chart" => $chart['chart'],
1533 "days" => $chart['days'],
1534 "logs" => WebTotem::wafLogs($service_data['logs']['edges']),
1535 ],
1536 'template' => 'firewall',
1537 ];
1538
1539 // Server Status RAM.
1540 $period = WebTotemOption::getSessionOption('ram_period') ?: 7;
1541 $service_data = $period ? WebTotemAPI::getServerStatusData($host['id'], $period) : $data['serverStatus'];
1542
1543 $build['server_status_ram'] = [
1544 'variables' => [
1545 "info" => $service_data['info'],
1546 "ram_chart" => WebTotem::generateChart($service_data['ramChart']),
1547 "days" => $period,
1548 ],
1549 'template' => 'server_status_ram',
1550 ];
1551
1552 // Server Status CPU.
1553 $period = WebTotemOption::getSessionOption('cpu_period') ?: 7;
1554 $service_data = $period ? WebTotemAPI::getServerStatusData($host['id'], $period) : $data['serverStatus'];
1555 $build['server_status_cpu'] = [
1556 'variables' => [
1557 "cpu_chart" => WebTotem::generateChart($service_data['cpuChart']),
1558 "days" => $period,
1559 ],
1560
1561 'template' => 'server_status_cpu',
1562 ];
1563
1564 // Antivirus stats blocks.
1565 $antivirus_stats = $data['antivirus']['stats'];
1566 $build['antivirus_stats'] = [
1567 'variables' => [
1568 "changes" => $antivirus_stats['changed'] ?: 0,
1569 "scanned" => $antivirus_stats['scanned'] ?: 0,
1570 "deleted" => $antivirus_stats['deleted'] ?: 0,
1571 "infected" => $antivirus_stats["infected"] ?: 0,
1572 ],
1573
1574 'template' => 'antivirus_stats',
1575 ];
1576
1577 // Monitoring blocks.
1578 $build['monitoring'] = [
1579 'variables' => [
1580 "ssl" => [
1581 'status' => WebTotem::getStatusData($data['ssl']['status']),
1582 'days_left' => WebTotem::daysLeft($data['ssl']['expiryDate']),
1583 'issue_date' => WebTotem::dateFormatter($data['ssl']['issueDate']),
1584 'expiry_date' => WebTotem::dateFormatter($data['ssl']['expiryDate']),
1585 ],
1586 "availability" => [
1587 'status' => WebTotem::getStatusData($data['availability']['status']),
1588 "percent" => $data['availability']['percent'],
1589 "response_time" => ceil($data['availability']['responseTime'] / 1000000) . ' ' . __('ms.', 'wtotem'),
1590 "downtime" => ceil($data['availability']['downTime'] / 1000000) . ' ' . __('ms.', 'wtotem'),
1591 "last_test" => WebTotem::dateFormatter($data['availability']['lastTest']['time']),
1592 ],
1593 'reputation' => [
1594 "status" => WebTotem::getStatusData($data['reputation']['status']),
1595 "blacklists_entries" => WebTotem::blacklistsEntries(
1596 $data['reputation']['status'],
1597 $data['reputation']['virusList']),
1598 "info" => WebTotem::getReputationInfo($data['reputation']['status']),
1599 "last_test" => WebTotem::dateFormatter($data['reputation']['lastTest']['time']),
1600 ],
1601 ],
1602 'template' => 'monitoring',
1603 ];
1604
1605 // Scanning blocks.
1606 $disc_usage_data = $data['serverStatus']['discUsage'];
1607 $disc_usage = [
1608 'total' => $disc_usage_data['total'],
1609 'free' => $disc_usage_data['free'],
1610 'used' => $disc_usage_data['total'] - $disc_usage_data['free'],
1611 ];
1612
1613 $build['scanning'] = [
1614 'variables' => [
1615 "ports" => [
1616 'status' => WebTotem::getStatusData($data['ports']['status']),
1617 "ip" => $data['ports']['ip'],
1618 "number" => count($data['ports']['tcp']),
1619 "tcp" => $data['ports']['tcp'],
1620 "ignore_ports" => $data['ports']['ignorePorts'],
1621 "last_test" => WebTotem::dateFormatter($data['ports']['lastTest']['time']),
1622 ],
1623 "deface" => [
1624 'status' => WebTotem::getStatusData($data['deface']['status']),
1625 "number" => $data['deface']['count'],
1626 "words" => !empty($data['deface']['words']) ? implode(",", $data['deface']['words']) : '',
1627 "last_test" => WebTotem::dateFormatter($data['deface']['lastTest']['time']),
1628 ],
1629 "disc_usage" => $disc_usage,
1630 "disc_chart" => json_encode($disc_usage),
1631 ],
1632 'template' => 'scanning',
1633 ];
1634
1635 $response['content'][] = ['selector' => '#scoring', 'content' => $template->arrayRender($build['scoring'])];
1636 $response['content'][] = ['selector' => '#firewall_stats', 'content' => $template->arrayRender($build['firewall_stats'])];
1637 $response['content'][] = ['selector' => '#waf_chart_period', 'content' => $template->arrayRender($build['chart_periods'])];
1638 $response['content'][] = ['selector' => '#firewall_data', 'content' => $template->arrayRender($build['firewall_data'])];
1639 $response['content'][] = ['selector' => '#server_status_cpu', 'content' => $template->arrayRender($build['server_status_cpu'])];
1640 $response['content'][] = ['selector' => '#server_status_ram', 'content' => $template->arrayRender($build['server_status_ram'])];
1641 $response['content'][] = ['selector' => '#antivirus_stats', 'content' => $template->arrayRender($build['antivirus_stats'])];
1642 $response['content'][] = ['selector' => '#monitoring', 'content' => $template->arrayRender($build['monitoring'])];
1643 $response['content'][] = ['selector' => '#scanning', 'content' => $template->arrayRender($build['scanning'])];
1644
1645 break;
1646 }
1647
1648 $response['success'] = true;
1649 $response['notifications'] = self::notifications();
1650 wp_send_json($response);
1651 }
1652
1653
1654 public static function authenticate() {
1655
1656 if (WebTotemRequest::post('ajax_action') !== 'authenticate') {
1657 return;
1658 }
1659
1660 $credentials = array(
1661 'log' => 'pwd',
1662 'username' => 'password'
1663 );
1664 $username = null;
1665 $password = null;
1666 foreach ($credentials as $usernameKey => $passwordKey) {
1667 if (array_key_exists($usernameKey, $_POST) &&
1668 array_key_exists($passwordKey, $_POST) &&
1669 is_string($_POST[$usernameKey]) &&
1670 is_string($_POST[$passwordKey])) {
1671 $username = $_POST[$usernameKey];
1672 $password = $_POST[$passwordKey];
1673 break;
1674 }
1675 }
1676 if (empty($username) || empty($password)) {
1677 $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())));
1678 }
1679
1680 do_action_ref_array('wp_authenticate', array(&$username, &$password));
1681
1682 $user = wp_authenticate($username, $password);
1683 $user = WebTotemBFProtection::checkBruteForceAttempts($user);
1684
1685 if (is_object($user) && ($user instanceof \WP_User)) {
1686
1687 $response['login'] = true;
1688
1689 if(WebTotemLogin::hasUser2faActivated($user)){
1690
1691 $template = new WebTotemTemplate();
1692
1693 $response['2fa'] = true;
1694 $response['content'] = $template->getHtml( 'login_auth_form' );
1695
1696 }
1697 } else if (is_wp_error($user)) {
1698 $errors = array();
1699 foreach ($user->get_error_codes() as $code) {
1700 if ($code == 'invalid_username' || $code == 'invalid_email' || $code == 'incorrect_password' || $code == 'authentication_failed') {
1701 $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())));
1702 }
1703 else {
1704 foreach ($user->get_error_messages($code) as $error_message) {
1705 $errors[] = $error_message;
1706 }
1707 }
1708 }
1709
1710 if (!empty($errors)) {
1711 $errors = implode('<br>', $errors);
1712 $response['error'] = apply_filters('login_errors', $errors);
1713 }
1714
1715 }
1716
1717 wp_send_json($response);
1718 }
1719
1720 /**
1721 * Notification output.
1722 *
1723 * @return string
1724 */
1725 public static function notifications() {
1726
1727 $notifications = WebTotem::getNotifications();
1728
1729 if($notifications){
1730 $build[] = [
1731 'variables' => [
1732 'notifications' => $notifications,
1733 ],
1734
1735 'template' => 'notifications',
1736 ];
1737
1738 $template = new WebTotemTemplate();
1739 return $template->arrayRender($build);
1740 }
1741 return false;
1742
1743 }
1744
1745
1746 }
1747