Configuration.php 8.4 KB

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