configuration.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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\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. $config = parse_ini_file($configFile, true);
  98. foreach (array('main', 'model', 'model_options') as $section) {
  99. if (!array_key_exists($section, $config)) {
  100. throw new Exception(i18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
  101. }
  102. }
  103. }
  104. $opts = '_options';
  105. foreach (self::getDefaults() as $section => $values) {
  106. // fill missing sections with default values
  107. if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
  108. $this->_configuration[$section] = $values;
  109. if (array_key_exists('dir', $this->_configuration[$section])) {
  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. $values = array(
  122. 'dsn' => 'sqlite:' . PATH . 'data/db.sq3',
  123. 'tbl' => null,
  124. 'usr' => null,
  125. 'pwd' => null,
  126. 'opt' => array(PDO::ATTR_PERSISTENT => true),
  127. );
  128. }
  129. // "*_options" sections don't require all defaults to be set
  130. if (
  131. $section !== 'model_options' &&
  132. ($from = strlen($section) - strlen($opts)) >= 0 &&
  133. strpos($section, $opts, $from) !== false
  134. ) {
  135. if (is_int(current($values))) {
  136. $config[$section] = array_map('intval', $config[$section]);
  137. }
  138. $this->_configuration[$section] = $config[$section];
  139. }
  140. // check for missing keys and set defaults if necessary
  141. else {
  142. foreach ($values as $key => $val) {
  143. if ($key == 'dir') {
  144. $val = PATH . $val;
  145. }
  146. $result = $val;
  147. if (array_key_exists($key, $config[$section])) {
  148. if ($val === null) {
  149. $result = $config[$section][$key];
  150. } elseif (is_bool($val)) {
  151. $val = strtolower($config[$section][$key]);
  152. if (in_array($val, array('true', 'yes', 'on'))) {
  153. $result = true;
  154. } elseif (in_array($val, array('false', 'no', 'off'))) {
  155. $result = false;
  156. } else {
  157. $result = (bool) $config[$section][$key];
  158. }
  159. } elseif (is_int($val)) {
  160. $result = (int) $config[$section][$key];
  161. } elseif (is_string($val) && !empty($config[$section][$key])) {
  162. $result = (string) $config[$section][$key];
  163. }
  164. }
  165. $this->_configuration[$section][$key] = $result;
  166. }
  167. }
  168. }
  169. // support for old config file format, before the fork was renamed and PSR-4 introduced
  170. $this->_configuration['model']['class'] = str_replace(
  171. 'zerobin_', 'privatebin_',
  172. $this->_configuration['model']['class']
  173. );
  174. $this->_configuration['model']['class'] = str_replace(
  175. array('privatebin_data', 'privatebin_db'),
  176. array('PrivateBin\\data\\data', 'PrivateBin\\data\\db'),
  177. $this->_configuration['model']['class']
  178. );
  179. // ensure a valid expire default key is set
  180. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
  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. throw new Exception(i18n::_('Invalid data.') . " $section / $key", 4);
  215. }
  216. return $this->_configuration[$section][$key];
  217. }
  218. /**
  219. * get a section from the configuration, must exist
  220. *
  221. * @param string $section
  222. * @throws Exception
  223. * return mixed
  224. */
  225. public function getSection($section)
  226. {
  227. if (!array_key_exists($section, $this->_configuration)) {
  228. throw new Exception(i18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 3);
  229. }
  230. return $this->_configuration[$section];
  231. }
  232. }