configuration.php 8.1 KB

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