public function getContents() { $folders = null; $files = null; $root = $this->getState('folder', '/'); if (!is_array($folders) || !is_array($files)) { $folders = array(); if ($this->_hasAdequateInformation()) { $s3 = $this->_getS3Object(); $raw = AEUtilAmazons3::getBucket($this->getState('s3bucket'), $root, null, null, '/', true); if ($raw) { foreach ($raw as $name => $record) { if (substr($name, -8) == '$folder$') { continue; } if (array_key_exists('name', $record)) { $extension = substr($name, -4); if (!in_array($extension, array('.zip', '.jpa'))) { continue; } $files[$name] = $record; } elseif (array_key_exists('prefix', $record)) { $folders[$name] = $record; } } } } } return array('files' => $files, 'folders' => $folders); }
public function _S3() { if ($this->_s3 == null) { $bucket = $this->config->find('files._s3bucket'); $accesskey = trim($this->app->zlfw->decryptPassword($this->config->find('files._awsaccesskey'))); $secretkey = trim($this->app->zlfw->decryptPassword($this->config->find('files._awssecretkey'))); // register s3 class $this->app->loader->register('AEUtilAmazons3', 'classes:amazons3.php'); // init S3 Utility $this->_s3 = AEUtilAmazons3::getInstance($accesskey, $secretkey, false); } return $this->_s3; }
public function downloadToFile($remotePath, $localFile, $fromOffset = null, $length = null) { // Retrieve engine configuration data $config = AEFactory::getConfiguration(); $accesskey = trim($config->get('engine.postproc.googlestorage.accesskey', '')); $secret = trim($config->get('engine.postproc.googlestorage.secretkey', '')); $usessl = $config->get('engine.postproc.googlestorage.usessl', 0) == 0 ? false : true; $bucket = $config->get('engine.postproc.googlestorage.bucket', ''); // Sanity checks if (empty($accesskey)) { $this->setError('You have not set up your Google Storage Access Key'); return false; } if (empty($secret)) { $this->setError('You have not set up your Google Storage Secret Key'); return false; } if (empty($bucket)) { $this->setError('You have not set up your Google Storage Bucket'); return false; } else { // Remove any slashes from the bucket $bucket = str_replace('/', '', $bucket); if ($lowercase) { $bucket = strtolower($bucket); } } // Create an S3 instance with the required credentials $s3 = AEUtilAmazons3::getInstance($accesskey, $secret, $usessl); $s3->defaultHost = 'commondatastorage.googleapis.com'; if ($fromOffset && $length) { $toOffset = $fromOffset + $length - 1; } else { $toOffset = null; } $result = $s3->getObject($bucket, $remotePath, $localFile, $fromOffset, $toOffset); // Return the result $this->propagateFromObject($s3); return $result; }
/** * Moves an uploaded file to a destination folder * * @param string $file The name of the php (temporary) uploaded file * @param string $dest The path (including filename) to move the uploaded file to * * @return boolean The success of the operation */ public function upload($file, $dest) { $rrs = false; $absolute_filename = $this->app->path->path('zlfw:changelog.txt'); // Legacy single part uploads $result = $this->s3->putObject(AEUtilAmazons3::inputFile($absolute_filename, false), 'milcom.testing', 'changelog.txt', AEUtilAmazons3::ACL_BUCKET_OWNER_FULL_CONTROL, array(), array('x-amz-storage-class' => $rrs ? 'REDUCED_REDUNDANCY' : 'STANDARD')); // $this->s3->setError('You have not set up your Amazon S3 Access Key'); }
public function downloadToBrowser($remotePath) { // Retrieve engine configuration data $config = AEFactory::getConfiguration(); $accesskey = trim($config->get('engine.postproc.s3.accesskey', '')); $secret = trim($config->get('engine.postproc.s3.secretkey', '')); $usessl = $config->get('engine.postproc.s3.usessl', 0) == 0 ? false : true; $bucket = $config->get('engine.postproc.s3.bucket', ''); $lowercase = $config->get('engine.postproc.s3.lowercase', 1); $rrs = $config->get('engine.postproc.s3.rrs', 1); $endpoint = $config->get('engine.postproc.s3.customendpoint', ''); $endpoint = trim($endpoint); if (!empty($endpoint)) { $protoPos = strpos($endpoint, ':\\'); if ($protoPos !== false) { $endpoint = substr($endpoint, $protoPos + 3); } $slashPos = strpos($endpoint, '/'); if ($slashPos !== false) { $endpoint = substr($endpoint, $slashPos + 1); } } // Sanity checks if (empty($accesskey)) { $this->setError('You have not set up your Amazon S3 Access Key'); return false; } if (empty($secret)) { $this->setError('You have not set up your Amazon S3 Secret Key'); return false; } if (empty($bucket)) { $this->setError('You have not set up your Amazon S3 Bucket'); return false; } else { // Remove any slashes from the bucket $bucket = str_replace('/', '', $bucket); if ($lowercase) { $bucket = strtolower($bucket); } } // Create an S3 instance with the required credentials $s3 = AEUtilAmazons3::getInstance($accesskey, $secret, $usessl); if (!empty($endpoint)) { $s3->defaultHost = $endpoint; } $expires = time() + 10; // Should be plenty of time for a simple redirection! $stringToSign = "GET\n\n\n{$expires}\n/{$bucket}/{$remotePath}"; $signature = AEUtilAmazons3::__getHash($stringToSign); $url = $usessl ? 'https://' : 'http://'; $url .= "{$bucket}.s3.amazonaws.com/{$remotePath}?AWSAccessKeyId=" . urlencode($accesskey) . "&Expires={$expires}&Signature=" . urlencode($signature); return $url; }
/** * 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('uploads', $this->parameters) || array_key_exists('uploadId', $this->parameters) || array_key_exists('partNumber', $this->parameters)) { $this->resource .= $query; } } $url = (AEUtilAmazons3::$useSSL && extension_loaded('openssl') ? 'https://' : 'http://') . $this->headers['Host'] . $this->uri; // Basic setup $curl = curl_init(); @curl_setopt($curl, CURLOPT_CAINFO, AKEEBA_CACERT_PEM); curl_setopt($curl, CURLOPT_USERAGENT, 'AkeebaBackupProfessional/S3PostProcessor'); if (AEUtilAmazons3::$useSSL) { curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, stristr(PHP_OS, 'WIN') ? false : true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, stristr(PHP_OS, 'WIN') ? false : true); } curl_setopt($curl, CURLOPT_URL, $url); // 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); $amz = "\n" . implode("\n", $amz); } else { $amz = ''; } // Authorization string (CloudFront stringToSign should only contain a date) if ($this->headers['Host'] == 'cloudfront.amazonaws.com') { $stringToSign = $this->headers['Date']; } else { $stringToSign = $this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource; } $headers[] = 'Authorization: ' . AEUtilAmazons3::__getSignature($stringToSign); 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); if ($this->size >= 0) { curl_setopt($curl, CURLOPT_BUFFERSIZE, $this->size); } } 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)) && 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; }