ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilVirusScannerSophos.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  public function __construct(string $scan_command, string $clean_command)
24  {
25  parent::__construct($scan_command, $clean_command);
26  $this->type = 'sophos';
27  $this->scanZipFiles = true;
28  }
29 
30  public function scanFile(string $file_path, string $org_name = ''): string
31  {
32  $this->scanFilePath = $file_path;
33  $this->scanFileOrigName = $org_name;
34 
35  // Call of scan_file from Sophie (www.vanja.com/tools/sophie)
36  // sophie must run as a process
37  $a_filepath = realpath($file_path);
38  $cmd = ilShellUtil::escapeShellCmd($this->scanCommand);
39  $args = ilShellUtil::escapeShellArg(' ' . $a_filepath . ' ');
40  $cmd .= ' ' . $args . ' 2>&1';
41  exec($cmd, $out, $ret);
42  $this->scanResult = implode("\n", $out);
43 
44  // sophie could be called
45  if ((int) $ret === 0) {
46  if (preg_match('/FILE INFECTED/', $this->scanResult)) {
47  $this->scanFileIsInfected = true;
48  $this->logScanResult();
49  return $this->scanResult;
50  }
51 
52  $this->scanFileIsInfected = false;
53  return '';
54  }
55 
56  // sophie has failed (probably the daemon doesn't run)
57  $this->log->write(
58  'ERROR (Virus Scanner failed): '
59  . $this->scanResult
60  . '; COMMAMD=' . $cmd
61  );
62 
63  // try fallback: scan by cleaner command (sweep)
64  // -ss: Don't display anything except on error or virus
65  // -archive: sweep inside archives
66  unset($out, $ret);
67  $cmd = $this->cleanCommand . ' -ss -archive ' . $file_path . ' 2>&1';
68  exec($cmd, $out, $ret);
69  $this->scanResult = implode("\n", $out) . ' [' . $ret . ']';
70 
71  // error codes from sweep:
72  // 0 If no errors are encountered and no viruses are found.
73  // 1 If the user interrupts SWEEP (usually by pressing control-C) or kills the process.
74  // 2 If some error preventing further execution is discovered.
75  // 3 If viruses or virus fragments are discovered.
76  if ((int) $ret === 0) {
77  $this->cleanFileIsCleaned = false;
78  return '';
79  } elseif ((int) $ret === 3) {
80  $this->scanFileIsInfected = true;
81  $this->logScanResult();
82  return $this->scanResult;
83  }
84 
85  $this->error->raiseError(
86  $this->lng->txt('virus_scan_error') . ' '
87  . $this->lng->txt('virus_scan_message') . ' '
89  $this->error->WARNING
90  );
91  }
92 
93  public function cleanFile(string $file_path, string $org_name = ''): string
94  {
95  $this->cleanFilePath = $file_path;
96  $this->cleanFileOrigName = $org_name;
97 
98  // Call of sweep from Sophos (www.sophos.com)
99  // -di: Disinfect infected items
100  // -nc: Don't ask for confirmation before disinfection/deletion
101  // -ss: Don't display anything except on error or virus
102  // -eec: Use extended error codes
103  // -archive: sweep inside archives
104 
105  $cmd = $this->cleanCommand . ' -di -nc -ss -eec -archive ' . $file_path . ' 2>&1';
106  exec($cmd, $out, $ret);
107  $this->cleanResult = implode("\n", $out) . ' [' . $ret . ']';
108 
109  // always log the result from a clean attempt
110  $this->logCleanResult();
111 
112  // Extended error codes from sweep:
113  // 0 If no errors are encountered and no viruses are found.
114  // 8 If survivable errors have occurred.
115  // 12 If compressed files have been found and decompressed.
116  // 16 If compressed files have been found and not decompressed.
117  // 20 If viruses have been found and disinfected.
118  // 24 If viruses have been found and not disinfected.
119  // 28 If viruses have been found in memory.
120  // 32 If there has been an integrity check failure.
121  // 36 If unsurvivable errors have occurred.
122  // 40 If execution has been interrupted.
123  if ((int) $ret === 20) {
124  $this->cleanFileIsCleaned = true;
125  return $this->cleanResult;
126  }
127 
128  $this->cleanFileIsCleaned = false;
129  return '';
130  }
131 }
__construct(string $scan_command, string $clean_command)
static escapeShellArg(string $a_arg)
$out
Definition: buildRTE.php:24
static escapeShellCmd(string $a_arg)
__construct(Container $dic, ilPlugin $plugin)
cleanFile(string $file_path, string $org_name='')
scanFile(string $file_path, string $org_name='')