/** * 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)); } } } }
/** * See Status of a working job * * @param $args * @param $assoc_args */ public function working($args, $assoc_args) { $job_object = BackWPup_Job::get_working_data(); if (!is_object($job_object)) { WP_CLI::error(__('No job running', 'backwpup')); } $formatter_args = array('format' => 'table', 'fields' => array('JobID', 'Name', 'Warnings', 'Errors', 'On Step', 'Done'), 'field' => NULL); $formatter = new WP_CLI\Formatter($formatter_args); $items = array(); $items[] = array('JobID' => $job_object->job['jobid'], 'Name' => $job_object->job['name'], 'Warnings' => $job_object->warnings, 'Errors' => $job_object->errors, 'On Step' => $job_object->steps_data[$job_object->step_working]['NAME'], 'Done' => $job_object->step_percent . ' / ' . $job_object->substep_percent, 'Last message' => str_replace('…', '...', strip_tags($job_object->lastmsg))); $formatter->display_items($items); WP_CLI::log('Last Message: ' . str_replace('…', '...', strip_tags($job_object->lastmsg))); }
/** * See Status of a working job * * @param $args * @param $assoc_args * @synopsis working */ public function working($args, $assoc_args) { $job_object = BackWPup_Job::get_working_data(); if (is_object($job_object)) { WP_CLI::error(__('No job running', 'backwpup')); } WP_CLI::line(__('Running job', 'backwpup')); WP_CLI::line('----------------------------------------------------------------------'); WP_CLI::line(sprintf(__('ID: %1$d Name: %2$s', 'backwpup'), $job_object->job['jobid'], $job_object->job['name'])); WP_CLI::line(sprintf(__('Warnings: %1$d Errors: %2$d', 'backwpup'), $job_object->warnings, $job_object->errors)); WP_CLI::line(sprintf(__('Steps in percent: %1$d percent of step: %2$d', 'backwpup'), $job_object->step_percent, $job_object->substep_percent)); WP_CLI::line(sprintf(__('On step: %s', 'backwpup'), $job_object->steps_data[$job_object->step_working]['NAME'])); WP_CLI::line(sprintf(__('Last message: %s', 'backwpup'), str_replace('…', '...', strip_tags($job_object->lastmsg)))); }
public static function user_abort() { /* @var $job_object BackWPup_Job */ $job_object = BackWPup_Job::get_working_data(); unlink(BackWPup::get_plugin_data('running_file')); //if job not working currently abort it this way for message $not_worked_time = microtime(true) - $job_object->timestamp_last_update; $restart_time = get_site_option('backwpup_cfg_jobmaxexecutiontime'); if (empty($restart_time)) { $restart_time = 60; } if (empty($job_object->pid) || $not_worked_time > $restart_time) { $job_object->user_abort = true; $job_object->update_working_data(); } }
/** * Displaying next jobs */ private static function mb_next_jobs() { if (!current_user_can('backwpup_jobs')) { return; } ?> <table class="wp-list-table widefat" cellspacing="0"> <caption><?php _e('Next scheduled jobs', 'backwpup'); ?> </caption> <thead> <tr> <th style="width: 30%"><?php _e('Time', 'backwpup'); ?> </th> <th style="width: 70%"><?php _e('Job', 'backwpup'); ?> </th> </tr> </thead> <?php //get next jobs $mainsactive = BackWPup_Option::get_job_ids('activetype', 'wpcron'); sort($mainsactive); $alternate = TRUE; // add working job if it not in active jobs $job_object = BackWPup_Job::get_working_data(); if (!empty($job_object) && !empty($job_object->job['jobid']) && !in_array($job_object->job['jobid'], $mainsactive)) { $mainsactive[] = $job_object->job['jobid']; } foreach ($mainsactive as $jobid) { $name = BackWPup_Option::get($jobid, 'name'); if (!empty($job_object) && $job_object->job['jobid'] == $jobid) { $runtime = current_time('timestamp') - $job_object->job['lastrun']; if (!$alternate) { echo '<tr>'; $alternate = TRUE; } else { echo '<tr class="alternate">'; $alternate = FALSE; } echo '<td>' . sprintf('<span style="color:#e66f00;">' . __('working since %d seconds', 'backwpup') . '</span>', $runtime) . '</td>'; echo '<td><span style="font-weight:bold;">' . esc_html($job_object->job['name']) . '</span><br />'; echo "<a style=\"color:red;\" href=\"" . wp_nonce_url(network_admin_url('admin.php') . '?page=backwpupjobs&action=abort', 'abort-job') . "\">" . __('Abort', 'backwpup') . "</a>"; echo "</td></tr>"; } else { if (!$alternate) { echo '<tr>'; $alternate = TRUE; } else { echo '<tr class="alternate">'; $alternate = FALSE; } if ($nextrun = wp_next_scheduled('backwpup_cron', array('id' => $jobid)) + get_option('gmt_offset') * 3600) { echo '<td>' . sprintf(__('%1$s at %2$s', 'backwpup'), date_i18n(get_option('date_format'), $nextrun, TRUE), date_i18n(get_option('time_format'), $nextrun, TRUE)) . '</td>'; } else { echo '<td><em>' . __('Not scheduled!', 'backwpup') . '</em></td>'; } echo '<td><a href="' . wp_nonce_url(network_admin_url('admin.php') . '?page=backwpupeditjob&jobid=' . $jobid, 'edit-job') . '" title="' . esc_attr(__('Edit Job', 'backwpup')) . '">' . $name . '</a></td></tr>'; } } if (empty($mainsactive) and !empty($job_object)) { echo '<tr><td colspan="2"><i>' . __('none', 'backwpup') . '</i></td></tr>'; } ?> </table> <?php }
/** * * Function to generate json data * */ public static function ajax_working() { check_ajax_referer('backwpupworking_ajax_nonce'); if (!current_user_can('backwpup_jobs_start')) { die('-1'); } $log_folder = get_site_option('backwpup_cfg_logfolder'); $log_folder = BackWPup_File::get_absolute_path($log_folder); $logfile = isset($_GET['logfile']) ? $log_folder . basename(trim($_GET['logfile'])) : NULL; $logpos = isset($_GET['logpos']) ? absint($_GET['logpos']) : 0; $restart_url = ''; //check if logfile renamed if (file_exists($logfile . '.gz')) { $logfile .= '.gz'; } if (!is_readable($logfile) || strstr($_GET['logfile'], 'backwpup_log_') === false) { die('0'); } $job_object = BackWPup_Job::get_working_data(); $done = 0; if (is_object($job_object)) { $warnings = $job_object->warnings; $errors = $job_object->errors; $step_percent = $job_object->step_percent; $substep_percent = $job_object->substep_percent; $runtime = current_time('timestamp') - $job_object->start_time; $onstep = $job_object->steps_data[$job_object->step_working]['NAME']; $lastmsg = $job_object->lastmsg; $lasterrormsg = $job_object->lasterrormsg; } else { $logheader = BackWPup_Job::read_logheader($logfile); $warnings = $logheader['warnings']; $runtime = $logheader['runtime']; $errors = $logheader['errors']; $step_percent = 100; $substep_percent = 100; $onstep = '<div class="backwpup-message backwpup-info"><p>' . esc_html__('Job completed', 'backwpup') . '</p></div>'; if ($errors > 0) { $lastmsg = '<div class="error"><p>' . esc_html__('ERROR:', 'backwpup') . ' ' . sprintf(esc_html__('Job has ended with errors in %s seconds. You must resolve the errors for correct execution.', 'backwpup'), $logheader['runtime']) . '</p></div>'; } elseif ($warnings > 0) { $lastmsg = '<div class="backwpup-message backwpup-warning"><p>' . esc_html__('WARNING:', 'backwpup') . ' ' . sprintf(esc_html__('Job has done with warnings in %s seconds. Please resolve them for correct execution.', 'backwpup'), $logheader['runtime']) . '</p></div>'; } else { $lastmsg = '<div class="updated"><p>' . sprintf(esc_html__('Job done in %s seconds.', 'backwpup'), $logheader['runtime']) . '</p></div>'; } $lasterrormsg = ''; $done = 1; } if ('.gz' == substr($logfile, -3)) { $logfiledata = file_get_contents('compress.zlib://' . $logfile, FALSE, NULL, $logpos); } else { $logfiledata = file_get_contents($logfile, FALSE, NULL, $logpos); } preg_match('/<body[^>]*>/si', $logfiledata, $match); if (!empty($match[0])) { $startpos = strpos($logfiledata, $match[0]) + strlen($match[0]); } else { $startpos = 0; } $endpos = stripos($logfiledata, '</body>'); if (FALSE === $endpos) { $endpos = strlen($logfiledata); } $length = strlen($logfiledata) - (strlen($logfiledata) - $endpos) - $startpos; //check if restart must done on ALTERNATE_WP_CRON if (is_object($job_object) && defined('ALTERNATE_WP_CRON') && ALTERNATE_WP_CRON) { $restart = BackWPup_Job::get_jobrun_url('restartalt'); if ($job_object->pid === 0 && $job_object->uniqid === '') { $restart_url = $restart['url']; } $last_update = microtime(TRUE) - $job_object->timestamp_last_update; if (empty($job_object->pid) && $last_update > 10) { $restart_url = $restart['url']; } } wp_send_json(array('log_pos' => strlen($logfiledata) + $logpos, 'log_text' => substr($logfiledata, $startpos, $length), 'warning_count' => $warnings, 'error_count' => $errors, 'running_time' => $runtime, 'step_percent' => $step_percent, 'on_step' => $onstep, 'last_msg' => $lastmsg, 'last_error_msg' => $lasterrormsg, 'sub_step_percent' => $substep_percent, 'restart_url' => $restart_url, 'job_done' => $done)); }
/** * Start job if in cron and run query args are set. */ public static function cron_active($args = array()) { //only if cron active if (!defined('DOING_CRON') || !DOING_CRON) { return; } if (isset($_GET['backwpup_run'])) { $args['run'] = sanitize_text_field($_GET['backwpup_run']); } if (isset($_GET['_nonce'])) { $args['nonce'] = sanitize_text_field($_GET['_nonce']); } if (isset($_GET['jobid'])) { $args['jobid'] = absint($_GET['jobid']); } $args = array_merge(array('run' => '', 'nonce' => '', 'jobid' => 0), $args); if (!in_array($args['run'], array('test', 'restart', 'runnow', 'runnowalt', 'runext', 'cronrun'), true)) { return; } //special header @session_write_close(); @header('Content-Type: text/html; charset=' . get_bloginfo('charset'), true); @header('X-Robots-Tag: noindex, nofollow', true); nocache_headers(); //on test die for fast feedback if ($args['run'] === 'test') { die('BackWPup test request'); } if ($args['run'] === 'restart') { $job_object = BackWPup_Job::get_working_data(); //restart job if not working or a restart wished $not_worked_time = microtime(TRUE) - $job_object->timestamp_last_update; if (!$job_object->pid || $not_worked_time > 300) { BackWPup_Job::start_http('restart'); return; } } // generate normal nonce $nonce = substr(wp_hash(wp_nonce_tick() . 'backwpup_job_run-' . $args['run'], 'nonce'), -12, 10); //special nonce on external start if ($args['run'] === 'runext') { $nonce = get_site_option('backwpup_cfg_jobrunauthkey'); } if ($args['run'] === 'cronrun') { $nonce = ''; } // check nonce if ($nonce !== $args['nonce']) { return; } //check runext is allowed for job if ($args['run'] === 'runext') { $jobids_link = BackWPup_Option::get_job_ids('activetype', 'link'); $jobids_easycron = BackWPup_Option::get_job_ids('activetype', 'easycron'); $jobids_external = array_merge($jobids_link, $jobids_easycron); if (!in_array($args['jobid'], $jobids_external, true)) { return; } } //run BackWPup job BackWPup_Job::start_http($args['run'], $args['jobid']); }
/** * * Function to generate json data * */ public static function ajax_working() { check_ajax_referer('backwpupworking_ajax_nonce'); $logfile = isset($_GET['logfile']) ? get_site_option('backwpup_cfg_logfolder') . trim($_GET['logfile']) : NULL; $logpos = isset($_GET['logpos']) ? (int) $_GET['logpos'] : 0; //check if logfile renamed if (file_exists($logfile . '.gz')) { $logfile .= '.gz'; } if (!is_readable($logfile)) { die('0'); } $job_object = BackWPup_Job::get_working_data(); $done = 0; if (is_object($job_object)) { $warnings = $job_object->warnings; $errors = $job_object->errors; $step_percent = $job_object->step_percent; $substep_percent = $job_object->substep_percent; $runtime = current_time('timestamp') - $job_object->start_time; $onstep = $job_object->steps_data[$job_object->step_working]['NAME']; $lastmsg = $job_object->lastmsg; $lasterrormsg = $job_object->lasterrormsg; } else { $logheader = BackWPup_Job::read_logheader($logfile); $warnings = $logheader['warnings']; $runtime = $logheader['runtime']; $errors = $logheader['errors']; $step_percent = 100; $substep_percent = 100; $onstep = '<div class="backwpup-message backwpup-info"><p>' . __('Job completed', 'backwpup') . '</p></div>'; if ($errors > 0) { $lastmsg = '<div class="error"><p>' . __('ERROR:', 'backwpup') . ' ' . sprintf(__('Job has ended with errors in %s seconds. You must resolve the errors for correct execution.', 'backwpup'), $logheader['runtime']) . '</p></div>'; } elseif ($warnings > 0) { $lastmsg = '<div class="backwpup-message backwpup-warning"><p>' . __('WARNING:', 'backwpup') . ' ' . sprintf(__('Job has done with warnings in %s seconds. Please resolve them for correct execution.', 'backwpup'), $logheader['runtime']) . '</p></div>'; } else { $lastmsg = '<div class="updated"><p>' . sprintf(__('Job done in %s seconds.', 'backwpup'), $logheader['runtime']) . '</p></div>'; } $lasterrormsg = ''; $done = 1; } if ('.gz' == substr($logfile, -3)) { $logfiledata = file_get_contents('compress.zlib://' . $logfile, FALSE, NULL, $logpos); } else { $logfiledata = file_get_contents($logfile, FALSE, NULL, $logpos); } preg_match('/<body[^>]*>/si', $logfiledata, $match); if (!empty($match[0])) { $startpos = strpos($logfiledata, $match[0]) + strlen($match[0]); } else { $startpos = 0; } $endpos = stripos($logfiledata, '</body>'); if (FALSE === $endpos) { $endpos = strlen($logfiledata); } $length = strlen($logfiledata) - (strlen($logfiledata) - $endpos) - $startpos; wp_send_json(array('log_pos' => strlen($logfiledata) + $logpos, 'log_text' => substr($logfiledata, $startpos, $length), 'warning_count' => $warnings, 'error_count' => $errors, 'running_time' => $runtime, 'step_percent' => $step_percent, 'on_step' => $onstep, 'last_msg' => $lastmsg, 'last_error_msg' => $lasterrormsg, 'sub_step_percent' => $substep_percent, 'job_done' => $done)); }
protected function backup_now() { if (!isset($_POST['settings']['job_id'])) { return array('error' => __('Missing job id', $this->plugin_translate)); } // Simulate http://wp/wp-admin/admin.php?jobid=1&page=backwpupjobs&action=runnow $_GET['jobid'] = $_POST['settings']['job_id']; $_REQUEST['action'] = 'runnow'; $_REQUEST['_wpnonce'] = wp_create_nonce('backwpup_job_run-runnowlink'); update_site_option('backwpup_messages', array()); $this->wp_list_table_dependency(); ob_start(); BackWPup_Page_Jobs::load(); ob_end_clean(); $output = $this->check_backwpup_messages(); if (isset($output['error'])) { return array('error' => 'BackWPup_Page_Jobs::load fail: ' . $output['error']); } else { $job_object = BackWPup_Job::get_working_data(); if (is_object($job_object)) { return array('success' => 1, 'response' => $output['message'], 'logfile' => basename($job_object->logfile)); } else { return array('success' => 1, 'response' => $output['message']); } } }