Configuration.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.7.2
  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. 'discussiondatedisplay' => true,
  40. 'password' => true,
  41. 'fileupload' => false,
  42. 'burnafterreadingselected' => false,
  43. 'defaultformatter' => 'plaintext',
  44. 'syntaxhighlightingtheme' => '',
  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. 'email' => true,
  54. 'icon' => 'identicon',
  55. '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',
  56. 'zerobincompatibility' => false,
  57. 'httpwarning' => true,
  58. 'compression' => 'zlib',
  59. ),
  60. 'expire' => array(
  61. 'default' => '1week',
  62. ),
  63. 'expire_options' => array(
  64. '5min' => 300,
  65. '10min' => 600,
  66. '1hour' => 3600,
  67. '1day' => 86400,
  68. '1week' => 604800,
  69. '1month' => 2592000,
  70. '1year' => 31536000,
  71. 'never' => 0,
  72. ),
  73. 'formatter_options' => array(
  74. 'plaintext' => 'Plain Text',
  75. 'syntaxhighlighting' => 'Source Code',
  76. 'markdown' => 'Markdown',
  77. ),
  78. 'traffic' => array(
  79. 'limit' => 10,
  80. 'header' => '',
  81. 'exempted' => '',
  82. 'creators' => '',
  83. ),
  84. 'purge' => array(
  85. 'limit' => 300,
  86. 'batchsize' => 10,
  87. ),
  88. 'model' => array(
  89. 'class' => 'Filesystem',
  90. ),
  91. 'model_options' => array(
  92. 'dir' => 'data',
  93. ),
  94. 'yourls' => array(
  95. 'signature' => '',
  96. 'apiurl' => '',
  97. ),
  98. );
  99. /**
  100. * parse configuration file and ensure default configuration values are present
  101. *
  102. * @throws Exception
  103. */
  104. public function __construct()
  105. {
  106. $basePaths = array();
  107. $config = array();
  108. $configPath = getenv('CONFIG_PATH');
  109. if ($configPath !== false && !empty($configPath)) {
  110. $basePaths[] = $configPath;
  111. }
  112. $basePaths[] = PATH . 'cfg';
  113. foreach ($basePaths as $basePath) {
  114. $configFile = $basePath . DIRECTORY_SEPARATOR . 'conf.php';
  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. break;
  123. }
  124. }
  125. $opts = '_options';
  126. foreach (self::getDefaults() as $section => $values) {
  127. // fill missing sections with default values
  128. if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
  129. $this->_configuration[$section] = $values;
  130. if (array_key_exists('dir', $this->_configuration[$section])) {
  131. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  132. }
  133. continue;
  134. }
  135. // provide different defaults for database model
  136. elseif (
  137. $section == 'model_options' && in_array(
  138. $this->_configuration['model']['class'],
  139. array('Database', 'privatebin_db', 'zerobin_db')
  140. )
  141. ) {
  142. $values = array(
  143. 'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
  144. 'tbl' => null,
  145. 'usr' => null,
  146. 'pwd' => null,
  147. 'opt' => array(PDO::ATTR_PERSISTENT => true),
  148. );
  149. } elseif (
  150. $section == 'model_options' && in_array(
  151. $this->_configuration['model']['class'],
  152. array('GoogleCloudStorage')
  153. )
  154. ) {
  155. $values = array(
  156. 'bucket' => getenv('PRIVATEBIN_GCS_BUCKET') ? getenv('PRIVATEBIN_GCS_BUCKET') : null,
  157. 'prefix' => 'pastes',
  158. 'uniformacl' => false,
  159. );
  160. } elseif (
  161. $section == 'model_options' && in_array(
  162. $this->_configuration['model']['class'],
  163. array('S3Storage')
  164. )
  165. ) {
  166. $values = array(
  167. 'region' => null,
  168. 'version' => null,
  169. 'endpoint' => null,
  170. 'accesskey' => null,
  171. 'secretkey' => null,
  172. 'use_path_style_endpoint' => null,
  173. 'bucket' => null,
  174. 'prefix' => '',
  175. );
  176. }
  177. // "*_options" sections don't require all defaults to be set
  178. if (
  179. $section !== 'model_options' &&
  180. ($from = strlen($section) - strlen($opts)) >= 0 &&
  181. strpos($section, $opts, $from) !== false
  182. ) {
  183. if (is_int(current($values))) {
  184. $config[$section] = array_map('intval', $config[$section]);
  185. }
  186. $this->_configuration[$section] = $config[$section];
  187. }
  188. // check for missing keys and set defaults if necessary
  189. else {
  190. foreach ($values as $key => $val) {
  191. if ($key == 'dir') {
  192. $val = PATH . $val;
  193. }
  194. $result = $val;
  195. if (array_key_exists($key, $config[$section])) {
  196. if ($val === null) {
  197. $result = $config[$section][$key];
  198. } elseif (is_bool($val)) {
  199. $val = strtolower($config[$section][$key]);
  200. if (in_array($val, array('true', 'yes', 'on'))) {
  201. $result = true;
  202. } elseif (in_array($val, array('false', 'no', 'off'))) {
  203. $result = false;
  204. } else {
  205. $result = (bool) $config[$section][$key];
  206. }
  207. } elseif (is_int($val)) {
  208. $result = (int) $config[$section][$key];
  209. } elseif (is_string($val) && !empty($config[$section][$key])) {
  210. $result = (string) $config[$section][$key];
  211. }
  212. }
  213. $this->_configuration[$section][$key] = $result;
  214. }
  215. }
  216. }
  217. // support for old config file format, before the fork was renamed and PSR-4 introduced
  218. $this->_configuration['model']['class'] = str_replace(
  219. 'zerobin_', 'privatebin_',
  220. $this->_configuration['model']['class']
  221. );
  222. $this->_configuration['model']['class'] = str_replace(
  223. array('privatebin_data', 'privatebin_db'),
  224. array('Filesystem', 'Database'),
  225. $this->_configuration['model']['class']
  226. );
  227. // ensure a valid expire default key is set
  228. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
  229. $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
  230. }
  231. // ensure the basepath ends in a slash, if one is set
  232. if (
  233. !empty($this->_configuration['main']['basepath']) &&
  234. substr_compare($this->_configuration['main']['basepath'], '/', -1) !== 0
  235. ) {
  236. $this->_configuration['main']['basepath'] .= '/';
  237. }
  238. }
  239. /**
  240. * get configuration as array
  241. *
  242. * @return array
  243. */
  244. public function get()
  245. {
  246. return $this->_configuration;
  247. }
  248. /**
  249. * get default configuration as array
  250. *
  251. * @return array
  252. */
  253. public static function getDefaults()
  254. {
  255. return self::$_defaults;
  256. }
  257. /**
  258. * get a key from the configuration, typically the main section or all keys
  259. *
  260. * @param string $key
  261. * @param string $section defaults to main
  262. * @throws Exception
  263. * @return mixed
  264. */
  265. public function getKey($key, $section = 'main')
  266. {
  267. $options = $this->getSection($section);
  268. if (!array_key_exists($key, $options)) {
  269. throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
  270. }
  271. return $this->_configuration[$section][$key];
  272. }
  273. /**
  274. * get a section from the configuration, must exist
  275. *
  276. * @param string $section
  277. * @throws Exception
  278. * @return mixed
  279. */
  280. public function getSection($section)
  281. {
  282. if (!array_key_exists($section, $this->_configuration)) {
  283. throw new Exception(I18n::_('%s requires configuration section [%s] to be present in configuration file.', I18n::_($this->getKey('name')), $section), 3);
  284. }
  285. return $this->_configuration[$section];
  286. }
  287. }