Configuration.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.1
  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. 'discussion' => true,
  37. 'opendiscussion' => false,
  38. 'password' => true,
  39. 'fileupload' => false,
  40. 'burnafterreadingselected' => false,
  41. 'instantburnafterreading' => false,
  42. 'defaultformatter' => 'plaintext',
  43. 'syntaxhighlightingtheme' => null,
  44. 'sizelimit' => 2097152,
  45. 'template' => 'bootstrap',
  46. 'notice' => '',
  47. 'languageselection' => false,
  48. 'languagedefault' => '',
  49. 'urlshortener' => '',
  50. 'icon' => 'identicon',
  51. 'cspheader' => 'default-src \'none\'; manifest-src \'self\'; connect-src *; script-src \'self\'; style-src \'self\'; font-src \'self\'; img-src \'self\' data:; referrer no-referrer; sandbox allow-same-origin allow-scripts allow-forms allow-popups',
  52. 'zerobincompatibility' => false,
  53. ),
  54. 'expire' => array(
  55. 'default' => '1week',
  56. 'clone' => true,
  57. ),
  58. 'expire_options' => array(
  59. '5min' => 300,
  60. '10min' => 600,
  61. '1hour' => 3600,
  62. '1day' => 86400,
  63. '1week' => 604800,
  64. '1month' => 2592000,
  65. '1year' => 31536000,
  66. 'never' => 0,
  67. ),
  68. 'formatter_options' => array(
  69. 'plaintext' => 'Plain Text',
  70. 'syntaxhighlighting' => 'Source Code',
  71. 'markdown' => 'Markdown',
  72. ),
  73. 'traffic' => array(
  74. 'limit' => 10,
  75. 'header' => null,
  76. 'dir' => 'data',
  77. ),
  78. 'purge' => array(
  79. 'limit' => 300,
  80. 'batchsize' => 10,
  81. 'dir' => 'data',
  82. ),
  83. 'model' => array(
  84. 'class' => 'Filesystem',
  85. ),
  86. 'model_options' => array(
  87. 'dir' => 'data',
  88. ),
  89. );
  90. /**
  91. * parse configuration file and ensure default configuration values are present
  92. *
  93. * @throws Exception
  94. */
  95. public function __construct()
  96. {
  97. $config = array();
  98. $configFile = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
  99. if (is_readable($configFile)) {
  100. $config = parse_ini_file($configFile, true);
  101. foreach (array('main', 'model', 'model_options') as $section) {
  102. if (!array_key_exists($section, $config)) {
  103. throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
  104. }
  105. }
  106. }
  107. $opts = '_options';
  108. foreach (self::getDefaults() as $section => $values) {
  109. // fill missing sections with default values
  110. if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
  111. $this->_configuration[$section] = $values;
  112. if (array_key_exists('dir', $this->_configuration[$section])) {
  113. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  114. }
  115. continue;
  116. }
  117. // provide different defaults for database model
  118. elseif (
  119. $section == 'model_options' && in_array(
  120. $this->_configuration['model']['class'],
  121. array('Database', 'privatebin_db', 'zerobin_db')
  122. )
  123. ) {
  124. $values = array(
  125. 'dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3',
  126. 'tbl' => null,
  127. 'usr' => null,
  128. 'pwd' => null,
  129. 'opt' => array(PDO::ATTR_PERSISTENT => true),
  130. );
  131. }
  132. // "*_options" sections don't require all defaults to be set
  133. if (
  134. $section !== 'model_options' &&
  135. ($from = strlen($section) - strlen($opts)) >= 0 &&
  136. strpos($section, $opts, $from) !== false
  137. ) {
  138. if (is_int(current($values))) {
  139. $config[$section] = array_map('intval', $config[$section]);
  140. }
  141. $this->_configuration[$section] = $config[$section];
  142. }
  143. // check for missing keys and set defaults if necessary
  144. else {
  145. foreach ($values as $key => $val) {
  146. if ($key == 'dir') {
  147. $val = PATH . $val;
  148. }
  149. $result = $val;
  150. if (array_key_exists($key, $config[$section])) {
  151. if ($val === null) {
  152. $result = $config[$section][$key];
  153. } elseif (is_bool($val)) {
  154. $val = strtolower($config[$section][$key]);
  155. if (in_array($val, array('true', 'yes', 'on'))) {
  156. $result = true;
  157. } elseif (in_array($val, array('false', 'no', 'off'))) {
  158. $result = false;
  159. } else {
  160. $result = (bool) $config[$section][$key];
  161. }
  162. } elseif (is_int($val)) {
  163. $result = (int) $config[$section][$key];
  164. } elseif (is_string($val) && !empty($config[$section][$key])) {
  165. $result = (string) $config[$section][$key];
  166. }
  167. }
  168. $this->_configuration[$section][$key] = $result;
  169. }
  170. }
  171. }
  172. // support for old config file format, before the fork was renamed and PSR-4 introduced
  173. $this->_configuration['model']['class'] = str_replace(
  174. 'zerobin_', 'privatebin_',
  175. $this->_configuration['model']['class']
  176. );
  177. $this->_configuration['model']['class'] = str_replace(
  178. array('privatebin_data', 'privatebin_db'),
  179. array('Filesystem', 'Database'),
  180. $this->_configuration['model']['class']
  181. );
  182. // ensure a valid expire default key is set
  183. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
  184. $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
  185. }
  186. }
  187. /**
  188. * get configuration as array
  189. *
  190. * return array
  191. */
  192. public function get()
  193. {
  194. return $this->_configuration;
  195. }
  196. /**
  197. * get default configuration as array
  198. *
  199. * return array
  200. */
  201. public static function getDefaults()
  202. {
  203. return self::$_defaults;
  204. }
  205. /**
  206. * get a key from the configuration, typically the main section or all keys
  207. *
  208. * @param string $key
  209. * @param string $section defaults to main
  210. * @throws Exception
  211. * return mixed
  212. */
  213. public function getKey($key, $section = 'main')
  214. {
  215. $options = $this->getSection($section);
  216. if (!array_key_exists($key, $options)) {
  217. throw new Exception(I18n::_('Invalid data.') . " $section / $key", 4);
  218. }
  219. return $this->_configuration[$section][$key];
  220. }
  221. /**
  222. * get a section from the configuration, must exist
  223. *
  224. * @param string $section
  225. * @throws Exception
  226. * return mixed
  227. */
  228. public function getSection($section)
  229. {
  230. if (!array_key_exists($section, $this->_configuration)) {
  231. throw new Exception(I18n::_('%s requires configuration section [%s] to be present in configuration file.', I18n::_($this->getKey('name')), $section), 3);
  232. }
  233. return $this->_configuration[$section];
  234. }
  235. }