Configuration.php 9.3 KB

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