| Total Complexity | 86 |
| Total Lines | 725 |
| Duplicated Lines | 0 % |
| Changes | 23 | ||
| Bugs | 7 | Features | 0 |
Complex classes like Xcloner_Remote_Storage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Xcloner_Remote_Storage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class Xcloner_Remote_Storage { |
||
| 55 | |||
| 56 | private $gdrive_app_name = "XCloner Backup and Restore"; |
||
| 57 | |||
| 58 | private $storage_fields = array( |
||
| 59 | "option_prefix" => "xcloner_", |
||
| 60 | "ftp" => array( |
||
| 61 | "text" => "FTP", |
||
| 62 | "ftp_enable" => "int", |
||
| 63 | "ftp_hostname" => "string", |
||
| 64 | "ftp_port" => "int", |
||
| 65 | "ftp_username" => "string", |
||
| 66 | "ftp_password" => "raw", |
||
| 67 | "ftp_path" => "path", |
||
| 68 | "ftp_transfer_mode" => "int", |
||
| 69 | "ftp_ssl_mode" => "int", |
||
| 70 | "ftp_timeout" => "int", |
||
| 71 | "ftp_cleanup_days" => "float", |
||
| 72 | ), |
||
| 73 | "sftp" => array( |
||
| 74 | "text" => "SFTP", |
||
| 75 | "sftp_enable" => "int", |
||
| 76 | "sftp_hostname" => "string", |
||
| 77 | "sftp_port" => "int", |
||
| 78 | "sftp_username" => "string", |
||
| 79 | "sftp_password" => "raw", |
||
| 80 | "sftp_path" => "path", |
||
| 81 | "sftp_private_key" => "raw", |
||
| 82 | "sftp_timeout" => "int", |
||
| 83 | "sftp_cleanup_days" => "float", |
||
| 84 | ), |
||
| 85 | "aws" => array( |
||
| 86 | "text" => "S3", |
||
| 87 | "aws_enable" => "int", |
||
| 88 | "aws_key" => "string", |
||
| 89 | "aws_secret" => "raw", |
||
| 90 | "aws_endpoint" => "string", |
||
| 91 | "aws_region" => "string", |
||
| 92 | "aws_bucket_name" => "string", |
||
| 93 | "aws_prefix" => "string", |
||
| 94 | "aws_cleanup_days" => "float", |
||
| 95 | ), |
||
| 96 | "dropbox" => array( |
||
| 97 | "text" => "Dropbox", |
||
| 98 | "dropbox_enable" => "int", |
||
| 99 | "dropbox_access_token" => "string", |
||
| 100 | "dropbox_app_secret" => "raw", |
||
| 101 | "dropbox_prefix" => "string", |
||
| 102 | "dropbox_cleanup_days" => "float", |
||
| 103 | ), |
||
| 104 | "azure" => array( |
||
| 105 | "text" => "Azure BLOB", |
||
| 106 | "azure_enable" => "int", |
||
| 107 | "azure_account_name" => "string", |
||
| 108 | "azure_api_key" => "string", |
||
| 109 | "azure_container" => "string", |
||
| 110 | "azure_cleanup_days" => "float", |
||
| 111 | ), |
||
| 112 | "backblaze" => array( |
||
| 113 | "text" => "Backblaze", |
||
| 114 | "backblaze_enable" => "int", |
||
| 115 | "backblaze_account_id" => "string", |
||
| 116 | "backblaze_application_key" => "string", |
||
| 117 | "backblaze_bucket_name" => "string", |
||
| 118 | "backblaze_cleanup_days" => "float", |
||
| 119 | ), |
||
| 120 | |||
| 121 | "webdav" => array( |
||
| 122 | "text" => "WebDAV", |
||
| 123 | "webdav_enable" => "int", |
||
| 124 | "webdav_url" => "string", |
||
| 125 | "webdav_username" => "string", |
||
| 126 | "webdav_password" => "raw", |
||
| 127 | "webdav_target_folder" => "string", |
||
| 128 | "webdav_cleanup_days" => "float", |
||
| 129 | ), |
||
| 130 | |||
| 131 | "gdrive" => array( |
||
| 132 | "text" => "Google Drive", |
||
| 133 | "gdrive_enable" => "int", |
||
| 134 | "gdrive_access_code" => "string", |
||
| 135 | "gdrive_client_id" => "string", |
||
| 136 | "gdrive_client_secret" => "raw", |
||
| 137 | "gdrive_target_folder" => "string", |
||
| 138 | "gdrive_cleanup_days" => "float", |
||
| 139 | "gdrive_empty_trash" => "int", |
||
| 140 | ), |
||
| 141 | ); |
||
| 142 | |||
| 143 | private $aws_regions = array( |
||
| 144 | 'us-east-1' => 'US East (N. Virginia)', |
||
| 145 | 'us-east-2' => 'US East (Ohio)', |
||
| 146 | 'us-west-1' => 'US West (N. California)', |
||
| 147 | 'us-west-2' => 'US West (Oregon)', |
||
| 148 | 'ca-central-1' => 'Canada (Central)', |
||
| 149 | 'eu-west-1' => 'EU (Ireland)', |
||
| 150 | 'eu-central-1' => 'EU (Frankfurt)', |
||
| 151 | 'eu-west-2' => 'EU (London)', |
||
| 152 | 'ap-northeast-1' => 'Asia Pacific (Tokyo)', |
||
| 153 | 'ap-northeast-2' => 'Asia Pacific (Seoul)', |
||
| 154 | 'ap-southeast-1' => 'Asia Pacific (Singapore)', |
||
| 155 | 'ap-southeast-2' => 'Asia Pacific (Sydney)', |
||
| 156 | 'ap-south-1' => 'Asia Pacific (Mumbai)', |
||
| 157 | 'sa-east-1' => 'South America (São Paulo)' |
||
| 158 | ); |
||
| 159 | |||
| 160 | private $xcloner_sanitization; |
||
| 161 | private $xcloner_file_system; |
||
| 162 | private $logger; |
||
| 163 | private $xcloner; |
||
| 164 | |||
| 165 | public function __construct( Xcloner $xcloner_container ) { |
||
| 166 | $this->xcloner_sanitization = $xcloner_container->get_xcloner_sanitization(); |
||
| 167 | $this->xcloner_file_system = $xcloner_container->get_xcloner_filesystem(); |
||
| 168 | $this->logger = $xcloner_container->get_xcloner_logger()->withName( "xcloner_remote_storage" ); |
||
| 169 | $this->xcloner = $xcloner_container; |
||
| 170 | |||
| 171 | foreach($this->storage_fields as $main_key=>$array){ |
||
| 172 | |||
| 173 | if(is_array($array)) { |
||
| 174 | foreach ($array as $key => $type) { |
||
| 175 | |||
| 176 | if( $type == "raw") { |
||
| 177 | add_filter("pre_update_option_" . $this->storage_fields['option_prefix'] . $key, |
||
|
|
|||
| 178 | function ($value) { |
||
| 179 | |||
| 180 | return $this->simple_crypt($value, 'e'); |
||
| 181 | |||
| 182 | }, 10, 1); |
||
| 183 | |||
| 184 | add_filter("option_" . $this->storage_fields['option_prefix'] . $key, function ($value) { |
||
| 185 | |||
| 186 | return $this->simple_crypt($value, 'd'); |
||
| 187 | |||
| 188 | }, 10, 1); |
||
| 189 | } |
||
| 190 | |||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Encrypts and Decrypt a string based on openssl lib |
||
| 199 | * |
||
| 200 | * @param $string |
||
| 201 | * @param string $action |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | private function simple_crypt( $string, $action = 'e' ) { |
||
| 205 | // you may change these values to your own |
||
| 206 | $secret_key = NONCE_KEY; |
||
| 207 | $secret_iv = NONCE_SALT; |
||
| 208 | |||
| 209 | $output = $string; |
||
| 210 | $encrypt_method = "AES-256-CBC"; |
||
| 211 | $key = hash( 'sha256', $secret_key ); |
||
| 212 | $iv = substr( hash( 'sha256', $secret_iv ), 0, 16 ); |
||
| 213 | |||
| 214 | if( $action == 'e' && function_exists('openssl_encrypt')) { |
||
| 215 | $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) ); |
||
| 216 | } |
||
| 217 | else if( $action == 'd' && function_exists('openssl_decrypt') && base64_decode( $string )){ |
||
| 218 | $decrypt = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv ); |
||
| 219 | if($decrypt) { |
||
| 220 | //we check if decrypt was succesful |
||
| 221 | $output = $decrypt; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | return $output; |
||
| 226 | } |
||
| 227 | |||
| 228 | private function get_xcloner_container() { |
||
| 229 | return $this->xcloner_container; |
||
| 230 | } |
||
| 231 | |||
| 232 | public function get_available_storages() { |
||
| 233 | $return = array(); |
||
| 234 | foreach ( $this->storage_fields as $storage => $data ) { |
||
| 235 | $check_field = $this->storage_fields["option_prefix"] . $storage . "_enable"; |
||
| 236 | if ( get_option( $check_field ) ) { |
||
| 237 | $return[ $storage ] = $data['text']; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | return $return; |
||
| 242 | } |
||
| 243 | |||
| 244 | public function save( $action = "ftp" ) { |
||
| 245 | if ( ! $action ) { |
||
| 246 | return false; |
||
| 247 | } |
||
| 248 | |||
| 249 | $storage = $this->xcloner_sanitization->sanitize_input_as_string( $action ); |
||
| 250 | $this->logger->debug( sprintf( "Saving the remote storage %s options", strtoupper( $action ) ) ); |
||
| 251 | |||
| 252 | if ( is_array( $this->storage_fields[ $storage ] ) ) { |
||
| 253 | foreach ( $this->storage_fields[ $storage ] as $field => $validation ) { |
||
| 254 | $check_field = $this->storage_fields["option_prefix"] . $field; |
||
| 255 | $sanitize_method = "sanitize_input_as_" . $validation; |
||
| 256 | |||
| 257 | if ( ! isset( $_POST[ $check_field ] ) ) { |
||
| 258 | $_POST[ $check_field ] = 0; |
||
| 259 | } |
||
| 260 | |||
| 261 | if ( ! method_exists( $this->xcloner_sanitization, $sanitize_method ) ) { |
||
| 262 | $sanitize_method = "sanitize_input_as_string"; |
||
| 263 | } |
||
| 264 | |||
| 265 | $sanitized_value = $this->xcloner_sanitization->$sanitize_method( stripslashes( $_POST[ $check_field ] ) ); |
||
| 266 | update_option( $check_field, $sanitized_value ); |
||
| 267 | } |
||
| 268 | |||
| 269 | $this->xcloner->trigger_message( __( "%s storage settings saved.", 'xcloner-backup-and-restore' ), "success", $this->storage_fields[ $action ]['text'] ); |
||
| 270 | } |
||
| 271 | |||
| 272 | } |
||
| 273 | |||
| 274 | public function check( $action = "ftp" ) { |
||
| 275 | try { |
||
| 276 | $this->verify_filesystem( $action ); |
||
| 277 | $this->xcloner->trigger_message( __( "%s connection is valid.", 'xcloner-backup-and-restore' ), "success", $this->storage_fields[ $action ]['text'] ); |
||
| 278 | $this->logger->debug( sprintf( "Connection to remote storage %s is valid", strtoupper( $action ) ) ); |
||
| 279 | } catch ( Exception $e ) { |
||
| 280 | $this->xcloner->trigger_message( "%s connection error: " . $e->getMessage(), "error", $this->storage_fields[ $action ]['text'] ); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $storage_type |
||
| 286 | */ |
||
| 287 | public function verify_filesystem( $storage_type ) { |
||
| 288 | $method = "get_" . $storage_type . "_filesystem"; |
||
| 289 | |||
| 290 | $this->logger->info( sprintf( "Checking validity of the remote storage %s filesystem", strtoupper( $storage_type ) ) ); |
||
| 291 | |||
| 292 | if ( ! method_exists( $this, $method ) ) { |
||
| 293 | return false; |
||
| 294 | } |
||
| 295 | |||
| 296 | list( $adapter, $filesystem ) = $this->$method(); |
||
| 297 | |||
| 298 | $test_file = substr( ".xcloner_" . md5( time() ), 0, 15 ); |
||
| 299 | |||
| 300 | if ( $storage_type == "gdrive" ) { |
||
| 301 | if ( ! is_array( $filesystem->listContents() ) ) { |
||
| 302 | throw new Exception( __( "Could not read data", 'xcloner-backup-and-restore' ) ); |
||
| 303 | } |
||
| 304 | $this->logger->debug( sprintf( "I can list data from remote storage %s", strtoupper( $storage_type ) ) ); |
||
| 305 | |||
| 306 | return true; |
||
| 307 | } |
||
| 308 | |||
| 309 | //testing write access |
||
| 310 | if ( ! $filesystem->write( $test_file, "data" ) ) { |
||
| 311 | throw new Exception( __( "Could not write data", 'xcloner-backup-and-restore' ) ); |
||
| 312 | } |
||
| 313 | $this->logger->debug( sprintf( "I can write data to remote storage %s", strtoupper( $storage_type ) ) ); |
||
| 314 | |||
| 315 | //testing read access |
||
| 316 | if ( ! $filesystem->has( $test_file ) ) { |
||
| 317 | throw new Exception( __( "Could not read data", 'xcloner-backup-and-restore' ) ); |
||
| 318 | } |
||
| 319 | $this->logger->debug( sprintf( "I can read data to remote storage %s", strtoupper( $storage_type ) ) ); |
||
| 320 | |||
| 321 | //delete test file |
||
| 322 | if ( ! $filesystem->delete( $test_file ) ) { |
||
| 323 | throw new Exception( __( "Could not delete data", 'xcloner-backup-and-restore' ) ); |
||
| 324 | } |
||
| 325 | $this->logger->debug( sprintf( "I can delete data to remote storage %s", strtoupper( $storage_type ) ) ); |
||
| 326 | |||
| 327 | return true; |
||
| 328 | } |
||
| 329 | |||
| 330 | public function upload_backup_to_storage( $file, $storage ) { |
||
| 331 | if ( ! $this->xcloner_file_system->get_storage_filesystem()->has( $file ) ) { |
||
| 332 | $this->logger->info( sprintf( "File not found %s in local storage", $file ) ); |
||
| 333 | |||
| 334 | return false; |
||
| 335 | } |
||
| 336 | |||
| 337 | $method = "get_" . $storage . "_filesystem"; |
||
| 338 | |||
| 339 | if ( ! method_exists( $this, $method ) ) { |
||
| 340 | return false; |
||
| 341 | } |
||
| 342 | |||
| 343 | list( $remote_storage_adapter, $remote_storage_filesystem ) = $this->$method(); |
||
| 344 | |||
| 345 | //doing remote storage cleaning here |
||
| 346 | $this->clean_remote_storage( $storage, $remote_storage_filesystem ); |
||
| 347 | |||
| 348 | $this->logger->info( sprintf( "Transferring backup %s to remote storage %s", $file, strtoupper( $storage ) ), array( "" ) ); |
||
| 349 | |||
| 350 | /*if(!$this->xcloner_file_system->get_storage_filesystem()->has($file)) |
||
| 351 | { |
||
| 352 | $this->logger->info(sprintf("File not found %s in local storage", $file)); |
||
| 353 | return false; |
||
| 354 | }*/ |
||
| 355 | |||
| 356 | $backup_file_stream = $this->xcloner_file_system->get_storage_filesystem()->readStream($file); |
||
| 357 | |||
| 358 | if (!$remote_storage_filesystem->writeStream($file, $backup_file_stream)) { |
||
| 359 | $this->logger->info(sprintf("Could not transfer file %s", $file)); |
||
| 360 | |||
| 361 | return false; |
||
| 362 | } |
||
| 363 | |||
| 364 | if ($this->xcloner_file_system->is_multipart($file)) { |
||
| 365 | $parts = $this->xcloner_file_system->get_multipart_files($file); |
||
| 366 | if (is_array($parts)) { |
||
| 367 | foreach ($parts as $part_file) { |
||
| 368 | $this->logger->info(sprintf("Transferring backup %s to remote storage %s", $part_file, strtoupper($storage)), array("")); |
||
| 369 | |||
| 370 | $backup_file_stream = $this->xcloner_file_system->get_storage_filesystem()->readStream($part_file); |
||
| 371 | if (!$remote_storage_filesystem->writeStream($part_file, $backup_file_stream)) { |
||
| 372 | return false; |
||
| 373 | } |
||
| 374 | } |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | $this->logger->info(sprintf("Upload done, disconnecting from remote storage %s", strtoupper($storage))); |
||
| 379 | |||
| 380 | return true; |
||
| 381 | |||
| 382 | } |
||
| 383 | |||
| 384 | public function copy_backup_remote_to_local($file, $storage) { |
||
| 385 | $method = "get_".$storage."_filesystem"; |
||
| 386 | |||
| 387 | $target_filename = $file; |
||
| 388 | |||
| 389 | if (!method_exists($this, $method)) { |
||
| 390 | return false; |
||
| 391 | } |
||
| 392 | |||
| 393 | list($remote_storage_adapter, $remote_storage_filesystem) = $this->$method(); |
||
| 394 | |||
| 395 | if (!$remote_storage_filesystem->has($file)) { |
||
| 396 | $this->logger->info(sprintf("File not found %s in remote storage %s", $file, strtoupper($storage))); |
||
| 397 | |||
| 398 | return false; |
||
| 399 | } |
||
| 400 | |||
| 401 | if ($storage == "gdrive") { |
||
| 402 | $metadata = $remote_storage_filesystem->getMetadata($file); |
||
| 403 | $target_filename = $metadata['filename'].".".$metadata['extension']; |
||
| 404 | } |
||
| 405 | |||
| 406 | $this->logger->info(sprintf("Transferring backup %s to local storage from %s storage", $file, strtoupper($storage)), array("")); |
||
| 407 | |||
| 408 | $backup_file_stream = $remote_storage_filesystem->readStream($file); |
||
| 409 | |||
| 410 | if (!$this->xcloner_file_system->get_storage_filesystem()->writeStream($target_filename, $backup_file_stream)) { |
||
| 411 | $this->logger->info(sprintf("Could not transfer file %s", $file)); |
||
| 412 | |||
| 413 | return false; |
||
| 414 | } |
||
| 415 | |||
| 416 | if ($this->xcloner_file_system->is_multipart($target_filename)) { |
||
| 417 | $parts = $this->xcloner_file_system->get_multipart_files($file, $storage); |
||
| 418 | if (is_array($parts)) { |
||
| 419 | foreach ($parts as $part_file) { |
||
| 420 | $this->logger->info(sprintf("Transferring backup %s to local storage from %s storage", $part_file, strtoupper($storage)), array("")); |
||
| 421 | |||
| 422 | $backup_file_stream = $remote_storage_filesystem->readStream($part_file); |
||
| 423 | if (!$this->xcloner_file_system->get_storage_filesystem()->writeStream($part_file, $backup_file_stream)) { |
||
| 424 | return false; |
||
| 425 | } |
||
| 426 | } |
||
| 427 | } |
||
| 428 | } |
||
| 429 | |||
| 430 | $this->logger->info(sprintf("Upload done, disconnecting from remote storage %s", strtoupper($storage))); |
||
| 431 | |||
| 432 | return true; |
||
| 433 | |||
| 434 | } |
||
| 435 | |||
| 436 | public function clean_remote_storage($storage, $remote_storage_filesystem) { |
||
| 437 | $check_field = $this->storage_fields["option_prefix"].$storage."_cleanup_days"; |
||
| 438 | if ($expire_days = get_option($check_field)) { |
||
| 439 | $this->logger->info(sprintf("Doing %s remote storage cleanup for %s days limit", strtoupper($storage), $expire_days)); |
||
| 440 | $files = $remote_storage_filesystem->listContents(); |
||
| 441 | |||
| 442 | $current_timestamp = strtotime("-".$expire_days." days"); |
||
| 443 | |||
| 444 | if (is_array($files)) { |
||
| 445 | foreach ($files as $file) { |
||
| 446 | $file['timestamp'] = $remote_storage_filesystem->getTimestamp($file['path']); |
||
| 447 | |||
| 448 | if ($current_timestamp >= $file['timestamp']) { |
||
| 449 | $remote_storage_filesystem->delete($file['path']); |
||
| 450 | $this->logger->info("Deleting remote file ".$file['path']." matching rule", array( |
||
| 451 | "RETENTION LIMIT TIMESTAMP", |
||
| 452 | $file['timestamp']." =< ".$expire_days |
||
| 453 | )); |
||
| 454 | } |
||
| 455 | |||
| 456 | } |
||
| 457 | } |
||
| 458 | } |
||
| 459 | } |
||
| 460 | |||
| 461 | public function get_azure_filesystem() { |
||
| 462 | $this->logger->info(sprintf("Creating the AZURE BLOB remote storage connection"), array("")); |
||
| 463 | |||
| 464 | if (version_compare(phpversion(), '5.6.0', '<')) { |
||
| 465 | throw new Exception("AZURE BLOB requires PHP 5.6 to be installed!"); |
||
| 466 | } |
||
| 467 | |||
| 468 | if (!class_exists('XmlWriter')) { |
||
| 469 | throw new Exception("AZURE BLOB requires libxml PHP module to be installed with XmlWriter class enabled!"); |
||
| 470 | } |
||
| 471 | |||
| 472 | $endpoint = sprintf( |
||
| 473 | 'DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s', |
||
| 474 | get_option("xcloner_azure_account_name"), |
||
| 475 | get_option("xcloner_azure_api_key") |
||
| 476 | ); |
||
| 477 | |||
| 478 | $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($endpoint); |
||
| 479 | |||
| 480 | $adapter = new AzureAdapter($blobRestProxy, get_option("xcloner_azure_container")); |
||
| 481 | |||
| 482 | $filesystem = new Filesystem($adapter, new Config([ |
||
| 483 | 'disable_asserts' => true, |
||
| 484 | ])); |
||
| 485 | |||
| 486 | return array($adapter, $filesystem); |
||
| 487 | } |
||
| 488 | |||
| 489 | public function get_dropbox_filesystem() { |
||
| 504 | } |
||
| 505 | |||
| 506 | public function get_aws_filesystem() { |
||
| 507 | $this->logger->info(sprintf("Creating the S3 remote storage connection"), array("")); |
||
| 508 | |||
| 509 | if (version_compare(phpversion(), '5.6.0', '<')) { |
||
| 510 | throw new Exception("S3 class requires PHP 5.6 to be installed!"); |
||
| 511 | } |
||
| 512 | |||
| 513 | if (!class_exists('XmlWriter')) { |
||
| 514 | throw new Exception("AZURE BLOB requires libxml PHP module to be installed with XmlWriter class enabled!"); |
||
| 515 | } |
||
| 516 | |||
| 517 | |||
| 518 | $credentials = array( |
||
| 519 | 'credentials' => array( |
||
| 520 | 'key' => get_option("xcloner_aws_key"), |
||
| 521 | 'secret' => get_option("xcloner_aws_secret") |
||
| 522 | ), |
||
| 523 | 'region' => get_option("xcloner_aws_region"), |
||
| 524 | 'version' => 'latest', |
||
| 525 | ); |
||
| 526 | |||
| 527 | if (get_option('xcloner_aws_endpoint') != "" && !get_option("xcloner_aws_region")) { |
||
| 528 | |||
| 529 | $credentials['endpoint'] = get_option('xcloner_aws_endpoint'); |
||
| 530 | #$credentials['use_path_style_endpoint'] = true; |
||
| 531 | #$credentials['bucket_endpoint'] = false; |
||
| 532 | |||
| 533 | |||
| 534 | } |
||
| 535 | |||
| 536 | $client = new S3Client($credentials); |
||
| 537 | |||
| 538 | $adapter = new AwsS3Adapter($client, get_option("xcloner_aws_bucket_name"), get_option("xcloner_aws_prefix")); |
||
| 539 | $filesystem = new Filesystem($adapter, new Config([ |
||
| 540 | 'disable_asserts' => true, |
||
| 541 | ])); |
||
| 542 | |||
| 543 | return array($adapter, $filesystem); |
||
| 544 | } |
||
| 545 | |||
| 546 | public function get_backblaze_filesystem() { |
||
| 547 | $this->logger->info(sprintf("Creating the BACKBLAZE remote storage connection"), array("")); |
||
| 548 | |||
| 549 | if (version_compare(phpversion(), '5.6.0', '<')) { |
||
| 550 | throw new Exception("BACKBLAZE API requires PHP 5.6 to be installed!"); |
||
| 551 | } |
||
| 552 | |||
| 553 | |||
| 554 | $client = new B2Client(get_option("xcloner_backblaze_account_id"), get_option("xcloner_backblaze_application_key")); |
||
| 555 | $adapter = new BackblazeAdapter($client, get_option("xcloner_backblaze_bucket_name")); |
||
| 556 | |||
| 557 | $filesystem = new Filesystem($adapter, new Config([ |
||
| 558 | 'disable_asserts' => true, |
||
| 559 | ])); |
||
| 560 | |||
| 561 | return array($adapter, $filesystem); |
||
| 562 | } |
||
| 563 | |||
| 564 | public function get_webdav_filesystem() { |
||
| 565 | $this->logger->info(sprintf("Creating the WEBDAV remote storage connection"), array("")); |
||
| 566 | |||
| 567 | if (version_compare(phpversion(), '5.6.0', '<')) { |
||
| 568 | throw new Exception("WEBDAV API requires PHP 5.6 to be installed!"); |
||
| 569 | } |
||
| 570 | |||
| 571 | $settings = array( |
||
| 572 | 'baseUri' => get_option("xcloner_webdav_url"), |
||
| 573 | 'userName' => get_option("xcloner_webdav_username"), |
||
| 574 | 'password' => get_option("xcloner_webdav_password"), |
||
| 575 | //'proxy' => 'locahost:8888', |
||
| 576 | ); |
||
| 577 | |||
| 578 | |||
| 579 | $client = new SabreClient($settings); |
||
| 580 | $adapter = new WebDAVAdapter($client, get_option("xcloner_webdav_target_folder")); |
||
| 581 | $filesystem = new Filesystem($adapter, new Config([ |
||
| 582 | 'disable_asserts' => true, |
||
| 583 | ])); |
||
| 584 | |||
| 585 | return array($adapter, $filesystem); |
||
| 586 | } |
||
| 587 | |||
| 588 | |||
| 589 | public function gdrive_construct() { |
||
| 590 | |||
| 591 | //if((function_exists("is_plugin_active") && !is_plugin_active("xcloner-google-drive/xcloner-google-drive.php")) || !file_exists(__DIR__ . "/../../xcloner-google-drive/vendor/autoload.php")) |
||
| 592 | if (!class_exists('Google_Client')) { |
||
| 593 | return false; |
||
| 594 | } |
||
| 595 | |||
| 596 | //require_once(__DIR__ . "/../../xcloner-google-drive/vendor/autoload.php"); |
||
| 597 | |||
| 598 | $client = new \Google_Client(); |
||
| 599 | $client->setApplicationName($this->gdrive_app_name); |
||
| 600 | $client->setClientId(get_option("xcloner_gdrive_client_id")); |
||
| 601 | $client->setClientSecret(get_option("xcloner_gdrive_client_secret")); |
||
| 602 | |||
| 603 | //$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']."?page=xcloner_remote_storage_page&action=set_gdrive_code"; |
||
| 604 | $redirect_uri = "urn:ietf:wg:oauth:2.0:oob"; |
||
| 605 | |||
| 606 | $client->setRedirectUri($redirect_uri); //urn:ietf:wg:oauth:2.0:oob |
||
| 607 | $client->addScope("https://www.googleapis.com/auth/drive"); |
||
| 608 | $client->setAccessType('offline'); |
||
| 609 | |||
| 610 | return $client; |
||
| 611 | } |
||
| 612 | |||
| 613 | public function get_gdrive_auth_url() { |
||
| 614 | $client = $this->gdrive_construct(); |
||
| 615 | |||
| 616 | if (!$client) { |
||
| 617 | return false; |
||
| 618 | } |
||
| 619 | |||
| 620 | return $authUrl = $client->createAuthUrl(); |
||
| 621 | } |
||
| 622 | |||
| 623 | public function set_access_token($code) { |
||
| 624 | $client = $this->gdrive_construct(); |
||
| 625 | |||
| 626 | if (!$client) { |
||
| 627 | $error_msg = "Could not initialize the Google Drive Class, please check that the xcloner-google-drive plugin is enabled..."; |
||
| 628 | $this->logger->error($error_msg); |
||
| 629 | |||
| 630 | return false; |
||
| 631 | } |
||
| 632 | |||
| 633 | $token = $client->fetchAccessTokenWithAuthCode($code); |
||
| 634 | $client->setAccessToken($token); |
||
| 635 | |||
| 636 | update_option("xcloner_gdrive_access_token", $token['access_token']); |
||
| 637 | update_option("xcloner_gdrive_refresh_token", $token['refresh_token']); |
||
| 638 | |||
| 639 | $redirect_url = ('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?page=xcloner_remote_storage_page#gdrive"); |
||
| 640 | |||
| 641 | ?> |
||
| 642 | <script> |
||
| 643 | window.location = '<?php echo $redirect_url?>'; |
||
| 644 | </script> |
||
| 645 | <?php |
||
| 646 | |||
| 647 | } |
||
| 648 | |||
| 649 | /* |
||
| 650 | * php composer.phar remove nao-pon/flysystem-google-drive |
||
| 651 | * |
||
| 652 | */ |
||
| 653 | public function get_gdrive_filesystem() { |
||
| 654 | |||
| 655 | if (version_compare(phpversion(), '5.6.0', '<')) { |
||
| 656 | throw new Exception("Google Drive API requires PHP 5.6 to be installed!"); |
||
| 657 | } |
||
| 658 | |||
| 659 | $this->logger->info(sprintf("Creating the Google Drive remote storage connection"), array("")); |
||
| 660 | |||
| 661 | $client = $this->gdrive_construct(); |
||
| 662 | |||
| 663 | if (!$client) { |
||
| 664 | $error_msg = "Could not initialize the Google Drive Class, please check that the xcloner-google-drive plugin is enabled..."; |
||
| 665 | $this->logger->error($error_msg); |
||
| 666 | throw new Exception($error_msg); |
||
| 667 | } |
||
| 668 | |||
| 669 | $client->refreshToken(get_option("xcloner_gdrive_refresh_token")); |
||
| 670 | |||
| 671 | $service = new \Google_Service_Drive($client); |
||
| 672 | |||
| 673 | if (get_option("xcloner_gdrive_empty_trash", 0)) { |
||
| 674 | $this->logger->info(sprintf("Doing a Google Drive emptyTrash call"), array("")); |
||
| 675 | $service->files->emptyTrash(); |
||
| 676 | } |
||
| 677 | |||
| 678 | $parent = 'root'; |
||
| 679 | $dir = basename(get_option("xcloner_gdrive_target_folder")); |
||
| 680 | |||
| 681 | $folderID = get_option("xcloner_gdrive_target_folder"); |
||
| 682 | |||
| 683 | $tmp = parse_url($folderID); |
||
| 684 | |||
| 685 | if (isset($tmp['query'])) { |
||
| 686 | $folderID = str_replace("id=", "", $tmp['query']); |
||
| 687 | } |
||
| 688 | |||
| 689 | if (stristr($folderID, "/")) { |
||
| 690 | $query = sprintf('mimeType = \'application/vnd.google-apps.folder\' and \'%s\' in parents and name contains \'%s\'', $parent, $dir); |
||
| 691 | $response = $service->files->listFiles([ |
||
| 692 | 'pageSize' => 1, |
||
| 693 | 'q' => $query |
||
| 694 | ]); |
||
| 695 | |||
| 696 | if (sizeof($response)) { |
||
| 697 | foreach ($response as $obj) { |
||
| 698 | $folderID = $obj->getId(); |
||
| 699 | } |
||
| 700 | } else { |
||
| 701 | $this->xcloner->trigger_message(sprintf(__("Could not find folder ID by name %s", 'xcloner-backup-and-restore'), $folderID), "error"); |
||
| 702 | } |
||
| 703 | } |
||
| 704 | |||
| 705 | $this->logger->info(sprintf("Using target folder with ID %s on the remote storage", $folderID)); |
||
| 706 | |||
| 707 | if (class_exists('XCloner_Google_Drive_Adapter')) { |
||
| 708 | $adapter = new XCloner_Google_Drive_Adapter($service, $folderID); |
||
| 709 | } else { |
||
| 710 | $adapter = new \Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter($service, $folderID); |
||
| 711 | } |
||
| 712 | |||
| 713 | $filesystem = new \League\Flysystem\Filesystem($adapter, new Config([ |
||
| 714 | 'disable_asserts' => true, |
||
| 715 | ])); |
||
| 716 | |||
| 717 | |||
| 718 | return array($adapter, $filesystem); |
||
| 719 | } |
||
| 720 | |||
| 721 | public function get_ftp_filesystem() { |
||
| 722 | $this->logger->info(sprintf("Creating the FTP remote storage connection"), array("")); |
||
| 723 | |||
| 724 | $adapter = new Adapter([ |
||
| 725 | 'host' => get_option("xcloner_ftp_hostname"), |
||
| 726 | 'username' => get_option("xcloner_ftp_username"), |
||
| 727 | 'password' => get_option("xcloner_ftp_password"), |
||
| 728 | |||
| 729 | /** optional config settings */ |
||
| 730 | 'port' => get_option("xcloner_ftp_port", 21), |
||
| 731 | 'root' => get_option("xcloner_ftp_path"), |
||
| 732 | 'passive' => get_option("xcloner_ftp_transfer_mode"), |
||
| 733 | 'ssl' => get_option("xcloner_ftp_ssl_mode"), |
||
| 734 | 'timeout' => get_option("xcloner_ftp_timeout", 30), |
||
| 735 | ]); |
||
| 736 | |||
| 737 | $adapter->connect(); |
||
| 738 | |||
| 739 | $filesystem = new Filesystem($adapter, new Config([ |
||
| 740 | 'disable_asserts' => true, |
||
| 741 | ])); |
||
| 742 | |||
| 743 | return array($adapter, $filesystem); |
||
| 744 | } |
||
| 745 | |||
| 746 | public function get_sftp_filesystem() { |
||
| 747 | $this->logger->info(sprintf("Creating the SFTP remote storage connection"), array("")); |
||
| 748 | |||
| 749 | $adapter = new SftpAdapter([ |
||
| 750 | 'host' => get_option("xcloner_sftp_hostname"), |
||
| 751 | 'username' => get_option("xcloner_sftp_username"), |
||
| 752 | 'password' => get_option("xcloner_sftp_password"), |
||
| 753 | |||
| 754 | /** optional config settings */ |
||
| 755 | 'port' => get_option("xcloner_sftp_port", 22), |
||
| 756 | 'root' => get_option("xcloner_sftp_path"), |
||
| 757 | 'privateKey' => get_option("xcloner_sftp_private_key"), |
||
| 758 | 'timeout' => get_option("xcloner_ftp_timeout", 30), |
||
| 759 | ]); |
||
| 760 | |||
| 761 | $adapter->connect(); |
||
| 762 | |||
| 763 | $filesystem = new Filesystem($adapter, new Config([ |
||
| 764 | 'disable_asserts' => true, |
||
| 765 | ])); |
||
| 766 | |||
| 767 | return array($adapter, $filesystem); |
||
| 768 | } |
||
| 769 | |||
| 770 | public function change_storage_status($field, $value) { |
||
| 771 | $field = $this->xcloner_sanitization->sanitize_input_as_string($field); |
||
| 772 | $value = $this->xcloner_sanitization->sanitize_input_as_int($value); |
||
| 773 | |||
| 774 | return update_option($field, $value); |
||
| 775 | } |
||
| 776 | |||
| 777 | public function get_aws_regions() { |
||
| 779 | } |
||
| 780 | |||
| 781 | } |
||
| 782 |