Bootstrap.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 = '5e9bc25c89fb3bf9';
  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. 0,
  61. 0
  62. ),
  63. 'meta' => array( 'expire' => '5min' ),
  64. 'v' => 2,
  65. 'ct' => 'ME5JF/YBEijp2uYMzLZozbKtWc5wfy6R59NBb7SmRig='
  66. );
  67. /**
  68. * example ID of a comment
  69. *
  70. * @var string
  71. */
  72. private static $commentid = '5a52eebf11c4c94b';
  73. /**
  74. * example comment
  75. *
  76. * @var array
  77. */
  78. private static $comment = array(
  79. 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  80. 'meta' => array(
  81. 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  82. 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
  83. 'postdate' => 1344803528,
  84. ),
  85. );
  86. /**
  87. * JS files and their SRI hashes
  88. *
  89. * @var array
  90. */
  91. private static $hashes = array();
  92. /**
  93. * get example paste ID
  94. *
  95. * @return string
  96. */
  97. public static function getPasteId()
  98. {
  99. return self::$pasteid;
  100. }
  101. /**
  102. * get example paste
  103. *
  104. * @return array
  105. */
  106. public static function getPaste($meta = array())
  107. {
  108. $example = self::getPasteWithAttachment($meta);
  109. unset($example['attachment'], $example['attachmentname']);
  110. return $example;
  111. }
  112. /**
  113. * get example paste
  114. *
  115. * @return array
  116. */
  117. public static function getPasteWithAttachment($meta = array())
  118. {
  119. $example = self::$pasteV1;
  120. $example['meta']['salt'] = ServerSalt::generate();
  121. $example['meta'] = array_merge($example['meta'], $meta);
  122. return $example;
  123. }
  124. /**
  125. * get example paste
  126. *
  127. * @return array
  128. */
  129. public static function getPasteAsJson($meta = array())
  130. {
  131. $example = self::getPaste();
  132. // the JSON shouldn't contain the salt
  133. unset($example['meta']['salt']);
  134. if (count($meta)) {
  135. $example['meta'] = $meta;
  136. }
  137. $example['comments'] = array();
  138. $example['comment_count'] = 0;
  139. $example['comment_offset'] = 0;
  140. $example['@context'] = 'js/paste.jsonld';
  141. return json_encode($example);
  142. }
  143. /**
  144. * get example paste ID
  145. *
  146. * @return string
  147. */
  148. public static function getCommentId()
  149. {
  150. return self::$commentid;
  151. }
  152. /**
  153. * get example comment
  154. *
  155. * @return array
  156. */
  157. public static function getComment($meta = array())
  158. {
  159. $example = self::$comment;
  160. $example['meta'] = array_merge($example['meta'], $meta);
  161. return $example;
  162. }
  163. /**
  164. * get example comment
  165. *
  166. * @return array
  167. */
  168. public static function getCommentPost($meta = array())
  169. {
  170. $example = self::getComment($meta);
  171. $example['nickname'] = $example['meta']['nickname'];
  172. unset($example['meta']['nickname']);
  173. return $example;
  174. }
  175. /**
  176. * delete directory and all its contents recursively
  177. *
  178. * @param string $path
  179. * @throws Exception
  180. */
  181. public static function rmDir($path)
  182. {
  183. if (is_dir($path)) {
  184. $path .= DIRECTORY_SEPARATOR;
  185. $dir = dir($path);
  186. while (false !== ($file = $dir->read())) {
  187. if ($file != '.' && $file != '..') {
  188. if (is_dir($path . $file)) {
  189. self::rmDir($path . $file);
  190. } elseif (is_file($path . $file)) {
  191. if (!unlink($path . $file)) {
  192. throw new Exception('Error deleting file "' . $path . $file . '".');
  193. }
  194. }
  195. }
  196. }
  197. $dir->close();
  198. if (!rmdir($path)) {
  199. throw new Exception('Error deleting directory "' . $path . '".');
  200. }
  201. }
  202. }
  203. /**
  204. * create a backup of the config file
  205. *
  206. * @return void
  207. */
  208. public static function confBackup()
  209. {
  210. if (!is_file(CONF . '.bak') && is_file(CONF)) {
  211. rename(CONF, CONF . '.bak');
  212. }
  213. if (!is_file(CONF_SAMPLE . '.bak') && is_file(CONF_SAMPLE)) {
  214. copy(CONF_SAMPLE, CONF_SAMPLE . '.bak');
  215. }
  216. }
  217. /**
  218. * restor backup of the config file
  219. *
  220. * @return void
  221. */
  222. public static function confRestore()
  223. {
  224. if (is_file(CONF . '.bak')) {
  225. rename(CONF . '.bak', CONF);
  226. }
  227. if (is_file(CONF_SAMPLE . '.bak')) {
  228. rename(CONF_SAMPLE . '.bak', CONF_SAMPLE);
  229. }
  230. }
  231. /**
  232. * create ini file
  233. *
  234. * @param string $pathToFile
  235. * @param array $values
  236. */
  237. public static function createIniFile($pathToFile, $values)
  238. {
  239. if (count($values)) {
  240. @unlink($pathToFile);
  241. $ini = fopen($pathToFile, 'a');
  242. foreach ($values as $section => $options) {
  243. fwrite($ini, "[$section]" . PHP_EOL);
  244. foreach ($options as $option => $setting) {
  245. if (is_null($setting)) {
  246. continue;
  247. } elseif (is_string($setting)) {
  248. $setting = '"' . $setting . '"';
  249. } elseif (is_array($setting)) {
  250. foreach ($setting as $key => $value) {
  251. if (is_null($value)) {
  252. $value = 'null';
  253. } elseif (is_string($value)) {
  254. $value = '"' . $value . '"';
  255. } else {
  256. $value = var_export($value, true);
  257. }
  258. fwrite($ini, $option . "[$key] = $value" . PHP_EOL);
  259. }
  260. continue;
  261. } else {
  262. $setting = var_export($setting, true);
  263. }
  264. fwrite($ini, "$option = $setting" . PHP_EOL);
  265. }
  266. fwrite($ini, PHP_EOL);
  267. }
  268. fclose($ini);
  269. }
  270. }
  271. /**
  272. * a var_export that returns arrays without line breaks
  273. * by linus@flowingcreativity.net via php.net
  274. *
  275. * @param mixed $var
  276. * @param bool $return
  277. * @return void|string
  278. */
  279. public static function varExportMin($var, $return = false)
  280. {
  281. if (is_array($var)) {
  282. $toImplode = array();
  283. foreach ($var as $key => $value) {
  284. $toImplode[] = var_export($key, true) . ' => ' . self::varExportMin($value, true);
  285. }
  286. $code = 'array(' . implode(', ', $toImplode) . ')';
  287. if ($return) {
  288. return $code;
  289. } else {
  290. echo $code;
  291. }
  292. } else {
  293. return var_export($var, $return);
  294. }
  295. }
  296. /**
  297. * update all templates with the latest SRI hashes for all JS files
  298. *
  299. * @return void
  300. */
  301. public static function updateSubresourceIntegrity()
  302. {
  303. $dir = dir(PATH . 'js');
  304. while (false !== ($file = $dir->read())) {
  305. if (substr($file, -3) === '.js') {
  306. self::$hashes[$file] = base64_encode(
  307. hash('sha512', file_get_contents(
  308. PATH . 'js' . DIRECTORY_SEPARATOR . $file
  309. ), true)
  310. );
  311. }
  312. }
  313. $dir = dir(PATH . 'tpl');
  314. while (false !== ($file = $dir->read())) {
  315. if (substr($file, -4) === '.php') {
  316. $content = file_get_contents(
  317. PATH . 'tpl' . DIRECTORY_SEPARATOR . $file
  318. );
  319. $content = preg_replace_callback(
  320. '#<script ([^>]+) src="js/([a-z0-9.-]+.js)([^"]*)"( integrity="[^"]+" crossorigin="[^"]+")?></script>#',
  321. function ($matches) {
  322. if (array_key_exists($matches[2], Helper::$hashes)) {
  323. return '<script ' . $matches[1] . ' src="js/' .
  324. $matches[2] . $matches[3] .
  325. '" integrity="sha512-' . Helper::$hashes[$matches[2]] .
  326. '" crossorigin="anonymous"></script>';
  327. } else {
  328. return $matches[0];
  329. }
  330. },
  331. $content
  332. );
  333. file_put_contents(
  334. PATH . 'tpl' . DIRECTORY_SEPARATOR . $file,
  335. $content
  336. );
  337. }
  338. }
  339. }
  340. }