ILIAS  release_8 Revision v8.24
class.ilVirusScannerICapClient.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 private const HEADER_INFECTION_FOUND = 'X-Infection-Found';
24
25 public function __construct(string $scan_command, string $clean_command)
26 {
27 parent::__construct($scan_command, $clean_command);
28 $this->scanCommand = IL_ICAP_CLIENT;
29 }
30
35 protected function buildScanCommandArguments($file = '-') // default means piping
36 {
37 return ' -i ' . IL_ICAP_HOST . ' -p ' . IL_ICAP_PORT . ' -v -s ' . IL_ICAP_AV_COMMAND . ' -f ' . $file;
38 }
39
40 public function scanFile(string $file_path, string $org_name = ""): string
41 {
42 $return_string = '';
43 if (file_exists($file_path)) {
44 if (is_readable($file_path)) {
45 $file_path = realpath($file_path);
46 $args = ilShellUtil::escapeShellArg($file_path);
47 $arguments = $this->buildScanCommandArguments($args) . " 2>&1";
48 $cmd = ilShellUtil::escapeShellCmd($this->scanCommand);
49 $out = ilShellUtil::execQuoted($cmd, $arguments);
50 $timeout = preg_grep('/failed\/timedout.*/', $out);
51 $virus_detected = preg_grep('/' . self::HEADER_INFECTION_FOUND . '.*/', $out);
52 if (is_array($virus_detected) && count($virus_detected) > 0) {
53 $return_string = sprintf('Virus detected in %s', $file_path);
54 $this->log->warning($return_string);
55
56 } elseif (is_array($timeout) && count($timeout) > 0) {
57 $return_string = 'Cannot connect to icap server.';
58 $this->log->warning($return_string);
59 }
60 $this->scanResult = implode("\n", $out);
61 } else {
62 $return_string = sprintf('File "%s" not readable.', $file_path);
63 $this->log->info($return_string);
64 }
65 } else {
66 $return_string = sprintf('File "%s" not found.', $file_path);
67 $this->log->info($return_string);
68 }
69
70 $this->log->info(sprintf('No virus found in file "%s".', $file_path));
71 return $return_string;
72 }
73}
$out
Definition: buildRTE.php:24
static escapeShellArg(string $a_arg)
static escapeShellCmd(string $a_arg)
static execQuoted(string $cmd, ?string $args=null)
scanFile(string $file_path, string $org_name="")
__construct(string $scan_command, string $clean_command)
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc