Configuration.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. ),
  55. 'info' => 'More information on the <a href=\'https://privatebin.info/\'>project page</a>.',
  56. 'notice' => '',
  57. 'languageselection' => false,
  58. 'languagedefault' => '',
  59. 'urlshortener' => '',
  60. 'qrcode' => true,
  61. 'email' => true,
  62. 'icon' => 'jdenticon',
  63. '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-modals allow-downloads',
  64. 'zerobincompatibility' => false,
  65. 'httpwarning' => true,
  66. 'compression' => 'zlib',
  67. ),
  68. 'expire' => array(
  69. 'default' => '1week',
  70. ),
  71. 'expire_options' => array(
  72. '5min' => 300,
  73. '10min' => 600,
  74. '1hour' => 3600,
  75. '1day' => 86400,
  76. '1week' => 604800,
  77. '1month' => 2592000,
  78. '1year' => 31536000,
  79. 'never' => 0,
  80. ),
  81. 'formatter_options' => array(
  82. 'plaintext' => 'Plain Text',
  83. 'syntaxhighlighting' => 'Source Code',
  84. 'markdown' => 'Markdown',
  85. ),
  86. 'traffic' => array(
  87. 'limit' => 10,
  88. 'header' => '',
  89. 'exempted' => '',
  90. 'creators' => '',
  91. ),
  92. 'purge' => array(
  93. 'limit' => 300,
  94. 'batchsize' => 10,
  95. ),
  96. 'model' => array(
  97. 'class' => 'Filesystem',
  98. ),
  99. 'model_options' => array(
  100. 'dir' => 'data',
  101. ),
  102. 'yourls' => array(
  103. 'signature' => '',
  104. 'apiurl' => '',
  105. ),
  106. // update this array when adding/changing/removing js files
  107. 'sri' => array(
  108. 'js/base-x-4.0.0.js' => 'sha512-nNPg5IGCwwrveZ8cA/yMGr5HiRS5Ps2H+s0J/mKTPjCPWUgFGGw7M5nqdnPD3VsRwCVysUh3Y8OWjeSKGkEQJQ==',
  109. 'js/base64-1.7.js' => 'sha512-JdwsSP3GyHR+jaCkns9CL9NTt4JUJqm/BsODGmYhBcj5EAPKcHYh+OiMfyHbcDLECe17TL0hjXADFkusAqiYgA==',
  110. 'js/bootstrap-3.4.1.js' => 'sha512-oBTprMeNEKCnqfuqKd6sbvFzmFQtlXS3e0C/RGFV0hD6QzhHV+ODfaQbAlmY6/q0ubbwlAM/nCJjkrgA3waLzg==',
  111. 'js/bootstrap-5.3.3.js' => 'sha512-in2rcOpLTdJ7/pw5qjF4LWHFRtgoBDxXCy49H4YGOcVdGiPaQucGIbOqxt1JvmpvOpq3J/C7VTa0FlioakB2gQ==',
  112. 'js/dark-mode-switch.js' => 'sha512-BhY7dNU14aDN5L+muoUmA66x0CkYUWkQT0nxhKBLP/o2d7jE025+dvWJa4OiYffBGEFgmhrD/Sp+QMkxGMTz2g==',
  113. 'js/jquery-3.7.1.js' => 'sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==',
  114. 'js/kjua-0.9.0.js' => 'sha512-CVn7af+vTMBd9RjoS4QM5fpLFEOtBCoB0zPtaqIDC7sF4F8qgUSRFQQpIyEDGsr6yrjbuOLzdf20tkHHmpaqwQ==',
  115. 'js/legacy.js' => 'sha512-UxW/TOZKon83n6dk/09GsYKIyeO5LeBHokxyIq+r7KFS5KMBeIB/EM7NrkVYIezwZBaovnyNtY2d9tKFicRlXg==',
  116. 'js/prettify.js' => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==',
  117. 'js/privatebin.js' => 'sha512-ErR4jczmxS4WnJ878GLIFYVmVzfDD9BYrdwBM3oUe86kanUD7DfS+fYW23Jg4OHKgrmmJkpBRus9ylVoWqhrFw==',
  118. 'js/purify-3.2.6.js' => 'sha512-zqwL4OoBLFx89QPewkz4Lz5CSA2ktU+f31fuECkF0iK3Id5qd3Zpq5dMby8KwHjIEpsUgOqwF58cnmcaNem0EA==',
  119. 'js/rawinflate-0.3.js' => 'sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==',
  120. 'js/showdown-2.1.0.js' => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',
  121. 'js/zlib-1.3.1-1.js' => 'sha512-5bU9IIP4PgBrOKLZvGWJD4kgfQrkTz8Z3Iqeu058mbQzW3mCumOU6M3UVbVZU9rrVoVwaW4cZK8U8h5xjF88eQ==',
  122. ),
  123. );
  124. /**
  125. * parse configuration file and ensure default configuration values are present
  126. *
  127. * @throws Exception
  128. */
  129. public function __construct()
  130. {
  131. $basePaths = array();
  132. $config = array();
  133. $configPath = getenv('CONFIG_PATH');
  134. if ($configPath !== false && !empty($configPath)) {
  135. $basePaths[] = $configPath;
  136. }
  137. $basePaths[] = PATH . 'cfg';
  138. foreach ($basePaths as $basePath) {
  139. $configFile = $basePath . DIRECTORY_SEPARATOR . 'conf.php';
  140. if (is_readable($configFile)) {
  141. $config = parse_ini_file($configFile, true);
  142. foreach (array('main', 'model', 'model_options') as $section) {
  143. if (!array_key_exists($section, $config)) {
  144. throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
  145. }
  146. }
  147. break;
  148. }
  149. }
  150. $opts = '_options';
  151. foreach (self::getDefaults() as $section => $values) {
  152. // fill missing sections with default values
  153. if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
  154. $this->_configuration[$section] = $values;
  155. if (array_key_exists('dir', $this->_configuration[$section])) {
  156. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  157. }
  158. continue;
  159. }
  160. // provide different defaults for database model
  161. elseif (
  162. $section == 'model_options' && in_array(
  163. $this->_configuration['model']['class'],
  164. array('Database', 'privatebin_db', 'zerobin_db')
  165. )
  166. ) {
  167. $values = array(
  168. 'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
  169. 'tbl' => null,
  170. 'usr' => null,
  171. 'pwd' => null,
  172. 'opt' => array(),
  173. );
  174. } elseif (
  175. $section == 'model_options' && in_array(
  176. $this->_configuration['model']['class'],
  177. array('GoogleCloudStorage')
  178. )
  179. ) {
  180. $values = array(
  181. 'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null,
  182. 'prefix' => 'pastes',
  183. 'uniformacl' => false,
  184. );
  185. } elseif (
  186. $section == 'model_options' && in_array(
  187. $this->_configuration['model']['class'],
  188. array('S3Storage')
  189. )
  190. ) {
  191. $values = array(
  192. 'region' => null,
  193. 'version' => null,
  194. 'endpoint' => null,
  195. 'accesskey' => null,
  196. 'secretkey' => null,
  197. 'use_path_style_endpoint' => null,
  198. 'bucket' => null,
  199. 'prefix' => '',
  200. );
  201. }
  202. // "*_options" sections don't require all defaults to be set
  203. if (
  204. $section !== 'model_options' &&
  205. ($from = strlen($section) - strlen($opts)) >= 0 &&
  206. strpos($section, $opts, $from) !== false
  207. ) {
  208. if (is_int(current($values))) {
  209. $config[$section] = array_map('intval', $config[$section]);
  210. }
  211. $this->_configuration[$section] = $config[$section];
  212. }
  213. // check for missing keys and set defaults if necessary
  214. else {
  215. // preserve configured SRI hashes
  216. if ($section == 'sri' && array_key_exists($section, $config)) {
  217. $this->_configuration[$section] = $config[$section];
  218. }
  219. foreach ($values as $key => $val) {
  220. if ($key == 'dir') {
  221. $val = PATH . $val;
  222. }
  223. $result = $val;
  224. if (array_key_exists($key, $config[$section])) {
  225. if ($val === null) {
  226. $result = $config[$section][$key];
  227. } elseif (is_bool($val)) {
  228. $val = strtolower($config[$section][$key]);
  229. if (in_array($val, array('true', 'yes', 'on'))) {
  230. $result = true;
  231. } elseif (in_array($val, array('false', 'no', 'off'))) {
  232. $result = false;
  233. } else {
  234. $result = (bool) $config[$section][$key];
  235. }
  236. } elseif (is_int($val)) {
  237. $result = (int) $config[$section][$key];
  238. } elseif (is_string($val) && !empty($config[$section][$key])) {
  239. $result = (string) $config[$section][$key];
  240. } elseif (is_array($val) && is_array($config[$section][$key])) {
  241. $result = $config[$section][$key];
  242. }
  243. }
  244. $this->_configuration[$section][$key] = $result;
  245. }
  246. }
  247. }
  248. // support for old config file format, before the fork was renamed and PSR-4 introduced
  249. $this->_configuration['model']['class'] = str_replace(
  250. 'zerobin_', 'privatebin_',
  251. $this->_configuration['model']['class']
  252. );
  253. $this->_configuration['model']['class'] = str_replace(
  254. array('privatebin_data', 'privatebin_db'),
  255. array('Filesystem', 'Database'),
  256. $this->_configuration['model']['class']
  257. );
  258. // ensure a valid expire default key is set
  259. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
  260. $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
  261. }
  262. // ensure the basepath ends in a slash, if one is set
  263. if (
  264. !empty($this->_configuration['main']['basepath']) &&
  265. substr_compare($this->_configuration['main']['basepath'], '/', -1) !== 0
  266. ) {
  267. $this->_configuration['main']['basepath'] .= '/';
  268. }
  269. }
  270. /**
  271. * get configuration as array
  272. *
  273. * @return array
  274. */
  275. public function get()
  276. {
  277. return $this->_configuration;
  278. }
  279. /**
  280. * get default configuration as array
  281. *
  282. * @return array
  283. */
  284. public static function getDefaults()
  285. {
  286. return self::$_defaults;
  287. }
  288. /**
  289. * get a key from the configuration, typically the main section or all keys
  290. *
  291. * @param string $key
  292. * @param string $section defaults to main
  293. * @throws Exception
  294. * @return mixed
  295. */
  296. public function getKey($key, $section = 'main')
  297. {
  298. $options = $this->getSection($section);
  299. if (!array_key_exists($key, $options)) {
  300. throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
  301. }
  302. return $this->_configuration[$section][$key];
  303. }
  304. /**
  305. * get a section from the configuration, must exist
  306. *
  307. * @param string $section
  308. * @throws Exception
  309. * @return mixed
  310. */
  311. public function getSection($section)
  312. {
  313. if (!array_key_exists($section, $this->_configuration)) {
  314. throw new Exception(I18n::_('%s requires configuration section [%s] to be present in configuration file.', I18n::_($this->getKey('name')), $section), 3);
  315. }
  316. return $this->_configuration[$section];
  317. }
  318. }