Bootstrap.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. <?php declare(strict_types=1);
  2. use Google\Cloud\Core\Exception\BadRequestException;
  3. use Google\Cloud\Core\Exception\NotFoundException;
  4. use Google\Cloud\Storage\Bucket;
  5. use Google\Cloud\Storage\Connection\ConnectionInterface;
  6. use Google\Cloud\Storage\StorageClient;
  7. use Google\Cloud\Storage\StorageObject;
  8. use PrivateBin\Configuration;
  9. use PrivateBin\Persistence\ServerSalt;
  10. use PrivateBin\TemplateSwitcher;
  11. error_reporting(E_ALL | E_STRICT);
  12. // change this, if your php files and data is outside of your webservers document root
  13. if (!defined('PUBLIC_PATH')) {
  14. define('PUBLIC_PATH', '..');
  15. }
  16. if (!defined('PATH')) {
  17. define('PATH', '..' . DIRECTORY_SEPARATOR);
  18. }
  19. if (!defined('CONF')) {
  20. define('CONF', PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.php');
  21. }
  22. if (!defined('CONF_SAMPLE')) {
  23. define('CONF_SAMPLE', PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.sample.php');
  24. }
  25. require PATH . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
  26. TemplateSwitcher::setAvailableTemplates(Configuration::getDefaults()['main']['availabletemplates']);
  27. /**
  28. * Class Helper provides unit tests pastes and comments of various formats
  29. */
  30. class Helper
  31. {
  32. /**
  33. * example ID of a paste
  34. *
  35. * @var string
  36. */
  37. private static $pasteid = '5b65a01b43987bc2';
  38. /**
  39. * example paste version 2
  40. *
  41. * @var array
  42. */
  43. private static $pasteV2 = [
  44. 'adata' => [
  45. [
  46. 'gMSNoLOk4z0RnmsYwXZ8mw==',
  47. 'TZO+JWuIuxs=',
  48. 100000,
  49. 256,
  50. 128,
  51. 'aes',
  52. 'gcm',
  53. 'zlib',
  54. ],
  55. 'plaintext',
  56. 1,
  57. 0,
  58. ],
  59. 'meta' => [
  60. 'expire' => '5min',
  61. ],
  62. 'v' => 2,
  63. 'ct' => 'ME5JF/YBEijp2uYMzLZozbKtWc5wfy6R59NBb7SmRig=',
  64. ];
  65. /**
  66. * example ID of a comment
  67. *
  68. * @var string
  69. */
  70. private static $commentid = '33a5671d11b29d01';
  71. /**
  72. * example comment cipher text, its hash is the comment ID
  73. *
  74. * @var string
  75. */
  76. private static $commentCt = 'XNE9E3AGszrjQfV5bxxSuaflS05MViI3rYtG1Nph0Qc=';
  77. /**
  78. * example comment meta data
  79. *
  80. * @var array
  81. */
  82. private static $commentV2 = [
  83. 'icon' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
  84. 'created' => 1344803528,
  85. ];
  86. /**
  87. * get example paste ID
  88. *
  89. * @return string
  90. */
  91. public static function getPasteId(): string
  92. {
  93. return self::$pasteid;
  94. }
  95. /**
  96. * get example paste, as stored on server
  97. *
  98. * @param array $meta
  99. * @return array
  100. */
  101. public static function getPaste(array $meta = []): array
  102. {
  103. $example = self::$pasteV2;
  104. $example['meta']['salt'] = ServerSalt::generate();
  105. $example['meta'] = array_merge($example['meta'], $meta);
  106. return $example;
  107. }
  108. /**
  109. * get example paste, as decoded from POST by the request object
  110. *
  111. * @param array $meta
  112. * @return array
  113. */
  114. public static function getPastePost(array $meta = []): array
  115. {
  116. $example = self::getPaste($meta);
  117. $example['meta'] = ['expire' => $example['meta']['expire']];
  118. return $example;
  119. }
  120. /**
  121. * get example paste, as received via POST by the user
  122. *
  123. * @param array $meta
  124. * @return string
  125. */
  126. public static function getPasteJson(array $meta = []): string
  127. {
  128. return json_encode(self::getPastePost($meta));
  129. }
  130. /**
  131. * get example paste ID
  132. *
  133. * @return string
  134. */
  135. public static function getCommentId(): string
  136. {
  137. return self::$commentid;
  138. }
  139. /**
  140. * get example comment, as stored on server
  141. *
  142. * @param array $meta
  143. * @return array
  144. */
  145. public static function getComment(array $meta = []): array
  146. {
  147. $example = self::$pasteV2;
  148. $example['adata'] = $example['adata'][0];
  149. $example['ct'] = self::$commentCt;
  150. $example['pasteid'] = $example['parentid'] = self::getPasteId();
  151. unset($example['meta']['expire']);
  152. $example['meta'] = array_merge($example['meta'], self::$commentV2);
  153. $example['meta'] = array_merge($example['meta'], $meta);
  154. return $example;
  155. }
  156. /**
  157. * get example comment, as decoded from POST by the request object
  158. *
  159. * @return array
  160. */
  161. public static function getCommentPost(): array
  162. {
  163. $example = self::getComment();
  164. unset($example['meta']);
  165. return $example;
  166. }
  167. /**
  168. * get example comment, as received via POST by user
  169. *
  170. * @return string
  171. */
  172. public static function getCommentJson(): string
  173. {
  174. return json_encode(self::getCommentPost());
  175. }
  176. /**
  177. * Returns 16 random hexadecimal characters.
  178. *
  179. * @return string
  180. */
  181. public static function getRandomId(): string
  182. {
  183. // 8 binary bytes are 16 characters long in hex
  184. return bin2hex(random_bytes(8));
  185. }
  186. /**
  187. * delete directory and all its contents recursively
  188. *
  189. * @param string $path
  190. * @throws Exception
  191. * @return void
  192. */
  193. public static function rmDir($path): void
  194. {
  195. if (is_dir($path)) {
  196. foreach (new DirectoryIterator($path) as $file) {
  197. if ($file->isFile()) {
  198. if (!unlink($file->getPathname())) {
  199. throw new Exception('Error deleting file "' . $file->getPathname() . '".');
  200. }
  201. } elseif ($file->isDir() && !$file->isDot()) {
  202. self::rmDir($file->getPathname());
  203. }
  204. }
  205. if (!rmdir($path)) {
  206. throw new Exception('Error deleting directory "' . $path . '".');
  207. }
  208. }
  209. }
  210. /**
  211. * create a backup of the config file
  212. *
  213. * @return void
  214. */
  215. public static function confBackup(): void
  216. {
  217. if (!is_file(CONF . '.bak') && is_file(CONF)) {
  218. rename(CONF, CONF . '.bak');
  219. }
  220. if (!is_file(CONF_SAMPLE . '.bak') && is_file(CONF_SAMPLE)) {
  221. copy(CONF_SAMPLE, CONF_SAMPLE . '.bak');
  222. }
  223. }
  224. /**
  225. * restore backup of the config file
  226. *
  227. * @return void
  228. */
  229. public static function confRestore(): void
  230. {
  231. if (is_file(CONF . '.bak')) {
  232. rename(CONF . '.bak', CONF);
  233. }
  234. if (is_file(CONF_SAMPLE . '.bak')) {
  235. rename(CONF_SAMPLE . '.bak', CONF_SAMPLE);
  236. }
  237. }
  238. /**
  239. * create ini file
  240. *
  241. * @param string $pathToFile
  242. * @param array $values
  243. */
  244. public static function createIniFile($pathToFile, array $values): void
  245. {
  246. if (count($values)) {
  247. @unlink($pathToFile);
  248. $ini = fopen($pathToFile, 'a');
  249. foreach ($values as $section => $options) {
  250. fwrite($ini, "[$section]" . PHP_EOL);
  251. foreach ($options as $option => $setting) {
  252. if (is_null($setting)) {
  253. continue;
  254. } elseif (is_string($setting)) {
  255. $setting = '"' . $setting . '"';
  256. } elseif (is_array($setting)) {
  257. foreach ($setting as $key => $value) {
  258. if (is_null($value)) {
  259. $value = 'null';
  260. } elseif (is_string($value)) {
  261. $value = '"' . $value . '"';
  262. } else {
  263. $value = var_export($value, true);
  264. }
  265. fwrite($ini, $option . "[$key] = $value" . PHP_EOL);
  266. }
  267. continue;
  268. } else {
  269. $setting = var_export($setting, true);
  270. }
  271. fwrite($ini, "$option = $setting" . PHP_EOL);
  272. }
  273. fwrite($ini, PHP_EOL);
  274. }
  275. fclose($ini);
  276. }
  277. }
  278. /**
  279. * create a temporary file, returns it's file name
  280. *
  281. * @return string
  282. */
  283. public static function createTempFile(): string
  284. {
  285. return tempnam(sys_get_temp_dir(), 'FOO');
  286. }
  287. /**
  288. * a var_export that returns arrays without line breaks
  289. * by linus@flowingcreativity.net via php.net
  290. *
  291. * @param mixed $var
  292. * @param bool $return
  293. * @return void|string
  294. */
  295. public static function varExportMin($var, $return = false): string
  296. {
  297. if (is_array($var)) {
  298. $toImplode = [];
  299. foreach ($var as $key => $value) {
  300. $toImplode[] = var_export($key, true) . ' => ' . self::varExportMin($value, true);
  301. }
  302. $code = 'array(' . implode(', ', $toImplode) . ')';
  303. if ($return) {
  304. return $code;
  305. } else {
  306. echo $code;
  307. }
  308. } else {
  309. return var_export($var, $return);
  310. }
  311. }
  312. }
  313. /**
  314. * Class BucketStub stubs a GCS bucket.
  315. */
  316. class BucketStub extends Bucket
  317. {
  318. public $_objects;
  319. private $_name;
  320. private $_info;
  321. private $_connection;
  322. private $_client;
  323. public function __construct(ConnectionInterface $connection, $name, array $info = [], $client = null)
  324. {
  325. $this->_name = $name;
  326. $this->_info = $info;
  327. $this->_connection = $connection;
  328. $this->_objects = [];
  329. $this->_client = $client;
  330. }
  331. public function acl()
  332. {
  333. throw new BadMethodCallException('not supported by this stub');
  334. }
  335. public function defaultAcl()
  336. {
  337. throw new BadMethodCallException('not supported by this stub');
  338. }
  339. public function exists(array $options = [])
  340. {
  341. return true;
  342. }
  343. public function upload($data, array $options = [])
  344. {
  345. if (!is_string($data) || !array_key_exists('name', $options)) {
  346. throw new BadMethodCallException('not supported by this stub');
  347. }
  348. $name = $options['name'];
  349. $generation = '1';
  350. $o = new StorageObjectStub($this->_connection, $name, $this, $generation, $options);
  351. $this->_objects[$options['name']] = $o;
  352. $o->setData($data);
  353. }
  354. public function uploadAsync($data, array $options = [])
  355. {
  356. throw new BadMethodCallException('not supported by this stub');
  357. }
  358. public function getResumableUploader($data, array $options = [])
  359. {
  360. throw new BadMethodCallException('not supported by this stub');
  361. }
  362. public function getStreamableUploader($data, array $options = [])
  363. {
  364. throw new BadMethodCallException('not supported by this stub');
  365. }
  366. public function object($name, array $options = [])
  367. {
  368. return $this->_objects[$name] ?? new StorageObjectStub($this->_connection, $name, $this, null, $options);
  369. }
  370. public function objects(array $options = [])
  371. {
  372. $prefix = $options['prefix'] ?? '';
  373. return new CallbackFilterIterator(
  374. new ArrayIterator($this->_objects),
  375. function ($current, $key, $iterator) use ($prefix) {
  376. return substr($key, 0, strlen($prefix)) === $prefix;
  377. }
  378. );
  379. }
  380. public function createNotification($topic, array $options = [])
  381. {
  382. throw new BadMethodCallException('not supported by this stub');
  383. }
  384. public function notification($id)
  385. {
  386. throw new BadMethodCallException('not supported by this stub');
  387. }
  388. public function notifications(array $options = [])
  389. {
  390. throw new BadMethodCallException('not supported by this stub');
  391. }
  392. public function delete(array $options = [])
  393. {
  394. $this->_client->deleteBucket($this->_name);
  395. }
  396. public function update(array $options = [])
  397. {
  398. throw new BadMethodCallException('not supported by this stub');
  399. }
  400. public function compose(array $sourceObjects, $name, array $options = [])
  401. {
  402. throw new BadMethodCallException('not supported by this stub');
  403. }
  404. public function info(array $options = [])
  405. {
  406. throw new BadMethodCallException('not supported by this stub');
  407. }
  408. public function reload(array $options = [])
  409. {
  410. throw new BadMethodCallException('not supported by this stub');
  411. }
  412. public function name()
  413. {
  414. return $this->_name;
  415. }
  416. public static function lifecycle(array $lifecycle = [])
  417. {
  418. throw new BadMethodCallException('not supported by this stub');
  419. }
  420. public function currentLifecycle(array $options = [])
  421. {
  422. throw new BadMethodCallException('not supported by this stub');
  423. }
  424. public function isWritable($file = null)
  425. {
  426. throw new BadMethodCallException('not supported by this stub');
  427. }
  428. public function iam()
  429. {
  430. throw new BadMethodCallException('not supported by this stub');
  431. }
  432. public function lockRetentionPolicy(array $options = [])
  433. {
  434. throw new BadMethodCallException('not supported by this stub');
  435. }
  436. public function signedUrl($expires, array $options = [])
  437. {
  438. throw new BadMethodCallException('not supported by this stub');
  439. }
  440. public function generateSignedPostPolicyV4($objectName, $expires, array $options = [])
  441. {
  442. throw new BadMethodCallException('not supported by this stub');
  443. }
  444. }
  445. /**
  446. * Class StorageObjectStub stubs a GCS storage object.
  447. */
  448. class StorageObjectStub extends StorageObject
  449. {
  450. private $_name;
  451. private $_data;
  452. private $_info;
  453. private $_bucket;
  454. private $_generation;
  455. private $_exists = false;
  456. private $_connection;
  457. public function __construct(ConnectionInterface $connection, $name, $bucket, $generation = null, array $info = [], $encryptionKey = null, $encryptionKeySHA256 = null)
  458. {
  459. $this->_name = $name;
  460. $this->_bucket = $bucket;
  461. $this->_generation = $generation;
  462. $this->_info = $info;
  463. $this->_connection = $connection;
  464. $timeCreated = new DateTime();
  465. $this->_info['metadata']['timeCreated'] = $timeCreated->format('Y-m-d\TH:i:s.u\Z');
  466. }
  467. public function acl()
  468. {
  469. throw new BadMethodCallException('not supported by this stub');
  470. }
  471. public function exists(array $options = [])
  472. {
  473. return array_key_exists($this->_name, $this->_bucket->_objects);
  474. }
  475. /**
  476. * @throws NotFoundException
  477. */
  478. public function delete(array $options = [])
  479. {
  480. if (array_key_exists($this->_name, $this->_bucket->_objects)) {
  481. unset($this->_bucket->_objects[$this->_name]);
  482. } else {
  483. throw new NotFoundException('key ' . $this->_name . ' not found.');
  484. }
  485. }
  486. /**
  487. * @throws NotFoundException
  488. */
  489. public function update(array $metadata, array $options = [])
  490. {
  491. if (!$this->_exists) {
  492. throw new NotFoundException('key ' . $this->_name . ' not found.');
  493. }
  494. $this->_info = $metadata;
  495. }
  496. public function copy($destination, array $options = [])
  497. {
  498. throw new BadMethodCallException('not supported by this stub');
  499. }
  500. public function rewrite($destination, array $options = [])
  501. {
  502. throw new BadMethodCallException('not supported by this stub');
  503. }
  504. public function rename($name, array $options = [])
  505. {
  506. throw new BadMethodCallException('not supported by this stub');
  507. }
  508. /**
  509. * @throws NotFoundException
  510. */
  511. public function downloadAsString(array $options = [])
  512. {
  513. if (!$this->_exists) {
  514. throw new NotFoundException('key ' . $this->_name . ' not found.');
  515. }
  516. return $this->_data;
  517. }
  518. public function downloadToFile($path, array $options = [])
  519. {
  520. throw new BadMethodCallException('not supported by this stub');
  521. }
  522. public function downloadAsStream(array $options = [])
  523. {
  524. throw new BadMethodCallException('not supported by this stub');
  525. }
  526. public function downloadAsStreamAsync(array $options = [])
  527. {
  528. throw new BadMethodCallException('not supported by this stub');
  529. }
  530. public function signedUrl($expires, array $options = [])
  531. {
  532. throw new BadMethodCallException('not supported by this stub');
  533. }
  534. public function signedUploadUrl($expires, array $options = [])
  535. {
  536. throw new BadMethodCallException('not supported by this stub');
  537. }
  538. public function beginSignedUploadSession(array $options = [])
  539. {
  540. throw new BadMethodCallException('not supported by this stub');
  541. }
  542. public function info(array $options = [])
  543. {
  544. return $this->_info['metadata'] ?? [];
  545. }
  546. public function reload(array $options = [])
  547. {
  548. throw new BadMethodCallException('not supported by this stub');
  549. }
  550. public function name()
  551. {
  552. return $this->_name;
  553. }
  554. public function identity()
  555. {
  556. throw new BadMethodCallException('not supported by this stub');
  557. }
  558. public function gcsUri()
  559. {
  560. return sprintf(
  561. 'gs://%s/%s',
  562. $this->_bucket->name(),
  563. $this->_name
  564. );
  565. }
  566. public function setData($data)
  567. {
  568. $this->_data = $data;
  569. $this->_exists = true;
  570. }
  571. }
  572. /**
  573. * Class ConnectionInterfaceStub required for the stubs.
  574. */
  575. class ConnectionInterfaceStub implements ConnectionInterface
  576. {
  577. public function deleteAcl(array $args = [])
  578. {
  579. throw new BadMethodCallException('not supported by this stub');
  580. }
  581. public function getAcl(array $args = [])
  582. {
  583. throw new BadMethodCallException('not supported by this stub');
  584. }
  585. public function listAcl(array $args = [])
  586. {
  587. throw new BadMethodCallException('not supported by this stub');
  588. }
  589. public function insertAcl(array $args = [])
  590. {
  591. throw new BadMethodCallException('not supported by this stub');
  592. }
  593. public function patchAcl(array $args = [])
  594. {
  595. throw new BadMethodCallException('not supported by this stub');
  596. }
  597. public function deleteBucket(array $args = [])
  598. {
  599. throw new BadMethodCallException('not supported by this stub');
  600. }
  601. public function restoreBucket(array $args = [])
  602. {
  603. throw new BadMethodCallException('not supported by this stub');
  604. }
  605. public function getBucket(array $args = [])
  606. {
  607. throw new BadMethodCallException('not supported by this stub');
  608. }
  609. public function listBuckets(array $args = [])
  610. {
  611. throw new BadMethodCallException('not supported by this stub');
  612. }
  613. public function insertBucket(array $args = [])
  614. {
  615. throw new BadMethodCallException('not supported by this stub');
  616. }
  617. public function getBucketIamPolicy(array $args)
  618. {
  619. throw new BadMethodCallException('not supported by this stub');
  620. }
  621. public function setBucketIamPolicy(array $args)
  622. {
  623. throw new BadMethodCallException('not supported by this stub');
  624. }
  625. public function testBucketIamPermissions(array $args)
  626. {
  627. throw new BadMethodCallException('not supported by this stub');
  628. }
  629. public function patchBucket(array $args = [])
  630. {
  631. throw new BadMethodCallException('not supported by this stub');
  632. }
  633. public function deleteObject(array $args = [])
  634. {
  635. throw new BadMethodCallException('not supported by this stub');
  636. }
  637. public function restoreObject(array $args = [])
  638. {
  639. throw new BadMethodCallException('not supported by this stub');
  640. }
  641. public function copyObject(array $args = [])
  642. {
  643. throw new BadMethodCallException('not supported by this stub');
  644. }
  645. public function rewriteObject(array $args = [])
  646. {
  647. throw new BadMethodCallException('not supported by this stub');
  648. }
  649. public function moveObject(array $args = [])
  650. {
  651. throw new BadMethodCallException('not supported by this stub');
  652. }
  653. public function composeObject(array $args = [])
  654. {
  655. throw new BadMethodCallException('not supported by this stub');
  656. }
  657. public function getObject(array $args = [])
  658. {
  659. throw new BadMethodCallException('not supported by this stub');
  660. }
  661. public function headObject(array $args = []): array
  662. {
  663. throw new BadMethodCallException('not supported by this stub');
  664. }
  665. public function listObjects(array $args = [])
  666. {
  667. throw new BadMethodCallException('not supported by this stub');
  668. }
  669. public function patchObject(array $args = [])
  670. {
  671. throw new BadMethodCallException('not supported by this stub');
  672. }
  673. public function downloadObject(array $args = [])
  674. {
  675. throw new BadMethodCallException('not supported by this stub');
  676. }
  677. public function insertObject(array $args = [])
  678. {
  679. throw new BadMethodCallException('not supported by this stub');
  680. }
  681. public function getNotification(array $args = [])
  682. {
  683. throw new BadMethodCallException('not supported by this stub');
  684. }
  685. public function deleteNotification(array $args = [])
  686. {
  687. throw new BadMethodCallException('not supported by this stub');
  688. }
  689. public function insertNotification(array $args = [])
  690. {
  691. throw new BadMethodCallException('not supported by this stub');
  692. }
  693. public function listNotifications(array $args = [])
  694. {
  695. throw new BadMethodCallException('not supported by this stub');
  696. }
  697. public function getServiceAccount(array $args = [])
  698. {
  699. throw new BadMethodCallException('not supported by this stub');
  700. }
  701. public function lockRetentionPolicy(array $args = [])
  702. {
  703. throw new BadMethodCallException('not supported by this stub');
  704. }
  705. public function createHmacKey(array $args = [])
  706. {
  707. throw new BadMethodCallException('not supported by this stub');
  708. }
  709. public function deleteHmacKey(array $args = [])
  710. {
  711. throw new BadMethodCallException('not supported by this stub');
  712. }
  713. public function getHmacKey(array $args = [])
  714. {
  715. throw new BadMethodCallException('not supported by this stub');
  716. }
  717. public function updateHmacKey(array $args = [])
  718. {
  719. throw new BadMethodCallException('not supported by this stub');
  720. }
  721. public function listHmacKeys(array $args = [])
  722. {
  723. throw new BadMethodCallException('not supported by this stub');
  724. }
  725. }
  726. /**
  727. * Class StorageClientStub provides a limited stub for performing the unit test
  728. */
  729. class StorageClientStub extends StorageClient
  730. {
  731. private $_config = null;
  732. private $_connection = null;
  733. private static $_buckets = [];
  734. public function __construct(array $config = [])
  735. {
  736. $this->_config = $config;
  737. $this->_connection = new ConnectionInterfaceStub();
  738. }
  739. public function bucket($name, $userProject = false, array $config = [])
  740. {
  741. if (!array_key_exists($name, self::$_buckets)) {
  742. self::$_buckets[$name] = new BucketStub($this->_connection, $name, [], $this);
  743. }
  744. return self::$_buckets[$name];
  745. }
  746. /**
  747. * @throws \Google\Cloud\Core\Exception\NotFoundException
  748. */
  749. public function deleteBucket($name)
  750. {
  751. if (array_key_exists($name, self::$_buckets)) {
  752. unset(self::$_buckets[$name]);
  753. } else {
  754. throw new NotFoundException();
  755. }
  756. }
  757. public function buckets(array $options = [])
  758. {
  759. throw new BadMethodCallException('not supported by this stub');
  760. }
  761. public function registerStreamWrapper($protocol = null)
  762. {
  763. throw new BadMethodCallException('not supported by this stub');
  764. }
  765. public function unregisterStreamWrapper($protocol = null)
  766. {
  767. throw new BadMethodCallException('not supported by this stub');
  768. }
  769. public function signedUrlUploader($uri, $data, array $options = [])
  770. {
  771. throw new BadMethodCallException('not supported by this stub');
  772. }
  773. public function timestamp(\DateTimeInterface $timestamp, $nanoSeconds = null)
  774. {
  775. throw new BadMethodCallException('not supported by this stub');
  776. }
  777. public function getServiceAccount(array $options = [])
  778. {
  779. throw new BadMethodCallException('not supported by this stub');
  780. }
  781. public function hmacKeys(array $options = [])
  782. {
  783. throw new BadMethodCallException('not supported by this stub');
  784. }
  785. public function hmacKey($accessId, $projectId = null, array $metadata = [])
  786. {
  787. throw new BadMethodCallException('not supported by this stub');
  788. }
  789. public function createHmacKey($serviceAccountEmail, array $options = [])
  790. {
  791. throw new BadMethodCallException('not supported by this stub');
  792. }
  793. public function createBucket($name, array $options = [])
  794. {
  795. if (array_key_exists($name, self::$_buckets)) {
  796. throw new BadRequestException('already exists');
  797. }
  798. $b = new BucketStub($this->_connection, $name, [], $this);
  799. self::$_buckets[$name] = $b;
  800. return $b;
  801. }
  802. }