Configuration.php 9.6 KB

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