Configuration.php 9.7 KB

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