PluginProbe
WebTotem Security / 2.4.13
WebTotem Security v2.4.13
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.13, at lib/Ajax.php

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