PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / model / audit-log.php

audit-log.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at src/model/audit-log.php

274 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WP_Defender\Model;
4
5 use WP_Defender\DB;
6
7 class Audit_Log extends DB {
8 public const EVENT_TYPE_USER = 'user', EVENT_TYPE_SYSTEM = 'system', EVENT_TYPE_COMMENT = 'comment',
9 EVENT_TYPE_MEDIA = 'media', EVENT_TYPE_SETTINGS = 'settings', EVENT_TYPE_CONTENT = 'content',
10 EVENT_TYPE_MENU = 'menu';
11
12 protected $table = 'defender_audit_log';
13
14 /**
15 * @var int
16 * @defender_property
17 */
18 public $id;
19 /**
20 * @var int
21 * @defender_property
22 */
23 public $timestamp;
24 /**
25 * @var string
26 * @defender_property
27 */
28 public $event_type;
29 /**
30 * @var string
31 * @defender_property
32 */
33 public $action_type;
34 /**
35 * @var string
36 * @defender_property
37 */
38 public $site_url;
39 /**
40 * @var int
41 * @defender_property
42 */
43 public $user_id;
44 /**
45 * @var string
46 * @defender_property
47 */
48 public $context;
49 /**
50 * @var string
51 * @defender_property
52 */
53 public $ip;
54 /**
55 * @var string
56 * @defender_property
57 */
58 public $msg;
59 /**
60 * @var int
61 * @defender_property
62 */
63 public $blog_id;
64 /**
65 * @var bool
66 * @defender_property
67 */
68 public $synced;
69 /**
70 * @var int
71 * @defender_property
72 */
73 public $ttl;
74
75 public $safe = array(
76 'id',
77 'timestamp',
78 'event_type',
79 'action_type',
80 'site_url',
81 'user_id',
82 'context',
83 'ip',
84 'msg',
85 'blog_id',
86 'synced',
87 'ttl',
88 );
89
90 /**
91 * Truncate the table, as this is mostly use for cache, when we fetch new data, old data should be removed for consistent sync with API side.
92 */
93 public static function truncate() {
94 $orm = self::get_orm();
95 $orm->get_repository( self::class )->truncate();
96 }
97
98 /**
99 * Get the very last item, mostly use for testing.
100 *
101 * @return self|null
102 */
103 public static function get_last() {
104 $orm = self::get_orm();
105
106 return $orm->get_repository( self::class )
107 ->where( 'synced', 'in', array( 0, 1 ) )
108 ->order_by( 'id', 'desc' )
109 ->first();
110 }
111
112 /**
113 * Sometimes we need the pre of last, for testing.
114 *
115 * @return self
116 */
117 public static function get_pre_last() {
118 $orm = self::get_orm();
119 $model = $orm->get_repository( self::class )
120 ->where( 'synced', 'in', array( 0, 1 ) )
121 ->order_by( 'id', 'desc' )
122 ->limit( '0,2' )
123 ->get();
124
125 return array_pop( $model );
126 }
127
128 /**
129 * @return self[]
130 */
131 public static function get_logs_need_flush() {
132 $orm = self::get_orm();
133
134 return $orm->get_repository( self::class )->where( 'synced', 0 )->limit( 50 )->get();
135 }
136
137 /**
138 * Query logs from internal cache.
139 *
140 * @param int $date_from The start date we want to query, in timestamp format.
141 * @param int $date_to The date end for the query, in timestamp format.
142 * @param array $events Type of the event, e.g. comment, user, system.
143 * @param string $user_id Who trigger this event, if it 0, will be guest.
144 * @param string $ip IP of who trigger this.
145 * @param int|bool $paged Current page.
146 *
147 * @return array
148 */
149 public static function query( $date_from, $date_to, $events = array(), $user_id = '', $ip = '', $paged = 1 ) {
150 $orm = self::get_orm();
151 $builder = $orm->get_repository( self::class );
152 $builder->where( 'timestamp', '>=', $date_from )
153 ->where( 'timestamp', '<=', $date_to );
154
155 if ( is_array( $events )
156 && count( $events )
157 && count( array_diff( $events, self::allowed_events() ) ) === 0 ) {
158 $builder->where( 'event_type', 'in', $events );
159 }
160
161 if ( ! empty( $user_id ) ) {
162 $builder->where( 'user_id', $user_id );
163 }
164
165 if ( ! empty( $ip ) ) {
166 $builder->where( 'ip', 'like', "%$ip%" );
167 }
168 $builder->order_by( 'timestamp', 'desc' );
169
170 if ( false !== $paged ) {
171 // If paged == false, then it will be no paging.
172 $per_page = 20;
173 $offset = ( ( $paged - 1 ) * $per_page ) . ',' . $per_page;
174 $builder->limit( $offset );
175 }
176
177 return $builder->get();
178 }
179
180 /**
181 * Clean up the old logs depending on the storage settings.
182 *
183 * @param $date_from The start date we want to query, in timestamp format.
184 * @param $date_to The date end for the query, in timestamp format.
185 * @param int|null $limit Limit.
186 *
187 * @return void
188 */
189 public static function delete_old_logs( $date_from, $date_to, $limit = null ) {
190 $orm = self::get_orm();
191 $builder = $orm->get_repository( self::class );
192 $builder->where( 'timestamp', '>=', $date_from )
193 ->where( 'timestamp', '<=', $date_to );
194
195 if ( empty( $limit ) ) {
196 $builder->delete_all();
197 } else {
198 $builder->order_by( 'id' )
199 ->limit( $limit )
200 ->delete_by_limit();
201 }
202 }
203
204 /**
205 * This similar to @query, but we count the total row.
206 *
207 * @param $date_from
208 * @param $date_to
209 * @param array $events
210 * @param string $user_id
211 * @param string $ip
212 */
213 public static function count( $date_from, $date_to, $events = array(), $user_id = '', $ip = '' ) {
214 $orm = self::get_orm();
215 $builder = $orm->get_repository( self::class );
216 $builder->where( 'timestamp', '>=', $date_from )
217 ->where( 'timestamp', '<=', $date_to );
218 if ( count( $events ) && count( array_diff( $events, self::allowed_events() ) ) === 0 ) {
219 $builder->where( 'event_type', 'in', $events );
220 }
221
222 if ( ! empty( $user_id ) ) {
223 $builder->where( 'user_id', $user_id );
224 }
225
226 if ( ! empty( $ip ) ) {
227 $builder->where( 'ip', 'like', "%$ip%" );
228 }
229
230 return $builder->count();
231 }
232
233 /**
234 * Mass insert logs, usually where we fetched from API.
235 *
236 * @param $data
237 *
238 * @throws \ReflectionException
239 */
240 public static function mass_insert( $data ) {
241 // Todo: use raw sql for faster.
242 foreach ( $data as $datum ) {
243 $item = new Audit_Log();
244 $item->import( $datum );
245 $item->synced = 1;
246 $item->save();
247 }
248 }
249
250 public static function get_by_id( $id ) {
251 $orm = self::get_orm();
252
253 return $orm->get_repository( self::class )->find_by_id( $id );
254 }
255
256 /**
257 * Get allowed types.
258 *
259 * @return array
260 */
261 public static function allowed_events() {
262
263 return array(
264 self::EVENT_TYPE_USER,
265 self::EVENT_TYPE_SYSTEM,
266 self::EVENT_TYPE_COMMENT,
267 self::EVENT_TYPE_MEDIA,
268 self::EVENT_TYPE_SETTINGS,
269 self::EVENT_TYPE_CONTENT,
270 self::EVENT_TYPE_MENU,
271 );
272 }
273 }
274