| 1 |
<?php |
| 2 |
/** |
| 3 |
* Destination CloudDrive. |
| 4 |
* |
| 5 |
* @package wpdbbkp |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
add_action( 'wp_db_backup_completed', array( 'WPDatabaseBackupCD', 'wp_db_backup_completed' ) ); |
| 16 |
|
| 17 |
/** |
| 18 |
* WPDatabaseBackupCD Class. |
| 19 |
* |
| 20 |
* @class WPDatabaseBackupCD |
| 21 |
*/ |
| 22 |
class WPDatabaseBackupCD { |
| 23 |
|
| 24 |
// Function to upload files to Backblaze B2 |
| 25 |
public static function upload_backup_to_clouddrive($file_path, $file_name) { |
| 26 |
|
| 27 |
global $wp_filesystem; |
| 28 |
if(!function_exists('WP_Filesystem')){ |
| 29 |
require_once ( ABSPATH . '/wp-admin/includes/file.php' ); |
| 30 |
} |
| 31 |
WP_Filesystem(); |
| 32 |
|
| 33 |
$api_url ="https://app.backupforwp.com/public"; |
| 34 |
|
| 35 |
$token = get_option('wpdb_clouddrive_token') ? get_option('wpdb_clouddrive_token') : ''; |
| 36 |
|
| 37 |
if(!$token){ |
| 38 |
return array('success' => false, 'message' => esc_html__('Cloud Backup token not found. Please enter your Cloud Backup token in the settings.', 'wpdbbkp')); |
| 39 |
} |
| 40 |
|
| 41 |
$upload_auth_token = 'Bearer '.$token; |
| 42 |
$upload_url = $api_url . '/api/v1/file/upload'; |
| 43 |
|
| 44 |
|
| 45 |
if (!$wp_filesystem) { |
| 46 |
return array('success' => false, 'message' => esc_html__('Unable to initialize wp_filesystem : ' , 'wpdbbkp'). $file_path); |
| 47 |
} |
| 48 |
|
| 49 |
if (!$wp_filesystem->exists($file_path)) { |
| 50 |
return array('success' => false, 'message' => esc_html__('File does not exist: ' , 'wpdbbkp'). $file_path); |
| 51 |
} |
| 52 |
|
| 53 |
$file_size = filesize($file_path); |
| 54 |
|
| 55 |
$file_contents = $wp_filesystem->get_contents( $file_path ); |
| 56 |
|
| 57 |
if ($file_contents === false) { |
| 58 |
return array('success' => false, 'message' => esc_html__('Failed to read file: ', 'wpdbbkp') . $file_path); |
| 59 |
} |
| 60 |
|
| 61 |
$root_path = str_replace('\\', '/', ABSPATH); // Normalize to forward slashes for consistency |
| 62 |
$file_path = str_replace($root_path, '', $file_path); |
| 63 |
$file_path = ltrim($file_path, '/'); // Ensure there is no leading slash |
| 64 |
|
| 65 |
|
| 66 |
$file_data = $file_contents; |
| 67 |
|
| 68 |
$boundary = wp_generate_password( 24 ); |
| 69 |
|
| 70 |
$headers = array( |
| 71 |
'Authorization' => $upload_auth_token, |
| 72 |
'domain'=> parse_url(get_site_url(), PHP_URL_HOST), |
| 73 |
'Content-Type' => 'multipart/form-data; boundary=' . $boundary, |
| 74 |
); |
| 75 |
|
| 76 |
$body = "--{$boundary}\r\n"; |
| 77 |
$body .= 'Content-Disposition: form-data; name="file"; filename="' . $file_name . '"' . "\r\n"; |
| 78 |
$body .= "file_contents-Type: application/octet-stream\r\n\r\n"; |
| 79 |
$body .= $file_data . "\r\n"; |
| 80 |
$body .= "--{$boundary}\r\n"; |
| 81 |
$body .= 'Content-Disposition: form-data; name="filename"' . "\r\n\r\n"; |
| 82 |
$body .= $file_path . "\r\n"; |
| 83 |
$body .= "--{$boundary}\r\n"; |
| 84 |
|
| 85 |
$response = wp_remote_post( $upload_url, array( |
| 86 |
'headers' => $headers, |
| 87 |
'body' => $body, |
| 88 |
'timeout' => 300, |
| 89 |
) ); |
| 90 |
|
| 91 |
|
| 92 |
if (is_wp_error($response)) { |
| 93 |
return array('success' => false, 'message' => esc_html__('Upload request failed: ', 'wpdbbkp') . $response->get_error_message()); |
| 94 |
} |
| 95 |
|
| 96 |
$response_code = wp_remote_retrieve_response_code($response); |
| 97 |
if ($response_code != 200) { |
| 98 |
$response_body = wp_remote_retrieve_body($response); |
| 99 |
return array('success' => false, 'message' => esc_html__('Failed to upload ' , 'wpdbbkp'). $file_name . ' to Cloud Backup. Response: ' . $response_body); |
| 100 |
} |
| 101 |
|
| 102 |
return array('success' => true, 'message' => 'File ' . $file_name . esc_html__(' uploaded successfully to Cloud Backup.', 'wpdbbkp')); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Run after complete backup. |
| 108 |
* |
| 109 |
* @param array $args - backup details. |
| 110 |
*/ |
| 111 |
|
| 112 |
public static function wp_db_backup_completed( &$args ) { |
| 113 |
$destination_cd = get_option( 'wpdb_clouddrive_token' , false); |
| 114 |
if ( $destination_cd && !empty($destination_cd)) { |
| 115 |
|
| 116 |
try { |
| 117 |
|
| 118 |
$ret = WPDatabaseBackupCD::upload_backup_to_clouddrive($args[1], $args[1]); |
| 119 |
$args[2] = $args[2] .$ret['message']; |
| 120 |
if ($ret['success']) { |
| 121 |
$args[4] .= 'CloudDrive, '; |
| 122 |
} |
| 123 |
return $ret; |
| 124 |
} catch ( Exception $e ) { |
| 125 |
$args[2] = $args[2] . "<br>".esc_html__("Failed to upload Database Backup on Cloud Backup", 'wpdbbkp'); |
| 126 |
return $e; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
public // Function to handle the file upload |
| 132 |
function wp_db_handle_file_upload($file_path) { |
| 133 |
// Prepare file array for sideload |
| 134 |
$file_array = array( |
| 135 |
'name' => basename($file_path), |
| 136 |
'tmp_name' => $file_path, |
| 137 |
); |
| 138 |
|
| 139 |
// Include WordPress file handling functions |
| 140 |
require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 141 |
require_once(ABSPATH . 'wp-admin/includes/media.php'); |
| 142 |
|
| 143 |
// Handle the file upload |
| 144 |
$uploaded_file = media_handle_sideload($file_array, 0); |
| 145 |
|
| 146 |
if (is_wp_error($uploaded_file)) { |
| 147 |
return $uploaded_file; // Return the WP_Error object |
| 148 |
} |
| 149 |
|
| 150 |
return get_attached_file($uploaded_file); |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|