Configuration.php 9.8 KB

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