function send_notification_email($err, $success, $logs = array(), $notification_email)
 {
     global $logger, $status_array, $backup_job;
     $logger->log_info(__METHOD__, "Begin");
     $utility = new WPBackItUp_Utility($logger);
     if ($success) {
         //Don't send logs on success unless debug is on.
         if (WPBACKITUP__DEBUG !== true) {
             $logs = array();
         }
         $subject = get_bloginfo() . ' - Restore completed successfully.';
         $message = '<b>Your site was restored successfully.</b><br/><br/>';
     } else {
         $subject = get_bloginfo() . ' - Backup did not complete successfully.';
         $message = '<b>Your restore did not complete successfully.</b><br/><br/>';
     }
     $local_datetime = get_date_from_gmt(date('Y-m-d H:i:s', current_time('timestamp')));
     $message .= 'WordPress Site: <a href="' . home_url() . '" target="_blank">' . home_url() . '</a><br/>';
     $message .= 'Restore date: ' . $local_datetime . '<br/>';
     //$message .= 'Completion Code: ' . $backup_job->backup_id .'-'. $processing_minutes .'-' .$processing_seconds .'<br/>';
     $message .= 'WP BackItUp Version: ' . WPBACKITUP__VERSION . '<br/>';
     $message .= '<br/>';
     //Add the completed steps on success
     //		if($success) {
     //			$message .='<b>Steps Completed</b><br/>';
     //
     //			//Add the completed statuses
     //			foreach ($status_array as $status_key => $status_value) {
     //				if ($status_value==2) {
     //					foreach ($status_description as $msg_key => $msg_value) {
     //						if ($status_key==$msg_key) {
     //							$message .=  $msg_value . '<br/>';
     //							break;
     //						}
     //					}
     //				}
     //			}
     //		} else  {
     //			//Error occurred
     //			$message .= '<br/>';
     //			$message .= 'Errors:<br/>' . get_error_message($err);
     //		}
     //		$term='success';
     //		if(!$success)$term='error';
     //		$message .='<br/><br/>Checkout '. $WPBackitup->get_anchor_with_utm('www.wpbackitup.com', '', 'notification+email', $term) .' for info about WP BackItUp and our other products.<br/>';
     if ($notification_email) {
         $utility->send_email($notification_email, $subject, $message, $logs);
     }
     $logger->log_info(__METHOD__, "End");
 }
 public function purge_files($path, $file_pattern, $days)
 {
     $this->logger->log('(FileSytem.purge_files) Purge files days:' . $days);
     $this->logger->log('(FileSytem.purge_files) Purge files path:' . $path);
     $this->logger->log('(FileSytem.purge_files) Purge files extension:' . $file_pattern);
     //Check Parms
     if (empty($path) || empty($file_pattern) || !is_numeric($days)) {
         $this->logger->log('(FileSytem.purge_files) Invalid Parm values');
         return false;
     }
     $FileList = glob($path . $file_pattern);
     //Sort by Date Time oldest first so can break when all old files are deleted
     usort($FileList, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
     foreach ($FileList as $key => $file) {
         $this->logger->log_info(__METHOD__, ' File:' . $file);
         $this->logger->log_info(__METHOD__, ' File Date Time:' . filemtime($file));
         $current_date = new DateTime('now');
         $file_mod_date = new DateTime(date('Y-m-d', filemtime($file)));
         //PHP 5.3 only
         //$date_diff = $current_date->diff($file_mod_date);
         //$date_diff_days = $date_diff->days;
         $util = new WPBackItUp_Utility($this->logger);
         $date_diff_days = $util->date_diff_days($file_mod_date, $current_date);
         if ($date_diff_days >= $days) {
             if (file_exists($file)) {
                 unlink($file);
             }
             $this->logger->log('Delete:' . $file);
         } else {
             break;
             //Exit for
         }
     }
     $this->logger->log('(FileSytem.purge_files) Completed.');
     return true;
 }
function send_backup_notification_email($err, $success, $logs = array())
{
    global $WPBackitup, $wp_backup, $logger, $status_array, $backup_job;
    $logger->log_info(__METHOD__, "Begin");
    $start_timestamp = $backup_job->get_job_start_time();
    $end_timestamp = $backup_job->get_job_end_time();
    $utility = new WPBackItUp_Utility($logger);
    $seconds = $utility->timestamp_diff_seconds($start_timestamp, $end_timestamp);
    $processing_minutes = round($seconds / 60);
    $processing_seconds = $seconds % 60;
    $status_description = array('preparing' => 'Preparing for backup...Done', 'backupdb' => 'Backing up database...Done', 'infofile' => 'Creating backup information file...Done', 'backup_themes' => 'Backing up themes...Done', 'backup_plugins' => 'Backing up plugins...Done', 'backup_uploads' => 'Backing up uploads...Done', 'backup_other' => 'Backing up miscellaneous files...Done', 'finalize_backup' => 'Finalizing backup...Done', 'validate_backup' => 'Validating backup...Done', 'cleanup' => 'Cleaning up...Done');
    if ($success) {
        //Don't send logs on success unless debug is on.
        if (WPBACKITUP__DEBUG !== true) {
            $logs = array();
        }
        $subject = get_bloginfo() . ' - Backup completed successfully.';
        $message = '<b>Your backup completed successfully.</b><br/><br/>';
    } else {
        $subject = get_bloginfo() . ' - Backup did not complete successfully.';
        $message = '<b>Your backup did not complete successfully.</b><br/><br/>';
    }
    $local_start_datetime = get_date_from_gmt(date('Y-m-d H:i:s', $start_timestamp));
    $local_end_datetime = get_date_from_gmt(date('Y-m-d H:i:s', $end_timestamp));
    $message .= 'WordPress Site: <a href="' . home_url() . '" target="_blank">' . home_url() . '</a><br/>';
    $message .= 'Backup date: ' . $local_start_datetime . '<br/>';
    $message .= 'Number of backups completed with WP BackItUp: ' . $WPBackitup->backup_count() . '<br/>';
    $message .= 'Completion Code: ' . $backup_job->backup_id . '-' . $processing_minutes . '-' . $processing_seconds . '<br/>';
    $message .= 'WP BackItUp Version: ' . WPBACKITUP__VERSION . '<br/>';
    $message .= '<br/>';
    //Add the completed steps on success
    if ($success) {
        $message .= '<b>Steps Completed</b><br/>';
        //Add the completed statuses
        foreach ($status_array as $status_key => $status_value) {
            if ($status_value == 2) {
                foreach ($status_description as $msg_key => $msg_value) {
                    if ($status_key == $msg_key) {
                        $message .= $msg_value . '<br/>';
                        break;
                    }
                }
            }
        }
    } else {
        //Error occurred
        $message .= '<br/>';
        $message .= 'Errors:<br/>' . get_error_message($err);
    }
    $term = 'success';
    if (!$success) {
        $term = 'error';
    }
    $message .= '<br/><br/>Checkout ' . $WPBackitup->get_anchor_with_utm('www.wpbackitup.com', '', 'notification+email', $term) . ' for info about WP BackItUp and our other products.<br/>';
    $notification_email = $WPBackitup->get_option('notification_email');
    if ($notification_email) {
        $utility->send_email($notification_email, $subject, $message, $logs);
    }
    $logger->log_info(__FUNCTION__, "End");
}
function end_job($err = null, $success = null)
{
    global $WPBackitup, $logger, $cleanup_job;
    $logger->log_info(__METHOD__, "Begin");
    WPBackItUp_Backup::end();
    //Release the lock
    $current_datetime = current_time('timestamp');
    $WPBackitup->set_cleanup_lastrun_date($current_datetime);
    $util = new WPBackItUp_Utility($logger);
    $seconds = $util->timestamp_diff_seconds($cleanup_job->get_job_start_time(), $cleanup_job->get_job_end_time());
    $processing_minutes = round($seconds / 60);
    $processing_seconds = $seconds % 60;
    $logger->log('Script Processing Time:' . $processing_minutes . ' Minutes ' . $processing_seconds . ' Seconds');
    if (true === $success) {
        $logger->log("Cleanup completed: SUCCESS");
    }
    if (false === $success) {
        $logger->log("Cleanup completed: ERROR");
    }
    $logger->log("*** END JOB ***");
    $logger->close_file();
    echo 'cleanup has completed';
    exit(0);
}
 /**
  * Send support request Schedule
  *
  */
 public function _admin_send_support_request()
 {
     // Verify submission for processing using wp_nonce
     $url = str_replace('&s=1', '', $_REQUEST['_wp_http_referer']);
     $logger = new WPBackItUp_Logger(true, null, 'debug_support');
     $logger->log_sysinfo();
     $logger->log_info(__METHOD__, 'Send Support Request');
     $error = false;
     if (wp_verify_nonce($_REQUEST['_wpnonce'], "{$this->namespace}-support-form")) {
         $logger->log_info(__METHOD__, "Send support request");
         $logger->log_info(__METHOD__, $_POST);
         //save the email in place of transient
         $this->set_support_email($_POST['support_email']);
         // save the transients in case of error
         foreach ($_POST as $key => $val) {
             set_transient($key, __($val, $this->namespace), 60);
         }
         //validate form fields
         if (empty($_POST['support_email']) || !is_email($_POST['support_email'])) {
             $error = true;
             set_transient('error-support-email', __('Please enter a valid email', $this->namespace), 60);
         }
         if (empty($_POST['support_ticket_id'])) {
             $error = true;
             set_transient('error-support-ticket', __('Please enter your support ticket id', $this->namespace), 60);
         } else {
             if (!is_numeric($_POST['support_ticket_id'])) {
                 $error = true;
                 set_transient('error-support-ticket', __('Please only enter numbers in this field', $this->namespace), 60);
             }
         }
         //			if(empty($_POST['support_subject']))
         //			{
         //				$error=true;
         //				set_transient('error-support-subject', __('Please enter a short description of your problem', $this->namespace), 60);
         //			}
         //			if(empty($_POST['support_body']))
         //			{
         //				$error=true;
         //				set_transient('error-support-body', __('Please enter your problem description', $this->namespace), 60);
         //			}
         $include_logs = true;
         //always send logs
         //			if(!empty($_POST['support_include_logs']))
         //			{
         //				$include_logs=true;
         //			}
         //Send if no errors
         if (!$error) {
             if (!class_exists('WPBackItUp_Zip')) {
                 include_once 'class-zip.php';
             }
             if (!class_exists('WPBackItUp_Utility')) {
                 include_once 'class-utility.php';
             }
             $support_request_id = time();
             $logs_attachment = array();
             //default to no logs
             if ($include_logs) {
                 $logs_path = WPBACKITUP__PLUGIN_PATH . 'logs';
                 //copy/replace WP debug file
                 $wpdebug_file_path = WPBACKITUP__CONTENT_PATH . '/debug.log';
                 $logger->log_info(__METHOD__, "Copy WP Debug: " . $wpdebug_file_path);
                 if (file_exists($wpdebug_file_path)) {
                     copy($wpdebug_file_path, $logs_path . '/wpdebug.log');
                 }
                 $zip_file_path = $logs_path . '/logs_' . $support_request_id . '.zip';
                 $zip = new WPBackItUp_Zip($logger, $zip_file_path);
                 $zip->zip_files_in_folder($logs_path, $support_request_id, '*.log');
                 $zip->close();
                 $logs_attachment = array($zip_file_path);
             }
             //Get registration name
             $utility = new WPBackItUp_Utility($logger);
             $support_to_address = WPBACKITUP__SUPPORT_EMAIL;
             //If we force registration then this will always be here.
             $from_name = $this->license_customer_name();
             $support_from_email = $_POST['support_email'];
             $support_subject = '[#' . trim($_POST['support_ticket_id']) . ']';
             $site_info = 'WordPress Site: <a href="' . home_url() . '" target="_blank">' . home_url() . '</a><br/>';
             $site_info .= "WordPress Version: " . get_bloginfo('version') . ' <br />';
             $site_info .= "WP BackItUp License Type: " . $this->license_type_description() . ' <br />';
             $site_info .= "WP BackItUp Version: " . $this->version . ' <br />';
             $support_body = $site_info . '<br/><br/><b>Customer Comments:</b><br/><br/>' . $_POST['support_body'];
             $utility->send_email_v2($support_to_address, $support_subject, $support_body, $logs_attachment, $from_name, $support_from_email, $support_from_email);
             // get rid of the transients
             foreach ($_POST as $key => $val) {
                 delete_transient($key);
             }
             wp_safe_redirect($url . '&s=1');
             exit;
         }
     }
     wp_safe_redirect($url);
     exit;
 }
     fatal_error($task, '221', 'Table prefix different from restore.');
 }
 //Check wordpress version
 $site_wordpress_version = get_bloginfo('version');
 $backup_wordpress_version = $site_info['restore_wp_version'];
 $logger->log_info(__METHOD__, 'Site Wordpress Version:' . $site_wordpress_version);
 $logger->log_info(__METHOD__, 'Backup Wordpress Version:' . $backup_wordpress_version);
 if (!WPBackItUp_Utility::version_compare($site_wordpress_version, $backup_wordpress_version)) {
     $logger->log('*VALIDATE SITEDATA FILE*');
     fatal_error($task, '226', 'Backup was created using different version of wordpress');
 }
 $restore_wpbackitup_version = $site_info['restore_wpbackitup_version'];
 $current_wpbackitup_version = WPBACKITUP__VERSION;
 $logger->log_info(__METHOD__, 'WP BackItUp current Version:' . $current_wpbackitup_version);
 $logger->log_info(__METHOD__, 'WP BackItUp backup  Version:' . $restore_wpbackitup_version);
 if (!WPBackItUp_Utility::version_compare($restore_wpbackitup_version, $current_wpbackitup_version)) {
     fatal_error($task, '227', 'Backup was created using different version of WP BackItUp');
 }
 $logger->log('*END VALIDATE SITEDATA FILE*');
 $logger->log('*VALIDATE SQL FILE EXISTS*');
 if (!$wp_restore->validate_SQL_exists()) {
     fatal_error($task, '216', 'NO Database backups in backup.');
 }
 $logger->log('*END VALIDATE SQL FILE EXISTS*');
 end_status($task);
 $logger->log('*DEACTIVATE ACTIVE PLUGINS*');
 $task = 'deactivate_plugins';
 start_status($task);
 $wp_restore->deactivate_plugins();
 end_status($task);
 $logger->log('*END DEACTIVATE ACTIVE PLUGINS*');