| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file contains logic for gathering facts for scan. |
| 4 |
* |
| 5 |
* @package WP_Defender\Behavior\Scan |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Behavior\Scan; |
| 9 |
|
| 10 |
use Calotes\Base\File; |
| 11 |
use WP_Defender\Traits\IO; |
| 12 |
use WP_Defender\Model\Scan; |
| 13 |
use Calotes\Base\Component; |
| 14 |
use WP_Defender\Component\Timer; |
| 15 |
use WP_Defender\Behavior\WPMUDEV; |
| 16 |
use WP_Defender\Model\Setting\Scan as Scan_Settings; |
| 17 |
use WP_Defender\Controller\Scan as Scan_Controller; |
| 18 |
|
| 19 |
/** |
| 20 |
* We will gather core files & content files, for using in core integrity. |
| 21 |
*/ |
| 22 |
class Gather_Fact extends Component { |
| 23 |
|
| 24 |
use IO; |
| 25 |
|
| 26 |
public const CACHE_CORE = 'wdfcore', CACHE_CONTENT = 'wdfcontent'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Holds the Scan model or null if not set. |
| 30 |
* |
| 31 |
* @var Scan|null |
| 32 |
*/ |
| 33 |
private ?Scan $scan; |
| 34 |
|
| 35 |
/** |
| 36 |
* Holds the WPMUDEV object or null if not set. |
| 37 |
* |
| 38 |
* @var WPMUDEV|null |
| 39 |
*/ |
| 40 |
private ?WPMUDEV $wpmudev; |
| 41 |
|
| 42 |
/** |
| 43 |
* Holds the Scan settings or null if not set. |
| 44 |
* |
| 45 |
* @var Scan_Settings|null |
| 46 |
*/ |
| 47 |
private ?Scan_Settings $settings; |
| 48 |
|
| 49 |
/** |
| 50 |
* Constructor for the Gather_Fact class. |
| 51 |
* |
| 52 |
* @param WPMUDEV $wpmudev The WPMUDEV object. |
| 53 |
* @param Scan $scan The Scan model. |
| 54 |
* @param Scan_Settings $scan_settings The Scan settings. |
| 55 |
*/ |
| 56 |
public function __construct( WPMUDEV $wpmudev, Scan $scan, Scan_Settings $scan_settings ) { |
| 57 |
$this->wpmudev = $wpmudev; |
| 58 |
$this->scan = $scan; |
| 59 |
$this->settings = $scan_settings; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Gather core files & content files. |
| 64 |
* |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
public function gather_info(): bool { |
| 68 |
$timer = new Timer(); |
| 69 |
$model = $this->scan; |
| 70 |
$need_to_run = '' === $model->task_checkpoint ? 'get_core_files' : 'get_content_files'; |
| 71 |
if ( 'get_core_files' === $need_to_run ) { |
| 72 |
if ( $this->settings->integrity_check && $this->settings->check_core ) { |
| 73 |
$this->get_core_files(); |
| 74 |
} |
| 75 |
$model->calculate_percent( 50, 1 ); |
| 76 |
} else { |
| 77 |
$this->get_content_files(); |
| 78 |
$model->calculate_percent( 100, 1 ); |
| 79 |
} |
| 80 |
$this->log( sprintf( '%s in %d', $need_to_run, $timer->get_difference() ), Scan_Controller::SCAN_LOG ); |
| 81 |
$model->task_checkpoint = $need_to_run; |
| 82 |
$model->save(); |
| 83 |
|
| 84 |
return 'get_content_files' === $need_to_run; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get core files and update the cache. |
| 89 |
* |
| 90 |
* @return array The array of core files. |
| 91 |
*/ |
| 92 |
private function get_core_files(): array { |
| 93 |
$cache = get_site_option( self::CACHE_CORE, false ); |
| 94 |
if ( is_array( $cache ) ) { |
| 95 |
return $cache; |
| 96 |
} |
| 97 |
$abs_path = defender_replace_line( ABSPATH ); |
| 98 |
$core = new File( |
| 99 |
$abs_path, |
| 100 |
true, |
| 101 |
false, |
| 102 |
array( |
| 103 |
'dir' => array( |
| 104 |
$abs_path . 'wp-admin', |
| 105 |
$abs_path . WPINC, |
| 106 |
), |
| 107 |
), |
| 108 |
array(), |
| 109 |
true, |
| 110 |
true, |
| 111 |
$this->settings->filesize |
| 112 |
); |
| 113 |
|
| 114 |
$outside = new File( |
| 115 |
$abs_path, |
| 116 |
true, |
| 117 |
true, |
| 118 |
array(), |
| 119 |
array( |
| 120 |
'dir' => array( |
| 121 |
$abs_path . 'wp-content' . DIRECTORY_SEPARATOR, |
| 122 |
$abs_path . 'wp-admin', |
| 123 |
$abs_path . WPINC, |
| 124 |
), |
| 125 |
'filename' => array( |
| 126 |
'wp-config.php', |
| 127 |
), |
| 128 |
), |
| 129 |
false, |
| 130 |
true, |
| 131 |
$this->settings->filesize |
| 132 |
); |
| 133 |
|
| 134 |
$files = array_merge( $core->get_dir_tree(), $outside->get_dir_tree() ); |
| 135 |
$files = array_filter( |
| 136 |
$files, |
| 137 |
function ( $value ) { |
| 138 |
return is_string( $value ) && '' !== trim( $value ); |
| 139 |
} |
| 140 |
); |
| 141 |
$this->log( sprintf( 'Core: %s', count( $files ) ), Scan_Controller::SCAN_LOG ); |
| 142 |
update_site_option( self::CACHE_CORE, $files ); |
| 143 |
|
| 144 |
return $files; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get various content files starting from the root and update the cache. |
| 149 |
* |
| 150 |
* @return array|void The array of content files if cache exists, or void if no files in cache. |
| 151 |
*/ |
| 152 |
private function get_content_files() { |
| 153 |
$cache = get_site_option( self::CACHE_CONTENT, false ); |
| 154 |
if ( is_array( $cache ) ) { |
| 155 |
return $cache; |
| 156 |
} |
| 157 |
|
| 158 |
$arr_extension = array( 'php' ); |
| 159 |
// Since v5.8.0. |
| 160 |
$enabled = apply_filters( 'wpdef_scan_js_detection_enabled', true ); |
| 161 |
$enabled = is_bool( $enabled ) ? $enabled : (bool) $enabled; |
| 162 |
if ( $enabled ) { |
| 163 |
$arr_extension[] = 'js'; |
| 164 |
} |
| 165 |
$content = new File( |
| 166 |
defender_replace_line( ABSPATH ), |
| 167 |
true, |
| 168 |
false, |
| 169 |
array( 'ext' => $arr_extension ), |
| 170 |
array(), |
| 171 |
true, |
| 172 |
true, |
| 173 |
$this->settings->filesize |
| 174 |
); |
| 175 |
|
| 176 |
$files = $content->get_dir_tree(); |
| 177 |
$files = array_filter( |
| 178 |
$files, |
| 179 |
function ( $value ) { |
| 180 |
return is_string( $value ) && '' !== trim( $value ); |
| 181 |
} |
| 182 |
); |
| 183 |
$files[] = defender_wp_config_path(); |
| 184 |
$this->log( sprintf( 'Number of files: %s', count( $files ) ), Scan_Controller::SCAN_LOG ); |
| 185 |
update_site_option( self::CACHE_CONTENT, $files ); |
| 186 |
} |
| 187 |
} |
| 188 |
|