configuration.php 6.1 KB

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