| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined('ABSPATH') ) |
| 4 |
die(); |
| 5 |
|
| 6 |
/* |
| 7 |
Methods to define when extending this class (can use $this->storage and $this->options where relevant): |
| 8 |
do_bootstrap($possible_options_array) # Return a WP_Error object if something goes wrong |
| 9 |
do_upload($file, $sourcefile) # Return true/false |
| 10 |
do_listfiles($match) |
| 11 |
do_delete($file) - return true/false |
| 12 |
do_download($file, $fullpath, $start_offset) - return true/false |
| 13 |
do_config_print() |
| 14 |
do_credentials_test_parameters() - return an array: keys = required _POST parameters; values = description of each |
| 15 |
do_credentials_test($testfile, $posted_settings) - return true/false |
| 16 |
do_credentials_test_deletefile($testfile, $posted_settings) |
| 17 |
*/ |
| 18 |
|
| 19 |
// Uses job options: Yes |
| 20 |
// Uses single-array storage: Yes |
| 21 |
|
| 22 |
if (!class_exists('IWP_MMB_UploadModule')) require_once($GLOBALS['iwp_mmb_plugin_dir'].'/backup/backup.upload.php'); |
| 23 |
|
| 24 |
/** |
| 25 |
* Note that the naming of this class is historical. There is nothing inherent which restricts it to add-ons, or requires add-ons to use it. It is just an abstraction layer that results in needing to write less code for the storage module. |
| 26 |
*/ |
| 27 |
abstract class IWP_MMB_RemoteStorage_Extension extends IWP_MMB_UploadModule { |
| 28 |
|
| 29 |
protected $method; |
| 30 |
|
| 31 |
protected $description; |
| 32 |
|
| 33 |
protected $storage; |
| 34 |
|
| 35 |
protected $options; |
| 36 |
|
| 37 |
private $chunked; |
| 38 |
|
| 39 |
public $test_button; |
| 40 |
|
| 41 |
public function __construct($method, $description, $chunked = true, $test_button = true) { |
| 42 |
|
| 43 |
$this->method = $method; |
| 44 |
$this->description = $description; |
| 45 |
$this->chunked = $chunked; |
| 46 |
$this->test_button = $test_button; |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* download method: takes a file name (base name), and removes it from the cloud storage |
| 52 |
* |
| 53 |
* @param string $file specific file for being removed from cloud storage |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function download($file) { |
| 57 |
return $this->download_file(false, $file); |
| 58 |
} |
| 59 |
|
| 60 |
public function backup($backup_array) { |
| 61 |
return $this->upload_files(null, $backup_array); |
| 62 |
} |
| 63 |
|
| 64 |
public function delete($files, $method_obj = false, $sizeinfo = array()) { |
| 65 |
return $this->delete_files(false, $files, $method_obj, $sizeinfo); |
| 66 |
} |
| 67 |
|
| 68 |
protected function required_configuration_keys() { |
| 69 |
} |
| 70 |
|
| 71 |
public function upload_files($ret, $backup_array) { |
| 72 |
|
| 73 |
global $iwp_backup_core; |
| 74 |
|
| 75 |
$this->options = $this->get_options(); |
| 76 |
|
| 77 |
if (!$this->options_exist($this->options)) { |
| 78 |
$iwp_backup_core->log('No '.$this->method.' settings were found'); |
| 79 |
$iwp_backup_core->log(sprintf(__('No %s settings were found', 'InfiniteWP'), $this->description), 'error'); |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
$storage = $this->bootstrap(); |
| 84 |
if (is_wp_error($storage)) return $iwp_backup_core->log_wp_error($storage, false, true); |
| 85 |
|
| 86 |
$this->storage = $storage; |
| 87 |
|
| 88 |
$iwp_backup_dir = trailingslashit($iwp_backup_core->backups_dir_location()); |
| 89 |
|
| 90 |
foreach ($backup_array as $file) { |
| 91 |
$iwp_backup_core->log($this->method." upload ".((!empty($this->options['ownername'])) ? '(account owner: '.$this->options['ownername'].')' : '').": attempt: $file"); |
| 92 |
try { |
| 93 |
if ($this->do_upload($file, $iwp_backup_dir.$file)) { |
| 94 |
$iwp_backup_core->uploaded_file($file); |
| 95 |
} else { |
| 96 |
$any_failures = true; |
| 97 |
$iwp_backup_core->log('ERROR: '.$this->method.': Failed to upload file: '.$file); |
| 98 |
$iwp_backup_core->log(__('Error', 'InfiniteWP').': '.$this->description.': '.sprintf(__('Failed to upload %s', 'InfiniteWP'), $file), 'error'); |
| 99 |
} |
| 100 |
} catch (Exception $e) { |
| 101 |
$any_failures = true; |
| 102 |
$iwp_backup_core->log('ERROR ('.get_class($e).'): '.$this->method.": $file: Failed to upload file: ".$e->getMessage().' (code: '.$e->getCode().', line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 103 |
$iwp_backup_core->log(__('Error', 'InfiniteWP').': '.$this->description.': '.sprintf(__('Failed to upload %s', 'InfiniteWP'), $file), 'error'); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return (!empty($any_failures)) ? null : true; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
public function listfiles($match = 'backup_') { |
| 112 |
|
| 113 |
try { |
| 114 |
|
| 115 |
if (!method_exists($this, 'do_listfiles')) { |
| 116 |
return new WP_Error('no_listing', 'This remote storage method does not support file listing'); |
| 117 |
} |
| 118 |
|
| 119 |
$this->options = $this->get_options(); |
| 120 |
if (!$this->options_exist($this->options)) return new WP_Error('no_settings', sprintf(__('No %s settings were found', 'InfiniteWP'), $this->description)); |
| 121 |
|
| 122 |
$this->storage = $this->bootstrap(); |
| 123 |
if (is_wp_error($this->storage)) return $this->storage; |
| 124 |
|
| 125 |
return $this->do_listfiles($match); |
| 126 |
|
| 127 |
} catch (Exception $e) { |
| 128 |
global $iwp_backup_core; |
| 129 |
$iwp_backup_core->log('ERROR: '.$this->method.": Failed to list files: ".$e->getMessage().' (code: '.$e->getCode().', line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 130 |
return new WP_Error('list_failed', $this->description.': '.__('failed to list files', 'InfiniteWP')); |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
public function delete_files($ret, $files, $ignore_it = false) { |
| 136 |
|
| 137 |
global $iwp_backup_core; |
| 138 |
|
| 139 |
if (is_string($files)) $files = array($files); |
| 140 |
|
| 141 |
if (empty($files)) return true; |
| 142 |
if (!method_exists($this, 'do_delete')) { |
| 143 |
$iwp_backup_core->log($this->method.": Delete failed: this storage method does not allow deletions"); |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
if (empty($this->storage)) { |
| 148 |
|
| 149 |
$this->options = $this->get_options(); |
| 150 |
if (!$this->options_exist($this->options)) { |
| 151 |
$iwp_backup_core->log('No '.$this->method.' settings were found'); |
| 152 |
$iwp_backup_core->log(sprintf(__('No %s settings were found', 'InfiniteWP'), $this->description), 'error'); |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
$this->storage = $this->bootstrap(); |
| 157 |
if (is_wp_error($this->storage)) return $this->storage; |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
$ret = true; |
| 162 |
|
| 163 |
foreach ($files as $file) { |
| 164 |
$iwp_backup_core->log($this->method.": Delete remote: $file"); |
| 165 |
try { |
| 166 |
if (!$this->do_delete($file)) { |
| 167 |
$ret = false; |
| 168 |
$iwp_backup_core->log($this->method.": Delete failed"); |
| 169 |
} else { |
| 170 |
$iwp_backup_core->log($this->method.": $file: Delete succeeded"); |
| 171 |
} |
| 172 |
} catch (Exception $e) { |
| 173 |
$iwp_backup_core->log('ERROR: '.$this->method.": $file: Failed to delete file: ".$e->getMessage().' (code: '.$e->getCode().', line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 174 |
$ret = false; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return $ret; |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
public function download_file($ret, $files) { |
| 183 |
|
| 184 |
global $iwp_backup_core; |
| 185 |
|
| 186 |
if (is_string($files)) $files = array($files); |
| 187 |
|
| 188 |
if (empty($files)) return true; |
| 189 |
if (!method_exists($this, 'do_download')) { |
| 190 |
$iwp_backup_core->log($this->method.": Download failed: this storage method does not allow downloading"); |
| 191 |
$iwp_backup_core->log($this->description.': '.__('This storage method does not allow downloading', 'InfiniteWP'), 'error'); |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
$this->options = $this->get_options(); |
| 196 |
if (!$this->options_exist($this->options)) { |
| 197 |
$iwp_backup_core->log('No '.$this->method.' settings were found'); |
| 198 |
$iwp_backup_core->log(sprintf(__('No %s settings were found', 'InfiniteWP'), $this->description), 'error'); |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
try { |
| 203 |
$this->storage = $this->bootstrap(); |
| 204 |
if (is_wp_error($this->storage)) return $iwp_backup_core->log_wp_error($this->storage, false, true); |
| 205 |
} catch (Exception $e) { |
| 206 |
$ret = false; |
| 207 |
$iwp_backup_core->log('ERROR: '.$this->method.": $files[0]: Failed to download file: ".$e->getMessage().' (code: '.$e->getCode().', line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 208 |
$iwp_backup_core->log(__('Error', 'InfiniteWP').': '.$this->description.': '.sprintf(__('Failed to download %s', 'InfiniteWP'), $files[0]), 'error'); |
| 209 |
} |
| 210 |
|
| 211 |
$ret = true; |
| 212 |
$iwp_backup_dir = untrailingslashit($iwp_backup_core->backups_dir_location()); |
| 213 |
|
| 214 |
foreach ($files as $file) { |
| 215 |
try { |
| 216 |
$fullpath = $iwp_backup_dir.'/'.$file; |
| 217 |
$start_offset = file_exists($fullpath) ? filesize($fullpath) : 0; |
| 218 |
|
| 219 |
if (false == ($this->do_download($file, $fullpath, $start_offset))) { |
| 220 |
$ret = false; |
| 221 |
$iwp_backup_core->log($this->method." error: failed to download: $file"); |
| 222 |
$iwp_backup_core->log("$file: ".sprintf(__("%s Error", 'InfiniteWP'), $this->description).": ".__('Failed to download', 'InfiniteWP'), 'error'); |
| 223 |
} |
| 224 |
|
| 225 |
} catch (Exception $e) { |
| 226 |
$ret = false; |
| 227 |
$iwp_backup_core->log('ERROR: '.$this->method.": $file: Failed to download file: ".$e->getMessage().' (code: '.$e->getCode().', line: '.$e->getLine().', file: '.$e->getFile().')'); |
| 228 |
$iwp_backup_core->log(__('Error', 'InfiniteWP').': '.$this->description.': '.sprintf(__('Failed to download %s', 'InfiniteWP'), $file), 'error'); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return $ret; |
| 233 |
} |
| 234 |
|
| 235 |
public function config_print() { |
| 236 |
|
| 237 |
$this->options = $this->get_options(); |
| 238 |
$method = $this->method; |
| 239 |
} |
| 240 |
|
| 241 |
protected function options_exist($opts) { |
| 242 |
if (is_array($opts) && !empty($opts)) return true; |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
public function bootstrap($opts = false, $connect = true) { |
| 247 |
if (false === $opts) $opts = $this->options; |
| 248 |
// Be careful of checking empty($opts) here - some storage methods may have no options until the OAuth token has been obtained |
| 249 |
if ($connect && !$this->options_exist($opts)) return new WP_Error('no_settings', sprintf(__('No %s settings were found', 'InfiniteWP'), $this->description)); |
| 250 |
if (!empty($this->storage) && !is_wp_error($this->storage)) return $this->storage; |
| 251 |
return $this->do_bootstrap($opts, $connect); |
| 252 |
} |
| 253 |
|
| 254 |
} |
| 255 |
|