function getS3($key, $secret, $useservercerts, $disableverify, $nossl) { global $updraftplus; if (!class_exists('UpdraftPlus_S3')) { require_once UPDRAFTPLUS_DIR . '/includes/S3.php'; } if (!class_exists('WP_HTTP_Proxy')) { require_once ABSPATH . 'wp-includes/class-http.php'; } $proxy = new WP_HTTP_Proxy(); $s3 = new UpdraftPlus_S3($key, $secret); if ($proxy->is_enabled()) { # WP_HTTP_Proxy returns empty strings where we want nulls $user = $proxy->username(); if (empty($user)) { $user = null; $pass = null; } else { $pass = $proxy->password(); if (empty($pass)) { $pass = null; } } $port = (int) $proxy->port(); if (empty($port)) { $port = 8080; } $s3->setProxy($proxy->host(), $user, $pass, CURLPROXY_HTTP, $port); } if (!$nossl) { $curl_version = function_exists('curl_version') ? curl_version() : array('features' => null); $curl_ssl_supported = $curl_version['features'] & CURL_VERSION_SSL; if ($curl_ssl_supported) { $s3->useSSL = true; if ($disableverify) { $s3->useSSLValidation = false; $updraftplus->log("S3: Disabling verification of SSL certificates"); } if ($useservercerts) { $updraftplus->log("S3: Using the server's SSL certificates"); } else { $s3->SSLCACert = UPDRAFTPLUS_DIR . '/includes/cacert.pem'; } } else { $updraftplus->log("S3: Curl/SSL is not available. Communications will not be encrypted."); } } else { $s3->useSSL = false; $updraftplus->log("SSL was disabled via the user's preference. Communications will not be encrypted."); } return $s3; }
/** * 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('logging', $this->parameters) || array_key_exists('partNumber', $this->parameters) || array_key_exists('uploads', $this->parameters) || array_key_exists('uploadId', $this->parameters)) { $this->resource .= $query; } } $url = (UpdraftPlus_S3::$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 (UpdraftPlus_S3::$useSSL) { // SSL Validation can now be optional for those with broken OpenSSL installations curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, UpdraftPlus_S3::$useSSLValidation ? 2 : 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, UpdraftPlus_S3::$useSSLValidation ? 1 : 0); if (UpdraftPlus_S3::$sslKey !== null) { curl_setopt($curl, CURLOPT_SSLKEY, UpdraftPlus_S3::$sslKey); } if (UpdraftPlus_S3::$sslCert !== null) { curl_setopt($curl, CURLOPT_SSLCERT, UpdraftPlus_S3::$sslCert); } if (UpdraftPlus_S3::$sslCACert !== null) { curl_setopt($curl, CURLOPT_CAINFO, UpdraftPlus_S3::$sslCACert); } } curl_setopt($curl, CURLOPT_URL, $url); $wp_proxy = new WP_HTTP_Proxy(); if (UpdraftPlus_S3::$proxy != null && isset(UpdraftPlus_S3::$proxy['host']) && $wp_proxy->send_through_proxy($url)) { curl_setopt($curl, CURLOPT_PROXY, UpdraftPlus_S3::$proxy['host']); curl_setopt($curl, CURLOPT_PROXYTYPE, UpdraftPlus_S3::$proxy['type']); if (!empty(UpdraftPlus_S3::$proxy['port'])) { curl_setopt($curl, CURLOPT_PROXYPORT, UpdraftPlus_S3::$proxy['port']); } if (isset(UpdraftPlus_S3::$proxy['user'], UpdraftPlus_S3::$proxy['pass']) && UpdraftPlus_S3::$proxy['user'] != null && UpdraftPlus_S3::$proxy['pass'] != null) { curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_ANY); curl_setopt($curl, CURLOPT_PROXYUSERPWD, sprintf('%s:%s', UpdraftPlus_S3::$proxy['user'], UpdraftPlus_S3::$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 (UpdraftPlus_S3::hasAuth()) { // Authorization string (CloudFront stringToSign should only contain a date) if ($this->headers['Host'] == 'cloudfront.amazonaws.com') { $headers[] = 'Authorization: ' . UpdraftPlus_S3::__getSignature($this->headers['Date']); } else { $headers[] = 'Authorization: ' . UpdraftPlus_S3::__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': 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); curl_setopt($curl, CURLOPT_INFILESIZE, strlen($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; } // 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; }
protected function getS3($key, $secret, $useservercerts, $disableverify, $nossl) { if (!empty($this->s3_object) && !is_wp_error($this->s3_object)) { return $this->s3_object; } if ('' == $key || '' == $secret) { return new WP_Error('no_settings', __('No settings were found', 'updraftplus')); } global $updraftplus; if (!class_exists('UpdraftPlus_S3')) { require_once UPDRAFTPLUS_DIR . '/includes/S3.php'; } if (!class_exists('WP_HTTP_Proxy')) { require_once ABSPATH . WPINC . '/class-http.php'; } $proxy = new WP_HTTP_Proxy(); $s3 = new UpdraftPlus_S3($key, $secret); if ($proxy->is_enabled()) { # WP_HTTP_Proxy returns empty strings where we want nulls $user = $proxy->username(); if (empty($user)) { $user = null; $pass = null; } else { $pass = $proxy->password(); if (empty($pass)) { $pass = null; } } $port = (int) $proxy->port(); if (empty($port)) { $port = 8080; } $s3->setProxy($proxy->host(), $user, $pass, CURLPROXY_HTTP, $port); } if (!$nossl) { $curl_version = function_exists('curl_version') ? curl_version() : array('features' => null); $curl_ssl_supported = $curl_version['features'] & CURL_VERSION_SSL; if ($curl_ssl_supported) { if ($disableverify) { $s3->setSSL(true, false); $updraftplus->log("S3: Disabling verification of SSL certificates"); } else { $s3->setSSL(true, true); } if ($useservercerts) { $updraftplus->log("S3: Using the server's SSL certificates"); } else { $s3->setSSLAuth(null, null, UPDRAFTPLUS_DIR . '/includes/cacert.pem'); } } else { $s3->setSSL(false, false); $updraftplus->log("S3: Curl/SSL is not available. Communications will not be encrypted."); } } else { $s3->setSSL(false, false); $updraftplus->log("SSL was disabled via the user's preference. Communications will not be encrypted."); } $this->s3_object = $s3; return $this->s3_object; }