Savoir quand un fichier sur votre serveur de changements
- camperjohn
- Guru


- Inscription: Nov 28, 2004
- Messages: 1127
- Loc: San Diego
- Status: Offline
J'ai eu l'autre jour, quelqu'un essayait d'entrer dans mon site. Juste au pied levé, j'ai écrit ce script pour alerter, espérons-moi, s'il obtient réellement po
Il prend un instantané de mon serveur, et les emails moi si ya des nouveaux fichiers, ou si l'un des fichiers existants changement. Il me permet de surveiller ce que je veux, exclure encore de login à dossier que je connais va changer.
Alors, quand je travaille sur le site, je reçois un courriel de toutes les heures des choses que les changements sur le serveur. J'espère que si ce gars fait entrer, mai je reçois un mail à 10pm pendant que je suis à une fête, en me disant qu'un fichier a changé et je dois prendre des mesures.
J'ai ceci sur une tâche cron toutes les 15 minutes.
// Sanity test on server files - email self when something changes
sanity_test_folders();
//
// Snapshot sanity test of server
//
function sanity_test_folders()
{
// Load up old filelist snapshot
$snn = '/home/yoursite/include/cache/snapshot-latest.txt';
$f = @file_get_contents($snn);
// Create new filelist snapshot, '.*\.html$'
$a = directory_to_array("/home/yoursite/include",true,array('^/home/yoursite/include/cache$','^/home/yoursite/include/apps/yoursite/logs$'));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html",false,array('^/home/yoursite/public_html/cl_cache$')));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/images",true));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/ads",true));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/cron",true,array("^/home/yoursite/public_html/cron/cache$")));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/captcha",true));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/bugs",true));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/uploads",true));
$a = array_merge($a,directory_to_array("/home/photof/public_html",true,array('/home/photof/public_html/cache','.*\.jpg$','.*\.nfo$','.*\.css$','.*\.rss$','.*\.txt$','.*\.gif$','.*\.ico$')));
// Save out latest snapshot
@unlink($snn);
logprintf($snn,serialize($a));
// Compare old and new
if ($f != '')
{
$diff = sanity_filelist_cmp($a,unserialize($f));
$t = '';
// Construct email of everything that changed and email to john
if (is_array($diff['newfiles']))
{
$t .= "<h2>New Files</h2><br /><table class=\"cp\"><tr><td><b>Filename</b></td><td><b>File Size</b></td><td><b>File Date</b></td></tr>";
foreach ($diff['newfiles'] as $k => $l)
{
$t .= "<tr><td>$k</td><td>{$l['filesize']}</td><td>{$l['filetime']}</td><br />";
}
$t .= "</table>";
}
if (is_array($diff['olddeleted']))
{
$t .= "<h2>Deleted Files</h2><br /><table class=\"cp\"><tr><td><b>Filename</b></td><td><b>File Size</b></td><td><b>File Date</b></td></tr>";
foreach ($diff['olddeleted'] as $k => $l)
{
$t .= "<tr><td>$k</td><td>{$l['filesize']}</td><td>{$l['filetime']}</td><br />";
}
$t .= "</table>";
}
if (is_array($diff['changed']))
{
$t .= "<h2>Changed Files</h2><br /><table class=\"cp\"><tr><td><b>Filename</b></td><td><b>Original Size</b></td><td><b>New Size</b></td><td><b>Last Date</b></td><td><b>New Date</b></td></tr>";
foreach ($diff['changed'] as $k => $l)
{
$t .= "<tr><td>$k</td><td>{$l['originalfilesize']}</td><td>{$l['filesize']}</td><td>{$l['originalfiledate']}</td><td>{$l['filedate']}</td></tr>";
}
$t .= "</table>";
}
if ($t != '')
{
// Save out archived snapshot
$snnarchive = '/home/yoursite/include/cache/snapshot-' . date("m.d.y.g.ia") . '.txt';
logprintf($snnarchive,serialize($a));
// Mail self harcoded
include (INCLUDE_PATH . '/lib/class.mailer.php');
mm_sendemail("SERVER FILES CHANGED!","john@mccarthy.net","SERVER FILES CHANGED!","john@mccarthy.net","SERVER FILES CHANGED!","<html><head><style>.cp tr td { padding:2px; }</style></head><body>$t</body></html>",$t);
echo "<style>.cp tr td { padding:2px; }</style>" . $t;
}
}
}
// This is the meat of the function. Compare old and new snapshots
function sanity_filelist_cmp($a,$b)
{
$ak = array_keys($a);
$bk = array_keys($b);
// Check for changed files
foreach ($a as $k => $l)
{
if (($b[$k]['filesize'] != $a[$k]['filesize']) || ($b[$k]['filedate'] != $a[$k]['filedate']))
{
$changed[$k] = array_merge($l,array('originalfilesize' => $b[$k]['filesize'],'originalfiledate' => $b[$k]['filedate']));
}
}
// Check for new files or deleted files
foreach ($a as $k => $l)
{
if (!in_array($k,$bk))
{
// New file, old doesn't exist
$newfiles[$k] = $l;
// Don't duplicate files that are new as they show up in changed list also
unset($changed[$k]);
}
}
foreach ($b as $k => $l)
{
if (!in_array($k,$ak))
{
// Old file deleted, new doesn't exist
$olddeleted[$k] = $l;
// Don't duplicate files that are new as they show up in changed list also
unset($changed[$k]);
}
}
$tr['newfiles'] = $newfiles;
$tr['olddeleted'] = $olddeleted;
$tr['changed'] = $changed;
return $tr;
}
// Get all files, sizes and timestamps of a folder. Exclude regular expression patterns on folders or files (for log file folders)
function directory_to_array($directory, $recursive, $patterns = '')
{
if (is_array($patterns)) { $patterns = '!' . implode('|',$patterns) . '!'; }
$array_items = array();
if ($handle = opendir($directory))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$file = $directory . "/" . $file;
if (($patterns == '') || (!@preg_match($patterns,$file)))
{
if (is_dir($file))
{
if ($recursive)
{
$array_items = array_merge($array_items, directory_to_array($file, $recursive, $patterns));
}
}
$filedate = date ("F d Y g:ia",filemtime($file));
$filesize = filesize($file);
$filename = preg_replace("/\/\//si", "/", $file);
$array_items[$filename] = array('filedate' => $filedate,'filesize' => $filesize);
}
}
}
closedir($handle);
}
return $array_items;
}
Il prend un instantané de mon serveur, et les emails moi si ya des nouveaux fichiers, ou si l'un des fichiers existants changement. Il me permet de surveiller ce que je veux, exclure encore de login à dossier que je connais va changer.
Alors, quand je travaille sur le site, je reçois un courriel de toutes les heures des choses que les changements sur le serveur. J'espère que si ce gars fait entrer, mai je reçois un mail à 10pm pendant que je suis à une fête, en me disant qu'un fichier a changé et je dois prendre des mesures.
J'ai ceci sur une tâche cron toutes les 15 minutes.
PHP Code: [ Select ]
// Sanity test on server files - email self when something changes
sanity_test_folders();
//
// Snapshot sanity test of server
//
function sanity_test_folders()
{
// Load up old filelist snapshot
$snn = '/home/yoursite/include/cache/snapshot-latest.txt';
$f = @file_get_contents($snn);
// Create new filelist snapshot, '.*\.html$'
$a = directory_to_array("/home/yoursite/include",true,array('^/home/yoursite/include/cache$','^/home/yoursite/include/apps/yoursite/logs$'));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html",false,array('^/home/yoursite/public_html/cl_cache$')));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/images",true));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/ads",true));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/cron",true,array("^/home/yoursite/public_html/cron/cache$")));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/captcha",true));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/bugs",true));
$a = array_merge($a,directory_to_array("/home/yoursite/public_html/uploads",true));
$a = array_merge($a,directory_to_array("/home/photof/public_html",true,array('/home/photof/public_html/cache','.*\.jpg$','.*\.nfo$','.*\.css$','.*\.rss$','.*\.txt$','.*\.gif$','.*\.ico$')));
// Save out latest snapshot
@unlink($snn);
logprintf($snn,serialize($a));
// Compare old and new
if ($f != '')
{
$diff = sanity_filelist_cmp($a,unserialize($f));
$t = '';
// Construct email of everything that changed and email to john
if (is_array($diff['newfiles']))
{
$t .= "<h2>New Files</h2><br /><table class=\"cp\"><tr><td><b>Filename</b></td><td><b>File Size</b></td><td><b>File Date</b></td></tr>";
foreach ($diff['newfiles'] as $k => $l)
{
$t .= "<tr><td>$k</td><td>{$l['filesize']}</td><td>{$l['filetime']}</td><br />";
}
$t .= "</table>";
}
if (is_array($diff['olddeleted']))
{
$t .= "<h2>Deleted Files</h2><br /><table class=\"cp\"><tr><td><b>Filename</b></td><td><b>File Size</b></td><td><b>File Date</b></td></tr>";
foreach ($diff['olddeleted'] as $k => $l)
{
$t .= "<tr><td>$k</td><td>{$l['filesize']}</td><td>{$l['filetime']}</td><br />";
}
$t .= "</table>";
}
if (is_array($diff['changed']))
{
$t .= "<h2>Changed Files</h2><br /><table class=\"cp\"><tr><td><b>Filename</b></td><td><b>Original Size</b></td><td><b>New Size</b></td><td><b>Last Date</b></td><td><b>New Date</b></td></tr>";
foreach ($diff['changed'] as $k => $l)
{
$t .= "<tr><td>$k</td><td>{$l['originalfilesize']}</td><td>{$l['filesize']}</td><td>{$l['originalfiledate']}</td><td>{$l['filedate']}</td></tr>";
}
$t .= "</table>";
}
if ($t != '')
{
// Save out archived snapshot
$snnarchive = '/home/yoursite/include/cache/snapshot-' . date("m.d.y.g.ia") . '.txt';
logprintf($snnarchive,serialize($a));
// Mail self harcoded
include (INCLUDE_PATH . '/lib/class.mailer.php');
mm_sendemail("SERVER FILES CHANGED!","john@mccarthy.net","SERVER FILES CHANGED!","john@mccarthy.net","SERVER FILES CHANGED!","<html><head><style>.cp tr td { padding:2px; }</style></head><body>$t</body></html>",$t);
echo "<style>.cp tr td { padding:2px; }</style>" . $t;
}
}
}
// This is the meat of the function. Compare old and new snapshots
function sanity_filelist_cmp($a,$b)
{
$ak = array_keys($a);
$bk = array_keys($b);
// Check for changed files
foreach ($a as $k => $l)
{
if (($b[$k]['filesize'] != $a[$k]['filesize']) || ($b[$k]['filedate'] != $a[$k]['filedate']))
{
$changed[$k] = array_merge($l,array('originalfilesize' => $b[$k]['filesize'],'originalfiledate' => $b[$k]['filedate']));
}
}
// Check for new files or deleted files
foreach ($a as $k => $l)
{
if (!in_array($k,$bk))
{
// New file, old doesn't exist
$newfiles[$k] = $l;
// Don't duplicate files that are new as they show up in changed list also
unset($changed[$k]);
}
}
foreach ($b as $k => $l)
{
if (!in_array($k,$ak))
{
// Old file deleted, new doesn't exist
$olddeleted[$k] = $l;
// Don't duplicate files that are new as they show up in changed list also
unset($changed[$k]);
}
}
$tr['newfiles'] = $newfiles;
$tr['olddeleted'] = $olddeleted;
$tr['changed'] = $changed;
return $tr;
}
// Get all files, sizes and timestamps of a folder. Exclude regular expression patterns on folders or files (for log file folders)
function directory_to_array($directory, $recursive, $patterns = '')
{
if (is_array($patterns)) { $patterns = '!' . implode('|',$patterns) . '!'; }
$array_items = array();
if ($handle = opendir($directory))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$file = $directory . "/" . $file;
if (($patterns == '') || (!@preg_match($patterns,$file)))
{
if (is_dir($file))
{
if ($recursive)
{
$array_items = array_merge($array_items, directory_to_array($file, $recursive, $patterns));
}
}
$filedate = date ("F d Y g:ia",filemtime($file));
$filesize = filesize($file);
$filename = preg_replace("/\/\//si", "/", $file);
$array_items[$filename] = array('filedate' => $filedate,'filesize' => $filesize);
}
}
}
closedir($handle);
}
return $array_items;
}
- // Sanity test on server files - email self when something changes
- sanity_test_folders();
- //
- // Snapshot sanity test of server
- //
- function sanity_test_folders()
- {
- // Load up old filelist snapshot
- $snn = '/home/yoursite/include/cache/snapshot-latest.txt';
- $f = @file_get_contents($snn);
- // Create new filelist snapshot, '.*\.html$'
- $a = directory_to_array("/home/yoursite/include",true,array('^/home/yoursite/include/cache$','^/home/yoursite/include/apps/yoursite/logs$'));
- $a = array_merge($a,directory_to_array("/home/yoursite/public_html",false,array('^/home/yoursite/public_html/cl_cache$')));
- $a = array_merge($a,directory_to_array("/home/yoursite/public_html/images",true));
- $a = array_merge($a,directory_to_array("/home/yoursite/public_html/ads",true));
- $a = array_merge($a,directory_to_array("/home/yoursite/public_html/cron",true,array("^/home/yoursite/public_html/cron/cache$")));
- $a = array_merge($a,directory_to_array("/home/yoursite/public_html/captcha",true));
- $a = array_merge($a,directory_to_array("/home/yoursite/public_html/bugs",true));
- $a = array_merge($a,directory_to_array("/home/yoursite/public_html/uploads",true));
- $a = array_merge($a,directory_to_array("/home/photof/public_html",true,array('/home/photof/public_html/cache','.*\.jpg$','.*\.nfo$','.*\.css$','.*\.rss$','.*\.txt$','.*\.gif$','.*\.ico$')));
- // Save out latest snapshot
- @unlink($snn);
- logprintf($snn,serialize($a));
- // Compare old and new
- if ($f != '')
- {
- $diff = sanity_filelist_cmp($a,unserialize($f));
- $t = '';
- // Construct email of everything that changed and email to john
- if (is_array($diff['newfiles']))
- {
- $t .= "<h2>New Files</h2><br /><table class=\"cp\"><tr><td><b>Filename</b></td><td><b>File Size</b></td><td><b>File Date</b></td></tr>";
- foreach ($diff['newfiles'] as $k => $l)
- {
- $t .= "<tr><td>$k</td><td>{$l['filesize']}</td><td>{$l['filetime']}</td><br />";
- }
- $t .= "</table>";
- }
- if (is_array($diff['olddeleted']))
- {
- $t .= "<h2>Deleted Files</h2><br /><table class=\"cp\"><tr><td><b>Filename</b></td><td><b>File Size</b></td><td><b>File Date</b></td></tr>";
- foreach ($diff['olddeleted'] as $k => $l)
- {
- $t .= "<tr><td>$k</td><td>{$l['filesize']}</td><td>{$l['filetime']}</td><br />";
- }
- $t .= "</table>";
- }
- if (is_array($diff['changed']))
- {
- $t .= "<h2>Changed Files</h2><br /><table class=\"cp\"><tr><td><b>Filename</b></td><td><b>Original Size</b></td><td><b>New Size</b></td><td><b>Last Date</b></td><td><b>New Date</b></td></tr>";
- foreach ($diff['changed'] as $k => $l)
- {
- $t .= "<tr><td>$k</td><td>{$l['originalfilesize']}</td><td>{$l['filesize']}</td><td>{$l['originalfiledate']}</td><td>{$l['filedate']}</td></tr>";
- }
- $t .= "</table>";
- }
- if ($t != '')
- {
- // Save out archived snapshot
- $snnarchive = '/home/yoursite/include/cache/snapshot-' . date("m.d.y.g.ia") . '.txt';
- logprintf($snnarchive,serialize($a));
- // Mail self harcoded
- include (INCLUDE_PATH . '/lib/class.mailer.php');
- mm_sendemail("SERVER FILES CHANGED!","john@mccarthy.net","SERVER FILES CHANGED!","john@mccarthy.net","SERVER FILES CHANGED!","<html><head><style>.cp tr td { padding:2px; }</style></head><body>$t</body></html>",$t);
- echo "<style>.cp tr td { padding:2px; }</style>" . $t;
- }
- }
- }
- // This is the meat of the function. Compare old and new snapshots
- function sanity_filelist_cmp($a,$b)
- {
- $ak = array_keys($a);
- $bk = array_keys($b);
- // Check for changed files
- foreach ($a as $k => $l)
- {
- if (($b[$k]['filesize'] != $a[$k]['filesize']) || ($b[$k]['filedate'] != $a[$k]['filedate']))
- {
- $changed[$k] = array_merge($l,array('originalfilesize' => $b[$k]['filesize'],'originalfiledate' => $b[$k]['filedate']));
- }
- }
- // Check for new files or deleted files
- foreach ($a as $k => $l)
- {
- if (!in_array($k,$bk))
- {
- // New file, old doesn't exist
- $newfiles[$k] = $l;
- // Don't duplicate files that are new as they show up in changed list also
- unset($changed[$k]);
- }
- }
- foreach ($b as $k => $l)
- {
- if (!in_array($k,$ak))
- {
- // Old file deleted, new doesn't exist
- $olddeleted[$k] = $l;
- // Don't duplicate files that are new as they show up in changed list also
- unset($changed[$k]);
- }
- }
- $tr['newfiles'] = $newfiles;
- $tr['olddeleted'] = $olddeleted;
- $tr['changed'] = $changed;
- return $tr;
- }
- // Get all files, sizes and timestamps of a folder. Exclude regular expression patterns on folders or files (for log file folders)
- function directory_to_array($directory, $recursive, $patterns = '')
- {
- if (is_array($patterns)) { $patterns = '!' . implode('|',$patterns) . '!'; }
- $array_items = array();
- if ($handle = opendir($directory))
- {
- while (false !== ($file = readdir($handle)))
- {
- if ($file != "." && $file != "..")
- {
- $file = $directory . "/" . $file;
- if (($patterns == '') || (!@preg_match($patterns,$file)))
- {
- if (is_dir($file))
- {
- if ($recursive)
- {
- $array_items = array_merge($array_items, directory_to_array($file, $recursive, $patterns));
- }
- }
- $filedate = date ("F d Y g:ia",filemtime($file));
- $filesize = filesize($file);
- $filename = preg_replace("/\/\//si", "/", $file);
- $array_items[$filename] = array('filedate' => $filedate,'filesize' => $filesize);
- }
- }
- }
- closedir($handle);
- }
- return $array_items;
- }
Upload video and picture galleries at http://www.bodydot.com?post+upload+video+picture+gallery
- Anonymous
- Bot


- Inscription: 25 Feb 2008
- Messages: ?
- Loc: Ozzuland
- Status: Online
Juin 3rd, 2009, 2:08 pm
Page 1 sur 1
Pour répondre à ce sujet, vous devez vous connecter ou vous enregistrer. Il est gratuit.
Afficher de l'information
- Total des messages de ce sujet: 1 message
- Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 2 invités
- Vous ne pouvez pas poster de nouveaux sujets
- Vous ne pouvez pas répondre aux sujets
- Vous ne pouvez pas éditer vos messages
- Vous ne pouvez pas supprimer vos messages
- Vous ne pouvez pas joindre des fichiers
