Bootstrap.php 30 KB

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