Bootstrap.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. * restor 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. * a var_export that returns arrays without line breaks
  280. * by linus@flowingcreativity.net via php.net
  281. *
  282. * @param mixed $var
  283. * @param bool $return
  284. * @return void|string
  285. */
  286. public static function varExportMin($var, $return = false): string
  287. {
  288. if (is_array($var)) {
  289. $toImplode = array();
  290. foreach ($var as $key => $value) {
  291. $toImplode[] = var_export($key, true) . ' => ' . self::varExportMin($value, true);
  292. }
  293. $code = 'array(' . implode(', ', $toImplode) . ')';
  294. if ($return) {
  295. return $code;
  296. } else {
  297. echo $code;
  298. }
  299. } else {
  300. return var_export($var, $return);
  301. }
  302. }
  303. /**
  304. * update all templates with the latest SRI hashes for all JS files
  305. *
  306. * @return void
  307. */
  308. public static function updateSubresourceIntegrity(): void
  309. {
  310. foreach (new GlobIterator(PATH . 'js' . DIRECTORY_SEPARATOR . '*.js') as $file) {
  311. if ($file->getBasename() === 'common.js' || $file->getBasename() === 'eslint.config.js') {
  312. continue; // ignore JS unit test bootstrap
  313. }
  314. self::$hashes[$file->getBasename()] = base64_encode(
  315. hash('sha512', file_get_contents($file->getPathname()), true)
  316. );
  317. }
  318. $counter = 0;
  319. $file = PATH . 'lib' . DIRECTORY_SEPARATOR . 'Configuration.php';
  320. $content = preg_replace_callback(
  321. '#\'js/([a-z0-9.-]+.js)(\' +)=\> \'[^\']*\',#',
  322. function ($matches) use (&$counter) {
  323. if (array_key_exists($matches[1], Helper::$hashes)) {
  324. ++$counter;
  325. return '\'js/' . $matches[1] . $matches[2] .
  326. '=> \'sha512-' . Helper::$hashes[$matches[1]] . '\',';
  327. } else {
  328. throw new Exception('SRI hash for file js/' . $matches[1] . ' not found, please add the missing file or remove it from lib/Configuration.php.');
  329. }
  330. },
  331. file_get_contents($file)
  332. );
  333. file_put_contents($file, $content);
  334. if ($counter !== count(self::$hashes)) {
  335. 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.');
  336. }
  337. }
  338. }
  339. /**
  340. * Class BucketStub stubs a GCS bucket.
  341. */
  342. class BucketStub extends Bucket
  343. {
  344. public $_objects;
  345. private $_name;
  346. private $_info;
  347. private $_connection;
  348. private $_client;
  349. public function __construct(ConnectionInterface $connection, $name, array $info = array(), $client = null)
  350. {
  351. $this->_name = $name;
  352. $this->_info = $info;
  353. $this->_connection = $connection;
  354. $this->_objects = array();
  355. $this->_client = $client;
  356. }
  357. public function acl()
  358. {
  359. throw new BadMethodCallException('not supported by this stub');
  360. }
  361. public function defaultAcl()
  362. {
  363. throw new BadMethodCallException('not supported by this stub');
  364. }
  365. public function exists(array $options = array())
  366. {
  367. return true;
  368. }
  369. public function upload($data, array $options = array())
  370. {
  371. if (!is_string($data) || !array_key_exists('name', $options)) {
  372. throw new BadMethodCallException('not supported by this stub');
  373. }
  374. $name = $options['name'];
  375. $generation = '1';
  376. $o = new StorageObjectStub($this->_connection, $name, $this, $generation, $options);
  377. $this->_objects[$options['name']] = $o;
  378. $o->setData($data);
  379. }
  380. public function uploadAsync($data, array $options = array())
  381. {
  382. throw new BadMethodCallException('not supported by this stub');
  383. }
  384. public function getResumableUploader($data, array $options = array())
  385. {
  386. throw new BadMethodCallException('not supported by this stub');
  387. }
  388. public function getStreamableUploader($data, array $options = array())
  389. {
  390. throw new BadMethodCallException('not supported by this stub');
  391. }
  392. public function object($name, array $options = array())
  393. {
  394. return $this->_objects[$name] ?? new StorageObjectStub($this->_connection, $name, $this, null, $options);
  395. }
  396. public function objects(array $options = array())
  397. {
  398. $prefix = $options['prefix'] ?? '';
  399. return new CallbackFilterIterator(
  400. new ArrayIterator($this->_objects),
  401. function ($current, $key, $iterator) use ($prefix) {
  402. return substr($key, 0, strlen($prefix)) === $prefix;
  403. }
  404. );
  405. }
  406. public function createNotification($topic, array $options = array())
  407. {
  408. throw new BadMethodCallException('not supported by this stub');
  409. }
  410. public function notification($id)
  411. {
  412. throw new BadMethodCallException('not supported by this stub');
  413. }
  414. public function notifications(array $options = array())
  415. {
  416. throw new BadMethodCallException('not supported by this stub');
  417. }
  418. public function delete(array $options = array())
  419. {
  420. $this->_client->deleteBucket($this->_name);
  421. }
  422. public function update(array $options = array())
  423. {
  424. throw new BadMethodCallException('not supported by this stub');
  425. }
  426. public function compose(array $sourceObjects, $name, array $options = array())
  427. {
  428. throw new BadMethodCallException('not supported by this stub');
  429. }
  430. public function info(array $options = array())
  431. {
  432. throw new BadMethodCallException('not supported by this stub');
  433. }
  434. public function reload(array $options = array())
  435. {
  436. throw new BadMethodCallException('not supported by this stub');
  437. }
  438. public function name()
  439. {
  440. return $this->_name;
  441. }
  442. public static function lifecycle(array $lifecycle = array())
  443. {
  444. throw new BadMethodCallException('not supported by this stub');
  445. }
  446. public function currentLifecycle(array $options = array())
  447. {
  448. throw new BadMethodCallException('not supported by this stub');
  449. }
  450. public function isWritable($file = null)
  451. {
  452. throw new BadMethodCallException('not supported by this stub');
  453. }
  454. public function iam()
  455. {
  456. throw new BadMethodCallException('not supported by this stub');
  457. }
  458. public function lockRetentionPolicy(array $options = array())
  459. {
  460. throw new BadMethodCallException('not supported by this stub');
  461. }
  462. public function signedUrl($expires, array $options = array())
  463. {
  464. throw new BadMethodCallException('not supported by this stub');
  465. }
  466. public function generateSignedPostPolicyV4($objectName, $expires, array $options = array())
  467. {
  468. throw new BadMethodCallException('not supported by this stub');
  469. }
  470. }
  471. /**
  472. * Class StorageObjectStub stubs a GCS storage object.
  473. */
  474. class StorageObjectStub extends StorageObject
  475. {
  476. private $_name;
  477. private $_data;
  478. private $_info;
  479. private $_bucket;
  480. private $_generation;
  481. private $_exists = false;
  482. private $_connection;
  483. public function __construct(ConnectionInterface $connection, $name, $bucket, $generation = null, array $info = array(), $encryptionKey = null, $encryptionKeySHA256 = null)
  484. {
  485. $this->_name = $name;
  486. $this->_bucket = $bucket;
  487. $this->_generation = $generation;
  488. $this->_info = $info;
  489. $this->_connection = $connection;
  490. $timeCreated = new DateTime();
  491. $this->_info['metadata']['timeCreated'] = $timeCreated->format('Y-m-d\TH:i:s.u\Z');
  492. }
  493. public function acl()
  494. {
  495. throw new BadMethodCallException('not supported by this stub');
  496. }
  497. public function exists(array $options = array())
  498. {
  499. return array_key_exists($this->_name, $this->_bucket->_objects);
  500. }
  501. /**
  502. * @throws NotFoundException
  503. */
  504. public function delete(array $options = array())
  505. {
  506. if (array_key_exists($this->_name, $this->_bucket->_objects)) {
  507. unset($this->_bucket->_objects[$this->_name]);
  508. } else {
  509. throw new NotFoundException('key ' . $this->_name . ' not found.');
  510. }
  511. }
  512. /**
  513. * @throws NotFoundException
  514. */
  515. public function update(array $metadata, array $options = array())
  516. {
  517. if (!$this->_exists) {
  518. throw new NotFoundException('key ' . $this->_name . ' not found.');
  519. }
  520. $this->_info = $metadata;
  521. }
  522. public function copy($destination, array $options = array())
  523. {
  524. throw new BadMethodCallException('not supported by this stub');
  525. }
  526. public function rewrite($destination, array $options = array())
  527. {
  528. throw new BadMethodCallException('not supported by this stub');
  529. }
  530. public function rename($name, array $options = array())
  531. {
  532. throw new BadMethodCallException('not supported by this stub');
  533. }
  534. /**
  535. * @throws NotFoundException
  536. */
  537. public function downloadAsString(array $options = array())
  538. {
  539. if (!$this->_exists) {
  540. throw new NotFoundException('key ' . $this->_name . ' not found.');
  541. }
  542. return $this->_data;
  543. }
  544. public function downloadToFile($path, array $options = array())
  545. {
  546. throw new BadMethodCallException('not supported by this stub');
  547. }
  548. public function downloadAsStream(array $options = array())
  549. {
  550. throw new BadMethodCallException('not supported by this stub');
  551. }
  552. public function downloadAsStreamAsync(array $options = array())
  553. {
  554. throw new BadMethodCallException('not supported by this stub');
  555. }
  556. public function signedUrl($expires, array $options = array())
  557. {
  558. throw new BadMethodCallException('not supported by this stub');
  559. }
  560. public function signedUploadUrl($expires, array $options = array())
  561. {
  562. throw new BadMethodCallException('not supported by this stub');
  563. }
  564. public function beginSignedUploadSession(array $options = array())
  565. {
  566. throw new BadMethodCallException('not supported by this stub');
  567. }
  568. public function info(array $options = array())
  569. {
  570. return $this->_info['metadata'] ?? array();
  571. }
  572. public function reload(array $options = array())
  573. {
  574. throw new BadMethodCallException('not supported by this stub');
  575. }
  576. public function name()
  577. {
  578. return $this->_name;
  579. }
  580. public function identity()
  581. {
  582. throw new BadMethodCallException('not supported by this stub');
  583. }
  584. public function gcsUri()
  585. {
  586. return sprintf(
  587. 'gs://%s/%s',
  588. $this->_bucket->name(),
  589. $this->_name
  590. );
  591. }
  592. public function setData($data)
  593. {
  594. $this->_data = $data;
  595. $this->_exists = true;
  596. }
  597. }
  598. /**
  599. * Class ConnectionInterfaceStub required for the stubs.
  600. */
  601. class ConnectionInterfaceStub implements ConnectionInterface
  602. {
  603. public function deleteAcl(array $args = array())
  604. {
  605. throw new BadMethodCallException('not supported by this stub');
  606. }
  607. public function getAcl(array $args = array())
  608. {
  609. throw new BadMethodCallException('not supported by this stub');
  610. }
  611. public function listAcl(array $args = array())
  612. {
  613. throw new BadMethodCallException('not supported by this stub');
  614. }
  615. public function insertAcl(array $args = array())
  616. {
  617. throw new BadMethodCallException('not supported by this stub');
  618. }
  619. public function patchAcl(array $args = array())
  620. {
  621. throw new BadMethodCallException('not supported by this stub');
  622. }
  623. public function deleteBucket(array $args = array())
  624. {
  625. throw new BadMethodCallException('not supported by this stub');
  626. }
  627. public function restoreBucket(array $args = array())
  628. {
  629. throw new BadMethodCallException('not supported by this stub');
  630. }
  631. public function getBucket(array $args = array())
  632. {
  633. throw new BadMethodCallException('not supported by this stub');
  634. }
  635. public function listBuckets(array $args = array())
  636. {
  637. throw new BadMethodCallException('not supported by this stub');
  638. }
  639. public function insertBucket(array $args = array())
  640. {
  641. throw new BadMethodCallException('not supported by this stub');
  642. }
  643. public function getBucketIamPolicy(array $args)
  644. {
  645. throw new BadMethodCallException('not supported by this stub');
  646. }
  647. public function setBucketIamPolicy(array $args)
  648. {
  649. throw new BadMethodCallException('not supported by this stub');
  650. }
  651. public function testBucketIamPermissions(array $args)
  652. {
  653. throw new BadMethodCallException('not supported by this stub');
  654. }
  655. public function patchBucket(array $args = array())
  656. {
  657. throw new BadMethodCallException('not supported by this stub');
  658. }
  659. public function deleteObject(array $args = array())
  660. {
  661. throw new BadMethodCallException('not supported by this stub');
  662. }
  663. public function restoreObject(array $args = array())
  664. {
  665. throw new BadMethodCallException('not supported by this stub');
  666. }
  667. public function copyObject(array $args = array())
  668. {
  669. throw new BadMethodCallException('not supported by this stub');
  670. }
  671. public function rewriteObject(array $args = array())
  672. {
  673. throw new BadMethodCallException('not supported by this stub');
  674. }
  675. public function moveObject(array $args = array())
  676. {
  677. throw new BadMethodCallException('not supported by this stub');
  678. }
  679. public function composeObject(array $args = array())
  680. {
  681. throw new BadMethodCallException('not supported by this stub');
  682. }
  683. public function getObject(array $args = array())
  684. {
  685. throw new BadMethodCallException('not supported by this stub');
  686. }
  687. public function listObjects(array $args = array())
  688. {
  689. throw new BadMethodCallException('not supported by this stub');
  690. }
  691. public function patchObject(array $args = array())
  692. {
  693. throw new BadMethodCallException('not supported by this stub');
  694. }
  695. public function downloadObject(array $args = array())
  696. {
  697. throw new BadMethodCallException('not supported by this stub');
  698. }
  699. public function insertObject(array $args = array())
  700. {
  701. throw new BadMethodCallException('not supported by this stub');
  702. }
  703. public function getNotification(array $args = array())
  704. {
  705. throw new BadMethodCallException('not supported by this stub');
  706. }
  707. public function deleteNotification(array $args = array())
  708. {
  709. throw new BadMethodCallException('not supported by this stub');
  710. }
  711. public function insertNotification(array $args = array())
  712. {
  713. throw new BadMethodCallException('not supported by this stub');
  714. }
  715. public function listNotifications(array $args = array())
  716. {
  717. throw new BadMethodCallException('not supported by this stub');
  718. }
  719. public function getServiceAccount(array $args = array())
  720. {
  721. throw new BadMethodCallException('not supported by this stub');
  722. }
  723. public function lockRetentionPolicy(array $args = array())
  724. {
  725. throw new BadMethodCallException('not supported by this stub');
  726. }
  727. public function createHmacKey(array $args = array())
  728. {
  729. throw new BadMethodCallException('not supported by this stub');
  730. }
  731. public function deleteHmacKey(array $args = array())
  732. {
  733. throw new BadMethodCallException('not supported by this stub');
  734. }
  735. public function getHmacKey(array $args = array())
  736. {
  737. throw new BadMethodCallException('not supported by this stub');
  738. }
  739. public function updateHmacKey(array $args = array())
  740. {
  741. throw new BadMethodCallException('not supported by this stub');
  742. }
  743. public function listHmacKeys(array $args = array())
  744. {
  745. throw new BadMethodCallException('not supported by this stub');
  746. }
  747. }
  748. /**
  749. * Class StorageClientStub provides a limited stub for performing the unit test
  750. */
  751. class StorageClientStub extends StorageClient
  752. {
  753. private $_config = null;
  754. private $_connection = null;
  755. private static $_buckets = array();
  756. public function __construct(array $config = array())
  757. {
  758. $this->_config = $config;
  759. $this->_connection = new ConnectionInterfaceStub();
  760. }
  761. public function bucket($name, $userProject = false, array $config = array())
  762. {
  763. if (!array_key_exists($name, self::$_buckets)) {
  764. self::$_buckets[$name] = new BucketStub($this->_connection, $name, array(), $this);
  765. }
  766. return self::$_buckets[$name];
  767. }
  768. /**
  769. * @throws \Google\Cloud\Core\Exception\NotFoundException
  770. */
  771. public function deleteBucket($name)
  772. {
  773. if (array_key_exists($name, self::$_buckets)) {
  774. unset(self::$_buckets[$name]);
  775. } else {
  776. throw new NotFoundException();
  777. }
  778. }
  779. public function buckets(array $options = array())
  780. {
  781. throw new BadMethodCallException('not supported by this stub');
  782. }
  783. public function registerStreamWrapper($protocol = null)
  784. {
  785. throw new BadMethodCallException('not supported by this stub');
  786. }
  787. public function unregisterStreamWrapper($protocol = null)
  788. {
  789. throw new BadMethodCallException('not supported by this stub');
  790. }
  791. public function signedUrlUploader($uri, $data, array $options = array())
  792. {
  793. throw new BadMethodCallException('not supported by this stub');
  794. }
  795. public function timestamp(\DateTimeInterface $timestamp, $nanoSeconds = null)
  796. {
  797. throw new BadMethodCallException('not supported by this stub');
  798. }
  799. public function getServiceAccount(array $options = array())
  800. {
  801. throw new BadMethodCallException('not supported by this stub');
  802. }
  803. public function hmacKeys(array $options = array())
  804. {
  805. throw new BadMethodCallException('not supported by this stub');
  806. }
  807. public function hmacKey($accessId, $projectId = null, array $metadata = array())
  808. {
  809. throw new BadMethodCallException('not supported by this stub');
  810. }
  811. public function createHmacKey($serviceAccountEmail, array $options = array())
  812. {
  813. throw new BadMethodCallException('not supported by this stub');
  814. }
  815. public function createBucket($name, array $options = array())
  816. {
  817. if (array_key_exists($name, self::$_buckets)) {
  818. throw new BadRequestException('already exists');
  819. }
  820. $b = new BucketStub($this->_connection, $name, array(), $this);
  821. self::$_buckets[$name] = $b;
  822. return $b;
  823. }
  824. }