get_var() here actually *introduces* a race condition
if (!is_array($backup_history)) $backup_history = array();
$backup_history = self::build_incremental_sets($backup_history);
// The most recent backup will be first. Then we can array_pop().
krsort($backup_history);
if (!$timestamp) return $backup_history;
return isset($backup_history[$timestamp]) ? $backup_history[$timestamp] : array();
}
/**
* Get the backup history for an indicated nonce
*
* @param String $nonce - Backup nonce to get a particular backup job
*
* @return Array|Boolean - either the particular backup indicated, or false
*/
public static function get_backup_set_by_nonce($nonce) {
if (empty($nonce)) return false;
$backup_history = self::get_history();
foreach ($backup_history as $timestamp => $backup_info) {
if ($nonce == $backup_info['nonce']) {
$backup_info['timestamp'] = $timestamp;
return $backup_info;
}
}
return false;
}
/**
* Get the HTML for the table of existing backups
*
* @param Array|Boolean $backup_history - a list of backups to use, or false to get the current list from the database
*
* @uses UpdraftPlus_Admin::include_template()
*
* @return String - HTML for the table
*/
public static function existing_backup_table($backup_history = false) {
global $updraftplus, $updraftplus_admin;
if (false === $backup_history) $backup_history = self::get_history();
if (!is_array($backup_history) || empty($backup_history)) return '
'.__('You have not yet made any backups.', 'updraftplus').'
';
$pass_values = array(
'backup_history' => $backup_history,
'updraft_dir' => $updraftplus->backups_dir_location(),
'backupable_entities' => $updraftplus->get_backupable_file_entities(true, true)
);
return $updraftplus_admin->include_template('wp-admin/settings/existing-backups-table.php', true, $pass_values);
}
/**
* This function will scan the backup history and split the files up in to incremental sets, foreign backup sets will only have one incremental set.
*
* @param Array $backup_history - the saved backup history
*
* @return Array - returns the backup history but also includes the incremental sets
*/
public static function build_incremental_sets($backup_history) {
global $updraftplus;
$backupable_entities = array_keys($updraftplus->get_backupable_file_entities(true, false));
$accept = apply_filters('updraftplus_accept_archivename', array());
foreach ($backup_history as $btime => $bdata) {
$incremental_sets = array();
foreach ($backupable_entities as $entity) {
if (empty($bdata[$entity]) || !is_array($bdata[$entity])) continue;
foreach ($bdata[$entity] as $key => $filename) {
if (preg_match('/^backup_([\-0-9]{15})_.*_([0-9a-f]{12})-[\-a-z]+([0-9]+)?+(\.(zip|gz|gz\.crypt))?$/i', $filename, $matches)) {
$timestamp = strtotime($matches[1]);
if (!isset($incremental_sets[$timestamp])) $incremental_sets[$timestamp] = array();
if (!isset($incremental_sets[$timestamp][$entity])) $incremental_sets[$timestamp][$entity] = array();
$incremental_sets[$timestamp][$entity][$key] = $filename;
} else {
$accepted = false;
foreach ($accept as $fkey => $acc) {
if (preg_match('/'.$acc['pattern'].'/i', $filename)) $accepted = $fkey;
}
if (!empty($accepted) && (false != ($btime = apply_filters('updraftplus_foreign_gettime', false, $accepted, $filename))) && $btime > 0) {
$timestamp = $btime;
if (!isset($incremental_sets[$timestamp])) $incremental_sets[$timestamp] = array();
if (!isset($incremental_sets[$timestamp][$entity])) $incremental_sets[$timestamp][$entity] = array();
$incremental_sets[$timestamp][$entity][] = $filename;
}
}
}
}
$backup_history[$btime]["incremental_sets"] = $incremental_sets;
}
return $backup_history;
}
/**
* Save the backup history. An abstraction function to make future changes easier.
*
* @param Array $backup_history - the backup history
* @param Boolean $use_cache - whether or not to use the WP options cache
*/
public static function save_history($backup_history, $use_cache = true) {
global $updraftplus;
// This data is constructed at run-time from the other keys; we do not wish to save redundant data
foreach ($backup_history as $btime => $bdata) {
unset($backup_history[$btime]['incremental_sets']);
}
// Explicitly set autoload to 'no', as the backup history can get quite big.
$changed = UpdraftPlus_Options::update_updraft_option('updraft_backup_history', $backup_history, $use_cache, 'no');
if (!$changed) {
$max_packet_size = $updraftplus->max_packet_size(false, false);
$serialization_size = strlen(addslashes(serialize($backup_history)));
// Take off the *approximate* over-head of UPDATE wp_options SET option_value='' WHERE option_name='updraft_backup_history'; (no need to be exact)
if ($max_packet_size < ($serialization_size + 100)) {
$max_packet_size = $updraftplus->max_packet_size();
$changed = UpdraftPlus_Options::update_updraft_option('updraft_backup_history', $backup_history, $use_cache, 'no');
if (!$changed) {
$updraftplus->log('The attempt to write the backup history to the WP database returned a negative code and the max packet size looked small. However, WP does not distinguish between a failure and no change from a previous update, so, this code is not conclusive and if no other symptoms are observed then there is no reason to infer any problem. Info: The updated database packet size is '.$max_packet_size.'; the serialization size is '.$serialization_size);
}
}
}
}
/**
* Used by self::always_get_from_db()
*
* @param String $v - ignored
* @return Mixed - the database option
*/
public static function filter_updraft_backup_history($v) {
global $wpdb;
$row = $wpdb->get_row($wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", 'updraft_backup_history'));
if (is_object($row)) return maybe_unserialize($row->option_value);
return false;
}
/**
* Make sure we get the value afresh from the db, instead of using the auto-loaded/cached value (which can be out of date, especially since backups are, by their nature, long-running)
*/
public static function always_get_from_db() {
add_filter('pre_option_updraft_backup_history', array('UpdraftPlus_Backup_History', 'filter_updraft_backup_history'));
}
/**
* This function examines inside the updraft directory to see if any new archives have been uploaded. If so, it adds them to the backup set. (Non-present items are also removed, only if the service is 'none').
*
* N.B. The logic is a bit more subtle than it needs to be, because of backups being keyed by backup time, instead of backup nonce, and the subsequent introduction of the possibility of incremental backup sets taken at different times. This could be cleaned up to reduce the amount of code and make it simpler in places.
*
* @param Boolean $remote_scan - scan not only local, but also remote storage
* @param Array|String $only_add_this_file - if set to an array (with keys 'file' and (optionally) 'label'), then a file will only be taken notice of if the filename matches the 'file' key (and the label will be associated with the backup set)
* @param Boolean $debug - include debugging messages. These will be keyed with keys beginning 'debug-' so that they can be distinguished.
*
* @return Array - an array of messages which the caller may wish to display to the user. N.B. Messages are not necessarily just strings.
*/
public static function rebuild($remote_scan = false, $only_add_this_file = false, $debug = false) {
global $updraftplus;
$messages = array();
$gmt_offset = get_option('gmt_offset');
// Array of nonces keyed by filename
$backup_nonces_by_filename = array();
// Array of backup times keyed by nonce
$backup_times_by_nonce = array();
$changes = false;
$backupable_entities = $updraftplus->get_backupable_file_entities(true, false);
$backup_history = self::get_history();
$updraft_dir = $updraftplus->backups_dir_location();
if (!is_dir($updraft_dir)) return array("Internal directory path does not indicate a directory ($updraft_dir)");
$accept = apply_filters('updraftplus_accept_archivename', array());
// First, process the database backup history to get a record of what is already known there. This means populating the arrays $backup_nonces_by_filename and $backup_times_by_nonce
foreach ($backup_history as $btime => $bdata) {
$found_file = false;
foreach ($bdata as $key => $values) {
if ('db' != $key && !isset($backupable_entities[$key])) continue;
// Record which set this file is found in
if (!is_array($values)) $values = array($values);
foreach ($values as $filename) {
if (!is_string($filename)) continue;
if (preg_match('/^backup_([\-0-9]{15})_.*_([0-9a-f]{12})-[\-a-z]+([0-9]+)?+(\.(zip|gz|gz\.crypt))?$/i', $filename, $matches)) {
$nonce = $matches[2];
if (isset($bdata['service']) && ('none' === $bdata['service'] || (is_array($bdata['service']) && (array('none') === $bdata['service'] || (1 == count($bdata['service']) && isset($bdata['service'][0]) && empty($bdata['service'][0]))))) && !is_file($updraft_dir.'/'.$filename)) {
// File without remote storage is no longer locally present
} else {
$found_file = true;
$backup_nonces_by_filename[$filename] = $nonce;
if (empty($backup_times_by_nonce[$nonce]) || $backup_times_by_nonce[$nonce] < 100) {
$backup_times_by_nonce[$nonce] = $btime;
} elseif ($btime < $backup_times_by_nonce[$nonce]) {
$backup_times_by_nonce[$nonce] = $btime;
}
}
} else {
$accepted = false;
foreach ($accept as $fkey => $acc) {
if (preg_match('/'.$acc['pattern'].'/i', $filename)) $accepted = $fkey;
}
if (!empty($accepted) && (false != ($btime = apply_filters('updraftplus_foreign_gettime', false, $accepted, $filename))) && $btime > 0) {
$found_file = true;
// Generate a nonce; this needs to be deterministic and based on the filename only
$nonce = substr(md5($filename), 0, 12);
$backup_nonces_by_filename[$filename] = $nonce;
if (empty($backup_times_by_nonce[$nonce]) || $backup_times_by_nonce[$nonce] < 100) {
$backup_times_by_nonce[$nonce] = $btime;
} elseif ($btime < $backup_times_by_nonce[$nonce]) {
$backup_times_by_nonce[$nonce] = $btime;
}
}
}
}
}
if (!$found_file) {
// File recorded as being without remote storage is no longer present. It may in fact exist in remote storage, and this will be picked up later (when we scan the remote storage).
unset($backup_history[$btime]);
$changes = true;
}
}
// Secondly, scan remote storage and get back lists of files and their sizes
// $remote_files has a key for each filename (basename), and the value is a list of remote destinations (e.g. 'dropbox', 's3') in which the file was found
$remote_files = array();
// A list of nonces found remotely (to help with handling sets split across destinations)
$remote_nonces_found = array();
$remote_sizes = array();
if ($remote_scan) {
$updraftplus->register_wp_http_option_hooks(true);
$storage_objects_and_ids = UpdraftPlus_Storage_Methods_Interface::get_storage_objects_and_ids(array_keys($updraftplus->backup_methods));
foreach ($storage_objects_and_ids as $method => $method_information) {
$object = $method_information['object'];
if (!method_exists($object, 'listfiles')) continue;
// Support of multi_options is now required for storage methods that implement listfiles()
if (!$object->supports_feature('multi_options')) {
error_log("UpdraftPlus: Multi-options not supported by: ".$method);
continue;
}
foreach ($method_information['instance_settings'] as $instance_id => $options) {
$object->set_options($options, false, $instance_id);
$files = $object->listfiles('backup_');
$method_description = $object->get_description();
if (is_array($files)) {
if ($debug) {
$messages[] = array(
'method' => $method,
'desc' => $method_description,
'code' => 'file-listing',
'message' => '',
'data' => $files,
'service_instance_id' => $instance_id,
);
}
foreach ($files as $entry) {
$filename = $entry['name'];
if (!preg_match('/^backup_([\-0-9]{15})_.*_([0-9a-f]{12})-([\-a-z]+)([0-9]+)?(\.(zip|gz|gz\.crypt))?$/i', $filename, $matches)) continue;
$nonce = $matches[2];
$btime = strtotime($matches[1]);
// Of course, it's possible that the site doing the scanning has a different timezone from the site that the backups were created in, in which case, this calculation will have a confusing result to the user. That outcome cannot be completely eliminated (without making the filename to reflect UTC, which confuses more users).
if (!empty($gmt_offset)) $btime -= $gmt_offset * 3600;
// Is the set already known?
if (isset($backup_times_by_nonce[$nonce])) {
// N.B. With an incremental set, the newly found file may be earlier than the known elements, so tha the backup array should be re-keyed.
$btime_exact = $backup_times_by_nonce[$nonce];
if ($btime > 100 && $btime_exact - $btime > 60 && !empty($backup_history[$btime_exact])) {
$changes = true;
$backup_history[$btime] = $backup_history[$btime_exact];
unset($backup_history[$btime_exact]);
$btime_exact = $btime;
$backup_times_by_nonce[$nonce] = $btime;
}
$btime = $btime_exact;
}
if ($btime <= 100) continue;
// We need to set this so that if a second file is found in remote storage then the time will be picked up.
$backup_times_by_nonce[$nonce] = $btime;
if (empty($backup_history[$btime]['service_instance_ids']) || empty($backup_history[$btime]['service_instance_ids'][$method])) {
$backup_history[$btime]['service_instance_ids'][$method] = array($instance_id);
$changes = true;
} elseif (!in_array($instance_id, $backup_history[$btime]['service_instance_ids'][$method])) {
$backup_history[$btime]['service_instance_ids'][$method][] = $instance_id;
$changes = true;
}
if (isset($remote_files[$filename])) {
$remote_files[$filename][] = $method;
} else {
$remote_files[$filename] = array($method);
}
if (!in_array($nonce, $remote_nonces_found)) $remote_nonces_found[] = $nonce;
if (!empty($entry['size'])) {
if (empty($remote_sizes[$filename]) || $remote_sizes[$filename] < $entry['size']) $remote_sizes[$filename] = $entry['size'];
}
}
} elseif (is_wp_error($files)) {
foreach ($files->get_error_codes() as $code) {
// Skip various codes which are not conditions to show to the user
if (in_array($code, array('no_settings', 'no_addon', 'insufficient_php', 'no_listing'))) continue;
$messages[] = array(
'method' => $method,
'desc' => $method_description,
'code' => $code,
'message' => $files->get_error_message($code),
'data' => $files->get_error_data($code),
'service_instance_id' => $instance_id,
);
}
}
}
}
$updraftplus->register_wp_http_option_hooks(false);
}
// Thirdly, see if there are any more files in the local directory than the ones already known about (possibly subject to a limitation specified via $only_add_this_file)
if (!$handle = opendir($updraft_dir)) return array("Failed to open the internal directory ($updraft_dir)");
while (false !== ($entry = readdir($handle))) {
if ('.' == $entry || '..' == $entry) continue;
$accepted_foreign = false;
$potmessage = false;
if (false !== $only_add_this_file && $entry != $only_add_this_file['file']) continue;
if (preg_match('/^backup_([\-0-9]{15})_.*_([0-9a-f]{12})-([\-a-z]+)([0-9]+)?(\.(zip|gz|gz\.crypt))?$/i', $entry, $matches)) {
// Interpret the time as one from the blog's local timezone, rather than as UTC
// $matches[1] is YYYY-MM-DD-HHmm, to be interpreted as being the local timezone
$btime = strtotime($matches[1]);
if (!empty($gmt_offset)) $btime -= $gmt_offset * 3600;
$nonce = $matches[2];
$type = $matches[3];
if ('db' == $type) {
$type .= empty($matches[4]) ? '' : $matches[4];
$index = 0;
} else {
$index = empty($matches[4]) ? '0' : max((int) $matches[4]-1, 0);
}
$itext = (0 == $index) ? '' : $index;
} elseif (false != ($accepted_foreign = apply_filters('updraftplus_accept_foreign', false, $entry)) && false !== ($btime = apply_filters('updraftplus_foreign_gettime', false, $accepted_foreign, $entry))) {
$nonce = substr(md5($entry), 0, 12);
$type = (preg_match('/\.sql(\.(bz2|gz))?$/i', $entry) || preg_match('/-database-([-0-9]+)\.zip$/i', $entry) || preg_match('/backup_db_/', $entry)) ? 'db' : 'wpcore';
$index = apply_filters('updraftplus_accepted_foreign_index', 0, $entry, $accepted_foreign);
$itext = $index ? $index : '';
$potmessage = array(
'code' => 'foundforeign_'.md5($entry),
'desc' => $entry,
'method' => '',
'message' => sprintf(__('Backup created by: %s.', 'updraftplus'), $accept[$accepted_foreign]['desc'])
);
} elseif ('.zip' == strtolower(substr($entry, -4, 4)) || preg_match('/\.sql(\.(bz2|gz))?$/i', $entry)) {
$potmessage = array(
'code' => 'possibleforeign_'.md5($entry),
'desc' => $entry,
'method' => '',
'message' => __('This file does not appear to be an UpdraftPlus backup archive (such files are .zip or .gz files which have a name like: backup_(time)_(site name)_(code)_(type).(zip|gz)).', 'updraftplus').'