Пример #1
0
 /**
  * Check Jobs worked and Cleanup logs and so on
  */
 public static function check_cleanup()
 {
     $job_object = BackWPup_Job::get_working_data();
     $log_folder = get_site_option('backwpup_cfg_logfolder');
     $log_folder = BackWPup_File::get_absolute_path($log_folder);
     // check aborted jobs for longer than a tow hours, abort them courtly and send mail
     if (is_object($job_object) && !empty($job_object->logfile)) {
         $not_worked_time = microtime(TRUE) - $job_object->timestamp_last_update;
         if ($not_worked_time > 3600) {
             $job_object->log(E_USER_ERROR, __('Aborted, because no progress for one hour!', 'backwpup'), __FILE__, __LINE__);
             unlink(BackWPup::get_plugin_data('running_file'));
             $job_object->update_working_data();
         }
     }
     //Compress not compressed logs
     if (is_readable($log_folder) && function_exists('gzopen') && get_site_option('backwpup_cfg_gzlogs') && !is_object($job_object)) {
         //Compress old not compressed logs
         if ($dir = opendir($log_folder)) {
             $jobids = BackWPup_Option::get_job_ids();
             while (FALSE !== ($file = readdir($dir))) {
                 if (is_writeable($log_folder . $file) && '.html' == substr($file, -5)) {
                     $compress = new BackWPup_Create_Archive($log_folder . $file . '.gz');
                     if ($compress->add_file($log_folder . $file)) {
                         unlink($log_folder . $file);
                         //change last logfile in jobs
                         foreach ($jobids as $jobid) {
                             $job_logfile = BackWPup_Option::get($jobid, 'logfile');
                             if (!empty($job_logfile) && $job_logfile === $log_folder . $file) {
                                 BackWPup_Option::update($jobid, 'logfile', $log_folder . $file . '.gz');
                             }
                         }
                     }
                     unset($compress);
                 }
             }
             closedir($dir);
         }
     }
     //Jobs cleanings
     if (!is_object($job_object)) {
         //remove restart cron
         wp_clear_scheduled_hook('backwpup_cron', array('id' => 'restart'));
         //temp cleanup
         BackWPup_Job::clean_temp_folder();
     }
     //check scheduling jobs that not found will removed because there are single scheduled
     $activejobs = BackWPup_Option::get_job_ids('activetype', 'wpcron');
     if (!empty($activejobs)) {
         foreach ($activejobs as $jobid) {
             $cron_next = wp_next_scheduled('backwpup_cron', array('id' => $jobid));
             if (!$cron_next || $cron_next < time()) {
                 wp_unschedule_event($cron_next, 'backwpup_cron', array('id' => $jobid));
                 $cron_next = BackWPup_Cron::cron_next(BackWPup_Option::get($jobid, 'cron'));
                 wp_schedule_single_event($cron_next, 'backwpup_cron', array('id' => $jobid));
             }
         }
     }
 }
Пример #2
0
 /**
  * Creates the backup archive
  */
 private function create_archive()
 {
     //load folders to backup
     $folders_to_backup = $this->get_folders_to_backup();
     $this->substeps_todo = $this->count_folder + 1;
     //initial settings for restarts in archiving
     if (!isset($this->steps_data[$this->step_working]['on_file'])) {
         $this->steps_data[$this->step_working]['on_file'] = '';
     }
     if (!isset($this->steps_data[$this->step_working]['on_folder'])) {
         $this->steps_data[$this->step_working]['on_folder'] = '';
     }
     if ($this->steps_data[$this->step_working]['on_folder'] == '' && $this->steps_data[$this->step_working]['on_file'] == '' && is_file($this->backup_folder . $this->backup_file)) {
         unlink($this->backup_folder . $this->backup_file);
     }
     if ($this->steps_data[$this->step_working]['SAVE_STEP_TRY'] != $this->steps_data[$this->step_working]['STEP_TRY']) {
         $this->log(sprintf(__('%d. Trying to create backup archive &hellip;', 'backwpup'), $this->steps_data[$this->step_working]['STEP_TRY']), E_USER_NOTICE);
     }
     try {
         $backup_archive = new BackWPup_Create_Archive($this->backup_folder . $this->backup_file);
         //show method for creation
         if ($this->substeps_done == 0) {
             $this->log(sprintf(_x('Compressing files as %s. Please be patient, this may take a moment.', 'Archive compression method', 'backwpup'), $backup_archive->get_method()));
         }
         //add extra files
         if ($this->substeps_done == 0) {
             if (!empty($this->additional_files_to_backup) && $this->substeps_done == 0) {
                 if ($this->is_debug()) {
                     $this->log(__('Adding Extra files to Archive', 'backwpup'));
                 }
                 foreach ($this->additional_files_to_backup as $file) {
                     if ($backup_archive->add_file($file, basename($file))) {
                         $this->count_files++;
                         $this->count_files_size = $this->count_files_size + filesize($file);
                         $this->update_working_data();
                     } else {
                         $backup_archive->close();
                         $this->steps_data[$this->step_working]['on_file'] = '';
                         $this->steps_data[$this->step_working]['on_folder'] = '';
                         $this->log(__('Cannot create backup archive correctly. Aborting creation.', 'backwpup'), E_USER_ERROR);
                         return false;
                     }
                 }
             }
             $this->substeps_done++;
         }
         //add normal files
         while ($folder = array_shift($folders_to_backup)) {
             //jump over already done folders
             if (in_array($this->steps_data[$this->step_working]['on_folder'], $folders_to_backup, true)) {
                 continue;
             }
             if ($this->is_debug()) {
                 $this->log(sprintf(__('Archiving Folder: %s', 'backwpup'), $folder));
             }
             $this->steps_data[$this->step_working]['on_folder'] = $folder;
             $files_in_folder = $this->get_files_in_folder($folder);
             //add empty folders
             if (empty($files_in_folder)) {
                 $folder_name_in_archive = trim(ltrim($this->get_destination_path_replacement($folder), '/'));
                 if (!empty($folder_name_in_archive)) {
                     $backup_archive->add_empty_folder($folder, $folder_name_in_archive);
                 }
                 continue;
             }
             //add files
             while ($file = array_shift($files_in_folder)) {
                 //jump over already done files
                 if (in_array($this->steps_data[$this->step_working]['on_file'], $files_in_folder, true)) {
                     continue;
                 }
                 $this->steps_data[$this->step_working]['on_file'] = $file;
                 //restart if needed
                 $restart_time = $this->get_restart_time();
                 if ($restart_time <= 0) {
                     unset($backup_archive);
                     $this->do_restart_time(true);
                     return false;
                 }
                 //generate filename in archive
                 $in_archive_filename = ltrim($this->get_destination_path_replacement($file), '/');
                 //add file to archive
                 if ($backup_archive->add_file($file, $in_archive_filename)) {
                     $this->count_files++;
                     $this->count_files_size = $this->count_files_size + filesize($file);
                     $this->update_working_data();
                 } else {
                     $backup_archive->close();
                     unset($backup_archive);
                     $this->steps_data[$this->step_working]['on_file'] = '';
                     $this->steps_data[$this->step_working]['on_folder'] = '';
                     $this->substeps_done = 0;
                     $this->backup_filesize = filesize($this->backup_folder . $this->backup_file);
                     if ($this->backup_filesize === false) {
                         $this->backup_filesize = PHP_INT_MAX;
                     }
                     $this->log(__('Cannot create backup archive correctly. Aborting creation.', 'backwpup'), E_USER_ERROR);
                     return false;
                 }
             }
             $this->steps_data[$this->step_working]['on_file'] = '';
             $this->substeps_done++;
         }
         $backup_archive->close();
         unset($backup_archive);
         $this->log(__('Backup archive created.', 'backwpup'), E_USER_NOTICE);
     } catch (Exception $e) {
         $this->log($e->getMessage(), E_USER_ERROR, $e->getFile(), $e->getLine());
         unset($backup_archive);
         return false;
     }
     $this->backup_filesize = filesize($this->backup_folder . $this->backup_file);
     if ($this->backup_filesize === false) {
         $this->backup_filesize = PHP_INT_MAX;
     }
     if ($this->backup_filesize >= PHP_INT_MAX) {
         $this->log(__('The Backup archive will be too large for file operations with this PHP Version. You might want to consider splitting the backup job in multiple jobs with less files each.', 'backwpup'), E_USER_ERROR);
         $this->end();
     } else {
         $this->log(sprintf(__('Archive size is %s.', 'backwpup'), size_format($this->backup_filesize, 2)), E_USER_NOTICE);
     }
     $this->log(sprintf(__('%1$d Files with %2$s in Archive.', 'backwpup'), $this->count_files, size_format($this->count_files_size, 2)), E_USER_NOTICE);
     return true;
 }
Пример #3
0
 /**
  * @param $job_object
  * @return bool
  */
 public function job_run(BackWPup_Job $job_object)
 {
     global $wpdb, $post, $wp_query;
     $wxr_version = '1.2';
     if ($job_object->steps_data[$job_object->step_working]['SAVE_STEP_TRY'] != $job_object->steps_data[$job_object->step_working]['STEP_TRY']) {
         $job_object->log(sprintf(__('%d. Trying to create a WordPress export to XML file&#160;&hellip;', 'backwpup'), $job_object->steps_data[$job_object->step_working]['STEP_TRY']));
         $job_object->steps_data[$job_object->step_working]['wpexportfile'] = BackWPup::get_plugin_data('TEMP') . $job_object->generate_filename($job_object->job['wpexportfile'], 'xml', TRUE);
         $job_object->steps_data[$job_object->step_working]['substep'] = 'header';
         $job_object->steps_data[$job_object->step_working]['post_ids'] = array();
         $job_object->substeps_todo = 10;
         $job_object->substeps_done = 0;
     }
     add_filter('wxr_export_skip_postmeta', array($this, 'wxr_filter_postmeta'), 10, 2);
     if ($job_object->steps_data[$job_object->step_working]['substep'] == 'header') {
         if ('all' != $job_object->job['wpexportcontent'] && post_type_exists($job_object->job['wpexportcontent'])) {
             $ptype = get_post_type_object($job_object->job['wpexportcontent']);
             if (!$ptype->can_export) {
                 $job_object->log(sprintf(__('WP Export: Post type “%s” does not allow export.', 'backwpup'), $job_object->job['wpexportcontent']), E_USER_ERROR);
                 return FALSE;
             }
             $where = $wpdb->prepare("{$wpdb->posts}.post_type = %s", $job_object->job['wpexportcontent']);
         } else {
             $post_types = get_post_types(array('can_export' => true));
             $esses = array_fill(0, count($post_types), '%s');
             $where = $wpdb->prepare("{$wpdb->posts}.post_type IN (" . implode(',', $esses) . ')', $post_types);
             $job_object->job['wpexportcontent'] = 'all';
         }
         $where .= " AND {$wpdb->posts}.post_status != 'auto-draft'";
         // grab a snapshot of post IDs, just in case it changes during the export
         $job_object->steps_data[$job_object->step_working]['post_ids'] = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE {$where}");
         $job_object->substeps_todo = $job_object->substeps_todo + count($job_object->steps_data[$job_object->step_working]['post_ids']);
         $header = '<?xml version="1.0" encoding="' . get_bloginfo('charset') . "\" ?>\n";
         $header .= "<!-- This is a WordPress eXtended RSS file generated by the WordPress plugin BackWPup as an export of your site. -->\n";
         $header .= "<!-- It contains information about your site's posts, pages, comments, categories, and other content. -->\n";
         $header .= "<!-- You may use this file to transfer that content from one site to another. -->\n";
         $header .= "<!-- This file is not intended to serve as a complete backup of your site. -->\n\n";
         $header .= "<!-- To import this information into a WordPress site follow these steps: -->\n";
         $header .= "<!-- 1. Log in to that site as an administrator. -->\n";
         $header .= "<!-- 2. Go to Tools: Import in the WordPress admin panel. -->\n";
         $header .= "<!-- 3. Install the \"WordPress\" importer from the list. -->\n";
         $header .= "<!-- 4. Activate & Run Importer. -->\n";
         $header .= "<!-- 5. Upload this file using the form provided on that page. -->\n";
         $header .= "<!-- 6. You will first be asked to map the authors in this export file to users -->\n";
         $header .= "<!--    on the site. For each author, you may choose to map to an -->\n";
         $header .= "<!--    existing user on the site or to create a new user. -->\n";
         $header .= "<!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->\n";
         $header .= "<!--    contained in this file into your site. -->\n\n";
         $header .= "<!-- generator=\"WordPress/" . get_bloginfo_rss('version') . "\" created=\"" . date('Y-m-d H:i') . "\" -->\n";
         $header .= "<rss version=\"2.0\" xmlns:excerpt=\"http://wordpress.org/export/{$wxr_version}/excerpt/\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:wp=\"http://wordpress.org/export/{$wxr_version}/\">\n";
         $header .= "<channel>\n";
         $header .= "\t<title>" . get_bloginfo_rss('name') . "</title>\n";
         $header .= "\t<link>" . get_bloginfo_rss('url') . "</link>\n";
         $header .= "\t<description>" . get_bloginfo_rss('description') . "</description>\n";
         $header .= "\t<pubDate>" . date('D, d M Y H:i:s +0000') . "</pubDate>\n";
         $header .= "\t<language>" . get_bloginfo_rss('language') . "</language>\n";
         $header .= "\t<wp:wxr_version>" . $wxr_version . "</wp:wxr_version>\n";
         $header .= "\t<wp:base_site_url>" . $this->wxr_site_url() . "</wp:base_site_url>\n";
         $header .= "\t<wp:base_blog_url>" . get_bloginfo_rss('url') . "</wp:base_blog_url>\n";
         $written = file_put_contents($job_object->steps_data[$job_object->step_working]['wpexportfile'], $header, FILE_APPEND);
         if ($written === FALSE) {
             $job_object->log(__('WP Export file could not written.', 'backwpup'), E_USER_ERROR);
             return FALSE;
         }
         unset($header);
         $job_object->steps_data[$job_object->step_working]['substep'] = 'authors';
         $job_object->substeps_done++;
         $job_object->update_working_data();
         $job_object->do_restart_time();
     }
     if ($job_object->steps_data[$job_object->step_working]['substep'] == 'authors') {
         $written = file_put_contents($job_object->steps_data[$job_object->step_working]['wpexportfile'], $this->wxr_authors_list(), FILE_APPEND);
         if ($written === FALSE) {
             $job_object->log(__('WP Export file could not written.', 'backwpup'), E_USER_ERROR);
             return FALSE;
         }
         $job_object->steps_data[$job_object->step_working]['substep'] = 'cats';
         $job_object->substeps_done++;
         $job_object->update_working_data();
         $job_object->do_restart_time();
     }
     if ($job_object->steps_data[$job_object->step_working]['substep'] == 'cats') {
         if ('all' == $job_object->job['wpexportcontent']) {
             $cats = array();
             $categories = (array) get_categories(array('get' => 'all'));
             // put categories in order with no child going before its parent
             while ($cat = array_shift($categories)) {
                 if ($cat->parent == 0 || isset($cats[$cat->parent])) {
                     $cats[$cat->term_id] = $cat;
                 } else {
                     $categories[] = $cat;
                 }
             }
             $cats_xml = '';
             foreach ($cats as $c) {
                 $parent_slug = $c->parent ? $cats[$c->parent]->slug : '';
                 $cats_xml .= "\t<wp:category><wp:term_id>" . $c->term_id . "</wp:term_id><wp:category_nicename>" . $c->slug . "</wp:category_nicename><wp:category_parent>" . $parent_slug . "</wp:category_parent>" . $this->wxr_cat_name($c) . $this->wxr_category_description($c) . "</wp:category>\n";
             }
             $written = file_put_contents($job_object->steps_data[$job_object->step_working]['wpexportfile'], $cats_xml, FILE_APPEND);
             if ($written === FALSE) {
                 $job_object->log(__('WP Export file could not written.', 'backwpup'), E_USER_ERROR);
                 return FALSE;
             }
             unset($cats_xml);
         }
         $job_object->steps_data[$job_object->step_working]['substep'] = 'tags';
         $job_object->substeps_done++;
         $job_object->update_working_data();
         $job_object->do_restart_time();
     }
     if ($job_object->steps_data[$job_object->step_working]['substep'] == 'tags') {
         if ('all' == $job_object->job['wpexportcontent']) {
             $tags = (array) get_tags(array('get' => 'all'));
             $tags_xml = '';
             foreach ($tags as $t) {
                 $tags_xml .= "\t<wp:tag><wp:term_id>" . $t->term_id . "</wp:term_id><wp:tag_slug>" . $t->slug . "</wp:tag_slug>" . $this->wxr_tag_name($t) . $this->wxr_tag_description($t) . "</wp:tag>\n";
             }
             $written = file_put_contents($job_object->steps_data[$job_object->step_working]['wpexportfile'], $tags_xml, FILE_APPEND);
             if ($written === FALSE) {
                 $job_object->log(__('WP Export file could not written.', 'backwpup'), E_USER_ERROR);
                 return FALSE;
             }
             unset($tags_xml);
         }
         $job_object->steps_data[$job_object->step_working]['substep'] = 'terms';
         $job_object->substeps_done++;
         $job_object->update_working_data();
         $job_object->do_restart_time();
     }
     if ($job_object->steps_data[$job_object->step_working]['substep'] == 'terms') {
         if ('all' == $job_object->job['wpexportcontent']) {
             $terms = array();
             $custom_taxonomies = get_taxonomies(array('_builtin' => false));
             $custom_terms = (array) get_terms($custom_taxonomies, array('get' => 'all'));
             // put terms in order with no child going before its parent
             while ($t = array_shift($custom_terms)) {
                 if ($t->parent == 0 || isset($terms[$t->parent])) {
                     $terms[$t->term_id] = $t;
                 } else {
                     $custom_terms[] = $t;
                 }
             }
             $terms_xml = '';
             foreach ($terms as $t) {
                 $parent_slug = $t->parent ? $terms[$t->parent]->slug : '';
                 $terms_xml .= "\t<wp:term><wp:term_id>" . $t->term_id . "</wp:term_id><wp:term_taxonomy>" . $t->taxonomy . "</wp:term_taxonomy><wp:term_slug>" . $t->slug . "</wp:term_slug><wp:term_parent>" . $parent_slug . "</wp:term_parent>" . $this->wxr_term_name($t) . $this->wxr_term_description($t) . "</wp:term>\n";
             }
             $written = file_put_contents($job_object->steps_data[$job_object->step_working]['wpexportfile'], $terms_xml, FILE_APPEND);
             if ($written === FALSE) {
                 $job_object->log(__('WP Export file could not written.', 'backwpup'), E_USER_ERROR);
                 return FALSE;
             }
             unset($terms_xml);
         }
         $job_object->steps_data[$job_object->step_working]['substep'] = 'menus';
         $job_object->substeps_done++;
         $job_object->update_working_data();
         $job_object->do_restart_time();
     }
     if ($job_object->steps_data[$job_object->step_working]['substep'] == 'menus') {
         $menu_xml = '';
         if ('all' == $job_object->job['wpexportcontent']) {
             $menu_xml .= $this->wxr_nav_menu_terms();
         }
         $menu_xml .= "\t<generator>http://wordpress.org/?v=" . get_bloginfo_rss('version') . "</generator>\n";
         $written = file_put_contents($job_object->steps_data[$job_object->step_working]['wpexportfile'], $menu_xml, FILE_APPEND);
         if ($written === FALSE) {
             $job_object->log(__('WP Export file could not written.', 'backwpup'), E_USER_ERROR);
             return FALSE;
         }
         unset($menu_xml);
         $job_object->steps_data[$job_object->step_working]['substep'] = 'posts';
         $job_object->substeps_done++;
         $job_object->update_working_data();
         $job_object->do_restart_time();
     }
     if ($job_object->steps_data[$job_object->step_working]['substep'] == 'posts') {
         if (!empty($job_object->steps_data[$job_object->step_working]['post_ids'])) {
             $wp_query->in_the_loop = true;
             // Fake being in the loop.
             // fetch 20 posts at a time rather than loading the entire table into memory
             while ($next_posts = array_splice($job_object->steps_data[$job_object->step_working]['post_ids'], 0, 20)) {
                 $where = 'WHERE ID IN (' . join(',', $next_posts) . ')';
                 $posts = $wpdb->get_results("SELECT * FROM {$wpdb->posts} {$where}");
                 $wxr_post = '';
                 // Begin Loop
                 foreach ($posts as $post) {
                     /* @var WP_Post $post */
                     $is_sticky = is_sticky($post->ID) ? 1 : 0;
                     $wxr_post .= "\t<item>\n";
                     $wxr_post .= "\t\t<title>" . apply_filters('the_title_rss', $post->post_title) . "</title>\n";
                     $wxr_post .= "\t\t<link>" . esc_url(apply_filters('the_permalink_rss', get_permalink($post))) . "</link>\n";
                     $wxr_post .= "\t\t<pubDate>" . mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true, $post), false) . "</pubDate>\n";
                     $wxr_post .= "\t\t<dc:creator>" . $this->wxr_cdata(get_the_author_meta('login', $post->post_author)) . "</dc:creator>\n";
                     $wxr_post .= "\t\t<guid isPermaLink=\"false\">" . esc_url(get_the_guid($post->ID)) . "</guid>\n";
                     $wxr_post .= "\t\t<description></description>\n";
                     $wxr_post .= "\t\t<content:encoded>" . $this->wxr_cdata(apply_filters('the_content_export', $post->post_content)) . "</content:encoded>\n";
                     $wxr_post .= "\t\t<excerpt:encoded>" . $this->wxr_cdata(apply_filters('the_excerpt_export', $post->post_excerpt)) . "</excerpt:encoded>\n";
                     $wxr_post .= "\t\t<wp:post_id>" . $post->ID . "</wp:post_id>\n";
                     $wxr_post .= "\t\t<wp:post_date>" . $post->post_date . "</wp:post_date>\n";
                     $wxr_post .= "\t\t<wp:post_date_gmt>" . $post->post_date_gmt . "</wp:post_date_gmt>\n";
                     $wxr_post .= "\t\t<wp:comment_status>" . $post->comment_status . "</wp:comment_status>\n";
                     $wxr_post .= "\t\t<wp:ping_status>" . $post->ping_status . "</wp:ping_status>\n";
                     $wxr_post .= "\t\t<wp:post_name>" . $post->post_name . "</wp:post_name>\n";
                     $wxr_post .= "\t\t<wp:status>" . $post->post_status . "</wp:status>\n";
                     $wxr_post .= "\t\t<wp:post_parent>" . $post->post_parent . "</wp:post_parent>\n";
                     $wxr_post .= "\t\t<wp:menu_order>" . $post->menu_order . "</wp:menu_order>\n";
                     $wxr_post .= "\t\t<wp:post_type>" . $post->post_type . "</wp:post_type>\n";
                     $wxr_post .= "\t\t<wp:post_password>" . $post->post_password . "</wp:post_password>\n";
                     $wxr_post .= "\t\t<wp:is_sticky>" . $is_sticky . "</wp:is_sticky>\n";
                     if ($post->post_type == 'attachment') {
                         $wxr_post .= "\t\t<wp:attachment_url>" . wp_get_attachment_url($post->ID) . "</wp:attachment_url>\n";
                     }
                     $wxr_post .= $this->wxr_post_taxonomy();
                     $postmeta = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->postmeta} WHERE post_id = %d", $post->ID));
                     foreach ($postmeta as $meta) {
                         if (apply_filters('wxr_export_skip_postmeta', false, $meta->meta_key, $meta)) {
                             continue;
                         }
                         $wxr_post .= "\t\t<wp:postmeta>\n\t\t\t<wp:meta_key>" . $meta->meta_key . "</wp:meta_key>\n\t\t\t<wp:meta_value>" . $this->wxr_cdata($meta->meta_value) . "</wp:meta_value>\n\t\t</wp:postmeta>\n";
                     }
                     $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID));
                     foreach ($comments as $c) {
                         $wxr_post .= "\t\t<wp:comment>\n";
                         $wxr_post .= "\t\t\t<wp:comment_id>" . $c->comment_ID . "</wp:comment_id>\n";
                         $wxr_post .= "\t\t\t<wp:comment_author>" . $this->wxr_cdata($c->comment_author) . "</wp:comment_author>\n";
                         $wxr_post .= "\t\t\t<wp:comment_author_email>" . $c->comment_author_email . "</wp:comment_author_email>\n";
                         $wxr_post .= "\t\t\t<wp:comment_author_url>" . esc_url_raw($c->comment_author_url) . "</wp:comment_author_url>\n";
                         $wxr_post .= "\t\t\t<wp:comment_author_IP>" . $c->comment_author_IP . "</wp:comment_author_IP>\n";
                         $wxr_post .= "\t\t\t<wp:comment_date>" . $c->comment_date . "</wp:comment_date>\n";
                         $wxr_post .= "\t\t\t<wp:comment_date_gmt>" . $c->comment_date_gmt . "</wp:comment_date_gmt>\n";
                         $wxr_post .= "\t\t\t<wp:comment_content>" . $this->wxr_cdata($c->comment_content) . "</wp:comment_content>\n";
                         $wxr_post .= "\t\t\t<wp:comment_approved>" . $c->comment_approved . "</wp:comment_approved>\n";
                         $wxr_post .= "\t\t\t<wp:comment_type>" . $c->comment_type . "</wp:comment_type>\n";
                         $wxr_post .= "\t\t\t<wp:comment_parent>" . $c->comment_parent . "</wp:comment_parent>\n";
                         $wxr_post .= "\t\t\t<wp:comment_user_id>" . $c->user_id . "</wp:comment_user_id>\n";
                         $c_meta = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->commentmeta} WHERE comment_id = %d", $c->comment_ID));
                         foreach ($c_meta as $meta) {
                             $wxr_post .= "\t\t\t<wp:commentmeta>\n\t\t\t\t<wp:meta_key>" . $meta->meta_key . "</wp:meta_key>\n\t\t\t\t<wp:meta_value>" . $this->wxr_cdata($meta->meta_value) . "</wp:meta_value>\n\t\t\t</wp:commentmeta>\n";
                         }
                         $wxr_post .= "\t\t</wp:comment>\n";
                     }
                     $wxr_post .= "\t</item>\n";
                     $job_object->substeps_done++;
                 }
                 $written = file_put_contents($job_object->steps_data[$job_object->step_working]['wpexportfile'], $wxr_post, FILE_APPEND);
                 if ($written === FALSE) {
                     $job_object->log(__('WP Export file could not written.', 'backwpup'), E_USER_ERROR);
                     return FALSE;
                 }
                 $job_object->do_restart_time();
             }
         }
         $written = file_put_contents($job_object->steps_data[$job_object->step_working]['wpexportfile'], "</channel>\n</rss>", FILE_APPEND);
         if ($written === FALSE) {
             $job_object->log(__('WP Export file could not written.', 'backwpup'), E_USER_ERROR);
             return FALSE;
         }
         $job_object->steps_data[$job_object->step_working]['substep'] = 'check';
         $job_object->substeps_done++;
         $job_object->update_working_data();
         $job_object->do_restart_time();
     }
     remove_filter('wxr_export_skip_postmeta', array($this, 'wxr_filter_postmeta'), 10);
     if ($job_object->steps_data[$job_object->step_working]['substep'] == 'check') {
         if (extension_loaded('simplexml') && class_exists('DOMDocument')) {
             $job_object->log(__('Check WP Export file&#160;&hellip;', 'backwpup'));
             $job_object->need_free_memory(filesize($job_object->steps_data[$job_object->step_working]['wpexportfile']) * 2);
             $valid = TRUE;
             $internal_errors = libxml_use_internal_errors(TRUE);
             $dom = new DOMDocument();
             $old_value = NULL;
             if (function_exists('libxml_disable_entity_loader')) {
                 $old_value = libxml_disable_entity_loader(TRUE);
             }
             $success = $dom->loadXML(file_get_contents($job_object->steps_data[$job_object->step_working]['wpexportfile']));
             if (!is_null($old_value)) {
                 libxml_disable_entity_loader($old_value);
             }
             if (!$success || isset($dom->doctype)) {
                 $errors = libxml_get_errors();
                 $valid = FALSE;
                 foreach ($errors as $error) {
                     switch ($error->level) {
                         case LIBXML_ERR_WARNING:
                             $job_object->log(E_USER_WARNING, sprintf(__('XML WARNING (%s): %s', 'backwpup'), $error->code, trim($error->message)), $job_object->steps_data[$job_object->step_working]['wpexportfile'], $error->line);
                             break;
                         case LIBXML_ERR_ERROR:
                             $job_object->log(E_USER_WARNING, sprintf(__('XML RECOVERABLE (%s): %s', 'backwpup'), $error->code, trim($error->message)), $job_object->steps_data[$job_object->step_working]['wpexportfile'], $error->line);
                             break;
                         case LIBXML_ERR_FATAL:
                             $job_object->log(E_USER_WARNING, sprintf(__('XML ERROR (%s): %s', 'backwpup'), $error->code, trim($error->message)), $job_object->steps_data[$job_object->step_working]['wpexportfile'], $error->line);
                             break;
                     }
                 }
             } else {
                 $xml = simplexml_import_dom($dom);
                 unset($dom);
                 // halt if loading produces an error
                 if (!$xml) {
                     $job_object->log(__('There was an error when reading this WXR file', 'backwpup'), E_USER_ERROR);
                     $valid = FALSE;
                 } else {
                     $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version');
                     if (!$wxr_version) {
                         $job_object->log(__('This does not appear to be a WXR file, missing/invalid WXR version number', 'backwpup'), E_USER_ERROR);
                         $valid = FALSE;
                     }
                     $wxr_version = (string) trim($wxr_version[0]);
                     // confirm that we are dealing with the correct file format
                     if (!preg_match('/^\\d+\\.\\d+$/', $wxr_version)) {
                         $job_object->log(__('This does not appear to be a WXR file, missing/invalid WXR version number', 'backwpup'), E_USER_ERROR);
                         $valid = FALSE;
                     }
                 }
             }
             libxml_use_internal_errors($internal_errors);
             if ($valid) {
                 $job_object->log(__('WP Export file is a valid WXR file.', 'backwpup'));
             }
         } else {
             $job_object->log(__('WP Export file can not be checked, because no XML extension is loaded, to ensure the file verification.', 'backwpup'));
         }
         $job_object->steps_data[$job_object->step_working]['substep'] = 'compress';
         $job_object->substeps_done++;
         $job_object->update_working_data();
         $job_object->do_restart_time();
     }
     //Compress file
     if ($job_object->steps_data[$job_object->step_working]['substep'] == 'compress') {
         if (!empty($job_object->job['wpexportfilecompression'])) {
             $job_object->log(__('Compressing file&#160;&hellip;', 'backwpup'));
             try {
                 $compress = new BackWPup_Create_Archive($job_object->steps_data[$job_object->step_working]['wpexportfile'] . $job_object->job['wpexportfilecompression']);
                 if ($compress->add_file($job_object->steps_data[$job_object->step_working]['wpexportfile'])) {
                     unset($compress);
                     unlink($job_object->steps_data[$job_object->step_working]['wpexportfile']);
                     $job_object->steps_data[$job_object->step_working]['wpexportfile'] .= $job_object->job['wpexportfilecompression'];
                     $job_object->log(__('Compressing done.', 'backwpup'));
                 }
             } catch (Exception $e) {
                 $job_object->log($e->getMessage(), E_USER_ERROR, $e->getFile(), $e->getLine());
                 unset($compress);
                 return FALSE;
             }
         }
         $job_object->steps_data[$job_object->step_working]['substep'] = 'addfile';
         $job_object->substeps_done++;
         $job_object->update_working_data();
         $job_object->do_restart_time();
     }
     if ($job_object->steps_data[$job_object->step_working]['substep'] == 'addfile') {
         //add XML file to backup files
         if (is_readable($job_object->steps_data[$job_object->step_working]['wpexportfile'])) {
             $job_object->additional_files_to_backup[] = $job_object->steps_data[$job_object->step_working]['wpexportfile'];
             $filesize = filesize($job_object->steps_data[$job_object->step_working]['wpexportfile']);
             $job_object->log(sprintf(__('Added XML export "%1$s" with %2$s to backup file list.', 'backwpup'), basename($job_object->steps_data[$job_object->step_working]['wpexportfile']), size_format($filesize, 2)));
         }
         $job_object->substeps_done++;
         $job_object->update_working_data();
     }
     return TRUE;
 }
Пример #4
0
 /**
  * @param $job_object
  * @return bool
  */
 public function job_run(&$job_object)
 {
     $job_object->substeps_todo = 2;
     $job_object->log(sprintf(__('%d. Trying to create a WordPress export to XML file&#160;&hellip;', 'backwpup'), $job_object->steps_data[$job_object->step_working]['STEP_TRY']));
     //build filename
     $job_object->temp['wpexportfile'] = $job_object->generate_filename($job_object->job['wpexportfile'], 'xml');
     //check export file for writing
     if (!touch(BackWPup::get_plugin_data('TEMP') . $job_object->temp['wpexportfile'])) {
         $job_object->log(__('WP Export file could not generated.', 'backwpup'), E_USER_ERROR);
         return FALSE;
     }
     //include WP export function
     $this->job_object =& $job_object;
     $this->job_object->temp['wp_export_part'] = 0;
     require_once ABSPATH . 'wp-admin/includes/export.php';
     ob_start(array($this, 'ob_callback'), 1024 * 1024);
     //start output buffering
     $args = array('content' => $job_object->job['wpexportcontent']);
     @export_wp($args);
     //WP export
     ob_end_clean();
     //End output buffering
     if (filesize(BackWPup::get_plugin_data('TEMP') . $this->job_object->temp['wpexportfile']) < 1500) {
         $job_object->log(__('Could not generate a WordPress export file.', 'backwpup'), E_USER_ERROR);
         return FALSE;
     }
     if (extension_loaded('simplexml') && class_exists('DOMDocument')) {
         $job_object->log(__('Check WP Export file&#160;&hellip;', 'backwpup'));
         $job_object->need_free_memory(filesize(BackWPup::get_plugin_data('TEMP') . $this->job_object->temp['wpexportfile']) * 2);
         $valid = TRUE;
         $internal_errors = libxml_use_internal_errors(TRUE);
         $dom = new DOMDocument();
         $old_value = NULL;
         if (function_exists('libxml_disable_entity_loader')) {
             $old_value = libxml_disable_entity_loader(TRUE);
         }
         $success = $dom->loadXML(file_get_contents(BackWPup::get_plugin_data('TEMP') . $this->job_object->temp['wpexportfile']));
         if (!is_null($old_value)) {
             libxml_disable_entity_loader($old_value);
         }
         if (!$success || isset($dom->doctype)) {
             $errors = libxml_get_errors();
             $valid = FALSE;
             foreach ($errors as $error) {
                 switch ($error->level) {
                     case LIBXML_ERR_WARNING:
                         $job_object->log(E_USER_WARNING, sprintf(__('XML WARNING (%s): %s', 'backwpup'), $error->code, trim($error->message)), BackWPup::get_plugin_data('TEMP') . $job_object->temp['wpexportfile'], $error->line);
                         break;
                     case LIBXML_ERR_ERROR:
                         $job_object->log(E_USER_WARNING, sprintf(__('XML RECOVERABLE (%s): %s', 'backwpup'), $error->code, trim($error->message)), BackWPup::get_plugin_data('TEMP') . $job_object->temp['wpexportfile'], $error->line);
                         break;
                     case LIBXML_ERR_FATAL:
                         $job_object->log(E_USER_WARNING, sprintf(__('XML ERROR (%s): %s', 'backwpup'), $error->code, trim($error->message)), BackWPup::get_plugin_data('TEMP') . $job_object->temp['wpexportfile'], $error->line);
                         break;
                 }
             }
         } else {
             $xml = simplexml_import_dom($dom);
             unset($dom);
             // halt if loading produces an error
             if (!$xml) {
                 $job_object->log(__('There was an error when reading this WXR file', 'backwpup'), E_USER_ERROR);
                 $valid = FALSE;
             } else {
                 $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version');
                 if (!$wxr_version) {
                     $job_object->log(__('This does not appear to be a WXR file, missing/invalid WXR version number', 'backwpup'), E_USER_ERROR);
                     $valid = FALSE;
                 }
                 $wxr_version = (string) trim($wxr_version[0]);
                 // confirm that we are dealing with the correct file format
                 if (!preg_match('/^\\d+\\.\\d+$/', $wxr_version)) {
                     $job_object->log(__('This does not appear to be a WXR file, missing/invalid WXR version number', 'backwpup'), E_USER_ERROR);
                     $valid = FALSE;
                 }
             }
         }
         libxml_use_internal_errors($internal_errors);
         if ($valid) {
             $job_object->log(__('WP Export file is a valid WXR file.', 'backwpup'));
         }
     } else {
         $job_object->log(__('WP Export file can not checked, because no XML extension loaded with the file can checked.', 'backwpup'));
     }
     $job_object->substeps_done++;
     //Compress file
     if (!empty($job_object->job['wpexportfilecompression'])) {
         $job_object->log(__('Compressing file&#160;&hellip;', 'backwpup'));
         try {
             $compress = new BackWPup_Create_Archive(BackWPup::get_plugin_data('TEMP') . $job_object->temp['wpexportfile'] . $job_object->job['wpexportfilecompression']);
             if ($compress->add_file(BackWPup::get_plugin_data('TEMP') . $job_object->temp['wpexportfile'])) {
                 unset($compress);
                 unlink(BackWPup::get_plugin_data('TEMP') . $job_object->temp['wpexportfile']);
                 $job_object->temp['wpexportfile'] .= $job_object->job['wpexportfilecompression'];
                 $job_object->log(__('Compressing done.', 'backwpup'));
             }
         } catch (Exception $e) {
             $job_object->log($e->getMessage(), E_USER_ERROR, $e->getFile(), $e->getLine());
             unset($compress);
             return FALSE;
         }
     }
     $job_object->substeps_done++;
     //add XML file to backup files
     if (is_readable(BackWPup::get_plugin_data('TEMP') . $job_object->temp['wpexportfile'])) {
         $job_object->additional_files_to_backup[] = BackWPup::get_plugin_data('TEMP') . $job_object->temp['wpexportfile'];
         $job_object->count_files++;
         $job_object->count_filesize = $job_object->count_filesize + @filesize(BackWPup::get_plugin_data('TEMP') . $job_object->temp['wpexportfile']);
         $job_object->log(sprintf(__('Added XML export "%1$s" with %2$s to backup file list.', 'backwpup'), $job_object->temp['wpexportfile'], size_format(filesize(BackWPup::get_plugin_data('TEMP') . $job_object->temp['wpexportfile']), 2)));
     }
     $job_object->substeps_done = 1;
     return TRUE;
 }
Пример #5
0
 /**
  * Creates the backup archive
  */
 private function create_archive()
 {
     //load folders to backup
     $folders_to_backup = $this->get_folders_to_backup();
     $this->substeps_todo = $this->count_folder + 1;
     //initial settings for restarts in archiving
     if (!isset($this->steps_data[$this->step_working]['on_file'])) {
         $this->steps_data[$this->step_working]['on_file'] = '';
     }
     if (!isset($this->steps_data[$this->step_working]['on_folder'])) {
         $this->steps_data[$this->step_working]['on_folder'] = '';
     }
     if ($this->steps_data[$this->step_working]['on_folder'] == '' && $this->steps_data[$this->step_working]['on_file'] == '' && is_file($this->backup_folder . $this->backup_file)) {
         unlink($this->backup_folder . $this->backup_file);
     }
     if ($this->steps_data[$this->step_working]['SAVE_STEP_TRY'] != $this->steps_data[$this->step_working]['STEP_TRY']) {
         $this->log(sprintf(__('%d. Trying to create backup archive &hellip;', 'backwpup'), $this->steps_data[$this->step_working]['STEP_TRY']), E_USER_NOTICE);
     }
     try {
         $backup_archive = new BackWPup_Create_Archive($this->backup_folder . $this->backup_file);
         //show method for creation
         if ($this->substeps_done == 0) {
             $this->log(sprintf(_x('Compressing files as %s. Please be patient, this may take a moment.', 'Archive compression method', 'backwpup'), $backup_archive->get_method()));
         }
         //add extra files
         if ($this->substeps_done == 0) {
             if (!empty($this->additional_files_to_backup) && $this->substeps_done == 0) {
                 foreach ($this->additional_files_to_backup as $file) {
                     if ($backup_archive->add_file($file, basename($file))) {
                         $this->count_files++;
                         $this->count_filesize = filesize($file);
                         $this->update_working_data();
                     } else {
                         $backup_archive->close();
                         $this->steps_data[$this->step_working]['on_file'] = '';
                         $this->steps_data[$this->step_working]['on_folder'] = '';
                         $this->log(__('Cannot create backup archive correctly. Aborting creation.', 'backwpup'), E_USER_ERROR);
                         return FALSE;
                     }
                 }
             }
             $this->substeps_done++;
         }
         //add normal files
         while ($folder = array_shift($folders_to_backup)) {
             //jump over already done folders
             if (in_array($this->steps_data[$this->step_working]['on_folder'], $folders_to_backup)) {
                 continue;
             }
             $this->steps_data[$this->step_working]['on_folder'] = $folder;
             $files_in_folder = $this->get_files_in_folder($folder);
             //add empty folders
             if (empty($files_in_folder)) {
                 $folder_name_in_archive = trim(ltrim(str_replace($this->remove_path, '', $folder), '/'));
                 if (!empty($folder_name_in_archive)) {
                     $backup_archive->add_empty_folder($folder, $folder_name_in_archive);
                 }
                 continue;
             }
             //add files
             while ($file = array_shift($files_in_folder)) {
                 //jump over already done files
                 if (in_array($this->steps_data[$this->step_working]['on_file'], $files_in_folder)) {
                     continue;
                 }
                 $this->steps_data[$this->step_working]['on_file'] = $file;
                 //close archive before restart
                 $restart_time = $this->get_restart_time();
                 if ($restart_time < 0) {
                     unset($backup_archive);
                     $this->do_restart_time(TRUE);
                 }
                 //generate filename in archive
                 $in_archive_filename = ltrim(str_replace($this->remove_path, '', $file), '/');
                 //add file to archive
                 if ($backup_archive->add_file($file, $in_archive_filename)) {
                     $this->update_working_data();
                 } else {
                     $backup_archive->close();
                     $this->steps_data[$this->step_working]['on_file'] = '';
                     $this->steps_data[$this->step_working]['on_folder'] = '';
                     $this->substeps_done = 0;
                     $this->backup_filesize = filesize($this->backup_folder . $this->backup_file);
                     if ($this->backup_filesize + filesize($file) >= 2147483647) {
                         $this->log(__('Aborting creation.', 'backwpup'), E_USER_ERROR);
                         return TRUE;
                     }
                     $this->log(__('Cannot create backup archive correctly. Aborting creation.', 'backwpup'), E_USER_ERROR);
                     return FALSE;
                 }
             }
             $this->steps_data[$this->step_working]['on_file'] = '';
             $this->substeps_done++;
         }
         //restart if needed
         $restart_time = $this->get_restart_time();
         if ($restart_time < 5) {
             unset($backup_archive);
             $this->do_restart_time(TRUE);
         }
         $backup_archive->close();
         unset($backup_archive);
         $this->log(__('Backup archive created.', 'backwpup'), E_USER_NOTICE);
     } catch (Exception $e) {
         $this->log($e->getMessage(), E_USER_ERROR, $e->getFile(), $e->getLine());
         unset($backup_archive);
         return FALSE;
     }
     $this->backup_filesize = filesize($this->backup_folder . $this->backup_file);
     if ($this->backup_filesize) {
         $this->log(sprintf(__('Archive size is %s.', 'backwpup'), size_format($this->backup_filesize, 2)), E_USER_NOTICE);
     }
     $this->log(sprintf(__('%1$d Files with %2$s in Archive.', 'backwpup'), $this->count_files + $this->count_files_in_folder, size_format($this->count_filesize + $this->count_filesize_in_folder, 2)), E_USER_NOTICE);
     return TRUE;
 }