Configuration.php 9.8 KB

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