Configuration.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  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. * @version 1.1.1
  11. */
  12. namespace PrivateBin;
  13. use Exception;
  14. use PDO;
  15. /**
  16. * Configuration
  17. *
  18. * parses configuration file, ensures default values present
  19. */
  20. class Configuration
  21. {
  22. /**
  23. * parsed configuration
  24. *
  25. * @var array
  26. */
  27. private $_configuration;
  28. /**
  29. * default configuration
  30. *
  31. * @var array
  32. */
  33. private static $_defaults = array(
  34. 'main' => array(
  35. 'discussion' => true,
  36. 'opendiscussion' => false,
  37. 'password' => true,
  38. 'fileupload' => false,
  39. 'burnafterreadingselected' => false,
  40. 'defaultformatter' => 'plaintext',
  41. 'syntaxhighlightingtheme' => null,
  42. 'sizelimit' => 2097152,
  43. 'template' => 'bootstrap',
  44. 'notice' => '',
  45. 'languageselection' => false,
  46. 'languagedefault' => '',
  47. 'urlshortener' => '',
  48. 'icon' => 'identicon',
  49. 'cspheader' => 'default-src \'none\'; manifest-src \'self\'; connect-src *; script-src \'self\'; style-src \'self\'; font-src \'self\'; img-src \'self\' data:; referrer no-referrer;',
  50. 'zerobincompatibility' => false,
  51. ),
  52. 'expire' => array(
  53. 'default' => '1week',
  54. 'clone' => true,
  55. ),
  56. 'expire_options' => array(
  57. '5min' => 300,
  58. '10min' => 600,
  59. '1hour' => 3600,
  60. '1day' => 86400,
  61. '1week' => 604800,
  62. '1month' => 2592000,
  63. '1year' => 31536000,
  64. 'never' => 0,
  65. ),
  66. 'formatter_options' => array(
  67. 'plaintext' => 'Plain Text',
  68. 'syntaxhighlighting' => 'Source Code',
  69. 'markdown' => 'Markdown',
  70. ),
  71. 'traffic' => array(
  72. 'limit' => 10,
  73. 'header' => null,
  74. 'dir' => 'data',
  75. ),
  76. 'purge' => array(
  77. 'limit' => 300,
  78. 'batchsize' => 10,
  79. 'dir' => 'data',
  80. ),
  81. 'model' => array(
  82. 'class' => 'Filesystem',
  83. ),
  84. 'model_options' => array(
  85. 'dir' => 'data',
  86. ),
  87. );
  88. /**
  89. * parse configuration file and ensure default configuration values are present
  90. *
  91. * @throws Exception
  92. */
  93. public function __construct()
  94. {
  95. $config = array();
  96. $configFile = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.php';
  97. $configIni = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
  98. // rename INI files to avoid configuration leakage
  99. if (is_readable($configIni)) {
  100. // don't overwrite already converted file
  101. if (!is_file($configFile)) {
  102. $iniHandle = fopen($configIni, 'r', false, stream_context_create());
  103. $written = file_put_contents($configFile, ';<?php http_response_code(403); /*' . PHP_EOL);
  104. $written = file_put_contents($configFile, $iniHandle, FILE_APPEND);
  105. fclose($iniHandle);
  106. unlink($configIni);
  107. }
  108. // cleanup sample, too
  109. $configSample = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.sample.php';
  110. $configIniSample = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample';
  111. if (is_readable($configIniSample)) {
  112. if (is_readable($configSample)) {
  113. unlink($configIniSample);
  114. } else {
  115. rename($configIniSample, $configSample);
  116. }
  117. }
  118. }
  119. if (is_readable($configFile)) {
  120. $config = parse_ini_file($configFile, true);
  121. foreach (array('main', 'model', 'model_options') as $section) {
  122. if (!array_key_exists($section, $config)) {
  123. throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
  124. }
  125. }
  126. }
  127. $opts = '_options';
  128. foreach (self::getDefaults() as $section => $values) {
  129. // fill missing sections with default values
  130. if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
  131. $this->_configuration[$section] = $values;
  132. if (array_key_exists('dir', $this->_configuration[$section])) {
  133. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  134. }
  135. continue;
  136. }
  137. // provide different defaults for database model
  138. elseif (
  139. $section == 'model_options' && in_array(
  140. $this->_configuration['model']['class'],
  141. array('Database', 'privatebin_db', 'zerobin_db')
  142. )
  143. ) {
  144. $values = array(
  145. 'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
  146. 'tbl' => null,
  147. 'usr' => null,
  148. 'pwd' => null,
  149. 'opt' => array(PDO::ATTR_PERSISTENT => true),
  150. );
  151. }
  152. // "*_options" sections don't require all defaults to be set
  153. if (
  154. $section !== 'model_options' &&
  155. ($from = strlen($section) - strlen($opts)) >= 0 &&
  156. strpos($section, $opts, $from) !== false
  157. ) {
  158. if (is_int(current($values))) {
  159. $config[$section] = array_map('intval', $config[$section]);
  160. }
  161. $this->_configuration[$section] = $config[$section];
  162. }
  163. // check for missing keys and set defaults if necessary
  164. else {
  165. foreach ($values as $key => $val) {
  166. if ($key == 'dir') {
  167. $val = PATH . $val;
  168. }
  169. $result = $val;
  170. if (array_key_exists($key, $config[$section])) {
  171. if ($val === null) {
  172. $result = $config[$section][$key];
  173. } elseif (is_bool($val)) {
  174. $val = strtolower($config[$section][$key]);
  175. if (in_array($val, array('true', 'yes', 'on'))) {
  176. $result = true;
  177. } elseif (in_array($val, array('false', 'no', 'off'))) {
  178. $result = false;
  179. } else {
  180. $result = (bool) $config[$section][$key];
  181. }
  182. } elseif (is_int($val)) {
  183. $result = (int) $config[$section][$key];
  184. } elseif (is_string($val) && !empty($config[$section][$key])) {
  185. $result = (string) $config[$section][$key];
  186. }
  187. }
  188. $this->_configuration[$section][$key] = $result;
  189. }
  190. }
  191. }
  192. // support for old config file format, before the fork was renamed and PSR-4 introduced
  193. $this->_configuration['model']['class'] = str_replace(
  194. 'zerobin_', 'privatebin_',
  195. $this->_configuration['model']['class']
  196. );
  197. $this->_configuration['model']['class'] = str_replace(
  198. array('privatebin_data', 'privatebin_db'),
  199. array('Filesystem', 'Database'),
  200. $this->_configuration['model']['class']
  201. );
  202. // ensure a valid expire default key is set
  203. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
  204. $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
  205. }
  206. }
  207. /**
  208. * get configuration as array
  209. *
  210. * return array
  211. */
  212. public function get()
  213. {
  214. return $this->_configuration;
  215. }
  216. /**
  217. * get default configuration as array
  218. *
  219. * return array
  220. */
  221. public static function getDefaults()
  222. {
  223. return self::$_defaults;
  224. }
  225. /**
  226. * get a key from the configuration, typically the main section or all keys
  227. *
  228. * @param string $key
  229. * @param string $section defaults to main
  230. * @throws Exception
  231. * return mixed
  232. */
  233. public function getKey($key, $section = 'main')
  234. {
  235. $options = $this->getSection($section);
  236. if (!array_key_exists($key, $options)) {
  237. throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
  238. }
  239. return $this->_configuration[$section][$key];
  240. }
  241. /**
  242. * get a section from the configuration, must exist
  243. *
  244. * @param string $section
  245. * @throws Exception
  246. * return mixed
  247. */
  248. public function getSection($section)
  249. {
  250. if (!array_key_exists($section, $this->_configuration)) {
  251. throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 3);
  252. }
  253. return $this->_configuration[$section];
  254. }
  255. }