Configuration.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. $iniUpgradeError = false;
  105. $context = stream_context_create();
  106. $iniHandle = fopen($configIni, 'r', 1, $context);
  107. $written = file_put_contents($configFile, ';<?php /*' . PHP_EOL);
  108. $written = file_put_contents($configFile, $iniHandle, FILE_APPEND);
  109. fclose($iniHandle);
  110. unlink($configIni);
  111. }
  112. // cleanup sample, too
  113. $configSample = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.sample.php';
  114. $configIniSample = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample';
  115. if (is_readable($configIniSample)) {
  116. if (is_readable($configSample)) {
  117. unlink($configIniSample);
  118. } else {
  119. rename($configIniSample, $configSample);
  120. }
  121. }
  122. }
  123. if (is_readable($configFile)) {
  124. $config = parse_ini_file($configFile, true);
  125. foreach (array('main', 'model', 'model_options') as $section) {
  126. if (!array_key_exists($section, $config)) {
  127. throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
  128. }
  129. }
  130. }
  131. $opts = '_options';
  132. foreach (self::getDefaults() as $section => $values) {
  133. // fill missing sections with default values
  134. if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
  135. $this->_configuration[$section] = $values;
  136. if (array_key_exists('dir', $this->_configuration[$section])) {
  137. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  138. }
  139. continue;
  140. }
  141. // provide different defaults for database model
  142. elseif (
  143. $section == 'model_options' && in_array(
  144. $this->_configuration['model']['class'],
  145. array('Database', 'privatebin_db', 'zerobin_db')
  146. )
  147. ) {
  148. $values = array(
  149. 'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
  150. 'tbl' => null,
  151. 'usr' => null,
  152. 'pwd' => null,
  153. 'opt' => array(PDO::ATTR_PERSISTENT => true),
  154. );
  155. }
  156. // "*_options" sections don't require all defaults to be set
  157. if (
  158. $section !== 'model_options' &&
  159. ($from = strlen($section) - strlen($opts)) >= 0 &&
  160. strpos($section, $opts, $from) !== false
  161. ) {
  162. if (is_int(current($values))) {
  163. $config[$section] = array_map('intval', $config[$section]);
  164. }
  165. $this->_configuration[$section] = $config[$section];
  166. }
  167. // check for missing keys and set defaults if necessary
  168. else {
  169. foreach ($values as $key => $val) {
  170. if ($key == 'dir') {
  171. $val = PATH . $val;
  172. }
  173. $result = $val;
  174. if (array_key_exists($key, $config[$section])) {
  175. if ($val === null) {
  176. $result = $config[$section][$key];
  177. } elseif (is_bool($val)) {
  178. $val = strtolower($config[$section][$key]);
  179. if (in_array($val, array('true', 'yes', 'on'))) {
  180. $result = true;
  181. } elseif (in_array($val, array('false', 'no', 'off'))) {
  182. $result = false;
  183. } else {
  184. $result = (bool) $config[$section][$key];
  185. }
  186. } elseif (is_int($val)) {
  187. $result = (int) $config[$section][$key];
  188. } elseif (is_string($val) && !empty($config[$section][$key])) {
  189. $result = (string) $config[$section][$key];
  190. }
  191. }
  192. $this->_configuration[$section][$key] = $result;
  193. }
  194. }
  195. }
  196. // support for old config file format, before the fork was renamed and PSR-4 introduced
  197. $this->_configuration['model']['class'] = str_replace(
  198. 'zerobin_', 'privatebin_',
  199. $this->_configuration['model']['class']
  200. );
  201. $this->_configuration['model']['class'] = str_replace(
  202. array('privatebin_data', 'privatebin_db'),
  203. array('Filesystem', 'Database'),
  204. $this->_configuration['model']['class']
  205. );
  206. // ensure a valid expire default key is set
  207. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
  208. $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
  209. }
  210. }
  211. /**
  212. * get configuration as array
  213. *
  214. * return array
  215. */
  216. public function get()
  217. {
  218. return $this->_configuration;
  219. }
  220. /**
  221. * get default configuration as array
  222. *
  223. * return array
  224. */
  225. public static function getDefaults()
  226. {
  227. return self::$_defaults;
  228. }
  229. /**
  230. * get a key from the configuration, typically the main section or all keys
  231. *
  232. * @param string $key
  233. * @param string $section defaults to main
  234. * @throws Exception
  235. * return mixed
  236. */
  237. public function getKey($key, $section = 'main')
  238. {
  239. $options = $this->getSection($section);
  240. if (!array_key_exists($key, $options)) {
  241. throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
  242. }
  243. return $this->_configuration[$section][$key];
  244. }
  245. /**
  246. * get a section from the configuration, must exist
  247. *
  248. * @param string $section
  249. * @throws Exception
  250. * return mixed
  251. */
  252. public function getSection($section)
  253. {
  254. if (!array_key_exists($section, $this->_configuration)) {
  255. throw new Exception(I18n::_('%s requires configuration section [%s] to be present in configuration file.', I18n::_($this->getKey('name')), $section), 3);
  256. }
  257. return $this->_configuration[$section];
  258. }
  259. }