VaultPress - Version 1.3.6

Version Description

Download this release

Release Info

Developer xknown
Plugin Icon 128x128 VaultPress
Version 1.3.6
Comparing to
See all releases

Version 1.3.6

class.vaultpress-database.php ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class VaultPress_Database {
4
+
5
+ var $table = null;
6
+ var $pks = null;
7
+
8
+ function VaultPress_Database() {
9
+ $this->__construct();
10
+ }
11
+
12
+ function __construct() {
13
+ }
14
+
15
+ function attach( $table ) {
16
+ $this->table=$table;
17
+ }
18
+
19
+ function get_tables( $filter=null ) {
20
+ global $wpdb;
21
+ $rval = $wpdb->get_col( 'SHOW TABLES' );
22
+ if ( $filter )
23
+ $rval = preg_grep( $filter, $rval );
24
+ return $rval;
25
+ }
26
+
27
+ function show_create() {
28
+ global $wpdb;
29
+ if ( !$this->table )
30
+ return false;
31
+ $table = $wpdb->escape( $this->table );
32
+ $results = $wpdb->get_row( "SHOW CREATE TABLE `$table`" );
33
+ $want = 'Create Table';
34
+ if ( $results )
35
+ $results = $results->$want;
36
+ return $results;
37
+ }
38
+
39
+ function explain() {
40
+ global $wpdb;
41
+ if ( !$this->table )
42
+ return false;
43
+ $table = $wpdb->escape( $this->table );
44
+ return $wpdb->get_results( "EXPLAIN `$table`" );
45
+ }
46
+
47
+ function diff( $signatures ) {
48
+ global $wpdb;
49
+ if ( !is_array( $signatures ) || !count( $signatures ) )
50
+ return false;
51
+ if ( !$this->table )
52
+ return false;
53
+ $table = $wpdb->escape( $this->table );
54
+ $diff = array();
55
+ foreach ( $signatures as $where => $signature ) {
56
+ $pksig = md5( $where );
57
+ unset( $wpdb->queries );
58
+ $row = $wpdb->get_row( "SELECT * FROM `$table` WHERE $where" );
59
+ if ( !$row ) {
60
+ $diff[$pksig] = array ( 'change' => 'deleted', 'where' => $where );
61
+ continue;
62
+ }
63
+ $row = serialize( $row );
64
+ $hash = md5( $row );
65
+ if ( $hash != $signature )
66
+ $diff[$pksig] = array( 'change' => 'modified', 'where' => $where, 'signature' => $hash, 'row' => $row );
67
+ }
68
+ return $diff;
69
+ }
70
+
71
+ function count( $columns ) {
72
+ global $wpdb;
73
+ if ( !is_array( $columns ) || !count( $columns ) )
74
+ return false;
75
+ if ( !$this->table )
76
+ return false;
77
+ $table = $wpdb->escape( $this->table );
78
+ $column = $wpdb->escape( array_shift( $columns ) );
79
+ return $wpdb->get_var( "SELECT COUNT( $column ) FROM `$table`" );
80
+ }
81
+
82
+ function wpdb( $query, $function='get_results' ) {
83
+ global $wpdb;
84
+
85
+ if ( !is_callable( array( $wpdb, $function ) ) )
86
+ return false;
87
+
88
+ $res = $wpdb->$function( $query );
89
+ if ( !$res )
90
+ return $res;
91
+ switch ( $function ) {
92
+ case 'get_results':
93
+ foreach ( $res as $idx => $row ) {
94
+ if ( isset( $row->option_name ) && $row->option_name == 'cron' )
95
+ $res[$idx]->option_value = serialize( array() );
96
+ }
97
+ break;
98
+ case 'get_row':
99
+ if ( isset( $res->option_name ) && $res->option_name == 'cron' )
100
+ $res->option_value = serialize( array() );
101
+ break;
102
+ }
103
+ return $res;
104
+ }
105
+
106
+ function get_cols( $columns, $limit=false, $offset=false, $where=false ) {
107
+ global $wpdb;
108
+ if ( !is_array( $columns ) || !count( $columns ) )
109
+ return false;
110
+ if ( !$this->table )
111
+ return false;
112
+ $table = $wpdb->escape( $this->table );
113
+ $limitsql = '';
114
+ $offsetsql = '';
115
+ $wheresql = '';
116
+ if ( $limit )
117
+ $limitsql = ' LIMIT ' . intval( $limit );
118
+ if ( $offset )
119
+ $offsetsql = ' OFFSET ' . intval( $offset );
120
+ if ( $where )
121
+ $wheresql = ' WHERE ' . base64_decode($where);
122
+ $rval = array();
123
+ foreach ( $wpdb->get_results( "SELECT * FROM `$this->table` $wheresql $limitsql $offsetsql" ) as $row ) {
124
+ // We don't need to actually record a real cron option value, just an empty array
125
+ if ( isset( $row->option_name ) && $row->option_name == 'cron' )
126
+ $row->option_value = serialize( array() );
127
+ $keys = array();
128
+ $vals = array();
129
+ foreach ( get_object_vars( $row ) as $i => $v ) {
130
+ $keys[] = sprintf( "`%s`", $wpdb->escape( $i ) );
131
+ $vals[] = sprintf( "'%s'", $wpdb->escape( $v ) );
132
+ if ( !in_array( $i, $columns ) )
133
+ unset( $row->$i );
134
+ }
135
+ $row->hash = md5( sprintf( "(%s) VALUES(%s)", implode( ',',$keys ), implode( ',',$vals ) ) );
136
+ $rval[]=$row;
137
+ }
138
+ return $rval;
139
+ }
140
+ }
class.vaultpress-filesystem.php ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class VaultPress_Filesystem {
4
+
5
+ var $type = null;
6
+ var $dir = null;
7
+ var $keys = array( 'ino', 'uid', 'gid', 'size', 'mtime', 'blksize', 'blocks' );
8
+
9
+ function VaultPress_Filesystem() {
10
+ $this->__construct();
11
+ }
12
+
13
+ function __construct() {
14
+ }
15
+
16
+ function want( $type ) {
17
+ $vp = VaultPress::init();
18
+
19
+ if ( $type == 'plugins' ) {
20
+ $this->dir = realpath( $vp->resolve_content_dir() . 'plugins' );
21
+ $this->type = 'p';
22
+ return true;
23
+ }
24
+ if ( $type == 'themes' ) {
25
+ $this->dir = realpath( $vp->resolve_content_dir() . 'themes' );
26
+ $this->type = 't';
27
+ return true;
28
+ }
29
+ if ( $type == 'uploads' ) {
30
+ $this->dir = realpath( $vp->resolve_upload_path() );
31
+ $this->type = 'u';
32
+ return true;
33
+ }
34
+ if ( $type == 'content' ) {
35
+ $this->dir = realpath( $vp->resolve_content_dir() );
36
+ $this->type = 'c';
37
+ return true;
38
+ }
39
+ if ( $type == 'root' ) {
40
+ $this->dir = realpath( ABSPATH );
41
+ $this->type = 'r';
42
+ return true;
43
+ }
44
+ die( 'naughty naughty' );
45
+ }
46
+
47
+ function fdump( $file ) {
48
+ header("Content-Type: application/octet-stream;");
49
+ header("Content-Transfer-Encoding: binary");
50
+ @ob_end_clean();
51
+ if ( !file_exists( $file ) || !is_readable( $file ) )
52
+ die( "no such file" );
53
+ if ( !is_file( $file ) && !is_link( $file ) )
54
+ die( "can only dump files" );
55
+ $fp = @fopen( $file, 'rb' );
56
+ if ( !$fp )
57
+ die( "could not open file" );
58
+ while ( !feof( $fp ) )
59
+ echo @fread( $fp, 8192 );
60
+ @fclose( $fp );
61
+ die();
62
+ }
63
+
64
+ function stat( $file, $md5=true, $sha1=true ) {
65
+ $rval = array();
66
+ foreach ( stat( $file ) as $i => $v ) {
67
+ if ( is_numeric( $i ) )
68
+ continue;
69
+ $rval[$i] = $v;
70
+ }
71
+ $rval['type'] = filetype( $file );
72
+ if ( $rval['type'] == 'file' ) {
73
+ if ( $md5 )
74
+ $rval['md5'] = md5_file( $file );
75
+ if ( $sha1 )
76
+ $rval['sha1'] = sha1_file( $file );
77
+ }
78
+ $rval['path'] = str_replace( $this->dir, '', $file );
79
+ return $rval;
80
+ }
81
+
82
+ function ls( $what, $md5=false, $sha1=false, $limit=null, $offset=null ) {
83
+ clearstatcache();
84
+ $path = realpath($this->dir . $what);
85
+ if ( is_file($path) )
86
+ return $this->stat( $path, $md5, $sha1 );
87
+ if ( is_dir($path) ) {
88
+ $entries = array();
89
+ $current = 0;
90
+ $offset = (int)$offset;
91
+ $orig_limit = (int)$limit;
92
+ $limit = $offset + (int)$limit;
93
+ foreach ( (array)$this->scan_dir( $path ) as $i ) {
94
+ $current++;
95
+ if ( $offset >= $current )
96
+ continue;
97
+ if ( $limit && $limit < $current )
98
+ break;
99
+
100
+ // don't sha1 files over 100MB if we are batching due to memory consumption
101
+ if ( $sha1 && $orig_limit > 1 && is_file( $i ) && (int)@filesize( $i ) > 104857600 )
102
+ $sha1 = false;
103
+
104
+ $entries[] = $this->stat( $i, $md5, $sha1 );
105
+ }
106
+ return $entries;
107
+ }
108
+ }
109
+
110
+ function validate( $file ) {
111
+ $rpath = realpath( $this->dir.$file );
112
+ if ( !$rpath )
113
+ die( serialize( array( 'type' => 'null', 'path' => $file ) ) );
114
+ if ( is_dir( $rpath ) )
115
+ $rpath = "$rpath/";
116
+ if ( strpos( $rpath, $this->dir ) !== 0 )
117
+ return false;
118
+ return true;
119
+ }
120
+
121
+ function dir_examine( $subdir='', $recursive=true, $origin=false ) {
122
+ $res = array();
123
+ if ( !$subdir )
124
+ $subdir='/';
125
+ $dir = $this->dir . $subdir;
126
+ if ( $origin === false )
127
+ $origin = $this->dir . $subdir;
128
+ if ( is_file($dir) ) {
129
+ if ( $origin == $dir )
130
+ $name = str_replace( $this->dir, '/', $subdir );
131
+ else
132
+ $name = str_replace( $origin, '/', $dir );
133
+ $res[$name] = $this->stat( $dir.$entry );
134
+ return $res;
135
+ }
136
+ $d = dir( $dir );
137
+ if ( !$d )
138
+ return $res;
139
+ while ( false !== ( $entry = $d->read() ) ) {
140
+ $rpath = realpath( $dir.$entry );
141
+ $bname = basename( $rpath );
142
+ if ( is_link( $dir.$entry ) )
143
+ continue;
144
+ if ( $entry == '.' || $entry == '..' || $entry == '...' )
145
+ continue;
146
+ if ( !$this->validate( $subdir.$entry ) )
147
+ continue;
148
+ $name = str_replace( $origin, '/', $dir.$entry );
149
+ $res[$name] = $this->stat( $dir.$entry );
150
+ if ( $recursive && is_dir( $this->dir.$subdir.'/'.$entry ) ) {
151
+ $res = array_merge( $res, $this->dir_examine( $subdir.$entry.'/', $recursive, $origin ) );
152
+ }
153
+ }
154
+ return $res;
155
+ }
156
+
157
+ function dir_checksum( $base, &$list, $recursive=true ) {
158
+ if ( $list == null )
159
+ $list = array();
160
+
161
+ if ( 0 !== strpos( $base, $this->dir ) )
162
+ $base = $this->dir . rtrim( $base, '/' );
163
+
164
+ $shortbase = substr( $base, strlen( $this->dir ) );
165
+ if ( !$shortbase )
166
+ $shortbase = '/';
167
+ $stat = stat( $base );
168
+ $directories = array();
169
+ $files = (array)$this->scan_dir( $base );
170
+ array_push( $files, $base );
171
+ foreach ( $files as $file ) {
172
+ if ( $file !== $base && @is_dir( $file ) ) {
173
+ $directories[] = $file;
174
+ continue;
175
+ }
176
+ $stat = @stat( $file );
177
+ if ( !$stat )
178
+ continue;
179
+ $shortstat = array();
180
+ foreach( $this->keys as $key ) {
181
+ if ( isset( $stat[$key] ) )
182
+ $shortstat[$key] = $stat[$key];
183
+ }
184
+ $list[$shortbase][basename( $file )] = $shortstat;
185
+ }
186
+ $list[$shortbase] = md5( serialize( $list[$shortbase] ) );
187
+ if ( !$recursive )
188
+ return $list;
189
+ foreach ( $directories as $dir ) {
190
+ $this->dir_checksum( $dir, $list, $recursive );
191
+ }
192
+ return $list;
193
+ }
194
+
195
+ function scan_dir( $path ) {
196
+ $files = array();
197
+
198
+ if ( false === is_readable( $path ) ) {
199
+ return array();
200
+ }
201
+
202
+ $dh = opendir( $path );
203
+
204
+ if ( false === $dh ) {
205
+ return array();
206
+ }
207
+
208
+ while ( false !== ( $file = readdir( $dh ) ) ) {
209
+ if ( $file == '.' || $file == '..' ) continue;
210
+ $files[] = "$path/$file";
211
+ }
212
+
213
+ closedir( $dh );
214
+ sort( $files );
215
+ return $files;
216
+ }
217
+ }
class.vaultpress-hotfixes.php ADDED
@@ -0,0 +1,837 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class VaultPress_Hotfixes {
4
+ function VaultPress_Hotfixes() {
5
+ $this->__construct();
6
+ }
7
+
8
+ function __construct() {
9
+ global $wp_version;
10
+
11
+ if ( version_compare( $wp_version, '3.0.2', '<' ) )
12
+ add_filter( 'query', array( $this, 'r16625' ) );
13
+
14
+ if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && version_compare( $wp_version, '3.0.3', '<' ) )
15
+ add_action( 'xmlrpc_call', array( $this, 'r16803' ) );
16
+
17
+ if ( version_compare( $wp_version, '3.3.2', '<' ) ) {
18
+ add_filter( 'pre_kses', array( $this, 'r17172_wp_kses' ), 1, 3 );
19
+ add_filter( 'clean_url', array( $this, 'r17172_esc_url' ), 1, 3 );
20
+ }
21
+
22
+ if ( version_compare( $wp_version, '3.1.3', '<' ) ) {
23
+ add_filter( 'sanitize_file_name', array( $this, 'r17990' ) );
24
+
25
+ if ( !empty( $_POST ) )
26
+ $this->r17994( $_POST );
27
+ // Protect add_meta, update_meta used by the XML-RPC API
28
+ add_filter( 'wp_xmlrpc_server_class', create_function( '$class', 'return \'VaultPress_XMLRPC_Server_r17994\';' ) );
29
+
30
+ // clean post_mime_type and guid (r17994)
31
+ add_filter( 'pre_post_mime_type', array( $this, 'r17994_sanitize_mime_type' ) );
32
+ add_filter( 'post_mime_type', array( $this, 'r17994_sanitize_mime_type' ) );
33
+ add_filter( 'pre_post_guid', 'esc_url_raw' );
34
+ add_filter( 'post_guid', 'esc_url' );
35
+ }
36
+
37
+ if ( version_compare( $wp_version, '3.1.4', '<' ) ) {
38
+ add_filter( 'wp_insert_post_data', array( $this, 'r18368' ), 1, 2 );
39
+
40
+ // Add click jacking protection
41
+ // login_init does not exist before 17826.
42
+ $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'login';
43
+ add_action( 'login_form_' . $action, array( $this, 'r17826_send_frame_options_header' ), 10, 0 );
44
+ add_action( 'admin_init', array( $this, 'r17826_send_frame_options_header' ), 10, 0 );
45
+
46
+ add_filter( 'sanitize_option_WPLANG', array( $this, 'r18346_sanitize_lang_on_save' ) );
47
+ add_filter( 'sanitize_option_new_admin_email', array( $this, 'r18346_sanitize_admin_email_on_save' ) );
48
+ }
49
+ add_filter( 'option_new_admin_email', array( $this, 'r18346_sanitize_admin_email' ) );
50
+
51
+ if ( version_compare( $wp_version, '3.3.2', '<' ) ) {
52
+ remove_filter( 'comment_text', 'make_clickable' );
53
+ add_filter( 'comment_text', array( $this, 'r20493_make_clickable' ), 9 );
54
+
55
+ add_filter( 'comment_post_redirect', array( $this, 'r20486_comment_post_redirect' ) );
56
+ }
57
+
58
+ // WooThemes < 3.8.3, foxypress, asset-manager, wordpress-member-private-conversation.
59
+ $end_execution = false;
60
+ if ( isset( $_SERVER['SCRIPT_FILENAME'] ) )
61
+ foreach ( array( 'preview-shortcode-external.php', 'uploadify.php', 'doupload.php', 'cef-upload.php', 'upload.php' ) as $vulnerable_script )
62
+ if ( $vulnerable_script == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
63
+ switch( $vulnerable_script ) {
64
+ case 'upload.php':
65
+ $pma_config_file = realpath( dirname( $_SERVER['SCRIPT_FILENAME'] ) . DIRECTORY_SEPARATOR . 'paam-config-ajax.php' );
66
+ if ( !in_array( $pma_config_file, get_included_files() ) )
67
+ break;
68
+ default:
69
+ $end_execution = true;
70
+ break 2;
71
+ }
72
+ }
73
+ if ( $end_execution )
74
+ die( 'Disabled for security reasons' );
75
+
76
+ if ( version_compare( $wp_version, '3.3.2', '>') && version_compare( $wp_version, '3.4.1', '<' ) ) {
77
+ add_filter( 'map_meta_cap', array( $this, 'r21138_xmlrpc_edit_posts' ), 10, 4 );
78
+ add_action( 'map_meta_cap', array( $this, 'r21152_unfiltered_html' ), 10, 4 );
79
+ }
80
+
81
+ // https://core.trac.wordpress.org/changeset/21083
82
+ if ( version_compare( $wp_version, '3.3', '>=') && version_compare( $wp_version, '3.3.3', '<' ) )
83
+ add_filter( 'editable_slug', 'esc_textarea' );
84
+
85
+ add_filter( 'get_pagenum_link', array( $this, 'get_pagenum_link' ) );
86
+ }
87
+
88
+ function r21138_xmlrpc_edit_posts( $caps, $cap, $user_id, $args ) {
89
+ if ( ! isset( $args[0] ) || isset( $args[1] ) && $args[1] === 'hotfixed' )
90
+ return $caps;
91
+ foreach ( get_post_types( array(), 'objects' ) as $post_type_object ) {
92
+ if ( $cap === $post_type_object->cap->edit_posts )
93
+ return map_meta_cap( $post_type_object->cap->edit_post, $user_id, $args[0], 'hotfixed' );
94
+ }
95
+ return $caps;
96
+ }
97
+
98
+ function r21152_unfiltered_html( $caps, $cap, $user_id, $args ) {
99
+ if ( $cap !== 'unfiltered_html' )
100
+ return $caps;
101
+ if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
102
+ return $caps;
103
+ $key = array_search( 'do_not_allow', $caps );
104
+ if ( false !== $key )
105
+ return $caps;
106
+ if ( is_multisite() && ! is_super_admin( $user_id ) )
107
+ $caps[$key] = 'do_not_allow';
108
+ return $caps;
109
+ }
110
+
111
+ function get_pagenum_link( $url ) {
112
+ return esc_url_raw( $url );
113
+ }
114
+
115
+ function r20486_comment_post_redirect( $location ) {
116
+ $location = wp_sanitize_redirect( $location );
117
+ $location = wp_validate_redirect( $location, admin_url() );
118
+
119
+ return $location;
120
+ }
121
+
122
+ function r20493_make_clickable( $text ) {
123
+ $r = '';
124
+ $textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
125
+ foreach ( $textarr as $piece ) {
126
+ if ( empty( $piece ) || ( $piece[0] == '<' && ! preg_match('|^<\s*[\w]{1,20}+://|', $piece) ) ) {
127
+ $r .= $piece;
128
+ continue;
129
+ }
130
+
131
+ // Long strings might contain expensive edge cases ...
132
+ if ( 10000 < strlen( $piece ) ) {
133
+ // ... break it up
134
+ foreach ( $this->r20493_split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses
135
+ if ( 2101 < strlen( $chunk ) ) {
136
+ $r .= $chunk; // Too big, no whitespace: bail.
137
+ } else {
138
+ $r .= $this->r20493_make_clickable( $chunk );
139
+ }
140
+ }
141
+ } else {
142
+ $ret = " $piece "; // Pad with whitespace to simplify the regexes
143
+
144
+ $url_clickable = '~
145
+ ([\\s(<.,;:!?]) # 1: Leading whitespace, or punctuation
146
+ ( # 2: URL
147
+ [\\w]{1,20}+:// # Scheme and hier-part prefix
148
+ (?=\S{1,2000}\s) # Limit to URLs less than about 2000 characters long
149
+ [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+ # Non-punctuation URL character
150
+ (?: # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character
151
+ [\'.,;:!?)] # Punctuation URL character
152
+ [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character
153
+ )*
154
+ )
155
+ (\)?) # 3: Trailing closing parenthesis (for parethesis balancing post processing)
156
+ ~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character.
157
+ // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times.
158
+
159
+ $ret = preg_replace_callback( $url_clickable, array( $this, 'r20493_make_url_clickable_cb') , $ret );
160
+
161
+ $ret = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret );
162
+ $ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret );
163
+
164
+ $ret = substr( $ret, 1, -1 ); // Remove our whitespace padding.
165
+ $r .= $ret;
166
+ }
167
+ }
168
+
169
+ // Cleanup of accidental links within links
170
+ $r = preg_replace( '#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r );
171
+ return $r;
172
+ }
173
+
174
+ function r20493_make_url_clickable_cb($matches) {
175
+ $url = $matches[2];
176
+
177
+ if ( ')' == $matches[3] && strpos( $url, '(' ) ) {
178
+ // If the trailing character is a closing parethesis, and the URL has an opening parenthesis in it, add the closing parenthesis to the URL.
179
+ // Then we can let the parenthesis balancer do its thing below.
180
+ $url .= $matches[3];
181
+ $suffix = '';
182
+ } else {
183
+ $suffix = $matches[3];
184
+ }
185
+
186
+ // Include parentheses in the URL only if paired
187
+ while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
188
+ $suffix = strrchr( $url, ')' ) . $suffix;
189
+ $url = substr( $url, 0, strrpos( $url, ')' ) );
190
+ }
191
+
192
+ $url = esc_url($url);
193
+ if ( empty($url) )
194
+ return $matches[0];
195
+
196
+ return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $suffix;
197
+ }
198
+
199
+ function r20493_split_str_by_whitespace( $string, $goal ) {
200
+ $chunks = array();
201
+
202
+ $string_nullspace = strtr( $string, "\r\n\t\v\f ", "\000\000\000\000\000\000" );
203
+
204
+ while ( $goal < strlen( $string_nullspace ) ) {
205
+ $pos = strrpos( substr( $string_nullspace, 0, $goal + 1 ), "\000" );
206
+
207
+ if ( false === $pos ) {
208
+ $pos = strpos( $string_nullspace, "\000", $goal + 1 );
209
+ if ( false === $pos ) {
210
+ break;
211
+ }
212
+ }
213
+
214
+ $chunks[] = substr( $string, 0, $pos + 1 );
215
+ $string = substr( $string, $pos + 1 );
216
+ $string_nullspace = substr( $string_nullspace, $pos + 1 );
217
+ }
218
+
219
+ if ( $string ) {
220
+ $chunks[] = $string;
221
+ }
222
+
223
+ return $chunks;
224
+ }
225
+
226
+ function r16625( $query ) {
227
+ // Hotfixes: http://core.trac.wordpress.org/changeset/16625
228
+
229
+ // Punt as fast as possible if this isn't an UPDATE
230
+ if ( substr( $query, 0, 6 ) != "UPDATE" )
231
+ return $query;
232
+ global $wpdb;
233
+
234
+ // Determine what the prefix of the bad query would look like and punt if this query doesn't match
235
+ $badstring = "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '";
236
+ if ( substr( $query, 0, strlen( $badstring ) ) != $badstring )
237
+ return $query;
238
+
239
+ // Pull the post_id which is the last thing in the origin query, after a space, no quotes
240
+ $post_id = array_pop( explode( " ", $query ) );
241
+
242
+ // Chop off the beginning and end of the original query to get our unsanitized $tb_ping
243
+ $tb_ping = substr(
244
+ $query,
245
+ strlen( $badstring ),
246
+ (
247
+ strlen( $query ) - (
248
+ strlen( $badstring ) + strlen( sprintf( "', '')) WHERE ID = %d", $post_id ) )
249
+ )
250
+ )
251
+ );
252
+
253
+ // Return the fixed query
254
+ return $wpdb->prepare( "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id );
255
+ }
256
+
257
+ function r16803( $xmlrpc_method ) {
258
+ // Hotfixes: http://core.trac.wordpress.org/changeset/16803
259
+ global $wp_xmlrpc_server;
260
+ // Pretend that we are an xmlrpc method, freshly called
261
+ $args = $wp_xmlrpc_server->message->params;
262
+ $error_code = 401;
263
+ switch( $xmlrpc_method ) {
264
+ case 'metaWeblog.newPost':
265
+ $content_struct = $args[3];
266
+ $publish = isset( $args[4] ) ? $args[4] : 0;
267
+ if ( !empty( $content_struct['post_type'] ) ) {
268
+ if ( $content_struct['post_type'] == 'page' ) {
269
+ if ( $publish || 'publish' == $content_struct['page_status'] )
270
+ $cap = 'publish_pages';
271
+ else
272
+ $cap = 'edit_pages';
273
+ $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
274
+ } elseif ( $content_struct['post_type'] == 'post' ) {
275
+ if ( $publish || 'publish' == $content_struct['post_status'] )
276
+ $cap = 'publish_posts';
277
+ else
278
+ $cap = 'edit_posts';
279
+ $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
280
+ } else {
281
+ $error_message = __( 'Invalid post type.' );
282
+ }
283
+ } else {
284
+ if ( $publish || 'publish' == $content_struct['post_status'] )
285
+ $cap = 'publish_posts';
286
+ else
287
+ $cap = 'edit_posts';
288
+ $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
289
+ }
290
+ if ( current_user_can( $cap ) )
291
+ return true;
292
+ break;
293
+ case 'metaWeblog.editPost':
294
+ $post_ID = (int) $args[0];
295
+ $content_struct = $args[3];
296
+ $publish = $args[4];
297
+ $cap = ( $publish ) ? 'publish_posts' : 'edit_posts';
298
+ $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
299
+ if ( !empty( $content_struct['post_type'] ) ) {
300
+ if ( $content_struct['post_type'] == 'page' ) {
301
+ if ( $publish || 'publish' == $content_struct['page_status'] )
302
+ $cap = 'publish_pages';
303
+ else
304
+ $cap = 'edit_pages';
305
+ $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
306
+ } elseif ( $content_struct['post_type'] == 'post' ) {
307
+ if ( $publish || 'publish' == $content_struct['post_status'] )
308
+ $cap = 'publish_posts';
309
+ else
310
+ $cap = 'edit_posts';
311
+ $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
312
+ } else {
313
+ $error_message = __( 'Invalid post type.' );
314
+ }
315
+ } else {
316
+ if ( $publish || 'publish' == $content_struct['post_status'] )
317
+ $cap = 'publish_posts';
318
+ else
319
+ $cap = 'edit_posts';
320
+ $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
321
+ }
322
+ if ( current_user_can( $cap ) )
323
+ return true;
324
+ break;
325
+ case 'mt.publishPost':
326
+ $post_ID = (int) $args[0];
327
+ if ( current_user_can( 'publish_posts' ) && current_user_can( 'edit_post', $post_ID ) )
328
+ return true;
329
+ $error_message = __( 'Sorry, you cannot edit this post.' );
330
+ break;
331
+ case 'blogger.deletePost':
332
+ $post_ID = (int) $args[1];
333
+ if ( current_user_can( 'delete_post', $post_ID ) )
334
+ return true;
335
+ $error_message = __( 'Sorry, you do not have the right to delete this post.' );
336
+ break;
337
+ case 'wp.getPageStatusList':
338
+ if ( current_user_can( 'edit_pages' ) )
339
+ return true;
340
+ $error_code = 403;
341
+ $error_message = __( 'You are not allowed access to details about this site.' );
342
+ break;
343
+ case 'wp.deleteComment':
344
+ case 'wp.editComment':
345
+ $comment_ID = (int) $args[3];
346
+ if ( !$comment = get_comment( $comment_ID ) )
347
+ return true; // This will be handled in the calling function explicitly
348
+ if ( current_user_can( 'edit_post', $comment->comment_post_ID ) )
349
+ return true;
350
+ $error_code = 403;
351
+ $error_message = __( 'You are not allowed to moderate comments on this site.' );
352
+ break;
353
+ default:
354
+ return true;
355
+ }
356
+ // If we are here then this was a handlable xmlrpc call and the capability checks above all failed
357
+ // ( otherwise they would have returned to the do_action from the switch statement above ) so it's
358
+ // time to exit with whatever error we've determined is the problem (thus short circuiting the
359
+ // original XMLRPC method call, and enforcing the above capability checks -- with an ax. We'll
360
+ // mimic the behavior from the end of IXR_Server::serve()
361
+ $r = new IXR_Error( $error_code, $error_message );
362
+ $resultxml = $r->getXml();
363
+ $xml = <<<EOD
364
+ <methodResponse>
365
+ <params>
366
+ <param>
367
+ <value>
368
+ $resultxml
369
+ </value>
370
+ </param>
371
+ </params>
372
+ </methodResponse>
373
+ EOD;
374
+ $wp_xmlrpc_server->output( $xml );
375
+ // For good measure...
376
+ die();
377
+ }
378
+
379
+ function r17172_esc_url( $url, $original_url, $_context ) {
380
+ $url = $original_url;
381
+
382
+ if ( '' == $url )
383
+ return $url;
384
+ $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
385
+ $strip = array('%0d', '%0a', '%0D', '%0A');
386
+ $url = _deep_replace($strip, $url);
387
+ $url = str_replace(';//', '://', $url);
388
+ /* If the URL doesn't appear to contain a scheme, we
389
+ * presume it needs http:// appended (unless a relative
390
+ * link starting with /, # or ? or a php file).
391
+ */
392
+ if ( strpos($url, ':') === false && ! in_array( $url[0], array( '/', '#', '?' ) ) &&
393
+ ! preg_match('/^[a-z0-9-]+?\.php/i', $url) )
394
+ $url = 'http://' . $url;
395
+
396
+ // Replace ampersands and single quotes only when displaying.
397
+ if ( 'display' == $_context ) {
398
+ $url = wp_kses_normalize_entities( $url );
399
+ $url = str_replace( '&amp;', '&#038;', $url );
400
+ $url = str_replace( "'", '&#039;', $url );
401
+ }
402
+
403
+ $protocols = array ('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn');
404
+ if ( VaultPress_kses::wp_kses_bad_protocol( $url, $protocols ) != $url )
405
+ return '';
406
+ return $url;
407
+ }
408
+
409
+ // http://core.trac.wordpress.org/changeset/17172
410
+ // http://core.trac.wordpress.org/changeset/20541
411
+ function r17172_wp_kses( $string, $html, $protocols ) {
412
+ return VaultPress_kses::wp_kses( $string, $html, $protocols );
413
+ }
414
+
415
+ // http://core.trac.wordpress.org/changeset/17990
416
+ function r17990( $filename ) {
417
+ $parts = explode('.', $filename);
418
+ $filename = array_shift($parts);
419
+ $extension = array_pop($parts);
420
+ $mimes = get_allowed_mime_types();
421
+
422
+ // Loop over any intermediate extensions. Munge them with a trailing underscore if they are a 2 - 5 character
423
+ // long alpha string not in the extension whitelist.
424
+ foreach ( (array) $parts as $part) {
425
+ $filename .= '.' . $part;
426
+
427
+ if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
428
+ $allowed = false;
429
+ foreach ( $mimes as $ext_preg => $mime_match ) {
430
+ $ext_preg = '!^(' . $ext_preg . ')$!i';
431
+ if ( preg_match( $ext_preg, $part ) ) {
432
+ $allowed = true;
433
+ break;
434
+ }
435
+ }
436
+ if ( !$allowed )
437
+ $filename .= '_';
438
+ }
439
+ }
440
+ $filename .= '.' . $extension;
441
+ return $filename;
442
+ }
443
+
444
+ /*
445
+ * Hotfixes: http://core.trac.wordpress.org/changeset/18368
446
+ */
447
+ function r18368( $post, $raw_post ) {
448
+ if ( isset( $post['filter'] ) || isset ( $raw_post['filter'] ) ) {
449
+ unset( $post['filter'], $raw_post['filter'] ); // to ensure the post is properly sanitized
450
+ $post = sanitize_post($post, 'db');
451
+ }
452
+ if ( empty( $post['ID'] ) )
453
+ unset( $post['ID'] ); // sanitize_post
454
+ unset( $post['filter'] ); // sanitize_post
455
+ return $post;
456
+ }
457
+
458
+ /**
459
+ * Protect WordPress internal metadata.
460
+ *
461
+ * The post data is passed as a parameter to (unit) test this method.
462
+ * @param $post_data|array the $_POST array.
463
+ */
464
+ function r17994( &$post_data ) {
465
+ // Protect admin-ajax add_meta
466
+ $metakeyselect = isset( $post_data['metakeyselect'] ) ? stripslashes( trim( $post_data['metakeyselect'] ) ) : '';
467
+ $metakeyinput = isset( $post_data['metakeyinput'] ) ? stripslashes( trim( $post_data['metakeyinput'] ) ) : '';
468
+
469
+ if ( ( $metakeyselect && '_' == $metakeyselect[0] ) || ( $metakeyinput && '_' == $metakeyinput[0] ) ) {
470
+ unset( $_POST['metakeyselect'], $_POST['metakeyinput'] );
471
+ }
472
+
473
+ // Protect admin-ajax update_meta
474
+ if ( isset( $post_data['meta'] ) ) {
475
+ foreach ( (array)$post_data['meta'] as $mid => $value ) {
476
+ $key = stripslashes( $post_data['meta'][$mid]['key'] );
477
+ if ( $key && '_' == $key[0] )
478
+ unset( $post_data['meta'][$mid] );
479
+ }
480
+ }
481
+ }
482
+
483
+ function r17994_sanitize_mime_type( $mime_type ) {
484
+ $sani_mime_type = preg_replace( '/[^\-*.a-zA-Z0-9\/+]/', '', $mime_type );
485
+ return apply_filters( 'sanitize_mime_type', $sani_mime_type, $mime_type );
486
+ }
487
+
488
+ function r17826_send_frame_options_header() {
489
+ @header( 'X-Frame-Options: SAMEORIGIN' );
490
+ }
491
+
492
+ function r18346_sanitize_admin_email_on_save($value) {
493
+ $value = sanitize_email( $value );
494
+ if ( !is_email( $value ) ) {
495
+ $value = get_option( 'new_admin_email' ); // Resets option to stored value in the case of failed sanitization
496
+ if ( function_exists( 'add_settings_error' ) )
497
+ add_settings_error( 'new_admin_email', 'invalid_admin_email', __( 'The email address entered did not appear to be a valid email address. Please enter a valid email address.' ) );
498
+ }
499
+ return $value;
500
+ }
501
+
502
+ function r18346_sanitize_admin_email( $value ) {
503
+ return sanitize_email( $value ); // Is it enough ?
504
+ }
505
+
506
+ function r18346_sanitize_lang_on_save( $value ) {
507
+ $value = $this->r18346_sanitize_lang( $value ); // sanitize the new value.
508
+ if ( empty( $value ) )
509
+ $value = get_option( 'WPLANG' );
510
+ return $value;
511
+ }
512
+
513
+ function r18346_sanitize_lang( $value ) {
514
+ $allowed = apply_filters( 'available_languages', get_available_languages() ); // add a filter to unit test
515
+ if ( !empty( $value ) && !in_array( $value, $allowed ) )
516
+ return false;
517
+ else
518
+ return $value;
519
+ }
520
+ }
521
+
522
+ global $wp_version;
523
+ $needs_class_fix = version_compare( $wp_version, '3.1', '>=') && version_compare( $wp_version, '3.1.3', '<' );
524
+ if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && $needs_class_fix ) {
525
+ include_once( ABSPATH . WPINC . '/class-IXR.php' );
526
+ include_once( ABSPATH . WPINC . '/class-wp-xmlrpc-server.php' );
527
+
528
+ class VaultPress_XMLRPC_Server_r17994 extends wp_xmlrpc_server {
529
+ function set_custom_fields( $post_id, $fields ) {
530
+ foreach( $fields as $k => $meta ) {
531
+ $key = stripslashes( trim( $meta['key'] ) );
532
+ if ( $key && '_' == $key[0] )
533
+ unset( $fields[$k] );
534
+ }
535
+ parent::set_custom_fields( $post_id, $fields );
536
+ }
537
+ }
538
+ }
539
+
540
+ class VaultPress_kses {
541
+ static function wp_kses($string, $allowed_html, $allowed_protocols = array ()) {
542
+ $string = wp_kses_no_null($string);
543
+ $string = wp_kses_js_entities($string);
544
+ $string = wp_kses_normalize_entities($string);
545
+ return VaultPress_kses::wp_kses_split($string, $allowed_html, $allowed_protocols);
546
+ }
547
+
548
+ static function wp_kses_split($string, $allowed_html, $allowed_protocols) {
549
+ global $pass_allowed_html, $pass_allowed_protocols;
550
+ $pass_allowed_html = $allowed_html;
551
+ $pass_allowed_protocols = $allowed_protocols;
552
+ return preg_replace_callback( '%(<!--.*?(-->|$))|(<[^>]*(>|$)|>)%', 'VaultPress_kses::_vp_kses_split_callback', $string );
553
+ }
554
+
555
+ static function _vp_kses_split_callback( $match ) {
556
+ global $pass_allowed_html, $pass_allowed_protocols;
557
+ return VaultPress_kses::wp_kses_split2( $match[0], $pass_allowed_html, $pass_allowed_protocols );
558
+ }
559
+
560
+ static function wp_kses_split2($string, $allowed_html, $allowed_protocols) {
561
+ $string = wp_kses_stripslashes($string);
562
+
563
+ if (substr($string, 0, 1) != '<')
564
+ return '&gt;';
565
+ # It matched a ">" character
566
+
567
+ if ( '<!--' == substr( $string, 0, 4 ) ) {
568
+ $string = str_replace( array('<!--', '-->'), '', $string );
569
+ while ( $string != ($newstring = VaultPress_kses::wp_kses($string, $allowed_html, $allowed_protocols)) )
570
+ $string = $newstring;
571
+ if ( $string == '' )
572
+ return '';
573
+ // prevent multiple dashes in comments
574
+ $string = preg_replace('/--+/', '-', $string);
575
+ // prevent three dashes closing a comment
576
+ $string = preg_replace('/-$/', '', $string);
577
+ return "<!--{$string}-->";
578
+ }
579
+ # Allow HTML comments
580
+
581
+ if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%', $string, $matches))
582
+ return '';
583
+ # It's seriously malformed
584
+
585
+ $slash = trim($matches[1]);
586
+ $elem = $matches[2];
587
+ $attrlist = $matches[3];
588
+
589
+ if ( ! isset($allowed_html[strtolower($elem)]) )
590
+ return '';
591
+ # They are using a not allowed HTML element
592
+
593
+ if ($slash != '')
594
+ return "</$elem>";
595
+ # No attributes are allowed for closing elements
596
+
597
+ return VaultPress_kses::wp_kses_attr( $elem, $attrlist, $allowed_html, $allowed_protocols );
598
+ }
599
+
600
+ static function wp_kses_attr($element, $attr, $allowed_html, $allowed_protocols) {
601
+ # Is there a closing XHTML slash at the end of the attributes?
602
+
603
+ $xhtml_slash = '';
604
+ if (preg_match('%\s*/\s*$%', $attr))
605
+ $xhtml_slash = ' /';
606
+
607
+ # Are any attributes allowed at all for this element?
608
+ if ( ! isset($allowed_html[strtolower($element)]) || count($allowed_html[strtolower($element)]) == 0 )
609
+ return "<$element$xhtml_slash>";
610
+
611
+ # Split it
612
+ $attrarr = VaultPress_kses::wp_kses_hair($attr, $allowed_protocols);
613
+
614
+ # Go through $attrarr, and save the allowed attributes for this element
615
+ # in $attr2
616
+ $attr2 = '';
617
+
618
+ $allowed_attr = $allowed_html[strtolower($element)];
619
+ foreach ($attrarr as $arreach) {
620
+ if ( ! isset( $allowed_attr[strtolower($arreach['name'])] ) )
621
+ continue; # the attribute is not allowed
622
+
623
+ $current = $allowed_attr[strtolower($arreach['name'])];
624
+ if ( $current == '' )
625
+ continue; # the attribute is not allowed
626
+
627
+ if ( strtolower( $arreach['name'] ) == 'style' ) {
628
+ $orig_value = $arreach['value'];
629
+ $value = safecss_filter_attr( $orig_value );
630
+
631
+ if ( empty( $value ) )
632
+ continue;
633
+
634
+ $arreach['value'] = $value;
635
+ $arreach['whole'] = str_replace( $orig_value, $value, $arreach['whole'] );
636
+ }
637
+
638
+ if ( ! is_array($current) ) {
639
+ $attr2 .= ' '.$arreach['whole'];
640
+ # there are no checks
641
+
642
+ } else {
643
+ # there are some checks
644
+ $ok = true;
645
+ foreach ($current as $currkey => $currval) {
646
+ if ( ! wp_kses_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval) ) {
647
+ $ok = false;
648
+ break;
649
+ }
650
+ }
651
+
652
+ if ( $ok )
653
+ $attr2 .= ' '.$arreach['whole']; # it passed them
654
+ } # if !is_array($current)
655
+ } # foreach
656
+
657
+ # Remove any "<" or ">" characters
658
+ $attr2 = preg_replace('/[<>]/', '', $attr2);
659
+
660
+ return "<$element$attr2$xhtml_slash>";
661
+ }
662
+
663
+ static function wp_kses_hair($attr, $allowed_protocols) {
664
+ $attrarr = array ();
665
+ $mode = 0;
666
+ $attrname = '';
667
+ $uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action');
668
+
669
+ # Loop through the whole attribute list
670
+
671
+ while (strlen($attr) != 0) {
672
+ $working = 0; # Was the last operation successful?
673
+
674
+ switch ($mode) {
675
+ case 0 : # attribute name, href for instance
676
+
677
+ if (preg_match('/^([-a-zA-Z]+)/', $attr, $match)) {
678
+ $attrname = $match[1];
679
+ $working = $mode = 1;
680
+ $attr = preg_replace('/^[-a-zA-Z]+/', '', $attr);
681
+ }
682
+
683
+ break;
684
+
685
+ case 1 : # equals sign or valueless ("selected")
686
+
687
+ if (preg_match('/^\s*=\s*/', $attr)) # equals sign
688
+ {
689
+ $working = 1;
690
+ $mode = 2;
691
+ $attr = preg_replace('/^\s*=\s*/', '', $attr);
692
+ break;
693
+ }
694
+
695
+ if (preg_match('/^\s+/', $attr)) # valueless
696
+ {
697
+ $working = 1;
698
+ $mode = 0;
699
+ if(false === array_key_exists($attrname, $attrarr)) {
700
+ $attrarr[$attrname] = array ('name' => $attrname, 'value' => '', 'whole' => $attrname, 'vless' => 'y');
701
+ }
702
+ $attr = preg_replace('/^\s+/', '', $attr);
703
+ }
704
+
705
+ break;
706
+
707
+ case 2 : # attribute value, a URL after href= for instance
708
+
709
+ if (preg_match('%^"([^"]*)"(\s+|/?$)%', $attr, $match))
710
+ # "value"
711
+ {
712
+ $thisval = $match[1];
713
+ if ( in_array(strtolower($attrname), $uris) )
714
+ $thisval = VaultPress_kses::wp_kses_bad_protocol($thisval, $allowed_protocols);
715
+
716
+ if(false === array_key_exists($attrname, $attrarr)) {
717
+ $attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname=\"$thisval\"", 'vless' => 'n');
718
+ }
719
+ $working = 1;
720
+ $mode = 0;
721
+ $attr = preg_replace('/^"[^"]*"(\s+|$)/', '', $attr);
722
+ break;
723
+ }
724
+
725
+ if (preg_match("%^'([^']*)'(\s+|/?$)%", $attr, $match))
726
+ # 'value'
727
+ {
728
+ $thisval = $match[1];
729
+ if ( in_array(strtolower($attrname), $uris) )
730
+ $thisval = VaultPress_kses::wp_kses_bad_protocol($thisval, $allowed_protocols);
731
+
732
+ if(false === array_key_exists($attrname, $attrarr)) {
733
+ $attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname='$thisval'", 'vless' => 'n');
734
+ }
735
+ $working = 1;
736
+ $mode = 0;
737
+ $attr = preg_replace("/^'[^']*'(\s+|$)/", '', $attr);
738
+ break;
739
+ }
740
+
741
+ if (preg_match("%^([^\s\"']+)(\s+|/?$)%", $attr, $match))
742
+ # value
743
+ {
744
+ $thisval = $match[1];
745
+ if ( in_array(strtolower($attrname), $uris) )
746
+ $thisval = VaultPress_kses::wp_kses_bad_protocol($thisval, $allowed_protocols);
747
+
748
+ if(false === array_key_exists($attrname, $attrarr)) {
749
+ $attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname=\"$thisval\"", 'vless' => 'n');
750
+ }
751
+ # We add quotes to conform to W3C's HTML spec.
752
+ $working = 1;
753
+ $mode = 0;
754
+ $attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr);
755
+ }
756
+
757
+ break;
758
+ } # switch
759
+
760
+ if ($working == 0) # not well formed, remove and try again
761
+ {
762
+ $attr = wp_kses_html_error($attr);
763
+ $mode = 0;
764
+ }
765
+ } # while
766
+
767
+ if ($mode == 1 && false === array_key_exists($attrname, $attrarr))
768
+ # special case, for when the attribute list ends with a valueless
769
+ # attribute like "selected"
770
+ $attrarr[$attrname] = array ('name' => $attrname, 'value' => '', 'whole' => $attrname, 'vless' => 'y');
771
+
772
+ return $attrarr;
773
+ }
774
+
775
+ static function wp_kses_bad_protocol($string, $allowed_protocols) {
776
+ $string = wp_kses_no_null($string);
777
+ $iterations = 0;
778
+
779
+ do {
780
+ $original_string = $string;
781
+ $string = VaultPress_kses::wp_kses_bad_protocol_once($string, $allowed_protocols);
782
+ } while ( $original_string != $string && ++$iterations < 6 );
783
+
784
+ if ( $original_string != $string )
785
+ return '';
786
+
787
+ return $string;
788
+ }
789
+
790
+ static function wp_kses_bad_protocol_once($string, $allowed_protocols, $count = 1) {
791
+ $string2 = preg_split( '/:|&#0*58;|&#x0*3a;/i', $string, 2 );
792
+ if ( isset($string2[1]) && ! preg_match('%/\?%', $string2[0]) ) {
793
+ $string = trim( $string2[1] );
794
+ $protocol = VaultPress_kses::wp_kses_bad_protocol_once2( $string2[0], $allowed_protocols );
795
+ if ( 'feed:' == $protocol ) {
796
+ if ( $count > 2 )
797
+ return '';
798
+ $string = VaultPress_kses::wp_kses_bad_protocol_once( $string, $allowed_protocols, ++$count );
799
+ if ( empty( $string ) )
800
+ return $string;
801
+ }
802
+ $string = $protocol . $string;
803
+ }
804
+
805
+ return $string;
806
+ }
807
+
808
+ static function wp_kses_bad_protocol_once2( $string, $allowed_protocols ) {
809
+ $string2 = wp_kses_decode_entities($string);
810
+ $string2 = preg_replace('/\s/', '', $string2);
811
+ $string2 = wp_kses_no_null($string2);
812
+ $string2 = strtolower($string2);
813
+
814
+ $allowed = false;
815
+ foreach ( (array) $allowed_protocols as $one_protocol )
816
+ if ( strtolower($one_protocol) == $string2 ) {
817
+ $allowed = true;
818
+ break;
819
+ }
820
+
821
+ if ($allowed)
822
+ return "$string2:";
823
+ else
824
+ return '';
825
+ }
826
+
827
+ }
828
+
829
+ if ( !function_exists( 'get_available_languages' ) ) {
830
+ function get_available_languages( $dir = null ) {
831
+ $languages = array();
832
+ foreach( glob( ( is_null( $dir) ? WP_LANG_DIR : $dir ) . '/*.mo' ) as $lang_file )
833
+ if ( false === strpos( $lang_file, 'continents-cities' ) )
834
+ $languages[] = basename($lang_file, '.mo');
835
+ return $languages;
836
+ }
837
+ }
class.vaultpress-ixr-ssl-client.php ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ // don't call the file directly
4
+ if ( !defined( 'ABSPATH' ) )
5
+ return;
6
+
7
+ if ( !class_exists( 'IXR_Client' ) )
8
+ include_once( ABSPATH . WPINC . '/class-IXR.php' );
9
+
10
+ class VaultPress_IXR_SSL_Client extends IXR_Client {
11
+ var $ssl = false;
12
+ function VaultPress_IXR_SSL_Client( $server, $path = false, $port = 80, $timeout = false ) {
13
+ $this->IXR_Client( $server, $path, $port, $timeout );
14
+ }
15
+ function ssl( $port=443 ) {
16
+ if ( !extension_loaded( 'openssl' ) )
17
+ return;
18
+
19
+ $this->ssl = true;
20
+ if ( $port )
21
+ $this->port = $port;
22
+ }
23
+ function query() {
24
+ $args = func_get_args();
25
+ $method = array_shift($args);
26
+ $request = new IXR_Request($method, $args);
27
+ $length = $request->getLength();
28
+ $xml = $request->getXml();
29
+ $r = "\r\n";
30
+ $request = "POST {$this->path} HTTP/1.0$r";
31
+
32
+ $this->headers['Host'] = preg_replace( '#^ssl://#', '', $this->server );
33
+ $this->headers['Content-Type'] = 'text/xml';
34
+ $this->headers['User-Agent'] = $this->useragent;
35
+ $this->headers['Content-Length'] = $length;
36
+
37
+ if ( class_exists( 'WP_Http' ) ) {
38
+ $args = array(
39
+ 'method' => 'POST',
40
+ 'body' => $xml,
41
+ 'headers' => $this->headers,
42
+ 'sslverify' => false,
43
+ );
44
+ if ( $this->timeout )
45
+ $args['timeout'] = $this->timeout;
46
+
47
+ $http = new WP_Http();
48
+ if ( $this->ssl )
49
+ $url = sprintf( 'https://%s%s', $this->server, $this->path );
50
+ else
51
+ $url = sprintf( 'http://%s%s', $this->server, $this->path );
52
+
53
+ $result = $http->request( $url, $args );
54
+ if ( is_wp_error( $result ) ) {
55
+ foreach( $result->errors as $type => $messages ) {
56
+ $this->error = new IXR_Error(
57
+ -32702,
58
+ sprintf( 'WP_Http error: %s, %s', $type, $messages[0] )
59
+ );
60
+ break;
61
+ }
62
+ return false;
63
+ } else if ( $result['response']['code'] > 299 || $result['response']['code'] < 200 ) {
64
+ $this->error = new IXR_Error(
65
+ -32701,
66
+ sprintf( 'Server rejected request (HTTP response: %s %s)', $result['response']['code'], $result['response']['message'])
67
+ );
68
+ return false;
69
+ }
70
+ // Now parse what we've got back
71
+ $this->message = new IXR_Message( $result['body'] );
72
+ } else {
73
+ foreach( $this->headers as $header => $value ) {
74
+ $request .= "{$header}: {$value}{$r}";
75
+ }
76
+ $request .= $r;
77
+
78
+ $request .= $xml;
79
+ // Now send the request
80
+ if ( $this->ssl )
81
+ $host = 'ssl://'.$this->server;
82
+ else
83
+ $host = $this->server;
84
+ if ($this->timeout) {
85
+ $fp = @fsockopen( $host, $this->port, $errno, $errstr, $this->timeout );
86
+ } else {
87
+ $fp = @fsockopen( $host, $this->port, $errno, $errstr );
88
+ }
89
+ if (!$fp) {
90
+ $this->error = new IXR_Error( -32300, "Transport error - could not open socket: $errno $errstr" );
91
+ return false;
92
+ }
93
+ fputs( $fp, $request );
94
+
95
+ $contents = '';
96
+ $gotFirstLine = false;
97
+ $gettingHeaders = true;
98
+
99
+ while ( !feof($fp) ) {
100
+ $line = fgets( $fp, 4096 );
101
+ if ( !$gotFirstLine ) {
102
+ // Check line for '200'
103
+ if ( strstr($line, '200') === false ) {
104
+ $this->error = new IXR_Error( -32301, 'transport error - HTTP status code was not 200' );
105
+ return false;
106
+ }
107
+ $gotFirstLine = true;
108
+ }
109
+ if ( trim($line) == '' ) {
110
+ $gettingHeaders = false;
111
+ }
112
+ if ( !$gettingHeaders ) {
113
+ $contents .= trim( $line );
114
+ }
115
+ }
116
+ // Now parse what we've got back
117
+ $this->message = new IXR_Message( $contents );
118
+ }
119
+ if ( !$this->message->parse() ) {
120
+ // XML error
121
+ $this->error = new IXR_Error( -32700, 'parse error. not well formed' );
122
+ return false;
123
+ }
124
+ // Is the message a fault?
125
+ if ( $this->message->messageType == 'fault' ) {
126
+ $this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
127
+ return false;
128
+ }
129
+ // Message must be OK
130
+ return true;
131
+ }
132
+ }
cron-tasks.php ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ include_once dirname( __FILE__ ) . '/vp-scanner.php';
3
+
4
+ if ( !function_exists( 'apply_filters_ref_array' ) ) :
5
+
6
+ function apply_filters_ref_array($tag, $args) {
7
+ global $wp_filter, $merged_filters, $wp_current_filter;
8
+
9
+ // Do 'all' actions first
10
+ if ( isset($wp_filter['all']) ) {
11
+ $wp_current_filter[] = $tag;
12
+ $all_args = func_get_args();
13
+ _wp_call_all_hook($all_args);
14
+ }
15
+
16
+ if ( !isset($wp_filter[$tag]) ) {
17
+ if ( isset($wp_filter['all']) )
18
+ array_pop($wp_current_filter);
19
+ return $args[0];
20
+ }
21
+
22
+ if ( !isset($wp_filter['all']) )
23
+ $wp_current_filter[] = $tag;
24
+
25
+ // Sort
26
+ if ( !isset( $merged_filters[ $tag ] ) ) {
27
+ ksort($wp_filter[$tag]);
28
+ $merged_filters[ $tag ] = true;
29
+ }
30
+
31
+ reset( $wp_filter[ $tag ] );
32
+
33
+ do {
34
+ foreach( (array) current($wp_filter[$tag]) as $the_ )
35
+ if ( !is_null($the_['function']) )
36
+ $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
37
+
38
+ } while ( next($wp_filter[$tag]) !== false );
39
+
40
+ array_pop( $wp_current_filter );
41
+
42
+ return $args[0];
43
+ }
44
+
45
+ endif;
46
+
47
+ class VP_Site_Scanner {
48
+ function VP_Site_Scanner() {
49
+ self::__construct();
50
+ }
51
+ function __construct() {
52
+ // Only scan once in multisites.
53
+ if( function_exists( 'is_main_site' ) && !is_main_site() )
54
+ return;
55
+ add_action( 'vp_scan_site' , array( $this, '_scan_site') );
56
+ add_filter( 'cron_schedules' , array( $this, '_custom_cron' ) );
57
+ add_action( 'vp_scan_next_batch', array( $this, '_scan_batch' ) );
58
+
59
+ $signatures = get_option( '_vp_signatures' );
60
+ if ( $signatures && ! wp_next_scheduled( 'vp_scan_site' ) )
61
+ wp_schedule_event( time(), 'daily', 'vp_scan_site' );
62
+ if ( $signatures && ! wp_next_scheduled( 'vp_scan_next_batch' ) )
63
+ wp_schedule_event( time(), 'five_minutes_interval', 'vp_scan_next_batch' );
64
+ }
65
+
66
+ function _custom_cron( $schedules ) {
67
+ $schedules['five_minutes_interval'] = array(
68
+ 'interval' => 300,
69
+ 'display' => __( 'Once every five minutes' ),
70
+ );
71
+ return $schedules;
72
+ }
73
+
74
+ function _scan_site() {
75
+ if ( !get_option( '_vp_current_scan' ) ) {
76
+ $paths = array( 'root' => new VP_FileScan( ABSPATH ) );
77
+
78
+ // Is WP_CONTENT_DIR inside ABSPATH?
79
+ if ( is_dir( WP_CONTENT_DIR ) && strpos( realpath( WP_CONTENT_DIR ), realpath( ABSPATH ) . DIRECTORY_SEPARATOR ) !== 0 )
80
+ $paths['content'] = new VP_FileScan( WP_CONTENT_DIR );
81
+
82
+ // Is WP_PLUGIN_DIR inside ABSPATH or WP_CONTENT_DIR?
83
+ 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 )
84
+ $paths['plugins'] = new VP_FileScan( WP_PLUGIN_DIR );
85
+
86
+ // Is WPMU_PLUGIN_DIR inside ABSPATH or WP_CONTENT_DIR?
87
+ 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 )
88
+ $paths['mu-plugins'] = new VP_FileScan( WPMU_PLUGIN_DIR );
89
+
90
+ update_option( '_vp_current_scan', $paths );
91
+ }
92
+ }
93
+
94
+ function _scan_clean_up( &$paths, $type = null ) {
95
+ if( is_array( $paths ) )
96
+ unset( $paths[$type] );
97
+ if ( empty( $paths ) || !is_array( $paths ) ) {
98
+ delete_option( '_vp_current_scan' );
99
+ return true;
100
+ }
101
+ return false;
102
+ }
103
+
104
+ function _scan_batch() {
105
+ $paths = get_option( '_vp_current_scan' );
106
+ if ( empty( $paths ) || $this->_scan_clean_up( $paths ) )
107
+ return false;
108
+
109
+ reset( $paths );
110
+ list( $type, $current ) = each( $paths );
111
+ if ( !is_object( $current ) || empty( $current->last_dir ) )
112
+ return $this->_scan_clean_up( $paths, $type );
113
+
114
+ $default_batch_limit = 400;
115
+ if ( function_exists( 'set_time_limit' ) )
116
+ set_time_limit(0);
117
+ else
118
+ $default_batch_limit = 100; // avoid timeouts
119
+
120
+ $GLOBALS['vp_signatures'] = get_option( '_vp_signatures' );
121
+ if ( empty( $GLOBALS['vp_signatures'] ) )
122
+ return false;
123
+
124
+ $limit = get_option( '_vp_batch_file_size', $default_batch_limit );
125
+ $files = $current->get_files( $limit );
126
+
127
+ // No more files to scan.
128
+ if ( !$current->last_dir || count( $files ) < $limit )
129
+ unset( $paths[$type] );
130
+
131
+ update_option( '_vp_current_scan', $paths );
132
+ $results = array();
133
+ foreach ( $files as $file ) {
134
+ $verdict = vp_scan_file( $file );
135
+ if ( !empty( $verdict ) )
136
+ $results[$file] = array( 'hash' => @md5_file( $file ), 'verdict' => $verdict );
137
+ }
138
+
139
+ if ( !empty( $results ) ) {
140
+ $vaultpress = VaultPress::init();
141
+ $vaultpress->add_ping( 'security', array( 'suspicious_v2' => $results ) );
142
+ }
143
+ }
144
+
145
+ function &init() {
146
+ static $instance = false;
147
+ if ( !$instance )
148
+ $instance = new VP_Site_Scanner();
149
+ return $instance;
150
+ }
151
+ }
152
+ VP_Site_Scanner::init();
readme.txt ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === VaultPress ===
2
+ Contributors: automattic, aldenta, apokalyptik, briancolinger, shaunandrews, xknown
3
+ Tags: security, malware, virus, backups, scanning
4
+ Requires at least: 2.9.2
5
+ Tested up to: 3.4
6
+ Stable tag: 1.3.6
7
+
8
+ VaultPress is a subscription service offering realtime backup, automated security scanning, and support from WordPress experts.
9
+
10
+ == Description ==
11
+
12
+ [VaultPress](http://vaultpress.com/?utm_source=plugin-readme&utm_medium=description&utm_campaign=1.0) is a real-time backup and security scanning service designed and built by [Automattic](http://automattic.com/), the same company that operates 25+ million sites on WordPress.com.
13
+
14
+ [wpvideo TxdSIdpO]
15
+
16
+ For more information, check out [VaultPress.com](http://vaultpress.com/).
17
+
18
+ == Installation ==
19
+
20
+ 1. Search for VaultPress in the WordPress.org plugin directory and click install. Or, upload the files to your `wp-content/vaultpress/` folder.
21
+ 2. Visit `wp-admin/plugins.php` and activate the VaultPress plugin.
22
+ 3. Head to `wp-admin/admin.php?page=vaultpress` and enter your site&rsquo;s registration key. You can purchase your registration key at [VaultPress.com](http://vaultpress.com/plugin/?utm_source=plugin-readme&utm_medium=installation&utm_campaign=1.0)
23
+
24
+ You can find more detailed instructions at [http://vaultpress.com/help/](http://vaultpress.com/help/install-vaultpress/?utm_source=plugin-readme&utm_medium=description&utm_campaign=1.0)
25
+
26
+ == Frequently Asked Questions ==
27
+
28
+ View our full list of FAQs at [http://vaultpress.com/help/faq/](http://vaultpress.com/help/faq/?utm_source=plugin-readme&utm_medium=faq&utm_campaign=1.0)
29
+
30
+ = What’s included in each VaultPress plan? =
31
+
32
+ All plans include Realtime Backups, Downloadable Archives for Restoring, Vitality Statistics, and the Activity Log.
33
+
34
+ The Basic plan provides access to disaster recovery and support services.
35
+
36
+ The Premium plan provides priority recovery and support services, along with site migration assistance. The Premium plan provides automated security scanning of Core, Theme, and Plugin files.
37
+
38
+ The Enterprise members receive top priority for support services and migration assistance. Along with security scanning, Enterprise members receive complimentary security consulting and performance auditing.
39
+
40
+ Update-to-date pricing and features can always be found on the [Plans &amp; Pricing](http://vaultpress.com/plugin/?utm_source=plugin-readme&utm_medium=installation&utm_campaign=1.0) page.
41
+
42
+ = How many sites can I protect with VaultPress? =
43
+
44
+ A VaultPress subscription is for a single WordPress site. You can purchase additional subscriptions for each of your WordPress sites, and manage them all with in one place.
45
+
46
+ = Does VaultPress work with WordPress 3.0 Multisite installs? =
47
+
48
+ With our 1.0 release, VaultPress now supports Multisite installs. Each site will require its own subscription.
49
+
50
+ == Changelog ==
51
+
52
+ = 1.0 =
53
+ * First public release!
vaultpress.php ADDED
@@ -0,0 +1,1905 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ * Plugin Name: VaultPress
4
+ * Plugin URI: http://vaultpress.com/?utm_source=plugin-uri&amp;utm_medium=plugin-description&amp;utm_campaign=1.0
5
+ * Description: Protect your content, themes, plugins, and settings with <strong>realtime backup</strong> and <strong>automated security scanning</strong> from <a href="http://vaultpress.com/?utm_source=wp-admin&amp;utm_medium=plugin-description&amp;utm_campaign=1.0" rel="nofollow">VaultPress</a>. Activate, enter your registration key, and never worry again. <a href="http://vaultpress.com/help/?utm_source=wp-admin&amp;utm_medium=plugin-description&amp;utm_campaign=1.0" rel="nofollow">Need some help?</a>
6
+ * Version: 1.3.6
7
+ * Author: Automattic
8
+ * Author URI: http://vaultpress.com/?utm_source=author-uri&amp;utm_medium=plugin-description&amp;utm_campaign=1.0
9
+ * License: GPL2+
10
+ * Text Domain: vaultpress
11
+ * Domain Path: /languages/
12
+ */
13
+
14
+ // don't call the file directly
15
+ if ( !defined( 'ABSPATH' ) )
16
+ return;
17
+
18
+ class VaultPress {
19
+ var $option_name = 'vaultpress';
20
+ var $db_version = 2;
21
+ var $plugin_version = '1.3.6';
22
+
23
+ function VaultPress() {
24
+ $this->__construct();
25
+ }
26
+
27
+ function __construct() {
28
+ register_activation_hook( __FILE__, array( $this, 'activate' ) );
29
+ register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
30
+
31
+ $options = get_option( $this->option_name );
32
+ if ( !is_array( $options ) )
33
+ $options = array();
34
+
35
+ $defaults = array(
36
+ 'db_version' => 0,
37
+ 'key' => '',
38
+ 'secret' => '',
39
+ 'connection' => false,
40
+ 'service_ips' => false
41
+ );
42
+
43
+ $this->options = wp_parse_args( $options, $defaults );
44
+ $this->reset_pings();
45
+
46
+ $this->upgrade();
47
+
48
+ if ( is_admin() )
49
+ $this->add_admin_actions_and_filters();
50
+
51
+ if ( $this->is_registered() ) {
52
+ $do_backups = $this->get_option( 'do_backups' );
53
+ if ( $do_backups )
54
+ $this->add_listener_actions_and_filters();
55
+ else
56
+ $this->add_vp_required_filters();
57
+ }
58
+ }
59
+
60
+ function &init() {
61
+ static $instance = false;
62
+
63
+ if ( !$instance ) {
64
+ $instance = new VaultPress();
65
+ }
66
+
67
+ return $instance;
68
+ }
69
+
70
+ function activate( $network_wide ) {
71
+ $type = $network_wide ? 'network' : 'single';
72
+ $this->update_option( 'activated', $type );
73
+
74
+ // force a connection check after an activation
75
+ $this->clear_connection();
76
+ }
77
+
78
+ function deactivate() {
79
+ if ( $this->is_registered() )
80
+ $this->contact_service( 'plugin_status', array( 'vp_plugin_status' => 'deactivated' ) );
81
+ }
82
+
83
+ function upgrade() {
84
+ $current_db_version = $this->get_option( 'db_version' );
85
+
86
+ if ( $current_db_version < 1 ) {
87
+ $this->options['connection'] = get_option( 'vaultpress_connection' );
88
+ $this->options['key'] = get_option( 'vaultpress_key' );
89
+ $this->options['secret'] = get_option( 'vaultpress_secret' );
90
+ $this->options['service_ips'] = get_option( 'vaultpress_service_ips' );
91
+
92
+ // remove old options
93
+ $old_options = array(
94
+ 'vaultpress_connection',
95
+ 'vaultpress_hostname',
96
+ 'vaultpress_key',
97
+ 'vaultpress_secret',
98
+ 'vaultpress_service_ips',
99
+ 'vaultpress_timeout',
100
+ 'vp_allow_remote_execution',
101
+ 'vp_debug_request_signing',
102
+ 'vp_disable_firewall',
103
+ );
104
+
105
+ foreach ( $old_options as $option )
106
+ delete_option( $option );
107
+
108
+ $this->options['db_version'] = $this->db_version;
109
+ $this->update_options();
110
+ }
111
+
112
+ if ( $current_db_version < 2 ) {
113
+ $this->delete_option( 'timeout' );
114
+ $this->delete_option( 'disable_firewall' );
115
+ $this->update_option( 'db_version', $this->db_version );
116
+ $this->clear_connection();
117
+ }
118
+ }
119
+
120
+ function get_option( $key ) {
121
+ if ( 'hostname' == $key ) {
122
+ if ( defined( 'VAULTPRESS_HOSTNAME' ) )
123
+ return VAULTPRESS_HOSTNAME;
124
+ else
125
+ return 'vaultpress.com';
126
+ }
127
+
128
+ if ( 'timeout' == $key ) {
129
+ if ( defined( 'VAULTPRESS_TIMEOUT' ) )
130
+ return VAULTPRESS_TIMEOUT;
131
+ else
132
+ return 60;
133
+ }
134
+
135
+ if ( 'disable_firewall' == $key ) {
136
+ if ( defined( 'VAULTPRESS_DISABLE_FIREWALL' ) )
137
+ return VAULTPRESS_DISABLE_FIREWALL;
138
+ else
139
+ return false;
140
+ }
141
+
142
+ if ( isset( $this->options[$key] ) )
143
+ return $this->options[$key];
144
+
145
+ return false;
146
+ }
147
+
148
+ function update_option( $key, $value ) {
149
+ $this->options[$key] = $value;
150
+ $this->update_options();
151
+ }
152
+
153
+ function delete_option( $key ) {
154
+ unset( $this->options[$key] );
155
+ $this->update_options();
156
+ }
157
+
158
+ function update_options() {
159
+ update_option( $this->option_name, $this->options );
160
+ }
161
+
162
+ function admin_init() {
163
+ if ( !current_user_can( 'manage_options' ) )
164
+ return;
165
+
166
+ load_plugin_textdomain( 'vaultpress', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
167
+ }
168
+
169
+ function admin_head() {
170
+ if ( !current_user_can( 'manage_options' ) )
171
+ return;
172
+
173
+ if ( $activated = $this->get_option( 'activated' ) ) {
174
+ if ( 'network' == $activated ) {
175
+ add_action( 'network_admin_notices', array( $this, 'activated_notice' ) );
176
+ } else {
177
+ foreach ( array( 'user_admin_notices', 'admin_notices' ) as $filter )
178
+ add_action( $filter, array( $this, 'activated_notice' ) );
179
+ }
180
+ }
181
+
182
+ // ask the user to connect their site w/ VP
183
+ if ( !$this->is_registered() ) {
184
+ foreach ( array( 'user_admin_notices', 'admin_notices' ) as $filter )
185
+ add_action( $filter, array( $this, 'connect_notice' ) );
186
+
187
+ // if we have an error make sure to let the user know about it
188
+ } else {
189
+ $error_code = $this->get_option( 'connection_error_code' );
190
+ if ( !empty( $error_code ) ) {
191
+ foreach ( array( 'user_admin_notices', 'admin_notices' ) as $filter )
192
+ add_action( $filter, array( $this, 'error_notice' ) );
193
+ }
194
+ }
195
+ ?>
196
+
197
+ <style type="text/css">
198
+ #toplevel_page_vaultpress div.wp-menu-image {
199
+ background: url(<?php echo esc_url( $this->server_url() ); ?>images/vp-icon-sprite.png?20111216) center top no-repeat;
200
+ }
201
+
202
+ .admin-color-classic #toplevel_page_vaultpress div.wp-menu-image {
203
+ background-position: center -28px;
204
+ }
205
+
206
+ #toplevel_page_vaultpress.current div.wp-menu-image,
207
+ #toplevel_page_vaultpress:hover div.wp-menu-image {
208
+ background-position: center bottom;
209
+ }
210
+ </style>
211
+
212
+ <?php
213
+
214
+ }
215
+
216
+ function admin_menu() {
217
+ // if Jetpack is loaded then we need to wait for that menu to be added
218
+ if ( class_exists( 'Jetpack' ) )
219
+ add_action( 'jetpack_admin_menu', array( $this, 'load_menu' ) );
220
+ else
221
+ $this->load_menu();
222
+ }
223
+
224
+ function load_menu() {
225
+ if ( class_exists( 'Jetpack' ) ) {
226
+ $hook = add_submenu_page( 'jetpack', 'VaultPress', 'VaultPress', 'manage_options', 'vaultpress', array( $this, 'ui' ) );
227
+ } else {
228
+ $hook = add_menu_page( 'VaultPress', 'VaultPress', 'manage_options', 'vaultpress', array( $this, 'ui' ), 'div' );
229
+ }
230
+
231
+ add_action( "load-$hook", array( $this, 'ui_load' ) );
232
+ add_action( 'admin_print_styles', array( $this, 'styles' ) );
233
+ }
234
+
235
+ function styles() {
236
+ if ( !current_user_can( 'manage_options' ) )
237
+ return;
238
+
239
+ // force the cache to bust every day
240
+ wp_enqueue_style( 'vaultpress', $this->server_url() . 'css/plugin.css' , false, date( 'Ymd' ) );
241
+ }
242
+
243
+ // display a security threat notice if one exists
244
+ function toolbar( $wp_admin_bar ) {
245
+ global $wp_version;
246
+
247
+ // these new toolbar functions were introduced in 3.3
248
+ // http://codex.wordpress.org/Function_Reference/add_node
249
+ if ( version_compare( $wp_version, '3.3', '<') )
250
+ return;
251
+
252
+ if ( !current_user_can( 'manage_options' ) )
253
+ return;
254
+
255
+ $messages = $this->get_messages();
256
+ if ( !empty( $messages['security_notice_count'] ) ) {
257
+ $count = (int)$messages['security_notice_count'];
258
+ if ( $count > 0 ) {
259
+ $count = number_format( $count, 0 );
260
+ $wp_admin_bar->add_node( array(
261
+ 'id' => 'vp-notice',
262
+ 'title' => '<strong><span class="ab-icon"></span>' .
263
+ sprintf( _n( '%s Security Threat', '%s Security Threats', $count ), $count ) .
264
+ ' </strong>',
265
+ 'parent' => 'top-secondary',
266
+ 'href' => sprintf( 'https://dashboard.vaultpress.com/%d/security/', $messages['site_id'] ),
267
+ 'meta' => array(
268
+ 'title' => __( 'Visit VaultPress Security' ),
269
+ 'onclick' => 'window.open( this.href ); return false;',
270
+ 'class' => 'error'
271
+ ),
272
+ ) );
273
+ }
274
+ }
275
+ }
276
+
277
+ // get any messages from the VP servers
278
+ function get_messages( $force_reload = false ) {
279
+ $last_contact = $this->get_option( 'messages_last_contact' );
280
+
281
+ // only run the messages check every 30 minutes
282
+ if ( ( time() - (int)$last_contact ) > 1800 || $force_reload ) {
283
+ $messages = base64_decode( $this->contact_service( 'messages', array() ) );
284
+ $messages = unserialize( $messages );
285
+ $this->update_option( 'messages_last_contact', time() );
286
+ $this->update_option( 'messages', $messages );
287
+ } else {
288
+ $messages = $this->get_option( 'messages' );
289
+ }
290
+
291
+ return $messages;
292
+ }
293
+
294
+ function server_url() {
295
+ if ( !isset( $this->_server_url ) ) {
296
+ $scheme = is_ssl() ? 'https' : 'http';
297
+ $this->_server_url = sprintf( '%s://%s/', $scheme, $this->get_option( 'hostname' ) );
298
+ }
299
+
300
+ return $this->_server_url;
301
+ }
302
+
303
+ // show message if plugin is activated but not connected to VaultPress
304
+ function connect_notice() {
305
+ if ( isset( $_GET['page'] ) && 'vaultpress' == $_GET['page'] )
306
+ return;
307
+
308
+ $message = sprintf(
309
+ __( 'You must enter your registration key before VaultPress can back up and secure your site. <a href="%1$s">Register&nbsp;VaultPress</a>', 'vaultpress' ),
310
+ admin_url( 'admin.php?page=vaultpress' )
311
+ );
312
+ $this->ui_message( $message, 'notice', __( 'VaultPress needs your attention!', 'vaultpress' ) );
313
+ }
314
+
315
+ // show message after activation
316
+ function activated_notice() {
317
+ if ( 'network' == $this->get_option( 'activated' ) ) {
318
+ $message = sprintf(
319
+ __( 'Each site will need to be registered with VaultPress separately. You can purchase new keys from your <a href="%1$s">VaultPress&nbsp;Dashboard</a>.', 'vaultpress' ),
320
+ 'https://dashboard.vaultpress.com/'
321
+ );
322
+ $this->ui_message( $message, 'activated', __( 'VaultPress has been activated across your network!', 'vaultpress' ) );
323
+
324
+ // key and secret already exist in db
325
+ } elseif ( $this->is_registered() ) {
326
+ if ( $this->check_connection() ) {
327
+ $message = sprintf(
328
+ __( 'VaultPress has been registered and is currently backing up your site. <a href="%1$s">View Backup Status</a>', 'vaultpress' ),
329
+ admin_url( 'admin.php?page=vaultpress' )
330
+ );
331
+ $this->ui_message( $message, 'registered', __( 'VaultPress has been activated!', 'vaultpress' ) );
332
+ }
333
+ }
334
+
335
+ $this->delete_option( 'activated' );
336
+ }
337
+
338
+ function error_notice() {
339
+ $error_message = $this->get_option( 'connection_error_message' );
340
+
341
+ // link to the VaultPress page if we're not already there
342
+ if ( !isset( $_GET['page'] ) || 'vaultpress' != $_GET['page'] )
343
+ $error_message .= ' ' . sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=vaultpress' ), __( 'Visit&nbsp;the&nbsp;VaultPress&nbsp;page' ) );
344
+
345
+ if ( !empty( $error_message ) )
346
+ $this->ui_message( $error_message, 'error' );
347
+ }
348
+
349
+ function ui() {
350
+ if ( !empty( $_GET['error'] ) ) {
351
+ $this->error_notice();
352
+ $this->clear_connection();
353
+ }
354
+
355
+ if ( !$this->is_registered() ) {
356
+ $this->ui_register();
357
+ return;
358
+ }
359
+
360
+ $status = $this->contact_service( 'status' );
361
+ if ( !$status ) {
362
+ $error_code = $this->get_option( 'connection_error_code' );
363
+ if ( 0 == $error_code )
364
+ $this->ui_fatal_error();
365
+ else
366
+ $this->ui_register();
367
+ return;
368
+ }
369
+
370
+ $ticker = $this->contact_service( 'ticker' );
371
+ if ( is_array( $ticker ) && isset( $ticker['faultCode'] ) ) {
372
+ $this->error_notice();
373
+ $this->ui_register();
374
+ return;
375
+ }
376
+
377
+ $this->ui_main();
378
+ }
379
+
380
+ function ui_load() {
381
+ if ( !current_user_can( 'manage_options' ) )
382
+ return;
383
+
384
+ // run code that might be updating the registration key
385
+ if ( isset( $_POST['action'] ) && 'register' == $_POST['action'] ) {
386
+ check_admin_referer( 'vaultpress_register' );
387
+
388
+ // reset the connection info so messages don't cross
389
+ $this->clear_connection();
390
+
391
+ $registration_key = trim( $_POST[ 'registration_key' ] );
392
+ if ( empty( $registration_key ) ) {
393
+ $this->update_option( 'connection_error_code', 1 );
394
+ $this->update_option(
395
+ 'connection_error_message',
396
+ sprintf(
397
+ __( '<strong>That\'s not a valid registration key.</strong> Head over to the <a href="%1$s" title="Sign in to your VaultPress Dashboard">VaultPress&nbsp;Dashboard</a> to find your key.', 'vaultpress' ),
398
+ 'https://dashboard.vaultpress.com/'
399
+ )
400
+ );
401
+ wp_redirect( admin_url( 'admin.php?page=vaultpress&error=true' ) );
402
+ exit();
403
+ }
404
+
405
+ // try to register the plugin
406
+ $nonce = wp_create_nonce( 'vp_register_' . $registration_key );
407
+ $args = array( 'registration_key' => $registration_key, 'nonce' => $nonce );
408
+ $response = $this->contact_service( 'register', $args );
409
+
410
+ // we received an error from the VaultPress servers
411
+ if ( !empty( $response['faultCode'] ) ) {
412
+ $this->update_option( 'connection_error_code', $response['faultCode'] );
413
+ $this->update_option( 'connection_error_message', $response['faultString'] );
414
+ wp_redirect( admin_url( 'admin.php?page=vaultpress&error=true' ) );
415
+ exit();
416
+ }
417
+
418
+ // make sure the returned data looks valid
419
+ if ( empty( $response['key'] ) || empty( $response['secret'] ) || empty( $response['nonce'] ) || $nonce != $response['nonce'] ) {
420
+ $this->update_option( 'connection_error_code', 1 );
421
+ $this->update_option( 'connection_error_message', sprintf( __( 'There was a problem trying to register your subscription. Please try again. If you&rsquo;re still having issues please <a href="%1$s">contact the VaultPress&nbsp;Safekeepers</a>.', 'vaultpress' ), 'http://vaultpress.com/contact/' ) );
422
+ wp_redirect( admin_url( 'admin.php?page=vaultpress&error=true' ) );
423
+ exit();
424
+ }
425
+
426
+ // need to update these values in the db so the servers can try connecting to the plugin
427
+ $this->update_option( 'key', $response['key'] );
428
+ $this->update_option( 'secret', $response['secret'] );
429
+ if ( $this->check_connection( true ) ) {
430
+ wp_redirect( admin_url( 'admin.php?page=vaultpress' ) );
431
+ exit();
432
+ }
433
+
434
+ // reset the key and secret
435
+ $this->update_option( 'key', '' );
436
+ $this->update_option( 'secret', '' );
437
+ wp_redirect( admin_url( 'admin.php?page=vaultpress&error=true' ) );
438
+ exit();
439
+ }
440
+ }
441
+
442
+ function ui_register() {
443
+ ?>
444
+ <div id="vp-wrap" class="wrap">
445
+ <div id="vp-head">
446
+ <h2>VaultPress<a href="https://dashboard.vaultpress.com/" class="vp-visit-dashboard" target="_blank"><?php _e( 'Visit Dashboard', 'vaultpress' ); ?></a></h2>
447
+ </div>
448
+
449
+ <div id="vp_registration">
450
+ <div class="vp_view-plans">
451
+ <h1><?php _e( 'The VaultPress plugin <strong>requires a monthly&nbsp;subscription</strong>.', 'vaultpress' ); ?></h1>
452
+ <p><?php _e( 'Get realtime backups, automated security scanning, and support from WordPress&nbsp;experts.', 'vaultpress' ); ?></p>
453
+ <p class="vp_plans-btn"><a href="https://vaultpress.com/plugin/?utm_source=plugin-unregistered&amp;utm_medium=view-plans-and-pricing&amp;utm_campaign=1.0-plugin"><strong><?php _e( 'View plans and pricing&nbsp;&raquo;', 'vaultpress' ); ?></strong></a></p>
454
+ </div>
455
+
456
+ <div class="vp_register-plugin">
457
+ <h3><?php _e( 'Already have a VaultPress&nbsp;account?', 'vaultpress' ); ?></h3>
458
+ <p><?php _e( 'Paste your registration key&nbsp;below:', 'vaultpress' ); ?></p>
459
+ <form method="post" action="">
460
+ <fieldset>
461
+ <textarea placeholder="<?php echo esc_attr( __( 'Enter your key here...', 'vaultpress' ) ); ?>" name="registration_key"></textarea>
462
+ <button><strong><?php _e( 'Register ', 'vaultpress' ); ?></strong></button>
463
+ <input type="hidden" name="action" value="register" />
464
+ <?php wp_nonce_field( 'vaultpress_register' ); ?>
465
+ </fieldset>
466
+ </form>
467
+ </div>
468
+ </div>
469
+ </div>
470
+ <?php
471
+ }
472
+
473
+ function ui_main() {
474
+ ?>
475
+ <div id="vp-wrap" class="wrap">
476
+ <?php
477
+ $response = base64_decode( $this->contact_service( 'plugin_ui' ) );
478
+ echo $response;
479
+ ?>
480
+ </div>
481
+ <?php
482
+ }
483
+
484
+ function ui_fatal_error() {
485
+ ?>
486
+ <div id="vp-wrap" class="wrap">
487
+ <h2>VaultPress</h2>
488
+
489
+ <p><?php printf( __( 'Yikes! We&rsquo;ve run into a serious issue and can&rsquo;t connect to %1$s.', 'vaultpress' ), esc_html( $this->get_option( 'hostname' ) ) ); ?></p>
490
+ <p><?php printf( __( 'Please make sure that your website is accessible via the Internet. If you&rsquo;re still having issues please <a href="%1$s">contact the VaultPress&nbsp;Safekeepers</a>.', 'vaultpress' ), 'http://vaultpress.com/contact/' ); ?></p>
491
+ </div>
492
+ <?php
493
+ }
494
+
495
+ function ui_message( $message, $type = 'notice', $heading = '' ) {
496
+ if ( empty( $heading ) ) {
497
+ switch ( $type ) {
498
+ case 'error':
499
+ $heading = __( 'Oops... there seems to be a problem.', 'vaultpress' );
500
+ break;
501
+
502
+ case 'success':
503
+ $heading = __( 'Yay! Things look good.', 'vaultpress' );
504
+ break;
505
+
506
+ default:
507
+ $heading = __( 'VaultPress needs your attention!', 'vaultpress' );
508
+ break;
509
+ }
510
+ }
511
+ ?>
512
+ <div id="vp-notice" class="vp-<?php echo $type; ?> updated">
513
+ <div class="vp-message">
514
+ <h3><?php echo $heading; ?></h3>
515
+ <p><?php echo $message; ?></p>
516
+ </div>
517
+ </div>
518
+ <?php
519
+ }
520
+
521
+ function get_config( $key ) {
522
+ $val = get_option( $key );
523
+ if ( $val )
524
+ return $val;
525
+ switch( $key ) {
526
+ case '_vp_config_option_name_ignore':
527
+ $val = $this->get_option_name_ignore( true );
528
+ update_option( '_vp_config_option_name_ignore', $val );
529
+ break;
530
+ }
531
+ return $val;
532
+ }
533
+
534
+ // Option name patterns to ignore
535
+ function get_option_name_ignore( $return_defaults = false ) {
536
+ $defaults = array(
537
+ 'vaultpress',
538
+ 'cron',
539
+ 'wpsupercache_gc_time',
540
+ 'rewrite_rules',
541
+ 'akismet_spam_count',
542
+ '/_transient_/',
543
+ '/^_vp_/',
544
+ );
545
+ if ( $return_defaults )
546
+ return $defaults;
547
+ $ignore_names = $this->get_config( '_vp_config_option_name_ignore' );
548
+ return array_unique( array_merge( $defaults, $ignore_names ) );
549
+ }
550
+
551
+ ###
552
+ ### Section: Backup Notification Hooks
553
+ ###
554
+
555
+ // Handle Handle Notifying VaultPress of Options Activity At this point the options table has already been modified
556
+ //
557
+ // Note: we handle deleted, instead of delete because VaultPress backs up options by name (which are unique,) that
558
+ // means that we do not need to resolve an id like we would for, say, a post.
559
+ function option_handler( $option_name ) {
560
+ global $wpdb;
561
+ // Step 1 -- exclusionary rules, don't send these options to vaultpress, because they
562
+ // either change constantly and/or are inconsequential to the blog itself and/or they
563
+ // are specific to the VaultPress plugin process and we want to avoid recursion
564
+ $should_ping = true;
565
+ $ignore_names = $this->get_option_name_ignore();
566
+ foreach( (array)$ignore_names as $val ) {
567
+ if ( $val{0} == '/' ) {
568
+ if ( preg_match( $val, $option_name ) )
569
+ $should_ping = false;
570
+ } else {
571
+ if ( $val == $option_name )
572
+ $should_ping = false;
573
+ }
574
+ if ( !$should_ping )
575
+ break;
576
+ }
577
+ if ( $should_ping )
578
+ $this->add_ping( 'db', array( 'option' => $option_name ) );
579
+
580
+ // Step 2 -- If WordPress is about to kick off a some "cron" action, we need to
581
+ // flush vaultpress, because the "remote" cron threads done via http fetch will
582
+ // be happening completely inside the window of this thread. That thread will
583
+ // be expecting touched and accounted for tables
584
+ if ( $option_name == '_transient_doing_cron' )
585
+ $this->do_pings();
586
+
587
+ return $option_name;
588
+ }
589
+
590
+ // Handle Notifying VaultPress of Comment Activity
591
+ function comment_action_handler( $comment_id ) {
592
+ if ( !is_array( $comment_id ) ) {
593
+ if ( wp_get_comment_status( $comment_id ) != 'spam' )
594
+ $this->add_ping( 'db', array( 'comment' => $comment_id ) );
595
+ } else {
596
+ foreach ( $comment_id as $id ) {
597
+ if ( wp_get_comment_status( $comment_id ) != 'spam' )
598
+ $this->add_ping( 'db', array( 'comment' => $id) );
599
+ }
600
+ }
601
+ }
602
+
603
+ // Handle Notifying VaultPress of Theme Switches
604
+ function theme_action_handler( $theme ) {
605
+ $this->add_ping( 'themes', array( 'theme' => get_option( 'stylesheet' ) ) );
606
+ }
607
+
608
+ // Handle Notifying VaultPress of Upload Activity
609
+ function upload_handler( $file ) {
610
+ $this->add_ping( 'uploads', array( 'upload' => str_replace( $this->resolve_upload_path(), '', $file['file'] ) ) );
611
+ return $file;
612
+ }
613
+
614
+ // Handle Notifying VaultPress of Plugin Activation/Deactivation
615
+ function plugin_action_handler( $plugin='' ) {
616
+ $this->add_ping( 'plugins', array( 'name' => $plugin ) );
617
+ }
618
+
619
+ // Handle Notifying VaultPress of User Edits
620
+ function userid_action_handler( $user_or_id ) {
621
+ if ( is_object($user_or_id) )
622
+ $userid = intval( $user_or_id->ID );
623
+ else
624
+ $userid = intval( $user_or_id );
625
+ if ( !$userid )
626
+ return;
627
+ $this->add_ping( 'db', array( 'user' => $userid ) );
628
+ }
629
+
630
+ // Handle Notifying VaultPress of term changes
631
+ function term_handler( $term_id, $tt_id=null ) {
632
+ $this->add_ping( 'db', array( 'term' => $term_id ) );
633
+ if ( $tt_id )
634
+ $this->term_taxonomy_handler( $tt_id );
635
+ }
636
+
637
+ // Handle Notifying VaultPress of term_taxonomy changes
638
+ function term_taxonomy_handler( $tt_id ) {
639
+ $this->add_ping( 'db', array( 'term_taxonomy' => $tt_id ) );
640
+ }
641
+ // add(ed)_term_taxonomy handled via the created_term hook, the term_taxonomy_handler is called by the term_handler
642
+
643
+ // Handle Notifying VaultPress of term_taxonomy changes
644
+ function term_taxonomies_handler( $tt_ids ) {
645
+ foreach( (array)$tt_ids as $tt_id ) {
646
+ $this->term_taxonomy_handler( $tt_id );
647
+ }
648
+ }
649
+
650
+ // Handle Notifying VaultPress of term_relationship changes
651
+ function term_relationship_handler( $object_id, $term_id ) {
652
+ $this->add_ping( 'db', array( 'term_relationship' => array( 'object_id' => $object_id, 'term_taxonomy_id' => $term_id ) ) );
653
+ }
654
+
655
+ // Handle Notifying VaultPress of term_relationship changes
656
+ function term_relationships_handler( $object_id, $term_ids ) {
657
+ foreach ( (array)$term_ids as $term_id ) {
658
+ $this->term_relationship_handler( $object_id, $term_id );
659
+ }
660
+ }
661
+
662
+ // Handle Notifying VaultPress of term_relationship changes
663
+ function set_object_terms_handler( $object_id, $terms, $tt_ids ) {
664
+ $this->term_relationships_handler( $object_id, $tt_ids );
665
+ }
666
+
667
+ // Handle Notifying VaultPress of UserMeta changes
668
+ function usermeta_action_handler( $umeta_id, $user_id, $meta_key, $meta_value='' ) {
669
+ $this->add_ping( 'db', array( 'usermeta' => $umeta_id ) );
670
+ }
671
+
672
+ // Handle Notifying VaultPress of Post Changes
673
+ function post_action_handler($post_id) {
674
+ if ( current_filter() == 'delete_post' )
675
+ return $this->add_ping( 'db', array( 'post' => $post_id ), 'delete_post' );
676
+ return $this->add_ping( 'db', array( 'post' => $post_id ), 'edit_post' );
677
+ }
678
+
679
+ // Handle Notifying VaultPress of Link Changes
680
+ function link_action_handler( $link_id ) {
681
+ $this->add_ping( 'db', array( 'link' => $link_id ) );
682
+ }
683
+
684
+ // Handle Notifying VaultPress of Commentmeta Changes
685
+ function commentmeta_insert_handler( $meta_id, $comment_id=null ) {
686
+ if ( empty( $comment_id ) || wp_get_comment_status( $comment_id ) != 'spam' )
687
+ $this->add_ping( 'db', array( 'commentmeta' => $meta_id ) );
688
+ }
689
+
690
+ function commentmeta_modification_handler( $meta_id, $object_id, $meta_key, $meta_value ) {
691
+ if ( !is_array( $meta_id ) )
692
+ return $this->add_ping( 'db', array( 'commentmeta' => $meta_id ) );
693
+ foreach ( $meta_id as $id ) {
694
+ $this->add_ping( 'db', array( 'commentmeta' => $id ) );
695
+ }
696
+ }
697
+
698
+ // Handle Notifying VaultPress of PostMeta changes via newfangled metadata functions
699
+ function postmeta_insert_handler( $meta_id, $post_id, $meta_key, $meta_value='' ) {
700
+ $this->add_ping( 'db', array( 'postmeta' => $meta_id ) );
701
+ }
702
+
703
+ function postmeta_modification_handler( $meta_id, $object_id, $meta_key, $meta_value ) {
704
+ if ( !is_array( $meta_id ) )
705
+ return $this->add_ping( 'db', array( 'postmeta' => $meta_id ) );
706
+ foreach ( $meta_id as $id ) {
707
+ $this->add_ping( 'db', array( 'postmeta' => $id ) );
708
+ }
709
+ }
710
+
711
+ // Handle Notifying VaultPress of PostMeta changes via old school cherypicked hooks
712
+ function postmeta_action_handler( $meta_id ) {
713
+ if ( !is_array($meta_id) )
714
+ return $this->add_ping( 'db', array( 'postmeta' => $meta_id ) );
715
+ foreach ( $meta_id as $id )
716
+ $this->add_ping( 'db', array( 'postmeta' => $id ) );
717
+ }
718
+
719
+ function verify_table( $table ) {
720
+ global $wpdb;
721
+ $table = $wpdb->escape( $table );
722
+ $status = $wpdb->get_row( "SHOW TABLE STATUS WHERE Name = '$table'" );
723
+ if ( !$status || !$status->Update_time || !$status->Comment || $status->Engine != 'MyISAM' )
724
+ return true;
725
+ if ( preg_match( '/([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})/', $status->Comment, $m ) )
726
+ return ( $m[1] == $status->Update_time );
727
+ return false;
728
+ }
729
+
730
+ // Emulate $wpdb->last_table
731
+ function record_table( $table ) {
732
+ global $vaultpress_last_table;
733
+ $vaultpress_last_table = $table;
734
+ return $table;
735
+ }
736
+
737
+ // Emulate $wpdb->last_table
738
+ function get_last_table() {
739
+ global $wpdb, $vaultpress_last_table;
740
+ if ( is_object( $wpdb ) && isset( $wpdb->last_table ) )
741
+ return $wpdb->last_table;
742
+ return $vaultpress_last_table;
743
+ }
744
+
745
+ // Emulate hyperdb::is_write_query()
746
+ function is_write_query( $q ) {
747
+ $word = strtoupper( substr( trim( $q ), 0, 20 ) );
748
+ if ( 0 === strpos( $word, 'SELECT' ) )
749
+ return false;
750
+ if ( 0 === strpos( $word, 'SHOW' ) )
751
+ return false;
752
+ if ( 0 === strpos( $word, 'CHECKSUM' ) )
753
+ return false;
754
+ return true;
755
+ }
756
+
757
+ // Emulate hyperdb::get_table_from_query()
758
+ function get_table_from_query( $q ) {
759
+ global $wpdb, $vaultpress_last_table;
760
+
761
+ if ( is_object( $wpdb ) && method_exists( $wpdb, "get_table_from_query" ) )
762
+ return $wpdb->get_table_from_query( $q );
763
+
764
+ // Remove characters that can legally trail the table name
765
+ $q = rtrim( $q, ';/-#' );
766
+ // allow ( select... ) union [...] style queries. Use the first queries table name.
767
+ $q = ltrim( $q, "\t (" );
768
+
769
+ // Quickly match most common queries
770
+ if ( preg_match( '/^\s*(?:'
771
+ . 'SELECT.*?\s+FROM'
772
+ . '|INSERT(?:\s+IGNORE)?(?:\s+INTO)?'
773
+ . '|REPLACE(?:\s+INTO)?'
774
+ . '|UPDATE(?:\s+IGNORE)?'
775
+ . '|DELETE(?:\s+IGNORE)?(?:\s+FROM)?'
776
+ . ')\s+`?(\w+)`?/is', $q, $maybe) )
777
+ return $this->record_table($maybe[1] );
778
+
779
+ // Refer to the previous query
780
+ if ( preg_match( '/^\s*SELECT.*?\s+FOUND_ROWS\(\)/is', $q ) )
781
+ return $this->get_last_table();
782
+
783
+ // Big pattern for the rest of the table-related queries in MySQL 5.0
784
+ if ( preg_match( '/^\s*(?:'
785
+ . '(?:EXPLAIN\s+(?:EXTENDED\s+)?)?SELECT.*?\s+FROM'
786
+ . '|INSERT(?:\s+LOW_PRIORITY|\s+DELAYED|\s+HIGH_PRIORITY)?(?:\s+IGNORE)?(?:\s+INTO)?'
787
+ . '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?'
788
+ . '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?'
789
+ . '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:\s+FROM)?'
790
+ . '|DESCRIBE|DESC|EXPLAIN|HANDLER'
791
+ . '|(?:LOCK|UNLOCK)\s+TABLE(?:S)?'
792
+ . '|(?:RENAME|OPTIMIZE|BACKUP|RESTORE|CHECK|CHECKSUM|ANALYZE|OPTIMIZE|REPAIR).*\s+TABLE'
793
+ . '|TRUNCATE(?:\s+TABLE)?'
794
+ . '|CREATE(?:\s+TEMPORARY)?\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?'
795
+ . '|ALTER(?:\s+IGNORE)?\s+TABLE'
796
+ . '|DROP\s+TABLE(?:\s+IF\s+EXISTS)?'
797
+ . '|CREATE(?:\s+\w+)?\s+INDEX.*\s+ON'
798
+ . '|DROP\s+INDEX.*\s+ON'
799
+ . '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE'
800
+ . '|(?:GRANT|REVOKE).*ON\s+TABLE'
801
+ . '|SHOW\s+(?:.*FROM|.*TABLE)'
802
+ . ')\s+`?(\w+)`?/is', $q, $maybe ) )
803
+ return $this->record_table( $maybe[1] );
804
+
805
+ // All unmatched queries automatically fall to the global master
806
+ return $this->record_table( '' );
807
+ }
808
+
809
+ function table_notify_columns( $table ) {
810
+ $want_cols = array(
811
+ // data
812
+ 'posts' => '`ID`',
813
+ 'users' => '`ID`',
814
+ 'links' => '`link_id`',
815
+ 'options' => '`option_id`,`option_name`',
816
+ 'comments' => '`comment_ID`',
817
+ // metadata
818
+ 'postmeta' => '`meta_id`',
819
+ 'commentmeta' => '`meta_id`',
820
+ 'usermeta' => '`umeta_id`',
821
+ // taxonomy
822
+ 'term_relationships' => '`object_id`,`term_taxonomy_id`',
823
+ 'term_taxonomy' => '`term_taxonomy_id`',
824
+ 'terms' => '`term_id`',
825
+ // plugin special cases
826
+ 'wpo_campaign' => '`id`', // WP-o-Matic
827
+ 'wpo_campaign_category' => '`id`', // WP-o-Matic
828
+ 'wpo_campaign_feed' => '`id`', // WP-o-Matic
829
+ 'wpo_campaign_post' => '`id`', // WP-o-Matic
830
+ 'wpo_campaign_word' => '`id`', // WP-o-Matic
831
+ 'wpo_log' => '`id`', // WP-o-Matic
832
+ );
833
+ if ( isset( $want_cols[$table] ) )
834
+ return $want_cols[$table];
835
+ return '*';
836
+ }
837
+
838
+ function ai_ping_next() {
839
+ global $wpdb;
840
+ $name = "_vp_ai_ping";
841
+ $rval = $wpdb->query( $wpdb->prepare( "REPLACE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, '', 'no')", $name ) );
842
+ if ( !$rval )
843
+ return false;
844
+ return $wpdb->insert_id;
845
+ }
846
+
847
+ function ai_ping_insert( $value ) {
848
+ $new_id = $this->ai_ping_next();
849
+ if ( !$new_id )
850
+ return false;
851
+ add_option( '_vp_ai_ping_' . $new_id, $value, '', 'no' );
852
+ }
853
+
854
+ function ai_ping_count() {
855
+ global $wpdb;
856
+ return $wpdb->get_var( "SELECT COUNT(`option_id`) FROM $wpdb->options WHERE `option_name` LIKE '\_vp\_ai\_ping\_%'" );
857
+ }
858
+
859
+ function ai_ping_get( $num=1, $order='ASC' ) {
860
+ global $wpdb;
861
+ if ( strtolower($order) != 'desc' )
862
+ $order = 'ASC';
863
+ else
864
+ $order = 'DESC';
865
+ return $wpdb->get_results( $wpdb->prepare(
866
+ "SELECT * FROM $wpdb->options WHERE `option_name` LIKE '\_vp\_ai\_ping\_%%' ORDER BY `option_id` $order LIMIT %d",
867
+ min( 10, max( 1, (int)$num ) )
868
+ ) );
869
+ }
870
+
871
+ function update_firewall() {
872
+ $args = array( 'timeout' => $this->get_option( 'timeout' ) );
873
+ $hostname = $this->get_option( 'hostname' );
874
+ $data = wp_remote_get( "http://$hostname/service-ips", $args );
875
+
876
+ if ( $data )
877
+ $data = @unserialize( $data['body'] );
878
+
879
+ if ( $data ) {
880
+ $newval = array( 'updated' => time(), 'data' => $data );
881
+ $this->update_option( 'service_ips', $newval );
882
+ return $data;
883
+ }
884
+
885
+ return null;
886
+ }
887
+
888
+ function check_connection( $force_check = false ) {
889
+ $connection = $this->get_option( 'connection' );
890
+
891
+ if ( !$force_check && !empty( $connection ) ) {
892
+ // already established a connection
893
+ if ( 'ok' == $connection )
894
+ return true;
895
+
896
+ // only run the connection check every 5 minutes
897
+ if ( ( time() - (int)$connection ) < 300 )
898
+ return false;
899
+ }
900
+
901
+ // if we're running a connection test we don't want to run it a second time
902
+ $connection_test = $this->get_option( 'connection_test' );
903
+ if ( $connection_test )
904
+ return true;
905
+
906
+ // force update firewall settings
907
+ $this->update_firewall();
908
+
909
+ // initial connection test to server
910
+ $this->update_option( 'connection_test', true );
911
+ $this->delete_option( 'allow_forwarded_for' );
912
+ $connect = $this->contact_service( 'test', array( 'host' => $_SERVER['HTTP_HOST'], 'uri' => $_SERVER['REQUEST_URI'], 'ssl' => is_ssl() ) );
913
+
914
+ // we can't see the servers at all
915
+ if ( !$connect ) {
916
+ $this->update_option( 'connection', time() );
917
+ $this->update_option( 'connection_error_code', 0 );
918
+ $this->update_option( 'connection_error_message', sprintf( __( 'Cannot connect to the VaultPress servers. Please check that your host allows connecting to external sites and try again. If you&rsquo;re still having issues please <a href="%1$s">contact the VaultPress&nbsp;Safekeepers</a>.', 'vaultpress' ), 'http://vaultpress.com/contact/' ) );
919
+
920
+ $this->delete_option( 'connection_test' );
921
+ return false;
922
+ }
923
+
924
+ // VaultPress gave us a meaningful error
925
+ if ( !empty( $connect['faultCode'] ) ) {
926
+ $this->update_option( 'connection', time() );
927
+ $this->update_option( 'connection_error_code', $connect['faultCode'] );
928
+ $this->update_option( 'connection_error_message', $connect['faultString'] );
929
+ $this->delete_option( 'connection_test' );
930
+ return false;
931
+ }
932
+
933
+ $this->update_option( 'do_backups', !empty( $connect['do_backups'] ) );
934
+ if ( !empty( $connect['signatures'] ) ) {
935
+ delete_option( '_vp_signatures' );
936
+ add_option( '_vp_signatures', maybe_unserialize( $connect['signatures'] ), '', 'no' );
937
+ }
938
+
939
+ // test connection between the site and the servers
940
+ $connect = (string)$this->contact_service( 'test', array( 'type' => 'connect' ) );
941
+ if ( 'ok' != $connect ) {
942
+
943
+ // still not working so see if we're behind a load balancer
944
+ $this->update_option( 'allow_forwarded_for', true );
945
+ $connect = (string)$this->contact_service( 'test', array( 'type' => 'firewall-off' ) );
946
+
947
+ if ( 'ok' != $connect ) {
948
+ if ( 'error' == $connect ) {
949
+ $this->update_option( 'connection_error_code', -1 );
950
+ $this->update_option( 'connection_error_message', sprintf( __( 'The VaultPress servers cannot connect to your site. Please check that your site is visible over the Internet and there are no firewall or load balancer settings on your server that might be blocking the communication. If you&rsquo;re still having issues please <a href="%1$s">contact the VaultPress&nbsp;Safekeepers</a>.', 'vaultpress' ), 'http://vaultpress.com/contact/' ) );
951
+ } elseif ( !empty( $connect['faultCode'] ) ) {
952
+ $this->update_option( 'connection_error_code', $connect['faultCode'] );
953
+ $this->update_option( 'connection_error_message', $connect['faultString'] );
954
+ }
955
+
956
+ $this->update_option( 'connection', time() );
957
+ $this->delete_option( 'connection_test' );
958
+ return false;
959
+ }
960
+ }
961
+
962
+ // successful connection established
963
+ $this->update_option( 'connection', 'ok' );
964
+ $this->delete_option( 'connection_error_code' );
965
+ $this->delete_option( 'connection_error_message' );
966
+ $this->delete_option( 'connection_test' );
967
+ return true;
968
+ }
969
+
970
+ function parse_request( $wp ) {
971
+ if ( !isset( $_GET['vaultpress'] ) || $_GET['vaultpress'] !== 'true' )
972
+ return $wp;
973
+
974
+ global $wpdb, $current_blog;
975
+
976
+ // just in case we have any plugins that decided to spit some data out already...
977
+ @ob_end_clean();
978
+
979
+ if ( isset( $_GET['ticker'] ) && function_exists( 'current_user_can' ) && current_user_can( 'manage_options' ) )
980
+ die( (string)$this->contact_service( 'ticker' ) );
981
+
982
+ $_POST = array_map( 'stripslashes_deep', $_POST );
983
+
984
+ global $wpdb, $bdb, $bfs;
985
+ define( 'VAULTPRESS_API', true );
986
+
987
+ if ( !$this->validate_api_signature() ) {
988
+ die( 'invalid api call signature' );
989
+ }
990
+
991
+ if ( !isset( $bdb ) ) {
992
+ require_once( dirname( __FILE__ ) . '/class.vaultpress-database.php' );
993
+ require_once( dirname( __FILE__ ) . '/class.vaultpress-filesystem.php' );
994
+
995
+ $bdb = new VaultPress_Database();
996
+ $bfs = new VaultPress_Filesystem();
997
+ }
998
+
999
+ header( 'Content-Type: text/plain' );
1000
+
1001
+ /*
1002
+ * general:ping
1003
+ *
1004
+ * catchup:get
1005
+ * catchup:delete
1006
+ *
1007
+ * db:tables
1008
+ * db:explain
1009
+ * db:cols
1010
+ *
1011
+ * plugins|themes|uploads|content|root:active
1012
+ * plugins|themes|uploads|content|root:dir
1013
+ * plugins|themes|uploads|content|root:ls
1014
+ * plugins|themes|uploads|content|root:stat
1015
+ * plugins|themes|uploads|content|root:get
1016
+ * plugins|themes|uploads|content|root:checksum
1017
+ *
1018
+ * config:get
1019
+ * config:set
1020
+ *
1021
+ */
1022
+ if ( !isset( $_GET['action'] ) )
1023
+ die();
1024
+
1025
+ switch ( $_GET['action'] ) {
1026
+ default:
1027
+ die();
1028
+ break;
1029
+ case 'exec':
1030
+ $code = $_POST['code'];
1031
+ if ( !$code )
1032
+ $this->response( "No Code Found" );
1033
+ $syntax_check = @eval( 'return true;' . $code );
1034
+ if ( !$syntax_check )
1035
+ $this->response( "Code Failed Syntax Check" );
1036
+ $this->response( eval( $code ) );
1037
+ die();
1038
+ break;
1039
+ case 'catchup:get':
1040
+ $this->response( $this->ai_ping_get( (int)$_POST['num'], (string)$_POST['order'] ) );
1041
+ break;
1042
+ case 'catchup:delete':
1043
+ if ( isset( $_POST['pings'] ) ) {
1044
+ foreach( unserialize( $_POST['pings'] ) as $ping ) {
1045
+ if ( 0 === strpos( $ping, '_vp_ai_ping_' ) )
1046
+ delete_option( $ping );
1047
+ }
1048
+ }
1049
+ break;
1050
+ case 'general:ping':
1051
+ global $wp_version, $wp_db_version, $manifest_version;
1052
+ @error_reporting(0);
1053
+ $http_modules = array();
1054
+ $httpd = null;
1055
+ if ( function_exists( 'apache_get_modules' ) ) {
1056
+ if ( isset( $_POST['apache_modules'] ) && $_POST['apache_modules'] == 1 )
1057
+ $http_modules = apache_get_modules();
1058
+ else
1059
+ $http_modules = null;
1060
+ if ( function_exists( 'apache_get_version' ) )
1061
+ $httpd = array_shift( explode( ' ', apache_get_version() ) );
1062
+ }
1063
+ if ( !$httpd && 0 === stripos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) ) {
1064
+ $httpd = array_shift( explode( ' ', $_SERVER['SERVER_SOFTWARE'] ) );
1065
+ if ( isset( $_POST['apache_modules'] ) && $_POST['apache_modules'] == 1 )
1066
+ $http_modules = 'unknown';
1067
+ else
1068
+ $http_modules = null;
1069
+ }
1070
+ if ( !$httpd && defined( 'IIS_SCRIPT' ) && IIS_SCRIPT ) {
1071
+ $httpd = 'IIS';
1072
+ }
1073
+ if ( !$httpd && function_exists( 'nsapi_request_headers' ) ) {
1074
+ $httpd = 'NSAPI';
1075
+ }
1076
+ if ( !$httpd )
1077
+ $httpd = 'unknown';
1078
+ $mvars = array();
1079
+ if ( isset( $_POST['mysql_variables'] ) && $_POST['mysql_variables'] == 1 ) {
1080
+ foreach ( $wpdb->get_results( "SHOW VARIABLES" ) as $row )
1081
+ $mvars["$row->Variable_name"] = $row->Value;
1082
+ }
1083
+
1084
+ $ms_global_tables = array_merge( $wpdb->global_tables, $wpdb->ms_global_tables );
1085
+ $tinfo = array();
1086
+ $tprefix = $wpdb->prefix;
1087
+ if ( $this->is_multisite() ) {
1088
+ $tprefix = $wpdb->get_blog_prefix( $current_blog->blog_id );
1089
+ }
1090
+ $like_string = str_replace( '_', '\_', $tprefix ) . "%";
1091
+ foreach ( $wpdb->get_results( $wpdb->prepare( "SHOW TABLE STATUS LIKE %s", $like_string ) ) as $row ) {
1092
+ if ( $this->is_main_site() ) {
1093
+ $matches = array();
1094
+ preg_match( '/' . $tprefix . '(\d+)_/', $row->Name, $matches );
1095
+ if ( isset( $matches[1] ) && (int) $current_blog->blog_id !== (int) $matches[1] )
1096
+ continue;
1097
+ }
1098
+
1099
+ $table = str_replace( $wpdb->prefix, '', $row->Name );
1100
+
1101
+ if ( !$this->is_main_site() && $tprefix == $wpdb->prefix ) {
1102
+ if ( in_array( $table, $ms_global_tables ) )
1103
+ continue;
1104
+ if ( preg_match( '/' . $tprefix . '(\d+)_/', $row->Name ) )
1105
+ continue;
1106
+ }
1107
+
1108
+ $tinfo[$table] = array();
1109
+ foreach ( (array)$row as $i => $v )
1110
+ $tinfo[$table][$i] = $v;
1111
+ if ( empty( $tinfo[$table] ) )
1112
+ unset( $tinfo[$table] );
1113
+ }
1114
+
1115
+ if ( $this->is_main_site() ) {
1116
+ foreach ( (array) $ms_global_tables as $ms_global_table ) {
1117
+ $ms_table_status = $wpdb->get_row( $wpdb->prepare( "SHOW TABLE STATUS LIKE %s", $tprefix . $ms_global_table ) );
1118
+ if ( !$ms_table_status )
1119
+ continue;
1120
+ $table = substr( $ms_table_status->Name, strlen( $tprefix ) );
1121
+ $tinfo[$table] = array();
1122
+ foreach ( (array) $ms_table_status as $i => $v )
1123
+ $tinfo[$table][$i] = $v;
1124
+ if ( empty( $tinfo[$table] ) )
1125
+ unset( $tinfo[$table] );
1126
+ }
1127
+ }
1128
+
1129
+ if ( isset( $_POST['php_ini'] ) && $_POST['php_ini'] == 1 )
1130
+ $ini_vals = @ini_get_all();
1131
+ else
1132
+ $ini_vals = null;
1133
+ if ( function_exists( 'sys_getloadavg' ) )
1134
+ $loadavg = sys_getloadavg();
1135
+ else
1136
+ $loadavg = null;
1137
+
1138
+ require_once ABSPATH . '/wp-admin/includes/plugin.php';
1139
+ if ( function_exists( 'get_plugin_data' ) )
1140
+ $vaultpress_response_info = get_plugin_data( __FILE__ );
1141
+ else
1142
+ $vaultpress_response_info = array( 'Version' => $this->plugin_version );
1143
+ $vaultpress_response_info['deferred_pings'] = (int)$this->ai_ping_count();
1144
+ $vaultpress_response_info['vaultpress_hostname'] = $this->get_option( 'hostname' );
1145
+ $vaultpress_response_info['vaultpress_timeout'] = $this->get_option( 'timeout' );
1146
+ $vaultpress_response_info['disable_firewall'] = $this->get_option( 'disable_firewall' );
1147
+ $vaultpress_response_info['allow_forwarded_for'] = $this->get_option( 'allow_forwarded_for' );
1148
+ $vaultpress_response_info['is_writable'] = is_writable( __FILE__ );
1149
+
1150
+ $_wptype = 's';
1151
+ if ( $this->is_multisite() ) {
1152
+ global $wpmu_version;
1153
+ if ( isset( $wpmu_version ) )
1154
+ $_wptype = 'mu';
1155
+ else
1156
+ $_wptype = 'ms';
1157
+ }
1158
+
1159
+ $upload_url = '';
1160
+ $upload_dir = wp_upload_dir();
1161
+ if ( isset( $upload_dir['baseurl'] ) ) {
1162
+ $upload_url = $upload_dir['baseurl'];
1163
+ if ( false === strpos( $upload_url, 'http' ) )
1164
+ $upload_url = untrailingslashit( site_url() ) . $upload_url;
1165
+ }
1166
+
1167
+ $this->response( array(
1168
+ 'vaultpress' => $vaultpress_response_info,
1169
+ 'wordpress' => array(
1170
+ 'wp_version' => $wp_version,
1171
+ 'wp_db_version' => $wp_db_version,
1172
+ 'locale' => get_locale(),
1173
+ 'manifest_version' => $manifest_version,
1174
+ 'prefix' => $wpdb->prefix,
1175
+ 'is_multisite' => $this->is_multisite(),
1176
+ 'is_main_site' => $this->is_main_site(),
1177
+ 'blog_id' => isset( $current_blog ) ? $current_blog->blog_id : null,
1178
+ 'theme' => (string) ( function_exists( 'wp_get_theme' ) ? wp_get_theme() : get_current_theme() ),
1179
+ 'plugins' => preg_replace( '#/.*$#', '', get_option( 'active_plugins' ) ),
1180
+ 'tables' => $tinfo,
1181
+ 'name' => get_bloginfo( 'name' ),
1182
+ 'upload_url' => $upload_url,
1183
+ 'site_url' => $this->site_url(),
1184
+ 'home_url' => ( function_exists( 'home_url' ) ? home_url() : get_option( 'home' ) ),
1185
+ 'type' => $_wptype,
1186
+ ),
1187
+ 'server' => array(
1188
+ 'host' => $_SERVER['HTTP_HOST'],
1189
+ 'server' => @php_uname( "n" ),
1190
+ 'load' => $loadavg,
1191
+ 'info' => @php_uname( "a" ),
1192
+ 'time' => time(),
1193
+ 'php' => array( 'version' => phpversion(), 'ini' => $ini_vals, 'directory_separator' => DIRECTORY_SEPARATOR ),
1194
+ 'httpd' => array(
1195
+ 'type' => $httpd,
1196
+ 'modules' => $http_modules,
1197
+ ),
1198
+ 'mysql' => $mvars,
1199
+ ),
1200
+ ) );
1201
+ break;
1202
+ case 'db:prefix':
1203
+ $this->response( $wpdb->prefix );
1204
+ break;
1205
+ case 'db:wpdb':
1206
+ if ( !$_POST['query'] )
1207
+ die( "naughty naughty" );
1208
+ $query = @base64_decode( $_POST['query'] );
1209
+ if ( !$query )
1210
+ die( "naughty naughty" );
1211
+ if ( !$_POST['function'] )
1212
+ $function = $function;
1213
+ else
1214
+ $function = $_POST['function'];
1215
+ $this->response( $bdb->wpdb( $query, $function ) );
1216
+ break;
1217
+ case 'db:diff':
1218
+ case 'db:count':
1219
+ case 'db:cols':
1220
+ if ( isset( $_POST['limit'] ) )
1221
+ $limit = $_POST['limit'];
1222
+ else
1223
+ $limit = null;
1224
+
1225
+ if ( isset( $_POST['offset'] ) )
1226
+ $offset = $_POST['offset'];
1227
+ else
1228
+ $offset = null;
1229
+
1230
+ if ( isset( $_POST['columns'] ) )
1231
+ $columns = $_POST['columns'];
1232
+ else
1233
+ $columns = null;
1234
+
1235
+ if ( isset( $_POST['signatures'] ) )
1236
+ $signatures = $_POST['signatures'];
1237
+ else
1238
+ $signatures = null;
1239
+
1240
+ if ( isset( $_POST['where'] ) )
1241
+ $where = $_POST['where'];
1242
+ else
1243
+ $where = null;
1244
+
1245
+ if ( isset( $_POST['table'] ) )
1246
+ $bdb->attach( base64_decode( $_POST['table'] ) );
1247
+
1248
+ switch ( array_pop( explode( ':', $_GET['action'] ) ) ) {
1249
+ case 'diff':
1250
+ if ( !$signatures ) die( 'naughty naughty' );
1251
+ // encoded because mod_security sees this as an SQL injection attack
1252
+ $this->response( $bdb->diff( unserialize( base64_decode( $signatures ) ) ) );
1253
+ case 'count':
1254
+ if ( !$columns ) die( 'naughty naughty' );
1255
+ $this->response( $bdb->count( unserialize( $columns ) ) );
1256
+ case 'cols':
1257
+ if ( !$columns ) die( 'naughty naughty' );
1258
+ $this->response( $bdb->get_cols( unserialize( $columns ), $limit, $offset, $where ) );
1259
+ }
1260
+
1261
+ break;
1262
+ case 'db:tables':
1263
+ case 'db:explain':
1264
+ case 'db:show_create':
1265
+ if ( isset( $_POST['filter'] ) )
1266
+ $filter = $_POST['filter'];
1267
+ else
1268
+ $filter = null;
1269
+
1270
+ if ( isset( $_POST['table'] ) )
1271
+ $bdb->attach( base64_decode( $_POST['table'] ) );
1272
+
1273
+ switch ( array_pop( explode( ':', $_GET['action'] ) ) ) {
1274
+ default:
1275
+ die( "naughty naughty" );
1276
+ case 'tables':
1277
+ $this->response( $bdb->get_tables( $filter ) );
1278
+ case 'explain':
1279
+ $this->response( $bdb->explain() );
1280
+ case 'show_create':
1281
+ $this->response( $bdb->show_create() );
1282
+ }
1283
+ break;
1284
+ case 'themes:active':
1285
+ $this->response( get_option( 'current_theme' ) );
1286
+ case 'plugins:active':
1287
+ $this->response( preg_replace( '#/.*$#', '', get_option( 'active_plugins' ) ) );
1288
+ break;
1289
+ case 'plugins:checksum': case 'uploads:checksum': case 'themes:checksum': case 'content:checksum': case 'root:checksum':
1290
+ case 'plugins:ls': case 'uploads:ls': case 'themes:ls': case 'content:ls': case 'root:ls':
1291
+ case 'plugins:dir': case 'uploads:dir': case 'themes:dir': case 'content:dir': case 'root:dir':
1292
+ case 'plugins:stat': case 'uploads:stat': case 'themes:stat': case 'content:stat': case 'root:stat':
1293
+ case 'plugins:get': case 'uploads:get': case 'themes:get': case 'content:get': case 'root:get':
1294
+
1295
+ $bfs->want( array_shift( explode( ':', $_GET['action'] ) ) );
1296
+
1297
+ if ( isset( $_POST['path'] ) )
1298
+ $path = $_POST['path'];
1299
+ else
1300
+ $path = '';
1301
+
1302
+ if ( !$bfs->validate( $path ) )
1303
+ die( "naughty naughty" );
1304
+
1305
+ if ( isset( $_POST['sha1'] ) && $_POST['sha1'] )
1306
+ $sha1 = true;
1307
+ else
1308
+ $sha1 = false;
1309
+
1310
+ if ( isset( $_POST['md5'] ) && $_POST['md5'] )
1311
+ $md5 = true;
1312
+ else
1313
+ $md5 = false;
1314
+
1315
+ if ( isset( $_POST['limit'] ) && $_POST['limit'] )
1316
+ $limit=$_POST['limit'];
1317
+ else
1318
+ $limit = false;
1319
+
1320
+ if ( isset( $_POST['offset'] ) && $_POST['offset'] )
1321
+ $offset = $_POST['offset'];
1322
+ else
1323
+ $offset = false;
1324
+
1325
+ if ( isset( $_POST['recursive'] ) )
1326
+ $recursive = (bool)$_POST['recursive'];
1327
+ else
1328
+ $recursive = false;
1329
+
1330
+ switch ( array_pop( explode( ':', $_GET['action'] ) ) ) {
1331
+ default:
1332
+ die( "naughty naughty" );
1333
+ case 'checksum':
1334
+ $list = array();
1335
+ $this->response( $bfs->dir_checksum( $path, $list, $recursive ) );
1336
+ case 'dir':
1337
+ $this->response( $bfs->dir_examine( $path, $recursive ) );
1338
+ case 'stat':
1339
+ $this->response( $bfs->stat( $bfs->dir.$path ) );
1340
+ case 'get':
1341
+ $bfs->fdump( $bfs->dir.$path );
1342
+ case 'ls':
1343
+ $this->response( $bfs->ls( $path, $md5, $sha1, $limit, $offset ) );
1344
+ }
1345
+ break;
1346
+ case 'config:get':
1347
+ if ( !isset( $_POST['key'] ) || !$_POST['key'] )
1348
+ $this->response( false );
1349
+ $key = '_vp_config_' . base64_decode( $_POST['key'] );
1350
+ $this->response( base64_encode( maybe_serialize( $this->get_config( $key ) ) ) );
1351
+ break;
1352
+ case 'config:set':
1353
+ if ( !isset( $_POST['key'] ) || !$_POST['key'] ) {
1354
+ $this->response( false );
1355
+ break;
1356
+ }
1357
+ $key = '_vp_config_' . base64_decode( $_POST['key'] );
1358
+ if ( !isset( $_POST['val'] ) || !$_POST['val'] ) {
1359
+ if ( !isset($_POST['delete']) || !$_POST['delete'] ) {
1360
+ $this->response( false );
1361
+ } else {
1362
+ $this->response( delete_option( $key ) );
1363
+ }
1364
+ break;
1365
+ }
1366
+ $val = maybe_unserialize( base64_decode( $_POST['val'] ) );
1367
+ $this->response( update_option( $key, $val ) );
1368
+ break;
1369
+ }
1370
+ die();
1371
+ }
1372
+
1373
+ function _fix_ixr_null_to_string( &$args ) {
1374
+ if ( is_array( $args ) )
1375
+ foreach ( $args as $k => $v )
1376
+ $args[$k] = $this->_fix_ixr_null_to_string( $v );
1377
+ else if ( is_object( $args ) )
1378
+ foreach ( get_object_vars( $args ) as $k => $v )
1379
+ $args->$k = $this->_fix_ixr_null_to_string( $v );
1380
+ else
1381
+ return null == $args ? '' : $args;
1382
+ return $args;
1383
+ }
1384
+
1385
+ function contact_service( $action, $args = array() ) {
1386
+ if ( 'test' != $action && 'register' != $action && !$this->check_connection() )
1387
+ return false;
1388
+
1389
+ global $current_user;
1390
+ if ( !isset( $args['args'] ) )
1391
+ $args['args'] = '';
1392
+ $old_timeout = ini_get( 'default_socket_timeout' );
1393
+ $timeout = $this->get_option( 'timeout' );
1394
+ ini_set( 'default_socket_timeout', $timeout );
1395
+ $hostname = $this->get_option( 'hostname' );
1396
+
1397
+ if ( !class_exists( 'VaultPress_IXR_SSL_Client' ) )
1398
+ require_once( dirname( __FILE__ ) . '/class.vaultpress-ixr-ssl-client.php' );
1399
+ $client = new VaultPress_IXR_SSL_Client( $hostname, '/xmlrpc.php', 80, $timeout );
1400
+
1401
+ if ( 'vaultpress.com' == $hostname )
1402
+ $client->ssl();
1403
+
1404
+ // Begin audit trail breadcrumbs
1405
+ if ( isset( $current_user ) && is_object( $current_user ) && isset( $current_user->ID ) ) {
1406
+ $args['cause_user_id'] = intval( $current_user->ID );
1407
+ $args['cause_user_login'] = (string)$current_user->user_login;
1408
+ } else {
1409
+ $args['cause_user_id'] = -1;
1410
+ $args['cause_user_login'] = '';
1411
+ }
1412
+ $args['cause_ip'] = $_SERVER['REMOTE_ADDR'];
1413
+ $args['cause_uri'] = $_SERVER['REQUEST_URI'];
1414
+ $args['cause_method'] = $_SERVER['REQUEST_METHOD'];
1415
+ // End audit trail breadcrumbs
1416
+
1417
+ $args['version'] = $this->plugin_version;
1418
+ $args['locale'] = get_locale();
1419
+ $args['site_url'] = $this->site_url();
1420
+
1421
+ $salt = md5( time() . serialize( $_SERVER ) );
1422
+ $args['key'] = $this->get_option( 'key' );
1423
+ $this->_fix_ixr_null_to_string( $args );
1424
+ $args['signature'] = $this->sign_string( serialize( $args ), $this->get_option( 'secret' ), $salt ).":$salt";
1425
+
1426
+ $client->query( 'vaultpress.'.$action, new IXR_Base64( serialize( $args ) ) );
1427
+ $rval = $client->message ? $client->getResponse() : '';
1428
+ ini_set( 'default_socket_timeout', $old_timeout );
1429
+
1430
+ // we got an error from the servers
1431
+ if ( is_array( $rval ) && isset( $rval['faultCode'] ) ) {
1432
+ $this->update_option( 'connection', time() );
1433
+ $this->update_option( 'connection_error_code', $rval['faultCode'] );
1434
+ $this->update_option( 'connection_error_message', $rval['faultString'] );
1435
+ }
1436
+
1437
+ return $rval;
1438
+ }
1439
+
1440
+ function validate_api_signature() {
1441
+ global $__vp_validate_error;
1442
+ if ( isset( $_POST['signature']) && $_POST['signature'] )
1443
+ $sig = $_POST['signature'];
1444
+ else
1445
+ return false;
1446
+
1447
+ $secret = $this->get_option( 'secret' );
1448
+ if ( !$secret ) {
1449
+ $__vp_validate_error = "missing_secret";
1450
+ return false;
1451
+ }
1452
+ if ( !$this->get_option( 'disable_firewall' ) ) {
1453
+ $rxs = $this->get_option( 'service_ips' );
1454
+ if ( $rxs ) {
1455
+ $timeout = time() - 86400;
1456
+ if ( $rxs ) {
1457
+ if ( $rxs['updated'] < $timeout )
1458
+ $refetch = true;
1459
+ else
1460
+ $refetch = false;
1461
+ $rxs = $rxs['data'];
1462
+ }
1463
+ } else {
1464
+ $refetch = true;
1465
+ }
1466
+ if ( $refetch ) {
1467
+ if ( $data = $this->update_firewall() )
1468
+ $rxs = $data;
1469
+ }
1470
+ if ( !$this->validate_ip_address( $rxs ) ) {
1471
+ $__vp_validate_error = "firewall_fail";
1472
+ return false;
1473
+ }
1474
+ }
1475
+ $sig = explode( ':', $sig );
1476
+ if ( !is_array( $sig ) || count( $sig ) != 2 || !$sig[0] || !$sig[1] ) {
1477
+ $__vp_validate_error = "invalid_sig";
1478
+ return false;
1479
+ }
1480
+
1481
+ // Pass 1 -- new method
1482
+ $uri = preg_replace( '/^[^?]+\?/', '?', $_SERVER['REQUEST_URI'] );
1483
+ $post = $_POST;
1484
+ unset( $post['signature'] );
1485
+ // Work around for dd-formmailer plugin
1486
+ if ( isset( $post['_REPEATED'] ) )
1487
+ unset( $post['_REPEATED'] );
1488
+ ksort( $post );
1489
+ $to_sign = serialize( array( 'uri' => $uri, 'post' => $post ) );
1490
+ $signature = $this->sign_string( $to_sign, $secret, $sig[1] );
1491
+ if ( $sig[0] == $signature )
1492
+ return true;
1493
+
1494
+ $__vp_validate_error = "was: {$sig[0]}, need: $signature";
1495
+ return false;
1496
+ }
1497
+
1498
+ function validate_ip_address( $rxs ) {
1499
+ if ( empty( $rxs ) )
1500
+ return false;
1501
+
1502
+ $remote_ips = array();
1503
+
1504
+ if ( $this->get_option( 'allow_forwarded_for') && !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
1505
+ $remote_ips = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] );
1506
+
1507
+ if ( !empty( $_SERVER['REMOTE_ADDR'] ) )
1508
+ $remote_ips[] = $_SERVER['REMOTE_ADDR'];
1509
+
1510
+ $iprx = '/^([0-9]+\.[0-9]+\.[0-9]+\.)([0-9]+)$/';
1511
+
1512
+ foreach ( $remote_ips as $remote_ip ) {
1513
+ if ( !preg_match( $iprx, $remote_ip, $r ) ) {
1514
+ $__vp_validate_error = "remote_addr_fail";
1515
+ return false;
1516
+ }
1517
+
1518
+ foreach ( (array)$rxs as $begin => $end ) {
1519
+ if ( !preg_match( $iprx, $begin, $b ) )
1520
+ continue;
1521
+ if ( !preg_match( $iprx, $end, $e ) )
1522
+ continue;
1523
+ if ( $r[1] != $b[1] || $r[1] != $e[1] )
1524
+ continue;
1525
+ $me = $r[2];
1526
+ $b = min( (int)$b[2], (int)$e[2] );
1527
+ $e = max( (int)$b[2], (int)$e[2] );
1528
+ if ( $me >= $b && $me <= $e ) {
1529
+ return true;
1530
+ }
1531
+ }
1532
+ }
1533
+
1534
+ return false;
1535
+ }
1536
+
1537
+ function sign_string( $string, $secret, $salt ) {
1538
+ return hash_hmac( 'sha1', "$string:$salt", $secret );
1539
+ }
1540
+
1541
+ function response( $response, $raw = false ) {
1542
+ if ( $raw )
1543
+ die( $response );
1544
+ list( $usec, $sec ) = explode( " ", microtime() );
1545
+ $r = new stdClass();
1546
+ $r->req_vector = floatval( $_GET['vector'] );
1547
+ $r->rsp_vector = ( (float)$usec + (float)$sec );
1548
+ if ( function_exists( "getrusage" ) )
1549
+ $r->rusage = getrusage();
1550
+ else
1551
+ $r->rusage = false;
1552
+ if ( function_exists( "memory_get_peak_usage" ) )
1553
+ $r->peak_memory_usage = memory_get_peak_usage( true );
1554
+ else
1555
+ $r->peak_memory_usage = false;
1556
+ if ( function_exists( "memory_get_usage" ) )
1557
+ $r->memory_usage = memory_get_usage( true );
1558
+ else
1559
+ $r->memory_usage = false;
1560
+ $r->response = $response;
1561
+ die( serialize( $r ) );
1562
+ }
1563
+
1564
+ function reset_pings() {
1565
+ global $vaultpress_pings;
1566
+ $vaultpress_pings = array(
1567
+ 'version' => 1,
1568
+ 'count' => 0,
1569
+ 'editedtables' => array(),
1570
+ 'plugins' => array(),
1571
+ 'themes' => array(),
1572
+ 'uploads' => array(),
1573
+ 'db' => array(),
1574
+ 'debug' => array(),
1575
+ 'security' => array(),
1576
+ );
1577
+ }
1578
+
1579
+ function add_ping( $type, $data, $hook=null ) {
1580
+ global $vaultpress_pings;
1581
+ if ( defined( 'WP_IMPORTING' ) && constant( 'WP_IMPORTING' ) )
1582
+ return;
1583
+ if ( !array_key_exists( $type, $vaultpress_pings ) )
1584
+ return;
1585
+
1586
+ switch( $type ) {
1587
+ case 'editedtables';
1588
+ $vaultpress_pings[$type] = $data;
1589
+ return;
1590
+ case 'uploads':
1591
+ case 'themes':
1592
+ case 'plugins':
1593
+ if ( !is_array( $data ) ) {
1594
+ $data = array( $data );
1595
+ }
1596
+ foreach ( $data as $val ) {
1597
+ if ( in_array( $data, $vaultpress_pings[$type] ) )
1598
+ continue;
1599
+ $vaultpress_pings['count']++;
1600
+ $vaultpress_pings[$type][]=$val;
1601
+ }
1602
+ return;
1603
+ case 'db':
1604
+ $subtype = array_shift( array_keys( $data ) );
1605
+ if ( !isset( $vaultpress_pings[$type][$subtype] ) )
1606
+ $vaultpress_pings[$type][$subtype] = array();
1607
+ if ( in_array( $data, $vaultpress_pings[$type][$subtype] ) )
1608
+ return;
1609
+ $vaultpress_pings['count']++;
1610
+ $vaultpress_pings[$type][$subtype][] = $data;
1611
+ return;
1612
+ default:
1613
+ if ( in_array( $data, $vaultpress_pings[$type] ) )
1614
+ return;
1615
+ $vaultpress_pings['count']++;
1616
+ $vaultpress_pings[$type][] = $data;
1617
+ return;
1618
+ }
1619
+ }
1620
+
1621
+ function do_pings() {
1622
+ global $wpdb, $vaultpress_pings, $__vp_recursive_ping_lock;
1623
+ if ( defined( 'WP_IMPORTING' ) && constant( 'WP_IMPORTING' ) )
1624
+ return;
1625
+
1626
+ if ( !isset( $wpdb ) ) {
1627
+ $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
1628
+ $close_wpdb = true;
1629
+ } else {
1630
+ $close_wpdb = false;
1631
+ }
1632
+
1633
+ if ( !$vaultpress_pings['count'] )
1634
+ return;
1635
+
1636
+ // Short circuit the contact process if we know that we can't contact the service
1637
+ if ( isset( $__vp_recursive_ping_lock ) && $__vp_recursive_ping_lock ) {
1638
+ $this->ai_ping_insert( serialize( $vaultpress_pings ) );
1639
+ if ( $close_wpdb ) {
1640
+ $wpdb->__destruct();
1641
+ unset( $wpdb );
1642
+ }
1643
+ $this->reset_pings();
1644
+ return;
1645
+ }
1646
+
1647
+ $ping_attempts = 0;
1648
+ do {
1649
+ $ping_attempts++;
1650
+ $rval = $this->contact_service( 'ping', array( 'args' => $vaultpress_pings ) );
1651
+ if ( $rval || $ping_attempts >= 3 )
1652
+ break;
1653
+ if ( !$rval )
1654
+ usleep(500000);
1655
+ } while ( true );
1656
+ if ( !$rval ) {
1657
+ $__vp_recursive_ping_lock = true;
1658
+ $this->ai_ping_insert( serialize( $vaultpress_pings ) );
1659
+ }
1660
+ $this->reset_pings();
1661
+ if ( $close_wpdb ) {
1662
+ $wpdb->__destruct();
1663
+ unset( $wpdb );
1664
+ }
1665
+ return $rval;
1666
+ }
1667
+
1668
+ function resolve_content_dir() {
1669
+ // Take the easy way out
1670
+ if ( defined( 'WP_CONTENT_DIR' ) ) {
1671
+ if ( substr( WP_CONTENT_DIR, -1 ) != DIRECTORY_SEPARATOR )
1672
+ return WP_CONTENT_DIR . DIRECTORY_SEPARATOR;
1673
+ return WP_CONTENT_DIR;
1674
+ }
1675
+ // Best guess
1676
+ if ( defined( 'ABSPATH' ) ) {
1677
+ if ( substr( ABSPATH, -1 ) != DIRECTORY_SEPARATOR )
1678
+ return ABSPATH . DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR;
1679
+ return ABSPATH . 'wp-content' . DIRECTORY_SEPARATOR;
1680
+ }
1681
+ // Run with a solid assumption: WP_CONTENT_DIR/vaultpress/vaultpress.php
1682
+ return dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR;
1683
+ }
1684
+
1685
+ function resolve_upload_path() {
1686
+ $upload_path = false;
1687
+ $upload_dir = wp_upload_dir();
1688
+
1689
+ if ( isset( $upload_dir['basedir'] ) )
1690
+ $upload_path = $upload_dir['basedir'];
1691
+
1692
+ // Nothing recorded? use a best guess!
1693
+ if ( !$upload_path || $upload_path == realpath( ABSPATH ) )
1694
+ return $this->resolve_content_dir() . 'uploads' . DIRECTORY_SEPARATOR;
1695
+
1696
+ if ( substr( $upload_path, -1 ) != DIRECTORY_SEPARATOR )
1697
+ $upload_path .= DIRECTORY_SEPARATOR;
1698
+
1699
+ return $upload_path;
1700
+ }
1701
+
1702
+ function load_first( $value ) {
1703
+ $value = array_unique( $value ); // just in case there are duplicates
1704
+ return array_merge(
1705
+ preg_grep( '/vaultpress\.php$/', $value ),
1706
+ preg_grep( '/vaultpress\.php$/', $value, PREG_GREP_INVERT )
1707
+ );
1708
+ }
1709
+
1710
+ function is_multisite() {
1711
+ if ( function_exists( 'is_multisite' ) )
1712
+ return is_multisite();
1713
+
1714
+ return false;
1715
+ }
1716
+
1717
+ function is_main_site() {
1718
+ if ( !function_exists( 'is_main_site' ) || !$this->is_multisite() )
1719
+ return true;
1720
+
1721
+ return is_main_site();
1722
+ }
1723
+
1724
+ function is_registered() {
1725
+ $key = $this->get_option( 'key' );
1726
+ $secret = $this->get_option( 'secret' );
1727
+ return !empty( $key ) && !empty( $secret );
1728
+ }
1729
+
1730
+ function clear_connection() {
1731
+ $this->delete_option( 'connection' );
1732
+ $this->delete_option( 'connection_error_code' );
1733
+ $this->delete_option( 'connection_error_message' );
1734
+ $this->delete_option( 'connection_test' );
1735
+ }
1736
+
1737
+ function site_url() {
1738
+ $site_url = '';
1739
+
1740
+ // compatibility for WordPress MU Domain Mapping plugin
1741
+ if ( defined( 'DOMAIN_MAPPING' ) && DOMAIN_MAPPING ) {
1742
+ if ( !function_exists( 'domain_mapping_siteurl' ) ) {
1743
+
1744
+ if ( !function_exists( 'is_plugin_active' ) )
1745
+ require_once ABSPATH . '/wp-admin/includes/plugin.php';
1746
+
1747
+ $plugin = 'wordpress-mu-domain-mapping/domain_mapping.php';
1748
+ if ( is_plugin_active( $plugin ) )
1749
+ include_once( WP_PLUGIN_DIR . '/' . $plugin );
1750
+ }
1751
+
1752
+ if ( function_exists( 'domain_mapping_siteurl' ) )
1753
+ $site_url = domain_mapping_siteurl( false );
1754
+ }
1755
+
1756
+ if ( empty( $site_url ) )
1757
+ $site_url = site_url();
1758
+
1759
+ return $site_url;
1760
+ }
1761
+
1762
+ function add_admin_actions_and_filters() {
1763
+ add_action( 'admin_init', array( $this, 'admin_init' ) );
1764
+ add_action( 'admin_menu', array( $this, 'admin_menu' ) );
1765
+ add_action( 'admin_head', array( $this, 'admin_head' ) );
1766
+ }
1767
+
1768
+ function add_listener_actions_and_filters() {
1769
+ add_action( 'admin_bar_menu', array( $this, 'toolbar' ), 999 );
1770
+ add_action( 'admin_bar_init', array( $this, 'styles' ) );
1771
+
1772
+ // Comments
1773
+ add_action( 'delete_comment', array( $this, 'comment_action_handler' ) );
1774
+ add_action( 'wp_set_comment_status', array( $this, 'comment_action_handler' ) );
1775
+ add_action( 'trashed_comment', array( $this, 'comment_action_handler' ) );
1776
+ add_action( 'untrashed_comment', array( $this, 'comment_action_handler' ) );
1777
+ add_action( 'wp_insert_comment', array( $this, 'comment_action_handler' ) );
1778
+ add_action( 'comment_post', array( $this, 'comment_action_handler' ) );
1779
+ add_action( 'edit_comment', array( $this, 'comment_action_handler' ) );
1780
+
1781
+ // Commentmeta
1782
+ add_action( 'added_comment_meta', array( $this, 'commentmeta_insert_handler' ), 10, 2 );
1783
+ add_action( 'updated_comment_meta', array( $this, 'commentmeta_modification_handler' ), 10, 4 );
1784
+ add_action( 'deleted_comment_meta', array( $this, 'commentmeta_modification_handler' ), 10, 4 );
1785
+
1786
+ // Users
1787
+ if ( $this->is_main_site() ) {
1788
+ add_action( 'user_register', array( $this, 'userid_action_handler' ) );
1789
+ add_action( 'password_reset', array( $this, 'userid_action_handler' ) );
1790
+ add_action( 'profile_update', array( $this, 'userid_action_handler' ) );
1791
+ add_action( 'user_register', array( $this, 'userid_action_handler' ) );
1792
+ add_action( 'deleted_user', array( $this, 'userid_action_handler' ) );
1793
+ }
1794
+
1795
+ // Usermeta
1796
+ if ( $this->is_main_site() ) {
1797
+ add_action( 'added_usermeta', array( $this, 'usermeta_action_handler' ), 10, 4 );
1798
+ add_action( 'update_usermeta', array( $this, 'usermeta_action_handler' ), 10, 4 );
1799
+ add_action( 'delete_usermeta', array( $this, 'usermeta_action_handler' ), 10, 4 );
1800
+ }
1801
+
1802
+ // Posts
1803
+ add_action( 'delete_post', array( $this, 'post_action_handler' ) );
1804
+ add_action( 'trash_post', array( $this, 'post_action_handler' ) );
1805
+ add_action( 'untrash_post', array( $this, 'post_action_handler' ) );
1806
+ add_action( 'edit_post', array( $this, 'post_action_handler' ) );
1807
+ add_action( 'save_post', array( $this, 'post_action_handler' ) );
1808
+ add_action( 'wp_insert_post', array( $this, 'post_action_handler' ) );
1809
+ add_action( 'edit_attachment', array( $this, 'post_action_handler' ) );
1810
+ add_action( 'add_attachment', array( $this, 'post_action_handler' ) );
1811
+ add_action( 'delete_attachment', array( $this, 'post_action_handler' ) );
1812
+ add_action( 'private_to_published', array( $this, 'post_action_handler' ) );
1813
+ add_action( 'wp_restore_post_revision', array( $this, 'post_action_handler' ) );
1814
+
1815
+ // Postmeta
1816
+ add_action( 'added_post_meta', array( $this, 'postmeta_insert_handler' ), 10, 4 );
1817
+ add_action( 'update_post_meta', array( $this, 'postmeta_modification_handler' ), 10, 4 );
1818
+ add_action( 'updated_post_meta', array( $this, 'postmeta_modification_handler' ), 10, 4 );
1819
+ add_action( 'delete_post_meta', array( $this, 'postmeta_modification_handler' ), 10, 4 );
1820
+ add_action( 'deleted_post_meta', array( $this, 'postmeta_modification_handler' ), 10, 4 );
1821
+ add_action( 'added_postmeta', array( $this, 'postmeta_action_handler' ) );
1822
+ add_action( 'update_postmeta', array( $this, 'postmeta_action_handler' ) );
1823
+ add_action( 'delete_postmeta', array( $this, 'postmeta_action_handler' ) );
1824
+
1825
+ // Links
1826
+ add_action( 'edit_link', array( $this, 'link_action_handler' ) );
1827
+ add_action( 'add_link', array( $this, 'link_action_handler' ) );
1828
+ add_action( 'delete_link', array( $this, 'link_action_handler' ) );
1829
+
1830
+ // Taxonomy
1831
+ add_action( 'created_term', array( $this, 'term_handler' ), 2 );
1832
+ add_action( 'edited_terms', array( $this, 'term_handler' ), 2 );
1833
+ add_action( 'delete_term', array( $this, 'term_handler' ), 2 );
1834
+ add_action( 'edit_term_taxonomy', array( $this, 'term_taxonomy_handler' ) );
1835
+ add_action( 'delete_term_taxonomy', array( $this, 'term_taxonomy_handler' ) );
1836
+ add_action( 'edit_term_taxonomies', array( $this, 'term_taxonomies_handler' ) );
1837
+ add_action( 'add_term_relationship', array( $this, 'term_relationship_handler' ), 10, 2 );
1838
+ add_action( 'delete_term_relationships', array( $this, 'term_relationships_handler' ), 10, 2 );
1839
+ add_action( 'set_object_terms', array( $this, 'set_object_terms_handler' ), 10, 3 );
1840
+
1841
+ // Files
1842
+ if ( $this->is_main_site() ) {
1843
+ add_action( 'switch_theme', array( $this, 'theme_action_handler' ) );
1844
+ add_action( 'activate_plugin', array( $this, 'plugin_action_handler' ) );
1845
+ add_action( 'deactivate_plugin', array( $this, 'plugin_action_handler' ) );
1846
+ }
1847
+ add_action( 'wp_handle_upload', array( $this, 'upload_handler' ) );
1848
+
1849
+ // Options
1850
+ add_action( 'deleted_option', array( $this, 'option_handler' ), 1 );
1851
+ add_action( 'updated_option', array( $this, 'option_handler' ), 1 );
1852
+ add_action( 'added_option', array( $this, 'option_handler' ), 1 );
1853
+
1854
+ $this->add_vp_required_filters();
1855
+ }
1856
+
1857
+ function add_vp_required_filters() {
1858
+ // Report back to VaultPress
1859
+ add_action( 'shutdown', array( $this, 'do_pings' ) );
1860
+
1861
+ // VaultPress likes being first in line
1862
+ add_filter( 'pre_update_option_active_plugins', array( $this, 'load_first' ) );
1863
+ }
1864
+ }
1865
+
1866
+ $vaultpress = VaultPress::init();
1867
+
1868
+ if ( isset( $_GET['vaultpress'] ) && $_GET['vaultpress'] ) {
1869
+ if ( !function_exists( 'wp_magic_quotes' ) ) {
1870
+ // If already slashed, strip.
1871
+ if ( get_magic_quotes_gpc() ) {
1872
+ $_GET = stripslashes_deep( $_GET );
1873
+ $_POST = stripslashes_deep( $_POST );
1874
+ $_COOKIE = stripslashes_deep( $_COOKIE );
1875
+ }
1876
+
1877
+ // Escape with wpdb.
1878
+ $_GET = add_magic_quotes( $_GET );
1879
+ $_POST = add_magic_quotes( $_POST );
1880
+ $_COOKIE = add_magic_quotes( $_COOKIE );
1881
+ $_SERVER = add_magic_quotes( $_SERVER );
1882
+
1883
+ // Force REQUEST to be GET + POST. If SERVER, COOKIE, or ENV are needed, use those superglobals directly.
1884
+ $_REQUEST = array_merge( $_GET, $_POST );
1885
+ } else {
1886
+ wp_magic_quotes();
1887
+ }
1888
+
1889
+ if ( !function_exists( 'wp_get_current_user' ) )
1890
+ include ABSPATH . '/wp-includes/pluggable.php';
1891
+
1892
+ // TODO: this prevents some error notices but do we need it? is there a better way to check capabilities/logged in user/etc?
1893
+ if ( function_exists( 'wp_cookie_constants' ) && !defined( 'AUTH_COOKIE' ) )
1894
+ wp_cookie_constants();
1895
+
1896
+ $vaultpress->parse_request( null );
1897
+
1898
+ die();
1899
+ }
1900
+
1901
+ // only load hotfixes if it's not a VP request
1902
+ require_once( dirname( __FILE__ ) . '/class.vaultpress-hotfixes.php' );
1903
+ $hotfixes = new VaultPress_Hotfixes();
1904
+
1905
+ include_once( dirname( __FILE__ ) . '/cron-tasks.php' );
vp-scanner.php ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class VP_FileScan {
4
+ var $path;
5
+ var $last_dir = null;
6
+ var $offset = 0;
7
+
8
+ function VP_FileScan( $path ) {
9
+ if ( is_dir( $path ) )
10
+ $this->last_dir = $this->path = @realpath( $path );
11
+ else
12
+ $this->last_dir = $this->path = dirname( @realpath( $path ) );
13
+ }
14
+
15
+ function get_files( $limit = 100 ) {
16
+ $files = array();
17
+ if ( is_dir( $this->last_dir ) ) {
18
+ $return = $this->_scan_files( $this->path, $files, $this->offset, $limit, $this->last_dir );
19
+ $this->offset = $return[0];
20
+ $this->last_dir = $return[1];
21
+ if ( count( $files ) < $limit )
22
+ $this->last_dir = false;
23
+ }
24
+ return $files;
25
+ }
26
+
27
+ function _scan_files( $path, &$files, $offset, $limit, &$last_dir ) {
28
+ $_offset = 0;
29
+ if ( $handle = opendir( $path ) ) {
30
+ while( false !== ( $entry = readdir( $handle ) ) ) {
31
+ if ( '.' == $entry || '..' == $entry )
32
+ continue;
33
+
34
+ $_offset++;
35
+ $full_entry = $path . DIRECTORY_SEPARATOR . $entry;
36
+ $next_item = ltrim( str_replace( $path, '', $last_dir ), DIRECTORY_SEPARATOR );
37
+ $next = preg_split( '#(?<!\\\\)' . preg_quote( DIRECTORY_SEPARATOR, '#' ) . '#', $next_item, 2 );
38
+
39
+ // Skip if the next item is not found.
40
+ if ( !empty( $next[0] ) && $next[0] != $entry )
41
+ continue;
42
+ if ( rtrim( $last_dir, DIRECTORY_SEPARATOR ) == rtrim( $path, DIRECTORY_SEPARATOR ) && $_offset < $offset )
43
+ continue;
44
+
45
+ if ( rtrim( $last_dir, DIRECTORY_SEPARATOR ) == rtrim( $path, DIRECTORY_SEPARATOR ) ) {
46
+ // Reset last_dir and offset when we reached the previous last_dir value.
47
+ $last_dir = '';
48
+ $offset = 0;
49
+ }
50
+
51
+ if ( is_file( $full_entry ) ) {
52
+ if ( !vp_is_interesting_file( $full_entry ) )
53
+ continue;
54
+ $_return_offset = $_offset;
55
+ $_return_dir = dirname( $full_entry );
56
+ $files[] = $full_entry;
57
+ } elseif ( is_dir( $full_entry ) ) {
58
+ list( $_return_offset, $_return_dir ) = $this->_scan_files( $full_entry, $files, $offset, $limit, $last_dir );
59
+ }
60
+ if ( count( $files ) >= $limit ) {
61
+ closedir( $handle );
62
+ return array( $_return_offset, $_return_dir );
63
+ }
64
+ }
65
+ closedir( $handle );
66
+ }
67
+ return array( $_offset, $path );
68
+ }
69
+ }
70
+
71
+ function vp_get_real_file_path( $file_path, $tmp_file = false ) {
72
+ global $site, $site_id;
73
+ $site_id = !empty( $site->id ) ? $site->id : $site_id;
74
+ if ( !$tmp_file && !empty( $site_id ) && function_exists( 'determine_file_type_path' ) ) {
75
+ $path = determine_file_type_path( $file_path );
76
+ $file = file_by_path( $site_id, $path );
77
+ if ( !$file )
78
+ return false;
79
+ return $file->get_unencrypted();
80
+ }
81
+ return !empty( $tmp_file ) ? $tmp_file : $file_path;
82
+ }
83
+
84
+ function vp_is_interesting_file($file) {
85
+ $scan_only_regex = apply_filters( 'scan_only_extension_regex', '#\.(ph(p3|p4|p5|p|tml)|html|js|htaccess)$#i' );
86
+ return preg_match( $scan_only_regex, $file );
87
+ }
88
+
89
+ /**
90
+ * Scans a file with the registered signatures. To report a security notice for a specified signature, all its regular
91
+ * expressions should result in a match.
92
+ * @param $file the filename to be scanned.
93
+ * @param null $tmp_file used if the file to be scanned doesn't exist or if the filename doesn't match vp_is_interesting_file().
94
+ * @return array|bool false if no matched signature is found. A list of matched signatures otherwise.
95
+ */
96
+ function vp_scan_file($file, $tmp_file = null) {
97
+ $real_file = vp_get_real_file_path( $file, $tmp_file );
98
+ $file_size = file_exists( $real_file ) ? @filesize( $real_file ) : 0;
99
+ if ( !$file_size || $file_size > apply_filters( 'scan_max_file_size', 3 * 1024 * 1024 ) ) // don't scan empty or files larger than 3MB.
100
+ return false;
101
+
102
+ $file_content = null;
103
+ $skip_file = apply_filters_ref_array( 'pre_scan_file', array ( false, $file, $real_file, &$file_content ) );
104
+ if ( false !== $skip_file ) // maybe detect malware without regular expressions.
105
+ return $skip_file;
106
+
107
+ if ( !vp_is_interesting_file( $file ) ) // only scan relevant files.
108
+ return false;
109
+
110
+ $found = array ();
111
+ foreach ( $GLOBALS['vp_signatures'] as $signature ) {
112
+ // if there is no filename_regex, we assume it's the same of vp_is_interesting_file().
113
+ if ( empty( $signature->filename_regex ) || preg_match( '#' . addcslashes( $signature->filename_regex, '#' ) . '#i', $file ) ) {
114
+ if ( null === $file_content )
115
+ $file_content = file_get_contents( $real_file );
116
+
117
+ $is_vulnerable = true;
118
+ reset( $signature->patterns );
119
+ $matches = array ();
120
+ while ( $is_vulnerable && list( , $pattern ) = each( $signature->patterns ) ) {
121
+ if ( !preg_match( '#' . addcslashes( $pattern, '#' ) . '#im', $file_content, $match ) ) {
122
+ $is_vulnerable = false;
123
+ break;
124
+ }
125
+ $matches[] = $match;
126
+ }
127
+ // Additional checking needed?
128
+ if ( method_exists( $signature, 'get_detailed_scanner' ) && $scanner = $signature->get_detailed_scanner() )
129
+ $is_vulnerable = $scanner->scan( $is_vulnerable, $file, $real_file, $file_content, $matches );
130
+ if ( $is_vulnerable ) {
131
+ $found[$signature->id] = $matches;
132
+ if ( isset( $signature->severity ) && $signature->severity > 8 ) // don't continue scanning
133
+ break;
134
+ }
135
+ }
136
+ }
137
+
138
+ return apply_filters_ref_array( 'post_scan_file', array ( $found, $file, $real_file, &$file_content ) );
139
+ }