1
0

Bootstrap.php 27 KB

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