configuration.php 7.9 KB

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