Configuration.php 13 KB

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