GoogleCloudStorage.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace PrivateBin\Data;
  3. use Exception;
  4. use Google\Cloud\Core\Exception\NotFoundException;
  5. use Google\Cloud\Storage\StorageClient;
  6. use PrivateBin\Json;
  7. class GoogleCloudStorage extends AbstractData
  8. {
  9. /**
  10. * returns a Google Cloud Storage data backend.
  11. *
  12. * @access public
  13. * @static
  14. * @param array $options
  15. * @return GoogleCloudStorage
  16. */
  17. public static function getInstance(array $options)
  18. {
  19. $client = null;
  20. $bucket = null;
  21. $prefix = 'pastes';
  22. if (getenv('PRIVATEBIN_GCS_BUCKET')) {
  23. $bucket = getenv('PRIVATEBIN_GCS_BUCKET');
  24. }
  25. if (is_array($options) && array_key_exists('bucket', $options)) {
  26. $bucket = $options['bucket'];
  27. }
  28. if (is_array($options) && array_key_exists('prefix', $options)) {
  29. $prefix = $options['prefix'];
  30. }
  31. if (is_array($options) && array_key_exists('client', $options)) {
  32. $client = $options['client'];
  33. }
  34. if (!(self::$_instance instanceof self)) {
  35. self::$_instance = new self($bucket, $prefix, $client);
  36. }
  37. return self::$_instance;
  38. }
  39. protected $_client = null;
  40. protected $_bucket = null;
  41. protected $_prefix = 'pastes';
  42. public function __construct($bucket, $prefix, $client = null)
  43. {
  44. parent::__construct();
  45. if ($client == null) {
  46. $this->_client = new StorageClient(array('suppressKeyFileNotice' => true));
  47. } else {
  48. // use given client for test purposes
  49. $this->_client = $client;
  50. }
  51. $this->_bucket = $this->_client->bucket($bucket);
  52. if ($prefix != null) {
  53. $this->_prefix = $prefix;
  54. }
  55. }
  56. /**
  57. * returns the google storage object key for $pasteid in $this->_bucket.
  58. * @param $pasteid string to get the key for
  59. * @return string
  60. */
  61. private function _getKey($pasteid)
  62. {
  63. if ($this->_prefix != '') {
  64. return $this->_prefix . '/' . $pasteid;
  65. }
  66. return $pasteid;
  67. }
  68. /**
  69. * Uploads the payload in the $this->_bucket under the specified key.
  70. * The entire payload is stored as a JSON document. The metadata is replicated
  71. * as the GCS object's metadata except for the fields attachment, attachmentname
  72. * and salt.
  73. *
  74. * @param $key string to store the payload under
  75. * @param $payload array to store
  76. * @return bool true if successful, otherwise false.
  77. */
  78. private function upload($key, $payload)
  79. {
  80. $metadata = array_key_exists('meta', $payload) ? $payload['meta'] : array();
  81. unset($metadata['attachment'], $metadata['attachmentname'], $metadata['salt']);
  82. foreach ($metadata as $k => $v) {
  83. $metadata[$k] = strval($v);
  84. }
  85. try {
  86. $this->_bucket->upload(Json::encode($payload), array(
  87. 'name' => $key,
  88. 'chunkSize' => 262144,
  89. 'predefinedAcl' => 'private',
  90. 'metadata' => array(
  91. 'content-type' => 'application/json',
  92. 'metadata' => $metadata,
  93. ),
  94. ));
  95. } catch (Exception $e) {
  96. error_log('failed to upload ' . $key . ' to ' . $this->_bucket->name() . ', ' .
  97. trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
  98. return false;
  99. }
  100. return true;
  101. }
  102. /**
  103. * @inheritDoc
  104. */
  105. public function create($pasteid, array $paste)
  106. {
  107. if ($this->exists($pasteid)) {
  108. return false;
  109. }
  110. return $this->upload($this->_getKey($pasteid), $paste);
  111. }
  112. /**
  113. * @inheritDoc
  114. */
  115. public function read($pasteid)
  116. {
  117. try {
  118. $o = $this->_bucket->object($this->_getKey($pasteid));
  119. $data = $o->downloadAsString();
  120. return Json::decode($data);
  121. } catch (NotFoundException $e) {
  122. return false;
  123. } catch (Exception $e) {
  124. error_log('failed to read ' . $pasteid . ' from ' . $this->_bucket->name() . ', ' .
  125. trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
  126. return false;
  127. }
  128. }
  129. /**
  130. * @inheritDoc
  131. */
  132. public function delete($pasteid)
  133. {
  134. $name = $this->_getKey($pasteid);
  135. try {
  136. foreach ($this->_bucket->objects(array('prefix' => $name . '/discussion/')) as $comment) {
  137. try {
  138. $this->_bucket->object($comment->name())->delete();
  139. } catch (NotFoundException $e) {
  140. // ignore if already deleted.
  141. }
  142. }
  143. } catch (NotFoundException $e) {
  144. // there are no discussions associated with the paste
  145. }
  146. try {
  147. $this->_bucket->object($name)->delete();
  148. } catch (NotFoundException $e) {
  149. // ignore if already deleted
  150. }
  151. }
  152. /**
  153. * @inheritDoc
  154. */
  155. public function exists($pasteid)
  156. {
  157. $o = $this->_bucket->object($this->_getKey($pasteid));
  158. return $o->exists();
  159. }
  160. /**
  161. * @inheritDoc
  162. */
  163. public function createComment($pasteid, $parentid, $commentid, array $comment)
  164. {
  165. if ($this->existsComment($pasteid, $parentid, $commentid)) {
  166. return false;
  167. }
  168. $key = $this->_getKey($pasteid) . '/discussion/' . $parentid . '/' . $commentid;
  169. return $this->upload($key, $comment);
  170. }
  171. /**
  172. * @inheritDoc
  173. */
  174. public function readComments($pasteid)
  175. {
  176. $comments = array();
  177. $prefix = $this->_getKey($pasteid) . '/discussion/';
  178. try {
  179. foreach ($this->_bucket->objects(array('prefix' => $prefix)) as $key) {
  180. $comment = JSON::decode($this->_bucket->object($key->name())->downloadAsString());
  181. $comment['id'] = basename($key->name());
  182. $slot = $this->getOpenSlot($comments, (int) $comment['meta']['created']);
  183. $comments[$slot] = $comment;
  184. }
  185. } catch (NotFoundException $e) {
  186. // no comments found
  187. }
  188. return $comments;
  189. }
  190. /**
  191. * @inheritDoc
  192. */
  193. public function existsComment($pasteid, $parentid, $commentid)
  194. {
  195. $name = $this->_getKey($pasteid) . '/discussion/' . $parentid . '/' . $commentid;
  196. $o = $this->_bucket->object($name);
  197. return $o->exists();
  198. }
  199. /**
  200. * Save a value.
  201. *
  202. * @access public
  203. * @param string $value
  204. * @param string $namespace
  205. * @param string $key
  206. * @return bool
  207. */
  208. public function setValue($value, $namespace, $key = '')
  209. {
  210. switch ($namespace) {
  211. case 'purge_limiter':
  212. ;
  213. break;
  214. case 'salt':
  215. ;
  216. break;
  217. case 'traffic_limiter':
  218. ;
  219. break;
  220. default:
  221. return false;
  222. break;
  223. }
  224. }
  225. /**
  226. * Load a value.
  227. *
  228. * @access public
  229. * @param string $namespace
  230. * @param string $key
  231. * @return string
  232. */
  233. public function getValue($namespace, $key = '')
  234. {
  235. }
  236. /**
  237. * @inheritDoc
  238. */
  239. protected function _getExpiredPastes($batchsize)
  240. {
  241. $expired = array();
  242. $now = time();
  243. $prefix = $this->_prefix;
  244. if ($prefix != '') {
  245. $prefix = $prefix . '/';
  246. }
  247. try {
  248. foreach ($this->_bucket->objects(array('prefix' => $prefix)) as $object) {
  249. $metadata = $object->info()['metadata'];
  250. if ($metadata != null && array_key_exists('expire_date', $metadata)) {
  251. $expire_at = intval($metadata['expire_date']);
  252. if ($expire_at != 0 && $expire_at < $now) {
  253. array_push($expired, basename($object->name()));
  254. }
  255. }
  256. if (count($expired) > $batchsize) {
  257. break;
  258. }
  259. }
  260. } catch (NotFoundException $e) {
  261. // no objects in the bucket yet
  262. }
  263. return $expired;
  264. }
  265. }