Configuration.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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.2
  11. */
  12. namespace PrivateBin;
  13. use Exception;
  14. use PDO;
  15. use PrivateBin\Persistence\DataStore;
  16. /**
  17. * Configuration
  18. *
  19. * parses configuration file, ensures default values present
  20. */
  21. class Configuration
  22. {
  23. /**
  24. * parsed configuration
  25. *
  26. * @var array
  27. */
  28. private $_configuration;
  29. /**
  30. * default configuration
  31. *
  32. * @var array
  33. */
  34. private static $_defaults = array(
  35. 'main' => array(
  36. 'name' => 'PrivateBin',
  37. 'discussion' => true,
  38. 'opendiscussion' => false,
  39. 'password' => true,
  40. 'fileupload' => false,
  41. 'burnafterreadingselected' => false,
  42. 'defaultformatter' => 'plaintext',
  43. 'syntaxhighlightingtheme' => null,
  44. 'sizelimit' => 2097152,
  45. 'template' => 'bootstrap',
  46. 'notice' => '',
  47. 'languageselection' => false,
  48. 'languagedefault' => '',
  49. 'urlshortener' => '',
  50. 'qrcode' => true,
  51. 'icon' => 'identicon',
  52. 'cspheader' => 'default-src \'none\'; manifest-src \'self\'; connect-src *; script-src \'self\'; style-src \'self\'; font-src \'self\'; img-src \'self\' data:; media-src data:; object-src data:; Referrer-Policy: \'no-referrer\'; sandbox allow-same-origin allow-scripts allow-forms allow-popups allow-modals',
  53. 'zerobincompatibility' => false,
  54. ),
  55. 'expire' => array(
  56. 'default' => '1week',
  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. DataStore::prependRename($configIni, $configFile, ';');
  103. // cleanup sample, too
  104. $configIniSample = $configIni . '.sample';
  105. if (is_readable($configIniSample)) {
  106. DataStore::prependRename($configIniSample, PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.sample.php', ';');
  107. }
  108. }
  109. if (is_readable($configFile)) {
  110. $config = parse_ini_file($configFile, true);
  111. foreach (array('main', 'model', 'model_options') as $section) {
  112. if (!array_key_exists($section, $config)) {
  113. throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
  114. }
  115. }
  116. }
  117. $opts = '_options';
  118. foreach (self::getDefaults() as $section => $values) {
  119. // fill missing sections with default values
  120. if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
  121. $this->_configuration[$section] = $values;
  122. if (array_key_exists('dir', $this->_configuration[$section])) {
  123. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  124. }
  125. continue;
  126. }
  127. // provide different defaults for database model
  128. elseif (
  129. $section == 'model_options' && in_array(
  130. $this->_configuration['model']['class'],
  131. array('Database', 'privatebin_db', 'zerobin_db')
  132. )
  133. ) {
  134. $values = array(
  135. 'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
  136. 'tbl' => null,
  137. 'usr' => null,
  138. 'pwd' => null,
  139. 'opt' => array(PDO::ATTR_PERSISTENT => true),
  140. );
  141. }
  142. // "*_options" sections don't require all defaults to be set
  143. if (
  144. $section !== 'model_options' &&
  145. ($from = strlen($section) - strlen($opts)) >= 0 &&
  146. strpos($section, $opts, $from) !== false
  147. ) {
  148. if (is_int(current($values))) {
  149. $config[$section] = array_map('intval', $config[$section]);
  150. }
  151. $this->_configuration[$section] = $config[$section];
  152. }
  153. // check for missing keys and set defaults if necessary
  154. else {
  155. foreach ($values as $key => $val) {
  156. if ($key == 'dir') {
  157. $val = PATH . $val;
  158. }
  159. $result = $val;
  160. if (array_key_exists($key, $config[$section])) {
  161. if ($val === null) {
  162. $result = $config[$section][$key];
  163. } elseif (is_bool($val)) {
  164. $val = strtolower($config[$section][$key]);
  165. if (in_array($val, array('true', 'yes', 'on'))) {
  166. $result = true;
  167. } elseif (in_array($val, array('false', 'no', 'off'))) {
  168. $result = false;
  169. } else {
  170. $result = (bool) $config[$section][$key];
  171. }
  172. } elseif (is_int($val)) {
  173. $result = (int) $config[$section][$key];
  174. } elseif (is_string($val) && !empty($config[$section][$key])) {
  175. $result = (string) $config[$section][$key];
  176. }
  177. }
  178. $this->_configuration[$section][$key] = $result;
  179. }
  180. }
  181. }
  182. // support for old config file format, before the fork was renamed and PSR-4 introduced
  183. $this->_configuration['model']['class'] = str_replace(
  184. 'zerobin_', 'privatebin_',
  185. $this->_configuration['model']['class']
  186. );
  187. $this->_configuration['model']['class'] = str_replace(
  188. array('privatebin_data', 'privatebin_db'),
  189. array('Filesystem', 'Database'),
  190. $this->_configuration['model']['class']
  191. );
  192. // ensure a valid expire default key is set
  193. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
  194. $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
  195. }
  196. }
  197. /**
  198. * get configuration as array
  199. *
  200. * @return array
  201. */
  202. public function get()
  203. {
  204. return $this->_configuration;
  205. }
  206. /**
  207. * get default configuration as array
  208. *
  209. * @return array
  210. */
  211. public static function getDefaults()
  212. {
  213. return self::$_defaults;
  214. }
  215. /**
  216. * get a key from the configuration, typically the main section or all keys
  217. *
  218. * @param string $key
  219. * @param string $section defaults to main
  220. * @throws Exception
  221. * @return mixed
  222. */
  223. public function getKey($key, $section = 'main')
  224. {
  225. $options = $this->getSection($section);
  226. if (!array_key_exists($key, $options)) {
  227. throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
  228. }
  229. return $this->_configuration[$section][$key];
  230. }
  231. /**
  232. * get a section from the configuration, must exist
  233. *
  234. * @param string $section
  235. * @throws Exception
  236. * @return mixed
  237. */
  238. public function getSection($section)
  239. {
  240. if (!array_key_exists($section, $this->_configuration)) {
  241. throw new Exception(I18n::_('%s requires configuration section [%s] to be present in configuration file.', I18n::_($this->getKey('name')), $section), 3);
  242. }
  243. return $this->_configuration[$section];
  244. }
  245. }