commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / packages / IDS / Log / File.php
1 <?php
2
3 /**
4 * PHPIDS
5 *
6 * Requirements: PHP5, SimpleXML
7 *
8 * Copyright (c) 2008 PHPIDS group (https://phpids.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 require_once 'IDS/Log/Interface.php';
35
36 /**
37 * File logging wrapper
38 *
39 * The file wrapper is designed to store data into a flatfile. It implements the
40 * singleton pattern.
41 *
42 * @category Security
43 * @package PHPIDS
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/
51 */
52 class IDS_Log_File implements IDS_Log_Interface
53 {
54
55 /**
56 * Path to the log file
57 *
58 * @var string
59 */
60 private $logfile = null;
61
62 /**
63 * Instance container
64 *
65 * Due to the singleton pattern this class allows to initiate only one
66 * instance for each file.
67 *
68 * @var array
69 */
70 private static $instances = array();
71
72 /**
73 * Holds current remote address
74 *
75 * @var string
76 */
77 private $ip = 'local/unknown';
78
79 /**
80 * Constructor
81 *
82 * @param string $logfile path to the log file
83 *
84 * @return void
85 */
86 protected function __construct($logfile)
87 {
88
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'] . ')' : '');
93
94 $this->logfile = $logfile;
95 }
96
97 /**
98 * Returns an instance of this class
99 *
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.
103 *
104 * @param mixed $config IDS_Init or path to a file
105 * @param string $classname the class name to use
106 *
107 * @return object $this
108 */
109 public static function getInstance($config, $classname = 'IDS_Log_File')
110 {
111 if ($config instanceof IDS_Init) {
112 $logfile = $config->getBasePath() . $config->config['Logging']['path'];
113 } elseif (is_string($config)) {
114 $logfile = $config;
115 }
116
117 if (!isset(self::$instances[$logfile])) {
118 self::$instances[$logfile] = new $classname($logfile);
119 }
120
121 return self::$instances[$logfile];
122 }
123
124 /**
125 * Permitting to clone this object
126 *
127 * For the sake of correctness of a singleton pattern, this is necessary
128 *
129 * @return void
130 */
131 private function __clone()
132 {
133 }
134
135 /**
136 * Prepares data
137 *
138 * Converts given data into a format that can be stored into a file.
139 * You might edit this method to your requirements.
140 *
141 * @param mixed $data incoming report data
142 *
143 * @return string
144 */
145 protected function prepareData($data)
146 {
147
148 $format = '"%s",%s,%d,"%s","%s","%s","%s"';
149
150 $attackedParameters = '';
151 foreach ($data as $event) {
152 $attackedParameters .= $event->getName() . '=' .
153 rawurlencode($event->getValue()) . ' ';
154 }
155
156 $dataString = sprintf($format,
157 urlencode($this->ip),
158 date('c'),
159 $data->getImpact(),
160 join(' ', $data->getTags()),
161 urlencode(trim($attackedParameters)),
162 urlencode($_SERVER['REQUEST_URI']),
163 $_SERVER['SERVER_ADDR']
164 );
165
166 return $dataString;
167 }
168
169 /**
170 * Stores given data into a file
171 *
172 * @param object $data IDS_Report
173 *
174 * @throws Exception if the logfile isn't writeable
175 * @return boolean
176 */
177 public function execute(IDS_Report $data)
178 {
179
180 /*
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
183 * into a file
184 */
185 $data = $this->prepareData($data);
186
187 if (is_string($data)) {
188
189 if (file_exists($this->logfile)) {
190 $data = trim($data);
191
192 if (!empty($data)) {
193 if (is_writable($this->logfile)) {
194
195 $handle = fopen($this->logfile, 'a');
196 fwrite($handle, trim($data) . "\n");
197 fclose($handle);
198
199 } else {
200 throw new Exception(
201 'Please make sure that ' . $this->logfile .
202 ' is writeable.'
203 );
204 }
205 }
206 } else {
207 throw new Exception(
208 'Given file does not exist. Please make sure the
209 logfile is present in the given directory.'
210 );
211 }
212 } else {
213 throw new Exception(
214 'Please make sure that data returned by
215 IDS_Log_File::prepareData() is a string.'
216 );
217 }
218
219 return true;
220 }
221 }
222
223 /**
224 * Local variables:
225 * tab-width: 4
226 * c-basic-offset: 4
227 * End:
228 * vim600: sw=4 ts=4 expandtab
229 */