diff --git a/admin/settings/plugins.php b/admin/settings/plugins.php
index ca99ec81c67..f10f1b631f9 100644
--- a/admin/settings/plugins.php
+++ b/admin/settings/plugins.php
@@ -172,22 +172,35 @@ if ($hassiteconfig) {
$temp->add(new admin_setting_heading('antiviruscommonsettings', new lang_string('antiviruscommonsettings', 'antivirus'), ''));
// Alert email.
- $temp->add(new admin_setting_configtext('antivirus/notifyemail',
+ $temp->add(
+ new admin_setting_configtext(
+ 'antivirus/notifyemail',
new lang_string('notifyemail', 'antivirus'),
- new lang_string('notifyemail_help', 'antivirus'), '', PARAM_EMAIL)
+ new lang_string('notifyemail_help', 'antivirus'),
+ '',
+ PARAM_EMAIL
+ )
);
// Enable quarantine.
- $temp->add(new admin_setting_configcheckbox('antivirus/enablequarantine',
- new lang_string('enablequarantine', 'antivirus'),
- new lang_string('enablequarantine_help', 'antivirus',
- \core\antivirus\quarantine::DEFAULT_QUARANTINE_FOLDER), 0));
+ $temp->add(
+ new admin_setting_configcheckbox(
+ 'antivirus/enablequarantine',
+ new lang_string('enablequarantine', 'antivirus'),
+ new lang_string('enablequarantine_help', 'antivirus',
+ \core\antivirus\quarantine::DEFAULT_QUARANTINE_FOLDER),
+ 0
+ )
+ );
// Quarantine time.
- $temp->add(new admin_setting_configduration('antivirus/quarantinetime',
+ $temp->add(
+ new admin_setting_configduration(
+ 'antivirus/quarantinetime',
new lang_string('quarantinetime', 'antivirus'),
new lang_string('quarantinetime_desc', 'antivirus'),
- \core\antivirus\quarantine::DEFAULT_QUARANTINE_TIME)
+ \core\antivirus\quarantine::DEFAULT_QUARANTINE_TIME
+ )
);
$ADMIN->add('antivirussettings', $temp);
diff --git a/lang/en/antivirus.php b/lang/en/antivirus.php
index 170d15f8eb7..907bf31f55d 100644
--- a/lang/en/antivirus.php
+++ b/lang/en/antivirus.php
@@ -27,32 +27,35 @@ $string['antiviruses'] = 'Antivirus plugins';
$string['antiviruscommonsettings'] = 'Common antivirus settings';
$string['antivirussettings'] = 'Manage antivirus plugins';
$string['configantivirusplugins'] = 'Please choose the antivirus plugins you wish to use and arrange them in order of being applied.';
-$string['confirmdelete'] = 'Do you really want to delete this file';
-$string['confirmdeleteall'] = 'Do you really want to delete all files';
$string['datastream'] = 'Data';
-$string['datainfecteddesc'] = 'There is a virus infected data';
+$string['datainfecteddesc'] = 'Infected data was detected.';
$string['datainfectedname'] = 'Data infected';
+$string['emailadditionalinfo'] = 'Additional details returned from the virus engine: ';
+$string['emailauthor'] = 'Uploaded by: ';
+$string['emailcontenthash'] = 'Content hash: ';
+$string['emailcontenttype'] = 'Content type: ';
+$string['emaildate'] = 'Date uploaded: ';
+$string['emailfilename'] = 'Filename: ';
+$string['emailfilesize'] = 'File size: ';
+$string['emailgeoinfo'] = 'Geolocation: ';
+$string['emailinfectedfiledetected'] = 'Infected file detected';
+$string['emailipaddress'] = 'IP Address: ';
+$string['emailreferer'] = 'Referer: ';
+$string['emailreport'] = 'Report: ';
+$string['emailscanner'] = 'Scanner: ';
+$string['emailscannererrordetected'] = 'A scanner error occured';
$string['emailsubject'] = '{$a} :: Antivirus notification';
$string['enablequarantine'] = 'Enable quarantine';
$string['enablequarantine_help'] = 'When quarantine is enabled, any files which are detected as viruses will be kept in a quarantine folder for later inspection ([dataroot]/{$a}).
The upload into Moodle will still fail.
If you have any file system level virus scanning in place, the quarantine folder should be excluded from the antivirus check to avoid detecting the quarantined files.';
+$string['fileinfecteddesc'] = 'An infected file was detected.';
$string['fileinfectedname'] = 'File infected';
-$string['incidencedetails'] = 'Infected file detected:
-Report: {$a->report}
-File name: {$a->filename}
-File size: {$a->filesize}
-File content hash: {$a->contenthash}
-File content type: {$a->contenttype}
-Uploaded by: {$a->author}
-IP: {$a->ipaddress}
-REFERER: {$a->referer}
-Date: {$a->date}
-{$a->notice}';
$string['notifyemail'] = 'Antivirus alert email';
$string['notifyemail_help'] = 'If set, then only the specified email will be notified when a virus is detected.
If blank, then all site admins will be notified by email when a virus is detected.';
$string['privacy:metadata'] = 'The Antivirus system does not store any personal data.';
+$string['quarantinedisabled'] = 'Quarantine disabled, file not stored.';
$string['quarantinedfiles'] = 'Antivirus quarantined files';
$string['quarantinetime'] = 'Maximum quarantine time';
$string['quarantinetime_desc'] = 'Quarantined files older than specified period will be removed.';
diff --git a/lang/en/moodle.php b/lang/en/moodle.php
index 24d2c03f974..9d7065e0244 100644
--- a/lang/en/moodle.php
+++ b/lang/en/moodle.php
@@ -1240,6 +1240,7 @@ $string['messageprovider:gradenotifications'] = 'Grade notifications';
$string['messageprovider:messagecontactrequests'] = 'Message contact requests notification';
$string['messageprovider:notices'] = 'Notices about minor problems';
$string['messageprovider:notices_help'] = 'These are notices that an administrator might be interested in seeing.';
+$string['messageprovider:infected'] = 'Antivirus failure notifications.';
$string['messageprovider:insights'] = 'Insights generated by prediction models';
$string['messageprovider:instantmessage'] = 'Personal messages between users';
$string['messageprovider:instantmessage_help'] = 'This section configures what happens to messages that are sent to you directly from other users on this site.';
diff --git a/lib/classes/antivirus/manager.php b/lib/classes/antivirus/manager.php
index 780e12a97be..d17c42e0760 100644
--- a/lib/classes/antivirus/manager.php
+++ b/lib/classes/antivirus/manager.php
@@ -70,29 +70,47 @@ class manager {
global $USER;
$antiviruses = self::get_enabled();
foreach ($antiviruses as $antivirus) {
- $result = $antivirus->scan_file($file, $filename);
+ // Attempt to scan, catching internal exceptions.
+ try {
+ $result = $antivirus->scan_file($file, $filename);
+ } catch (\core\antivirus\scanner_exception $e) {
+ // If there was a scanner exception (such as ClamAV denying upload), send messages and rethrow.
+ $notice = $antivirus->get_scanning_notice();
+ $incidentdetails = $antivirus->get_incident_details($file, $filename, $notice, false);
+ self::send_antivirus_messages($antivirus, $incidentdetails);
+ throw $e;
+ }
+
+ $notice = $antivirus->get_scanning_notice();
if ($result === $antivirus::SCAN_RESULT_FOUND) {
// Infection found, send notification.
- $notice = $antivirus->get_scanning_notice();
- $incidencedetails = $antivirus->get_incidence_details($file, $filename, $notice);
- $antivirus->message_admins($notice, FORMAT_MOODLE, 'infected');
+ $incidentdetails = $antivirus->get_incident_details($file, $filename, $notice);
+ self::send_antivirus_messages($antivirus, $incidentdetails);
// Move to quarantine folder.
- $zipfile = \core\antivirus\quarantine::quarantine_file($file, $filename, $incidencedetails, $notice);
+ $zipfile = \core\antivirus\quarantine::quarantine_file($file, $filename, $incidentdetails, $notice);
+ // If file not stored due to disabled quarantine, store a message.
+ if (empty($zipfile)) {
+ $zipfile = get_string('quarantinedisabled', 'antivirus');
+ }
// Log file infected event.
- $params = array(
+ $params = [
'context' => \context_system::instance(),
'relateduserid' => $USER->id,
- 'other' => ['filename' => $filename, 'zipfile' => $zipfile, 'incidencedetails' => $incidencedetails],
- );
- $event = \core\event\antivirus_file_infected::create($params);
+ 'other' => ['filename' => $filename, 'zipfile' => $zipfile, 'incidentdetails' => $incidentdetails],
+ ];
+ $event = \core\event\virus_infected_file_detected::create($params);
$event->trigger();
if ($deleteinfected) {
unlink($file);
}
throw new \core\antivirus\scanner_exception('virusfound', '', array('item' => $filename));
+ } else if ($result === $antivirus::SCAN_RESULT_ERROR) {
+ // Here we need to generate a different incident based on an error.
+ $incidentdetails = $antivirus->get_incident_details($file, $filename, $notice, false);
+ self::send_antivirus_messages($antivirus, $incidentdetails);
}
}
}
@@ -108,27 +126,48 @@ class manager {
global $USER;
$antiviruses = self::get_enabled();
foreach ($antiviruses as $antivirus) {
- $result = $antivirus->scan_data($data);
+ // Attempt to scan, catching internal exceptions.
+ try {
+ $result = $antivirus->scan_data($data);
+ } catch (\core\antivirus\scanner_exception $e) {
+ // If there was a scanner exception (such as ClamAV denying upload), send messages and rethrow.
+ $notice = $antivirus->get_scanning_notice();
+ $filename = get_string('datastream', 'antivirus');
+ $incidentdetails = $antivirus->get_incident_details('', $filename, $notice, false);
+ self::send_antivirus_messages($antivirus, $incidentdetails);
+
+ throw $e;
+ }
+
+ $filename = get_string('datastream', 'antivirus');
+ $notice = $antivirus->get_scanning_notice();
+
if ($result === $antivirus::SCAN_RESULT_FOUND) {
// Infection found, send notification.
- $filename = get_string('datastream', 'antivirus');
- $notice = $antivirus->get_scanning_notice();
- $incidencedetails = $antivirus->get_incidence_details('', $filename, $notice);
- $antivirus->message_admins($notice, FORMAT_MOODLE, 'infected');
+ $incidentdetails = $antivirus->get_incident_details('', $filename, $notice);
+ self::send_antivirus_messages($antivirus, $incidentdetails);
// Copy data to quarantine folder.
- $zipfile = \core\antivirus\quarantine::quarantine_data($data, $filename, $incidencedetails, $notice);
+ $zipfile = \core\antivirus\quarantine::quarantine_data($data, $filename, $incidentdetails, $notice);
+ // If file not stored due to disabled quarantine, store a message.
+ if (empty($zipfile)) {
+ $zipfile = get_string('quarantinedisabled', 'antivirus');
+ }
// Log file infected event.
- $params = array(
+ $params = [
'context' => \context_system::instance(),
'relateduserid' => $USER->id,
- 'other' => ['filename' => $filename, 'zipfile' => $zipfile, 'incidencedetails' => $incidencedetails],
- );
- $event = \core\event\antivirus_data_infected::create($params);
+ 'other' => ['filename' => $filename, 'zipfile' => $zipfile, 'incidentdetails' => $incidentdetails],
+ ];
+ $event = \core\event\virus_infected_data_detected::create($params);
$event->trigger();
throw new \core\antivirus\scanner_exception('virusfound', '', array('item' => get_string('datastream', 'antivirus')));
+ } else if ($result === $antivirus::SCAN_RESULT_ERROR) {
+ // Here we need to generate a different incident based on an error.
+ $incidentdetails = $antivirus->get_incident_details('', $filename, $notice, false);
+ self::send_antivirus_messages($antivirus, $incidentdetails);
}
}
}
@@ -161,4 +200,53 @@ class manager {
}
return $antiviruses;
}
+
+ /**
+ * This function puts all relevant information into the messages required, and sends them.
+ *
+ * @param \core\antivirus\scanner $antivirus the scanner engine.
+ * @param string $incidentdetails details of the incident.
+ * @return void
+ */
+ public static function send_antivirus_messages(\core\antivirus\scanner $antivirus, string $incidentdetails) {
+ $messages = $antivirus->get_messages();
+
+ // If there is no messages, and a virus is found, we should generate one, then send it.
+ if (empty($messages)) {
+ $antivirus->message_admins($antivirus->get_scanning_notice(), FORMAT_MOODLE, 'infected');
+ $messages = $antivirus->get_messages();
+ }
+
+ foreach ($messages as $message) {
+
+ // Check if the information is already in the current scanning notice.
+ if (!empty($antivirus->get_scanning_notice()) &&
+ strpos($antivirus->get_scanning_notice(), $message->fullmessage) === false) {
+ // This is some extra information. We should append this to the end of the incident details.
+ $incidentdetails .= \html_writer::tag('pre', $message->fullmessage);
+ }
+
+ // Now update the message to the detailed version, and format.
+ $message->name = 'infected';
+ $message->fullmessagehtml = $incidentdetails;
+ $message->fullmessageformat = FORMAT_MOODLE;
+ $message->fullmessage = format_text_email($incidentdetails, $message->fullmessageformat);
+
+ // Now we must check if message is going to a real account.
+ // It may be an email that needs to be sent to non-user address.
+ if ($message->userto->id === -1) {
+ // If this doesnt exist, send a regular email.
+ email_to_user(
+ $message->userto,
+ get_admin(),
+ $message->subject,
+ $message->fullmessage,
+ $message->fullmessagehtml
+ );
+ } else {
+ // And now we can send.
+ message_send($message);
+ }
+ }
+ }
}
diff --git a/lib/classes/antivirus/quarantine.php b/lib/classes/antivirus/quarantine.php
index c8a038df593..e3136ffa8a0 100644
--- a/lib/classes/antivirus/quarantine.php
+++ b/lib/classes/antivirus/quarantine.php
@@ -47,27 +47,28 @@ class quarantine {
/** Zip all infected file */
const FILE_ZIP_ALL_INFECTED = '_all_infected_files.zip';
- /** Incidence details file */
+ /** Incident details file */
const FILE_HTML_DETAILS = '_details.html';
- /** Incidence details file */
+ /** Incident details file */
const DEFAULT_QUARANTINE_TIME = DAYSECS * 28;
/** Date format in filename */
const FILE_NAME_DATE_FORMAT = '%Y%m%d%H%M%S';
/**
- * Move the infected file to the quarantine folder
+ * Move the infected file to the quarantine folder.
*
- * @param string $file infected file
- * @param string $filename infected file name
- * @param string $incidencedetails incidence details
- * @param string $notice notice details
+ * @param string $file infected file.
+ * @param string $filename infected file name.
+ * @param string $incidentdetails incident details.
+ * @param string $notice notice details.
+ * @return string|null the name of the newly created quarantined file.
* @throws \dml_exception
*/
- public static function quarantine_file($file, $filename, $incidencedetails, $notice) {
- if (!self::is_allowed_quarantine()) {
- return;
+ public static function quarantine_file(string $file, string $filename, string $incidentdetails, string $notice) : ?string {
+ if (!self::is_quarantine_enabled()) {
+ return null;
}
// Generate file names.
$date = userdate(time(), self::FILE_NAME_DATE_FORMAT) . "_" . rand();
@@ -77,7 +78,7 @@ class quarantine {
// Create Zip file.
$ziparchive = new \zip_archive();
if ($ziparchive->open($zipfilepath, \file_archive::CREATE)) {
- $ziparchive->add_file_from_string($detailsfilename, format_text($incidencedetails, FORMAT_MOODLE));
+ $ziparchive->add_file_from_string($detailsfilename, format_text($incidentdetails, FORMAT_MOODLE));
$ziparchive->add_file_from_pathname($filename, $file);
$ziparchive->close();
}
@@ -87,17 +88,18 @@ class quarantine {
}
/**
- * Move the infected file to the quarantine folder
+ * Move the infected file to the quarantine folder.
*
- * @param string $data data which is infected
- * @param string $filename infected file name
- * @param string $incidencedetails incidence details
- * @param string $notice notice details
+ * @param string $data data which is infected.
+ * @param string $filename infected file name.
+ * @param string $incidentdetails incident details.
+ * @param string $notice notice details.
+ * @return string|null the name of the newly created quarantined file.
* @throws \dml_exception
*/
- public static function quarantine_data($data, $filename, $incidencedetails, $notice) {
- if (!self::is_allowed_quarantine()) {
- return;
+ public static function quarantine_data(string $data, string $filename, string $incidentdetails, string $notice) : ?string {
+ if (!self::is_quarantine_enabled()) {
+ return null;
}
// Generate file names.
$date = userdate(time(), self::FILE_NAME_DATE_FORMAT) . "_" . rand();
@@ -107,7 +109,7 @@ class quarantine {
// Create Zip file.
$ziparchive = new \zip_archive();
if ($ziparchive->open($zipfilepath, \file_archive::CREATE)) {
- $ziparchive->add_file_from_string($detailsfilename, format_text($incidencedetails, FORMAT_MOODLE));
+ $ziparchive->add_file_from_string($detailsfilename, format_text($incidentdetails, FORMAT_MOODLE));
$ziparchive->add_file_from_string($filename, $data);
$ziparchive->close();
}
@@ -122,7 +124,7 @@ class quarantine {
* @return bool
* @throws \dml_exception
*/
- public static function is_allowed_quarantine() {
+ public static function is_quarantine_enabled() : bool {
return !empty(get_config("antivirus", "enablequarantine"));
}
@@ -131,7 +133,7 @@ class quarantine {
*
* @return string path of quarantine folder
*/
- public static function get_quarantine_folder() {
+ private static function get_quarantine_folder() : string {
global $CFG;
$quarantinefolder = $CFG->dataroot . DIRECTORY_SEPARATOR . self::DEFAULT_QUARANTINE_FOLDER;
if (!file_exists($quarantinefolder)) {
@@ -141,43 +143,78 @@ class quarantine {
}
/**
- * Download quarantined file
+ * Checks whether a file exists inside the antivirus quarantine folder.
*
- * @param string $filename name of file to be downloaded
+ * @param string $filename the filename to check.
+ * @return boolean whether file exists.
*/
- public static function download_quarantined_file($filename) {
- $file = self::get_quarantine_folder() . $filename;
-// send_file($file, $filename);
+ public static function quarantined_file_exists(string $filename) : bool {
+ $folder = self::get_quarantine_folder();
+ return file_exists($folder . $filename);
}
/**
- * Delete quarantined file
+ * Download quarantined file.
*
- * @param string $filename name of file to be deleted
+ * @param int $fileid the id of file to be downloaded.
*/
- public static function delete_quarantined_file($filename) {
- self::delete_infected_file_record($filename);
+ public static function download_quarantined_file(int $fileid) {
+ global $DB;
+
+ // Get the filename to be downloaded.
+ $filename = $DB->get_field('infected_files', 'quarantinedfile', ['id' => $fileid], IGNORE_MISSING);
+ // If file record isnt found, user might be doing something naughty in params, or a stale request.
+ if (empty($filename)) {
+ return;
+ }
+
+ $file = self::get_quarantine_folder() . $filename;
+ send_file($file, $filename);
+ }
+
+ /**
+ * Delete quarantined file.
+ *
+ * @param int $fileid id of file to be deleted.
+ */
+ public static function delete_quarantined_file(int $fileid) {
+ global $DB;
+
+ // Get the filename to be deleted.
+ $filename = $DB->get_field('infected_files', 'quarantinedfile', ['id' => $fileid], IGNORE_MISSING);
+ // If file record isnt found, user might be doing something naughty in params, or a stale request.
+ if (empty($filename)) {
+ return;
+ }
+
+ // Delete the file from the folder.
$file = self::get_quarantine_folder() . $filename;
if (file_exists($file)) {
-// unlink($file);
+ unlink($file);
}
+
+ // Now we are finished with the record, delete the quarantine information.
+ self::delete_infected_file_record($fileid);
}
/**
- * Download all quarantined files
+ * Download all quarantined files.
*
+ * @return void
*/
public static function download_all_quarantined_files() {
$files = new \DirectoryIterator(self::get_quarantine_folder());
- // Add all infected file to a zip file.
+ // Add all infected files to a zip file.
$date = userdate(time(), self::FILE_NAME_DATE_FORMAT);
$zipfilename = $date . self::FILE_ZIP_ALL_INFECTED;
$zipfilepath = self::get_quarantine_folder() . DIRECTORY_SEPARATOR . $zipfilename;
$tempfilestocleanup = [];
+
$ziparchive = new \zip_archive();
if ($ziparchive->open($zipfilepath, \file_archive::CREATE)) {
foreach ($files as $file) {
if (!$file->isDot()) {
+ // Only send the actual files.
$filename = $file->getFilename();
$filepath = $file->getPathname();
$ziparchive->add_file_from_pathname($filename, $filepath);
@@ -185,29 +222,34 @@ class quarantine {
}
$ziparchive->close();
}
+
// Clean up temp files.
foreach ($tempfilestocleanup as $tempfile) {
if (file_exists($tempfile)) {
unlink($tempfile);
}
}
-// send_temp_file($zipfilepath, $zipfilename);
+
+ send_temp_file($zipfilepath, $zipfilename);
}
/**
- * Return array of quarantined files
+ * Return array of quarantined files.
*
- * @return array list of quarantined files
+ * @return array list of quarantined files.
*/
- public static function get_quarantined_files() {
+ public static function get_quarantined_files() : array {
$files = new \DirectoryIterator(self::get_quarantine_folder());
$filestosort = [];
+
+ // Grab all files that match the naming structure.
foreach ($files as $file) {
$filename = $file->getFilename();
if (!$file->isDot() && strpos($filename, self::FILE_ZIP_INFECTED) !== false) {
$filestosort[$filename] = $file->getPathname();
}
}
+
krsort($filestosort, SORT_NATURAL);
return $filestosort;
}
@@ -217,19 +259,37 @@ class quarantine {
*
* @param int $timetocleanup time to clean up
*/
- public static function clean_up_quarantine_folder($timetocleanup) {
+ public static function clean_up_quarantine_folder(int $timetocleanup) {
$files = new \DirectoryIterator(self::get_quarantine_folder());
// Clean up the folder.
foreach ($files as $file) {
$filename = $file->getFilename();
+
+ // Only delete files that match the correct name structure.
if (!$file->isDot() && strpos($filename, self::FILE_ZIP_INFECTED) !== false) {
$modifiedtime = $file->getMTime();
+
if ($modifiedtime <= $timetocleanup) {
unlink($file->getPathname());
- self::delete_infected_file_record($filename);
}
}
}
+
+ // Lastly cleanup the infected files table as well.
+ self::clean_up_infected_records($timetocleanup);
+ }
+
+ /**
+ * This function removes any stale records from the infected files table.
+ *
+ * @param int $timetocleanup the time to cleanup from
+ * @return void
+ */
+ private static function clean_up_infected_records(int $timetocleanup) {
+ global $DB;
+
+ $select = "timecreated <= ?";
+ $DB->delete_records_select('infected_files', $select, [$timetocleanup]);
}
/**
@@ -240,25 +300,27 @@ class quarantine {
* @param string $reason failure reason
* @throws \dml_exception
*/
- private static function create_infected_file_record($filename, $zipfile, $reason) {
+ private static function create_infected_file_record(string $filename, string $zipfile, string $reason) {
global $DB, $USER;
+
$record = new \stdClass();
$record->filename = $filename;
$record->quarantinedfile = $zipfile;
- $record->author = fullname($USER);
+ $record->userid = $USER->id;
$record->reason = $reason;
$record->timecreated = time();
+
$DB->insert_record('infected_files', $record);
}
/**
- * Delete an infected_file_record
+ * Delete the database record for an infected file.
*
- * @param string $zipfile quarantined file name
+ * @param int $fileid quarantined file id
* @throws \dml_exception
*/
- private static function delete_infected_file_record($zipfile) {
+ private static function delete_infected_file_record(int $fileid) {
global $DB;
- $DB->delete_records('infected_files', ['quarantinedfile' => $zipfile]);
+ $DB->delete_records('infected_files', ['id' => $fileid]);
}
}
diff --git a/lib/classes/antivirus/scanner.php b/lib/classes/antivirus/scanner.php
index 546b4a76e45..462684f4ab5 100644
--- a/lib/classes/antivirus/scanner.php
+++ b/lib/classes/antivirus/scanner.php
@@ -25,6 +25,7 @@
namespace core\antivirus;
defined('MOODLE_INTERNAL') || die();
+require_once(__DIR__ . '../../../../iplookup/lib.php');
/**
* Base abstract antivirus scanner class.
@@ -46,6 +47,8 @@ abstract class scanner {
protected $config;
/** @var string scanning notice */
protected $scanningnotice = '';
+ /** @var array any admin messages generated by a plugin. */
+ protected $messages = [];
/**
* Class constructor.
@@ -130,7 +133,7 @@ abstract class scanner {
}
/**
- * Email admins about antivirus scan outcomes.
+ * This function pushes given messages into the message queue, which will be sent by the antivirus manager.
*
* @param string $notice The body of the email to be sent.
* @param string $format The body format.
@@ -145,15 +148,18 @@ abstract class scanner {
$subject = get_string('emailsubject', 'antivirus', format_string($site->fullname));
$notifyemail = get_config('antivirus', 'notifyemail');
+ // If one email address is specified, construct a message to fake account.
if (!empty($notifyemail)) {
$user = new \stdClass();
$user->id = -1;
$user->email = $notifyemail;
- email_to_user($user, get_admin(), $subject, $noticehtml);
- return;
+ $user->mailformat = 1;
+ $admins = [$user];
+ } else {
+ // Otherwise, we message all admins.
+ $admins = get_admins();
}
- $admins = get_admins();
foreach ($admins as $admin) {
$eventdata = new \core\message\message();
$eventdata->courseid = SITEID;
@@ -166,29 +172,38 @@ abstract class scanner {
$eventdata->fullmessageformat = $format;
$eventdata->fullmessagehtml = $noticehtml;
$eventdata->smallmessage = '';
- message_send($eventdata);
+
+ // Now add the message to an array to be sent by the antivirus manager.
+ $this->messages[] = $eventdata;
}
}
/**
- * Return incidence details
+ * Return incident details
*
* @param string $file full path to the file
* @param string $filename original name of the file
* @param string $notice notice from antivirus
- * @return string the incidence details
+ * @param string $virus if this template is due to a virus found.
+ * @return string the incident details
* @throws \coding_exception
*/
- public function get_incidence_details($file = '', $filename = '', $notice = '') {
- global $USER;
+ public function get_incident_details($file = '', $filename = '', $notice = '', $virus = true) {
+ global $OUTPUT, $USER;
if (empty($notice)) {
$notice = $this->get_scanning_notice();
}
+ $classname = get_class($this);
+ $component = explode('\\', $classname)[0];
+
$content = new \stdClass();
- $unknown = get_string('unknown', 'antivirus');;
+ $unknown = get_string('unknown', 'antivirus');
+ $content->header = get_string('emailinfectedfiledetected', 'antivirus');
$content->filename = !empty($filename) ? $filename : $unknown;
- if (!empty($file)) {
- $content->filesize = filesize($file);
+ $content->scanner = $component;
+ // Check for empty file, or file not uploaded.
+ if (!empty($file) && filesize($file) !== false) {
+ $content->filesize = display_size(filesize($file));
$content->contenthash = \file_storage::hash_from_string(file_get_contents($file));
$content->contenttype = mime_content_type($file);
} else {
@@ -199,11 +214,28 @@ abstract class scanner {
$content->author = \core_user::is_real_user($USER->id) ? fullname($USER) . " ($USER->username)" : $unknown;
$content->ipaddress = getremoteaddr();
+ $geoinfo = iplookup_find_location(getremoteaddr());
+ $content->geoinfo = $geoinfo['city'] . ', ' . $geoinfo['country'];
$content->date = userdate(time(), get_string('strftimedatetimeshort'));
$content->referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $unknown;
$content->notice = $notice;
$report = new \moodle_url('/report/infectedfiles/index.php');
$content->report = $report->out();
- return get_string('incidencedetails', 'antivirus', $content);
+
+ // If this is not due to a virus, we need to change the header line.
+ if (!$virus) {
+ $content->header = get_string('emailscannererrordetected', 'antivirus');
+ }
+
+ return $OUTPUT->render_from_template('core/infected_file_email', $content);
+ }
+
+ /**
+ * Getter method for messages queued by the antivirus scanner.
+ *
+ * @return array
+ */
+ public function get_messages() : array {
+ return $this->messages;
}
}
diff --git a/lib/classes/event/antivirus_data_infected.php b/lib/classes/event/virus_infected_data_detected.php
similarity index 87%
rename from lib/classes/event/antivirus_data_infected.php
rename to lib/classes/event/virus_infected_data_detected.php
index 9bcfdeca0c5..66d3fe3a10e 100644
--- a/lib/classes/event/antivirus_data_infected.php
+++ b/lib/classes/event/virus_infected_data_detected.php
@@ -34,7 +34,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class antivirus_data_infected extends \core\event\base {
+class virus_infected_data_detected extends \core\event\base {
/**
* Event data
*/
@@ -50,8 +50,11 @@ class antivirus_data_infected extends \core\event\base {
* @throws \coding_exception
*/
public function get_description() {
- return isset($this->other['incidencedetails']) ?
- format_text($this->other['incidencedetails'], FORMAT_MOODLE) : 'Infected data';
+ if (isset($this->other['incidentdetails'])) {
+ return format_text($this->other['incidentdetails'], FORMAT_MOODLE);
+ } else {
+ return get_string('datainfecteddesc', 'antivirus');
+ }
}
/**
diff --git a/lib/classes/event/antivirus_file_infected.php b/lib/classes/event/virus_infected_file_detected.php
similarity index 87%
rename from lib/classes/event/antivirus_file_infected.php
rename to lib/classes/event/virus_infected_file_detected.php
index 1e2e71a280e..3acd52a93ee 100644
--- a/lib/classes/event/antivirus_file_infected.php
+++ b/lib/classes/event/virus_infected_file_detected.php
@@ -34,7 +34,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class antivirus_file_infected extends \core\event\base {
+class virus_infected_file_detected extends \core\event\base {
/**
* Event data
*/
@@ -50,8 +50,11 @@ class antivirus_file_infected extends \core\event\base {
* @throws \coding_exception
*/
public function get_description() {
- return isset($this->other['incidencedetails']) ?
- format_text($this->other['incidencedetails'], FORMAT_MOODLE) : 'Infected file';
+ if (isset($this->other['incidentdetails'])) {
+ return format_text($this->other['incidentdetails'], FORMAT_MOODLE);
+ } else {
+ return get_string('fileinfecteddesc', 'antivirus');
+ }
}
/**
diff --git a/lib/classes/plugin_manager.php b/lib/classes/plugin_manager.php
index 4d69a120480..886b442d250 100644
--- a/lib/classes/plugin_manager.php
+++ b/lib/classes/plugin_manager.php
@@ -1966,8 +1966,8 @@ class core_plugin_manager {
'report' => array(
'backups', 'competency', 'completion', 'configlog', 'courseoverview', 'eventlist',
- 'insights', 'log', 'loglive', 'outline', 'participation', 'progress', 'questioninstances',
- 'security', 'stats', 'status', 'performance', 'usersessions'
+ 'infectedfiles', 'insights', 'log', 'loglive', 'outline', 'participation', 'progress',
+ 'questioninstances', 'security', 'stats', 'status', 'performance', 'usersessions'
),
'repository' => array(
diff --git a/lib/classes/task/antivirus_cleanup_task.php b/lib/classes/task/antivirus_cleanup_task.php
index 1fc278c1a85..6a321d384b4 100644
--- a/lib/classes/task/antivirus_cleanup_task.php
+++ b/lib/classes/task/antivirus_cleanup_task.php
@@ -35,7 +35,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class antivirus_cleanup_task extends \core\task\scheduled_task {
+class antivirus_cleanup_task extends scheduled_task {
/**
* Get a descriptive name for this task.
diff --git a/lib/db/install.xml b/lib/db/install.xml
index 45070361411..8c14324e837 100644
--- a/lib/db/install.xml
+++ b/lib/db/install.xml
@@ -4275,15 +4275,19 @@
{{#str}} emailreport, antivirus {{/str}}{{report}}
+{{#str}} emailscanner, antivirus {{/str}}{{scanner}}
+{{#str}} emailfilename, antivirus {{/str}}{{filename}}
+{{#str}} emailfilesize, antivirus {{/str}}{{filesize}}
+{{#str}} emailcontenthash, antivirus {{/str}}{{contenthash}}
+{{#str}} emailcontenttype, antivirus {{/str}}{{contenttype}}
+{{#str}} emaildate, antivirus {{/str}}{{date}}
+{{#str}} emailauthor, antivirus {{/str}}{{author}}
+{{#str}} emailipaddress, antivirus {{/str}}{{ipaddress}}
+{{#str}} emailgeoinfo, antivirus {{/str}}{{geoinfo}}
+{{#str}} emailreferer, antivirus {{/str}}{{referer}}
+{{notice}}
+ {{#str}} emailadditionalinfo, antivirus {{/str}}
+