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