2021-05-28 20:39:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PrivateBin\Data;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Google\Cloud\Core\Exception\NotFoundException;
|
2021-06-16 03:57:26 +00:00
|
|
|
use Google\Cloud\Storage\Bucket;
|
2021-05-28 20:39:50 +00:00
|
|
|
use Google\Cloud\Storage\StorageClient;
|
|
|
|
use PrivateBin\Json;
|
|
|
|
|
|
|
|
class GoogleCloudStorage extends AbstractData
|
|
|
|
{
|
2021-06-16 03:19:45 +00:00
|
|
|
/**
|
|
|
|
* GCS client
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var StorageClient
|
|
|
|
*/
|
|
|
|
private static $_client = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GCS bucket
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
2021-06-16 03:57:26 +00:00
|
|
|
* @var Bucket
|
2021-06-16 03:19:45 +00:00
|
|
|
*/
|
|
|
|
private static $_bucket = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* object prefix
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $_prefix = 'pastes';
|
|
|
|
|
2022-10-06 04:19:06 +00:00
|
|
|
/**
|
|
|
|
* bucket acl type
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $_uniformacl = false;
|
|
|
|
|
2021-05-28 20:39:50 +00:00
|
|
|
/**
|
|
|
|
* returns a Google Cloud Storage data backend.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param array $options
|
|
|
|
* @return GoogleCloudStorage
|
|
|
|
*/
|
|
|
|
public static function getInstance(array $options)
|
|
|
|
{
|
2021-06-16 03:19:45 +00:00
|
|
|
// if needed initialize the singleton
|
|
|
|
if (!(self::$_instance instanceof self)) {
|
|
|
|
self::$_instance = new self;
|
|
|
|
}
|
2021-05-28 20:39:50 +00:00
|
|
|
|
2021-06-16 03:19:45 +00:00
|
|
|
$bucket = null;
|
2021-05-28 20:39:50 +00:00
|
|
|
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)) {
|
2021-06-16 03:19:45 +00:00
|
|
|
self::$_prefix = $options['prefix'];
|
2021-05-28 20:39:50 +00:00
|
|
|
}
|
2022-10-06 04:19:06 +00:00
|
|
|
if (is_array($options) && array_key_exists('uniformacl', $options)) {
|
|
|
|
self::$_uniformacl = $options['uniformacl'];
|
|
|
|
}
|
2021-05-28 20:39:50 +00:00
|
|
|
|
2021-06-16 03:19:45 +00:00
|
|
|
if (empty(self::$_client)) {
|
|
|
|
self::$_client = class_exists('StorageClientStub', false) ?
|
|
|
|
new \StorageClientStub(array()) :
|
|
|
|
new StorageClient(array('suppressKeyFileNotice' => true));
|
2021-05-28 20:39:50 +00:00
|
|
|
}
|
2021-06-16 03:19:45 +00:00
|
|
|
self::$_bucket = self::$_client->bucket($bucket);
|
2021-05-28 20:39:50 +00:00
|
|
|
|
2021-06-16 03:19:45 +00:00
|
|
|
return self::$_instance;
|
2021-05-28 20:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-16 03:19:45 +00:00
|
|
|
* returns the google storage object key for $pasteid in self::$_bucket.
|
2021-06-16 03:43:18 +00:00
|
|
|
*
|
2021-06-16 03:19:45 +00:00
|
|
|
* @access private
|
2021-05-28 20:39:50 +00:00
|
|
|
* @param $pasteid string to get the key for
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function _getKey($pasteid)
|
|
|
|
{
|
2021-06-16 03:19:45 +00:00
|
|
|
if (self::$_prefix != '') {
|
|
|
|
return self::$_prefix . '/' . $pasteid;
|
2021-05-28 20:39:50 +00:00
|
|
|
}
|
|
|
|
return $pasteid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-16 03:19:45 +00:00
|
|
|
* Uploads the payload in the self::$_bucket under the specified key.
|
2021-05-28 20:39:50 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2021-06-16 03:19:45 +00:00
|
|
|
private function _upload($key, $payload)
|
2021-05-28 20:39:50 +00:00
|
|
|
{
|
2022-10-06 06:41:37 +00:00
|
|
|
$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);
|
|
|
|
}
|
2021-05-28 20:39:50 +00:00
|
|
|
try {
|
2022-10-06 04:19:06 +00:00
|
|
|
$data = array(
|
2021-05-28 20:39:50 +00:00
|
|
|
'name' => $key,
|
|
|
|
'chunkSize' => 262144,
|
|
|
|
'metadata' => array(
|
|
|
|
'content-type' => 'application/json',
|
2022-10-06 06:41:37 +00:00
|
|
|
'metadata' => $metadata,
|
2021-05-28 20:39:50 +00:00
|
|
|
),
|
2022-10-06 04:19:06 +00:00
|
|
|
);
|
|
|
|
if (!self::$_uniformacl) {
|
|
|
|
$data['predefinedAcl'] = 'private';
|
|
|
|
}
|
|
|
|
self::$_bucket->upload(Json::encode($payload), $data);
|
2021-05-28 20:39:50 +00:00
|
|
|
} catch (Exception $e) {
|
2021-06-16 03:19:45 +00:00
|
|
|
error_log('failed to upload ' . $key . ' to ' . self::$_bucket->name() . ', ' .
|
2021-05-28 20:39:50 +00:00
|
|
|
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function create($pasteid, array $paste)
|
|
|
|
{
|
|
|
|
if ($this->exists($pasteid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-16 03:19:45 +00:00
|
|
|
return $this->_upload($this->_getKey($pasteid), $paste);
|
2021-05-28 20:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function read($pasteid)
|
|
|
|
{
|
|
|
|
try {
|
2021-06-16 03:19:45 +00:00
|
|
|
$o = self::$_bucket->object($this->_getKey($pasteid));
|
2021-05-28 20:39:50 +00:00
|
|
|
$data = $o->downloadAsString();
|
|
|
|
return Json::decode($data);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return false;
|
|
|
|
} catch (Exception $e) {
|
2021-06-16 03:19:45 +00:00
|
|
|
error_log('failed to read ' . $pasteid . ' from ' . self::$_bucket->name() . ', ' .
|
2021-05-28 20:39:50 +00:00
|
|
|
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function delete($pasteid)
|
|
|
|
{
|
|
|
|
$name = $this->_getKey($pasteid);
|
|
|
|
|
|
|
|
try {
|
2021-06-16 03:19:45 +00:00
|
|
|
foreach (self::$_bucket->objects(array('prefix' => $name . '/discussion/')) as $comment) {
|
2021-05-28 20:39:50 +00:00
|
|
|
try {
|
2021-06-16 03:19:45 +00:00
|
|
|
self::$_bucket->object($comment->name())->delete();
|
2021-05-28 20:39:50 +00:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// ignore if already deleted.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// there are no discussions associated with the paste
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-06-16 03:19:45 +00:00
|
|
|
self::$_bucket->object($name)->delete();
|
2021-05-28 20:39:50 +00:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// ignore if already deleted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function exists($pasteid)
|
|
|
|
{
|
2021-06-16 03:19:45 +00:00
|
|
|
$o = self::$_bucket->object($this->_getKey($pasteid));
|
2021-05-28 20:39:50 +00:00
|
|
|
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;
|
2021-06-16 03:19:45 +00:00
|
|
|
return $this->_upload($key, $comment);
|
2021-05-28 20:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function readComments($pasteid)
|
|
|
|
{
|
|
|
|
$comments = array();
|
|
|
|
$prefix = $this->_getKey($pasteid) . '/discussion/';
|
|
|
|
try {
|
2021-06-16 03:19:45 +00:00
|
|
|
foreach (self::$_bucket->objects(array('prefix' => $prefix)) as $key) {
|
|
|
|
$comment = JSON::decode(self::$_bucket->object($key->name())->downloadAsString());
|
2021-05-28 20:39:50 +00:00
|
|
|
$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;
|
2021-06-16 03:19:45 +00:00
|
|
|
$o = self::$_bucket->object($name);
|
2021-05-28 20:39:50 +00:00
|
|
|
return $o->exists();
|
|
|
|
}
|
|
|
|
|
2021-06-08 05:49:22 +00:00
|
|
|
/**
|
2021-06-09 20:27:34 +00:00
|
|
|
* @inheritDoc
|
2021-06-08 05:49:22 +00:00
|
|
|
*/
|
|
|
|
public function purgeValues($namespace, $time)
|
|
|
|
{
|
2021-06-10 19:39:15 +00:00
|
|
|
$path = 'config/' . $namespace;
|
2021-06-09 20:27:34 +00:00
|
|
|
try {
|
2021-06-16 03:19:45 +00:00
|
|
|
foreach (self::$_bucket->objects(array('prefix' => $path)) as $object) {
|
2021-06-10 19:39:15 +00:00
|
|
|
$name = $object->name();
|
|
|
|
if (strlen($name) > strlen($path) && substr($name, strlen($path), 1) !== '/') {
|
|
|
|
continue;
|
2021-06-09 20:27:34 +00:00
|
|
|
}
|
2021-06-13 09:02:53 +00:00
|
|
|
$info = $object->info();
|
2021-06-10 19:39:15 +00: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 20:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// no objects in the bucket yet
|
2021-06-08 05:49:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 05:02:47 +00:00
|
|
|
/**
|
2021-06-10 19:39:15 +00:00
|
|
|
* For GoogleCloudStorage, the value will also be stored in the metadata for the
|
|
|
|
* namespaces traffic_limiter and purge_limiter.
|
2021-06-07 07:11:24 +00:00
|
|
|
* @inheritDoc
|
2021-06-07 05:02:47 +00:00
|
|
|
*/
|
|
|
|
public function setValue($value, $namespace, $key = '')
|
|
|
|
{
|
2021-06-10 19:39:15 +00:00
|
|
|
if ($key === '') {
|
|
|
|
$key = 'config/' . $namespace;
|
|
|
|
} else {
|
|
|
|
$key = 'config/' . $namespace . '/' . $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
$metadata = array('namespace' => $namespace);
|
|
|
|
if ($namespace != 'salt') {
|
|
|
|
$metadata['value'] = strval($value);
|
|
|
|
}
|
2021-06-07 07:11:24 +00:00
|
|
|
try {
|
2022-10-06 04:19:06 +00:00
|
|
|
$data = array(
|
2021-06-07 07:11:24 +00:00
|
|
|
'name' => $key,
|
|
|
|
'chunkSize' => 262144,
|
|
|
|
'metadata' => array(
|
|
|
|
'content-type' => 'application/json',
|
2021-06-10 19:39:15 +00:00
|
|
|
'metadata' => $metadata,
|
2021-06-07 07:11:24 +00:00
|
|
|
),
|
2022-10-06 04:19:06 +00:00
|
|
|
);
|
|
|
|
if (!self::$_uniformacl) {
|
|
|
|
$data['predefinedAcl'] = 'private';
|
|
|
|
}
|
|
|
|
self::$_bucket->upload($value, $data);
|
2021-06-07 07:11:24 +00:00
|
|
|
} catch (Exception $e) {
|
2021-06-16 03:19:45 +00:00
|
|
|
error_log('failed to set key ' . $key . ' to ' . self::$_bucket->name() . ', ' .
|
2021-06-07 07:11:24 +00:00
|
|
|
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
|
|
|
|
return false;
|
2021-06-07 05:02:47 +00:00
|
|
|
}
|
2021-06-07 07:11:24 +00:00
|
|
|
return true;
|
2021-06-07 05:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-07 07:11:24 +00:00
|
|
|
* @inheritDoc
|
2021-06-07 05:02:47 +00:00
|
|
|
*/
|
|
|
|
public function getValue($namespace, $key = '')
|
|
|
|
{
|
2021-06-10 19:39:15 +00:00
|
|
|
if ($key === '') {
|
|
|
|
$key = 'config/' . $namespace;
|
|
|
|
} else {
|
|
|
|
$key = 'config/' . $namespace . '/' . $key;
|
|
|
|
}
|
2021-06-07 07:11:24 +00:00
|
|
|
try {
|
2021-06-16 03:19:45 +00:00
|
|
|
$o = self::$_bucket->object($key);
|
2021-06-13 19:16:30 +00:00
|
|
|
return $o->downloadAsString();
|
2021-06-07 07:11:24 +00:00
|
|
|
} catch (NotFoundException $e) {
|
2021-06-13 09:02:53 +00:00
|
|
|
return '';
|
2021-06-07 07:11:24 +00:00
|
|
|
}
|
2021-06-07 05:02:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 20:39:50 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function _getExpiredPastes($batchsize)
|
|
|
|
{
|
|
|
|
$expired = array();
|
|
|
|
|
|
|
|
$now = time();
|
2021-06-16 03:19:45 +00:00
|
|
|
$prefix = self::$_prefix;
|
2021-05-28 20:39:50 +00:00
|
|
|
if ($prefix != '') {
|
2021-06-16 03:19:45 +00:00
|
|
|
$prefix .= '/';
|
2021-05-28 20:39:50 +00:00
|
|
|
}
|
|
|
|
try {
|
2021-06-16 03:19:45 +00:00
|
|
|
foreach (self::$_bucket->objects(array('prefix' => $prefix)) as $object) {
|
2021-05-28 20:39:50 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|