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