<?
/** This expects the output from a command like
- * clamscan -r --infected --no-summary <files> 2>&1 | php thisfile.php
+ * clamscan -r --infected --no-summary <files> 2>&1 | php -d error_log=/path/to/log thisfile.php
* also it's important that the output of clamscan prints the FULL PATH to each infected file, so use absolute paths for area to scan
* also it should be run as root, or whatever the webserver runs as so that it has the right permissions in the quarantine dir etc.
*/
$bits = explode('/',$file);
$a->filename = $bits[count($bits)-1];
- if (!$log = get_record("log","module","upload","info",$file)) {
+ if (!$log = get_record("log","module","upload","info",$file,"action","upload")) {
$a->action = clam_handle_infected_file($file,0,false);
clam_replace_infected_file($file);
notify_admins_unknown($file,$a);
$now = date('YmdHis');
if (rename($file,$CFG->quarantinedir.'/'.$now.'-user-'.$userid.'-infected')) {
$delete = false;
+ clam_log_infected($file,$CFG->quarantinedir.'/'.$now.'-user-'.$userid.'-infected',$userid);
if ($basiconly) {
$notice .= "\n".get_string('clammovedfilebasic');
}
}
if ($delete) {
if (unlink($file)) {
+ clam_log_infected($file,'',$userid);
$notice .= "\n".get_string('clamdeletedfile');
}
else {
if (strpos($newfilepath,$CFG->dataroot) === false) {
$newfilepath = $CFG->dataroot.'/'.$newfilepath;
}
- $CFG->debug=10;
$courseid = 0;
if ($course) {
$courseid = $course->id;
add_to_log($courseid,"upload","upload","",$newfilepath);
}
+/**
+ * This function logs to error_log and to the log table that an infected file has been found and what's happened to it.
+ * @param $oldfilepath - full path to the infected file before it was moved.
+ * @param $newfilepath - full path to the infected file since it was moved to the quarantine directory (if the file was deleted, leave empty).
+ * @param $userid - id of user who uploaded the file.
+ */
+function clam_log_infected($oldfilepath='',$newfilepath='',$userid=0) {
+
+ add_to_log(0,"upload","infected","",$oldfilepath,0,$userid);
+
+ $user = get_record('user','id',$userid);
+
+ $errorstr = 'Clam AV has found a file that is infected with a virus. It was uploaded by '
+ . ((empty($user) ? ' an unknown user ' : $user->firstname. ' '.$user->lastname))
+ . ((empty($oldfilepath)) ? '. The infected file was caught on upload ('.$oldfilepath.')'
+ : '. The original file path of the infected file was '.$oldfilepath)
+ . ((empty($newfilepath)) ? '. The file has been deleted ' : '. The file has been moved to a quarantine directory and the new path is '.$newfilepath);
+
+ error_log($errorstr);
+}
+
+
/**
* some of the modules allow moving attachments (glossary), in which case we need to hunt down an original log and change the path.
*/