/
var
/
www
/
barefootlaw.org
/
wp-content
/
plugins
/
wp-health
/
wpu-backup
/
src
/
Api
/
Upload File
HOME
<?php namespace WPUmbrellaBackup\Api; use WPUmbrellaBackup\Api\BaseClient; class Backup extends BaseClient { /** * Upload directly on the cloud storage * @param string $signedUrl * @param string $filename * @return void */ public function postBackupBySignedUrl($signedUrl, $filename) { $directorySuffix = get_option('wp_umbrella_backup_suffix_security'); $filePath = @realpath(sprintf('%s/%s/%s', WP_UMBRELLA_DIR_WPU_BACKUP_BOX, $directorySuffix, $filename)); if (!file_exists($filePath)) { return ['success' => false]; } try { $curl = curl_init(); curl_setopt($curl, CURLOPT_PUT, 1); curl_setopt($curl, CURLOPT_INFILESIZE, filesize($filePath)); curl_setopt($curl, CURLOPT_INFILE, ($in = fopen($filePath, 'r'))); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/zip']); curl_setopt($curl, CURLOPT_URL, $signedUrl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($curl); if (!empty($response)) { return [ 'success' => true ]; } return [ 'success' => false ]; } catch (\Exception $e) { error_log($e->getMessage()); return [ 'success' => false ]; } } /** * * @param string $filename * @return string */ public function getSignedUrlForUpload($filename) { try { $url = sprintf('/v1/projects/%s/backups/signed-url?filename=%s', PROJECT_ID, $filename); $response = $this->getClient()->get($url); $body = json_decode($response->getBody()->getContents(), true); if (!$body['success']) { return null; } return isset($body['result']['signed_url']) ? $body['result']['signed_url'] : null; } catch (\Exception $e) { return null; } } public function putUpdateBackupData($data) { try { $url = sprintf('/v1/projects/%s/backups/%s', PROJECT_ID, $data['backupId']); $response = $this->getClient()->put($url, [ 'json' => $data ]); return json_decode($response->getBody()->getContents(), true); } catch (\Exception $e) { error_log($e->getMessage()); return null; } } public function postErrorBackup($data) { try { $url = sprintf('/v1/projects/%s/backups/%s/error', PROJECT_ID, $data['backupId']); $response = $this->getClient()->post($url, [ 'json' => $data ]); return json_decode($response->getBody()->getContents(), true); } catch (\Exception $e) { return null; } } public function postFinishBackup($data) { try { $url = sprintf('/v1/projects/%s/backups/%s/finish', PROJECT_ID, $data['backupId']); $response = $this->getClient()->post($url, [ 'json' => $data ]); return json_decode($response->getBody()->getContents(), true); } catch (\Exception $e) { error_log($e->getMessage()); return null; } } }