/** * Helper function to create "path" elements for a given Object name * * Given an Object whos name contains '/' path separators, this function * will create the "directory marker" Objects of one byte with the * Content-Type of "application/directory". * * It assumes the last element of the full path is the "real" Object * and does NOT create a remote storage Object for that last element. */ function create_paths($path_name) { if ($path_name[0] == '/') { $path_name = mb_substr($path_name, 0, 1); } $elements = explode('/', $path_name, -1); $build_path = ""; foreach ($elements as $idx => $val) { if (!$build_path) { $build_path = $val; } else { $build_path .= "/" . $val; } $obj = new UpdraftPlus_CF_Object($this, $build_path); $obj->content_type = "application/directory"; $obj->write(".", 1); } }
function download($file) { global $updraftplus; $updraft_dir = $updraftplus->backups_dir_location(); $user = UpdraftPlus_Options::get_updraft_option('updraft_cloudfiles_user'); $apikey = UpdraftPlus_Options::get_updraft_option('updraft_cloudfiles_apikey'); $authurl = UpdraftPlus_Options::get_updraft_option('updraft_cloudfiles_authurl'); try { $conn = $this->getCF($user, $apikey, $authurl, UpdraftPlus_Options::get_updraft_option('updraft_ssl_useservercerts')); } catch (AuthenticationException $e) { $updraftplus->log('Cloud Files authentication failed (' . $e->getMessage() . ')'); $updraftplus->log(sprintf(__('%s authentication failed', 'updraftplus'), 'Cloud Files') . ' (' . $e->getMessage() . ')', 'error'); return false; } catch (NoSuchAccountException $s) { $updraftplus->log('Cloud Files authentication failed (' . $e->getMessage() . ')'); $updraftplus->log(sprintf(__('%s authentication failed', 'updraftplus'), 'Cloud Files') . ' (' . $e->getMessage() . ')', 'error'); return false; } catch (Exception $e) { $updraftplus->log('Cloud Files error - failed to create and access the container (' . $e->getMessage() . ')'); $updraftplus->log(__('Cloud Files error - failed to create and access the container', 'updraftplus') . ' (' . $e->getMessage() . ')', 'error'); return; } $path = untrailingslashit(get_option('updraft_cloudfiles_path')); // if (preg_match("#^([^/]+)/(.*)$#", $path, $bmatches)) { // $container = $bmatches[1]; // $path = $bmatches[2]; // } else { // $container = $path; // $path = ""; // } $container = $path; try { $cont_obj = $conn->create_container($container); } catch (Exception $e) { $updraftplus->log('Cloud Files error - failed to create and access the container (' . $e->getMessage() . ')'); $updraftplus->log(__('Cloud Files error - failed to create and access the container', 'updraftplus') . ' (' . $e->getMessage() . ')', 'error'); return false; } // $path = ($path == '') ? $file : "$path/$file"; $path = $file; $updraftplus->log("Cloud Files download: cloudfiles://{$container}/{$path}"); try { // The third parameter causes an exception to be thrown if the object does not exist remotely $object = new UpdraftPlus_CF_Object($cont_obj, $path, true); $fullpath = $updraft_dir . '/' . $file; $start_offset = file_exists($fullpath) ? filesize($fullpath) : 0; // Get file size from remote - see if we've already finished $remote_size = $object->content_length; if ($start_offset >= $remote_size) { $updraftplus->log("Cloud Files: file is already completely downloaded ({$start_offset}/{$remote_size})"); return true; } // Some more remains to download - so let's do it if (!($fh = fopen($fullpath, 'a'))) { $updraftplus->log("Cloud Files: Error opening local file: {$fullpath}"); $updraftplus->log(sprintf("{$file}: " . __("%s Error", 'updraftplus'), 'Cloud Files') . ": " . __('Error opening local file: Failed to download', 'updraftplus'), 'error'); return false; } $headers = array(); // If resuming, then move to the end of the file if ($start_offset) { $updraftplus->log("Cloud Files: local file is already partially downloaded ({$start_offset}/{$remote_size})"); fseek($fh, $start_offset); $headers['Range'] = "bytes={$start_offset}-"; } // Now send the request itself try { $object->stream($fh, $headers); } catch (Exception $e) { $updraftplus->log("Cloud Files: Failed to download: {$file} (" . $e->getMessage() . ")"); $updraftplus->log("{$file}: " . sprintf(__("%s Error", 'updraftplus'), 'Cloud Files') . ": " . __('Error downloading remote file: Failed to download' . ' (' . $e->getMessage() . ")", 'updraftplus'), 'error'); return false; } // All-in-one-go method: // $object->save_to_filename($fullpath); } catch (NoSuchObjectException $e) { $updraftplus->log('Cloud Files error - no such file exists at Cloud Files (' . $e->getMessage() . ')'); $updraftplus->log(sprintf(__('Error - no such file exists at %s', 'updraftplus'), 'Cloud Files') . ' (' . $e->getMessage() . ')', 'error'); return false; } catch (Exception $e) { $updraftplus->log('Cloud Files error - failed to download the file (' . $e->getMessage() . ')'); $updraftplus->log(sprintf(__('Error - failed to download the file from %s', 'updraftplus'), 'Cloud Files') . ' (' . $e->getMessage() . ')', 'error'); return false; } }