Skip to content

Commit

Permalink
Plugins: Upgrade VaultPress to 1.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgonzalez committed Dec 17, 2013
1 parent 81e1bf3 commit d1ae725
Show file tree
Hide file tree
Showing 20 changed files with 1,371 additions and 115 deletions.
230 changes: 214 additions & 16 deletions plugins/vaultpress/class.vaultpress-database.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ class VaultPress_Database {
var $table = null;
var $pks = null;

function VaultPress_Database() {
$this->__construct();
}

function __construct() {
}

function attach( $table ) {
function attach( $table, $parse_create_table = false ) {
$this->table=$table;
if ( $parse_create_table ) {
$this->structure = $this->parse_create_table( $this->show_create() );
}
}

function get_tables( $filter=null ) {
Expand Down Expand Up @@ -49,7 +48,7 @@ function diff( $signatures ) {
if ( !is_array( $signatures ) || !count( $signatures ) )
return false;
if ( !$this->table )
return false;
return false;
$table = $wpdb->escape( $this->table );
$diff = array();
foreach ( $signatures as $where => $signature ) {
Expand All @@ -73,7 +72,7 @@ function count( $columns ) {
if ( !is_array( $columns ) || !count( $columns ) )
return false;
if ( !$this->table )
return false;
return false;
$table = $wpdb->escape( $this->table );
$column = $wpdb->escape( array_shift( $columns ) );
return $wpdb->get_var( "SELECT COUNT( $column ) FROM `$table`" );
Expand Down Expand Up @@ -124,17 +123,216 @@ function get_cols( $columns, $limit=false, $offset=false, $where=false ) {
// We don't need to actually record a real cron option value, just an empty array
if ( isset( $row->option_name ) && $row->option_name == 'cron' )
$row->option_value = serialize( array() );
$keys = array();
$vals = array();
foreach ( get_object_vars( $row ) as $i => $v ) {
$keys[] = sprintf( "`%s`", $wpdb->escape( $i ) );
$vals[] = sprintf( "'%s'", $wpdb->escape( $v ) );
if ( !in_array( $i, $columns ) )
unset( $row->$i );
if ( !empty( $this->structure ) ) {
$hash = md5( $this->convert_to_sql_string( $row, $this->structure->columns ) );
foreach ( get_object_vars( $row ) as $i => $v ) {
if ( !in_array( $i, $columns ) )
unset( $row->$i );
}

$row->hash = $hash;
} else {
$keys = array();
$vals = array();
foreach ( get_object_vars( $row ) as $i => $v ) {
$keys[] = sprintf( "`%s`", $wpdb->escape( $i ) );
$vals[] = sprintf( "'%s'", $wpdb->escape( $v ) );
if ( !in_array( $i, $columns ) )
unset( $row->$i );
}
$row->hash = md5( sprintf( "(%s) VALUES(%s)", implode( ',',$keys ), implode( ',',$vals ) ) );
}
$row->hash = md5( sprintf( "(%s) VALUES(%s)", implode( ',',$keys ), implode( ',',$vals ) ) );
$rval[]=$row;
}
return $rval;
}
}

/**
* Convert a PHP object to a mysqldump compatible string, using the provided data type information.
**/
function convert_to_sql_string( $data, $datatypes ) {
global $wpdb;
if ( !is_object( $data ) || !is_object( $datatypes ) )
return false;

foreach ( array_keys( (array)$data ) as $key )
$keys[] = sprintf( "`%s`", $wpdb->escape( $key ) );
foreach ( (array)$data as $key => $val ) {
if ( null === $val ) {
$vals[] = 'NULL';
continue;
}
$type = 'text';
if ( isset( $datatypes->$key->type ) )
$type= strtolower( $datatypes->$key->type );
if ( preg_match( '/int|double|float|decimal|bool/i', $type ) )
$type = 'number';

if ( 'number' === $type ) {
// do not add quotes to numeric types.
$vals[] = $val;
} else {
$val = $wpdb->escape( $val );
// Escape characters that aren't escaped by $wpdb->escape(): \n, \r, etc.
$val = str_replace( array( "\x0a", "\x0d", "\x1a" ), array( '\n', '\r', '\Z' ), $val );
$vals[] = sprintf( "'%s'", $val );
}
}
if ( !count($keys) )
return false;
// format the row as a mysqldump line: (`column1`, `column2`) VALUES (numeric_value1,'text value 2')
return sprintf( "(%s) VALUES (%s)", implode( ', ',$keys ), implode( ',',$vals ) );
}



function parse_create_table( $sql ) {
$table = new stdClass();

$table->raw = $sql;
$table->columns = new stdClass();
$table->primary = null;
$table->uniques = new stdClass();
$table->keys = new stdClass();
$sql = explode( "\n", trim( $sql ) );
$table->engine = preg_replace( '/^.+ ENGINE=(\S+) .+$/i', "$1", $sql[(count($sql)-1)] );
$table->charset = preg_replace( '/^.+ DEFAULT CHARSET=(\S+)( .+)?$/i', "$1", $sql[(count($sql)-1)] );
$table->single_int_paging_column = null;

foreach ( $sql as $idx => $val )
$sql[$idx] = trim($val);
$columns = preg_grep( '/^\s*`[^`]+`\s*\S*/', $sql );
if ( !$columns )
return false;

$table->name = preg_replace( '/(^[^`]+`|`[^`]+$)/', '', array_shift( preg_grep( '/^CREATE\s+TABLE\s+/', $sql ) ) );

foreach ( $columns as $line ) {
preg_match( '/^`([^`]+)`\s+([a-z]+)(\(\d+\))?\s*/', $line, $m );
$name = $m[1];
$table->columns->$name = new stdClass();
$table->columns->$name->null = (bool)stripos( $line, ' NOT NULL ' );
$table->columns->$name->type = $m[2];
if ( isset($m[3]) ) {
if ( substr( $m[3], 0, 1 ) == '(' )
$table->columns->$name->length = substr( $m[3], 1, -1 );
else
$table->columns->$name->length = $m[3];
} else {
$table->columns->$name->length = null;
}
if ( preg_match( '/ character set (\S+)/i', $line, $m ) ) {
$table->columns->$name->charset = $m[1];
} else {
$table->columns->$name->charset = '';
}
if ( preg_match( '/ collate (\S+)/i', $line, $m ) ) {
$table->columns->$name->collate = $m[1];
} else {
$table->columns->$name->collate = '';
}
if ( preg_match( '/ DEFAULT (.+),$/i', $line, $m ) ) {
if ( substr( $m[1], 0, 1 ) == "'" )
$table->columns->$name->default = substr( $m[1], 1, -1 );
else
$table->columns->$name->default = $m[1];
} else {
$table->columns->$name->default = null;
}
$table->columns->$name->line = $line;
}
$pk = preg_grep( '/^PRIMARY\s+KEY\s+/i', $sql );
if ( count( $pk ) ) {
$pk = array_pop( $pk );
$pk = preg_replace( '/(^[^\(]+\(`|`\),?$)/', '', $pk );
$pk = preg_replace( '/\([0-9]+\)/', '', $pk );
$pk = explode( '`,`', $pk );
$table->primary = $pk;
}
if ( is_array( $table->primary ) && count( $table->primary ) == 1 ) {
$pk_column_name = $table->primary[0];
switch( strtolower( $table->columns->$pk_column_name->type ) ) {
// Integers, exact value
case 'tinyint':
case 'smallint':
case 'int':
case 'integer':
case 'bigint':
// Fixed point, exact value
case 'decimal':
case 'numeric':
// Floating point, approximate value
case 'float':
case 'double':
case 'real':
// Date and Time
case 'date':
case 'datetime':
case 'timestamp':
$table->single_int_paging_column = $pk_column_name;
break;
}
}
$keys = preg_grep( '/^((?:UNIQUE )?INDEX|(?:UNIQUE )?KEY)\s+/i', $sql );
if ( !count( $keys ) )
return $table;
foreach ( $keys as $idx => $key ) {
if ( 0 === strpos( $key, 'UNIQUE' ) )
$is_unique = false;
else
$is_unique = true;

// for KEY `refresh` (`ip`,`time_last`) USING BTREE,
$key = preg_replace( '/ USING \S+ ?(,?)$/', '$1', $key );

// for KEY `id` USING BTREE (`id`),
$key = preg_replace( '/` USING \S+ \(/i', '` (', $key );

$key = preg_replace( '/^((?:UNIQUE )?INDEX|(?:UNIQUE )?KEY)\s+/i', '', $key );
$key = preg_replace( '/\([0-9]+\)/', '', $key );
preg_match( '/^`([^`]+)`\s+\(`(.+)`\),?$/', $key, $m );
$key = $m[1]; //preg_replace( '/\([^)]+\)/', '', $m[1]);
if ( !$key )
continue;
if ( $is_unique )
$table->keys->$key = explode( '`,`', $m[2] );
else
$table->uniques->$key = explode( '`,`', $m[2] );
}

$uniques = get_object_vars( $table->uniques );
foreach( $uniques as $idx => $val ) {
if ( is_array( $val ) && count( $val ) == 1 ) {
$pk_column_name = $val[0];
switch( strtolower( $table->columns->$pk_column_name->type ) ) {
// Integers, exact value
case 'tinyint':
case 'smallint':
case 'int':
case 'integer':
case 'bigint':
// Fixed point, exact value
case 'decimal':
case 'numeric':
// Floating point, approximate value
case 'float':
case 'double':
case 'real':
// Date and Time
case 'date':
case 'datetime':
case 'timestamp':
$table->single_int_paging_column = $pk_column_name;
break;
}
}
}

if ( empty( $table->primary ) ) {
if ( !empty( $uniques ) )
$table->primary = array_shift( $uniques );
}

return $table;
}
}
62 changes: 53 additions & 9 deletions plugins/vaultpress/class.vaultpress-filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ class VaultPress_Filesystem {
var $dir = null;
var $keys = array( 'ino', 'uid', 'gid', 'size', 'mtime', 'blksize', 'blocks' );

function VaultPress_Filesystem() {
$this->__construct();
}

function __construct() {
}

Expand Down Expand Up @@ -48,8 +44,20 @@ function fdump( $file ) {
header("Content-Type: application/octet-stream;");
header("Content-Transfer-Encoding: binary");
@ob_end_clean();
if ( !file_exists( $file ) || !is_readable( $file ) )
die( "no such file" );
if ( !file_exists( $file ) || !is_readable( $file ) ) {
$file_name = basename( $file );
if ( 'wp-config.php' == $file_name ) {
$dir = dirname( $file );
$dir = explode( DIRECTORY_SEPARATOR, $dir );
array_pop( $dir );
$dir = implode( DIRECTORY_SEPARATOR, $dir );
$file = trailingslashit( $dir ) . $file_name;
if ( !file_exists( $file ) || !is_readable( $file ) )
die( "no such file" );
} else {
die( "no such file" );
}
}
if ( !is_file( $file ) && !is_link( $file ) )
die( "can only dump files" );
$fp = @fopen( $file, 'rb' );
Expand All @@ -75,13 +83,26 @@ function stat( $file, $md5=true, $sha1=true ) {
if ( $sha1 )
$rval['sha1'] = sha1_file( $file );
}
$rval['path'] = str_replace( $this->dir, '', $file );
$dir = $this->dir;
if ( 0 !== strpos( $file, $dir ) && 'wp-config.php' == basename( $file ) ) {
$dir = explode( DIRECTORY_SEPARATOR, $dir );
array_pop( $dir );
$dir = implode( DIRECTORY_SEPARATOR, $dir );
}
$rval['path'] = str_replace( $dir, '', $file );
return $rval;
}

function ls( $what, $md5=false, $sha1=false, $limit=null, $offset=null ) {
function ls( $what, $md5=false, $sha1=false, $limit=null, $offset=null, $full_list=false ) {
clearstatcache();
$path = realpath($this->dir . $what);
$dir = $this->dir;
if ( !$path && '/wp-config.php' == $what ) {
$dir = explode( DIRECTORY_SEPARATOR, $dir );
array_pop( $dir );
$dir = implode( DIRECTORY_SEPARATOR, $dir );
$path = realpath( $dir . $what );
}
if ( is_file($path) )
return $this->stat( $path, $md5, $sha1 );
if ( is_dir($path) ) {
Expand All @@ -92,6 +113,8 @@ function ls( $what, $md5=false, $sha1=false, $limit=null, $offset=null ) {
$limit = $offset + (int)$limit;
foreach ( (array)$this->scan_dir( $path ) as $i ) {
$current++;
if ( !$full_list && !$this->should_backup_file( $i ) )
continue;
if ( $offset >= $current )
continue;
if ( $limit && $limit < $current )
Expand All @@ -107,13 +130,34 @@ function ls( $what, $md5=false, $sha1=false, $limit=null, $offset=null ) {
}
}

function should_backup_file( $filepath ) {
$vp = VaultPress::init();
if ( is_dir( $filepath ) )
$filepath = trailingslashit( $filepath );
$regex_patterns = $vp->get_should_ignore_files();
foreach ( $regex_patterns as $pattern ) {
$matches = array();
if ( preg_match( $pattern, $filepath, $matches ) ) {
return false;
}
}
return true;
}

function validate( $file ) {
$rpath = realpath( $this->dir.$file );
$dir = $this->dir;
if ( !$rpath && '/wp-config.php' == $file ) {
$dir = explode( DIRECTORY_SEPARATOR, $dir );
array_pop( $dir );
$dir = implode( DIRECTORY_SEPARATOR, $dir );
$rpath = realpath( $dir . $file );
}
if ( !$rpath )
die( serialize( array( 'type' => 'null', 'path' => $file ) ) );
if ( is_dir( $rpath ) )
$rpath = "$rpath/";
if ( strpos( $rpath, $this->dir ) !== 0 )
if ( strpos( $rpath, $dir ) !== 0 )
return false;
return true;
}
Expand Down
7 changes: 2 additions & 5 deletions plugins/vaultpress/class.vaultpress-hotfixes.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?php

class VaultPress_Hotfixes {
function VaultPress_Hotfixes() {
$this->__construct();
}

function __construct() {
global $wp_version;

Expand Down Expand Up @@ -237,7 +233,8 @@ function r16625( $query ) {
return $query;

// Pull the post_id which is the last thing in the origin query, after a space, no quotes
$post_id = array_pop( explode( " ", $query ) );
$query_parts = explode( " ", $query );
$post_id = array_pop( $query_parts );

// Chop off the beginning and end of the original query to get our unsanitized $tb_ping
$tb_ping = substr(
Expand Down
Loading

0 comments on commit d1ae725

Please sign in to comment.