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