1
0

Configuration.php 9.1 KB

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