1
0

Bootstrap.php 27 KB

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