configuration.php 8.1 KB

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