PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.4
Defender Security – Malware Scanner, Login Security & Firewall v6.2.4
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 / class-scan-item.php

class-scan-item.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.4, at src/model/class-scan-item.php

172 lines 4.2 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 for scan items.
4 *
5 * @package WP_Defender\Model
6 */
7
8 namespace WP_Defender\Model;
9
10 use WP_Defender\DB;
11 use WP_Defender\Behavior\Scan_Item\Vuln_Result;
12 use WP_Defender\Behavior\Scan_Item\Malware_Result;
13 use WP_Defender\Behavior\Scan_Item\Abandoned_Result;
14
15 /**
16 * Model for scan item table.
17 */
18 class Scan_Item extends DB {
19
20 // For 'File change detection' option.
21 public const TYPE_INTEGRITY = 'core_integrity', TYPE_PLUGIN_CHECK = 'plugin_integrity';
22 // For 'Known vulnerabilities' and 'Suspicious code' options.
23 public const TYPE_VULNERABILITY = 'vulnerability', TYPE_SUSPICIOUS = 'malware';
24 // For 'Abandoned' option.
25 public const TYPE_PLUGIN_CLOSED = 'plugin_closed', TYPE_PLUGIN_OUTDATED = 'plugin_outdated';
26 // Different statuses.
27 public const STATUS_ACTIVE = 'active', STATUS_IGNORE = 'ignore';
28
29 /**
30 * Defines the table name.
31 *
32 * @var string
33 */
34 protected $table = 'defender_scan_item';
35 /**
36 * Defines the primary key.
37 *
38 * @var int
39 * @defender_property
40 */
41 public $id;
42 /**
43 * Defines a public property for storing the parent ID.
44 *
45 * @var int
46 * @defender_property
47 */
48 public $parent_id;
49 /**
50 * Type of the issue, base on this we will load the behavior.
51 *
52 * @var string
53 * @defender_property
54 */
55 public $type;
56 /**
57 * Contain generic data.
58 *
59 * @var array
60 * @defender_property
61 */
62 public $raw_data = array();
63
64 /**
65 * Defines a public property for storing the status.
66 *
67 * @var string
68 * @defender_property
69 */
70 public $status;
71
72 /**
73 * Get the total of each type of provided status either STATUS_ACTIVE or STATUS_IGNORE.
74 *
75 * @param int $parent_id The primary key of the scan table.
76 * @param string $status Active or ignore status of scan item(s).
77 *
78 * @return array Return array of groups and all total.
79 */
80 public function get_types_total( $parent_id, $status ): array {
81 global $wpdb;
82
83 $types = self::get_all_scan_types();
84 $placeholders = implode( ', ', array_fill( 0, count( $types ), '%s' ) );
85
86 $records = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
87 $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
88 "SELECT IFNULL(`type`, 'all') as `item_type`, count(*) as `type_total`" .
89 " FROM {$wpdb->base_prefix}defender_scan_item" .
90 " WHERE `parent_id` = %d AND `status` = %s AND `type` IN ($placeholders)" . // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
91 ' Group BY `type` WITH ROLLUP',
92 array_merge( array( $parent_id, $status ), $types )
93 )
94 );
95
96 $results = array();
97 foreach ( $records as $record ) {
98 $results[ $record->item_type ] = (int) $record->type_total;
99 }
100
101 return $results;
102 }
103
104 /**
105 * Deletes a record from the database by its ID.
106 *
107 * @param int $id The ID of the record to be deleted.
108 *
109 * @return bool Returns true if the record was successfully deleted, false otherwise.
110 */
111 public function delete_by_id( int $id ): bool {
112 $delete = self::get_orm()->get_repository( self::class )
113 ->delete( array( 'id' => $id ) );
114
115 return is_int( $delete );
116 }
117
118 /**
119 * Import data into the model.
120 *
121 * @param mixed $data The data array to import values from.
122 *
123 * @return void
124 */
125 public function import( $data ): void {
126 parent::import( $data );
127 // Add behaviors.
128 switch ( $this->type ) {
129 case self::TYPE_SUSPICIOUS:
130 $this->attach_behavior( Malware_Result::class, Malware_Result::class );
131 break;
132 case self::TYPE_VULNERABILITY:
133 $this->attach_behavior( Vuln_Result::class, Vuln_Result::class );
134 break;
135 case self::TYPE_PLUGIN_CLOSED:
136 case self::TYPE_PLUGIN_OUTDATED:
137 $this->attach_behavior( Abandoned_Result::class, Abandoned_Result::class );
138 break;
139 default:
140 break;
141 }
142 }
143
144 /**
145 * Get all Scan types.
146 *
147 * @return array
148 */
149 public static function get_all_scan_types(): array {
150 return array(
151 self::TYPE_VULNERABILITY,
152 self::TYPE_SUSPICIOUS,
153 self::TYPE_INTEGRITY,
154 self::TYPE_PLUGIN_CHECK,
155 self::TYPE_PLUGIN_CLOSED,
156 self::TYPE_PLUGIN_OUTDATED,
157 );
158 }
159
160 /**
161 * Get all scan statuses.
162 *
163 * @return array
164 */
165 public static function get_all_scan_statuses(): array {
166 return array(
167 self::STATUS_ACTIVE,
168 self::STATUS_IGNORE,
169 );
170 }
171 }
172