<?php
/**
* Gajus Kuizinas, 2010 11 25
* http://hotelpublisher.com
*/
class GoogleStorage
{
private $access_key;
private $secret_key;
private $host = 'commondatastorage.googleapis.com';
private $headers = array();
private $bucket = FALSE;
private $parameters = array();
public function __construct($access_key, $secret_key)
{
$this->access_key = $access_key;
$this->secret_key = $secret_key;
$this->request_time = $this->_getRequestTime();
$this->createObject('ahostel.lt');
die();
}
public function getBucketList()
{
return $this->exec('GET');
}
public function getBucket($bucket, $prefix = '')
{
if($prefix)
{
$this->parameters['prefix'] = $prefix;
}
$this->bucket = $bucket;
$response = $this->exec('GET');
$xml = simplexml_load_string($response['body']);
$files = array();
foreach($xml->Contents as $name => $object)
{
$files[(string)$object->Key] = array
(
'last_modified' => strtotime((string) $object->LastModified),
'size' => (string) $object->Size,
);
}
return $files;
}
public function createObject($bucket)
{
$this->bucket = $bucket;
$this->post = array('key' => $this->bucket . '/testtesttest_$folder$');
$this->exec2('POST');
}
private function exec2($method)
{
$this->message = "{$method}\n\n\n{$this->request_time}\n/" . ($this->bucket ? $this->bucket . "/" : '');
$header = array
(
'Date: ' . $this->request_time,
'Content-Length: ' . (empty($options[CURLOPT_POSTFIELDS]) ? '0' : strlen(http_build_query($this->post))),
'Authorization: GOOG1 ' . $this->access_key . ':' . $this->_getSignature(),
);
$options = array
(
CURLOPT_HTTPGET => TRUE,
CURLOPT_URL => ($this->bucket ? $this->bucket . '.' : '') . $this->host . (!empty($this->parameters) ? '?' . http_build_query($this->parameters) : ''),
CURLOPT_HEADER => TRUE,
CURLINFO_HEADER_OUT => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_VERBOSE => TRUE,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTPHEADER => $header,
);
if(!empty($this->post))
{
$options[CURLOPT_POST] = TRUE;
$options[CURLOPT_POSTFIELDS] = $this->post;
}
$ch = curl_init();
curl_setopt_array($ch, $options);
$return = array();
$return['raw'] = curl_exec($ch);
$return['header_out'] = curl_getinfo($ch, CURLINFO_HEADER_OUT);
$info = curl_getinfo($ch);
$return['header'] = http_parse_headers(substr($return['raw'], 0, $info['header_size']));
$return['body'] = substr($return['raw'], $info['header_size']);
if($info['http_code'] != '200')
{
die('Error: ' . $info['http_code'] . $return['body']);
}
//die(var_dump( $return['header_out'] ));
return $return;
}
private function exec($method)
{
$this->message = "{$method}\n\n\n{$this->request_time}\n/" . ($this->bucket ? $this->bucket . "/" : '');
$options = array
(
CURLOPT_HTTPGET => TRUE,
CURLOPT_URL => ($this->bucket ? $this->bucket . '.' : '') . $this->host . (!empty($this->parameters) ? '?' . http_build_query($this->parameters) : ''),
CURLOPT_HEADER => TRUE,
CURLINFO_HEADER_OUT => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_VERBOSE => TRUE,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 30,
);
if(!empty($this->post))
{
$options[CURLOPT_POST] = TRUE;
$options[CURLOPT_POSTFIELDS] = $this->post;
}
$header = array
(
#'Host: ' . ($this->bucket ? $this->bucket . '.' : '') . $this->host,
'Date: ' . $this->request_time,
'Content-Length: ' . (empty($options[CURLOPT_POSTFIELDS]) ? '0' : strlen(http_build_query($options[CURLOPT_POSTFIELDS]))),
'Authorization: GOOG1 ' . $this->access_key . ':' . $this->_getSignature(),
);
$options[CURLOPT_HTTPHEADER] = $header;
$ch = curl_init();
curl_setopt_array($ch, $options);
$return = array();
$return['raw'] = curl_exec($ch);
$return['header_out'] = curl_getinfo($ch, CURLINFO_HEADER_OUT);
$info = curl_getinfo($ch);
$return['header'] = http_parse_headers(substr($return['raw'], 0, $info['header_size']));
$return['body'] = substr($return['raw'], $info['header_size']);
if($info['http_code'] != '200')
{
die('Error: ' . $info['http_code'] . $return['body']);
}
//die(var_dump( $return['header_out'] ));
return $return;
}
private function _getSignature()
{
return base64_encode(hash_hmac('sha1', utf8_encode($this->message), $this->secret_key, TRUE));
}
private function _getRequestTime()
{
return date('D, d M Y H:i:s O');
return date('r');
}
}