Configuration.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. use PrivateBin\Exception\TranslatedException;
  14. /**
  15. * Configuration
  16. *
  17. * parses configuration file, ensures default values present
  18. */
  19. class Configuration
  20. {
  21. /**
  22. * parsed configuration
  23. *
  24. * @var array
  25. */
  26. protected $_configuration;
  27. /**
  28. * default configuration
  29. *
  30. * @var array
  31. */
  32. private static $_defaults = array(
  33. 'main' => array(
  34. 'name' => 'PrivateBin',
  35. 'basepath' => '',
  36. 'discussion' => true,
  37. 'opendiscussion' => false,
  38. 'discussiondatedisplay' => true,
  39. 'password' => true,
  40. 'fileupload' => false,
  41. 'burnafterreadingselected' => false,
  42. 'defaultformatter' => 'plaintext',
  43. 'syntaxhighlightingtheme' => '',
  44. 'sizelimit' => 10485760,
  45. 'templateselection' => false,
  46. 'template' => 'bootstrap5',
  47. 'availabletemplates' => array(
  48. 'bootstrap5',
  49. 'bootstrap',
  50. 'bootstrap-page',
  51. 'bootstrap-dark',
  52. 'bootstrap-dark-page',
  53. 'bootstrap-compact',
  54. 'bootstrap-compact-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. 'shortenbydefault' => false,
  62. 'qrcode' => true,
  63. 'email' => true,
  64. 'icon' => 'jdenticon',
  65. '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',
  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. 'shlink' => array(
  108. 'apikey' => '',
  109. 'apiurl' => '',
  110. ),
  111. // update this array when adding/changing/removing js files
  112. 'sri' => array(
  113. 'js/base-x-5.0.1.js' => 'sha512-FmhlnjIxQyxkkxQmzf0l6IRGsGbgyCdgqPxypFsEtHMF1naRqaLLo6mcyN5rEaT16nKx1PeJ4g7+07D6gnk/Tg==',
  114. 'js/bootstrap-3.4.1.js' => 'sha512-oBTprMeNEKCnqfuqKd6sbvFzmFQtlXS3e0C/RGFV0hD6QzhHV+ODfaQbAlmY6/q0ubbwlAM/nCJjkrgA3waLzg==',
  115. 'js/bootstrap-5.3.8.js' => 'sha512-BkZvJ5rZ3zbDCod5seWHpRGg+PRd6ZgE8Nua/OMtcxqm8Wtg0PqwhUUXK5bqvl3oclMt5O+3zjRVX0L+L2j7fA==',
  116. 'js/dark-mode-switch.js' => 'sha512-BhY7dNU14aDN5L+muoUmA66x0CkYUWkQT0nxhKBLP/o2d7jE025+dvWJa4OiYffBGEFgmhrD/Sp+QMkxGMTz2g==',
  117. 'js/jquery-3.7.1.js' => 'sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==',
  118. 'js/kjua-0.10.0.js' => 'sha512-BYj4xggowR7QD150VLSTRlzH62YPfhpIM+b/1EUEr7RQpdWAGKulxWnOvjFx1FUlba4m6ihpNYuQab51H6XlYg==',
  119. 'js/legacy.js' => 'sha512-RQEo1hxpNc37i+jz/D9/JiAZhG8GFx3+SNxjYnI7jUgirDIqrCSj6QPAAZeaidditcWzsJ3jxfEj5lVm7ZwTRQ==',
  120. 'js/prettify.js' => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==',
  121. 'js/privatebin.js' => 'sha512-U52jFm7xsPJsJ7M/sFF6GoEBLTQ4pcvNDN5z3JcH0P2xHokiV3YrhfSi6XgTnBANdnjAXmg9GfJf3Bs2yKAiug==',
  122. 'js/purify-3.4.1.js' => 'sha512-280a/Vb6fVFsYaeRrkuDp4EDmdYlt2XS+dlDEO/U9qljPrAraA2bIzHTNmP+9dpwPDDwTML+RS+h5iaagPwTzA==',
  123. 'js/showdown-2.1.0.js' => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',
  124. 'js/zlib-1.3.2.js' => 'sha512-RAhJgxg9siMIA8ky4c10Rc2zUgnK80olHB8Tt1IOYWY4Eh1WmrviQkDn+sgBlb38ZHq3tzufGC41kP360gmosQ==',
  125. 'js/zlib.js' => 'sha512-QOaEwssHqHRRcWJ2Un3Kl2Zhyprzl7T8zmsKN2FppFxW3VR+8UChYOx2iuL0HbXK42fuBWJm5PNQJxufulrt/w==',
  126. ),
  127. );
  128. /**
  129. * parse configuration file and ensure default configuration values are present
  130. *
  131. * @throws TranslatedException
  132. */
  133. public function __construct()
  134. {
  135. $basePaths = array();
  136. $config = array();
  137. $configPath = getenv('CONFIG_PATH');
  138. if ($configPath !== false && !empty($configPath)) {
  139. $basePaths[] = $configPath;
  140. }
  141. $basePaths[] = PATH . 'cfg';
  142. foreach ($basePaths as $basePath) {
  143. $configFile = $basePath . DIRECTORY_SEPARATOR . 'conf.php';
  144. if (is_readable($configFile)) {
  145. $config = parse_ini_file($configFile, true);
  146. foreach (array('main', 'model', 'model_options') as $section) {
  147. if (!array_key_exists($section, $config)) {
  148. $name = $config['main']['name'] ?? self::getDefaults()['main']['name'];
  149. throw new TranslatedException(array('%s requires configuration section [%s] to be present in configuration file.', I18n::_($name), $section), 2);
  150. }
  151. }
  152. break;
  153. }
  154. }
  155. $opts = '_options';
  156. foreach (self::getDefaults() as $section => $values) {
  157. // fill missing sections with default values
  158. if (!array_key_exists($section, $config) || count($config[$section]) === 0) {
  159. $this->_configuration[$section] = $values;
  160. if (array_key_exists('dir', $this->_configuration[$section])) {
  161. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  162. }
  163. continue;
  164. }
  165. // provide different defaults for database model
  166. elseif (
  167. $section === 'model_options' &&
  168. $this->_configuration['model']['class'] === 'Database'
  169. ) {
  170. $values = array(
  171. 'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
  172. 'tbl' => null,
  173. 'usr' => null,
  174. 'pwd' => null,
  175. 'opt' => array(),
  176. );
  177. } elseif (
  178. $section === 'model_options' &&
  179. $this->_configuration['model']['class'] === 'GoogleCloudStorage'
  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' &&
  188. $this->_configuration['model']['class'] === 'S3Storage'
  189. ) {
  190. $values = array(
  191. 'region' => null,
  192. 'version' => null,
  193. 'endpoint' => null,
  194. 'accesskey' => null,
  195. 'secretkey' => null,
  196. 'use_path_style_endpoint' => null,
  197. 'bucket' => null,
  198. 'prefix' => '',
  199. );
  200. }
  201. // "*_options" sections don't require all defaults to be set
  202. if (
  203. $section !== 'model_options' &&
  204. ($from = strlen($section) - strlen($opts)) >= 0 &&
  205. strpos($section, $opts, $from) !== false
  206. ) {
  207. if (is_int(current($values))) {
  208. $config[$section] = array_map('intval', $config[$section]);
  209. }
  210. $this->_configuration[$section] = $config[$section];
  211. }
  212. // check for missing keys and set defaults if necessary
  213. else {
  214. // preserve configured SRI hashes
  215. if ($section === 'sri' && array_key_exists($section, $config)) {
  216. $this->_configuration[$section] = $config[$section];
  217. }
  218. foreach ($values as $key => $val) {
  219. if ($key === 'dir') {
  220. $val = PATH . $val;
  221. }
  222. $result = $val;
  223. if (array_key_exists($key, $config[$section])) {
  224. if ($val === null) {
  225. $result = $config[$section][$key];
  226. } elseif (is_bool($val)) {
  227. $val = strtolower($config[$section][$key]);
  228. if (in_array($val, array('true', 'yes', 'on'))) {
  229. $result = true;
  230. } elseif (in_array($val, array('false', 'no', 'off'))) {
  231. $result = false;
  232. } else {
  233. $result = (bool) $config[$section][$key];
  234. }
  235. } elseif (is_int($val)) {
  236. $result = (int) $config[$section][$key];
  237. } elseif (is_string($val) && !empty($config[$section][$key])) {
  238. $result = (string) $config[$section][$key];
  239. } elseif (is_array($val) && is_array($config[$section][$key])) {
  240. $result = $config[$section][$key];
  241. }
  242. }
  243. $this->_configuration[$section][$key] = $result;
  244. }
  245. }
  246. }
  247. // ensure a valid expire default key is set
  248. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
  249. $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
  250. }
  251. // ensure the basepath ends in a slash, if one is set
  252. if (
  253. !empty($this->_configuration['main']['basepath']) &&
  254. substr_compare($this->_configuration['main']['basepath'], '/', -1) !== 0
  255. ) {
  256. $this->_configuration['main']['basepath'] .= '/';
  257. }
  258. }
  259. /**
  260. * get configuration as array
  261. *
  262. * @return array
  263. */
  264. public function get()
  265. {
  266. return $this->_configuration;
  267. }
  268. /**
  269. * get default configuration as array
  270. *
  271. * @return array
  272. */
  273. public static function getDefaults()
  274. {
  275. return self::$_defaults;
  276. }
  277. /**
  278. * get a key from the configuration, typically the main section or all keys
  279. *
  280. * @param string $key
  281. * @param string $section defaults to main
  282. * @throws Exception
  283. * @return mixed
  284. */
  285. public function getKey($key, $section = 'main')
  286. {
  287. $options = $this->getSection($section);
  288. if (!array_key_exists($key, $options)) {
  289. throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
  290. }
  291. return $this->_configuration[$section][$key];
  292. }
  293. /**
  294. * get a section from the configuration, must exist
  295. *
  296. * @param string $section
  297. * @throws TranslatedException
  298. * @return mixed
  299. */
  300. public function getSection($section)
  301. {
  302. if (!array_key_exists($section, $this->_configuration)) {
  303. throw new TranslatedException(array('%s requires configuration section [%s] to be present in configuration file.', I18n::_($this->getKey('name')), $section), 3);
  304. }
  305. return $this->_configuration[$section];
  306. }
  307. }