Esempio n. 1
0
 /**
  * This 'init' process begins the user's backup via AJAX. Creates the session backup file.
  *
  * @since 1.0.0
  * @see
  *
  * @param none
  *
  * @return none
  */
 function snapshot_ajax_backup_init($item, $_post_array)
 {
     global $wpdb;
     $home_path = apply_filters('snapshot_home_path', get_home_path());
     $error_status = array();
     $error_status['errorStatus'] = false;
     $error_status['errorText'] = "";
     $error_status['responseText'] = "";
     $error_status['table_data'] = array();
     $error_status['files_data'] = array();
     if (isset($this->_session->data)) {
         unset($this->_session->data);
     }
     $sessionItemBackupFolder = trailingslashit($this->_settings['backupBackupFolderFull']);
     $sessionItemBackupFolder = trailingslashit($sessionItemBackupFolder) . intval($item['timestamp']);
     if (!file_exists($sessionItemBackupFolder)) {
         wp_mkdir_p($sessionItemBackupFolder);
     }
     if (!is_writable($sessionItemBackupFolder)) {
         $error_status['errorStatus'] = true;
         $error_status['errorText'] = "<p>" . __("ERROR: Snapshot backup aborted.<br />The Snapshot folder is not writeable. Check the settings.", SNAPSHOT_I18N_DOMAIN) . " " . $sessionItemBackupFolder . "</p>";
         return $error_status;
     }
     // Cleanup any files from a previous backup attempt
     if ($dh = opendir($sessionItemBackupFolder)) {
         while (($file = readdir($dh)) !== false) {
             if ($file == '.' || $file == '..') {
                 continue;
             }
             @unlink(trailingslashit($sessionItemBackupFolder) . $file);
         }
         closedir($dh);
     }
     $this->_session->data['backupItemFolder'] = $sessionItemBackupFolder;
     if (isset($this->_session->data['table_data'])) {
         unset($this->_session->data['table_data']);
     }
     if (isset($item['tables-option'])) {
         if ($item['tables-option'] == "none") {
         } else {
             if ($item['tables-option'] == "all") {
                 $tables_sections = Snapshot_Helper_Utility::get_database_tables($item['blog-id']);
             } else {
                 if ($item['tables-option'] == "selected") {
                     // This should already be set from the Add/Update form post
                     $tables_sections = $item['tables-sections'];
                 }
             }
         }
     }
     //echo "tables_sections<pre>"; print_r($tables_sections); echo "</pre>";
     //die();
     if (isset($tables_sections) && count($tables_sections)) {
         foreach ($tables_sections as $section => $tables_set) {
             foreach ($tables_set as $table_name) {
                 $_set = array();
                 if ($section == "global") {
                     //echo "table_name[". $table_name ."]<br />";
                     if ($table_name == $wpdb->base_prefix . "users" || $table_name == $wpdb->base_prefix . "usermeta") {
                         if (!isset($this->_session->data['global_user_ids'])) {
                             $this->_session->data['global_user_ids'] = array();
                             $sql_str = "SELECT user_id FROM " . $wpdb->base_prefix . "usermeta WHERE meta_key='primary_blog' AND meta_value='" . $item['blog-id'] . "'";
                             //echo "sql_str=[". $sql_str ."]<br />";
                             $user_ids = $wpdb->get_col($sql_str);
                             if ($user_ids) {
                                 $this->_session->data['global_user_ids'] = $user_ids;
                             }
                         }
                         if (isset($this->_session->data['global_user_ids']) && is_array($this->_session->data['global_user_ids']) && count($this->_session->data['global_user_ids'])) {
                             if ($table_name == $wpdb->base_prefix . "users") {
                                 $tables_segment = Snapshot_Helper_Utility::get_table_segments($table_name, intval($this->config_data['config']['segmentSize']), 'WHERE ID IN (' . implode(',', $this->_session->data['global_user_ids']) . ');');
                             } else {
                                 if ($table_name == $wpdb->base_prefix . "usermeta") {
                                     $tables_segment = Snapshot_Helper_Utility::get_table_segments($table_name, intval($this->config_data['config']['segmentSize']), 'WHERE user_id IN (' . implode(',', $this->_session->data['global_user_ids']) . ');');
                                 }
                             }
                             //echo "tables_segment<pre>"; print_r($tables_segment); echo "</pre>";
                             if ($tables_segment['segments'] && count($tables_segment['segments'])) {
                                 foreach ($tables_segment['segments'] as $segment_idx => $_set) {
                                     $_set['table_name'] = $tables_segment['table_name'];
                                     $_set['rows_total'] = $tables_segment['rows_total'];
                                     $_set['segment_idx'] = intval($segment_idx) + 1;
                                     $_set['segment_total'] = count($tables_segment['segments']);
                                     $error_status['table_data'][] = $_set;
                                 }
                             }
                         }
                     }
                 } else {
                     $tables_segment = Snapshot_Helper_Utility::get_table_segments($table_name, intval($this->config_data['config']['segmentSize']));
                     if ($tables_segment['segments'] && count($tables_segment['segments'])) {
                         foreach ($tables_segment['segments'] as $segment_idx => $_set) {
                             $_set['table_name'] = $tables_segment['table_name'];
                             $_set['rows_total'] = $tables_segment['rows_total'];
                             $_set['segment_idx'] = intval($segment_idx) + 1;
                             $_set['segment_total'] = count($tables_segment['segments']);
                             $error_status['table_data'][] = $_set;
                         }
                     } else {
                         $_set['table_name'] = $tables_segment['table_name'];
                         $_set['rows_total'] = $tables_segment['rows_total'];
                         $_set['segment_idx'] = 1;
                         $_set['segment_total'] = 1;
                         $_set['rows_start'] = 0;
                         $_set['rows_end'] = 0;
                         $error_status['table_data'][] = $_set;
                     }
                 }
             }
         }
         if (isset($tables_sections) && count($tables_sections)) {
             $this->_session->data['tables_sections'] = $tables_sections;
         } else {
             $this->_session->data['tables_sections'] = array();
         }
         if (isset($error_status['table_data'])) {
             $this->_session->data['table_data'] = $error_status['table_data'];
         }
     }
     //echo "table_data<pre>"; print_r($this->_session->data['table_data']); echo "</pre>";
     //die();
     if (!isset($item['destination-sync'])) {
         $item['destination-sync'] = "archive";
     }
     if ($item['destination-sync'] == "archive") {
         //echo "_post_array<pre>"; print_r($_post_array); echo "</pre>";
         //echo "item<pre>"; print_r($item); echo "</pre>";
         $error_status['files_data'] = $this->snapshot_gather_item_files($item);
         //echo "files_data<pre>"; print_r($error_status['files_data']); echo "</pre>";
         //die();
         if (isset($error_status['files_data']['included']) && count($error_status['files_data']['included'])) {
             $files_data = array();
             foreach ($error_status['files_data']['included'] as $_section => $_files) {
                 if (!count($_files)) {
                     continue;
                 }
                 switch ($_section) {
                     /*
                     case 'home':
                     	$_path = $home_path;
                     	if (($_post_array['snapshot-action']) && ($_post_array['snapshot-action'] == "cron"))
                     		$_max_depth=0;
                     	else
                     		$_max_depth=2;
                     	break;
                     */
                     case 'media':
                         $_path = trailingslashit($home_path) . Snapshot_Helper_Utility::get_blog_upload_path($item['blog-id']) . "/";
                         //$_path = snapshot_utility_get_blog_upload_path($item['blog-id']) ."/";
                         if ($_post_array['snapshot-action'] && $_post_array['snapshot-action'] == "cron") {
                             $_max_depth = 0;
                         } else {
                             $_max_depth = 2;
                         }
                         break;
                     case 'plugins':
                         /* case 'mu-plugins': */
                         $_path = trailingslashit($this->plugins_dir);
                         //$_max_depth=0;
                         if ($_post_array['snapshot-action'] && $_post_array['snapshot-action'] == "cron") {
                             $_max_depth = 0;
                         } else {
                             $_max_depth = 0;
                         }
                         break;
                     case 'mu-plugins':
                         $_path = trailingslashit(WP_CONTENT_DIR) . 'mu-plugins/';
                         //$_max_depth=0;
                         if ($_post_array['snapshot-action'] && $_post_array['snapshot-action'] == "cron") {
                             $_max_depth = 0;
                         } else {
                             $_max_depth = 0;
                         }
                         break;
                     case 'themes':
                         $_path = trailingslashit(WP_CONTENT_DIR) . 'themes/';
                         if ($_post_array['snapshot-action'] && $_post_array['snapshot-action'] == "cron") {
                             $_max_depth = 0;
                         } else {
                             $_max_depth = 0;
                         }
                         //$_max_depth=0;
                         break;
                     default:
                         $_path = '';
                         $_max_depth = 0;
                         break;
                 }
                 if ($_max_depth > 0 && !empty($_path)) {
                     foreach ($_files as $_idx => $_file) {
                         $_new_file = str_replace($_path, '', $_file);
                         $_slash_parts = split('/', $_new_file);
                         if (count($_slash_parts) > $_max_depth) {
                             // We first remove the file from this section...
                             unset($error_status['files_data']['included'][$_section][$_idx]);
                             // ... then we add a new section for this group of files.
                             $_new_section = '';
                             foreach ($_slash_parts as $_slash_idx => $slash_part) {
                                 if ($_slash_idx > $_max_depth - 1) {
                                     break;
                                 }
                                 if (strlen($_new_section)) {
                                     $_new_section .= "/";
                                 }
                                 $_new_section .= $slash_part;
                                 unset($_slash_parts[$_slash_idx]);
                             }
                             $_new_file = implode('/', array_values($_slash_parts));
                             if (!isset($error_status['files_data']['included'][$_section . "/" . $_new_section])) {
                                 $error_status['files_data']['included'][$_section . "/" . $_new_section] = array();
                             }
                             $error_status['files_data']['included'][$_section . "/" . $_new_section][] = $_file;
                         }
                     }
                     if (empty($error_status['files_data']['included'][$_section])) {
                         unset($error_status['files_data']['included'][$_section]);
                     }
                 }
             }
             ksort($error_status['files_data']['included']);
             if ($_post_array['snapshot-action'] && $_post_array['snapshot-action'] == "cron") {
                 $all_files = array('all_files' => array());
                 foreach ($error_status['files_data']['included'] as $section => $files) {
                     $all_files['all_files'] = array_merge($all_files['all_files'], $files);
                 }
                 $this->_session->data['files_data']['included'] = $all_files;
             } else {
                 $this->_session->data['files_data']['included'] = $error_status['files_data']['included'];
             }
             $error_status['files_data'] = array_keys($this->_session->data['files_data']['included']);
         }
     } else {
         $this->_session->data['files_data'] = '';
         $error_status['files_data'] = '';
     }
     $this->_session->data['snapshot_time_start'] = time();
     $error_status['errorStatus'] = false;
     $error_status['responseText'] = "Init Start";
     //echo "DEBUG: In: ". __FUNCTION__ ."  Line:". __LINE__ ."<br />";
     //echo "error_status<pre>"; print_r($error_status); echo "</pre>";
     //echo "_session<pre>"; print_r($this->_session->data); echo "</pre>";
     //die();
     return $error_status;
 }