configuration.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.21.1
  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. ),
  45. 'expire' => array(
  46. 'default' => '1week',
  47. ),
  48. 'expire_options' => array(
  49. '5min' => 300,
  50. '10min' => 600,
  51. '1hour' => 3600,
  52. '1day' => 86400,
  53. '1week' => 604800,
  54. '1month' => 2592000,
  55. '1year' => 31536000,
  56. 'never' => 0,
  57. ),
  58. 'formatter_options' => array(
  59. 'plaintext' => 'Plain Text',
  60. 'syntaxhighlighting' => 'Source Code',
  61. 'markdown' => 'Markdown',
  62. ),
  63. 'traffic' => array(
  64. 'limit' => 10,
  65. 'header' => null,
  66. 'dir' => 'data',
  67. ),
  68. 'model' => array(
  69. 'class' => 'zerobin_data',
  70. ),
  71. 'model_options' => array(
  72. 'dir' => 'data',
  73. ),
  74. );
  75. /**
  76. * parse configuration file and ensure default configuration values are present
  77. *
  78. * @throws Exception
  79. */
  80. public function __construct()
  81. {
  82. $config = parse_ini_file(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', true);
  83. foreach (array('main', 'model') as $section) {
  84. if (!array_key_exists($section, $config)) {
  85. throw new Exception(i18n::_('ZeroBin requires configuration section [%s] to be present in configuration file.', $section), 2);
  86. }
  87. }
  88. foreach ($this->_defaults as $section => $values)
  89. {
  90. if (!array_key_exists($section, $config))
  91. {
  92. $this->_configuration[$section] = $this->_defaults[$section];
  93. if (array_key_exists('dir', $this->_configuration[$section]))
  94. {
  95. $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
  96. }
  97. continue;
  98. }
  99. foreach ($values as $key => $val)
  100. {
  101. if ($key == 'dir')
  102. {
  103. $val = PATH . $val;
  104. }
  105. $result = $val;
  106. if (array_key_exists($key, $config[$section]))
  107. {
  108. if ($val === null)
  109. {
  110. $result = $config[$section][$key];
  111. }
  112. elseif (is_bool($val))
  113. {
  114. $val = strtolower($config[$section][$key]);
  115. if (in_array($val, array('true', 'yes', 'on')))
  116. {
  117. $result = true;
  118. }
  119. elseif (in_array($val, array('false', 'no', 'off')))
  120. {
  121. $result = false;
  122. }
  123. else
  124. {
  125. $result = (bool) $config[$section][$key];
  126. }
  127. }
  128. elseif (is_int($val))
  129. {
  130. $result = (int) $config[$section][$key];
  131. }
  132. elseif (is_string($val) && !empty($config[$section][$key]))
  133. {
  134. $result = (string) $config[$section][$key];
  135. }
  136. }
  137. $this->_configuration[$section][$key] = $result;
  138. }
  139. }
  140. }
  141. /**
  142. * get configuration as array
  143. *
  144. * return array
  145. */
  146. public function get()
  147. {
  148. return $this->_configuration;
  149. }
  150. /**
  151. * get a key from the configuration, typically the main section or all keys
  152. *
  153. * @param string $key
  154. * @param string $section defaults to main
  155. * @throws Exception
  156. * return mixed
  157. */
  158. public function getKey($key, $section = 'main')
  159. {
  160. $options = $this->getSection($section);
  161. if (!array_key_exists($key, $options))
  162. {
  163. throw new Exception(i18n::_('Invalid data.') . " $section / $key", 4);
  164. }
  165. return $this->_configuration[$section][$key];
  166. }
  167. /**
  168. * get a key from the configuration, typically the main section or all keys
  169. *
  170. * @param string $key if empty, return all configuration options
  171. * @param string $section defaults to main
  172. * @throws Exception
  173. * return mixed
  174. */
  175. public function getSection($section)
  176. {
  177. if (!array_key_exists($section, $this->_configuration))
  178. {
  179. throw new Exception(i18n::_('ZeroBin requires configuration section [%s] to be present in configuration file.', $section), 3);
  180. }
  181. return $this->_configuration[$section];
  182. }
  183. }