Configuration.php 9.3 KB

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