Bootstrap.php 13 KB

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