1
0

Configuration.php 9.2 KB

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