1
0

Configuration.php 10.0 KB

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