Merge branch 'MDL-25863_automated_backups_wip' of git://github.com/stronk7/moodle
[moodle.git] / admin / handlevirus.php
blob0ddfeaa2258a7e14dd94c8b638eec4099b348cff
1 <?php
2 /** This expects the output from a command like
3 * clamscan -r --infected --no-summary <files> 2>&1 | php -d error_log=/path/to/log thisfile.php
4 * 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
5 * 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.
6 * php -d error_log=/path/to/log thisfile.php will override the default error log for php cli, which is stderr, so if you want this script to just print stuff out, use php thisfile.php instead.
7 */
9 die('TODO: MDL-19380');
11 $fd = fopen('php://stdin','r');
12 if (!$fd) {
13 exit();
16 require_once(dirname(dirname(__FILE__)).'/config.php');
17 require_once($CFG->libdir.'/eventslib.php');
18 require_once($CFG->dirroot.'/lib/uploadlib.php'); // contains virus handling stuff.
20 $site = get_site();
22 while(!feof($fd)) {
23 $entry = fgets($fd);
24 if (strlen(trim($entry)) == 0) {
25 continue;
27 if (!$file = validate_line($entry)) {
28 continue;
30 $bits = explode('/',$file);
31 $a->filename = $bits[count($bits)-1];
33 if (!$log = $DB->get_record("log", array("module"=>"upload", "info"=>$file, "action"=>"upload"))) {
34 $a->action = clam_handle_infected_file($file,0,false);
35 clam_replace_infected_file($file);
36 notify_admins_unknown($file,$a);
37 continue;
39 $action = clam_handle_infected_file($file,$log->userid,true);
40 clam_replace_infected_file($file);
42 $user = $DB->get_record("user", array("id"=>$log->userid));
43 $course = $DB->get_record("course", array("id"=>$log->course));
44 $subject = get_string('virusfoundsubject','moodle',format_string($site->fullname));
45 $a->date = userdate($log->time);
47 $a->action = $action;
48 $a->course = $course->fullname;
49 $a->user = fullname($user);
51 notify_user($user,$subject,$a);
52 notify_admins($user,$subject,$a);
54 fclose($fd);
57 function notify_user($user,$subject,$a) {
59 if (!$user) {
60 return false;
62 $body = get_string('virusfoundlater','moodle',$a);
64 $eventdata = new stdClass();
65 $eventdata->modulename = 'moodle';
66 $eventdata->userfrom = get_admin();
67 $eventdata->userto = $user;
68 $eventdata->subject = $subject;
69 $eventdata->fullmessage = $body;
70 $eventdata->fullmessageformat = FORMAT_PLAIN;
71 $eventdata->fullmessagehtml = '';
72 $eventdata->smallmessage = '';
73 message_send($eventdata);
77 function notify_admins($user,$subject,$a) {
79 $admins = get_admins();
81 $body = get_string('virusfoundlateradmin','moodle',$a);
82 foreach ($admins as $admin) {
83 $eventdata = new stdClass();
84 $eventdata->modulename = 'moodle';
85 $eventdata->userfrom = $admin;
86 $eventdata->userto = $admin;
87 $eventdata->subject = $subject;
88 $eventdata->fullmessage = $body;
89 $eventdata->fullmessageformat = FORMAT_PLAIN;
90 $eventdata->fullmessagehtml = '';
91 $eventdata->smallmessage = '';
92 message_send($eventdata);
96 function notify_admins_unknown($file,$a) {
98 global $site;
100 $admins = get_admins();
101 $subject = get_string('virusfoundsubject','moodle',format_string($site->fullname));
102 $body = get_string('virusfoundlateradminnolog','moodle',$a);
103 foreach ($admins as $admin) {
104 $eventdata = new stdClass();
105 $eventdata->modulename = 'moodle';
106 $eventdata->userfrom = $admin;
107 $eventdata->userto = $admin;
108 $eventdata->subject = $subject;
109 $eventdata->fullmessage = $body;
110 $eventdata->fullmessageformat = FORMAT_PLAIN;
111 $eventdata->fullmessagehtml = '';
112 $eventdata->smallmessage = '';
113 message_send($eventdata);
117 function validate_line($line) {
118 global $CFG;
119 if (strpos($line,"FOUND") === false) {
120 return false;
122 $index = strpos($line,":");
123 $file = substr($line,0,$index);
124 if (!(strpos($file,$CFG->dataroot) === false)) {
125 if (!file_exists($file)) {
126 return false;
129 else {
130 if ($file{0} == "/") {
131 $file = $CFG->dataroot.$file;
133 else {
134 $file = $CFG->dataroot."/".$file;
136 if (!file_exists($file)) {
137 return false;
140 // clean up
141 $file = preg_replace('/\.\//','/',$file);
142 $file = preg_replace('/\/\//','/',$file);
143 return $file;