Configuration.php 9.6 KB

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