1
0

Bootstrap.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. use PrivateBin\Persistence\ServerSalt;
  3. error_reporting(E_ALL | E_STRICT);
  4. // change this, if your php files and data is outside of your webservers document root
  5. if (!defined('PUBLIC_PATH')) {
  6. define('PUBLIC_PATH', '..');
  7. }
  8. if (!defined('PATH')) {
  9. define('PATH', '..' . DIRECTORY_SEPARATOR);
  10. }
  11. if (!defined('CONF')) {
  12. define('CONF', PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.php');
  13. }
  14. if (!defined('CONF_SAMPLE')) {
  15. define('CONF_SAMPLE', PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.sample.php');
  16. }
  17. require PATH . 'vendor/autoload.php';
  18. Helper::updateSubresourceIntegrity();
  19. class Helper
  20. {
  21. /**
  22. * example ID of a paste
  23. *
  24. * @var string
  25. */
  26. private static $pasteid = '5b65a01b43987bc2';
  27. /**
  28. * example paste version 1
  29. *
  30. * @var array
  31. */
  32. private static $pasteV1 = array(
  33. 'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
  34. 'attachment' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  35. 'attachmentname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  36. 'meta' => array(
  37. 'formatter' => 'plaintext',
  38. 'postdate' => 1344803344,
  39. 'opendiscussion' => true,
  40. ),
  41. );
  42. /**
  43. * example paste version 2
  44. *
  45. * @var array
  46. */
  47. private static $pasteV2 = array(
  48. 'adata' => array(
  49. array(
  50. 'gMSNoLOk4z0RnmsYwXZ8mw==',
  51. 'TZO+JWuIuxs=',
  52. 100000,
  53. 256,
  54. 128,
  55. 'aes',
  56. 'gcm',
  57. 'zlib',
  58. ),
  59. 'plaintext',
  60. 1,
  61. 0,
  62. ),
  63. 'meta' => array(
  64. 'expire' => '5min',
  65. 'created' => 1344803344,
  66. ),
  67. 'v' => 2,
  68. 'ct' => 'ME5JF/YBEijp2uYMzLZozbKtWc5wfy6R59NBb7SmRig=',
  69. );
  70. /**
  71. * example ID of a comment
  72. *
  73. * @var string
  74. */
  75. private static $commentid = '5a52eebf11c4c94b';
  76. /**
  77. * example comment
  78. *
  79. * @var array
  80. */
  81. private static $commentV1 = array(
  82. 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  83. 'meta' => array(
  84. 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  85. 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
  86. 'postdate' => 1344803528,
  87. ),
  88. );
  89. /**
  90. * JS files and their SRI hashes
  91. *
  92. * @var array
  93. */
  94. private static $hashes = array();
  95. /**
  96. * get example paste ID
  97. *
  98. * @return string
  99. */
  100. public static function getPasteId()
  101. {
  102. return version_compare(PHP_VERSION, '5.6', '<') ? hash('fnv164', self::$pasteV2['ct']) : self::$pasteid;
  103. }
  104. /**
  105. * get example paste, as stored on server
  106. *
  107. * @param int $version
  108. * @param array $meta
  109. * @return array
  110. */
  111. public static function getPaste($version = 2, array $meta = array())
  112. {
  113. $example = self::getPasteWithAttachment($version, $meta);
  114. // v1 has the attachment stored in a separate property
  115. if ($version === 1) {
  116. unset($example['attachment'], $example['attachmentname']);
  117. }
  118. return $example;
  119. }
  120. /**
  121. * get example paste with attachment, as stored on server
  122. *
  123. * @param int $version
  124. * @param array $meta
  125. * @return array
  126. */
  127. public static function getPasteWithAttachment($version = 2, array $meta = array())
  128. {
  129. $example = $version === 1 ? self::$pasteV1 : self::$pasteV2;
  130. $example['meta']['salt'] = ServerSalt::generate();
  131. $example['meta'] = array_merge($example['meta'], $meta);
  132. return $example;
  133. }
  134. /**
  135. * get example paste, as decoded from POST by the request object
  136. *
  137. * @param int $version
  138. * @param array $meta
  139. * @return array
  140. */
  141. public static function getPastePost($version = 2, array $meta = array())
  142. {
  143. $example = self::getPaste($version, $meta);
  144. $example['meta'] = array('expire' => $example['meta']['expire']);
  145. return $example;
  146. }
  147. /**
  148. * get example paste, as received via POST by the user
  149. *
  150. * @param int $version
  151. * @param array $meta
  152. * @return array
  153. */
  154. public static function getPasteJson($version = 2, array $meta = array())
  155. {
  156. return json_encode(self::getPastePost($version, $meta));
  157. }
  158. /**
  159. * get example paste ID
  160. *
  161. * @return string
  162. */
  163. public static function getCommentId()
  164. {
  165. return self::$commentid;
  166. }
  167. /**
  168. * get example comment, as stored on server
  169. *
  170. * @param int $version
  171. * @param array $meta
  172. * @return array
  173. */
  174. public static function getComment($version = 2, array $meta = array())
  175. {
  176. $example = $version === 1 ? self::$commentV1 : self::$pasteV2;
  177. if ($version === 2) {
  178. $example['pasteid'] = $example['parentid'] = self::getPasteId();
  179. $example['meta']['created'] = self::$commentV1['meta']['postdate'];
  180. $example['meta']['icon'] = self::$commentV1['meta']['vizhash'];
  181. unset($example['meta']['expire']);
  182. }
  183. $example['meta'] = array_merge($example['meta'], $meta);
  184. return $example;
  185. }
  186. /**
  187. * get example comment, as decoded from POST by the request object
  188. *
  189. * @param int $version
  190. * @return array
  191. */
  192. public static function getCommentPost()
  193. {
  194. $example = self::getComment();
  195. unset($example['meta']);
  196. return $example;
  197. }
  198. /**
  199. * get example comment, as received via POST by user
  200. *
  201. * @param int $version
  202. * @return array
  203. */
  204. public static function getCommentJson()
  205. {
  206. return json_encode(self::getCommentPost());
  207. }
  208. /**
  209. * delete directory and all its contents recursively
  210. *
  211. * @param string $path
  212. * @throws Exception
  213. */
  214. public static function rmDir($path)
  215. {
  216. if (is_dir($path)) {
  217. $path .= DIRECTORY_SEPARATOR;
  218. $dir = dir($path);
  219. while (false !== ($file = $dir->read())) {
  220. if ($file != '.' && $file != '..') {
  221. if (is_dir($path . $file)) {
  222. self::rmDir($path . $file);
  223. } elseif (is_file($path . $file)) {
  224. if (!unlink($path . $file)) {
  225. throw new Exception('Error deleting file "' . $path . $file . '".');
  226. }
  227. }
  228. }
  229. }
  230. $dir->close();
  231. if (!rmdir($path)) {
  232. throw new Exception('Error deleting directory "' . $path . '".');
  233. }
  234. }
  235. }
  236. /**
  237. * create a backup of the config file
  238. *
  239. * @return void
  240. */
  241. public static function confBackup()
  242. {
  243. if (!is_file(CONF . '.bak') && is_file(CONF)) {
  244. rename(CONF, CONF . '.bak');
  245. }
  246. if (!is_file(CONF_SAMPLE . '.bak') && is_file(CONF_SAMPLE)) {
  247. copy(CONF_SAMPLE, CONF_SAMPLE . '.bak');
  248. }
  249. }
  250. /**
  251. * restor backup of the config file
  252. *
  253. * @return void
  254. */
  255. public static function confRestore()
  256. {
  257. if (is_file(CONF . '.bak')) {
  258. rename(CONF . '.bak', CONF);
  259. }
  260. if (is_file(CONF_SAMPLE . '.bak')) {
  261. rename(CONF_SAMPLE . '.bak', CONF_SAMPLE);
  262. }
  263. }
  264. /**
  265. * create ini file
  266. *
  267. * @param string $pathToFile
  268. * @param array $values
  269. */
  270. public static function createIniFile($pathToFile, array $values)
  271. {
  272. if (count($values)) {
  273. @unlink($pathToFile);
  274. $ini = fopen($pathToFile, 'a');
  275. foreach ($values as $section => $options) {
  276. fwrite($ini, "[$section]" . PHP_EOL);
  277. foreach ($options as $option => $setting) {
  278. if (is_null($setting)) {
  279. continue;
  280. } elseif (is_string($setting)) {
  281. $setting = '"' . $setting . '"';
  282. } elseif (is_array($setting)) {
  283. foreach ($setting as $key => $value) {
  284. if (is_null($value)) {
  285. $value = 'null';
  286. } elseif (is_string($value)) {
  287. $value = '"' . $value . '"';
  288. } else {
  289. $value = var_export($value, true);
  290. }
  291. fwrite($ini, $option . "[$key] = $value" . PHP_EOL);
  292. }
  293. continue;
  294. } else {
  295. $setting = var_export($setting, true);
  296. }
  297. fwrite($ini, "$option = $setting" . PHP_EOL);
  298. }
  299. fwrite($ini, PHP_EOL);
  300. }
  301. fclose($ini);
  302. }
  303. }
  304. /**
  305. * a var_export that returns arrays without line breaks
  306. * by linus@flowingcreativity.net via php.net
  307. *
  308. * @param mixed $var
  309. * @param bool $return
  310. * @return void|string
  311. */
  312. public static function varExportMin($var, $return = false)
  313. {
  314. if (is_array($var)) {
  315. $toImplode = array();
  316. foreach ($var as $key => $value) {
  317. $toImplode[] = var_export($key, true) . ' => ' . self::varExportMin($value, true);
  318. }
  319. $code = 'array(' . implode(', ', $toImplode) . ')';
  320. if ($return) {
  321. return $code;
  322. } else {
  323. echo $code;
  324. }
  325. } else {
  326. return var_export($var, $return);
  327. }
  328. }
  329. /**
  330. * update all templates with the latest SRI hashes for all JS files
  331. *
  332. * @return void
  333. */
  334. public static function updateSubresourceIntegrity()
  335. {
  336. $dir = dir(PATH . 'js');
  337. while (false !== ($file = $dir->read())) {
  338. if (substr($file, -3) === '.js') {
  339. self::$hashes[$file] = base64_encode(
  340. hash('sha512', file_get_contents(
  341. PATH . 'js' . DIRECTORY_SEPARATOR . $file
  342. ), true)
  343. );
  344. }
  345. }
  346. $dir = dir(PATH . 'tpl');
  347. while (false !== ($file = $dir->read())) {
  348. if (substr($file, -4) === '.php') {
  349. $content = file_get_contents(
  350. PATH . 'tpl' . DIRECTORY_SEPARATOR . $file
  351. );
  352. $content = preg_replace_callback(
  353. '#<script ([^>]+) src="js/([a-z0-9.-]+.js)([^"]*)"( integrity="[^"]+" crossorigin="[^"]+")?></script>#',
  354. function ($matches) {
  355. if (array_key_exists($matches[2], Helper::$hashes)) {
  356. return '<script ' . $matches[1] . ' src="js/' .
  357. $matches[2] . $matches[3] .
  358. '" integrity="sha512-' . Helper::$hashes[$matches[2]] .
  359. '" crossorigin="anonymous"></script>';
  360. } else {
  361. return $matches[0];
  362. }
  363. },
  364. $content
  365. );
  366. file_put_contents(
  367. PATH . 'tpl' . DIRECTORY_SEPARATOR . $file,
  368. $content
  369. );
  370. }
  371. }
  372. }
  373. }