| 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 group and all total. |
| 79 |
*/ |
| 80 |
public function get_types_total( $parent_id, $status ) { |
| 81 |
global $wpdb; |
| 82 |
|
| 83 |
$records = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 84 |
$wpdb->prepare( |
| 85 |
"SELECT IFNULL(`type`, 'all') as `item_type`, count(*) as `type_total` FROM {$wpdb->base_prefix}defender_scan_item WHERE `parent_id` = %d AND `status` = %s Group BY `type` WITH ROLLUP", |
| 86 |
$parent_id, |
| 87 |
$status |
| 88 |
) |
| 89 |
); |
| 90 |
|
| 91 |
$results = array(); |
| 92 |
foreach ( $records as $record ) { |
| 93 |
$results[ $record->item_type ] = (int) $record->type_total; |
| 94 |
} |
| 95 |
|
| 96 |
return $results; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Deletes a record from the database by its ID. |
| 101 |
* |
| 102 |
* @param int $id The ID of the record to be deleted. |
| 103 |
* |
| 104 |
* @return bool Returns true if the record was successfully deleted, false otherwise. |
| 105 |
*/ |
| 106 |
public function delete_by_id( int $id ): bool { |
| 107 |
$delete = self::get_orm()->get_repository( self::class ) |
| 108 |
->delete( array( 'id' => $id ) ); |
| 109 |
|
| 110 |
return is_int( $delete ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Import data into the model. |
| 115 |
* |
| 116 |
* @param mixed $data The data array to import values from. |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function import( $data ): void { |
| 121 |
parent::import( $data ); |
| 122 |
switch ( $this->type ) { |
| 123 |
// Add other behaviors here. |
| 124 |
case self::TYPE_SUSPICIOUS: |
| 125 |
$this->attach_behavior( Malware_Result::class, Malware_Result::class ); |
| 126 |
break; |
| 127 |
case self::TYPE_VULNERABILITY: |
| 128 |
$this->attach_behavior( Vuln_Result::class, Vuln_Result::class ); |
| 129 |
break; |
| 130 |
case self::TYPE_PLUGIN_CLOSED: |
| 131 |
case self::TYPE_PLUGIN_OUTDATED: |
| 132 |
$this->attach_behavior( Abandoned_Result::class, Abandoned_Result::class ); |
| 133 |
break; |
| 134 |
default: |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|