configuration.php 8.1 KB

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