VaultPress - Version 1.3

Version Description

Download this release

Release Info

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

Version 1.3

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