ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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
31 protected function buildScanCommandArguments(string $file = '-'): string
32 {
33 return ' -i ' . IL_ICAP_HOST . ' -p ' . IL_ICAP_PORT . ' -v -s ' . IL_ICAP_AV_COMMAND . ' -f ' . $file;
34 }
35
36 public function scanFile(string $file_path, string $org_name = ""): string
37 {
38 $return_string = '';
39 if (file_exists($file_path)) {
40 if (is_readable($file_path)) {
41 $file_path = realpath($file_path);
42 $args = ilShellUtil::escapeShellArg($file_path);
43 $arguments = $this->buildScanCommandArguments($args) . " 2>&1";
44 $cmd = ilShellUtil::escapeShellCmd($this->scanCommand);
45 $out = ilShellUtil::execQuoted($cmd, $arguments);
46 $timeout = preg_grep('/failed\/timedout.*/', $out);
47 $virus_detected = preg_grep('/' . self::HEADER_INFECTION_FOUND . '.*/', $out);
48 if (is_array($virus_detected) && count($virus_detected) > 0) {
49 $return_string = sprintf('Virus detected in %s', $file_path);
50 $this->log->warning($return_string);
51
52 } elseif (is_array($timeout) && count($timeout) > 0) {
53 $return_string = 'Cannot connect to icap server.';
54 $this->log->warning($return_string);
55 }
56 $this->scanResult = implode("\n", $out);
57 } else {
58 $return_string = sprintf('File "%s" not readable.', $file_path);
59 $this->log->info($return_string);
60 }
61 } else {
62 $return_string = sprintf('File "%s" not found.', $file_path);
63 $this->log->info($return_string);
64 }
65
66 $this->log->info(sprintf('No virus found in file "%s".', $file_path));
67
68 return $return_string;
69 }
70}
$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