/**
  * 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_backup_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->warning('Webhook', sprintf(__('Error: %s', 'backupwordpress'), $ret->get_error_message()));
     }
 }
Ejemplo n.º 2
0
 /**
  * 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);
         }
     }
 }