MDL-15565, add maintenance mode alert reminder in title and navbar.
[moodle.git] / lib / uploadlib.php
CommitLineData
18b8fbfa 1<?php
c9b5ebf5 2
3/**
4 * uploadlib.php - This class handles all aspects of fileuploading
5 *
6 * @author ?
e0e1cd26 7 * @version $Id$
c9b5ebf5 8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package moodlecore
10 */
11
4fb072d9 12//error_reporting(E_ALL ^ E_NOTICE);
18b8fbfa 13/**
c9b5ebf5 14 * This class handles all aspects of fileuploading
18b8fbfa 15 */
16class upload_manager {
17
c9b5ebf5 18 /**
19 * Array to hold local copies of stuff in $_FILES
20 * @var array $files
21 */
22 var $files;
23 /**
24 * Holds all configuration stuff
25 * @var array $config
26 */
27 var $config;
28 /**
29 * Keep track of if we're ok
30 * (errors for each file are kept in $files['whatever']['uploadlog']
31 * @var boolean $status
32 */
33 var $status;
34 /**
35 * The course this file has been uploaded for. {@link $COURSE}
36 * (for logging and virus notifications)
37 * @var course $course
38 */
39 var $course;
40 /**
41 * If we're only getting one file.
42 * (for logging and virus notifications)
43 * @var string $inputname
44 */
45 var $inputname;
46 /**
47 * If we're given silent=true in the constructor, this gets built
48 * up to hold info about the process.
49 * @var string $notify
50 */
51 var $notify;
18b8fbfa 52
53 /**
54 * Constructor, sets up configuration stuff so we know how to act.
c9b5ebf5 55 *
18b8fbfa 56 * Note: destination not taken as parameter as some modules want to use the insertid in the path and we need to check the other stuff first.
c9b5ebf5 57 *
58 * @uses $CFG
59 * @param string $inputname If this is given the upload manager will only process the file in $_FILES with this name.
60 * @param boolean $deleteothers Whether to delete other files in the destination directory (optional, defaults to false)
61 * @param boolean $handlecollisions Whether to use {@link handle_filename_collision()} or not. (optional, defaults to false)
62 * @param course $course The course the files are being uploaded for (for logging and virus notifications) {@link $COURSE}
63 * @param boolean $recoverifmultiple If we come across a virus, or if a file doesn't validate or whatever, do we continue? optional, defaults to true.
64 * @param int $modbytes Max bytes for this module - this and $course->maxbytes are used to get the maxbytes from {@link get_max_upload_file_size()}.
65 * @param boolean $silent Whether to notify errors or not.
66 * @param boolean $allownull Whether we care if there's no file when we've set the input name.
aff2944e 67 * @param boolean $allownullmultiple Whether we care if there's no files AT ALL when we've got multiples. This won't complain if we have file 1 and file 3 but not file 2, only for NO FILES AT ALL.
18b8fbfa 68 */
aff2944e 69 function upload_manager($inputname='', $deleteothers=false, $handlecollisions=false, $course=null, $recoverifmultiple=false, $modbytes=0, $silent=false, $allownull=false, $allownullmultiple=true) {
18b8fbfa 70
4fb072d9 71 global $CFG, $SITE;
72
73 if (empty($course->id)) {
74 $course = $SITE;
75 }
18b8fbfa 76
77 $this->config->deleteothers = $deleteothers;
78 $this->config->handlecollisions = $handlecollisions;
79 $this->config->recoverifmultiple = $recoverifmultiple;
c9b5ebf5 80 $this->config->maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes, $modbytes);
81d425b4 81 $this->config->silent = $silent;
96038147 82 $this->config->allownull = $allownull;
18b8fbfa 83 $this->files = array();
84 $this->status = false;
85 $this->course = $course;
86 $this->inputname = $inputname;
96038147 87 if (empty($this->inputname)) {
aff2944e 88 $this->config->allownull = $allownullmultiple;
96038147 89 }
18b8fbfa 90 }
91
92 /**
c9b5ebf5 93 * Gets all entries out of $_FILES and stores them locally in $files and then
94 * checks each one against {@link get_max_upload_file_size()} and calls {@link cleanfilename()}
95 * and scans them for viruses etc.
96 * @uses $CFG
97 * @uses $_FILES
98 * @return boolean
18b8fbfa 99 */
100 function preprocess_files() {
101 global $CFG;
9056cec0 102
18b8fbfa 103 foreach ($_FILES as $name => $file) {
104 $this->status = true; // only set it to true here so that we can check if this function has been called.
105 if (empty($this->inputname) || $name == $this->inputname) { // if we have input name, only process if it matches.
106 $file['originalname'] = $file['name']; // do this first for the log.
107 $this->files[$name] = $file; // put it in first so we can get uploadlog out in print_upload_log.
4fb072d9 108 $this->files[$name]['uploadlog'] = ''; // initialize error log
96038147 109 $this->status = $this->validate_file($this->files[$name]); // default to only allowing empty on multiple uploads.
aff2944e 110 if (!$this->status && ($this->files[$name]['error'] == 0 || $this->files[$name]['error'] == 4) && ($this->config->allownull || empty($this->inputname))) {
18b8fbfa 111 // this shouldn't cause everything to stop.. modules should be responsible for knowing which if any are compulsory.
112 continue;
113 }
a2520070 114 if ($this->status && !empty($CFG->runclamonupload)) {
34941e07 115 $this->status = clam_scan_moodle_file($this->files[$name],$this->course);
18b8fbfa 116 }
117 if (!$this->status) {
118 if (!$this->config->recoverifmultiple && count($this->files) > 1) {
bdd3e83a 119 $a = new object();
120 $a->name = $this->files[$name]['originalname'];
18b8fbfa 121 $a->problem = $this->files[$name]['uploadlog'];
81d425b4 122 if (!$this->config->silent) {
123 notify(get_string('uploadfailednotrecovering','moodle',$a));
124 }
125 else {
c9b5ebf5 126 $this->notify .= '<br />'. get_string('uploadfailednotrecovering','moodle',$a);
81d425b4 127 }
18b8fbfa 128 $this->status = false;
129 return false;
9056cec0 130
131 } else if (count($this->files) == 1) {
132
133 if (!$this->config->silent and !$this->config->allownull) {
81d425b4 134 notify($this->files[$name]['uploadlog']);
9056cec0 135 } else {
c9b5ebf5 136 $this->notify .= '<br />'. $this->files[$name]['uploadlog'];
81d425b4 137 }
18b8fbfa 138 $this->status = false;
139 return false;
140 }
141 }
142 else {
143 $newname = clean_filename($this->files[$name]['name']);
144 if ($newname != $this->files[$name]['name']) {
bdd3e83a 145 $a = new object();
18b8fbfa 146 $a->oldname = $this->files[$name]['name'];
147 $a->newname = $newname;
c9b5ebf5 148 $this->files[$name]['uploadlog'] .= get_string('uploadrenamedchars','moodle', $a);
18b8fbfa 149 }
150 $this->files[$name]['name'] = $newname;
151 $this->files[$name]['clear'] = true; // ok to save.
3abd1a55 152 $this->config->somethingtosave = true;
18b8fbfa 153 }
154 }
155 }
46c5446e 156 if (!is_array($_FILES) || count($_FILES) == 0) {
96038147 157 return $this->config->allownull;
46c5446e 158 }
18b8fbfa 159 $this->status = true;
160 return true; // if we've got this far it means that we're recovering so we want status to be ok.
161 }
162
163 /**
164 * Validates a single file entry from _FILES
c9b5ebf5 165 *
166 * @param object $file The entry from _FILES to validate
167 * @return boolean True if ok.
18b8fbfa 168 */
96038147 169 function validate_file(&$file) {
18b8fbfa 170 if (empty($file)) {
96038147 171 return false;
18b8fbfa 172 }
173 if (!is_uploaded_file($file['tmp_name']) || $file['size'] == 0) {
174 $file['uploadlog'] .= "\n".$this->get_file_upload_error($file);
18b8fbfa 175 return false;
176 }
177 if ($file['size'] > $this->config->maxbytes) {
c9b5ebf5 178 $file['uploadlog'] .= "\n". get_string('uploadedfiletoobig', 'moodle', $this->config->maxbytes);
18b8fbfa 179 return false;
180 }
181 return true;
182 }
183
184 /**
185 * Moves all the files to the destination directory.
c9b5ebf5 186 *
187 * @uses $CFG
188 * @uses $USER
189 * @param string $destination The destination directory.
190 * @return boolean status;
18b8fbfa 191 */
192 function save_files($destination) {
c9b5ebf5 193 global $CFG, $USER;
18b8fbfa 194
195 if (!$this->status) { // preprocess_files hasn't been run
196 $this->preprocess_files();
197 }
3abd1a55 198
199 // if there are no files, bail before we create an empty directory.
200 if (empty($this->config->somethingtosave)) {
201 return true;
202 }
203
e78d624f 204 $savedsomething = false;
205
18b8fbfa 206 if ($this->status) {
c9b5ebf5 207 if (!(strpos($destination, $CFG->dataroot) === false)) {
18b8fbfa 208 // take it out for giving to make_upload_directory
c9b5ebf5 209 $destination = substr($destination, strlen($CFG->dataroot)+1);
18b8fbfa 210 }
211
c9b5ebf5 212 if ($destination{strlen($destination)-1} == '/') { // strip off a trailing / if we have one
213 $destination = substr($destination, 0, -1);
18b8fbfa 214 }
215
c9b5ebf5 216 if (!make_upload_directory($destination, true)) { //TODO maybe put this function here instead of moodlelib.php now.
18b8fbfa 217 $this->status = false;
218 return false;
219 }
220
c9b5ebf5 221 $destination = $CFG->dataroot .'/'. $destination; // now add it back in so we have a full path
18b8fbfa 222
223 $exceptions = array(); //need this later if we're deleting other files.
224
225 foreach (array_keys($this->files) as $i) {
226
227 if (!$this->files[$i]['clear']) {
228 // not ok to save
229 continue;
230 }
231
232 if ($this->config->handlecollisions) {
c9b5ebf5 233 $this->handle_filename_collision($destination, $this->files[$i]);
18b8fbfa 234 }
235 if (move_uploaded_file($this->files[$i]['tmp_name'], $destination.'/'.$this->files[$i]['name'])) {
c9b5ebf5 236 chmod($destination .'/'. $this->files[$i]['name'], $CFG->directorypermissions);
18b8fbfa 237 $this->files[$i]['fullpath'] = $destination.'/'.$this->files[$i]['name'];
238 $this->files[$i]['uploadlog'] .= "\n".get_string('uploadedfile');
239 $this->files[$i]['saved'] = true;
240 $exceptions[] = $this->files[$i]['name'];
241 // now add it to the log (this is important so we know who to notify if a virus is found later on)
c9b5ebf5 242 clam_log_upload($this->files[$i]['fullpath'], $this->course);
18b8fbfa 243 $savedsomething=true;
244 }
245 }
246 if ($savedsomething && $this->config->deleteothers) {
c9b5ebf5 247 $this->delete_other_files($destination, $exceptions);
18b8fbfa 248 }
249 }
aff2944e 250 if (empty($savedsomething)) {
18b8fbfa 251 $this->status = false;
aba94550 252 if ((empty($this->config->allownull) && !empty($this->inputname)) || (empty($this->inputname) && empty($this->config->allownullmultiple))) {
aff2944e 253 notify(get_string('uploadnofilefound'));
254 }
18b8fbfa 255 return false;
256 }
257 return $this->status;
258 }
259
260 /**
c9b5ebf5 261 * Wrapper function that calls {@link preprocess_files()} and {@link viruscheck_files()} and then {@link save_files()}
18b8fbfa 262 * Modules that require the insert id in the filepath should not use this and call these functions seperately in the required order.
c9b5ebf5 263 * @parameter string $destination Where to save the uploaded files to.
264 * @return boolean
18b8fbfa 265 */
266 function process_file_uploads($destination) {
267 if ($this->preprocess_files()) {
268 return $this->save_files($destination);
269 }
270 return false;
271 }
272
273 /**
274 * Deletes all the files in a given directory except for the files in $exceptions (full paths)
c9b5ebf5 275 *
276 * @param string $destination The directory to clean up.
277 * @param array $exceptions Full paths of files to KEEP.
18b8fbfa 278 */
c9b5ebf5 279 function delete_other_files($destination, $exceptions=null) {
4fb072d9 280 $deletedsomething = false;
18b8fbfa 281 if ($filestodel = get_directory_list($destination)) {
282 foreach ($filestodel as $file) {
c9b5ebf5 283 if (!is_array($exceptions) || !in_array($file, $exceptions)) {
284 unlink($destination .'/'. $file);
18b8fbfa 285 $deletedsomething = true;
286 }
287 }
288 }
289 if ($deletedsomething) {
81d425b4 290 if (!$this->config->silent) {
291 notify(get_string('uploadoldfilesdeleted'));
292 }
293 else {
c9b5ebf5 294 $this->notify .= '<br />'. get_string('uploadoldfilesdeleted');
81d425b4 295 }
18b8fbfa 296 }
297 }
298
299 /**
300 * Handles filename collisions - if the desired filename exists it will rename it according to the pattern in $format
c9b5ebf5 301 * @param string $destination Destination directory (to check existing files against)
302 * @param object $file Passed in by reference. The current file from $files we're processing.
94ba6c90 303 * @return void - modifies &$file parameter.
18b8fbfa 304 */
94ba6c90 305 function handle_filename_collision($destination, &$file) {
306 if (!file_exists($destination .'/'. $file['name'])) {
307 return;
308 }
309
310 $parts = explode('.', $file['name']);
311 if (count($parts) > 1) {
312 $extension = '.'.array_pop($parts);
313 $name = implode('.', $parts);
314 } else {
315 $extension = '';
316 $name = $file['name'];
317 }
318
319 $current = 0;
320 if (preg_match('/^(.*)_(\d*)$/s', $name, $matches)) {
321 $name = $matches[1];
322 $current = (int)$matches[2];
323 }
324 $i = $current + 1;
325
326 while (!$this->check_before_renaming($destination, $name.'_'.$i.$extension, $file)) {
327 $i++;
18b8fbfa 328 }
94ba6c90 329 $a = new object();
330 $a->oldname = $file['name'];
331 $file['name'] = $name.'_'.$i.$extension;
332 $a->newname = $file['name'];
333 $file['uploadlog'] .= "\n". get_string('uploadrenamedcollision','moodle', $a);
18b8fbfa 334 }
335
336 /**
337 * This function checks a potential filename against what's on the filesystem already and what's been saved already.
c9b5ebf5 338 * @param string $destination Destination directory (to check existing files against)
339 * @param string $nametocheck The filename to be compared.
340 * @param object $file The current file from $files we're processing.
341 * return boolean
18b8fbfa 342 */
c9b5ebf5 343 function check_before_renaming($destination, $nametocheck, $file) {
344 if (!file_exists($destination .'/'. $nametocheck)) {
18b8fbfa 345 return true;
346 }
347 if ($this->config->deleteothers) {
348 foreach ($this->files as $tocheck) {
349 // if we're deleting files anyway, it's not THIS file and we care about it and it has the same name and has already been saved..
350 if ($file['tmp_name'] != $tocheck['tmp_name'] && $tocheck['clear'] && $nametocheck == $tocheck['name'] && $tocheck['saved']) {
351 $collision = true;
352 }
353 }
354 if (!$collision) {
355 return true;
356 }
357 }
358 return false;
359 }
360
c9b5ebf5 361 /**
362 * ?
363 *
364 * @param object $file Passed in by reference. The current file from $files we're processing.
365 * @return string
366 * @todo Finish documenting this function
367 */
18b8fbfa 368 function get_file_upload_error(&$file) {
369
370 switch ($file['error']) {
371 case 0: // UPLOAD_ERR_OK
372 if ($file['size'] > 0) {
373 $errmessage = get_string('uploadproblem', $file['name']);
374 } else {
375 $errmessage = get_string('uploadnofilefound'); /// probably a dud file name
376 }
377 break;
378
379 case 1: // UPLOAD_ERR_INI_SIZE
380 $errmessage = get_string('uploadserverlimit');
381 break;
382
383 case 2: // UPLOAD_ERR_FORM_SIZE
384 $errmessage = get_string('uploadformlimit');
385 break;
386
387 case 3: // UPLOAD_ERR_PARTIAL
388 $errmessage = get_string('uploadpartialfile');
389 break;
390
391 case 4: // UPLOAD_ERR_NO_FILE
392 $errmessage = get_string('uploadnofilefound');
393 break;
394
395 default:
396 $errmessage = get_string('uploadproblem', $file['name']);
397 }
398 return $errmessage;
399 }
400
401 /**
402 * prints a log of everything that happened (of interest) to each file in _FILES
403 * @param $return - optional, defaults to false (log is echoed)
404 */
aff2944e 405 function print_upload_log($return=false,$skipemptyifmultiple=false) {
7bd43426 406 $str = '';
18b8fbfa 407 foreach (array_keys($this->files) as $i => $key) {
aff2944e 408 if (count($this->files) > 1 && !empty($skipemptyifmultiple) && $this->files[$key]['error'] == 4) {
409 continue;
410 }
c9b5ebf5 411 $str .= '<strong>'. get_string('uploadfilelog', 'moodle', $i+1) .' '
18b8fbfa 412 .((!empty($this->files[$key]['originalname'])) ? '('.$this->files[$key]['originalname'].')' : '')
c9b5ebf5 413 .'</strong> :'. nl2br($this->files[$key]['uploadlog']) .'<br />';
18b8fbfa 414 }
415 if ($return) {
416 return $str;
417 }
418 echo $str;
419 }
420
421 /**
422 * If we're only handling one file (if inputname was given in the constructor) this will return the (possibly changed) filename of the file.
c9b5ebf5 423 @return boolean
18b8fbfa 424 */
425 function get_new_filename() {
4fb072d9 426 if (!empty($this->inputname) and count($this->files) == 1 and $this->files[$this->inputname]['error'] != 4) {
18b8fbfa 427 return $this->files[$this->inputname]['name'];
428 }
429 return false;
430 }
431
81d425b4 432 /**
433 * If we're only handling one file (if input name was given in the constructor) this will return the full path to the saved file.
c9b5ebf5 434 * @return boolean
81d425b4 435 */
436 function get_new_filepath() {
4fb072d9 437 if (!empty($this->inputname) and count($this->files) == 1 and $this->files[$this->inputname]['error'] != 4) {
81d425b4 438 return $this->files[$this->inputname]['fullpath'];
439 }
440 return false;
441 }
442
18b8fbfa 443 /**
444 * If we're only handling one file (if inputname was given in the constructor) this will return the ORIGINAL filename of the file.
c9b5ebf5 445 * @return boolean
18b8fbfa 446 */
447 function get_original_filename() {
4fb072d9 448 if (!empty($this->inputname) and count($this->files) == 1 and $this->files[$this->inputname]['error'] != 4) {
18b8fbfa 449 return $this->files[$this->inputname]['originalname'];
450 }
451 return false;
452 }
96038147 453
454 /**
c9b5ebf5 455 * This function returns any errors wrapped up in red.
456 * @return string
96038147 457 */
458 function get_errors() {
902d5cc0 459 if (!empty($this->notify)) {
460 return '<p class="notifyproblem">'. $this->notify .'</p>';
461 } else {
462 return null;
463 }
96038147 464 }
18b8fbfa 465}
466
467/**************************************************************************************
468THESE FUNCTIONS ARE OUTSIDE THE CLASS BECAUSE THEY NEED TO BE CALLED FROM OTHER PLACES.
469FOR EXAMPLE CLAM_HANDLE_INFECTED_FILE AND CLAM_REPLACE_INFECTED_FILE USED FROM CRON
470UPLOAD_PRINT_FORM_FRAGMENT DOESN'T REALLY BELONG IN THE CLASS BUT CERTAINLY IN THIS FILE
471***************************************************************************************/
472
473
474/**
c9b5ebf5 475 * This function prints out a number of upload form elements.
476 *
477 * @param int $numfiles The number of elements required (optional, defaults to 1)
478 * @param array $names Array of element names to use (optional, defaults to FILE_n)
479 * @param array $descriptions Array of strings to be printed out before each file bit.
480 * @param boolean $uselabels -Whether to output text fields for file descriptions or not (optional, defaults to false)
481 * @param array $labelnames Array of element names to use for labels (optional, defaults to LABEL_n)
482 * @param int $coursebytes $coursebytes and $maxbytes are used to calculate upload max size ( using {@link get_max_upload_file_size})
483 * @param int $modbytes $coursebytes and $maxbytes are used to calculate upload max size ( using {@link get_max_upload_file_size})
484 * @param boolean $return -Whether to return the string (defaults to false - string is echoed)
485 * @return string Form returned as string if $return is true
18b8fbfa 486 */
c9b5ebf5 487function upload_print_form_fragment($numfiles=1, $names=null, $descriptions=null, $uselabels=false, $labelnames=null, $coursebytes=0, $modbytes=0, $return=false) {
18b8fbfa 488 global $CFG;
c9b5ebf5 489 $maxbytes = get_max_upload_file_size($CFG->maxbytes, $coursebytes, $modbytes);
490 $str = '<input type="hidden" name="MAX_FILE_SIZE" value="'. $maxbytes .'" />'."\n";
18b8fbfa 491 for ($i = 0; $i < $numfiles; $i++) {
492 if (is_array($descriptions) && !empty($descriptions[$i])) {
c9b5ebf5 493 $str .= '<strong>'. $descriptions[$i] .'</strong><br />';
18b8fbfa 494 }
09e8a2f8 495 $name = ((is_array($names) && !empty($names[$i])) ? $names[$i] : 'FILE_'.$i);
c9b5ebf5 496 $str .= '<input type="file" size="50" name="'. $name .'" alt="'. $name .'" /><br />'."\n";
18b8fbfa 497 if ($uselabels) {
09e8a2f8 498 $lname = ((is_array($labelnames) && !empty($labelnames[$i])) ? $labelnames[$i] : 'LABEL_'.$i);
c9b5ebf5 499 $str .= get_string('uploadlabel').' <input type="text" size="50" name="'. $lname .'" alt="'. $lname
18b8fbfa 500 .'" /><br /><br />'."\n";
501 }
502 }
503 if ($return) {
504 return $str;
505 }
506 else {
507 echo $str;
508 }
509}
510
511
512/**
513 * Deals with an infected file - either moves it to a quarantinedir
514 * (specified in CFG->quarantinedir) or deletes it.
c9b5ebf5 515 *
18b8fbfa 516 * If moving it fails, it deletes it.
c9b5ebf5 517 *
518 *@uses $CFG
519 * @uses $USER
520 * @param string $file Full path to the file
521 * @param int $userid If not used, defaults to $USER->id (there in case called from cron)
522 * @param boolean $basiconly Admin level reporting or user level reporting.
523 * @return string Details of what the function did.
18b8fbfa 524 */
c9b5ebf5 525function clam_handle_infected_file($file, $userid=0, $basiconly=false) {
18b8fbfa 526
c9b5ebf5 527 global $CFG, $USER;
18b8fbfa 528 if ($USER && !$userid) {
529 $userid = $USER->id;
530 }
531 $delete = true;
532 if (file_exists($CFG->quarantinedir) && is_dir($CFG->quarantinedir) && is_writable($CFG->quarantinedir)) {
533 $now = date('YmdHis');
c9b5ebf5 534 if (rename($file, $CFG->quarantinedir .'/'. $now .'-user-'. $userid .'-infected')) {
18b8fbfa 535 $delete = false;
c9b5ebf5 536 clam_log_infected($file, $CFG->quarantinedir.'/'. $now .'-user-'. $userid .'-infected', $userid);
18b8fbfa 537 if ($basiconly) {
c9b5ebf5 538 $notice .= "\n". get_string('clammovedfilebasic');
18b8fbfa 539 }
540 else {
c9b5ebf5 541 $notice .= "\n". get_string('clammovedfile', 'moodle', $CFG->quarantinedir.'/'. $now .'-user-'. $userid .'-infected');
18b8fbfa 542 }
543 }
544 else {
545 if ($basiconly) {
c9b5ebf5 546 $notice .= "\n". get_string('clamdeletedfile');
18b8fbfa 547 }
548 else {
c9b5ebf5 549 $notice .= "\n". get_string('clamquarantinedirfailed', 'moodle', $CFG->quarantinedir);
18b8fbfa 550 }
551 }
552 }
553 else {
554 if ($basiconly) {
c9b5ebf5 555 $notice .= "\n". get_string('clamdeletedfile');
18b8fbfa 556 }
557 else {
c9b5ebf5 558 $notice .= "\n". get_string('clamquarantinedirfailed', 'moodle', $CFG->quarantinedir);
18b8fbfa 559 }
560 }
561 if ($delete) {
562 if (unlink($file)) {
c9b5ebf5 563 clam_log_infected($file, '', $userid);
564 $notice .= "\n". get_string('clamdeletedfile');
18b8fbfa 565 }
566 else {
567 if ($basiconly) {
568 // still tell the user the file has been deleted. this is only for admins.
c9b5ebf5 569 $notice .= "\n". get_string('clamdeletedfile');
18b8fbfa 570 }
571 else {
c9b5ebf5 572 $notice .= "\n". get_string('clamdeletedfilefailed');
18b8fbfa 573 }
574 }
575 }
576 return $notice;
577}
578
579/**
c9b5ebf5 580 * Replaces the given file with a string.
581 *
582 * The replacement string is used to notify that the original file had a virus
18b8fbfa 583 * This is to avoid missing files but could result in the wrong content-type.
c9b5ebf5 584 * @param string $file Full path to the file.
585 * @return boolean
18b8fbfa 586 */
587function clam_replace_infected_file($file) {
588 $newcontents = get_string('virusplaceholder');
c9b5ebf5 589 if (!$f = fopen($file, 'w')) {
18b8fbfa 590 return false;
591 }
c9b5ebf5 592 if (!fwrite($f, $newcontents)) {
18b8fbfa 593 return false;
594 }
595 return true;
596}
597
598
599/**
c9b5ebf5 600 * If $CFG->runclamonupload is set, we scan a given file. (called from {@link preprocess_files()})
601 *
18b8fbfa 602 * This function will add on a uploadlog index in $file.
c9b5ebf5 603 * @param mixed $file The file to scan from $files. or an absolute path to a file.
604 * @param course $course {@link $COURSE}
605 * @return int 1 if good, 0 if something goes wrong (opposite from actual error code from clam)
18b8fbfa 606 */
34941e07 607function clam_scan_moodle_file(&$file, $course) {
c9b5ebf5 608 global $CFG, $USER;
18b8fbfa 609
610 if (is_array($file) && is_uploaded_file($file['tmp_name'])) { // it's from $_FILES
611 $appendlog = true;
612 $fullpath = $file['tmp_name'];
613 }
614 else if (file_exists($file)) { // it's a path to somewhere on the filesystem!
615 $fullpath = $file;
616 }
617 else {
618 return false; // erm, what is this supposed to be then, huh?
619 }
620
96038147 621 $CFG->pathtoclam = trim($CFG->pathtoclam);
622
18b8fbfa 623 if (!$CFG->pathtoclam || !file_exists($CFG->pathtoclam) || !is_executable($CFG->pathtoclam)) {
624 $newreturn = 1;
c9b5ebf5 625 $notice = get_string('clamlost', 'moodle', $CFG->pathtoclam);
18b8fbfa 626 if ($CFG->clamfailureonupload == 'actlikevirus') {
c9b5ebf5 627 $notice .= "\n". get_string('clamlostandactinglikevirus');
628 $notice .= "\n". clam_handle_infected_file($fullpath);
18b8fbfa 629 $newreturn = false;
630 }
631 clam_mail_admins($notice);
96038147 632 if ($appendlog) {
c9b5ebf5 633 $file['uploadlog'] .= "\n". get_string('clambroken');
96038147 634 $file['clam'] = 1;
635 }
18b8fbfa 636 return $newreturn; // return 1 if we're allowing clam failures
637 }
638
c9b5ebf5 639 $cmd = $CFG->pathtoclam .' '. $fullpath ." 2>&1";
18b8fbfa 640
641 // before we do anything we need to change perms so that clamscan can read the file (clamdscan won't work otherwise)
642 chmod($fullpath,0644);
643
c9b5ebf5 644 exec($cmd, $output, $return);
18b8fbfa 645
646
647 switch ($return) {
648 case 0: // glee! we're ok.
649 return 1; // translate clam return code into reasonable return code consistent with everything else.
650 case 1: // bad wicked evil, we have a virus.
bdd3e83a 651 $info = new object();
18b8fbfa 652 if (!empty($course)) {
653 $info->course = $course->fullname;
654 }
655 else {
656 $info->course = 'No course';
657 }
92e754eb 658 $info->user = fullname($USER);
c9b5ebf5 659 $notice = get_string('virusfound', 'moodle', $info);
660 $notice .= "\n\n". implode("\n", $output);
661 $notice .= "\n\n". clam_handle_infected_file($fullpath);
18b8fbfa 662 clam_mail_admins($notice);
663 if ($appendlog) {
664 $info->filename = $file['originalname'];
c9b5ebf5 665 $file['uploadlog'] .= "\n". get_string('virusfounduser', 'moodle', $info);
18b8fbfa 666 $file['virus'] = 1;
667 }
668 return false; // in this case, 0 means bad.
669 default:
670 // error - clam failed to run or something went wrong
c9b5ebf5 671 $notice .= get_string('clamfailed', 'moodle', get_clam_error_code($return));
672 $notice .= "\n\n". implode("\n", $output);
18b8fbfa 673 $newreturn = true;
674 if ($CFG->clamfailureonupload == 'actlikevirus') {
c9b5ebf5 675 $notice .= "\n". clam_handle_infected_file($fullpath);
18b8fbfa 676 $newreturn = false;
677 }
678 clam_mail_admins($notice);
679 if ($appendlog) {
c9b5ebf5 680 $file['uploadlog'] .= "\n". get_string('clambroken');
18b8fbfa 681 $file['clam'] = 1;
682 }
683 return $newreturn; // return 1 if we're allowing failures.
684 }
685}
686
687/**
c9b5ebf5 688 * Emails admins about a clam outcome
689 *
690 * @param string $notice The body of the email to be sent.
18b8fbfa 691 */
692function clam_mail_admins($notice) {
693
694 $site = get_site();
695
268ddd50 696 $subject = get_string('clamemailsubject', 'moodle', format_string($site->fullname));
18b8fbfa 697 $admins = get_admins();
698 foreach ($admins as $admin) {
c9b5ebf5 699 email_to_user($admin, get_admin(), $subject, $notice);
18b8fbfa 700 }
701}
702
703
c9b5ebf5 704/**
705 * Returns the string equivalent of a numeric clam error code
706 *
707 * @param int $returncode The numeric error code in question.
708 * return string The definition of the error code
709 */
18b8fbfa 710function get_clam_error_code($returncode) {
711 $returncodes = array();
712 $returncodes[0] = 'No virus found.';
713 $returncodes[1] = 'Virus(es) found.';
714 $returncodes[2] = ' An error occured'; // specific to clamdscan
715 // all after here are specific to clamscan
716 $returncodes[40] = 'Unknown option passed.';
717 $returncodes[50] = 'Database initialization error.';
718 $returncodes[52] = 'Not supported file type.';
719 $returncodes[53] = 'Can\'t open directory.';
720 $returncodes[54] = 'Can\'t open file. (ofm)';
721 $returncodes[55] = 'Error reading file. (ofm)';
722 $returncodes[56] = 'Can\'t stat input file / directory.';
723 $returncodes[57] = 'Can\'t get absolute path name of current working directory.';
724 $returncodes[58] = 'I/O error, please check your filesystem.';
725 $returncodes[59] = 'Can\'t get information about current user from /etc/passwd.';
726 $returncodes[60] = 'Can\'t get information about user \'clamav\' (default name) from /etc/passwd.';
727 $returncodes[61] = 'Can\'t fork.';
728 $returncodes[63] = 'Can\'t create temporary files/directories (check permissions).';
729 $returncodes[64] = 'Can\'t write to temporary directory (please specify another one).';
730 $returncodes[70] = 'Can\'t allocate and clear memory (calloc).';
731 $returncodes[71] = 'Can\'t allocate memory (malloc).';
732 if ($returncodes[$returncode])
733 return $returncodes[$returncode];
734 return get_string('clamunknownerror');
735
736}
737
738/**
c9b5ebf5 739 * Adds a file upload to the log table so that clam can resolve the filename to the user later if necessary
740 *
741 * @uses $CFG
742 * @uses $USER
743 * @param string $newfilepath ?
744 * @param course $course {@link $COURSE}
745 * @param boolean $nourl ?
746 * @todo Finish documenting this function
18b8fbfa 747 */
c9b5ebf5 748function clam_log_upload($newfilepath, $course=null, $nourl=false) {
749 global $CFG, $USER;
18b8fbfa 750 // get rid of any double // that might have appeared
c9b5ebf5 751 $newfilepath = preg_replace('/\/\//', '/', $newfilepath);
752 if (strpos($newfilepath, $CFG->dataroot) === false) {
753 $newfilepath = $CFG->dataroot .'/'. $newfilepath;
18b8fbfa 754 }
18b8fbfa 755 $courseid = 0;
756 if ($course) {
757 $courseid = $course->id;
758 }
c9b5ebf5 759 add_to_log($courseid, 'upload', 'upload', ((!$nourl) ? substr($_SERVER['HTTP_REFERER'], 0, 100) : ''), $newfilepath);
18b8fbfa 760}
761
7604c7db 762/**
763 * This function logs to error_log and to the log table that an infected file has been found and what's happened to it.
c9b5ebf5 764 *
765 * @param string $oldfilepath Full path to the infected file before it was moved.
766 * @param string $newfilepath Full path to the infected file since it was moved to the quarantine directory (if the file was deleted, leave empty).
767 * @param int $userid The user id of the user who uploaded the file.
7604c7db 768 */
c9b5ebf5 769function clam_log_infected($oldfilepath='', $newfilepath='', $userid=0) {
bdd3e83a 770 global $DB;
7604c7db 771
c9b5ebf5 772 add_to_log(0, 'upload', 'infected', $_SERVER['HTTP_REFERER'], $oldfilepath, 0, $userid);
7604c7db 773
bdd3e83a 774 $user = $DB->get_record('user', array('id'=>$userid));
7604c7db 775
776 $errorstr = 'Clam AV has found a file that is infected with a virus. It was uploaded by '
436d4c6b 777 . ((empty($user)) ? ' an unknown user ' : fullname($user))
7604c7db 778 . ((empty($oldfilepath)) ? '. The infected file was caught on upload ('.$oldfilepath.')'
c9b5ebf5 779 : '. The original file path of the infected file was '. $oldfilepath)
780 . ((empty($newfilepath)) ? '. The file has been deleted ' : '. The file has been moved to a quarantine directory and the new path is '. $newfilepath);
7604c7db 781
782 error_log($errorstr);
783}
784
785
18b8fbfa 786/**
c9b5ebf5 787 * Some of the modules allow moving attachments (glossary), in which case we need to hunt down an original log and change the path.
788 *
789 * @uses $CFG
790 * @param string $oldpath The old path to the file (should be in the log)
791 * @param string $newpath The new path to the file
792 * @param boolean $update If true this function will overwrite old record (used for forum moving etc).
18b8fbfa 793 */
c9b5ebf5 794function clam_change_log($oldpath, $newpath, $update=true) {
bdd3e83a 795 global $DB;
8930f900 796
bdd3e83a 797 if (!$record = $DB->get_record('log', array('info'=>$oldpath, 'module'=>'upload'))) {
8930f900 798 return false;
799 }
800 $record->info = $newpath;
801 if ($update) {
bdd3e83a 802 $DB->update_record('log', $record);
803 } else {
8930f900 804 unset($record->id);
bdd3e83a 805 $DB->insert_record('log', $record);
8930f900 806 }
18b8fbfa 807}
9056cec0 808?>