Bootstrap.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 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. 'created' => 1344803344,
  558. ),
  559. 'v' => 2,
  560. 'ct' => 'ME5JF/YBEijp2uYMzLZozbKtWc5wfy6R59NBb7SmRig=',
  561. );
  562. /**
  563. * example ID of a comment
  564. *
  565. * @var string
  566. */
  567. private static $commentid = '5a52eebf11c4c94b';
  568. /**
  569. * example comment
  570. *
  571. * @var array
  572. */
  573. private static $commentV1 = array(
  574. 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  575. 'meta' => array(
  576. 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  577. 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
  578. 'postdate' => 1344803528,
  579. ),
  580. );
  581. /**
  582. * JS files and their SRI hashes
  583. *
  584. * @var array
  585. */
  586. private static $hashes = array();
  587. /**
  588. * get example paste ID
  589. *
  590. * @return string
  591. */
  592. public static function getPasteId()
  593. {
  594. return self::$pasteid;
  595. }
  596. /**
  597. * get example paste, as stored on server
  598. *
  599. * @param int $version
  600. * @param array $meta
  601. * @return array
  602. */
  603. public static function getPaste($version = 2, array $meta = array())
  604. {
  605. $example = self::getPasteWithAttachment($version, $meta);
  606. // v1 has the attachment stored in a separate property
  607. if ($version === 1) {
  608. unset($example['attachment'], $example['attachmentname']);
  609. }
  610. return $example;
  611. }
  612. /**
  613. * get example paste with attachment, as stored on server
  614. *
  615. * @param int $version
  616. * @param array $meta
  617. * @return array
  618. */
  619. public static function getPasteWithAttachment($version = 2, array $meta = array())
  620. {
  621. $example = $version === 1 ? self::$pasteV1 : self::$pasteV2;
  622. $example['meta']['salt'] = ServerSalt::generate();
  623. $example['meta'] = array_merge($example['meta'], $meta);
  624. return $example;
  625. }
  626. /**
  627. * get example paste, as decoded from POST by the request object
  628. *
  629. * @param int $version
  630. * @param array $meta
  631. * @return array
  632. */
  633. public static function getPastePost($version = 2, array $meta = array())
  634. {
  635. $example = self::getPaste($version, $meta);
  636. if ($version == 2) {
  637. $example['meta'] = array('expire' => $example['meta']['expire']);
  638. } else {
  639. unset($example['meta']['postdate']);
  640. }
  641. return $example;
  642. }
  643. /**
  644. * get example paste, as received via POST by the user
  645. *
  646. * @param int $version
  647. * @param array $meta
  648. * @return array
  649. */
  650. public static function getPasteJson($version = 2, array $meta = array())
  651. {
  652. return json_encode(self::getPastePost($version, $meta));
  653. }
  654. /**
  655. * get example paste ID
  656. *
  657. * @return string
  658. */
  659. public static function getCommentId()
  660. {
  661. return self::$commentid;
  662. }
  663. /**
  664. * get example comment, as stored on server
  665. *
  666. * @param int $version
  667. * @param array $meta
  668. * @return array
  669. */
  670. public static function getComment($version = 2, array $meta = array())
  671. {
  672. $example = $version === 1 ? self::$commentV1 : self::$pasteV2;
  673. if ($version === 2) {
  674. $example['adata'] = $example['adata'][0];
  675. $example['pasteid'] = $example['parentid'] = self::getPasteId();
  676. $example['meta']['created'] = self::$commentV1['meta']['postdate'];
  677. $example['meta']['icon'] = self::$commentV1['meta']['vizhash'];
  678. unset($example['meta']['expire']);
  679. }
  680. $example['meta'] = array_merge($example['meta'], $meta);
  681. return $example;
  682. }
  683. /**
  684. * get example comment, as decoded from POST by the request object
  685. *
  686. * @param int $version
  687. * @return array
  688. */
  689. public static function getCommentPost()
  690. {
  691. $example = self::getComment();
  692. unset($example['meta']);
  693. return $example;
  694. }
  695. /**
  696. * get example comment, as received via POST by user
  697. *
  698. * @param int $version
  699. * @return array
  700. */
  701. public static function getCommentJson()
  702. {
  703. return json_encode(self::getCommentPost());
  704. }
  705. /**
  706. * delete directory and all its contents recursively
  707. *
  708. * @param string $path
  709. * @throws Exception
  710. */
  711. public static function rmDir($path)
  712. {
  713. if (is_dir($path)) {
  714. $path .= DIRECTORY_SEPARATOR;
  715. $dir = dir($path);
  716. while (false !== ($file = $dir->read())) {
  717. if ($file != '.' && $file != '..') {
  718. if (is_dir($path . $file)) {
  719. self::rmDir($path . $file);
  720. } elseif (is_file($path . $file)) {
  721. if (!unlink($path . $file)) {
  722. throw new Exception('Error deleting file "' . $path . $file . '".');
  723. }
  724. }
  725. }
  726. }
  727. $dir->close();
  728. if (!rmdir($path)) {
  729. throw new Exception('Error deleting directory "' . $path . '".');
  730. }
  731. }
  732. }
  733. /**
  734. * create a backup of the config file
  735. *
  736. * @return void
  737. */
  738. public static function confBackup()
  739. {
  740. if (!is_file(CONF . '.bak') && is_file(CONF)) {
  741. rename(CONF, CONF . '.bak');
  742. }
  743. if (!is_file(CONF_SAMPLE . '.bak') && is_file(CONF_SAMPLE)) {
  744. copy(CONF_SAMPLE, CONF_SAMPLE . '.bak');
  745. }
  746. }
  747. /**
  748. * restor backup of the config file
  749. *
  750. * @return void
  751. */
  752. public static function confRestore()
  753. {
  754. if (is_file(CONF . '.bak')) {
  755. rename(CONF . '.bak', CONF);
  756. }
  757. if (is_file(CONF_SAMPLE . '.bak')) {
  758. rename(CONF_SAMPLE . '.bak', CONF_SAMPLE);
  759. }
  760. }
  761. /**
  762. * create ini file
  763. *
  764. * @param string $pathToFile
  765. * @param array $values
  766. */
  767. public static function createIniFile($pathToFile, array $values)
  768. {
  769. if (count($values)) {
  770. @unlink($pathToFile);
  771. $ini = fopen($pathToFile, 'a');
  772. foreach ($values as $section => $options) {
  773. fwrite($ini, "[$section]" . PHP_EOL);
  774. foreach ($options as $option => $setting) {
  775. if (is_null($setting)) {
  776. continue;
  777. } elseif (is_string($setting)) {
  778. $setting = '"' . $setting . '"';
  779. } elseif (is_array($setting)) {
  780. foreach ($setting as $key => $value) {
  781. if (is_null($value)) {
  782. $value = 'null';
  783. } elseif (is_string($value)) {
  784. $value = '"' . $value . '"';
  785. } else {
  786. $value = var_export($value, true);
  787. }
  788. fwrite($ini, $option . "[$key] = $value" . PHP_EOL);
  789. }
  790. continue;
  791. } else {
  792. $setting = var_export($setting, true);
  793. }
  794. fwrite($ini, "$option = $setting" . PHP_EOL);
  795. }
  796. fwrite($ini, PHP_EOL);
  797. }
  798. fclose($ini);
  799. }
  800. }
  801. /**
  802. * a var_export that returns arrays without line breaks
  803. * by linus@flowingcreativity.net via php.net
  804. *
  805. * @param mixed $var
  806. * @param bool $return
  807. * @return void|string
  808. */
  809. public static function varExportMin($var, $return = false)
  810. {
  811. if (is_array($var)) {
  812. $toImplode = array();
  813. foreach ($var as $key => $value) {
  814. $toImplode[] = var_export($key, true) . ' => ' . self::varExportMin($value, true);
  815. }
  816. $code = 'array(' . implode(', ', $toImplode) . ')';
  817. if ($return) {
  818. return $code;
  819. } else {
  820. echo $code;
  821. }
  822. } else {
  823. return var_export($var, $return);
  824. }
  825. }
  826. /**
  827. * update all templates with the latest SRI hashes for all JS files
  828. *
  829. * @return void
  830. */
  831. public static function updateSubresourceIntegrity()
  832. {
  833. $dir = dir(PATH . 'js');
  834. while (false !== ($file = $dir->read())) {
  835. if (substr($file, -3) === '.js') {
  836. self::$hashes[$file] = base64_encode(
  837. hash('sha512', file_get_contents(
  838. PATH . 'js' . DIRECTORY_SEPARATOR . $file
  839. ), true)
  840. );
  841. }
  842. }
  843. $dir = dir(PATH . 'tpl');
  844. while (false !== ($file = $dir->read())) {
  845. if (substr($file, -4) === '.php') {
  846. $content = file_get_contents(
  847. PATH . 'tpl' . DIRECTORY_SEPARATOR . $file
  848. );
  849. $content = preg_replace_callback(
  850. '#<script ([^>]+) src="js/([a-z0-9.-]+.js)([^"]*)"( integrity="[^"]+" crossorigin="[^"]+")?></script>#',
  851. function ($matches) {
  852. if (array_key_exists($matches[2], Helper::$hashes)) {
  853. return '<script ' . $matches[1] . ' src="js/' .
  854. $matches[2] . $matches[3] .
  855. '" integrity="sha512-' . Helper::$hashes[$matches[2]] .
  856. '" crossorigin="anonymous"></script>';
  857. } else {
  858. return $matches[0];
  859. }
  860. },
  861. $content
  862. );
  863. file_put_contents(
  864. PATH . 'tpl' . DIRECTORY_SEPARATOR . $file,
  865. $content
  866. );
  867. }
  868. }
  869. }
  870. }