-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck4change.php
More file actions
264 lines (239 loc) · 10.7 KB
/
check4change.php
File metadata and controls
264 lines (239 loc) · 10.7 KB
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
<?php
/*~ check4change.php
.---------------------------------------------------------------------------.
| Software: PHPCheck4Change - PHP script that scan directories and files |
| for change during interval |
| Version: 1.1 |
| Site: |
| ------------------------------------------------------------------------- |
| Authors: Com'onSoft https://www.comonsoft.com |
| Founder: Com'onSoft (original founder) |
| ------------------------------------------------------------------------- |
| License: Distributed under the Lesser General Public License (LGPL) |
| https://www.gnu.org/copyleft/lesser.html |
| This program is distributed in the hope that it will be useful - WITHOUT |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| FITNESS FOR A PARTICULAR PURPOSE. |
'---------------------------------------------------------------------------'
*/
/**
* check4change - PHP script to detect directories and files modifications
* NOTE: Requires PHP version 5.3 or later
* @package PHPCheck4Change
* @author Com'onSoft
* @copyright 2022 Com'onSoft
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
/* Debug only */
if (!defined('_MODE_DEBUG_')) {
define('_MODE_DEBUG_', false);
}
if (_MODE_DEBUG_ === true) {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
set_time_limit(0); // No time limit, normaly script running on a cron job as a php command line
// Constants used for mail() see PHP user manual, should not be modified
define("cMAIL_HtmlFormat","0");
define("cMAIL_TextFormat","1");
define("cMAIL_HighPriority","1");
define("cMAIL_NormalPriority","3");
define("cMAIL_LowPriority","5");
// Constants messages with language suffix, feel free to change messages used for report
// english
define("cMSG-REPORT-TITLE_EN",".:: LIST OF FILES/FOLDERS CREATED OR MODIFIED LESS THAN %s MINUTES ::.\r\n\r\n");
define("cMSG-REPORT-LIST-HEADER_EN","OBJECT\tCREATION\tMODIFICATION\tTYPE\r\n");
define("cMSG-REPORT-CREATION_EN","created");
define("cMSG-REPORT-MODIFICATION_EN","modified");
define("cMSG-REPORT-START_EN","Start:");
define("cMSG-REPORT-END_EN","End:");
define("cMSG-REPORT-OBJECTS_EN","Number of scanned objects:");
// french
define("cMSG-REPORT-TITLE_FR",".:: LISTE DES FICHIERS/DOSSIERS MODIFIES OU CREES IL Y A MOINS DE %s MINUTES ::.\r\n\r\n");
define("cMSG-REPORT-LIST-HEADER_FR","OBJET\tCRÉATION\tMODIFICATION\tTYPE\r\n");
define("cMSG-REPORT-CREATION_FR","créé");
define("cMSG-REPORT-MODIFICATION_FR","modifié");
define("cMSG-REPORT-START_FR","Début:");
define("cMSG-REPORT-END_FR","Fin:");
define("cMSG-REPORT-OBJECTS_FR","Nombre d'objets scannés:");
/*-------------------------------------------------------------------------
* Class : DirFilter extend RecursiveFilterIterator
* Purpose : Filter list of directories to exclude from scan
*-------------------------------------------------------------------------*/
class DirFilter extends RecursiveFilterIterator
{
private static $mExcludes = array(); // List of directories to exclude
public function setExcludeDirs( $inArrayDir) {
if( $inArrayDir && count($inArrayDir)>0 )
DirFilter::$mExcludes = $inArrayDir;
}
public function accept() {
return !($this->isDir() && in_array($this->getFilename(), DirFilter::$mExcludes));
}
}
//-----------End function : DirFilter()------------------------------------
/*
* Class : scanDirectory( $inPath=null, $inLang='EN', $inDelta = 3600, $inExclude=null)
* Purpose : Scan directory recursively, scan for date creation/modification according to delta
* Generate a report and send report by mail
*/
class scanDirectory {
private $nbObjects = 0; // Number of objects scanned
private $start, $end ; // Timers
private $result; // Array result of scan
private $report = null; // Human readable report
private $delta = 3600 ; // Default delta interval in seconds
private $homeDir ; // Start directory scanning
private $excludeDir = null; // Array of exclude directories
private $lang = 'EN' ; // Default language for report
private $excludeFile = null; // Array of exclude files
private $reportDir = false; // Display or not directories in report
/*
* function: __constructor( $inPath=null, $inDelta = 3600, $inExclude=null)
* Parameters
* string $inPath = start path to scan
* chars(2) = language iso code for report
* int $inDelta = time delta to check in seconds. Recomanded delta is 3600s interval for a cron running each hour
* array string $inExclude = null or array of directories exclusion
* array string $inExcludeFile = null or array of directories exclusion
*/
function __construct( $inPath=null, $inLang='EN', $inDelta = 3600, $inExclude=null, $inExcludeFile = null, $inReportDir=false) {
if( is_null( $inPath) )
$this->homeDir = dirname(__DIR__);
else
$this->homeDir = $inPath;
if( $inDelta )
$this->delta = $inDelta;
if( $inExclude )
$this->excludeDir = $inExclude;
if($inExcludeFile)
$this->excludeFile = $inExcludeFile;
if(is_bool($inReportDir))
$this->reportDir = $inReportDir;
// Verify supported languages
if( $inLang=='EN' || $inLang=='FR')
$this->lang = $inLang;
}
//-----------End default constructor------------------------------------
/*
* Function : Run()
* Purpose : Scan directory recursively, scan for date creation/modification according to delta
* Generate multidimensional array of files/directories changed or created since last delta
* string array[]['obj'] = full path file or directory
* string array[]['date'] = date time cretation or modification
* int array[]['cre'] = true if object is creation
* and process for report
* Return
* int number of objects scanned
*/
public function Run() {
$this->start = date("Y/m/d H:i:s", time());
clearstatcache( true) ; // Be sure to clear the cache before
$res = array();
$root = new RecursiveDirectoryIterator($this->homeDir, FilesystemIterator::SKIP_DOTS);
if( $this->excludeDir ) {
$root = new DirFilter($root);
$root->setExcludeDirs( $this->excludeDir);
}
foreach ( new RecursiveIteratorIterator($root, RecursiveIteratorIterator::SELF_FIRST) as $filename => $obj ) {
if($obj->isFile() && !empty($this->excludeFile)){
if(in_array($obj->getFilename(), $this->excludeFile)){
continue;
}
}
$this->nbObjects++;
if($obj->isDir() && $this->reportDir==false){
continue;
}
$datec = exec('stat --format=%W '.$obj->getRealPath());
if( $datec==false) {
$datec = $obj->getCTime();
}
$date = $obj->getMTime();
if( $datec==$date ) { // Object created
$isCreation = true ;
}
else {
$isCreation = 0 ;
}
if( (time()-$date) <= $this->delta ) {
$res[] = array( 'obj' => $filename, 'datec' => date("Y/m/d H:i:s", $datec), 'date' => date("Y/m/d H:i:s", $date), 'cre' => $isCreation) ;
}
}
$this->result = $res;
$this->Report();
return $this->nbObjects;
}
//-----------End method : Run()------------------------------------
/*
* Function : Report()
* Purpose : Generate a human readable report from scan
* Return
* void
*/
private function Report() {
if( count($this->result)>0 ) { // Generate report only if something to notify
$this->report = sprintf( constant('cMSG-REPORT-TITLE_'.$this->lang), ($this->delta)/60);
$this->report .= constant('cMSG-REPORT-LIST-HEADER_'.$this->lang);
foreach( $this->result as $obj) {
if( $obj['cre']==true )
$msgType = constant('cMSG-REPORT-CREATION_'.$this->lang);
else
$msgType = constant('cMSG-REPORT-MODIFICATION_'.$this->lang);
$this->report .= $obj['obj']."\t".$obj['datec']."\t".$obj['date']."\t$msgType\r\n";
}
$this->end = date("Y/m/d H:i:s", time());
$this->report = 'CRON: '.__FILE__."\r\n".constant('cMSG-REPORT-START_'.$this->lang)." $this->start\r\n".constant('cMSG-REPORT-OBJECTS_'.$this->lang)." $this->nbObjects\r\n".constant('cMSG-REPORT-END_'.$this->lang)." $this->end\r\n\r\n$this->report";
}
}
//-----------End method : Report()------------------------------------
/*-------------------------------------------------------------------------
* Function : boolean MailReport( $inFrom, $inDest, $inSubject , $inPrio, $inFormat, $inCharset, $inCc)
* Purpose : Send report by mail in text or html mode
* Parameters
* string inFrom from recipient
* string inDest dest recipient
* string inSubject message subject
* int inPrio message priority : 1:Highest / 3:Normal / 5: lowest
* int inFormat message format : 0:TEXT / 1:HTML
* string inCharset : utf-8, ansi ...
* Return
* true if OK
* false otherwise
* External function(s)
* stripslashes()
* mail()
*-------------------------------------------------------------------------*/
public function MailReport ($inFrom, $inDest, $inSubject, $inPrio=cMAIL_NormalPriority, $inFormat=cMAIL_TextFormat, $inCharset="utf-8", $inCc=null)
{
if( ! is_null( $this->report) ) {
$headers = "From: ". $inFrom . "\n"; // Initialize mail Header
if ($inPrio!=cMAIL_NormalPriority) $headers .= "X-Priority: ".$inPrio."\n"; // Set Message priority
if ($inCc) $headers .= "Cc: ".$inCc."\n"; // Set carbon copy
// Safe mode
$headers .= "Errors-To: $inFrom\n";
$headers .= "Return-Path: ".$inFrom."\n";
$headers .= "Reply-To: $inFrom\n";
$headers .= "X-Sender: $inFrom\n";
$headers .= "X-auth-smtp-user: $inFrom\n";
$headers .= "X-Mailer: Security pack COMONSOFT\n";
$format_body = stripslashes( $this->report);
if ($inFormat==cMAIL_HtmlFormat) {
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=".$inCharset."\n"; // Type MIME
}
else
$headers .= "Content-Type: text/plain; charset=".$inCharset."\n"; // Type text
$format_subject = stripslashes( $inSubject );
return mail($inDest, $format_subject, $format_body , $headers );
}
}
//-----------End method : MailReport()------------------------------------
}
//---------------------End class: scanDirectory-------------------------------
// -- MAIN --------------------------------
$scan = new scanDirectory( dirname(__DIR__), 'FR');
$scan->Run();
$scan->MailReport( 'sender@yourdomain.com', 'receiver@yourdomain.com', 'Alert modifications: www.yourdomain.com');
?>