2021-05-28 22:39:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PrivateBin\Data;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Google\Cloud\Core\Exception\NotFoundException;
|
|
|
|
use Google\Cloud\Storage\StorageClient;
|
|
|
|
use PrivateBin\Json;
|
|
|
|
|
|
|
|
class GoogleCloudStorage extends AbstractData
|
|
|
|
{
|
2021-06-09 22:27:34 +02:00
|
|
|
const DATETIME_FORMAT = 'Y-m-d\TH:i:s.u\Z';
|
|
|
|
|
2021-05-28 22:39:50 +02:00
|
|
|
/**
|
|
|
|
* returns a Google Cloud Storage data backend.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param array $options
|
|
|
|
* @return GoogleCloudStorage
|
|
|
|
*/
|
|
|
|
public static function getInstance(array $options)
|
|
|
|
{
|
|
|
|
$client = null;
|
|
|
|
$bucket = null;
|
|
|
|
$prefix = 'pastes';
|
|
|
|
|
|
|
|
if (getenv('PRIVATEBIN_GCS_BUCKET')) {
|
|
|
|
$bucket = getenv('PRIVATEBIN_GCS_BUCKET');
|
|
|
|
}
|
|
|
|
if (is_array($options) && array_key_exists('bucket', $options)) {
|
|
|
|
$bucket = $options['bucket'];
|
|
|
|
}
|
|
|
|
if (is_array($options) && array_key_exists('prefix', $options)) {
|
|
|
|
$prefix = $options['prefix'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(self::$_instance instanceof self)) {
|
2021-06-13 10:44:26 +02:00
|
|
|
self::$_instance = new self($bucket, $prefix);
|
2021-05-28 22:39:50 +02:00
|
|
|
}
|
|
|
|
return self::$_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected $_client = null;
|
|
|
|
protected $_bucket = null;
|
|
|
|
protected $_prefix = 'pastes';
|
|
|
|
|
2021-06-13 10:44:26 +02:00
|
|
|
public function __construct($bucket, $prefix)
|
2021-05-28 22:39:50 +02:00
|
|
|
{
|
|
|
|
parent::__construct();
|
2021-06-13 10:44:26 +02:00
|
|
|
$this->_client = class_exists('StorageClientStub', false) ?
|
|
|
|
new \StorageClientStub(array()) :
|
|
|
|
new StorageClient(array('suppressKeyFileNotice' => true));
|
2021-05-28 22:39:50 +02:00
|
|
|
$this->_bucket = $this->_client->bucket($bucket);
|
|
|
|
if ($prefix != null) {
|
|
|
|
$this->_prefix = $prefix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the google storage object key for $pasteid in $this->_bucket.
|
|
|
|
* @param $pasteid string to get the key for
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function _getKey($pasteid)
|
|
|
|
{
|
|
|
|
if ($this->_prefix != '') {
|
|
|
|
return $this->_prefix . '/' . $pasteid;
|
|
|
|
}
|
|
|
|
return $pasteid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads the payload in the $this->_bucket under the specified key.
|
|
|
|
* The entire payload is stored as a JSON document. The metadata is replicated
|
|
|
|
* as the GCS object's metadata except for the fields attachment, attachmentname
|
|
|
|
* and salt.
|
|
|
|
*
|
|
|
|
* @param $key string to store the payload under
|
|
|
|
* @param $payload array to store
|
|
|
|
* @return bool true if successful, otherwise false.
|
|
|
|
*/
|
|
|
|
private function upload($key, $payload)
|
|
|
|
{
|
|
|
|
$metadata = array_key_exists('meta', $payload) ? $payload['meta'] : array();
|
|
|
|
unset($metadata['attachment'], $metadata['attachmentname'], $metadata['salt']);
|
|
|
|
foreach ($metadata as $k => $v) {
|
|
|
|
$metadata[$k] = strval($v);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$this->_bucket->upload(Json::encode($payload), array(
|
|
|
|
'name' => $key,
|
|
|
|
'chunkSize' => 262144,
|
|
|
|
'predefinedAcl' => 'private',
|
|
|
|
'metadata' => array(
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'metadata' => $metadata,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
error_log('failed to upload ' . $key . ' to ' . $this->_bucket->name() . ', ' .
|
|
|
|
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function create($pasteid, array $paste)
|
|
|
|
{
|
|
|
|
if ($this->exists($pasteid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->upload($this->_getKey($pasteid), $paste);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function read($pasteid)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$o = $this->_bucket->object($this->_getKey($pasteid));
|
|
|
|
$data = $o->downloadAsString();
|
|
|
|
return Json::decode($data);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return false;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
error_log('failed to read ' . $pasteid . ' from ' . $this->_bucket->name() . ', ' .
|
|
|
|
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function delete($pasteid)
|
|
|
|
{
|
|
|
|
$name = $this->_getKey($pasteid);
|
|
|
|
|
|
|
|
try {
|
|
|
|
foreach ($this->_bucket->objects(array('prefix' => $name . '/discussion/')) as $comment) {
|
|
|
|
try {
|
|
|
|
$this->_bucket->object($comment->name())->delete();
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// ignore if already deleted.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// there are no discussions associated with the paste
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->_bucket->object($name)->delete();
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// ignore if already deleted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function exists($pasteid)
|
|
|
|
{
|
|
|
|
$o = $this->_bucket->object($this->_getKey($pasteid));
|
|
|
|
return $o->exists();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function createComment($pasteid, $parentid, $commentid, array $comment)
|
|
|
|
{
|
|
|
|
if ($this->existsComment($pasteid, $parentid, $commentid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$key = $this->_getKey($pasteid) . '/discussion/' . $parentid . '/' . $commentid;
|
|
|
|
return $this->upload($key, $comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function readComments($pasteid)
|
|
|
|
{
|
|
|
|
$comments = array();
|
|
|
|
$prefix = $this->_getKey($pasteid) . '/discussion/';
|
|
|
|
try {
|
|
|
|
foreach ($this->_bucket->objects(array('prefix' => $prefix)) as $key) {
|
|
|
|
$comment = JSON::decode($this->_bucket->object($key->name())->downloadAsString());
|
|
|
|
$comment['id'] = basename($key->name());
|
|
|
|
$slot = $this->getOpenSlot($comments, (int) $comment['meta']['created']);
|
|
|
|
$comments[$slot] = $comment;
|
|
|
|
}
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// no comments found
|
|
|
|
}
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function existsComment($pasteid, $parentid, $commentid)
|
|
|
|
{
|
|
|
|
$name = $this->_getKey($pasteid) . '/discussion/' . $parentid . '/' . $commentid;
|
|
|
|
$o = $this->_bucket->object($name);
|
|
|
|
return $o->exists();
|
|
|
|
}
|
|
|
|
|
2021-06-08 07:49:22 +02:00
|
|
|
/**
|
2021-06-09 22:27:34 +02:00
|
|
|
* @inheritDoc
|
2021-06-08 07:49:22 +02:00
|
|
|
*/
|
|
|
|
public function purgeValues($namespace, $time)
|
|
|
|
{
|
2021-06-10 21:39:15 +02:00
|
|
|
$path = 'config/' . $namespace;
|
2021-06-09 22:27:34 +02:00
|
|
|
try {
|
2021-06-10 21:39:15 +02:00
|
|
|
foreach ($this->_bucket->objects(array('prefix' => $path)) as $object) {
|
|
|
|
$name = $object->name();
|
|
|
|
if (strlen($name) > strlen($path) && substr($name, strlen($path), 1) !== '/') {
|
|
|
|
continue;
|
2021-06-09 22:27:34 +02:00
|
|
|
}
|
2021-06-13 11:02:53 +02:00
|
|
|
$info = $object->info();
|
2021-06-10 21:39:15 +02:00
|
|
|
if (key_exists('metadata', $info) && key_exists('value', $info['metadata'])) {
|
|
|
|
$value = $info['metadata']['value'];
|
|
|
|
if (is_numeric($value) && intval($value) < $time) {
|
|
|
|
try {
|
|
|
|
$object->delete();
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// deleted by another instance.
|
|
|
|
}
|
2021-06-09 22:27:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// no objects in the bucket yet
|
2021-06-08 07:49:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 07:02:47 +02:00
|
|
|
/**
|
2021-06-10 21:39:15 +02:00
|
|
|
* For GoogleCloudStorage, the value will also be stored in the metadata for the
|
|
|
|
* namespaces traffic_limiter and purge_limiter.
|
2021-06-07 09:11:24 +02:00
|
|
|
* @inheritDoc
|
2021-06-07 07:02:47 +02:00
|
|
|
*/
|
|
|
|
public function setValue($value, $namespace, $key = '')
|
|
|
|
{
|
2021-06-10 21:39:15 +02:00
|
|
|
if ($key === '') {
|
|
|
|
$key = 'config/' . $namespace;
|
|
|
|
} else {
|
|
|
|
$key = 'config/' . $namespace . '/' . $key;
|
|
|
|
}
|
|
|
|
|
2021-06-07 09:11:24 +02:00
|
|
|
$data = Json::encode($value);
|
|
|
|
|
2021-06-10 21:39:15 +02:00
|
|
|
$metadata = array('namespace' => $namespace);
|
|
|
|
if ($namespace != 'salt') {
|
|
|
|
$metadata['value'] = strval($value);
|
|
|
|
}
|
2021-06-07 09:11:24 +02:00
|
|
|
try {
|
|
|
|
$this->_bucket->upload($data, array(
|
|
|
|
'name' => $key,
|
|
|
|
'chunkSize' => 262144,
|
|
|
|
'predefinedAcl' => 'private',
|
|
|
|
'metadata' => array(
|
|
|
|
'content-type' => 'application/json',
|
2021-06-10 21:39:15 +02:00
|
|
|
'metadata' => $metadata,
|
2021-06-07 09:11:24 +02:00
|
|
|
),
|
|
|
|
));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
error_log('failed to set key ' . $key . ' to ' . $this->_bucket->name() . ', ' .
|
|
|
|
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
|
|
|
|
return false;
|
2021-06-07 07:02:47 +02:00
|
|
|
}
|
2021-06-07 09:11:24 +02:00
|
|
|
return true;
|
2021-06-07 07:02:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-07 09:11:24 +02:00
|
|
|
* @inheritDoc
|
2021-06-07 07:02:47 +02:00
|
|
|
*/
|
|
|
|
public function getValue($namespace, $key = '')
|
|
|
|
{
|
2021-06-10 21:39:15 +02:00
|
|
|
if ($key === '') {
|
|
|
|
$key = 'config/' . $namespace;
|
|
|
|
} else {
|
|
|
|
$key = 'config/' . $namespace . '/' . $key;
|
|
|
|
}
|
2021-06-07 09:11:24 +02:00
|
|
|
try {
|
|
|
|
$o = $this->_bucket->object($key);
|
|
|
|
$data = $o->downloadAsString();
|
2021-06-13 12:43:18 +02:00
|
|
|
return (string) Json::decode($data);
|
2021-06-07 09:11:24 +02:00
|
|
|
} catch (NotFoundException $e) {
|
2021-06-13 11:02:53 +02:00
|
|
|
return '';
|
2021-06-07 09:11:24 +02:00
|
|
|
}
|
2021-06-07 07:02:47 +02:00
|
|
|
}
|
|
|
|
|
2021-05-28 22:39:50 +02:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function _getExpiredPastes($batchsize)
|
|
|
|
{
|
|
|
|
$expired = array();
|
|
|
|
|
|
|
|
$now = time();
|
|
|
|
$prefix = $this->_prefix;
|
|
|
|
if ($prefix != '') {
|
|
|
|
$prefix = $prefix . '/';
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
foreach ($this->_bucket->objects(array('prefix' => $prefix)) as $object) {
|
|
|
|
$metadata = $object->info()['metadata'];
|
|
|
|
if ($metadata != null && array_key_exists('expire_date', $metadata)) {
|
|
|
|
$expire_at = intval($metadata['expire_date']);
|
|
|
|
if ($expire_at != 0 && $expire_at < $now) {
|
|
|
|
array_push($expired, basename($object->name()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($expired) > $batchsize) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// no objects in the bucket yet
|
|
|
|
}
|
|
|
|
return $expired;
|
|
|
|
}
|
|
|
|
}
|