Skip to content

Commit ce1fad9

Browse files
committed
Multiple features
Able to specify a full file location for exclusion ("./folder/subfolder/file.ext") Debug switch now available External config file option
1 parent 79c7fbb commit ce1fad9

File tree

2 files changed

+264
-86
lines changed

2 files changed

+264
-86
lines changed

tripwire.php

Lines changed: 205 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<pre><?php
1+
<?php
22

33
/*
44
* Tripwire
@@ -18,56 +18,80 @@
1818
*
1919
*/
2020

21-
$config = array(
22-
23-
// File Containing the MD5 Keys
24-
'md5file' => 'tripwire_filelist.md5' ,
25-
// Delimited for Filelist
26-
'delim' => '&&&' ,
27-
28-
'exclude' => array(
29-
// Specific Files to Exclude from Scanning
30-
'files' => array(
31-
'.' ,
32-
'..' ,
33-
'tripwire_filelist.md5' ,
34-
'error_log' ,
35-
'backup.zip' ,
36-
'.bash_history'
37-
) ,
38-
// Extensions to Exclude from Scanning
39-
'extensions' => array(
40-
'afm' ,
41-
// Flash
42-
'flv' , 'swf' ,
43-
// Images
44-
'bmp' , 'gif' , 'jpeg' , 'jpg' , 'png' , 'psd' ,
45-
'log' ,
46-
'txt' ,
47-
// Videos
48-
'mp4' , 'mov' , 'ogv' , 'webm' ,
49-
// Dreamweaver
50-
'mno' ,
51-
// Audio
52-
'aif' , 'mp3' ,
53-
// Microsoft Word Files
54-
'doc' , 'docx' , 'xls' , 'xlsx' ,
55-
// Compressed Files
56-
'7z' , 'rar' , 'sitx' , 'zip'
57-
) ,
58-
'reg'
59-
) ,
21+
set_time_limit( 0 );
22+
23+
if( file_exists( 'tripwire_config.php' ) ){
24+
25+
# Standalone Config File (for easy upgrading of this file)
26+
require_once( 'tripwire_config.php' );
27+
28+
}else{
6029

61-
'email' => array(
62-
'to' => array( // Email these people on changes
63-
// 'user@server.com'
30+
# Default Configuration
31+
$config = array(
32+
33+
// Debugging (Verbose Output)
34+
'debug' => false ,
35+
36+
// File Containing the MD5 Keys
37+
'md5file' => 'tripwire_filelist.md5' ,
38+
// Delimited for Filelist
39+
'delim' => '&&&' ,
40+
41+
'exclude' => array(
42+
// Specific Files to Exclude from Scanning
43+
'files' => array(
44+
'.' ,
45+
'..' ,
46+
'tripwire_filelist.md5' ,
47+
'tripwire_config.php' ,
48+
'error_log' ,
49+
'backup.zip' ,
50+
'.bash_history'
51+
) ,
52+
// Extensions to Exclude from Scanning
53+
'extensions' => array(
54+
'afm' ,
55+
// Flash
56+
'flv' , 'swf' ,
57+
// Images
58+
'bmp' , 'gif' , 'jpeg' , 'jpg' , 'png' , 'psd' ,
59+
'log' ,
60+
'txt' ,
61+
// Videos
62+
'mp4' , 'mov' , 'ogv' , 'webm' ,
63+
// Dreamweaver
64+
'mno' ,
65+
// Audio
66+
'aif' , 'mp3' ,
67+
// Microsoft Word Files
68+
'doc' , 'docx' , 'xls' , 'xlsx' ,
69+
// Compressed Files
70+
'7z' , 'rar' , 'sitx' , 'zip'
71+
)
6472
) ,
65-
'title' => '[Tripwire] Change Detected - [X] Files' , // The Email Title
66-
'body' => "Tripwire (https://github.com/lucanos/Tripwire) has detected a number of changes:\n\n[AN] Files Added:\n[AF]\n\n[MN] Files Modified:\n[MF]\n\n[DN] Files Deleted:\n[DF]\n\n" // The Email Template
67-
)
6873

69-
);
74+
'email' => array(
75+
'to' => array( // Email these people on changes
76+
// 'user@server.com'
77+
) ,
78+
'title' => '[Tripwire] Change Detected - [X] Files' , // The Email Title
79+
'body' => "Tripwire (https://github.com/lucanos/Tripwire) has detected a number of changes:\n\n[AN] Files Added:\n[AF]\n\n[MN] Files Modified:\n[MF]\n\n[DN] Files Deleted:\n[DF]\n\n" // The Email Template
80+
)
81+
82+
);
83+
84+
}
85+
86+
7087

88+
define( 'DEBUG' , isset( $config['debug'] ) && $config['debug'] );
89+
90+
91+
function debugMsg( $msg = false ){
92+
if( DEBUG && $msg )
93+
echo $msg;
94+
}
7195

7296

7397
function run_tripwire( $dir=false ){
@@ -82,32 +106,52 @@ function run_tripwire( $dir=false ){
82106
$dir = substr( $dir , 0 , -1 );
83107

84108
// If the supplied variable is not a Directory, terminate
85-
if( !is_dir( $dir ) )
109+
if( !is_dir( $dir ) ){
110+
debugMsg( "Directory '$dir' does not exist.\n" );
86111
return false;
112+
}
113+
debugMsg( "Checking directory '$dir'\n" );
87114

88115
$temp = array();
89116
$d = dir( $dir );
90117

91118
// Loop through the files
92119
while( false!==( $entry = $d->read() ) ){
120+
121+
// Full Entry (including Directory)
122+
$entry_full = $dir.'/'.$entry;
123+
124+
// Debugging
125+
debugMsg( "<tr><th align='left'>$entry_full</th>" );
126+
93127
// Symbolic Link - Excluded
94-
if( is_link( $entry ) )
128+
if( is_link( $entry ) ){
129+
debugMsg( "<td><em>Symbolic Link</em></td></tr>\n" );
95130
continue;
131+
}
96132

97133
// Excluded File/Folder
98-
if( in_array( $entry , $config['exclude']['files'] ) )
134+
if( $f1 = in_array( $entry , $config['exclude']['files'] )
135+
|| in_array( $entry_full , $config['exclude']['files'] ) ){
136+
debugMsg( "<td><em>Excluded File/Folder (".array_search( isset( $f1 ) && $f1 ? $entry : $entry_full , $config['exclude']['files'] ).")</em></td></tr>\n" );
99137
continue;
138+
}
100139

101140
// Excluded File Extension
102-
if( in_array( pathinfo( $entry , PATHINFO_EXTENSION ) , $config['exclude']['extensions'] ) )
141+
if( in_array( pathinfo( $entry , PATHINFO_EXTENSION ) , $config['exclude']['extensions'] ) ){
142+
debugMsg( "<td><em>Excluded File Extension (".array_search( pathinfo( $entry , PATHINFO_EXTENSION ) , $config['exclude']['extensions'] ).")</em></td></tr>\n" );
103143
continue;
144+
}
104145

105146
if( is_dir( $dir.'/'.$entry ) ){
106147
// Recurse
148+
debugMsg( "<td>Directory - Recursing</td></tr>\n" );
107149
$temp = array_merge( $temp , run_tripwire( $dir.'/'.$entry ) );
108150
}else{
109151
$md5 = @md5_file( $dir.'/'.$entry );
152+
debugMsg( "<td>$md5</td></tr>\n" );
110153
if( !$md5 ){
154+
debugMsg( "<td><strong>Unreadable. Adding to Unreadable List.</strong></td></tr>\n" );
111155
file_put_contents( 'tripwire_unreadable.txt' , "{$dir}/{$entry} - Unreadable\n" , FILE_APPEND );
112156
}else{
113157
$temp[$dir.'/'.$entry] = $md5;
@@ -154,11 +198,35 @@ function file_put_contents( $filename , $data ){
154198

155199

156200

201+
// Format
202+
if( DEBUG ){
203+
?>
204+
<pre><?php var_dump( $config['exclude'] ); ?></pre>
205+
<table border="1" cellpadding="2" cellspacing="0">
206+
<thead>
207+
<tr>
208+
<th>File</th>
209+
<th>Result</th>
210+
</tr>
211+
</thead>
212+
<tbody>
213+
<?php
214+
}
215+
216+
157217
// Init the This Check List
158218
$now = run_tripwire( '.' );
159219

160220

161221

222+
if( DEBUG ){
223+
?>
224+
</tbody>
225+
</table>
226+
<?php
227+
}
228+
229+
162230
// Perform Comparisons
163231

164232
// New Files = Files in $now, but not in $last
@@ -171,16 +239,18 @@ function file_put_contents( $filename , $data ){
171239
$modified = array_diff_assoc( array_intersect_key( $last , $now ) , array_intersect_key( $now , $last ) );
172240

173241

242+
if( DEBUG ){
174243

175-
echo "\$new\n";
176-
var_dump( $new );
177-
178-
echo "\$deleted\n";
179-
var_dump( $deleted );
180-
181-
echo "\$modified\n";
182-
var_dump( $modified );
244+
echo '<pre>';
245+
echo "\$new\n";
246+
var_dump( $new );
247+
echo "\$deleted\n";
248+
var_dump( $deleted );
249+
echo "\$modified\n";
250+
var_dump( $modified );
251+
echo '</pre>';
183252

253+
}
184254

185255

186256
if( !count( $last ) // If there was no file list
@@ -203,48 +273,97 @@ function file_put_contents( $filename , $data ){
203273

204274

205275
// If there was a Filelist from the last run to compare against, and changes have occurred
276+
277+
# Changes Detected
278+
279+
// Prepare Report
280+
206281
if( count( $last )
207282
&& ( count( $new ) || count( $deleted ) || count( $modified ) ) ){
208-
209283
# Changes Detected
210284

211-
// If there are email addresses to notify
212-
if( count( $config['email']['to'] ) ){
285+
// Prepare the placeholder details
286+
$body_replacements = array(
287+
'[AN]' => count( $new ) ,
288+
'[AF]' => ( count( $new ) ? implode( "\n" , $new ) : 'No Files' ) ,
289+
'[MN]' => count( $modified ) ,
290+
'[MF]' => ( count( $modified ) ? implode( "\n" , array_keys( $modified ) ) : 'No Files' ) ,
291+
'[DN]' => count( $deleted ) ,
292+
'[DF]' => ( count( $deleted ) ? implode( "\n" , $deleted ) : 'No Files' ) ,
293+
);
294+
$title = str_replace( '[X]' , ( count( $new )+count( $deleted )+count( $modified ) ) , $config['email']['title'] );
295+
296+
// Send Email Flag
297+
$sendEmail = true;
298+
299+
}elseif( count( $last ) ){
300+
# No Changes Detected
301+
302+
// Prepare the placeholder details
303+
$body_replacements = array(
304+
'[AN]' => 0 ,
305+
'[AF]' => 'No Files' ,
306+
'[MN]' => 0 ,
307+
'[MF]' => 'No Files' ,
308+
'[DN]' => 0 ,
309+
'[DF]' => 'No Files' ,
310+
);
311+
$title = str_replace( '[X]' , 0 , $config['email']['title'] );
312+
313+
// Send Email Flag
314+
$sendEmail = false;
315+
316+
}else{
317+
# First Run
318+
319+
// Prepare the placeholder details
320+
$body_replacements = array(
321+
'[AN]' => count( $new ) ,
322+
'[AF]' => ( count( $new ) ? implode( "\n" , $new ) : 'No Files' ) ,
323+
'[MN]' => 0 ,
324+
'[MF]' => 'No Files' ,
325+
'[DN]' => 0 ,
326+
'[DF]' => 'No Files' ,
327+
);
328+
$title = str_replace( '[X]' , count( $new ) , $config['email']['title'] );
329+
330+
// Adjust the Template
331+
$config['email']['body'] = "Tripwire has made it's first pass of your files.\n\n".$config['email']['body'];
332+
333+
// Send Email Flag
334+
$sendEmail = true;
335+
336+
}
337+
338+
339+
// Perform the Placeholder Substitutions within the Body
340+
$body = str_replace(
341+
array_keys( $body_replacements ) ,
342+
$body_replacements ,
343+
$config['email']['body']
344+
);
345+
213346

214-
# Compile the Email
347+
if( count( $config['email']['to'] ) ){
215348

216-
// Prepare the placeholder details
217-
$body_replacements = array(
218-
'[AN]' => count( $new ) ,
219-
'[AF]' => ( count( $new ) ? implode( "\n" , $new ) : 'No Files' ) ,
220-
'[MN]' => count( $modified ) ,
221-
'[MF]' => ( count( $modified ) ? implode( "\n" , array_keys( $modified ) ) : 'No Files' ) ,
222-
'[DN]' => count( $deleted ) ,
223-
'[DF]' => ( count( $deleted ) ? implode( "\n" , $deleted ) : 'No Files' ) ,
224-
);
349+
if( $sendEmail ){
225350

226351
// Prepare the recipients
227352
$to = implode( ', ' , $config['email']['to'] );
228-
// Prepare the Subject Line
229-
$title = str_replace( '[X]' , ( count( $new )+count( $deleted )+count( $modified ) ) , $config['email']['title'] );
230-
// Perform the Placeholder Substitutions within the Body
231-
$body = str_replace(
232-
array_keys( $body_replacements ) ,
233-
$body_replacements ,
234-
$config['email']['body']
235-
);
236-
237353
// Send it
238354
if( mail( $to , $title , $body ) ){
239355
echo "Email Sent Successfully\n";
240356
}else{
241357
echo "Email Failed\n";
242358
}
243-
359+
360+
}else{
361+
362+
// No Email Needed
363+
244364
}
245-
365+
246366
}else{
247-
248-
# No Changes Detected
249-
367+
// Just echo the result
368+
echo '<pre>'.$body.'</pre>';
250369
}

0 commit comments

Comments
 (0)