Configuration.php 9.5 KB

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