VaultPress - Version 1.3.2

Version Description

Download this release

Release Info

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

Version 1.3.2

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