configuration.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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') 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 ($section == 'model_options' && $this->_configuration['model']['class'] == 'privatebin_db')
  111. {
  112. $values = array(
  113. 'dsn' => 'sqlite:' . PATH . 'data/db.sq3',
  114. 'tbl' => null,
  115. 'usr' => null,
  116. 'pwd' => null,
  117. 'opt' => array(PDO::ATTR_PERSISTENT => true),
  118. );
  119. }
  120. // "*_options" sections don't require all defaults to be set
  121. if (
  122. $section !== 'model_options' &&
  123. ($from = strlen($section) - strlen($opts)) >= 0 &&
  124. strpos($section, $opts, $from) !== false
  125. )
  126. {
  127. if (is_int(current($values)))
  128. {
  129. $config[$section] = array_map('intval', $config[$section]);
  130. }
  131. $this->_configuration[$section] = $config[$section];
  132. }
  133. // check for missing keys and set defaults if necessary
  134. else
  135. {
  136. foreach ($values as $key => $val)
  137. {
  138. if ($key == 'dir')
  139. {
  140. $val = PATH . $val;
  141. }
  142. $result = $val;
  143. if (array_key_exists($key, $config[$section]))
  144. {
  145. if ($val === null)
  146. {
  147. $result = $config[$section][$key];
  148. }
  149. elseif (is_bool($val))
  150. {
  151. $val = strtolower($config[$section][$key]);
  152. if (in_array($val, array('true', 'yes', 'on')))
  153. {
  154. $result = true;
  155. }
  156. elseif (in_array($val, array('false', 'no', 'off')))
  157. {
  158. $result = false;
  159. }
  160. else
  161. {
  162. $result = (bool) $config[$section][$key];
  163. }
  164. }
  165. elseif (is_int($val))
  166. {
  167. $result = (int) $config[$section][$key];
  168. }
  169. elseif (is_string($val) && !empty($config[$section][$key]))
  170. {
  171. $result = (string) $config[$section][$key];
  172. }
  173. }
  174. $this->_configuration[$section][$key] = $result;
  175. }
  176. }
  177. }
  178. // ensure a valid expire default key is set
  179. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options']))
  180. {
  181. $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
  182. }
  183. }
  184. /**
  185. * get configuration as array
  186. *
  187. * return array
  188. */
  189. public function get()
  190. {
  191. return $this->_configuration;
  192. }
  193. /**
  194. * get default configuration as array
  195. *
  196. * return array
  197. */
  198. public static function getDefaults()
  199. {
  200. return self::$_defaults;
  201. }
  202. /**
  203. * get a key from the configuration, typically the main section or all keys
  204. *
  205. * @param string $key
  206. * @param string $section defaults to main
  207. * @throws Exception
  208. * return mixed
  209. */
  210. public function getKey($key, $section = 'main')
  211. {
  212. $options = $this->getSection($section);
  213. if (!array_key_exists($key, $options))
  214. {
  215. throw new Exception(i18n::_('Invalid data.') . " $section / $key", 4);
  216. }
  217. return $this->_configuration[$section][$key];
  218. }
  219. /**
  220. * get a section from the configuration, must exist
  221. *
  222. * @param string $section
  223. * @throws Exception
  224. * return mixed
  225. */
  226. public function getSection($section)
  227. {
  228. if (!array_key_exists($section, $this->_configuration))
  229. {
  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. }