Exemplo n.º 1
0
 /**
  * This 'finish' process is called from JS when all selected tables have been archived. This process
  * renames the session backup file to the final location and writes an activity log record.
  *
  * @since 1.0.0
  * @see
  *
  * @param none
  *
  * @return none
  */
 function snapshot_ajax_backup_finish($item, $_post_array)
 {
     global $wpdb;
     //echo "item<pre>"; print_r($item); echo "</pre>";
     //echo "_post_array<pre>"; print_r($_post_array); echo "</pre>";
     //die();
     $error_status = array();
     $error_status['errorStatus'] = false;
     $error_status['errorText'] = "";
     $error_status['responseText'] = "";
     $manifest_array = array();
     $home_path = apply_filters('snapshot_home_path', get_home_path());
     if (isset($this->_session->data['backupItemFolder'])) {
         $item_key = $item['timestamp'];
         if (isset($_post_array['snapshot-data-item'])) {
             $data_item_key = intval($_post_array['snapshot-data-item']);
         } else {
             $data_item_key = time();
         }
         $data_item = array();
         $data_item['timestamp'] = $data_item_key;
         if (isset($item['tables-option'])) {
             $data_item['tables-option'] = $item['tables-option'];
         }
         if (isset($this->_session->data['tables_sections'])) {
             $data_item['tables-sections'] = $this->_session->data['tables_sections'];
             $item['tables-sections'] = $this->_session->data['tables_sections'];
         }
         if (isset($item['files-option'])) {
             $data_item['files-option'] = $item['files-option'];
         }
         if ($data_item['files-option'] == "all") {
             if (is_main_site($item['blog-id'])) {
                 $data_item['files-sections'] = array('themes', 'plugins', 'media');
                 if (is_multisite()) {
                     $data_item['files-sections'][] = 'mu-plugins';
                 }
             } else {
                 $data_item['files-sections'] = array('media');
             }
         } else {
             if ($data_item['files-option'] == "selected") {
                 if (is_main_site($item['blog-id'])) {
                     if (isset($item['files-sections'])) {
                         $data_item['files-sections'] = $item['files-sections'];
                     }
                 } else {
                     $data_item['files-sections'] = '';
                 }
             }
         }
         if (isset($this->_session->data['files_data']['included'])) {
             $session_files_data = array();
             foreach ($this->_session->data['files_data']['included'] as $files_section => $files_set) {
                 if ($files_section == "plugins") {
                     if (!function_exists('get_plugins')) {
                         require_once ABSPATH . 'wp-admin/includes/plugin.php';
                     }
                     $manifest_array['FILES-DATA-THEMES-PLUGINS'] = get_plugins();
                 } else {
                     if ($files_section == "themes") {
                         $themes = wp_get_themes();
                         $manifest_array['FILES-DATA-THEMES'] = get_plugins();
                     } else {
                         // Nothing
                     }
                 }
                 $session_files_data = array_merge($session_files_data, $files_set);
             }
             $item['files-count'] = count($session_files_data);
             $data_item['files-count'] = count($session_files_data);
         } else {
             $item['files-count'] = 0;
             $data_item['files-count'] = 0;
         }
         // If the master item destination is not empty, means we are connected to some external system (FTP, S3, Dropbox)
         if (empty($item['destination']) || $item['destination'] == "local") {
             // Else if the master item destination is empty..
             $data_item['destination'] = 'local';
             // We assume the local archive folder for this item is set to something non-standard.
             if (isset($item['destination-directory']) && strlen($item['destination-directory'])) {
                 $data_item['destination-directory'] = $item['destination-directory'];
             } else {
                 $data_item['destination-directory'] = '';
             }
         } else {
             // In that case we don't want to set the destination and path until the file has been transmitted.
             $data_item['destination'] = '';
             $data_item['destination-directory'] = '';
         }
         if (isset($item['destination-sync'])) {
             $data_item['destination-sync'] = $item['destination-sync'];
         }
         if (isset($this->_session->data['snapshot_time_start'])) {
             $data_item['time-start'] = $this->_session->data['snapshot_time_start'];
             $data_item['time-end'] = time();
             unset($this->_session->data['snapshot_time_start']);
         }
         $manifest_array['SNAPSHOT_VERSION'] = $this->_settings['SNAPSHOT_VERSION'];
         if (!isset($item['blog-id']) && empty($item['blog-id']) && isset($_post_array['snapshot-blog-id'])) {
             $item['blog-id'] = intval($_post_array['snapshot-blog-id']);
         }
         $manifest_array['WP_BLOG_ID'] = $item['blog-id'];
         if (is_multisite()) {
             $manifest_array['WP_MULTISITE'] = 1;
             if (is_main_site(intval($item['blog-id']))) {
                 $manifest_array['WP_MULTISITE_MAIN_SITE'] = 1;
             }
             $manifest_array['WP_HOME'] = get_blog_option(intval($item['blog-id']), 'home');
             $manifest_array['WP_SITEURL'] = get_blog_option(intval($item['blog-id']), 'siteurl');
             $blog_details = get_blog_details(intval($item['blog-id']));
             if (isset($blog_details->blogname)) {
                 $manifest_array['WP_BLOG_NAME'] = $blog_details->blogname;
             }
             if (isset($blog_details->domain)) {
                 $manifest_array['WP_BLOG_DOMAIN'] = $blog_details->domain;
             }
             if (isset($blog_details->path)) {
                 $manifest_array['WP_BLOG_PATH'] = $blog_details->path;
             }
             if (defined('UPLOADBLOGSDIR')) {
                 $manifest_array['WP_UPLOADBLOGSDIR'] = UPLOADBLOGSDIR;
             }
             // We can't use the 'UPLOADS' defined because it is set via the live site and does ot changes when using switch blog
             //if ( defined( 'UPLOADS' ) ) {
             //	$manifest_array['WP_UPLOADS'] = UPLOADS;
             //}
         } else {
             $manifest_array['MULTISITE'] = 0;
             $manifest_array['WP_HOME'] = get_option('home');
             $manifest_array['WP_BLOG_NAME'] = get_option('blogname');
             $home_url_parts = parse_url($manifest_array['WP_HOME']);
             if (isset($home_url_parts['host'])) {
                 $manifest_array['WP_BLOG_DOMAIN'] = $home_url_parts['host'];
             }
             if (isset($home_url_parts['path'])) {
                 $manifest_array['WP_BLOG_PATH'] = $home_url_parts['path'];
             }
             $manifest_array['WP_SITEURL'] = get_option('siteurl');
         }
         global $wp_version, $wp_db_version;
         $manifest_array['WP_VERSION'] = $wp_version;
         $manifest_array['WP_DB_VERSION'] = $wp_db_version;
         $manifest_array['WP_DB_NAME'] = Snapshot_Helper_Utility::get_db_name();
         $manifest_array['WP_DB_BASE_PREFIX'] = $wpdb->base_prefix;
         $manifest_array['WP_DB_PREFIX'] = $wpdb->get_blog_prefix(intval($item['blog-id']));
         $manifest_array['WP_UPLOAD_PATH'] = Snapshot_Helper_Utility::get_blog_upload_path(intval($item['blog-id']), 'basedir');
         $manifest_array['WP_UPLOAD_URLS'] = Snapshot_Helper_Utility::get_blog_upload_path(intval($item['blog-id']), 'baseurl');
         //if (is_multisite()) && (!is_main_site()) {
         //$manifest_array['WP_UPLOAD_URL_UNFILTERED'] = snapshot_utility_get_blog_upload_path(intval($item['blog-id']), 'baseurl', false);
         //}
         $manifest_array['SEGMENT_SIZE'] = intval($this->config_data['config']['segmentSize']);
         $item_tmp = $item;
         if (isset($item_tmp['data'])) {
             unset($item_tmp['data']);
         }
         $item_tmp['data'] = array();
         $item_tmp['data'][$data_item_key] = $data_item;
         $manifest_array['ITEM'] = $item_tmp;
         if (isset($this->_session->data['tables_sections'])) {
             //fwrite($fp, "TABLES:". serialize($this->_session->data['tables_sections']) ."\r\n");
             $manifest_array['TABLES'] = $this->_session->data['tables_sections'];
         }
         if (isset($this->_session->data['table_data'])) {
             //fwrite($fp, "TABLES-DATA:". serialize($this->_session->data['table_data']) ."\r\n");
             $manifest_array['TABLES-DATA'] = $this->_session->data['table_data'];
         }
         if (isset($session_files_data)) {
             // We want to remove the ABSPATH from the stored file items.
             foreach ($session_files_data as $file_item_idx => $file_item) {
                 $session_files_data[$file_item_idx] = str_replace($home_path, '', $file_item);
             }
             //fwrite($fp, "FILES-DATA:". serialize($this->_session->data['files_data']) ."\r\n");
             //$manifest_array['FILES-DATA'] = $session_files_data;
         }
         // Let's actually create the zip file from the files_array. We strip off the leading path (3rd param)
         $backupZipFile = trailingslashit($this->_session->data['backupItemFolder']) . 'snapshot-backup.zip';
         //if (file_exists($backupZipFile)) {
         /* Create a zip manifest file */
         $manifestFile = trailingslashit($this->_session->data['backupItemFolder']) . 'snapshot_manifest.txt';
         if (Snapshot_Helper_Utility::create_archive_manifest($manifest_array, $manifestFile)) {
             $archiveFiles = array();
             $archiveFiles[] = $manifestFile;
             // Let's actually create the zip file from the files_array. We strip off the leading path (3rd param)
             $backupZipFile = trailingslashit($this->_session->data['backupItemFolder']) . 'snapshot-backup.zip';
             if ($this->config_data['config']['zipLibrary'] == "PclZip") {
                 $zipArchive = new PclZip($backupZipFile);
                 $zipArchive->add($archiveFiles, PCLZIP_OPT_REMOVE_PATH, $this->_session->data['backupItemFolder'], PCLZIP_OPT_TEMP_FILE_THRESHOLD, 10, PCLZIP_OPT_ADD_TEMP_FILE_ON);
                 unset($zipArchive);
             } else {
                 if ($this->config_data['config']['zipLibrary'] == "ZipArchive") {
                     $zipArchive = new ZipArchive();
                     if ($zipArchive) {
                         if (!file_exists($backupZipFile)) {
                             $zip_flags = ZIPARCHIVE::CREATE;
                         } else {
                             $zip_flags = null;
                         }
                         $zip_hdl = $zipArchive->open($backupZipFile, $zip_flags);
                         if ($zip_hdl !== true) {
                             $error_status['errorStatus'] = true;
                             $error_status['errorText'] = "ERROR: ZipArchive file:" . baename($manifestFile) . " : add failed: " . ZipArchiveStatusString($zip_hdl);
                             return $error_status;
                         }
                         foreach ($archiveFiles as $file) {
                             $file = str_replace('\\', '/', $file);
                             $zipArchive->addFile($file, str_replace($this->_session->data['backupItemFolder'] . '/', '', $file));
                         }
                         $zipArchive->close();
                     }
                 }
             }
             foreach ($archiveFiles as $archiveFile) {
                 @unlink($archiveFile);
             }
         }
         $checksum = Snapshot_Helper_Utility::get_file_checksum($backupZipFile);
         //$date_key = date('ymd-His', $item_key); // This timestamp format is used for the filename on disk.
         $date_key = date('ymd-His', $data_item_key);
         // This timestamp format is used for the filename on disk.
         $backupZipFilename = 'snapshot-' . $item_key . '-' . $date_key . '-' . $checksum . '.zip';
         $data_item['filename'] = $backupZipFilename;
         $data_item['file_size'] = filesize($backupZipFile);
         //$backupZipFolder = $this->snapshot_get_item_destination_path($item, $data_item, true);
         if (empty($data_item['destination']) || $data_item['destination'] == "local") {
             $backupZipFolder = $this->snapshot_get_item_destination_path($item, $data_item, true);
             $this->snapshot_logger->log_message('backupZipFolder[' . $backupZipFolder . ']');
             if (empty($backupZipFolder)) {
                 $backupZipFolder = $this->_settings['backupBaseFolderFull'];
             }
         } else {
             $backupZipFolder = $this->_settings['backupBaseFolderFull'];
         }
         $backupZipFileFinal = trailingslashit($backupZipFolder) . $backupZipFilename;
         if (file_exists($backupZipFileFinal)) {
             @unlink($backupZipFileFinal);
         }
         $this->snapshot_logger->log_message('rename: backupZipFile[' . $backupZipFile . '] backupZipFileFinal[' . $backupZipFileFinal . ']');
         // Remove the destination file if it exists. If should not but just in case.
         if (file_exists($backupZipFileFinal)) {
             @unlink($backupZipFileFinal);
         }
         //$rename_ret = @rename($backupZipFile, $backupZipFileFinal);
         $rename_ret = rename($backupZipFile, $backupZipFileFinal);
         if ($rename_ret === false) {
             //$this->snapshot_logger->log_message('rename: failed: error:'. print_r(error_get_last(), true) .'');
             // IF for some reason the destination path is not our default snapshot backups folder AND we could not not rename to that
             // alternate path. We then try the default snapshot destination.
             if (trailingslashit($this->_settings['backupBaseFolderFull']) != trailingslashit(dirname($backupZipFileFinal))) {
                 $backupZipFileTMP = trailingslashit($this->_settings['backupBaseFolderFull']) . basename($backupZipFileFinal);
                 $this->snapshot_logger->log_message('rename: backupZipFile[' . $backupZipFile . '] backupZipFileFinal[' . $backupZipFileTMP . ']');
                 $rename_ret = rename($backupZipFile, $backupZipFileTMP);
                 if ($rename_ret !== false) {
                     $this->snapshot_logger->log_message('rename: success');
                     $error_status['responseFile'] = basename($backupZipFileFinal);
                     $data_item['destination-directory'] = '';
                 }
             }
         } else {
             $error_status['responseFile'] = basename($backupZipFileFinal);
         }
         $error_status['responseFile'] = basename($backupZipFileFinal);
         // echo out the finished message so the user knows we are done.
         $error_status['responseText'] = __("SUCCESS: Created Snapshot: ", SNAPSHOT_I18N_DOMAIN) . basename($backupZipFileFinal) . "<br />" . '<a href="' . $this->_settings['SNAPSHOT_MENU_URL'] . 'snapshots_new_panel">' . __("Add Another Snapshot") . '</a>';
         //}
         if (!isset($item['data'])) {
             $item['data'] = array();
         }
         // Add the file entry to the data section of out snapshot item
         $item['data'][$data_item_key] = $data_item;
         ksort($item['data']);
         $this->config_data['items'][$item_key] = $item;
         if (isset($this->_session->data['tables_sections']) && isset($this->_session->data['table_data']) && count($this->_session->data['table_data'])) {
             if (!isset($this->config_data['config']['tables_last'][$item['blog-id']])) {
                 $this->config_data['config']['tables_last'][$item['blog-id']] = array();
             }
             $this->config_data['config']['tables_last'][$item['blog-id']] = $this->_session->data['tables_sections'];
         }
         //unset($this->_session->data);
         return $error_status;
     }
 }