PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.3
Defender Security – Malware Scanner, Login Security & Firewall v6.2.3
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 3.1.1 All 140 releases
defender-security / src / model / class-quarantine.php

class-quarantine.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.3, at src/model/class-quarantine.php

348 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles interactions with the database table for quarantined files.
4 *
5 * @package WP_Defender\Model
6 */
7
8 namespace WP_Defender\Model;
9
10 use WP_Defender\DB;
11
12 /**
13 * Model for the quarantine table.
14 *
15 * @since 4.0.0
16 */
17 class Quarantine extends DB {
18
19 public const WP_UNCATEGORIZED = 0;
20 public const WP_CORE = 1;
21 public const WP_PLUGIN = 2;
22 public const WP_THEME = 3;
23 public const WP_DROPINS = 4;
24 /**
25 * Table name.
26 *
27 * @var string
28 */
29 protected $table = 'defender_quarantine';
30
31 /**
32 * Quarantine record ID.
33 *
34 * @var int
35 * @since 4.0.0
36 * @access public
37 * @defender_property
38 */
39 public $id;
40
41 /**
42 * Associated defender scan item ID.
43 *
44 * @var int
45 * @since 4.0.0
46 * @access public
47 * @defender_property
48 */
49 public $defender_scan_item_id;
50
51 /**
52 * File hash.
53 *
54 * @var string
55 * @since 4.0.0
56 * @access public
57 * @defender_property
58 */
59 public $file_hash;
60
61 /**
62 * Full path of the file.
63 *
64 * @var string
65 * @since 4.0.0
66 * @access public
67 * @defender_property
68 */
69 public $file_full_path;
70
71 /**
72 * Original name of the file.
73 *
74 * @var string
75 * @since 4.0.0
76 * @access public
77 * @defender_property
78 */
79 public $file_original_name;
80
81 /**
82 * File extension.
83 *
84 * @var string
85 * @since 4.0.0
86 * @access public
87 * @defender_property
88 */
89 public $file_extension;
90
91 /**
92 * File MIME type.
93 *
94 * @var string
95 * @since 4.0.0
96 * @access public
97 * @defender_property
98 */
99 public $file_mime_type;
100
101 /**
102 * File read/write permission.
103 *
104 * @var int
105 * @since 4.0.0
106 * @access public
107 * @defender_property
108 */
109 public $file_rw_permission;
110
111 /**
112 * File owner.
113 *
114 * @var string
115 * @since 4.0.0
116 * @access public
117 * @defender_property
118 */
119 public $file_owner;
120
121 /**
122 * File group.
123 *
124 * @var string
125 * @since 4.0.0
126 * @access public
127 * @defender_property
128 */
129 public $file_group;
130
131 /**
132 * File version.
133 *
134 * @var string
135 * @since 4.0.0
136 * @access public
137 * @defender_property
138 */
139 public $file_version;
140
141 /**
142 * File category.
143 *
144 * @var int
145 * @since 4.0.0
146 * @access public
147 * @defender_property
148 */
149 public $file_category;
150
151 /**
152 * File modified time.
153 *
154 * @var string
155 * @since 4.0.0
156 * @access public
157 * @defender_property
158 */
159 public $file_modified_time;
160
161 /**
162 * Source slug.
163 *
164 * @var string
165 * @since 4.0.0
166 * @access public
167 * @defender_property
168 */
169 public $source_slug;
170
171 /**
172 * Created time.
173 *
174 * @var string
175 * @since 4.0.0
176 * @access public
177 * @defender_property
178 */
179 public $created_time;
180
181 /**
182 * ID of the user who created the record.
183 *
184 * @var int
185 * @since 4.0.0
186 * @access public
187 * @defender_property
188 */
189 public $created_by;
190
191 /**
192 * Checks if a given scan item is quarantined.
193 *
194 * @param Scan_Item $scan_item The scan item to check.
195 *
196 * @return bool Returns true if the scan item is quarantined, false otherwise.
197 */
198 public function is_quarantined( Scan_Item $scan_item ): bool {
199 global $wpdb;
200
201 $scan_item_id = $scan_item->id;
202 $file_path = $scan_item->raw_data['file'];
203 $table = $wpdb->prefix . $this->table;
204
205 $records = (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
206 $wpdb->prepare(
207 "SELECT EXISTS( SELECT 1 FROM {$table} WHERE `defender_scan_item_id` = %d OR `file_full_path` = %s ORDER BY `created_time` DESC LIMIT 0, 1 )", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
208 $scan_item_id,
209 $file_path
210 )
211 );
212
213 return 1 === $records;
214 }
215
216 /**
217 * Deletes a record from the database based on the given ID.
218 *
219 * @param int $id The ID of the record to delete.
220 *
221 * @return bool Returns true if the record was successfully deleted, false otherwise.
222 */
223 public function delete( int $id ): bool {
224 $delete = self::get_orm()->get_repository( self::class )
225 ->delete( array( 'id' => $id ) );
226
227 return is_int( $delete );
228 }
229
230 /**
231 * Select records from the database based on the given scan item ID.
232 *
233 * @param int $scan_item_id The ID of the scan item.
234 *
235 * @return array The selected records.
236 */
237 public function select_by_scan_item_id( int $scan_item_id ): array {
238 return self::get_orm()->get_repository( self::class )->select( '' )
239 ->where( 'defender_scan_item_id', $scan_item_id )->get();
240 }
241
242 /**
243 * Select records from the database based on the given file full path.
244 *
245 * @param string $file_full_path The full path of the file.
246 *
247 * @return array The selected records.
248 */
249 public function select_by_file_full_path( string $file_full_path ): array {
250 return self::get_orm()->get_repository( self::class )->select( '' )
251 ->where( 'file_full_path', $file_full_path )->get();
252 }
253
254 /**
255 * Selects the restore detail for a given scan item.
256 *
257 * @param Scan_Item $scan_item The scan item to retrieve the restore detail for.
258 *
259 * @return mixed The restore detail record from the database.
260 */
261 public function select_restore_detail( Scan_Item $scan_item ) {
262 global $wpdb;
263
264 $scan_item_id = $scan_item->id;
265 $file_path = $scan_item->raw_data['file'];
266 $table = $wpdb->prefix . $this->table;
267
268 $records = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
269 $wpdb->prepare(
270 "SELECT * FROM {$table} WHERE `defender_scan_item_id` = %d OR `file_full_path` = %s ORDER BY `created_time` DESC LIMIT 0", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
271 $scan_item_id,
272 $file_path
273 )
274 );
275
276 return $records;
277 }
278
279 /**
280 * Retrieves a collection of quarantined files along with their metadata.
281 *
282 * @return array An array of quarantined files with their metadata.
283 */
284 public function quarantine_collection(): array {
285 global $wpdb;
286
287 return $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
288 "SELECT {$wpdb->prefix}defender_quarantine.id, file_hash, file_original_name, file_extension, source_slug, created_by, file_full_path, file_modified_time, created_time, display_name as user_display_name FROM {$wpdb->prefix}defender_quarantine LEFT JOIN $wpdb->users ON {$wpdb->prefix}defender_quarantine.created_by = $wpdb->users.id ORDER BY {$wpdb->prefix}defender_quarantine.id DESC",
289 ARRAY_A
290 );
291 }
292
293 /**
294 * Get record by primary key.
295 *
296 * @param int $id Primary key of the record.
297 *
298 * @return Quarantine|null Return Quarantine model on fetched else null.
299 */
300 public function find_by_id( int $id ) {
301 $orm = self::get_orm();
302
303 $record = $orm->get_repository( self::class )->find_by_id( $id )->get();
304
305 return isset( $record[0] ) ? $record[0] : null;
306 }
307
308 /**
309 * Get records which are created older than the $expiry_limit
310 *
311 * @param string $expiry_limit Expiry limit date time in mysql format.
312 *
313 * @return array Array of quarantined files primary key if exists else empty array.
314 */
315 public function get_old_records( string $expiry_limit ): array {
316 $orm = self::get_orm();
317 $builder = $orm->get_repository( self::class );
318
319 $records = $builder->select( 'id' )
320 ->where( 'created_time', '<=', $expiry_limit )->get_results();
321
322 return $records;
323 }
324
325 /**
326 * Drop quarantine table.
327 */
328 public function drop_table(): void {
329 global $wpdb;
330
331 $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
332 "DROP TABLE IF EXISTS {$wpdb->prefix}defender_quarantine" // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange
333 );
334 }
335
336 /**
337 * SQL to fetch array of last 5 recently quarantined files.
338 */
339 public function hub_list(): array {
340 global $wpdb;
341
342 return $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
343 "SELECT {$wpdb->prefix}defender_quarantine.id, file_original_name, file_extension, created_time as quarantined_time, file_hash as quarantined_path, file_full_path as source_path FROM {$wpdb->prefix}defender_quarantine ORDER BY quarantined_time DESC LIMIT 0, 5",
344 ARRAY_A
345 );
346 }
347 }
348