/**
  * Utility function called by WPCron scheduling dispatch. The parameter passed in is the
  * config item key to an existing entry. If a match is found and verified it will be processed
  *
  * @since 1.0.2
  * @see
  *
  * @param int $item_key - Match to an item in the $this->config_data['items'] array.
  *
  * @return none
  */
 function snapshot_backup_cron_proc($item_key)
 {
     global $wpdb;
     $home_path = apply_filters('snapshot_home_path', get_home_path());
     @set_time_limit(0);
     $old_error_handler = set_error_handler(array($this, 'snapshot_ErrorHandler'));
     if (isset($this->config_data['config']['memoryLimit']) && !empty($this->config_data['config']['memoryLimit'])) {
         @ini_set('memory_limit', $this->config_data['config']['memoryLimit']);
     }
     $item_key = intval($item_key);
     // If we are somehow called for an item_key not in our list then remove any future cron calls then die
     if (!defined('SNAPSHOT_DOING_CRON') || SNAPSHOT_DOING_CRON != true) {
         if (!isset($this->config_data['items'][$item_key])) {
             $timestamp = wp_next_scheduled($this->_settings['backup_cron_hook'], array(intval($item_key)));
             if ($timestamp) {
                 wp_unschedule_event($timestamp, $this->_settings['backup_cron_hook'], array(intval($item_key)));
             }
             die;
         }
     }
     $item = $this->config_data['items'][$item_key];
     $data_item_key = time();
     if (!isset($item['destination-sync'])) {
         $item['destination-sync'] = "archive";
     }
     // If we are syncing/mirroring file and we don't have and database files. Then no need going through the
     // process of creating a new data_item entry.
     $_has_incomplete = false;
     if ($item['destination-sync'] == "mirror") {
         if (isset($item['data']) && count($item['data'])) {
             $data_item = Snapshot_Helper_Utility::latest_data_item($item['data']);
             //echo "data_item<pre>"; print_r($data_item); echo "</pre>";
             if (isset($data_item['destination-status']) && count($data_item['destination-status'])) {
                 $dest_item = Snapshot_Helper_Utility::latest_data_item($data_item['destination-status']);
                 if (!isset($dest_item['sendFileStatus']) || $dest_item['sendFileStatus'] !== true) {
                     $_has_incomplete = true;
                 }
             }
         }
     }
     if ($item['destination-sync'] != "mirror" || $_has_incomplete == false || $item['tables-option'] == "all" || $item['tables-option'] == "selected") {
         if (!isset($this->config_data['items'][$item_key]['data'][$data_item_key]['archive-status'])) {
             $this->config_data['items'][$item_key]['data'][$data_item_key]['archive-status'] = array();
         }
         if (!defined('SNAPSHOT_DOING_CRON') || SNAPSHOT_DOING_CRON != true) {
             // If we have a valid time_key but the item's interval is not a scheduled item then remove future cron and die
             //if ((isset($item['interval'])) && ($item['interval'] == "")) {
             //	$timestamp = wp_next_scheduled( $this->_settings['backup_cron_hook'], array(intval($item_key)) );
             //
             //	if ($timestamp) {
             //		wp_unschedule_event($timestamp, $this->_settings['backup_cron_hook'], array(intval($item_key)) );
             //	}
             //	die();
             //}
         }
         $snapshot_locker = new Snapshot_Helper_Locker($this->_settings['backupLockFolderFull'], $item_key);
         // If we can't lock the locker then abort.
         if (!$snapshot_locker->is_locked()) {
             return;
         }
         $locket_info = array('doing' => __('Creating Archive', SNAPSHOT_I18N_DOMAIN), 'item_key' => $item_key, 'data_item_key' => $data_item_key, 'time_start' => time());
         $snapshot_locker->set_locker_info($locket_info);
         $this->snapshot_logger = new Snapshot_Helper_Logger($this->_settings['backupLogFolderFull'], $item_key, $data_item_key);
         Snapshot_Helper_Debug::set_error_reporting($this->config_data['config']['errorReporting']);
         /* Needed to create the archvie zip file */
         if ($this->config_data['config']['zipLibrary'] == "PclZip") {
             if (!defined('PCLZIP_TEMPORARY_DIR')) {
                 define('PCLZIP_TEMPORARY_DIR', trailingslashit($this->_settings['backupBackupFolderFull']) . $item_key . "/");
             }
             //$this->snapshot_logger->log_message("init: Using PclZip PCLZIP_TEMPORARY_DIR". PCLZIP_TEMPORARY_DIR);
             if (!class_exists('class PclZip')) {
                 require_once ABSPATH . '/wp-admin/includes/class-pclzip.php';
             }
         }
         $this->snapshot_logger->log_message('init');
         $_post_array['snapshot-proc-action'] = "init";
         $_post_array['snapshot-action'] = "cron";
         $_post_array['snapshot-blog-id'] = $item['blog-id'];
         $_post_array['snapshot-item'] = $item_key;
         $_post_array['snapshot-data-item'] = $data_item_key;
         $_post_array['snapshot-interval'] = $item['interval'];
         $_post_array['snapshot-tables-option'] = $item['tables-option'];
         $_post_array['snapshot-destination-sync'] = $item['destination-sync'];
         $_post_array['snapshot-tables-array'] = array();
         if ($_post_array['snapshot-tables-option'] == "none") {
             // Nothing to process here.
         } else {
             if ($_post_array['snapshot-tables-option'] == "all") {
                 $tables_sections = Snapshot_Helper_Utility::get_database_tables($item['blog-id']);
                 //$this->_session->data['tables_sections'] = $tables_sections;
                 if ($tables_sections) {
                     foreach ($tables_sections as $section => $tables) {
                         $_post_array['snapshot-tables-array'] = array_merge($_post_array['snapshot-tables-array'], $tables);
                     }
                 }
             } else {
                 if ($_post_array['snapshot-tables-option'] == "selected") {
                     if (isset($item['tables-sections'])) {
                         $this->_session->data['tables-sections'] = $item['tables-sections'];
                         foreach ($item['tables-sections'] as $section => $tables) {
                             $_post_array['snapshot-tables-array'] = array_merge($_post_array['snapshot-tables-array'], $tables);
                         }
                     }
                 }
             }
         }
         if ($item['destination-sync'] == "archive") {
             $_post_array['snapshot-files-option'] = $item['files-option'];
             $_post_array['snapshot-files-sections'] = array();
             if ($_post_array['snapshot-files-option'] == "none") {
             } else {
                 if ($_post_array['snapshot-files-option'] == "all") {
                     if (is_main_site($item['blog-id'])) {
                         $_post_array['snapshot-files-sections'] = array('themes', 'plugins', 'media');
                         if (is_multisite()) {
                             $_post_array['snapshot-files-sections'][] = 'mu-plugins';
                         }
                     } else {
                         $_post_array['snapshot-files-sections'] = array('media');
                     }
                 } else {
                     if ($_post_array['snapshot-files-option'] == "selected") {
                         if (isset($item['files-sections'])) {
                             $_post_array['snapshot-files-sections'] = $item['files-sections'];
                         }
                     }
                 }
             }
         } else {
             $_post_array['snapshot-files-option'] = "none";
         }
         ob_start();
         $error_array = $this->snapshot_ajax_backup_init($item, $_post_array);
         $function_output = ob_get_contents();
         ob_end_clean();
         if (isset($error_array['errorStatus']) && $error_array['errorStatus'] == true) {
             $this->snapshot_logger->log_message("init: error_array" . print_r($error_array, true));
             $this->snapshot_logger->log_message("init: item" . print_r($item, true));
             $this->snapshot_logger->log_message("init: output:" . $function_output);
             $this->snapshot_logger->log_message("memory limit: " . ini_get('memory_limit') . ": memory usage current: " . Snapshot_Helper_Utility::size_format(memory_get_usage(true)) . ": memory usage peak: " . Snapshot_Helper_Utility::size_format(memory_get_peak_usage(true)));
             $this->config_data['items'][$item_key]['data'][$data_item_key]['archive-status'][time()] = $error_array;
             $this->add_update_config_item($item_key, $this->config_data['items'][$item_key]);
             unset($snapshot_locker);
             die;
         }
         if (isset($error_array['table_data']) && count($error_array['table_data'])) {
             // Switch to the blog site we are attempting to backup. This will ensure it should work properly
             if (is_multisite()) {
                 $current_blogid = $wpdb->blogid;
                 switch_to_blog(intval($item['blog-id']));
             }
             foreach ($error_array['table_data'] as $idx => $table_item) {
                 unset($_post_array);
                 $_post_array['snapshot-proc-action'] = "table";
                 $_post_array['snapshot-action'] = "cron";
                 $_post_array['snapshot-blog-id'] = $item['blog-id'];
                 $_post_array['snapshot-item'] = $item_key;
                 $_post_array['snapshot-data-item'] = $data_item_key;
                 $_post_array['snapshot-table-data-idx'] = $idx;
                 $this->snapshot_logger->log_message("table: " . $table_item['table_name'] . " segment: " . $table_item['segment_idx'] . "/" . $table_item['segment_total']);
                 ob_start();
                 $error_array_table = $this->snapshot_ajax_backup_table($item, $_post_array);
                 $function_output = ob_get_contents();
                 ob_end_clean();
                 if (isset($error_array['errorStatus']) && $error_array['errorStatus'] == true) {
                     // We have a problem.
                     $this->snapshot_logger->log_message("table: " . $table_item['table_name'] . ": error_array" . print_r($error_array_table, true));
                     $this->snapshot_logger->log_message("table: " . $table_item['table_name'] . ": _SESSION" . print_r($this->_session, true));
                     $this->snapshot_logger->log_message("table: " . $table_item['table_name'] . ": item" . print_r($item, true));
                     $this->snapshot_logger->log_message("table: output:" . $function_output);
                     $this->snapshot_logger->log_message("memory limit: " . ini_get('memory_limit') . ": memory usage current: " . Snapshot_Helper_Utility::size_format(memory_get_usage(true)) . ": memory usage peak: " . Snapshot_Helper_Utility::size_format(memory_get_peak_usage(true)));
                     $this->config_data['items'][$item_key]['data'][$data_item_key]['archive-status'][time()] = $error_array;
                     $this->add_update_config_item($item_key, $this->config_data['items'][$item_key]);
                     unset($snapshot_locker);
                     die;
                 }
             }
             if (is_multisite()) {
                 if (isset($current_blogid)) {
                     switch_to_blog(intval($current_blogid));
                 }
             }
         } else {
             $this->snapshot_logger->log_message("table: non selected");
         }
         if ($item['destination-sync'] == "archive") {
             if (isset($error_array['files_data']) && count($error_array['files_data'])) {
                 foreach ($error_array['files_data'] as $file_set_key) {
                     unset($_post_array);
                     $_post_array['snapshot-proc-action'] = "file";
                     $_post_array['snapshot-action'] = "cron";
                     $_post_array['snapshot-blog-id'] = $item['blog-id'];
                     $_post_array['snapshot-item'] = $item_key;
                     $_post_array['snapshot-file-data-key'] = $file_set_key;
                     ob_start();
                     $error_array_file = $this->snapshot_ajax_backup_file($item, $_post_array);
                     $function_output = ob_get_contents();
                     ob_end_clean();
                     if (isset($error_array_file['errorStatus']) && $error_array_file['errorStatus'] == true) {
                         // We have a problem.
                         $this->snapshot_logger->log_message("file: _post_array:" . print_r($_post_array, true));
                         $this->snapshot_logger->log_message("file: error_array:" . print_r($error_array_file, true));
                         $this->snapshot_logger->log_message("file: _SESSION:" . print_r($this->_session, true));
                         $this->snapshot_logger->log_message("file: item:" . print_r($item, true));
                         $this->snapshot_logger->log_message("file: output:" . $function_output);
                         $this->snapshot_logger->log_message("memory limit: " . ini_get('memory_limit') . ": memory usage current: " . Snapshot_Helper_Utility::size_format(memory_get_usage(true)) . ": memory usage peak: " . Snapshot_Helper_Utility::size_format(memory_get_peak_usage(true)));
                         $this->config_data['items'][$item_key]['data'][$data_item_key]['archive-status'][time()] = $error_array;
                         $this->add_update_config_item($item_key, $this->config_data['items'][$item_key]);
                         unset($snapshot_locker);
                         die;
                     }
                 }
                 if (isset($error_array['files_data']['excluded']['pattern']) && count($error_array['files_data']['excluded']['pattern'])) {
                     $this->snapshot_logger->log_message(__("file: The following files are excluded due to match exclusion patterns.", SNAPSHOT_I18N_DOMAIN));
                     foreach ($error_array['files_data']['excluded']['pattern'] as $idx => $filename) {
                         $filename = str_replace($home_path, '', $filename);
                         $this->snapshot_logger->log_message("file: excluded:  " . $filename);
                     }
                 }
                 if (isset($error_array['files_data']['excluded']['error']) && count($error_array['files_data']['excluded']['error'])) {
                     $this->snapshot_logger->log_message(__("file: The following files are excluded because snapshot cannot open them. Check file permissions or locks", SNAPSHOT_I18N_DOMAIN));
                     foreach ($error_array['files_data']['excluded']['error'] as $idx => $filename) {
                         $filename = str_replace($home_path, '', $filename);
                         $this->snapshot_logger->log_message("file: error: " . $filename);
                     }
                 }
             } else {
                 $this->snapshot_logger->log_message("file: non selected");
             }
         } else {
             $this->snapshot_logger->log_message("file: mirroring enabled. Files are synced during send to destination.");
         }
         $_post_array['snapshot-proc-action'] = "finish";
         $_post_array['snapshot-action'] = "cron";
         $_post_array['snapsho-blog-id'] = $item['blog-id'];
         $_post_array['snapshot-item'] = $item_key;
         $_post_array['snapshot-data-item'] = $data_item_key;
         ob_start();
         $error_array = $this->snapshot_ajax_backup_finish($item, $_post_array);
         $function_output = ob_get_contents();
         ob_end_clean();
         if (isset($error_array['errorStatus']) && $error_array['errorStatus'] == true) {
             // We have a problem.
             $this->snapshot_logger->log_message("finish: error_array:" . print_r($error_array, true));
             $this->snapshot_logger->log_message("finish: _SESSION:" . print_r($this->_session, true));
             $this->snapshot_logger->log_message("finish: item:" . print_r($item, true));
             $this->snapshot_logger->log_message("finish: output:" . $function_output);
             $this->snapshot_logger->log_message("memory limit: " . ini_get('memory_limit') . ": memory usage current: " . Snapshot_Helper_Utility::size_format(memory_get_usage(true)) . ": memory usage peak: " . Snapshot_Helper_Utility::size_format(memory_get_peak_usage(true)));
             $this->config_data['items'][$item_key]['data'][$data_item_key]['archive-status'][time()] = $error_array;
             $this->add_update_config_item($item_key, $this->config_data['items'][$item_key]);
             unset($snapshot_locker);
             die;
         } else {
         }
         $this->snapshot_logger->log_message("memory limit: " . ini_get('memory_limit') . ": memory usage current: " . Snapshot_Helper_Utility::size_format(memory_get_usage(true)) . ": memory usage peak: " . Snapshot_Helper_Utility::size_format(memory_get_peak_usage(true)));
         if (isset($error_array['responseFile'])) {
             $this->snapshot_logger->log_message("finish: " . basename($error_array['responseFile']));
         }
         $this->config_data['items'][$item_key]['data'][$data_item_key]['archive-status'][time()] = $error_array;
         $this->add_update_config_item($item_key, $this->config_data['items'][$item_key]);
         // Checking for Archive Account purge
         $this->purge_archive_limit($item_key);
         unset($snapshot_locker);
     }
     $this->process_item_remote_files($item_key);
 }
示例#2
0
        /**
         * Metabox to show the site tables with checkboxes
         *
         * @since 1.0.2
         *
         * @param string $title - Title to be displayed in header of metabox
         * @param array $item - The current viewed item
         * @param array $blog_tables_last - Array of tables used in the previous snapshot for this site.
         * Using these tables will pre-select the table in this instance
         *
         * @return none
         */
        function snapshot_metabox_show_backup_tables_options($title, $item)
        {
            global $wpdb;
            if (!isset($item['blog-id'])) {
                $item['blog-id'] = $wpdb->blogid;
            }
            $table_sets = Snapshot_Helper_Utility::get_database_tables($item['blog-id']);
            if (isset(WPMUDEVSnapshot::instance()->config_data['config']['tables_last'][$item['blog-id']])) {
                $blog_tables_last = WPMUDEVSnapshot::instance()->config_data['config']['tables_last'][$item['blog-id']];
            } else {
                $blog_tables_last = array();
            }
            if (!isset($item['tables-option'])) {
                $item['tables-option'] = "all";
            }
            ?>
			<div class="postbox">
				<h3 class="hndle"><span><?php 
            echo $title;
            ?>
</span></h3>

				<div class="inside">

					<table class="form-table snapshot-backup-tables">
						<tr class="">
							<td>
								<p><?php 
            _e('Select the database tables backup option for this Snapshot.', SNAPSHOT_I18N_DOMAIN);
            ?>
</p>
								<ul>
									<li><input type="radio" class="snapshot-tables-option"
									           id="snapshot-tables-option-none" value="none"
											<?php 
            if ($item['tables-option'] == "none") {
                echo ' checked="checked" ';
            }
            ?>
									           name="snapshot-tables-option"> <label
											for="snapshot-tables-option-none"><?php 
            _e('Do not include database tables in this Snapshot', SNAPSHOT_I18N_DOMAIN);
            ?>
</label>
									</li>

									<li><input type="radio" class="snapshot-tables-option"
									           id="snapshot-tables-option-all" value="all"
											<?php 
            if ($item['tables-option'] == "all") {
                echo ' checked="checked" ';
            }
            ?>
									           name="snapshot-tables-option"> <label
											for="snapshot-tables-option-all"><?php 
            _e('Include <strong>all</strong> blog database tables in this archive. This will automatically include new tables.', SNAPSHOT_I18N_DOMAIN);
            ?>
</label></li>

									<li><input type="radio" class="snapshot-tables-option"
									           id="snapshot-tables-option-selected" value="selected"
											<?php 
            if ($item['tables-option'] == "selected") {
                echo ' checked="checked" ';
            }
            ?>
									           name="snapshot-tables-option"> <label
											for="snapshot-tables-option-selected"><?php 
            _e('Include <strong>selected</strong> database tables in this Snapshot', SNAPSHOT_I18N_DOMAIN);
            ?>
</label>

										<div id="snapshot-selected-tables-container"
										     style="margin-left: 30px; padding-top: 20px; <?php 
            if ($item['tables-option'] == "none" || $item['tables-option'] == "all") {
                echo ' display:none; ';
            }
            ?>
">
											<?php 
            $tables_sets_idx = array('global' => __("WordPress Global Tables", SNAPSHOT_I18N_DOMAIN), 'wp' => __("WordPress core Tables", SNAPSHOT_I18N_DOMAIN), 'non' => __("Non-WordPress Tables", SNAPSHOT_I18N_DOMAIN), 'other' => __("Other Tables", SNAPSHOT_I18N_DOMAIN), 'error' => __("Error Tables - These tables are skipped for the noted reasons.", SNAPSHOT_I18N_DOMAIN));
            foreach ($tables_sets_idx as $table_set_key => $table_set_title) {
                if (isset($table_sets[$table_set_key]) && count($table_sets[$table_set_key])) {
                    $display_set = 'block';
                } else {
                    $display_set = 'none';
                }
                ?>
											<div id="snapshot-tables-<?php 
                echo $table_set_key;
                ?>
-set"
											     class="snapshot-tables-set"
											     style="display: <?php 
                echo $display_set;
                ?>
">

												<p class="snapshot-tables-title"><?php 
                echo $table_set_title;
                ?>
 <?php 
                if ($table_set_key != 'error') {
                    ?>
														<a
														class="button-link snapshot-table-select-all" href="#"
														id="snapshot-table-<?php 
                    echo $table_set_key;
                    ?>
-select-all"><?php 
                    _e('Select all', SNAPSHOT_I18N_DOMAIN);
                    ?>
</a><?php 
                }
                ?>
</p>

												<?php 
                if ($table_set_key == "global") {
                    ?>
<p
														class="description"><?php 
                    _e('These global user tables contain blog specific user information which can be included as part of the snapshot archive. Only users whose primary blog matches this selected blog will be included. <strong>Superadmin users will not be included in the sub-site archive.</strong>', SNAPSHOT_I18N_DOMAIN);
                    ?>
</p><?php 
                }
                ?>
												<ul class="snapshot-table-list"
												    id="snapshot-table-list-<?php 
                echo $table_set_key;
                ?>
">
													<?php 
                if (isset($table_sets[$table_set_key]) && count($table_sets[$table_set_key])) {
                    $tables = $table_sets[$table_set_key];
                    foreach ($tables as $table_key => $table_name) {
                        $is_checked = '';
                        if ($table_set_key == 'error') {
                            ?>
																<li style="clear:both"><?php 
                            echo $table_name['name'];
                            ?>
 &ndash; <?php 
                            echo $table_name['reason'];
                            ?>
</li><?php 
                        } else {
                            if (isset($_REQUEST['backup-tables'])) {
                                if (isset($_REQUEST['backup-tables'][$table_set_key][$table_key])) {
                                    $is_checked = ' checked="checked" ';
                                }
                            } else {
                                if (isset($_GET['page']) && sanitize_text_field($_GET['page']) == "snapshots_new_panel") {
                                    if (isset($blog_tables_last[$table_set_key]) && array_search($table_key, $blog_tables_last[$table_set_key]) !== false) {
                                        $is_checked = ' checked="checked" ';
                                    }
                                } else {
                                    if (isset($_GET['page']) && sanitize_text_field($_GET['page']) == "snapshots_edit_panel") {
                                        if (isset($item['tables-sections'])) {
                                            if (isset($item['tables-sections'][$table_set_key][$table_key])) {
                                                $is_checked = ' checked="checked" ';
                                            }
                                        } else {
                                            if (isset($item['tables'])) {
                                                if (array_search($table_key, $item['tables']) !== false) {
                                                    $is_checked = ' checked="checked" ';
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            ?>
																<li><input type="checkbox" <?php 
                            echo $is_checked;
                            ?>
																           class="snapshot-table-item"
																           id="snapshot-tables-<?php 
                            echo $table_key;
                            ?>
"
																           value="<?php 
                            echo $table_key;
                            ?>
"
																           name="snapshot-tables[<?php 
                            echo $table_set_key;
                            ?>
][<?php 
                            echo $table_key;
                            ?>
]">
																<label
																	for="snapshot-tables-<?php 
                            echo $table_key;
                            ?>
"><?php 
                            echo $table_name;
                            ?>
</label></li><?php 
                        }
                    }
                } else {
                    ?>
														<li><?php 
                    _e('No Tables', SNAPSHOT_I18N_DOMAIN);
                    ?>
</li><?php 
                }
                ?>
												</ul>

												</div><?php 
            }
            ?>
										</div>
									</li>

								</ul>
							</td>
						</tr>
					</table>
				</div>
				<!-- end inside -->
			</div><!-- end postbox -->
		<?php 
        }