Purpose: detect filesystem changes on a (web)server and send list of changed or added files via email. Can send a kind of alert as SMS if your mobile network operator supports email to sms gateway. Tip: see Wikipedia for "List of SMS gateways".
Usage:
Change script settings (in file) according to your needs
Upload to your (web)server
Setup cron job to run it periodically;
- via PHP "php -q /home/serverpath/script/change-detect-alert.php"
- via cURL "curl http://www.example.com/path/to/change-detect-alert.php"
Notes: script checks file size only, will possibly add date and contents comparison later if needed; invoking via PHP is preferred; you may need to specify absolute path to php or curl
Requirements: Change Detect and Alert is a script which is supposed to be run on a server with PHP and SQLite3 installed.
I use it to detect ebook uploads on my ftp server. The only change i made: invoke external program in addition to chnages email. Really helpful...
Usage:
Change script settings (in file) according to your needs
Upload to your (web)server
Setup cron job to run it periodically;
- via PHP "php -q /home/serverpath/script/change-detect-alert.php"
- via cURL "curl http://www.example.com/path/to/change-detect-alert.php"
Notes: script checks file size only, will possibly add date and contents comparison later if needed; invoking via PHP is preferred; you may need to specify absolute path to php or curl
Requirements: Change Detect and Alert is a script which is supposed to be run on a server with PHP and SQLite3 installed.
PHP Code:
// Specify your email here. Use comma as delimiter if you want to sent to multiple emails
define('EMAIL', 'youremail@example.com');
// Specify path from the root folder where script will store its database, NO ENDING SLASH
define('DBPATH', '/usr/home/my-site/www');
// Filename of database (no path)
define('DBNAME', 'comments.idx');
// folder being tracked, NO ENDING SLASH
define('CHECK_PATH', '/usr/home/my-site/www/uploads');
// true - check all subfolders of CHECK_PATH
// false - do not check subfolders
define('CHECK_RECURSIVE', true);
###############################################################
## YOU DO NOT HAVE TO CHANGE ANYTHING BELOW UNLESS YOU KNOW WHAT YOU ARE DOING
###############################################################
define('DBFILE', DBPATH . '/' .DBNAME);
define('CHG_UNCHANGED', 'UNCHANGED');
define('CHG_ADDED', 'ADDED');
define('CHG_CHANGED', 'CHANGED');
function createDb() {
$db = new SQLiteDatabase(DBFILE);
$db->queryExec("CREATE TABLE tbl_files (f_name TEXT, f_size INTEGER, PRIMARY KEY (f_name))");
}
function sendEmail($changesBody, $time) {
if (empty($changesBody)) return;
// set email headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=\"us-ascii\"\r\n";
$headers .= "From: " . EMAIL . "\r\n";
$headers .= "Reply-To: " . EMAIL . "\r\n";
$headers .= 'X-Mailer: Change Detect and Alert';
$message = $changesBody . "
Job took from start to finish: " . $time;
if (EMAIL != '') {
// send email
@mail(EMAIL, // TO email
'Server Change Alert', // subject
$message, // email text
$headers); // headers
}
}
function isChanged($filename) {
if (strpos($filename, DBNAME) != FALSE) return '';
$isChanged = CHG_UNCHANGED;
$db = new SQLiteDatabase(DBFILE);
$q = $db->query("SELECT f_size FROM tbl_files WHERE f_name = '$filename' ");
$filesize_old = $q->fetchSingle();
$filesize_new = filesize($filename);
// not in db?
if ($filesize_old === FALSE) {
$isChanged = CHG_ADDED;
}
else if (filesize($filename) != $filesize_old) {
$isChanged = CHG_CHANGED;
}
// update database
if ($isChanged != CHG_UNCHANGED) {
if ($isChanged == CHG_ADDED) {
$db->queryExec("INSERT INTO tbl_files VALUES ('$filename', " . filesize($filename) . " )");
}
else if ($isChanged == CHG_CHANGED) {
$db->queryExec("UPDATE tbl_files SET f_size = " . filesize($filename) . " WHERE f_name = '$filename'");
}
}
if ($isChanged != CHG_UNCHANGED) {
if (!empty($_GET['web'])) echo "$filename -- $isChanged (was:" . $filesize_old . '/is:' . $filesize_new . ")<br>";
return "$filename -- $isChanged (was:" . $filesize_old . '/is:' . $filesize_new . ")\n";
}
else
return "";
}
$changesBody = "";
function loopDir($dirname) {
global $changesBody;
$dir = opendir($dirname);
while ($file = readdir($dir)) {
if ($file != '.' && $file != '..') {
if (is_dir($dirname.'/'.$file)) {
if (CHECK_RECURSIVE) {
loopDir($dirname.'/'.$file);
}
} else {
$changesBody = $changesBody . isChanged($dirname.'/'.$file);
}
}
}
}
if (!file_exists(DBFILE)) {
createDb();
sendEmail("Database file not found! Creating.", 0);
}
$stime = microtime(true);
loopDir(CHECK_PATH);
sendEmail($changesBody, microtime(true) - $stime);
echo "\nDONE";
?>
Comment