1
0

Configuration.php 13 KB

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