-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFileWatcher.php
319 lines (279 loc) · 10.4 KB
/
FileWatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/**
* @author Michael Kliewe
* @copyright 2011 Michael Kliewe
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://www.phpgangsta.de/
*/
class FileWatcher
{
/**
* @var array
*/
protected $_config;
/**
* @var string
*/
protected $_providedPassword;
/**
* @var string
*/
protected $_providedOverallHash;
/**
* @param array $options
*/
public function __construct($options = array())
{
if (isset($options['configFilename'])) {
$this->readConfig($options['configFilename']);
}
}
/**
* @param $configFilename
* @return FileWatcher
*/
public function readConfig($configFilename)
{
include $configFilename;
$this->_config = $config;
$this->_log('Config file loaded');
if (php_sapi_name() == "cli") {
$longopts = array(
'password::', // Optional value
'overallHash::', // Optional value
);
$options = getopt('', $longopts);
if (isset($options['password'])) {
$this->_providedPassword = $options['password'];
}
if (isset($options['overallHash'])) {
$this->_providedOverallHash = $options['overallHash'];
}
} else { // not in cli-mode, try to get password from $_GET or $_POST
if (isset($_POST['password'])) {
$this->_providedPassword = $_POST['password'];
} elseif($_GET['password']) {
$this->_providedPassword = $_GET['password'];
}
if (isset($_POST['overallHash'])) {
$this->_providedOverallHash = $_POST['overallHash'];
} elseif($_GET['overallHash']) {
$this->_providedOverallHash = $_GET['overallHash'];
}
}
return $this;
}
/**
* @return FileWatcher
*/
public function checkNow()
{
$this->_log('Checking password...');
$this->_checkPassword();
$this->_log('Checking files...');
$currentHashes = $this->_getCurrentHashes();
$masterHashes = $this->_loadMasterHashes();
// compare both hash arrays
$newFiles = array_diff_key($currentHashes, $masterHashes);
$deletedFiles = array_diff_key($masterHashes, $currentHashes);
$changedFiles = array();
$intersectKeys = array_keys(array_intersect_key($masterHashes, $currentHashes));
foreach ($intersectKeys as $intersectKey) {
if ($masterHashes[$intersectKey] != $currentHashes[$intersectKey]) {
$changedFiles[$intersectKey] = $masterHashes[$intersectKey];
}
}
$overallHash = $this->_calcOverallHash($newFiles, $deletedFiles, $changedFiles);
$this->_log('Overall Hash is: '.$overallHash);
if (count($newFiles) > 0 || count($deletedFiles) > 0 || count($changedFiles) > 0 || (!empty($this->_providedOverallHash) && $overallHash != $this->_providedOverallHash)) {
$this->_log('Sending alerts now...');
$this->_logAlert($newFiles, $deletedFiles, $changedFiles);
$this->_outputAlert($newFiles, $deletedFiles, $changedFiles);
$this->_emailAlert($newFiles, $deletedFiles, $changedFiles);
// save new master hash file if needed
if ($this->_config['overwriteMasterFile']) {
$this->_saveMasterHashes($currentHashes);
}
} else {
$this->_log('Everything OK, exiting...');
}
return $this;
}
/**
* @return array
*/
protected function _getCurrentHashes() {
$currentHashes = array();
foreach ($this->_config['includePaths'] as $includePath) {
$iterator = new RecursiveDirectoryIterator($includePath);
foreach(new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
/** @var $file SplFileInfo */
// check against excludeFolderList
if (!$this->_isPathExcluded($file->getPathname(), $this->_config['excludeFolderList'])) {
if (!$file->isDir()) {
// check against excludeExtensionList
$extension = pathinfo($file->getFilename(), PATHINFO_EXTENSION);
if (!in_array(strtolower($extension), $this->_config['excludeExtensionList'])) {
$hash = sha1_file($file->getPathname());
$currentHashes[$file->getPathname()] = $hash;
$this->_log('Found file '.$file->getPathname().' with sha1: '.$hash);
} else {
$this->_log('File skipped: '.$file->getPathname());
}
}
} else {
$this->_log('Directory skipped: '.$file->getPathname());
}
}
}
return $currentHashes;
}
/**
* @return FileWatcher
*/
protected function _checkPassword()
{
if (!empty($this->_config['password']) && $this->_config['password'] != $this->_providedPassword) {
die('wrong password, exiting now...');
}
return $this;
}
/**
* @param array $files
* @return array
*/
protected function _createAllHashesOfFilelist(array $files) {
$list = array();
foreach ($files as $file) {
$list[$file] = sha1_file($file);
}
return $list;
}
/**
* @param array $files
*/
protected function _saveMasterHashes(array $files) {
$fileContent = '';
foreach ($files as $key => $filepath) {
$fileContent .= $key.'='.$filepath."\n";
}
$fh = fopen($this->_config['hashMasterFilename'], 'w');
fwrite($fh, $fileContent);
fclose($fh);
}
/**
* @return array
*/
protected function _loadMasterHashes() {
$masterFilePath = $this->_config['hashMasterFilename'];
if (!file_exists($masterFilePath)) {
return array();
}
$hashes = array();
$masterFileLines = file($masterFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($masterFileLines as $masterFileLine) {
list($filepath, $hash) = explode('=', $masterFileLine);
$hashes[$filepath] = $hash;
}
return $hashes;
}
/**
* Build the overallHash over all single file hashes
*
* @param array $newFiles
* @param array $deletedFiles
* @param array $changedFiles
* @return string
*/
protected function _calcOverallHash(array $newFiles, array $deletedFiles, array $changedFiles)
{
$merged = array_merge($newFiles, $deletedFiles, $changedFiles);
return sha1(join(',',$merged));
}
/**
* @param array $newFiles
* @param array $deletedFiles
* @param array $changedFiles
*/
protected function _emailAlert(array $newFiles, array $deletedFiles, array $changedFiles)
{
$body = 'new: '.var_export($newFiles, true)."\n".
'deleted: '.var_export($deletedFiles, true)."\n".
'changed: '.var_export($changedFiles, true)."\n";
if ($this->_config['alertEmailMethod'] == 'mail') {
mail(
$this->_config['alertEmailAddress'],
$this->_config['alertEmailSubject'],
$body
);
} elseif ($this->_config['alertEmailMethod'] == 'smtp') {
require_once('PHPMailer_5.2.0/class.phpmailer.php');
// oh man, using PHPMailer is really ugly, but it seems to work
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = false; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = $this->_config['alertEmailSmtpServer']; // sets the SMTP server
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = $this->_config['alertEmailSmtpUser']; // SMTP account username
$mail->Password = $this->_config['alertEmailSmtpPass']; // SMTP account password
$mail->SetFrom($this->_config['alertEmailSmtpUser']);
$mail->AddAddress($this->_config['alertEmailAddress']);
$mail->Subject = $this->_config['alertEmailSubject'];
$mail->Body = $body;
$mail->Send();
} else {
throw new Exception('Invalid value for alertEmailMethod');
}
}
/**
* @param array $newFiles
* @param array $deletedFiles
* @param array $changedFiles
*/
protected function _logAlert(array $newFiles, array $deletedFiles, array $changedFiles)
{
$body = 'new: '.var_export($newFiles, true)."\n".
'deleted: '.var_export($deletedFiles, true)."\n".
'changed: '.var_export($changedFiles, true)."\n";
$this->_log($body);
}
/**
* @param array $newFiles
* @param array $deletedFiles
* @param array $changedFiles
* @return void
*/
protected function _outputAlert(array $newFiles, array $deletedFiles, array $changedFiles)
{
$body = 'new: '.var_export($newFiles, true)."\n".
'deleted: '.var_export($deletedFiles, true)."\n".
'changed: '.var_export($changedFiles, true)."\n";
echo $body;
}
/**
* @param $fullFilename
* @param array $pathArray
* @return bool
*/
protected function _isPathExcluded($fullFilename, array $pathArray) {
foreach ($pathArray as $path) {
$path = rtrim($path, '/\\');
if (strpos($fullFilename, $path) === 0) {
return true;
}
}
return false;
}
/**
* @param $message
*/
protected function _log($message)
{
$fh = fopen($this->_config['logFilename'], 'a');
fwrite($fh, date('Y-m-d H:i:s').' '.$message."\n");
fclose($fh);
}
}