1
0

Configuration.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 PDO;
  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. private $_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. 'template' => 'bootstrap',
  46. 'info' => 'More information on the <a href=\'https://privatebin.info/\'>project page</a>.',
  47. 'notice' => '',
  48. 'languageselection' => false,
  49. 'languagedefault' => '',
  50. 'urlshortener' => '',
  51. 'qrcode' => true,
  52. 'email' => true,
  53. 'icon' => 'identicon',
  54. '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',
  55. 'zerobincompatibility' => false,
  56. 'httpwarning' => true,
  57. 'compression' => 'zlib',
  58. ),
  59. 'expire' => array(
  60. 'default' => '1week',
  61. ),
  62. 'expire_options' => array(
  63. '5min' => 300,
  64. '10min' => 600,
  65. '1hour' => 3600,
  66. '1day' => 86400,
  67. '1week' => 604800,
  68. '1month' => 2592000,
  69. '1year' => 31536000,
  70. 'never' => 0,
  71. ),
  72. 'formatter_options' => array(
  73. 'plaintext' => 'Plain Text',
  74. 'syntaxhighlighting' => 'Source Code',
  75. 'markdown' => 'Markdown',
  76. ),
  77. 'traffic' => array(
  78. 'limit' => 10,
  79. 'header' => '',
  80. 'exempted' => '',
  81. 'creators' => '',
  82. ),
  83. 'purge' => array(
  84. 'limit' => 300,
  85. 'batchsize' => 10,
  86. ),
  87. 'model' => array(
  88. 'class' => 'Filesystem',
  89. ),
  90. 'model_options' => array(
  91. 'dir' => 'data',
  92. ),
  93. 'yourls' => array(
  94. 'signature' => '',
  95. 'apiurl' => '',
  96. ),
  97. // update this array when adding/changing/removing js files
  98. 'sri' => array(
  99. 'js/base-x-4.0.0.js' => 'sha512-nNPg5IGCwwrveZ8cA/yMGr5HiRS5Ps2H+s0J/mKTPjCPWUgFGGw7M5nqdnPD3VsRwCVysUh3Y8OWjeSKGkEQJQ==',
  100. 'js/base64-1.7.js' => 'sha512-JdwsSP3GyHR+jaCkns9CL9NTt4JUJqm/BsODGmYhBcj5EAPKcHYh+OiMfyHbcDLECe17TL0hjXADFkusAqiYgA==',
  101. 'js/bootstrap-3.4.1.js' => 'sha512-oBTprMeNEKCnqfuqKd6sbvFzmFQtlXS3e0C/RGFV0hD6QzhHV+ODfaQbAlmY6/q0ubbwlAM/nCJjkrgA3waLzg==',
  102. 'js/bootstrap-5.3.3.js' => 'sha512-in2rcOpLTdJ7/pw5qjF4LWHFRtgoBDxXCy49H4YGOcVdGiPaQucGIbOqxt1JvmpvOpq3J/C7VTa0FlioakB2gQ==',
  103. 'js/dark-mode-switch.js' => 'sha512-CCbdHdeWDbDO7aqFFmhgnvFESzaILHbUYmbhNjTpcjyO/XYdouQ9Pw8W9rpV8oJT1TsK5FbwSHU1oazmnb7BWA==',
  104. 'js/jquery-3.7.1.js' => 'sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==',
  105. 'js/kjua-0.9.0.js' => 'sha512-CVn7af+vTMBd9RjoS4QM5fpLFEOtBCoB0zPtaqIDC7sF4F8qgUSRFQQpIyEDGsr6yrjbuOLzdf20tkHHmpaqwQ==',
  106. 'js/legacy.js' => 'sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==',
  107. 'js/prettify.js' => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==',
  108. 'js/privatebin.js' => 'sha512-cbmXvtZ/5gZPFjQDzP3IEhUAIhFPAoM31gw2kRYCT5xOh8wv9gXeDqI/t798luRW1xdC4gaYodjEFCzrsZR4mA==',
  109. 'js/purify-3.1.6.js' => 'sha512-SDwfsqHZFhbcBo/6AN7Te4KoUkWHHw7TzyZCuRkkpl0o8oV4owT/lprznWWe9+qFtigb6MrF81K4CQRLN6be+A==',
  110. 'js/rawinflate-0.3.js' => 'sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==',
  111. 'js/showdown-2.1.0.js' => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',
  112. 'js/zlib-1.3.1.js' => 'sha512-Z90oppVx/mn0DG2k9airjFVQuliELlXLeT3SRiO6MLiUSbhGlAq+UFwmYbG4i9mwW87dkG8fgJPapGwnUq7Osg==',
  113. ),
  114. );
  115. /**
  116. * parse configuration file and ensure default configuration values are present
  117. *
  118. * @throws Exception
  119. */
  120. public function __construct()
  121. {
  122. $basePaths = array();
  123. $config = array();
  124. $configPath = getenv('CONFIG_PATH');
  125. if ($configPath !== false && !empty($configPath)) {
  126. $basePaths[] = $configPath;
  127. }
  128. $basePaths[] = PATH . 'cfg';
  129. foreach ($basePaths as $basePath) {
  130. $configFile = $basePath . DIRECTORY_SEPARATOR . 'conf.php';
  131. if (is_readable($configFile)) {
  132. $config = parse_ini_file($configFile, true);
  133. foreach (array('main', 'model', 'model_options') as $section) {
  134. if (!array_key_exists($section, $config)) {
  135. throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
  136. }
  137. }
  138. break;
  139. }
  140. }
  141. $opts = '_options';
  142. foreach (self::getDefaults() as $section => $values) {
  143. // fill missing sections with default values
  144. if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
  145. $this->_configuration[$section] = $values;
  146. if (array_key_exists('dir', $this->_configuration[$section])) {
  147. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  148. }
  149. continue;
  150. }
  151. // provide different defaults for database model
  152. elseif (
  153. $section == 'model_options' && in_array(
  154. $this->_configuration['model']['class'],
  155. array('Database', 'privatebin_db', 'zerobin_db')
  156. )
  157. ) {
  158. $values = array(
  159. 'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
  160. 'tbl' => null,
  161. 'usr' => null,
  162. 'pwd' => null,
  163. 'opt' => array(PDO::ATTR_PERSISTENT => true),
  164. );
  165. } elseif (
  166. $section == 'model_options' && in_array(
  167. $this->_configuration['model']['class'],
  168. array('GoogleCloudStorage')
  169. )
  170. ) {
  171. $values = array(
  172. 'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null,
  173. 'prefix' => 'pastes',
  174. 'uniformacl' => false,
  175. );
  176. } elseif (
  177. $section == 'model_options' && in_array(
  178. $this->_configuration['model']['class'],
  179. array('S3Storage')
  180. )
  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. }
  232. }
  233. $this->_configuration[$section][$key] = $result;
  234. }
  235. }
  236. }
  237. // support for old config file format, before the fork was renamed and PSR-4 introduced
  238. $this->_configuration['model']['class'] = str_replace(
  239. 'zerobin_', 'privatebin_',
  240. $this->_configuration['model']['class']
  241. );
  242. $this->_configuration['model']['class'] = str_replace(
  243. array('privatebin_data', 'privatebin_db'),
  244. array('Filesystem', 'Database'),
  245. $this->_configuration['model']['class']
  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 Exception
  298. * @return mixed
  299. */
  300. public function getSection($section)
  301. {
  302. if (!array_key_exists($section, $this->_configuration)) {
  303. throw new Exception(I18n::_('%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. }