/**
  * Fire the webhook notification on the hmbkp_backup_complete
  *
  * @see  Backup::do_action
  * @param  string $action The action received from the backup
  * @return void
  */
 public function action($action, Backup $backup)
 {
     if ('hmbkp_backup_complete' !== $action || !$this->is_service_active()) {
         return;
     }
     $webhook_url = $this->get_url();
     $file = $backup->get_archive_filepath();
     $download = add_query_arg('hmbkp_download', base64_encode($file), HMBKP_ADMIN_URL);
     $domain = parse_url(home_url(), PHP_URL_HOST) . parse_url(home_url(), PHP_URL_PATH);
     // The backup failed, send a message saying as much
     if (!file_exists($file) && ($errors = array_merge($backup->get_errors(), $backup->get_warnings()))) {
         $error_message = '';
         foreach ($errors as $error_set) {
             $error_message .= implode("\n - ", $error_set);
         }
         if ($error_message) {
             $error_message = ' - ' . $error_message;
         }
         $subject = sprintf(__('Backup of %s Failed', 'backupwordpress'), $domain);
         $body = array('type' => 'backup.error', 'site_url' => site_url(), 'backup' => array('id' => 'backup_' . pathinfo($file, PATHINFO_FILENAME), 'start' => '0', 'end' => '0', 'download_url' => '', 'type' => $this->schedule->get_type(), 'status' => array('message' => $subject . ' - ' . $error_message, 'success' => '0')));
     } else {
         $body = array('type' => 'backup.success', 'site_url' => site_url(), 'backup' => array('id' => 'backup_' . $this->schedule->get_id(), 'start' => '0', 'end' => '0', 'download_url' => $download, 'type' => $this->schedule->get_type(), 'status' => array('message' => 'Backup complete', 'success' => '1')));
     }
     $signature = hash_hmac('sha1', serialize($body), $this->get_secret_key());
     $webhook_args = array('headers' => array('X-BWP-Signature' => $signature), 'body' => $body);
     $ret = wp_remote_post($webhook_url, $webhook_args);
     if (is_wp_error($ret)) {
         $backup->error('Webhook', sprintf(__('Error: %s', 'backupwordpress'), $ret->get_error_message()));
     }
 }
 /**
  * Fire the email notification on the hmbkp_backup_complete
  *
  * @see  Backup::do_action
  * @param  string $action The action received from the backup
  * @return void
  */
 public function action($action, Backup $backup)
 {
     if ($action === 'hmbkp_backup_complete' && $this->get_email_address_array()) {
         $file = $backup->get_archive_filepath();
         $sent = false;
         $download = add_query_arg('hmbkp_download', base64_encode($file), HMBKP_ADMIN_URL);
         $domain = parse_url(home_url(), PHP_URL_HOST) . parse_url(home_url(), PHP_URL_PATH);
         $headers = 'From: BackUpWordPress <' . apply_filters('hmbkp_from_email', get_bloginfo('admin_email')) . '>' . "\r\n";
         // The backup failed, send a message saying as much
         if (!file_exists($file) && ($errors = array_merge($backup->get_errors(), $backup->get_warnings()))) {
             $error_message = '';
             foreach ($errors as $error_set) {
                 $error_message .= implode("\n - ", $error_set);
             }
             if ($error_message) {
                 $error_message = ' - ' . $error_message;
             }
             $subject = sprintf(__('Backup of %s Failed', 'backupwordpress'), $domain);
             $message = sprintf(__('BackUpWordPress was unable to backup your site %1$s.', 'backupwordpress') . "\n\n" . __('Here are the errors that we\'re encountered:', 'backupwordpress') . "\n\n" . '%2$s' . "\n\n" . __('If the errors above look like Martian, forward this email to %3$s and we\'ll take a look', 'backupwordpress') . "\n\n" . __("Kind Regards,\nThe Apologetic BackUpWordPress Backup Emailing Robot", 'backupwordpress'), home_url(), $error_message, '*****@*****.**');
             wp_mail($this->get_email_address_array(), $subject, $message, $headers);
             return;
         }
         $subject = sprintf(__('Backup of %s', 'backupwordpress'), $domain);
         // If it's larger than the max attachment size limit assume it's not going to be able to send the backup
         if (@filesize($file) < hmbkp_get_max_attachment_size()) {
             $message = sprintf(__('BackUpWordPress has completed a backup of your site %1$s.', 'backupwordpress') . "\n\n" . __('The backup file should be attached to this email.', 'backupwordpress') . "\n\n" . __('You can download the backup file by clicking the link below:', 'backupwordpress') . "\n\n" . '%2$s' . "\n\n" . __("Kind Regards,\nThe Happy BackUpWordPress Backup Emailing Robot", 'backupwordpress'), home_url(), $download);
             $sent = wp_mail($this->get_email_address_array(), $subject, $message, $headers, $file);
         }
         // If we didn't send the email above then send just the notification
         if (!$sent) {
             $message = sprintf(__('BackUpWordPress has completed a backup of your site %1$s.', 'backupwordpress') . "\n\n" . __('Unfortunately the backup file was too large to attach to this email.', 'backupwordpress') . "\n\n" . __('You can download the backup file by clicking the link below:', 'backupwordpress') . "\n\n" . '%2$s' . "\n\n" . __("Kind Regards,\nThe Happy BackUpWordPress Backup Emailing Robot", 'backupwordpress'), home_url(), $download);
             wp_mail($this->get_email_address_array(), $subject, $message, $headers);
         }
     }
 }
 /**
  * Perform a Backup.
  *
  * ## OPTIONS
  *
  * [--files_only]
  * : Backup files only, default to off
  *
  * [--database_only]
  * : Backup database only, defaults to off
  *
  * [--destination]
  * : dir that the backup should be save in, defaults to your existing backups directory
  *
  * [--root]
  * : dir that should be backed up, defaults to site root.
  *
  * [--zip_command_path]
  * : path to your zip binary, standard locations are automatically used
  *
  * [--mysqldump_command_path]
  * : path to your mysqldump binary, standard locations are automatically used
  *
  * [--archive_filename]
  * : filename for the resulting zip file
  *
  * [--excludes]
  * : list of paths you'd like to exclude
  *
  * ## Usage
  *
  *     wp backupwordpress backup [--files_only] [--database_only] [--path<dir>] [--root<dir>] [--zip_command_path=<path>] [--mysqldump_command_path=<path>]
  *
  * @todo errors should be bubbled from Backup, Scheduled_Backup and the like instead of being repeated.
  */
 public function backup($args, $assoc_args)
 {
     add_action('hmbkp_mysqldump_started', function () {
         WP_CLI::line(__('Backup: Dumping database...', 'backupwordpress'));
     });
     add_action('hmbkp_archive_started', function () {
         WP_CLI::line(__('Backup: Zipping everything up...', 'backupwordpress'));
     });
     $hm_backup = new Backup();
     if (!empty($assoc_args['destination'])) {
         Path::get_instance()->set_path($assoc_args['destination']);
     }
     Path::get_instance()->cleanup();
     if (!empty($assoc_args['root'])) {
         $hm_backup->set_root($assoc_args['root']);
     }
     if (!is_dir(hmbkp_path())) {
         WP_CLI::error(__('Invalid backup path', 'backupwordpress'));
         return false;
     }
     if (!is_dir($hm_backup->get_root()) || !is_readable($hm_backup->get_root())) {
         WP_CLI::error(__('Invalid root path', 'backupwordpress'));
         return false;
     }
     if (isset($assoc_args['archive_filename'])) {
         $hm_backup->set_archive_filename($assoc_args['archive_filename']);
     }
     if (!empty($assoc_args['files_only'])) {
         $hm_backup->set_type('file');
     }
     if (!empty($assoc_args['database_only'])) {
         $hm_backup->set_type('database');
     }
     if (isset($assoc_args['mysqldump_command_path'])) {
         $hm_backup->set_mysqldump_command_path($assoc_args['mysqldump_command_path']);
     }
     if (isset($assoc_args['zip_command_path'])) {
         $hm_backup->set_zip_command_path($assoc_args['zip_command_path']);
     }
     if (!empty($assoc_args['excludes'])) {
         $hm_backup->set_excludes($assoc_args['excludes']);
     }
     $hm_backup->backup();
     if (file_exists($hm_backup->get_archive_filepath())) {
         WP_CLI::success(__('Backup Complete: ', 'backupwordpress') . $hm_backup->get_archive_filepath());
     } else {
         WP_CLI::error(__('Backup Failed', 'backupwordpress'));
     }
 }