configuration.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * ZeroBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
  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 $_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. 'base64version' => '2.1.9',
  43. 'languageselection' => false,
  44. 'languagedefault' => '',
  45. ),
  46. 'expire' => array(
  47. 'default' => '1week',
  48. 'clone' => true,
  49. ),
  50. 'expire_options' => array(
  51. '5min' => 300,
  52. '10min' => 600,
  53. '1hour' => 3600,
  54. '1day' => 86400,
  55. '1week' => 604800,
  56. '1month' => 2592000,
  57. '1year' => 31536000,
  58. 'never' => 0,
  59. ),
  60. 'formatter_options' => array(
  61. 'plaintext' => 'Plain Text',
  62. 'syntaxhighlighting' => 'Source Code',
  63. 'markdown' => 'Markdown',
  64. ),
  65. 'traffic' => array(
  66. 'limit' => 10,
  67. 'header' => null,
  68. 'dir' => 'data',
  69. ),
  70. 'model' => array(
  71. 'class' => 'zerobin_data',
  72. ),
  73. 'model_options' => array(
  74. 'dir' => 'data',
  75. ),
  76. );
  77. /**
  78. * parse configuration file and ensure default configuration values are present
  79. *
  80. * @throws Exception
  81. */
  82. public function __construct()
  83. {
  84. $config = array();
  85. $configFile = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
  86. if (is_readable($configFile))
  87. {
  88. $config = parse_ini_file($configFile, true);
  89. foreach (array('main', 'model') as $section) {
  90. if (!array_key_exists($section, $config)) {
  91. throw new Exception(i18n::_('ZeroBin requires configuration section [%s] to be present in configuration file.', $section), 2);
  92. }
  93. }
  94. }
  95. $opts = '_options';
  96. foreach ($this->_defaults as $section => $values)
  97. {
  98. // fill missing sections with default values
  99. if (!array_key_exists($section, $config) || count($config[$section]) == 0)
  100. {
  101. $this->_configuration[$section] = $values;
  102. if (array_key_exists('dir', $this->_configuration[$section]))
  103. {
  104. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  105. }
  106. continue;
  107. }
  108. // provide different defaults for database model
  109. elseif ($section == 'model_options' && $this->_configuration['model']['class'] == 'zerobin_db')
  110. {
  111. $values = array(
  112. 'dsn' => 'sqlite:' . PATH . 'data/db.sq3',
  113. 'tbl' => null,
  114. 'usr' => null,
  115. 'pwd' => null,
  116. 'opt' => array(PDO::ATTR_PERSISTENT => true),
  117. );
  118. }
  119. // "*_options" sections don't require all defaults to be set
  120. if (
  121. $section !== 'model_options' &&
  122. ($from = strlen($section) - strlen($opts)) >= 0 &&
  123. strpos($section, $opts, $from) !== false
  124. )
  125. {
  126. if (is_int(current($values)))
  127. {
  128. $config[$section] = array_map('intval', $config[$section]);
  129. }
  130. $this->_configuration[$section] = $config[$section];
  131. }
  132. // check for missing keys and set defaults if necessary
  133. else
  134. {
  135. foreach ($values as $key => $val)
  136. {
  137. if ($key == 'dir')
  138. {
  139. $val = PATH . $val;
  140. }
  141. $result = $val;
  142. if (array_key_exists($key, $config[$section]))
  143. {
  144. if ($val === null)
  145. {
  146. $result = $config[$section][$key];
  147. }
  148. elseif (is_bool($val))
  149. {
  150. $val = strtolower($config[$section][$key]);
  151. if (in_array($val, array('true', 'yes', 'on')))
  152. {
  153. $result = true;
  154. }
  155. elseif (in_array($val, array('false', 'no', 'off')))
  156. {
  157. $result = false;
  158. }
  159. else
  160. {
  161. $result = (bool) $config[$section][$key];
  162. }
  163. }
  164. elseif (is_int($val))
  165. {
  166. $result = (int) $config[$section][$key];
  167. }
  168. elseif (is_string($val) && !empty($config[$section][$key]))
  169. {
  170. $result = (string) $config[$section][$key];
  171. }
  172. }
  173. $this->_configuration[$section][$key] = $result;
  174. }
  175. }
  176. }
  177. // ensure a valid expire default key is set
  178. if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options']))
  179. {
  180. $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
  181. }
  182. }
  183. /**
  184. * get configuration as array
  185. *
  186. * return array
  187. */
  188. public function get()
  189. {
  190. return $this->_configuration;
  191. }
  192. /**
  193. * get a key from the configuration, typically the main section or all keys
  194. *
  195. * @param string $key
  196. * @param string $section defaults to main
  197. * @throws Exception
  198. * return mixed
  199. */
  200. public function getKey($key, $section = 'main')
  201. {
  202. $options = $this->getSection($section);
  203. if (!array_key_exists($key, $options))
  204. {
  205. throw new Exception(i18n::_('Invalid data.') . " $section / $key", 4);
  206. }
  207. return $this->_configuration[$section][$key];
  208. }
  209. /**
  210. * get a key from the configuration, typically the main section or all keys
  211. *
  212. * @param string $key if empty, return all configuration options
  213. * @param string $section defaults to main
  214. * @throws Exception
  215. * return mixed
  216. */
  217. public function getSection($section)
  218. {
  219. if (!array_key_exists($section, $this->_configuration))
  220. {
  221. throw new Exception(i18n::_('ZeroBin requires configuration section [%s] to be present in configuration file.', $section), 3);
  222. }
  223. return $this->_configuration[$section];
  224. }
  225. }