Bootstrap.php 29 KB

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