Bootstrap.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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/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 = array(
  45. 'adata' => array(
  46. array(
  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' => array(
  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 = '5a52eebf11c4c94b';
  72. /**
  73. * example comment meta data
  74. *
  75. * @var array
  76. */
  77. private static $commentV2 = array(
  78. 'icon' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
  79. 'created' => 1344803528,
  80. );
  81. /**
  82. * JS files and their SRI hashes
  83. *
  84. * @var array
  85. */
  86. private static $hashes = array();
  87. /**
  88. * get example paste ID
  89. *
  90. * @return string
  91. */
  92. public static function getPasteId(): string
  93. {
  94. return self::$pasteid;
  95. }
  96. /**
  97. * get example paste, as stored on server
  98. *
  99. * @param array $meta
  100. * @return array
  101. */
  102. public static function getPaste(array $meta = array()): array
  103. {
  104. $example = self::$pasteV2;
  105. $example['meta']['salt'] = ServerSalt::generate();
  106. $example['meta'] = array_merge($example['meta'], $meta);
  107. return $example;
  108. }
  109. /**
  110. * get example paste, as decoded from POST by the request object
  111. *
  112. * @param array $meta
  113. * @return array
  114. */
  115. public static function getPastePost(array $meta = array()): array
  116. {
  117. $example = self::getPaste($meta);
  118. $example['meta'] = array('expire' => $example['meta']['expire']);
  119. return $example;
  120. }
  121. /**
  122. * get example paste, as received via POST by the user
  123. *
  124. * @param array $meta
  125. * @return string
  126. */
  127. public static function getPasteJson(array $meta = array()): string
  128. {
  129. return json_encode(self::getPastePost($meta));
  130. }
  131. /**
  132. * get example paste ID
  133. *
  134. * @return string
  135. */
  136. public static function getCommentId(): string
  137. {
  138. return self::$commentid;
  139. }
  140. /**
  141. * get example comment, as stored on server
  142. *
  143. * @param array $meta
  144. * @return array
  145. */
  146. public static function getComment(array $meta = array()): array
  147. {
  148. $example = self::$pasteV2;
  149. $example['adata'] = $example['adata'][0];
  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 = array();
  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. * update all templates with the latest SRI hashes for all JS files
  314. *
  315. * @return void
  316. */
  317. public static function updateSubresourceIntegrity(): void
  318. {
  319. foreach (new GlobIterator(PATH . 'js' . DIRECTORY_SEPARATOR . '*.js') as $file) {
  320. if ($file->getBasename() === 'common.js' || $file->getBasename() === 'eslint.config.js') {
  321. continue; // ignore JS unit test bootstrap
  322. }
  323. self::$hashes[$file->getBasename()] = base64_encode(
  324. hash('sha512', file_get_contents($file->getPathname()), true)
  325. );
  326. }
  327. $counter = 0;
  328. $file = PATH . 'lib' . DIRECTORY_SEPARATOR . 'Configuration.php';
  329. $content = preg_replace_callback(
  330. '#\'js/([a-z0-9.-]+.js)(\' +)=\> \'[^\']*\',#',
  331. function ($matches) use (&$counter) {
  332. if (array_key_exists($matches[1], Helper::$hashes)) {
  333. ++$counter;
  334. return '\'js/' . $matches[1] . $matches[2] .
  335. '=> \'sha512-' . Helper::$hashes[$matches[1]] . '\',';
  336. } else {
  337. throw new Exception('SRI hash for file js/' . $matches[1] . ' not found, please add the missing file or remove it from lib/Configuration.php.');
  338. }
  339. },
  340. file_get_contents($file)
  341. );
  342. file_put_contents($file, $content);
  343. if ($counter !== count(self::$hashes)) {
  344. 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.');
  345. }
  346. }
  347. }
  348. /**
  349. * Class BucketStub stubs a GCS bucket.
  350. */
  351. class BucketStub extends Bucket
  352. {
  353. public $_objects;
  354. private $_name;
  355. private $_info;
  356. private $_connection;
  357. private $_client;
  358. public function __construct(ConnectionInterface $connection, $name, array $info = array(), $client = null)
  359. {
  360. $this->_name = $name;
  361. $this->_info = $info;
  362. $this->_connection = $connection;
  363. $this->_objects = array();
  364. $this->_client = $client;
  365. }
  366. public function acl()
  367. {
  368. throw new BadMethodCallException('not supported by this stub');
  369. }
  370. public function defaultAcl()
  371. {
  372. throw new BadMethodCallException('not supported by this stub');
  373. }
  374. public function exists(array $options = array())
  375. {
  376. return true;
  377. }
  378. public function upload($data, array $options = array())
  379. {
  380. if (!is_string($data) || !array_key_exists('name', $options)) {
  381. throw new BadMethodCallException('not supported by this stub');
  382. }
  383. $name = $options['name'];
  384. $generation = '1';
  385. $o = new StorageObjectStub($this->_connection, $name, $this, $generation, $options);
  386. $this->_objects[$options['name']] = $o;
  387. $o->setData($data);
  388. }
  389. public function uploadAsync($data, array $options = array())
  390. {
  391. throw new BadMethodCallException('not supported by this stub');
  392. }
  393. public function getResumableUploader($data, array $options = array())
  394. {
  395. throw new BadMethodCallException('not supported by this stub');
  396. }
  397. public function getStreamableUploader($data, array $options = array())
  398. {
  399. throw new BadMethodCallException('not supported by this stub');
  400. }
  401. public function object($name, array $options = array())
  402. {
  403. return $this->_objects[$name] ?? new StorageObjectStub($this->_connection, $name, $this, null, $options);
  404. }
  405. public function objects(array $options = array())
  406. {
  407. $prefix = $options['prefix'] ?? '';
  408. return new CallbackFilterIterator(
  409. new ArrayIterator($this->_objects),
  410. function ($current, $key, $iterator) use ($prefix) {
  411. return substr($key, 0, strlen($prefix)) === $prefix;
  412. }
  413. );
  414. }
  415. public function createNotification($topic, array $options = array())
  416. {
  417. throw new BadMethodCallException('not supported by this stub');
  418. }
  419. public function notification($id)
  420. {
  421. throw new BadMethodCallException('not supported by this stub');
  422. }
  423. public function notifications(array $options = array())
  424. {
  425. throw new BadMethodCallException('not supported by this stub');
  426. }
  427. public function delete(array $options = array())
  428. {
  429. $this->_client->deleteBucket($this->_name);
  430. }
  431. public function update(array $options = array())
  432. {
  433. throw new BadMethodCallException('not supported by this stub');
  434. }
  435. public function compose(array $sourceObjects, $name, array $options = array())
  436. {
  437. throw new BadMethodCallException('not supported by this stub');
  438. }
  439. public function info(array $options = array())
  440. {
  441. throw new BadMethodCallException('not supported by this stub');
  442. }
  443. public function reload(array $options = array())
  444. {
  445. throw new BadMethodCallException('not supported by this stub');
  446. }
  447. public function name()
  448. {
  449. return $this->_name;
  450. }
  451. public static function lifecycle(array $lifecycle = array())
  452. {
  453. throw new BadMethodCallException('not supported by this stub');
  454. }
  455. public function currentLifecycle(array $options = array())
  456. {
  457. throw new BadMethodCallException('not supported by this stub');
  458. }
  459. public function isWritable($file = null)
  460. {
  461. throw new BadMethodCallException('not supported by this stub');
  462. }
  463. public function iam()
  464. {
  465. throw new BadMethodCallException('not supported by this stub');
  466. }
  467. public function lockRetentionPolicy(array $options = array())
  468. {
  469. throw new BadMethodCallException('not supported by this stub');
  470. }
  471. public function signedUrl($expires, array $options = array())
  472. {
  473. throw new BadMethodCallException('not supported by this stub');
  474. }
  475. public function generateSignedPostPolicyV4($objectName, $expires, array $options = array())
  476. {
  477. throw new BadMethodCallException('not supported by this stub');
  478. }
  479. }
  480. /**
  481. * Class StorageObjectStub stubs a GCS storage object.
  482. */
  483. class StorageObjectStub extends StorageObject
  484. {
  485. private $_name;
  486. private $_data;
  487. private $_info;
  488. private $_bucket;
  489. private $_generation;
  490. private $_exists = false;
  491. private $_connection;
  492. public function __construct(ConnectionInterface $connection, $name, $bucket, $generation = null, array $info = array(), $encryptionKey = null, $encryptionKeySHA256 = null)
  493. {
  494. $this->_name = $name;
  495. $this->_bucket = $bucket;
  496. $this->_generation = $generation;
  497. $this->_info = $info;
  498. $this->_connection = $connection;
  499. $timeCreated = new DateTime();
  500. $this->_info['metadata']['timeCreated'] = $timeCreated->format('Y-m-d\TH:i:s.u\Z');
  501. }
  502. public function acl()
  503. {
  504. throw new BadMethodCallException('not supported by this stub');
  505. }
  506. public function exists(array $options = array())
  507. {
  508. return array_key_exists($this->_name, $this->_bucket->_objects);
  509. }
  510. /**
  511. * @throws NotFoundException
  512. */
  513. public function delete(array $options = array())
  514. {
  515. if (array_key_exists($this->_name, $this->_bucket->_objects)) {
  516. unset($this->_bucket->_objects[$this->_name]);
  517. } else {
  518. throw new NotFoundException('key ' . $this->_name . ' not found.');
  519. }
  520. }
  521. /**
  522. * @throws NotFoundException
  523. */
  524. public function update(array $metadata, array $options = array())
  525. {
  526. if (!$this->_exists) {
  527. throw new NotFoundException('key ' . $this->_name . ' not found.');
  528. }
  529. $this->_info = $metadata;
  530. }
  531. public function copy($destination, array $options = array())
  532. {
  533. throw new BadMethodCallException('not supported by this stub');
  534. }
  535. public function rewrite($destination, array $options = array())
  536. {
  537. throw new BadMethodCallException('not supported by this stub');
  538. }
  539. public function rename($name, array $options = array())
  540. {
  541. throw new BadMethodCallException('not supported by this stub');
  542. }
  543. /**
  544. * @throws NotFoundException
  545. */
  546. public function downloadAsString(array $options = array())
  547. {
  548. if (!$this->_exists) {
  549. throw new NotFoundException('key ' . $this->_name . ' not found.');
  550. }
  551. return $this->_data;
  552. }
  553. public function downloadToFile($path, array $options = array())
  554. {
  555. throw new BadMethodCallException('not supported by this stub');
  556. }
  557. public function downloadAsStream(array $options = array())
  558. {
  559. throw new BadMethodCallException('not supported by this stub');
  560. }
  561. public function downloadAsStreamAsync(array $options = array())
  562. {
  563. throw new BadMethodCallException('not supported by this stub');
  564. }
  565. public function signedUrl($expires, array $options = array())
  566. {
  567. throw new BadMethodCallException('not supported by this stub');
  568. }
  569. public function signedUploadUrl($expires, array $options = array())
  570. {
  571. throw new BadMethodCallException('not supported by this stub');
  572. }
  573. public function beginSignedUploadSession(array $options = array())
  574. {
  575. throw new BadMethodCallException('not supported by this stub');
  576. }
  577. public function info(array $options = array())
  578. {
  579. return $this->_info['metadata'] ?? array();
  580. }
  581. public function reload(array $options = array())
  582. {
  583. throw new BadMethodCallException('not supported by this stub');
  584. }
  585. public function name()
  586. {
  587. return $this->_name;
  588. }
  589. public function identity()
  590. {
  591. throw new BadMethodCallException('not supported by this stub');
  592. }
  593. public function gcsUri()
  594. {
  595. return sprintf(
  596. 'gs://%s/%s',
  597. $this->_bucket->name(),
  598. $this->_name
  599. );
  600. }
  601. public function setData($data)
  602. {
  603. $this->_data = $data;
  604. $this->_exists = true;
  605. }
  606. }
  607. /**
  608. * Class ConnectionInterfaceStub required for the stubs.
  609. */
  610. class ConnectionInterfaceStub implements ConnectionInterface
  611. {
  612. public function deleteAcl(array $args = array())
  613. {
  614. throw new BadMethodCallException('not supported by this stub');
  615. }
  616. public function getAcl(array $args = array())
  617. {
  618. throw new BadMethodCallException('not supported by this stub');
  619. }
  620. public function listAcl(array $args = array())
  621. {
  622. throw new BadMethodCallException('not supported by this stub');
  623. }
  624. public function insertAcl(array $args = array())
  625. {
  626. throw new BadMethodCallException('not supported by this stub');
  627. }
  628. public function patchAcl(array $args = array())
  629. {
  630. throw new BadMethodCallException('not supported by this stub');
  631. }
  632. public function deleteBucket(array $args = array())
  633. {
  634. throw new BadMethodCallException('not supported by this stub');
  635. }
  636. public function restoreBucket(array $args = array())
  637. {
  638. throw new BadMethodCallException('not supported by this stub');
  639. }
  640. public function getBucket(array $args = array())
  641. {
  642. throw new BadMethodCallException('not supported by this stub');
  643. }
  644. public function listBuckets(array $args = array())
  645. {
  646. throw new BadMethodCallException('not supported by this stub');
  647. }
  648. public function insertBucket(array $args = array())
  649. {
  650. throw new BadMethodCallException('not supported by this stub');
  651. }
  652. public function getBucketIamPolicy(array $args)
  653. {
  654. throw new BadMethodCallException('not supported by this stub');
  655. }
  656. public function setBucketIamPolicy(array $args)
  657. {
  658. throw new BadMethodCallException('not supported by this stub');
  659. }
  660. public function testBucketIamPermissions(array $args)
  661. {
  662. throw new BadMethodCallException('not supported by this stub');
  663. }
  664. public function patchBucket(array $args = array())
  665. {
  666. throw new BadMethodCallException('not supported by this stub');
  667. }
  668. public function deleteObject(array $args = array())
  669. {
  670. throw new BadMethodCallException('not supported by this stub');
  671. }
  672. public function restoreObject(array $args = array())
  673. {
  674. throw new BadMethodCallException('not supported by this stub');
  675. }
  676. public function copyObject(array $args = array())
  677. {
  678. throw new BadMethodCallException('not supported by this stub');
  679. }
  680. public function rewriteObject(array $args = array())
  681. {
  682. throw new BadMethodCallException('not supported by this stub');
  683. }
  684. public function moveObject(array $args = array())
  685. {
  686. throw new BadMethodCallException('not supported by this stub');
  687. }
  688. public function composeObject(array $args = array())
  689. {
  690. throw new BadMethodCallException('not supported by this stub');
  691. }
  692. public function getObject(array $args = array())
  693. {
  694. throw new BadMethodCallException('not supported by this stub');
  695. }
  696. public function listObjects(array $args = array())
  697. {
  698. throw new BadMethodCallException('not supported by this stub');
  699. }
  700. public function patchObject(array $args = array())
  701. {
  702. throw new BadMethodCallException('not supported by this stub');
  703. }
  704. public function downloadObject(array $args = array())
  705. {
  706. throw new BadMethodCallException('not supported by this stub');
  707. }
  708. public function insertObject(array $args = array())
  709. {
  710. throw new BadMethodCallException('not supported by this stub');
  711. }
  712. public function getNotification(array $args = array())
  713. {
  714. throw new BadMethodCallException('not supported by this stub');
  715. }
  716. public function deleteNotification(array $args = array())
  717. {
  718. throw new BadMethodCallException('not supported by this stub');
  719. }
  720. public function insertNotification(array $args = array())
  721. {
  722. throw new BadMethodCallException('not supported by this stub');
  723. }
  724. public function listNotifications(array $args = array())
  725. {
  726. throw new BadMethodCallException('not supported by this stub');
  727. }
  728. public function getServiceAccount(array $args = array())
  729. {
  730. throw new BadMethodCallException('not supported by this stub');
  731. }
  732. public function lockRetentionPolicy(array $args = array())
  733. {
  734. throw new BadMethodCallException('not supported by this stub');
  735. }
  736. public function createHmacKey(array $args = array())
  737. {
  738. throw new BadMethodCallException('not supported by this stub');
  739. }
  740. public function deleteHmacKey(array $args = array())
  741. {
  742. throw new BadMethodCallException('not supported by this stub');
  743. }
  744. public function getHmacKey(array $args = array())
  745. {
  746. throw new BadMethodCallException('not supported by this stub');
  747. }
  748. public function updateHmacKey(array $args = array())
  749. {
  750. throw new BadMethodCallException('not supported by this stub');
  751. }
  752. public function listHmacKeys(array $args = array())
  753. {
  754. throw new BadMethodCallException('not supported by this stub');
  755. }
  756. }
  757. /**
  758. * Class StorageClientStub provides a limited stub for performing the unit test
  759. */
  760. class StorageClientStub extends StorageClient
  761. {
  762. private $_config = null;
  763. private $_connection = null;
  764. private static $_buckets = array();
  765. public function __construct(array $config = array())
  766. {
  767. $this->_config = $config;
  768. $this->_connection = new ConnectionInterfaceStub();
  769. }
  770. public function bucket($name, $userProject = false, array $config = array())
  771. {
  772. if (!array_key_exists($name, self::$_buckets)) {
  773. self::$_buckets[$name] = new BucketStub($this->_connection, $name, array(), $this);
  774. }
  775. return self::$_buckets[$name];
  776. }
  777. /**
  778. * @throws \Google\Cloud\Core\Exception\NotFoundException
  779. */
  780. public function deleteBucket($name)
  781. {
  782. if (array_key_exists($name, self::$_buckets)) {
  783. unset(self::$_buckets[$name]);
  784. } else {
  785. throw new NotFoundException();
  786. }
  787. }
  788. public function buckets(array $options = array())
  789. {
  790. throw new BadMethodCallException('not supported by this stub');
  791. }
  792. public function registerStreamWrapper($protocol = null)
  793. {
  794. throw new BadMethodCallException('not supported by this stub');
  795. }
  796. public function unregisterStreamWrapper($protocol = null)
  797. {
  798. throw new BadMethodCallException('not supported by this stub');
  799. }
  800. public function signedUrlUploader($uri, $data, array $options = array())
  801. {
  802. throw new BadMethodCallException('not supported by this stub');
  803. }
  804. public function timestamp(\DateTimeInterface $timestamp, $nanoSeconds = null)
  805. {
  806. throw new BadMethodCallException('not supported by this stub');
  807. }
  808. public function getServiceAccount(array $options = array())
  809. {
  810. throw new BadMethodCallException('not supported by this stub');
  811. }
  812. public function hmacKeys(array $options = array())
  813. {
  814. throw new BadMethodCallException('not supported by this stub');
  815. }
  816. public function hmacKey($accessId, $projectId = null, array $metadata = array())
  817. {
  818. throw new BadMethodCallException('not supported by this stub');
  819. }
  820. public function createHmacKey($serviceAccountEmail, array $options = array())
  821. {
  822. throw new BadMethodCallException('not supported by this stub');
  823. }
  824. public function createBucket($name, array $options = array())
  825. {
  826. if (array_key_exists($name, self::$_buckets)) {
  827. throw new BadRequestException('already exists');
  828. }
  829. $b = new BucketStub($this->_connection, $name, array(), $this);
  830. self::$_buckets[$name] = $b;
  831. return $b;
  832. }
  833. }