Configuration.php 13 KB

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