Configuration.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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-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. } elseif (
  148. $section == 'model_options' && in_array(
  149. $this->_configuration['model']['class'],
  150. array('GoogleCloudStorage')
  151. )
  152. ) {
  153. $values = array(
  154. 'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null,
  155. 'prefix' => 'pastes',
  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. }