Configuration.php 11 KB

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