-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcron-tasks.php
More file actions
162 lines (128 loc) · 4.85 KB
/
Copy pathcron-tasks.php
File metadata and controls
162 lines (128 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
// don't call the file directly
defined( 'ABSPATH' ) || die( 0 );
require_once __DIR__ . '/vp-scanner.php';
if ( !function_exists( 'apply_filters_ref_array' ) ) :
function apply_filters_ref_array($tag, $args) {
global $wp_filter, $merged_filters, $wp_current_filter;
// Do 'all' actions first
if ( isset($wp_filter['all']) ) {
$all_args = func_get_args();
$wp_current_filter[] = $tag;
_wp_call_all_hook($all_args);
}
if ( !isset($wp_filter[$tag]) ) {
if ( isset($wp_filter['all']) )
array_pop($wp_current_filter);
return $args[0];
}
if ( !isset($wp_filter['all']) )
$wp_current_filter[] = $tag;
// Sort
if ( !isset( $merged_filters[ $tag ] ) ) {
ksort($wp_filter[$tag]);
$merged_filters[ $tag ] = true;
}
reset( $wp_filter[ $tag ] );
do {
foreach( (array) current($wp_filter[$tag]) as $the_ )
if ( !is_null($the_['function']) )
$args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
} while ( next($wp_filter[$tag]) !== false );
array_pop( $wp_current_filter );
return $args[0];
}
endif;
class VP_Site_Scanner {
function __construct() {
// Only scan once in multisites.
if( function_exists( 'is_main_site' ) && !is_main_site() )
return;
add_action( 'vp_scan_site' , array( $this, '_scan_site') );
add_filter( 'cron_schedules' , array( $this, '_custom_cron' ) );
add_action( 'vp_scan_next_batch', array( $this, '_scan_batch' ) );
$signatures = get_option( '_vp_signatures' );
if ( $signatures && ! wp_next_scheduled( 'vp_scan_site' ) )
wp_schedule_event( time(), 'daily', 'vp_scan_site' );
if ( $signatures && ! wp_next_scheduled( 'vp_scan_next_batch' ) )
wp_schedule_event( time(), 'five_minutes_interval', 'vp_scan_next_batch' );
}
function _custom_cron( $schedules ) {
$schedules['five_minutes_interval'] = array(
'interval' => 300,
'display' => __( 'Once every five minutes' , 'vaultpress'),
);
return $schedules;
}
function _scan_site() {
if ( !get_option( '_vp_current_scan' ) ) {
$ignore_symlinks = get_option( '_vp_ignore_symlinks', false );
$paths = array( 'root' => new VP_FileScan( ABSPATH, $ignore_symlinks ) );
// Is WP_CONTENT_DIR inside ABSPATH?
if ( is_dir( WP_CONTENT_DIR ) && strpos( realpath( WP_CONTENT_DIR ), realpath( ABSPATH ) . DIRECTORY_SEPARATOR ) !== 0 )
$paths['content'] = new VP_FileScan( WP_CONTENT_DIR, $ignore_symlinks );
// Is WP_PLUGIN_DIR inside ABSPATH or WP_CONTENT_DIR?
if ( is_dir( WP_PLUGIN_DIR ) && strpos( realpath( WP_PLUGIN_DIR ), realpath( WP_CONTENT_DIR ) . DIRECTORY_SEPARATOR ) !== 0 && strpos( realpath( WP_PLUGIN_DIR ), realpath( ABSPATH ) . DIRECTORY_SEPARATOR ) !== 0 )
$paths['plugins'] = new VP_FileScan( WP_PLUGIN_DIR, $ignore_symlinks );
// Is WPMU_PLUGIN_DIR inside ABSPATH or WP_CONTENT_DIR?
if ( is_dir( WPMU_PLUGIN_DIR ) && strpos( realpath( WPMU_PLUGIN_DIR ), realpath( WP_CONTENT_DIR ) . DIRECTORY_SEPARATOR ) !== 0 && strpos( realpath( WPMU_PLUGIN_DIR ), realpath( ABSPATH ) . DIRECTORY_SEPARATOR ) !== 0 )
$paths['mu-plugins'] = new VP_FileScan( WPMU_PLUGIN_DIR, $ignore_symlinks );
update_option( '_vp_current_scan', $paths );
}
}
function _scan_clean_up( &$paths, $type = null ) {
if( is_array( $paths ) )
unset( $paths[$type] );
if ( empty( $paths ) || !is_array( $paths ) ) {
delete_option( '_vp_current_scan' );
return true;
}
return false;
}
function _scan_batch() {
$paths = get_option( '_vp_current_scan' );
if ( empty( $paths ) || $this->_scan_clean_up( $paths ) )
return false;
if ( ! is_array( $paths ) ) {
return false;
}
reset( $paths );
$type = null;
$current = false;
foreach ( $paths as $type => $current ) {
break;
}
if ( !is_object( $current ) || empty( $current->last_dir ) )
return $this->_scan_clean_up( $paths, $type );
$default_batch_limit = 400;
if ( ! function_exists( 'set_time_limit' ) || ! @set_time_limit( 0 ) ) {
$default_batch_limit = 100; // avoid timeouts
}
$GLOBALS['vp_signatures'] = get_option( '_vp_signatures' );
if ( empty( $GLOBALS['vp_signatures'] ) )
return false;
$limit = get_option( '_vp_batch_file_size', $default_batch_limit );
$files = $current->get_files( $limit );
// No more files to scan.
if ( !$current->last_dir || count( $files ) < $limit )
unset( $paths[$type] );
update_option( '_vp_current_scan', $paths );
$results = array();
foreach ( $files as $file ) {
$verdict = vp_scan_file( $file );
if ( !empty( $verdict ) )
$results[$file] = array( 'hash' => @md5_file( $file ), 'verdict' => $verdict );
}
if ( !empty( $results ) ) {
$vaultpress = VaultPress::init();
$vaultpress->add_ping( 'security', array( 'suspicious_v2' => $results ) );
}
}
static function &init() {
static $instance = false;
if ( !$instance )
$instance = new VP_Site_Scanner();
return $instance;
}
}
VP_Site_Scanner::init();