PluginProbe
WP Database Backup – Unlimited Database & Files Backup by Backup for WP / 7.7
WP Database Backup – Unlimited Database & Files Backup by Backup for WP v7.7
7.13 7.12 trunk 1.1 2.1.1 5.9 6.0 6.1 6.10 6.11 6.12 6.12.1 6.2 6.3 6.4 6.5 6.5.1 6.6 6.7 6.8 6.9 7.0 7.0.1 7.1 7.10 All 34 releases
wp-database-backup / includes / admin / Destination / CloudDrive / class-wpdatabasebackupcd.php

class-wpdatabasebackupcd.php in WP Database Backup – Unlimited Database & Files Backup by Backup for WP 7.7, at includes/admin/Destination/CloudDrive/class-wpdatabasebackupcd.php

152 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = apply_filters('wpdbbkp_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 $root_path_1 = ABSPATH;
61 $root_path_2 = str_replace('\\', '/', ABSPATH);
62 $file_path = str_replace(array($root_path,$root_path_1), array('',''), $file_path);
63 $file_path = ltrim($file_path, '/'); // Ensure there is no leading slash
64
65 $file_data = $file_contents;
66
67 $boundary = wp_generate_password( 24 );
68
69 $headers = array(
70 'Authorization' => $upload_auth_token,
71 'domain'=> parse_url(get_site_url(), PHP_URL_HOST),
72 'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
73 );
74
75 $body = "--{$boundary}\r\n";
76 $body .= 'Content-Disposition: form-data; name="file"; filename="' . $file_name . '"' . "\r\n";
77 $body .= "file_contents-Type: application/octet-stream\r\n\r\n";
78 $body .= $file_data . "\r\n";
79 $body .= "--{$boundary}\r\n";
80 $body .= 'Content-Disposition: form-data; name="filename"' . "\r\n\r\n";
81 $body .= $file_path . "\r\n";
82 $body .= "--{$boundary}\r\n";
83
84 $response = wp_remote_post( $upload_url, array(
85 'headers' => $headers,
86 'body' => $body,
87 'timeout' => 300,
88 ) );
89
90 if (is_wp_error($response)) {
91 return array('success' => false, 'message' => esc_html__('Upload request failed: ', 'wpdbbkp') . $response->get_error_message());
92 }
93
94 $response_code = wp_remote_retrieve_response_code($response);
95 if ($response_code != 200) {
96 $response_body = wp_remote_retrieve_body($response);
97 return array('success' => false, 'message' => esc_html__('Failed to upload ' , 'wpdbbkp'). $file_name . ' to Cloud Backup. Response: ' . $response_body);
98 }
99
100 return array('success' => true, 'message' => 'File ' . $file_name . esc_html__(' uploaded successfully to Cloud Backup.', 'wpdbbkp'));
101 }
102
103
104 /**
105 * Run after complete backup.
106 *
107 * @param array $args - backup details.
108 */
109
110 public static function wp_db_backup_completed( &$args ) {
111 $destination_cd = get_option( 'wpdb_clouddrive_token' , false);
112 if ( $destination_cd && !empty($destination_cd)) {
113
114 try {
115
116 $ret = WPDatabaseBackupCD::upload_backup_to_clouddrive($args[1], $args[1]);
117 $args[2] = $args[2] .$ret['message'];
118 if ($ret['success']) {
119 $args[4] .= 'CloudDrive, ';
120 }
121 return $ret;
122 } catch ( Exception $e ) {
123 $args[2] = $args[2] . "<br>".esc_html__("Failed to upload Database Backup on Cloud Backup", 'wpdbbkp');
124 return $e;
125 }
126 }
127 }
128
129 public // Function to handle the file upload
130 function wp_db_handle_file_upload($file_path) {
131 // Prepare file array for sideload
132 $file_array = array(
133 'name' => basename($file_path),
134 'tmp_name' => $file_path,
135 );
136
137 // Include WordPress file handling functions
138 require_once(ABSPATH . 'wp-admin/includes/file.php');
139 require_once(ABSPATH . 'wp-admin/includes/media.php');
140
141 // Handle the file upload
142 $uploaded_file = media_handle_sideload($file_array, 0);
143
144 if (is_wp_error($uploaded_file)) {
145 return $uploaded_file; // Return the WP_Error object
146 }
147
148 return get_attached_file($uploaded_file);
149 }
150
151 }
152