Configuration.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. protected $_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' => 'bootstrap5',
  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. 'shortenbydefault' => false,
  61. 'qrcode' => true,
  62. 'email' => true,
  63. 'icon' => 'jdenticon',
  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-modals allow-downloads',
  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-5.0.1.js' => 'sha512-FmhlnjIxQyxkkxQmzf0l6IRGsGbgyCdgqPxypFsEtHMF1naRqaLLo6mcyN5rEaT16nKx1PeJ4g7+07D6gnk/Tg==',
  109. 'js/bootstrap-3.4.1.js' => 'sha512-oBTprMeNEKCnqfuqKd6sbvFzmFQtlXS3e0C/RGFV0hD6QzhHV+ODfaQbAlmY6/q0ubbwlAM/nCJjkrgA3waLzg==',
  110. 'js/bootstrap-5.3.7.js' => 'sha512-UqmrCkPcp6WOB9cC/NB5GB7vQd2/sB70bLpFk0bqHz/WQIFucjAM0vFNI4xp8B7jJ8KIUWPblNAS/M30AHKSzA==',
  111. 'js/dark-mode-switch.js' => 'sha512-BhY7dNU14aDN5L+muoUmA66x0CkYUWkQT0nxhKBLP/o2d7jE025+dvWJa4OiYffBGEFgmhrD/Sp+QMkxGMTz2g==',
  112. 'js/jquery-3.7.1.js' => 'sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==',
  113. 'js/kjua-0.10.0.js' => 'sha512-BYj4xggowR7QD150VLSTRlzH62YPfhpIM+b/1EUEr7RQpdWAGKulxWnOvjFx1FUlba4m6ihpNYuQab51H6XlYg==',
  114. 'js/legacy.js' => 'sha512-08+subq1Lo+r+la5ENqeXiMgNJcVaaTtBIFGkrjziSpvtgCId3Jtin4/OkSdHYSoeztwwIab8uvCzPKHta6puQ==',
  115. 'js/prettify.js' => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==',
  116. 'js/privatebin.js' => 'sha512-Cor4acli/veLhX81YVTGQNkQ+poXsz0gRQUmLeJ6WebIXUlUi7ZQZ6lk1oIYesnBG3cV/stVT07cdVsByXoaJA==',
  117. 'js/purify-3.2.6.js' => 'sha512-zqwL4OoBLFx89QPewkz4Lz5CSA2ktU+f31fuECkF0iK3Id5qd3Zpq5dMby8KwHjIEpsUgOqwF58cnmcaNem0EA==',
  118. 'js/showdown-2.1.0.js' => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',
  119. 'js/zlib-1.3.1-1.js' => 'sha512-5bU9IIP4PgBrOKLZvGWJD4kgfQrkTz8Z3Iqeu058mbQzW3mCumOU6M3UVbVZU9rrVoVwaW4cZK8U8h5xjF88eQ==',
  120. ),
  121. );
  122. /**
  123. * parse configuration file and ensure default configuration values are present
  124. *
  125. * @throws Exception
  126. */
  127. public function __construct()
  128. {
  129. $basePaths = array();
  130. $config = array();
  131. $configPath = getenv('CONFIG_PATH');
  132. if ($configPath !== false && !empty($configPath)) {
  133. $basePaths[] = $configPath;
  134. }
  135. $basePaths[] = PATH . 'cfg';
  136. foreach ($basePaths as $basePath) {
  137. $configFile = $basePath . DIRECTORY_SEPARATOR . 'conf.php';
  138. if (is_readable($configFile)) {
  139. $config = parse_ini_file($configFile, true);
  140. foreach (array('main', 'model', 'model_options') as $section) {
  141. if (!array_key_exists($section, $config)) {
  142. throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
  143. }
  144. }
  145. break;
  146. }
  147. }
  148. $opts = '_options';
  149. foreach (self::getDefaults() as $section => $values) {
  150. // fill missing sections with default values
  151. if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
  152. $this->_configuration[$section] = $values;
  153. if (array_key_exists('dir', $this->_configuration[$section])) {
  154. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  155. }
  156. continue;
  157. }
  158. // provide different defaults for database model
  159. elseif (
  160. $section == 'model_options' &&
  161. $this->_configuration['model']['class'] === 'Database'
  162. ) {
  163. $values = array(
  164. 'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
  165. 'tbl' => null,
  166. 'usr' => null,
  167. 'pwd' => null,
  168. 'opt' => array(),
  169. );
  170. } elseif (
  171. $section == 'model_options' &&
  172. $this->_configuration['model']['class'] === 'GoogleCloudStorage'
  173. ) {
  174. $values = array(
  175. 'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null,
  176. 'prefix' => 'pastes',
  177. 'uniformacl' => false,
  178. );
  179. } elseif (
  180. $section == 'model_options' &&
  181. $this->_configuration['model']['class'] === 'S3Storage'
  182. ) {
  183. $values = array(
  184. 'region' => null,
  185. 'version' => null,
  186. 'endpoint' => null,
  187. 'accesskey' => null,
  188. 'secretkey' => null,
  189. 'use_path_style_endpoint' => null,
  190. 'bucket' => null,
  191. 'prefix' => '',
  192. );
  193. }
  194. // "*_options" sections don't require all defaults to be set
  195. if (
  196. $section !== 'model_options' &&
  197. ($from = strlen($section) - strlen($opts)) >= 0 &&
  198. strpos($section, $opts, $from) !== false
  199. ) {
  200. if (is_int(current($values))) {
  201. $config[$section] = array_map('intval', $config[$section]);
  202. }
  203. $this->_configuration[$section] = $config[$section];
  204. }
  205. // check for missing keys and set defaults if necessary
  206. else {
  207. // preserve configured SRI hashes
  208. if ($section == 'sri' && array_key_exists($section, $config)) {
  209. $this->_configuration[$section] = $config[$section];
  210. }
  211. foreach ($values as $key => $val) {
  212. if ($key == 'dir') {
  213. $val = PATH . $val;
  214. }
  215. $result = $val;
  216. if (array_key_exists($key, $config[$section])) {
  217. if ($val === null) {
  218. $result = $config[$section][$key];
  219. } elseif (is_bool($val)) {
  220. $val = strtolower($config[$section][$key]);
  221. if (in_array($val, array('true', 'yes', 'on'))) {
  222. $result = true;
  223. } elseif (in_array($val, array('false', 'no', 'off'))) {
  224. $result = false;
  225. } else {
  226. $result = (bool) $config[$section][$key];
  227. }
  228. } elseif (is_int($val)) {
  229. $result = (int) $config[$section][$key];
  230. } elseif (is_string($val) && !empty($config[$section][$key])) {
  231. $result = (string) $config[$section][$key];
  232. } elseif (is_array($val) && is_array($config[$section][$key])) {
  233. $result = $config[$section][$key];
  234. }
  235. }
  236. $this->_configuration[$section][$key] = $result;
  237. }
  238. }
  239. }
  240. // ensure a valid expire default key is set
  241. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
  242. $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
  243. }
  244. // ensure the basepath ends in a slash, if one is set
  245. if (
  246. !empty($this->_configuration['main']['basepath']) &&
  247. substr_compare($this->_configuration['main']['basepath'], '/', -1) !== 0
  248. ) {
  249. $this->_configuration['main']['basepath'] .= '/';
  250. }
  251. }
  252. /**
  253. * get configuration as array
  254. *
  255. * @return array
  256. */
  257. public function get()
  258. {
  259. return $this->_configuration;
  260. }
  261. /**
  262. * get default configuration as array
  263. *
  264. * @return array
  265. */
  266. public static function getDefaults()
  267. {
  268. return self::$_defaults;
  269. }
  270. /**
  271. * get a key from the configuration, typically the main section or all keys
  272. *
  273. * @param string $key
  274. * @param string $section defaults to main
  275. * @throws Exception
  276. * @return mixed
  277. */
  278. public function getKey($key, $section = 'main')
  279. {
  280. $options = $this->getSection($section);
  281. if (!array_key_exists($key, $options)) {
  282. throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
  283. }
  284. return $this->_configuration[$section][$key];
  285. }
  286. /**
  287. * get a section from the configuration, must exist
  288. *
  289. * @param string $section
  290. * @throws Exception
  291. * @return mixed
  292. */
  293. public function getSection($section)
  294. {
  295. if (!array_key_exists($section, $this->_configuration)) {
  296. throw new Exception(I18n::_('%s requires configuration section [%s] to be present in configuration file.', I18n::_($this->getKey('name')), $section), 3);
  297. }
  298. return $this->_configuration[$section];
  299. }
  300. }