Configuration.php 9.5 KB

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