ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
ilVirusScannerICapRemote Class Reference

Interface to the ClamAV virus protector. More...

+ Inheritance diagram for ilVirusScannerICapRemote:
+ Collaboration diagram for ilVirusScannerICapRemote:

Public Member Functions

 __construct ($a_scancommand, $a_cleancommand)
 ilVirusScannerICap constructor. More...
 
 options ($service)
 
 getRequest ($method, $service, $body=[], $headers=[])
 
 send ($request)
 
 getLastSocketError ()
 Get last error code from socket object. More...
 
 respMod ($service, $body=[], $headers=[])
 
 reqMod ($service, $body=[], $headers=[])
 
- Public Member Functions inherited from ilVirusScanner
 __construct ($a_scancommand, $a_cleancommand)
 Constructor public. More...
 
 scanBuffer ($buffer)
 
 scanFile ($a_filepath, $a_origname="")
 scan a file for viruses needs to be redefined in child classes here it simulates a scan "infected.txt" or "cleanable.txt" are expected to be infected More...
 
 cleanFile ($a_filepath, $a_origname="")
 clean an infected file needs to be redefined in child classes here it simulates a clean "cleanable.txt" is expected to be cleanable More...
 
 fileCleaned ()
 returns wether file has been cleaned successfully or not More...
 
 logScanResult ()
 write the result of the last scan to the log public More...
 
 logCleanResult ()
 write the result of the last clean to the log public More...
 
 getScanResult ()
 get the pure output of the external scan More...
 
 getCleanResult ()
 get the pure output of the external scan More...
 
 getScanMessage ()
 get a located message with the result from the last scan More...
 
 getCleanMessage ()
 get a located message with the result from the last clean More...
 
 getScanZipFiles ()
 get info if class can scan ZIP files More...
 

Data Fields

 $userAgent = 'PHP-CLIENT/0.1.0'
 
- Data Fields inherited from ilVirusScanner
 $type
 
 $scanZipFiles
 
 $scanCommand
 
 $cleanCommand
 
 $scanFilePath
 
 $scanFileOrigName
 
 $cleanFilePath
 
 $cleanFileOrigName
 
 $scanFileIsInfected
 
 $cleanFileIsCleaned
 
 $scanResult
 
 $cleanResult
 
 $ilias
 
 $lng
 
 $log
 

Private Member Functions

 connect ()
 
 disconnect ()
 
 parseResponse ($string)
 

Private Attributes

 $host
 
 $port
 
 $socket
 

Additional Inherited Members

- Protected Member Functions inherited from ilVirusScanner
 scanFileFromBuffer ($buffer)
 
 createBufferFile ($buffer)
 
 removeBufferFile ($bufferFile)
 

Detailed Description

Interface to the ClamAV virus protector.

Author
Ralf Schenk rs@da.nosp@m.taba.nosp@m.y.de
Version
$Id$

Definition at line 15 of file class.ilVirusScannerICapRemote.php.

Constructor & Destructor Documentation

◆ __construct()

ilVirusScannerICapRemote::__construct (   $a_scancommand,
  $a_cleancommand 
)

ilVirusScannerICap constructor.

Parameters
$a_scancommand
$a_cleancommand

Definition at line 31 of file class.ilVirusScannerICapRemote.php.

References ILIAS\GlobalScreen\Provider\__construct().

32  {
33  parent::__construct($a_scancommand, $a_cleancommand);
34  $this->host = IL_ICAP_HOST;
35  $this->port = IL_ICAP_PORT;
36  }
__construct(Container $dic, ilPlugin $plugin)
+ Here is the call graph for this function:

Member Function Documentation

◆ connect()

ilVirusScannerICapRemote::connect ( )
private
Returns
bool
Exceptions
ErrorException

Definition at line 135 of file class.ilVirusScannerICapRemote.php.

References Vendor\Package\$e.

Referenced by send().

136  {
137  $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
138  try{
139  if (!socket_connect($this->socket, $this->host, $this->port)) {
140  return false;
141  }
142  } catch(ErrorException $e){
143  throw $e;
144  }
145 
146  return true;
147  }
+ Here is the caller graph for this function:

◆ disconnect()

ilVirusScannerICapRemote::disconnect ( )
private

Definition at line 161 of file class.ilVirusScannerICapRemote.php.

Referenced by send().

162  {
163  socket_shutdown($this->socket);
164  socket_close($this->socket);
165  }
+ Here is the caller graph for this function:

◆ getLastSocketError()

ilVirusScannerICapRemote::getLastSocketError ( )

Get last error code from socket object.

Returns
int Socket error code

Definition at line 153 of file class.ilVirusScannerICapRemote.php.

Referenced by send().

154  {
155  return socket_last_error($this->socket);
156  }
+ Here is the caller graph for this function:

◆ getRequest()

ilVirusScannerICapRemote::getRequest (   $method,
  $service,
  $body = [],
  $headers = [] 
)
Parameters
$method
$service
array$body
array$headers
Returns
string

Definition at line 59 of file class.ilVirusScannerICapRemote.php.

References $data, $host, $section, ilVirusScanner\$type, and $userAgent.

Referenced by options(), reqMod(), and respMod().

60  {
61  if (!array_key_exists('Host', $headers)) {
62  $headers['Host'] = $this->host;
63  }
64  if (!array_key_exists('User-Agent', $headers)) {
65  $headers['User-Agent'] = $this->userAgent;
66  }
67  if (!array_key_exists('Connection', $headers)) {
68  $headers['Connection'] = 'close';
69  }
70  $bodyData = '';
71  $hasBody = false;
72  $encapsulated = [];
73  foreach ($body as $type => $data) {
74  switch ($type) {
75  case 'req-hdr':
76  case 'res-hdr':
77  $encapsulated[$type] = strlen($bodyData);
78  $bodyData .= $data;
79  break;
80  case 'req-body':
81  case 'res-body':
82  $encapsulated[$type] = strlen($bodyData);
83  $bodyData .= dechex(strlen($data)) . "\r\n";
84  $bodyData .= $data;
85  $bodyData .= "\r\n";
86  $hasBody = true;
87  break;
88  }
89  }
90  if ($hasBody) {
91  $bodyData .= "0\r\n\r\n";
92  } elseif (count($encapsulated) > 0) {
93  $encapsulated['null-body'] = strlen($bodyData);
94  }
95  if (count($encapsulated) > 0) {
96  $headers['Encapsulated'] = '';
97  foreach ($encapsulated as $section => $offset) {
98  $headers['Encapsulated'] .= $headers['Encapsulated'] === '' ? '' : ', ';
99  $headers['Encapsulated'] .= "{$section}={$offset}";
100  }
101  }
102  $request = "{$method} icap://{$this->host}/{$service} ICAP/1.0\r\n";
103  foreach ($headers as $header => $value) {
104  $request .= "{$header}: {$value}\r\n";
105  }
106  $request .= "\r\n";
107  $request .= $bodyData;
108  return $request;
109  }
$data
Definition: storeScorm.php:23
$section
Definition: Utf8Test.php:83
+ Here is the caller graph for this function:

◆ options()

ilVirusScannerICapRemote::options (   $service)
Parameters
$service
Returns
array

Definition at line 42 of file class.ilVirusScannerICapRemote.php.

References $response, $service, getRequest(), parseResponse(), and send().

Referenced by ilVirusScannerICapRemoteAvClient\__construct().

43  {
44  $request = $this->getRequest('OPTIONS', $service);
45  $response = $this->send($request);
46  if(strlen($response) > 0) {
47  return $this->parseResponse($response);
48  }
49  return [];
50  }
$service
Definition: result.php:17
$response
getRequest($method, $service, $body=[], $headers=[])
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parseResponse()

ilVirusScannerICapRemote::parseResponse (   $string)
private
Parameters
$string
Returns
array

Definition at line 171 of file class.ilVirusScannerICapRemote.php.

References $data, $param, $response, and $section.

Referenced by options(), reqMod(), and respMod().

172  {
173  $response = [
174  'protocol' => [],
175  'headers' => [],
176  'body' => [],
177  'rawBody' => ''
178  ];
179  foreach (preg_split('/\r?\n/', $string) as $line) {
180  if ([] === $response['protocol']) {
181  if (0 !== strpos($line, 'ICAP/')) {
182  throw new RuntimeException('Unknown ICAP response');
183  }
184  $parts = preg_split('/ +/', $line, 3);
185  $response['protocol'] = [
186  'icap' => isset($parts[0]) ? $parts[0] : '',
187  'code' => isset($parts[1]) ? $parts[1] : '',
188  'message' => isset($parts[2]) ? $parts[2] : '',
189  ];
190  continue;
191  }
192  if ('' === $line) {
193  break;
194  }
195  $parts = preg_split('/: /', $line, 2);
196  if (isset($parts[0])) {
197  $response['headers'][$parts[0]] = isset($parts[1]) ? $parts[1] : '';
198  }
199  }
200  $body = preg_split('/\r?\n\r?\n/', $string, 2);
201  if (isset($body[1])) {
202  $response['rawBody'] = $body[1];
203  if (array_key_exists('Encapsulated', $response['headers'])) {
204  $encapsulated = [];
205  $params = preg_split('/, /', $response['headers']['Encapsulated']);
206  if (count($params) > 0) {
207  foreach ($params as $param) {
208  $parts = preg_split('/=/', $param);
209  if (count($parts) !== 2) {
210  continue;
211  }
212  $encapsulated[$parts[0]] = $parts[1];
213  }
214  }
215  foreach ($encapsulated as $section => $offset) {
216  $data = substr($body[1], $offset);
217  switch ($section) {
218  case 'req-hdr':
219  case 'res-hdr':
220  $response['body'][$section] = preg_split('/\r?\n\r?\n/', $data, 2)[0];
221  break;
222  case 'req-body':
223  case 'res-body':
224  $parts = preg_split('/\r?\n/', $data, 2);
225  if (count($parts) === 2) {
226  $response['body'][$section] = substr($parts[1], 0, hexdec($parts[0]));
227  }
228  break;
229  }
230  }
231  }
232  }
233  return $response;
234  }
$data
Definition: storeScorm.php:23
$section
Definition: Utf8Test.php:83
$param
Definition: xapitoken.php:29
$response
+ Here is the caller graph for this function:

◆ reqMod()

ilVirusScannerICapRemote::reqMod (   $service,
  $body = [],
  $headers = [] 
)
Parameters
$service
array$body
array$headers
Returns
array

Definition at line 255 of file class.ilVirusScannerICapRemote.php.

References $response, $service, getRequest(), parseResponse(), and send().

256  {
257  $request = $this->getRequest('REQMOD', $service, $body, $headers);
258  $response = $this->send($request);
259  return $this->parseResponse($response);
260  }
$service
Definition: result.php:17
$response
getRequest($method, $service, $body=[], $headers=[])
+ Here is the call graph for this function:

◆ respMod()

ilVirusScannerICapRemote::respMod (   $service,
  $body = [],
  $headers = [] 
)
Parameters
$service
array$body
array$headers
Returns
array

Definition at line 242 of file class.ilVirusScannerICapRemote.php.

References $response, $service, getRequest(), parseResponse(), and send().

243  {
244  $request = $this->getRequest('RESPMOD', $service, $body, $headers);
245  $response = $this->send($request);
246  return $this->parseResponse($response);
247  }
$service
Definition: result.php:17
$response
getRequest($method, $service, $body=[], $headers=[])
+ Here is the call graph for this function:

◆ send()

ilVirusScannerICapRemote::send (   $request)
Parameters
$request
Returns
string

Definition at line 115 of file class.ilVirusScannerICapRemote.php.

References Vendor\Package\$e, $response, connect(), disconnect(), and getLastSocketError().

Referenced by options(), reqMod(), and respMod().

116  {
117  $response = '';
118  try{
119  $this->connect();
120  socket_write($this->socket, $request);
121  while ($buffer = socket_read($this->socket, 2048)) {
122  $response .= $buffer;
123  }
124  $this->disconnect();
125  } catch(ErrorException $e){
126  $this->log->warning("Cannot connect to icap://{$this->host}:{$this->port} (Socket error: " . $this->getLastSocketError() . ")");
127  }
128  return $response;
129  }
getLastSocketError()
Get last error code from socket object.
$response
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $host

ilVirusScannerICapRemote::$host
private

Definition at line 18 of file class.ilVirusScannerICapRemote.php.

Referenced by getRequest().

◆ $port

ilVirusScannerICapRemote::$port
private

Definition at line 20 of file class.ilVirusScannerICapRemote.php.

◆ $socket

ilVirusScannerICapRemote::$socket
private

Definition at line 22 of file class.ilVirusScannerICapRemote.php.

◆ $userAgent

ilVirusScannerICapRemote::$userAgent = 'PHP-CLIENT/0.1.0'

Definition at line 24 of file class.ilVirusScannerICapRemote.php.

Referenced by getRequest().


The documentation for this class was generated from the following file: