Init.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * PHPIDS
  4. *
  5. * Requirements: PHP5, SimpleXML
  6. *
  7. * Copyright (c) 2008 PHPIDS group (https://phpids.org)
  8. *
  9. * PHPIDS is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation, version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * PHPIDS is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * PHP version 5.1.6+
  23. *
  24. * @category Security
  25. * @package PHPIDS
  26. * @author Mario Heiderich <mario.heiderich@gmail.com>
  27. * @author Christian Matthies <ch0012@gmail.com>
  28. * @author Lars Strojny <lars@strojny.net>
  29. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  30. * @link http://php-ids.org/
  31. */
  32. /**
  33. * Framework initiation
  34. *
  35. * This class is used for the purpose to initiate the framework and inhabits
  36. * functionality to parse the needed configuration file.
  37. *
  38. * @category Security
  39. * @package PHPIDS
  40. * @author Christian Matthies <ch0012@gmail.com>
  41. * @author Mario Heiderich <mario.heiderich@gmail.com>
  42. * @author Lars Strojny <lars@strojny.net>
  43. * @copyright 2007-2009 The PHPIDS Groupup
  44. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  45. * @version Release: $Id:Init.php 517 2007-09-15 15:04:13Z mario $
  46. * @link http://php-ids.org/
  47. * @since Version 0.4
  48. */
  49. class IDS_Init
  50. {
  51. /**
  52. * Holds config settings
  53. *
  54. * @var array
  55. */
  56. public $config = array();
  57. /**
  58. * Instance of this class depending on the supplied config file
  59. *
  60. * @var array
  61. * @static
  62. */
  63. private static $instances = array();
  64. /**
  65. * Path to the config file
  66. *
  67. * @var string
  68. */
  69. private $configPath = null;
  70. /**
  71. * Constructor
  72. *
  73. * Includes needed classes and parses the configuration file
  74. *
  75. * @param string $configPath the path to the config file
  76. *
  77. * @return object $this
  78. */
  79. private function __construct($configPath = null)
  80. {
  81. include_once 'IDS/Monitor.php';
  82. include_once 'IDS/Filter/Storage.php';
  83. if ($configPath) {
  84. $this->setConfigPath($configPath);
  85. $this->config = parse_ini_file($this->configPath, true);
  86. }
  87. }
  88. /**
  89. * Permitting to clone this object
  90. *
  91. * For the sake of correctness of a singleton pattern, this is necessary
  92. *
  93. * @return void
  94. */
  95. public final function __clone()
  96. {
  97. }
  98. /**
  99. * Returns an instance of this class. Also a PHP version check
  100. * is being performed to avoid compatibility problems with PHP < 5.1.6
  101. *
  102. * @param string $configPath the path to the config file
  103. *
  104. * @return object
  105. */
  106. public static function init($configPath = null)
  107. {
  108. if (!isset(self::$instances[$configPath])) {
  109. self::$instances[$configPath] = new IDS_Init($configPath);
  110. }
  111. return self::$instances[$configPath];
  112. }
  113. /**
  114. * Sets the path to the configuration file
  115. *
  116. * @param string $path the path to the config
  117. *
  118. * @throws Exception if file not found
  119. * @return void
  120. */
  121. public function setConfigPath($path)
  122. {
  123. if (file_exists($path)) {
  124. $this->configPath = $path;
  125. } else {
  126. throw new Exception(
  127. 'Configuration file could not be found at ' .
  128. htmlspecialchars($path, ENT_QUOTES, 'UTF-8')
  129. );
  130. }
  131. }
  132. /**
  133. * Returns path to configuration file
  134. *
  135. * @return string the config path
  136. */
  137. public function getConfigPath()
  138. {
  139. return $this->configPath;
  140. }
  141. /**
  142. * This method checks if a base path is given and usage is set to true.
  143. * If all that tests succeed the base path will be returned as a string -
  144. * else null will be returned.
  145. *
  146. * @return string the base path or null
  147. */
  148. public function getBasePath() {
  149. return ((isset($this->config['General']['base_path'])
  150. && $this->config['General']['base_path']
  151. && isset($this->config['General']['use_base_path'])
  152. && $this->config['General']['use_base_path'])
  153. ? $this->config['General']['base_path'] : null);
  154. }
  155. /**
  156. * Merges new settings into the exsiting ones or overwrites them
  157. *
  158. * @param array $config the config array
  159. * @param boolean $overwrite config overwrite flag
  160. *
  161. * @return void
  162. */
  163. public function setConfig(array $config, $overwrite = false)
  164. {
  165. if ($overwrite) {
  166. $this->config = $this->_mergeConfig($this->config, $config);
  167. } else {
  168. $this->config = $this->_mergeConfig($config, $this->config);
  169. }
  170. }
  171. /**
  172. * Merge config hashes recursivly
  173. *
  174. * The algorithm merges configuration arrays recursively. If an element is
  175. * an array in both, the values will be appended. If it is a scalar in both,
  176. * the value will be replaced.
  177. *
  178. * @param array $current The legacy hash
  179. * @param array $successor The hash which values count more when in doubt
  180. * @return array Merged hash
  181. */
  182. protected function _mergeConfig($current, $successor)
  183. {
  184. if (is_array($current) and is_array($successor)) {
  185. foreach ($successor as $key => $value) {
  186. if (isset($current[$key])
  187. and is_array($value)
  188. and is_array($current[$key])) {
  189. $current[$key] = $this->_mergeConfig($current[$key], $value);
  190. } else {
  191. $current[$key] = $successor[$key];
  192. }
  193. }
  194. }
  195. return $current;
  196. }
  197. /**
  198. * Returns the config array
  199. *
  200. * @return array the config array
  201. */
  202. public function getConfig()
  203. {
  204. return $this->config;
  205. }
  206. }
  207. /**
  208. * Local variables:
  209. * tab-width: 4
  210. * c-basic-offset: 4
  211. * End:
  212. * vim600: sw=4 ts=4 expandtab
  213. */