Incorrect variable name - not sure why this worked!
[moodle-linuxchix.git] / admin / handlevirus.php
blob5d4b022b5cfe9ecf547a05e5aaa98d4e8d67c0cb
1 <?
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 */
10 $fd = fopen('php://stdin','r');
11 if (!$fd) {
12 exit();
15 $FULLME='cron';
16 require_once(dirname(dirname(__FILE__)).'/config.php');
17 require_once($CFG->dirroot.'/lib/uploadlib.php'); // contains virus handling stuff.
19 $site = get_site();
21 while(!feof($fd)) {
22 $entry = fgets($fd);
23 if (strlen(trim($entry)) == 0) {
24 continue;
26 if (!$file = validate_line($entry)) {
27 continue;
29 $bits = explode('/',$file);
30 $a->filename = $bits[count($bits)-1];
32 if (!$log = get_record("log","module","upload","info",$file,"action","upload")) {
33 $a->action = clam_handle_infected_file($file,0,false);
34 clam_replace_infected_file($file);
35 notify_admins_unknown($file,$a);
36 continue;
38 $action = clam_handle_infected_file($file,$log->userid,true);
39 clam_replace_infected_file($file);
41 $user = get_record("user","id",$log->userid);
42 $course = get_record("course","id",$log->course);
43 $subject = get_string('virusfoundsubject','moodle',$site->fullname);
44 $a->date = userdate($log->time);
46 $a->action = $action;
47 $a->course = $course->fullname;
48 $a->user = fullname($user);
50 notify_user($user,$subject,$a);
51 notify_admins($user,$subject,$a);
53 fclose($fd);
56 function notify_user($user,$subject,$a) {
58 if (!$user) {
59 return false;
61 $body = get_string('virusfoundlater','moodle',$a);
62 email_to_user($user,get_admin(),$subject,$body);
66 function notify_admins($user,$subject,$a) {
68 $admins = get_admins();
70 $body = get_string('virusfoundlateradmin','moodle',$a);
71 foreach ($admins as $admin) {
72 email_to_user($admin,$admin,$subject,$body);
76 function notify_admins_unknown($file,$a) {
78 global $site;
80 $admins = get_admins();
81 $subject = get_string('virusfoundsubject','moodle',$site->fullname);
82 $body = get_string('virusfoundlateradminnolog','moodle',$a);
83 foreach ($admins as $admin) {
84 email_to_user($admin,$admin,$subject,$body);
88 function validate_line($line) {
89 global $CFG;
90 if (strpos($line,"FOUND") === false) {
91 return false;
93 $index = strpos($line,":");
94 $file = substr($line,0,$index);
95 if (!(strpos($file,$CFG->dataroot) === false)) {
96 if (!file_exists($file)) {
97 return false;
100 else {
101 if ($file{0} == "/") {
102 $file = $CFG->dataroot.$file;
104 else {
105 $file = $CFG->dataroot."/".$file;
107 if (!file_exists($file)) {
108 return false;
111 // clean up
112 $file = preg_replace('/\.\//','/',$file);
113 $file = preg_replace('/\/\//','/',$file);
114 return $file;