Configuration.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php declare(strict_types=1);
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. */
  11. namespace PrivateBin;
  12. use Exception;
  13. /**
  14. * Configuration
  15. *
  16. * parses configuration file, ensures default values present
  17. */
  18. class Configuration
  19. {
  20. /**
  21. * parsed configuration
  22. *
  23. * @var array
  24. */
  25. private $_configuration;
  26. /**
  27. * default configuration
  28. *
  29. * @var array
  30. */
  31. private static $_defaults = array(
  32. 'main' => array(
  33. 'name' => 'PrivateBin',
  34. 'basepath' => '',
  35. 'discussion' => true,
  36. 'opendiscussion' => false,
  37. 'discussiondatedisplay' => true,
  38. 'password' => true,
  39. 'fileupload' => false,
  40. 'burnafterreadingselected' => false,
  41. 'defaultformatter' => 'plaintext',
  42. 'syntaxhighlightingtheme' => '',
  43. 'sizelimit' => 10485760,
  44. 'templateselection' => false,
  45. 'template' => 'bootstrap',
  46. 'availabletemplates' => array(
  47. 'bootstrap5',
  48. 'bootstrap',
  49. 'bootstrap-page',
  50. 'bootstrap-dark',
  51. 'bootstrap-dark-page',
  52. 'bootstrap-compact',
  53. 'bootstrap-compact-page',
  54. 'page',
  55. ),
  56. 'info' => 'More information on the <a href=\'https://privatebin.info/\'>project page</a>.',
  57. 'notice' => '',
  58. 'languageselection' => false,
  59. 'languagedefault' => '',
  60. 'urlshortener' => '',
  61. 'qrcode' => true,
  62. 'email' => true,
  63. 'icon' => 'identicon',
  64. 'cspheader' => 'default-src \'none\'; base-uri \'self\'; form-action \'none\'; manifest-src \'self\'; connect-src * blob:; script-src \'self\' \'wasm-unsafe-eval\'; style-src \'self\'; font-src \'self\'; frame-ancestors \'none\'; frame-src blob:; img-src \'self\' data: blob:; media-src blob:; object-src blob:; sandbox allow-same-origin allow-scripts allow-forms allow-popups allow-modals allow-downloads',
  65. 'zerobincompatibility' => false,
  66. 'httpwarning' => true,
  67. 'compression' => 'zlib',
  68. ),
  69. 'expire' => array(
  70. 'default' => '1week',
  71. ),
  72. 'expire_options' => array(
  73. '5min' => 300,
  74. '10min' => 600,
  75. '1hour' => 3600,
  76. '1day' => 86400,
  77. '1week' => 604800,
  78. '1month' => 2592000,
  79. '1year' => 31536000,
  80. 'never' => 0,
  81. ),
  82. 'formatter_options' => array(
  83. 'plaintext' => 'Plain Text',
  84. 'syntaxhighlighting' => 'Source Code',
  85. 'markdown' => 'Markdown',
  86. ),
  87. 'traffic' => array(
  88. 'limit' => 10,
  89. 'header' => '',
  90. 'exempted' => '',
  91. 'creators' => '',
  92. ),
  93. 'purge' => array(
  94. 'limit' => 300,
  95. 'batchsize' => 10,
  96. ),
  97. 'model' => array(
  98. 'class' => 'Filesystem',
  99. ),
  100. 'model_options' => array(
  101. 'dir' => 'data',
  102. ),
  103. 'yourls' => array(
  104. 'signature' => '',
  105. 'apiurl' => '',
  106. ),
  107. // update this array when adding/changing/removing js files
  108. 'sri' => array(
  109. 'js/base-x-5.0.1.js' => 'sha512-FmhlnjIxQyxkkxQmzf0l6IRGsGbgyCdgqPxypFsEtHMF1naRqaLLo6mcyN5rEaT16nKx1PeJ4g7+07D6gnk/Tg==',
  110. 'js/base64-1.7.js' => 'sha512-JdwsSP3GyHR+jaCkns9CL9NTt4JUJqm/BsODGmYhBcj5EAPKcHYh+OiMfyHbcDLECe17TL0hjXADFkusAqiYgA==',
  111. 'js/bootstrap-3.4.1.js' => 'sha512-oBTprMeNEKCnqfuqKd6sbvFzmFQtlXS3e0C/RGFV0hD6QzhHV+ODfaQbAlmY6/q0ubbwlAM/nCJjkrgA3waLzg==',
  112. 'js/bootstrap-5.3.8.js' => 'sha512-BkZvJ5rZ3zbDCod5seWHpRGg+PRd6ZgE8Nua/OMtcxqm8Wtg0PqwhUUXK5bqvl3oclMt5O+3zjRVX0L+L2j7fA==',
  113. 'js/dark-mode-switch.js' => 'sha512-BhY7dNU14aDN5L+muoUmA66x0CkYUWkQT0nxhKBLP/o2d7jE025+dvWJa4OiYffBGEFgmhrD/Sp+QMkxGMTz2g==',
  114. 'js/jquery-3.7.1.js' => 'sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==',
  115. 'js/kjua-0.10.0.js' => 'sha512-BYj4xggowR7QD150VLSTRlzH62YPfhpIM+b/1EUEr7RQpdWAGKulxWnOvjFx1FUlba4m6ihpNYuQab51H6XlYg==',
  116. 'js/legacy.js' => 'sha512-iP69buypAHBJOgt7AyDcfaelVxBES9/k3dVfd6hPxTRizVRH2dijEpMWCt1D8OH4FNgytKsDI/J7+9y7IgXPaA==',
  117. 'js/prettify.js' => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==',
  118. 'js/privatebin.js' => 'sha512-3E8P4YPoOga6g/qexHVzjEczbeiQckJ8Wg4b7cvm5SW0CN6KlXSNvV3fb8FMFnroyHoYiLuPTrNFc9gXLJTN6A==',
  119. 'js/purify-3.3.0.js' => 'sha512-lsHD5zxs4lu/NDzaaibe27Vd2t7Cy9JQ3qDHUvDfb4oZvKoWDNEhwUY+4bT3R68cGgpgCYp8U1x2ifeVxqurdQ==',
  120. 'js/rawinflate-0.3.js' => 'sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==',
  121. 'js/showdown-2.1.0.js' => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',
  122. 'js/zlib-1.3.1-1.js' => 'sha512-5bU9IIP4PgBrOKLZvGWJD4kgfQrkTz8Z3Iqeu058mbQzW3mCumOU6M3UVbVZU9rrVoVwaW4cZK8U8h5xjF88eQ==',
  123. ),
  124. );
  125. /**
  126. * parse configuration file and ensure default configuration values are present
  127. *
  128. * @throws Exception
  129. */
  130. public function __construct()
  131. {
  132. $basePaths = array();
  133. $config = array();
  134. $configPath = getenv('CONFIG_PATH');
  135. if ($configPath !== false && !empty($configPath)) {
  136. $basePaths[] = $configPath;
  137. }
  138. $basePaths[] = PATH . 'cfg';
  139. foreach ($basePaths as $basePath) {
  140. $configFile = $basePath . DIRECTORY_SEPARATOR . 'conf.php';
  141. if (is_readable($configFile)) {
  142. $config = parse_ini_file($configFile, true);
  143. foreach (array('main', 'model', 'model_options') as $section) {
  144. if (!array_key_exists($section, $config)) {
  145. throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
  146. }
  147. }
  148. break;
  149. }
  150. }
  151. $opts = '_options';
  152. foreach (self::getDefaults() as $section => $values) {
  153. // fill missing sections with default values
  154. if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
  155. $this->_configuration[$section] = $values;
  156. if (array_key_exists('dir', $this->_configuration[$section])) {
  157. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  158. }
  159. continue;
  160. }
  161. // provide different defaults for database model
  162. elseif (
  163. $section == 'model_options' && in_array(
  164. $this->_configuration['model']['class'],
  165. array('Database', 'privatebin_db', 'zerobin_db')
  166. )
  167. ) {
  168. $values = array(
  169. 'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
  170. 'tbl' => null,
  171. 'usr' => null,
  172. 'pwd' => null,
  173. 'opt' => array(),
  174. );
  175. } elseif (
  176. $section == 'model_options' && in_array(
  177. $this->_configuration['model']['class'],
  178. array('GoogleCloudStorage')
  179. )
  180. ) {
  181. $values = array(
  182. 'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null,
  183. 'prefix' => 'pastes',
  184. 'uniformacl' => false,
  185. );
  186. } elseif (
  187. $section == 'model_options' && in_array(
  188. $this->_configuration['model']['class'],
  189. array('S3Storage')
  190. )
  191. ) {
  192. $values = array(
  193. 'region' => null,
  194. 'version' => null,
  195. 'endpoint' => null,
  196. 'accesskey' => null,
  197. 'secretkey' => null,
  198. 'use_path_style_endpoint' => null,
  199. 'bucket' => null,
  200. 'prefix' => '',
  201. );
  202. }
  203. // "*_options" sections don't require all defaults to be set
  204. if (
  205. $section !== 'model_options' &&
  206. ($from = strlen($section) - strlen($opts)) >= 0 &&
  207. strpos($section, $opts, $from) !== false
  208. ) {
  209. if (is_int(current($values))) {
  210. $config[$section] = array_map('intval', $config[$section]);
  211. }
  212. $this->_configuration[$section] = $config[$section];
  213. }
  214. // check for missing keys and set defaults if necessary
  215. else {
  216. // preserve configured SRI hashes
  217. if ($section == 'sri' && array_key_exists($section, $config)) {
  218. $this->_configuration[$section] = $config[$section];
  219. }
  220. foreach ($values as $key => $val) {
  221. if ($key == 'dir') {
  222. $val = PATH . $val;
  223. }
  224. $result = $val;
  225. if (array_key_exists($key, $config[$section])) {
  226. if ($val === null) {
  227. $result = $config[$section][$key];
  228. } elseif (is_bool($val)) {
  229. $val = strtolower($config[$section][$key]);
  230. if (in_array($val, array('true', 'yes', 'on'))) {
  231. $result = true;
  232. } elseif (in_array($val, array('false', 'no', 'off'))) {
  233. $result = false;
  234. } else {
  235. $result = (bool) $config[$section][$key];
  236. }
  237. } elseif (is_int($val)) {
  238. $result = (int) $config[$section][$key];
  239. } elseif (is_string($val) && !empty($config[$section][$key])) {
  240. $result = (string) $config[$section][$key];
  241. } elseif (is_array($val) && is_array($config[$section][$key])) {
  242. $result = $config[$section][$key];
  243. }
  244. }
  245. $this->_configuration[$section][$key] = $result;
  246. }
  247. }
  248. }
  249. // support for old config file format, before the fork was renamed and PSR-4 introduced
  250. $this->_configuration['model']['class'] = str_replace(
  251. 'zerobin_', 'privatebin_',
  252. $this->_configuration['model']['class']
  253. );
  254. $this->_configuration['model']['class'] = str_replace(
  255. array('privatebin_data', 'privatebin_db'),
  256. array('Filesystem', 'Database'),
  257. $this->_configuration['model']['class']
  258. );
  259. // ensure a valid expire default key is set
  260. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
  261. $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
  262. }
  263. // ensure the basepath ends in a slash, if one is set
  264. if (
  265. !empty($this->_configuration['main']['basepath']) &&
  266. substr_compare($this->_configuration['main']['basepath'], '/', -1) !== 0
  267. ) {
  268. $this->_configuration['main']['basepath'] .= '/';
  269. }
  270. }
  271. /**
  272. * get configuration as array
  273. *
  274. * @return array
  275. */
  276. public function get()
  277. {
  278. return $this->_configuration;
  279. }
  280. /**
  281. * get default configuration as array
  282. *
  283. * @return array
  284. */
  285. public static function getDefaults()
  286. {
  287. return self::$_defaults;
  288. }
  289. /**
  290. * get a key from the configuration, typically the main section or all keys
  291. *
  292. * @param string $key
  293. * @param string $section defaults to main
  294. * @throws Exception
  295. * @return mixed
  296. */
  297. public function getKey($key, $section = 'main')
  298. {
  299. $options = $this->getSection($section);
  300. if (!array_key_exists($key, $options)) {
  301. throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
  302. }
  303. return $this->_configuration[$section][$key];
  304. }
  305. /**
  306. * get a section from the configuration, must exist
  307. *
  308. * @param string $section
  309. * @throws Exception
  310. * @return mixed
  311. */
  312. public function getSection($section)
  313. {
  314. if (!array_key_exists($section, $this->_configuration)) {
  315. throw new Exception(I18n::_('%s requires configuration section [%s] to be present in configuration file.', I18n::_($this->getKey('name')), $section), 3);
  316. }
  317. return $this->_configuration[$section];
  318. }
  319. }