1
0

Bootstrap.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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 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 restoreObject(array $args = array())
  428. {
  429. throw new BadMethodCallException('not supported by this stub');
  430. }
  431. public function copyObject(array $args = array())
  432. {
  433. throw new BadMethodCallException('not supported by this stub');
  434. }
  435. public function rewriteObject(array $args = array())
  436. {
  437. throw new BadMethodCallException('not supported by this stub');
  438. }
  439. public function composeObject(array $args = array())
  440. {
  441. throw new BadMethodCallException('not supported by this stub');
  442. }
  443. public function getObject(array $args = array())
  444. {
  445. throw new BadMethodCallException('not supported by this stub');
  446. }
  447. public function listObjects(array $args = array())
  448. {
  449. throw new BadMethodCallException('not supported by this stub');
  450. }
  451. public function patchObject(array $args = array())
  452. {
  453. throw new BadMethodCallException('not supported by this stub');
  454. }
  455. public function downloadObject(array $args = array())
  456. {
  457. throw new BadMethodCallException('not supported by this stub');
  458. }
  459. public function insertObject(array $args = array())
  460. {
  461. throw new BadMethodCallException('not supported by this stub');
  462. }
  463. public function getNotification(array $args = array())
  464. {
  465. throw new BadMethodCallException('not supported by this stub');
  466. }
  467. public function deleteNotification(array $args = array())
  468. {
  469. throw new BadMethodCallException('not supported by this stub');
  470. }
  471. public function insertNotification(array $args = array())
  472. {
  473. throw new BadMethodCallException('not supported by this stub');
  474. }
  475. public function listNotifications(array $args = array())
  476. {
  477. throw new BadMethodCallException('not supported by this stub');
  478. }
  479. public function getServiceAccount(array $args = array())
  480. {
  481. throw new BadMethodCallException('not supported by this stub');
  482. }
  483. public function lockRetentionPolicy(array $args = array())
  484. {
  485. throw new BadMethodCallException('not supported by this stub');
  486. }
  487. public function createHmacKey(array $args = array())
  488. {
  489. throw new BadMethodCallException('not supported by this stub');
  490. }
  491. public function deleteHmacKey(array $args = array())
  492. {
  493. throw new BadMethodCallException('not supported by this stub');
  494. }
  495. public function getHmacKey(array $args = array())
  496. {
  497. throw new BadMethodCallException('not supported by this stub');
  498. }
  499. public function updateHmacKey(array $args = array())
  500. {
  501. throw new BadMethodCallException('not supported by this stub');
  502. }
  503. public function listHmacKeys(array $args = array())
  504. {
  505. throw new BadMethodCallException('not supported by this stub');
  506. }
  507. }
  508. /**
  509. * Class Helper provides unit tests pastes and comments of various formats
  510. */
  511. class Helper
  512. {
  513. /**
  514. * example ID of a paste
  515. *
  516. * @var string
  517. */
  518. private static $pasteid = '5b65a01b43987bc2';
  519. /**
  520. * example paste version 1
  521. *
  522. * @var array
  523. */
  524. private static $pasteV1 = array(
  525. 'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
  526. 'attachment' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  527. 'attachmentname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  528. 'meta' => array(
  529. 'formatter' => 'plaintext',
  530. 'postdate' => 1344803344,
  531. 'opendiscussion' => true,
  532. ),
  533. );
  534. /**
  535. * example paste version 2
  536. *
  537. * @var array
  538. */
  539. private static $pasteV2 = array(
  540. 'adata' => array(
  541. array(
  542. 'gMSNoLOk4z0RnmsYwXZ8mw==',
  543. 'TZO+JWuIuxs=',
  544. 100000,
  545. 256,
  546. 128,
  547. 'aes',
  548. 'gcm',
  549. 'zlib',
  550. ),
  551. 'plaintext',
  552. 1,
  553. 0,
  554. ),
  555. 'meta' => array(
  556. 'expire' => '5min',
  557. ),
  558. 'v' => 2,
  559. 'ct' => 'ME5JF/YBEijp2uYMzLZozbKtWc5wfy6R59NBb7SmRig=',
  560. );
  561. /**
  562. * example ID of a comment
  563. *
  564. * @var string
  565. */
  566. private static $commentid = '5a52eebf11c4c94b';
  567. /**
  568. * example comment
  569. *
  570. * @var array
  571. */
  572. private static $commentV1 = array(
  573. 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  574. 'meta' => array(
  575. 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  576. 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
  577. 'postdate' => 1344803528,
  578. ),
  579. );
  580. /**
  581. * JS files and their SRI hashes
  582. *
  583. * @var array
  584. */
  585. private static $hashes = array();
  586. /**
  587. * get example paste ID
  588. *
  589. * @return string
  590. */
  591. public static function getPasteId()
  592. {
  593. return self::$pasteid;
  594. }
  595. /**
  596. * get example paste, as stored on server
  597. *
  598. * @param int $version
  599. * @param array $meta
  600. * @return array
  601. */
  602. public static function getPaste($version = 2, array $meta = array())
  603. {
  604. $example = self::getPasteWithAttachment($version, $meta);
  605. // v1 has the attachment stored in a separate property
  606. if ($version === 1) {
  607. unset($example['attachment'], $example['attachmentname']);
  608. }
  609. return $example;
  610. }
  611. /**
  612. * get example paste with attachment, as stored on server
  613. *
  614. * @param int $version
  615. * @param array $meta
  616. * @return array
  617. */
  618. public static function getPasteWithAttachment($version = 2, array $meta = array())
  619. {
  620. $example = $version === 1 ? self::$pasteV1 : self::$pasteV2;
  621. $example['meta']['salt'] = ServerSalt::generate();
  622. $example['meta'] = array_merge($example['meta'], $meta);
  623. return $example;
  624. }
  625. /**
  626. * get example paste, as decoded from POST by the request object
  627. *
  628. * @param int $version
  629. * @param array $meta
  630. * @return array
  631. */
  632. public static function getPastePost($version = 2, array $meta = array())
  633. {
  634. $example = self::getPaste($version, $meta);
  635. if ($version == 2) {
  636. $example['meta'] = array('expire' => $example['meta']['expire']);
  637. } else {
  638. unset($example['meta']['postdate']);
  639. }
  640. return $example;
  641. }
  642. /**
  643. * get example paste, as received via POST by the user
  644. *
  645. * @param int $version
  646. * @param array $meta
  647. * @return array
  648. */
  649. public static function getPasteJson($version = 2, array $meta = array())
  650. {
  651. return json_encode(self::getPastePost($version, $meta));
  652. }
  653. /**
  654. * get example paste ID
  655. *
  656. * @return string
  657. */
  658. public static function getCommentId()
  659. {
  660. return self::$commentid;
  661. }
  662. /**
  663. * get example comment, as stored on server
  664. *
  665. * @param int $version
  666. * @param array $meta
  667. * @return array
  668. */
  669. public static function getComment($version = 2, array $meta = array())
  670. {
  671. $example = $version === 1 ? self::$commentV1 : self::$pasteV2;
  672. if ($version === 2) {
  673. $example['adata'] = $example['adata'][0];
  674. $example['pasteid'] = $example['parentid'] = self::getPasteId();
  675. $example['meta']['created'] = self::$commentV1['meta']['postdate'];
  676. $example['meta']['icon'] = self::$commentV1['meta']['vizhash'];
  677. unset($example['meta']['expire']);
  678. }
  679. $example['meta'] = array_merge($example['meta'], $meta);
  680. return $example;
  681. }
  682. /**
  683. * get example comment, as decoded from POST by the request object
  684. *
  685. * @param int $version
  686. * @return array
  687. */
  688. public static function getCommentPost()
  689. {
  690. $example = self::getComment();
  691. unset($example['meta']);
  692. return $example;
  693. }
  694. /**
  695. * get example comment, as received via POST by user
  696. *
  697. * @param int $version
  698. * @return array
  699. */
  700. public static function getCommentJson()
  701. {
  702. return json_encode(self::getCommentPost());
  703. }
  704. /**
  705. * delete directory and all its contents recursively
  706. *
  707. * @param string $path
  708. * @throws Exception
  709. */
  710. public static function rmDir($path)
  711. {
  712. if (is_dir($path)) {
  713. $path .= DIRECTORY_SEPARATOR;
  714. $dir = dir($path);
  715. while (false !== ($file = $dir->read())) {
  716. if ($file != '.' && $file != '..') {
  717. if (is_dir($path . $file)) {
  718. self::rmDir($path . $file);
  719. } elseif (is_file($path . $file)) {
  720. if (!unlink($path . $file)) {
  721. throw new Exception('Error deleting file "' . $path . $file . '".');
  722. }
  723. }
  724. }
  725. }
  726. $dir->close();
  727. if (!rmdir($path)) {
  728. throw new Exception('Error deleting directory "' . $path . '".');
  729. }
  730. }
  731. }
  732. /**
  733. * create a backup of the config file
  734. *
  735. * @return void
  736. */
  737. public static function confBackup()
  738. {
  739. if (!is_file(CONF . '.bak') && is_file(CONF)) {
  740. rename(CONF, CONF . '.bak');
  741. }
  742. if (!is_file(CONF_SAMPLE . '.bak') && is_file(CONF_SAMPLE)) {
  743. copy(CONF_SAMPLE, CONF_SAMPLE . '.bak');
  744. }
  745. }
  746. /**
  747. * restor backup of the config file
  748. *
  749. * @return void
  750. */
  751. public static function confRestore()
  752. {
  753. if (is_file(CONF . '.bak')) {
  754. rename(CONF . '.bak', CONF);
  755. }
  756. if (is_file(CONF_SAMPLE . '.bak')) {
  757. rename(CONF_SAMPLE . '.bak', CONF_SAMPLE);
  758. }
  759. }
  760. /**
  761. * create ini file
  762. *
  763. * @param string $pathToFile
  764. * @param array $values
  765. */
  766. public static function createIniFile($pathToFile, array $values)
  767. {
  768. if (count($values)) {
  769. @unlink($pathToFile);
  770. $ini = fopen($pathToFile, 'a');
  771. foreach ($values as $section => $options) {
  772. fwrite($ini, "[$section]" . PHP_EOL);
  773. foreach ($options as $option => $setting) {
  774. if (is_null($setting)) {
  775. continue;
  776. } elseif (is_string($setting)) {
  777. $setting = '"' . $setting . '"';
  778. } elseif (is_array($setting)) {
  779. foreach ($setting as $key => $value) {
  780. if (is_null($value)) {
  781. $value = 'null';
  782. } elseif (is_string($value)) {
  783. $value = '"' . $value . '"';
  784. } else {
  785. $value = var_export($value, true);
  786. }
  787. fwrite($ini, $option . "[$key] = $value" . PHP_EOL);
  788. }
  789. continue;
  790. } else {
  791. $setting = var_export($setting, true);
  792. }
  793. fwrite($ini, "$option = $setting" . PHP_EOL);
  794. }
  795. fwrite($ini, PHP_EOL);
  796. }
  797. fclose($ini);
  798. }
  799. }
  800. /**
  801. * a var_export that returns arrays without line breaks
  802. * by linus@flowingcreativity.net via php.net
  803. *
  804. * @param mixed $var
  805. * @param bool $return
  806. * @return void|string
  807. */
  808. public static function varExportMin($var, $return = false)
  809. {
  810. if (is_array($var)) {
  811. $toImplode = array();
  812. foreach ($var as $key => $value) {
  813. $toImplode[] = var_export($key, true) . ' => ' . self::varExportMin($value, true);
  814. }
  815. $code = 'array(' . implode(', ', $toImplode) . ')';
  816. if ($return) {
  817. return $code;
  818. } else {
  819. echo $code;
  820. }
  821. } else {
  822. return var_export($var, $return);
  823. }
  824. }
  825. /**
  826. * update all templates with the latest SRI hashes for all JS files
  827. *
  828. * @return void
  829. */
  830. public static function updateSubresourceIntegrity()
  831. {
  832. $dir = dir(PATH . 'js');
  833. while (false !== ($file = $dir->read())) {
  834. if (substr($file, -3) === '.js') {
  835. self::$hashes[$file] = base64_encode(
  836. hash('sha512', file_get_contents(
  837. PATH . 'js' . DIRECTORY_SEPARATOR . $file
  838. ), true)
  839. );
  840. }
  841. }
  842. $dir = dir(PATH . 'tpl');
  843. while (false !== ($file = $dir->read())) {
  844. if (substr($file, -4) === '.php') {
  845. $content = file_get_contents(
  846. PATH . 'tpl' . DIRECTORY_SEPARATOR . $file
  847. );
  848. $content = preg_replace_callback(
  849. '#<script ([^>]+) src="js/([a-z0-9.-]+.js)([^"]*)"( integrity="[^"]+" crossorigin="[^"]+")?></script>#',
  850. function ($matches) {
  851. if (array_key_exists($matches[2], Helper::$hashes)) {
  852. return '<script ' . $matches[1] . ' src="js/' .
  853. $matches[2] . $matches[3] .
  854. '" integrity="sha512-' . Helper::$hashes[$matches[2]] .
  855. '" crossorigin="anonymous"></script>';
  856. } else {
  857. return $matches[0];
  858. }
  859. },
  860. $content
  861. );
  862. file_put_contents(
  863. PATH . 'tpl' . DIRECTORY_SEPARATOR . $file,
  864. $content
  865. );
  866. }
  867. }
  868. }
  869. }