Configuration.php 8.2 KB

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