6 * Requirements: PHP5, SimpleXML
8 * Copyright (c) 2008 PHPIDS group (https://phpids.org)
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.
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.
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/>.
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/
34 require_once 'IDS/Log/Interface.php';
37 * File logging wrapper
39 * The file wrapper is designed to store data into a flatfile. It implements the
44 * @author Christian Matthies <ch0012@gmail.com>
45 * @author Mario Heiderich <mario.heiderich@gmail.com>
46 * @author Lars Strojny <lars@strojny.net>
47 * @copyright 2007-2009 The PHPIDS Group
48 * @license http://www.gnu.org/licenses/lgpl.html LGPL
49 * @version Release: $Id:File.php 517 2007-09-15 15:04:13Z mario $
50 * @link http://php-ids.org/
52 class IDS_Log_File
implements IDS_Log_Interface
56 * Path to the log file
60 private $logfile = null;
65 * Due to the singleton pattern this class allows to initiate only one
66 * instance for each file.
70 private static $instances = array();
73 * Holds current remote address
77 private $ip = 'local/unknown';
82 * @param string $logfile path to the log file
86 protected function __construct($logfile)
89 // determine correct IP address and concat them if necessary
90 $this->ip
= $_SERVER['REMOTE_ADDR'] .
91 (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ?
92 ' (' . $_SERVER['HTTP_X_FORWARDED_FOR'] . ')' : '');
94 $this->logfile
= $logfile;
98 * Returns an instance of this class
100 * This method allows the passed argument to be either an instance of
101 * IDS_Init or a path to a log file. Due to the singleton pattern only one
102 * instance for each file can be initiated.
104 * @param mixed $config IDS_Init or path to a file
105 * @param string $classname the class name to use
107 * @return object $this
109 public static function getInstance($config, $classname = 'IDS_Log_File')
111 if ($config instanceof IDS_Init
) {
112 $logfile = $config->getBasePath() . $config->config
['Logging']['path'];
113 } elseif (is_string($config)) {
117 if (!isset(self
::$instances[$logfile])) {
118 self
::$instances[$logfile] = new $classname($logfile);
121 return self
::$instances[$logfile];
125 * Permitting to clone this object
127 * For the sake of correctness of a singleton pattern, this is necessary
131 private function __clone()
138 * Converts given data into a format that can be stored into a file.
139 * You might edit this method to your requirements.
141 * @param mixed $data incoming report data
145 protected function prepareData($data)
148 $format = '"%s",%s,%d,"%s","%s","%s","%s"';
150 $attackedParameters = '';
151 foreach ($data as $event) {
152 $attackedParameters .= $event->getName() . '=' .
153 rawurlencode($event->getValue()) . ' ';
156 $dataString = sprintf($format,
157 urlencode($this->ip
),
160 join(' ', $data->getTags()),
161 urlencode(trim($attackedParameters)),
162 urlencode($_SERVER['REQUEST_URI']),
163 $_SERVER['SERVER_ADDR']
170 * Stores given data into a file
172 * @param object $data IDS_Report
174 * @throws Exception if the logfile isn't writeable
177 public function execute(IDS_Report
$data)
181 * In case the data has been modified before it might be necessary
182 * to convert it to string since we can't store array or object
185 $data = $this->prepareData($data);
187 if (is_string($data)) {
189 if (file_exists($this->logfile
)) {
193 if (is_writable($this->logfile
)) {
195 $handle = fopen($this->logfile
, 'a');
196 fwrite($handle, trim($data) . "\n");
201 'Please make sure that ' . $this->logfile
.
208 'Given file does not exist. Please make sure the
209 logfile is present in the given directory.'
214 'Please make sure that data returned by
215 IDS_Log_File::prepareData() is a string.'
228 * vim600: sw=4 ts=4 expandtab