| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Behavior\Scan; |
| 4 |
|
| 5 |
use Calotes\Base\File; |
| 6 |
use Calotes\Component\Behavior; |
| 7 |
use WP_Defender\Model\Setting\Scan; |
| 8 |
|
| 9 |
/** |
| 10 |
* We will gather core files & content files, for using in core integrity. |
| 11 |
* |
| 12 |
* Class Gather_Fact |
| 13 |
* @package WP_Defender\Behavior\Scan |
| 14 |
*/ |
| 15 |
class Gather_Fact extends Behavior { |
| 16 |
use \WP_Defender\Traits\IO; |
| 17 |
|
| 18 |
public const CACHE_CORE = 'wdfcore', CACHE_CONTENT = 'wdfcontent'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Gather core files & content files. |
| 22 |
*/ |
| 23 |
public function gather_fact() { |
| 24 |
$timer = time(); |
| 25 |
$model = $this->owner->scan; |
| 26 |
$need_to_run = empty( $model->task_checkpoint ) ? 'get_core_files' : 'get_content_files'; |
| 27 |
if ( 'get_core_files' === $need_to_run ) { |
| 28 |
$settings = new Scan(); |
| 29 |
if ( $settings->integrity_check && $settings->check_core ) { |
| 30 |
$this->get_core_files(); |
| 31 |
} |
| 32 |
$model->calculate_percent( 50, 1 ); |
| 33 |
} else { |
| 34 |
$this->get_content_files(); |
| 35 |
$model->calculate_percent( 100, 1 ); |
| 36 |
} |
| 37 |
$this->log( sprintf( '%s in %s', $need_to_run, time() - $timer ), 'scan.log' ); |
| 38 |
$model->task_checkpoint = $need_to_run; |
| 39 |
$model->save(); |
| 40 |
|
| 41 |
return 'get_content_files' === $need_to_run; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @return array|mixed |
| 46 |
*/ |
| 47 |
private function get_core_files() { |
| 48 |
$cache = get_site_option( self::CACHE_CORE, false ); |
| 49 |
if ( is_array( $cache ) ) { |
| 50 |
|
| 51 |
return $cache; |
| 52 |
} |
| 53 |
$abs_path = ABSPATH; |
| 54 |
if ( defender_is_windows() ) { |
| 55 |
// This mean we are on Windows. |
| 56 |
$abs_path = str_replace( '/', DIRECTORY_SEPARATOR, $abs_path ); |
| 57 |
} |
| 58 |
$core = new File( |
| 59 |
ABSPATH, |
| 60 |
true, |
| 61 |
false, |
| 62 |
array( |
| 63 |
'dir' => array( |
| 64 |
$abs_path . 'wp-admin', |
| 65 |
$abs_path . WPINC, |
| 66 |
), |
| 67 |
), |
| 68 |
array(), |
| 69 |
true, |
| 70 |
true |
| 71 |
); |
| 72 |
|
| 73 |
$outside = new File( |
| 74 |
$abs_path, |
| 75 |
true, |
| 76 |
true, |
| 77 |
array(), |
| 78 |
array( |
| 79 |
'dir' => array( |
| 80 |
$abs_path . 'wp-content', |
| 81 |
$abs_path . 'wp-admin', |
| 82 |
$abs_path . 'wp-includes', |
| 83 |
), |
| 84 |
'filename' => array( |
| 85 |
'wp-config.php', |
| 86 |
), |
| 87 |
), |
| 88 |
false, |
| 89 |
true |
| 90 |
); |
| 91 |
|
| 92 |
$files = array_merge( $core->get_dir_tree(), $outside->get_dir_tree() ); |
| 93 |
$files = array_filter( $files ); |
| 94 |
$this->log( sprintf( 'Core: %s', count( $files ) ), 'scan.log' ); |
| 95 |
update_site_option( self::CACHE_CORE, $files ); |
| 96 |
|
| 97 |
return $files; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Return every php files inside wp-content. |
| 102 |
* |
| 103 |
* @return mixed |
| 104 |
*/ |
| 105 |
private function get_content_files() { |
| 106 |
$cache = get_site_option( self::CACHE_CONTENT, false ); |
| 107 |
if ( is_array( $cache ) ) { |
| 108 |
|
| 109 |
return $cache; |
| 110 |
} |
| 111 |
$content = new File( |
| 112 |
WP_CONTENT_DIR, |
| 113 |
true, |
| 114 |
false, |
| 115 |
array( |
| 116 |
'ext' => array( |
| 117 |
'php', |
| 118 |
), |
| 119 |
), |
| 120 |
array(), |
| 121 |
true, |
| 122 |
true, |
| 123 |
$this->owner->settings->filesize |
| 124 |
); |
| 125 |
|
| 126 |
$files = $content->get_dir_tree(); |
| 127 |
$files = array_filter( $files ); |
| 128 |
$files[] = defender_wp_config_path(); |
| 129 |
$this->log( sprintf( 'Content: %s', count( $files ) ), 'scan.log' ); |
| 130 |
update_site_option( self::CACHE_CONTENT, $files ); |
| 131 |
} |
| 132 |
} |
| 133 |
|