/** * Downloads backup file from Amazon S3 to root folder on local server. * * @param array $args arguments passed to the function * [as3_bucket_region] -> Amazon S3 bucket region * [as3_bucket] -> Amazon S3 bucket * [as3_access_key] -> Amazon S3 access key * [as3_secure_key] -> Amazon S3 secure key * [as3_directory] -> folder on user's Amazon S3 account which backup file should be downloaded from * [as3_site_folder] -> subfolder with site name in as3_directory which backup file should be downloaded from * [backup_file] -> absolute path of backup file on local server * * @return bool|array absolute path to downloaded file is successful, array with error message if not */ public function get_amazons3_backup($args) { if (!mwp_container()->getSystemEnvironment()->isCurlEnabled()) { throw new MWP_Worker_Exception(MWP_Worker_Exception::PHP_EXTENSION_REQUIRED_CURL, 'The cURL PHP extension is required for Amazon S3 backup functionality to work. Please, enquire your hosting provider on how to enable that extension.'); } $endpoint = isset($args['as3_bucket_region']) ? $args['as3_bucket_region'] : 's3.amazonaws.com'; mwp_logger()->info('Downloading the backup file from Amazon S3', array('directory' => $args['as3_directory'], 'bucket' => $args['as3_bucket'], 'endpoint' => $args['endpoint'], 'backup_file' => $args['backup_file'])); if ($args['as3_site_folder'] == true) { $args['as3_directory'] .= '/' . $this->site_name; } $start = microtime(true); try { $s3 = new S3_Client($args['as3_access_key'], str_replace(' ', '+', $args['as3_secure_key']), false, $endpoint); $s3->setExceptions(true); $temp = ABSPATH . 'mwp_temp_backup.zip'; if (version_compare(PHP_VERSION, '5.3', '>=')) { $progress = new MWP_Progress_Download(3, mwp_logger()); $s3->setProgressCallback($progress->getCallback()); } $s3->getObject($args['as3_bucket'], $args['as3_directory'] . '/' . $args['backup_file'], $temp); } catch (Exception $e) { mwp_logger()->error('Error while downloading the backup file', array('exception' => $e)); return array('error' => 'Error while downloading the backup file from Amazon S3: ' . $e->getMessage()); } $fileSize = filesize($temp); mwp_logger()->info('Downloading backup file from Amazon S3 completed; file size is {backup_size}; average speed is {speed}', array('backup_size' => mwp_format_bytes($fileSize), 'speed' => mwp_format_bytes($fileSize / (microtime(true) - $start)))); return $temp; }
/** * Get the S3 response * * @return object | false */ public function getResponse() { $query = ''; if (sizeof($this->parameters) > 0) { $query = substr($this->uri, -1) !== '?' ? '?' : '&'; foreach ($this->parameters as $var => $value) { if ($value == null || $value == '') { $query .= $var . '&'; } else { $query .= $var . '=' . rawurlencode($value) . '&'; } } $query = substr($query, 0, -1); $this->uri .= $query; if (array_key_exists('acl', $this->parameters) || array_key_exists('location', $this->parameters) || array_key_exists('torrent', $this->parameters) || array_key_exists('website', $this->parameters) || array_key_exists('logging', $this->parameters)) { $this->resource .= $query; } } $url = (S3_Client::$useSSL ? 'https://' : 'http://') . ($this->headers['Host'] !== '' ? $this->headers['Host'] : $this->endpoint) . $this->uri; //var_dump('bucket: ' . $this->bucket, 'uri: ' . $this->uri, 'resource: ' . $this->resource, 'url: ' . $url); // Basic setup $curl = curl_init(); curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php'); if (S3_Client::$useSSL) { // SSL Validation can now be optional for those with broken OpenSSL installations curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, S3_Client::$useSSLValidation ? 2 : 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, S3_Client::$useSSLValidation ? 1 : 0); if (S3_Client::$sslKey !== null) { curl_setopt($curl, CURLOPT_SSLKEY, S3_Client::$sslKey); } if (S3_Client::$sslCert !== null) { curl_setopt($curl, CURLOPT_SSLCERT, S3_Client::$sslCert); } if (S3_Client::$sslCACert !== null) { curl_setopt($curl, CURLOPT_CAINFO, S3_Client::$sslCACert); } } curl_setopt($curl, CURLOPT_URL, $url); if (S3_Client::$proxy != null && isset(S3_Client::$proxy['host'])) { curl_setopt($curl, CURLOPT_PROXY, S3_Client::$proxy['host']); curl_setopt($curl, CURLOPT_PROXYTYPE, S3_Client::$proxy['type']); if (isset(S3_Client::$proxy['user'], S3_Client::$proxy['pass']) && S3_Client::$proxy['user'] != null && S3_Client::$proxy['pass'] != null) { curl_setopt($curl, CURLOPT_PROXYUSERPWD, sprintf('%s:%s', S3_Client::$proxy['user'], S3_Client::$proxy['pass'])); } } // Headers $headers = array(); $amz = array(); foreach ($this->amzHeaders as $header => $value) { if (strlen($value) > 0) { $headers[] = $header . ': ' . $value; } } foreach ($this->headers as $header => $value) { if (strlen($value) > 0) { $headers[] = $header . ': ' . $value; } } // Collect AMZ headers for signature foreach ($this->amzHeaders as $header => $value) { if (strlen($value) > 0) { $amz[] = strtolower($header) . ':' . $value; } } // AMZ headers must be sorted if (sizeof($amz) > 0) { //sort($amz); usort($amz, array(&$this, '__sortMetaHeadersCmp')); $amz = "\n" . implode("\n", $amz); } else { $amz = ''; } if (S3_Client::hasAuth()) { // Authorization string (CloudFront stringToSign should only contain a date) if ($this->headers['Host'] == 'cloudfront.amazonaws.com') { $headers[] = 'Authorization: ' . S3_Client::__getSignature($this->headers['Date']); } else { $headers[] = 'Authorization: ' . S3_Client::__getSignature($this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource); } } curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback')); curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback')); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Request types switch ($this->verb) { case 'GET': break; case 'PUT': case 'POST': // POST only used for CloudFront if ($this->fp !== false) { curl_setopt($curl, CURLOPT_PUT, true); curl_setopt($curl, CURLOPT_INFILE, $this->fp); if ($this->size >= 0) { curl_setopt($curl, CURLOPT_INFILESIZE, $this->size); } } elseif ($this->data !== false) { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb); curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data); } else { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb); } break; case 'HEAD': curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD'); curl_setopt($curl, CURLOPT_NOBODY, true); break; case 'DELETE': curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; default: break; } if ($this->progressCallback !== null) { curl_setopt($curl, CURLOPT_NOPROGRESS, false); curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, $this->progressCallback); } // Execute, grab errors if (curl_exec($curl)) { $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE); } else { $this->response->error = array('code' => curl_errno($curl), 'message' => curl_error($curl), 'resource' => $this->resource); } @curl_close($curl); // Parse body into XML if ($this->response->error === false && isset($this->response->headers['type']) && $this->response->headers['type'] == 'application/xml' && isset($this->response->body)) { $this->response->body = simplexml_load_string($this->response->body); // Grab S3 errors if (!in_array($this->response->code, array(200, 204, 206)) && isset($this->response->body->Code, $this->response->body->Message)) { $this->response->error = array('code' => (string) $this->response->body->Code, 'message' => (string) $this->response->body->Message); if (isset($this->response->body->Resource)) { $this->response->error['resource'] = (string) $this->response->body->Resource; } unset($this->response->body); } } // Clean up file resources if ($this->fp !== false && is_resource($this->fp)) { fclose($this->fp); } return $this->response; }
/** * Correct time skew between client server and amazon * * @param S3_Request $rest */ public static function setCorrectDate(S3_Request $rest) { if (!self::$__timeCorrected) { self::setTimeCorrectionOffset(); self::$__timeCorrected = true; } $rest->setHeader('Date', gmdate('D, d M Y H:i:s T', self::__getTime())); }