Bootstrap.php 29 KB

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