PluginProbe
FluentAuth – The Ultimate Authorization & Security Plugin for WordPress / trunk
FluentAuth – The Ultimate Authorization & Security Plugin for WordPress vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2
fluent-security / app / Http / Controllers / LogsController.php

LogsController.php in FluentAuth – The Ultimate Authorization & Security Plugin for WordPress trunk, at app/Http/Controllers/LogsController.php

129 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentAuth\App\Http\Controllers;
4
5 use FluentAuth\App\Helpers\Helper;
6
7 class LogsController
8 {
9 public static function getLogs(\WP_REST_Request $request)
10 {
11 $orderByColumn = sanitize_sql_orderby($request->get_param('sortBy')) ?: 'id';
12 $orderBy = sanitize_sql_orderby($request->get_param('sortType')) ?: 'DESC';
13
14 $query = flsDb()->table('fls_auth_logs')->orderBy($orderByColumn, $orderBy);
15
16 if ($statuses = $request->get_param('statuses')) {
17 $statuses = array_filter(map_deep($statuses, 'sanitize_text_field'));
18 if ($statuses && !in_array('all', $statuses)) {
19 $query->whereIn('status', $statuses);
20 }
21 }
22
23 if ($search = $request->get_param('search')) {
24 $search = sanitize_text_field($search);
25 $query->where(function ($q) use ($search) {
26 $q->where('username', 'LIKE', '%' . $search . '%');
27 $q->orWhere('media', 'LIKE', '%' . $search . '%');
28 return $q;
29 });
30 }
31
32 $logs = $query->paginate();
33
34 $wpTimestamp = current_time('timestamp');
35 foreach ($logs['data'] as $log) {
36 $log->human_time_diff = human_time_diff(strtotime($log->created_at), $wpTimestamp) . ' ago';
37 }
38
39 return [
40 'logs' => $logs
41 ];
42 }
43
44 public static function deleteLog(\WP_REST_Request $request)
45 {
46 $id = (int) $request->get_param('id');
47 flsDb()->table('fls_auth_logs')->where('id', $id)->delete();
48
49 return [
50 'message' => __('Log has been deleted', 'fluent-security')
51 ];
52 }
53
54 public static function deleteAllLog(\WP_REST_Request $request)
55 {
56 global $wpdb;
57
58 $wpdb->query("TRUNCATE TABLE {$wpdb->prefix}fls_auth_logs");
59
60 return [
61 'message' => __('All Logs has been deleted', 'fluent-security')
62 ];
63
64 }
65
66 public static function quickStats(\WP_REST_Request $request)
67 {
68 $fromRange = sanitize_text_field($request->get_param('day_range'));
69
70 if (!$fromRange) {
71 $fromRange = '-0 days';
72 }
73
74 $wpTimestamp = current_time('timestamp');
75
76 if ($fromRange == 'this_month') {
77 $fromDate = date('Y-m-01 00:00:00', $wpTimestamp);
78 } else if ($fromRange == 'all_time') {
79 $fromDate = '1970-01-01 00:00:00';
80 } else {
81 $fromDate = date('Y-m-d 00:00:00', strtotime($fromRange, $wpTimestamp));
82 }
83
84 $toDate = date('Y-m-d 23:59:59', $wpTimestamp);
85
86 $counts = flsDb()->table('fls_auth_logs')
87 ->select(['status', flsDb()->raw('count(*) as total')])
88 ->whereBetween('created_at', $fromDate, $toDate)
89 ->groupBy('status')
90 ->get();
91
92 $items = [
93 'failed' => [
94 'count' => 0,
95 'title' => __('Failed Logins', 'fluent-security')
96 ],
97 'blocked' => [
98 'count' => 0,
99 'title' => __('Blocked Logins', 'fluent-security')
100 ],
101 'success' => [
102 'count' => 0,
103 'title' => __('Successful Logins', 'fluent-security')
104 ]
105 ];
106
107 foreach ($counts as $countItem) {
108 if (isset($items[$countItem->status])) {
109 $items[$countItem->status]['count'] = $countItem->total;
110 }
111 }
112
113 if (Helper::getSetting('magic_login') === 'yes') {
114 $items['magic_login'] = [
115 'title' => __('Login via URL', 'fluent-security'),
116 'count' => flsDb()->table('fls_login_hashes')
117 ->where('status', 'used')
118 ->where('use_type', 'magic_login')
119 ->whereBetween('created_at', $fromDate, $toDate)
120 ->count()
121 ];
122 }
123
124 return [
125 'stats' => $items
126 ];
127 }
128 }
129