dolibarr  16.0.5
antivir.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2000-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org>
4  * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  * or see https://www.gnu.org/
20  */
21 
30 class AntiVir
31 {
35  public $error = '';
36 
40  public $errors = array();
41 
45  public $output;
46 
50  public $db;
51 
57  public function __construct($db)
58  {
59  $this->db = $db;
60  }
61 
62  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
71  public function dol_avscan_file($file)
72  {
73  // phpcs:enable
74  global $conf;
75 
76  $return = 0;
77 
78  if (preg_match('/\.virus$/i', $file)) {
79  $this->errors[] = 'File has an extension saying file is a virus';
80  return -97;
81  }
82 
83  $fullcommand = $this->getCliCommand($file);
84  //$fullcommand='"c:\Program Files (x86)\ClamWin\bin\clamscan.exe" --database="C:\Program Files (x86)\ClamWin\lib" "c:\temp\aaa.txt"';
85  $fullcommand .= ' 2>&1'; // This is to get error output
86 
87  $output = array();
88  $return_var = 0;
89  $safemode = ini_get("safe_mode");
90  // Create a clean fullcommand
91  dol_syslog("AntiVir::dol_avscan_file Run command=".$fullcommand." with safe_mode ".($safemode ? "on" : "off"));
92  // Run CLI command. If run of Windows, you can get return with echo %ERRORLEVEL%
93  $lastline = exec($fullcommand, $output, $return_var);
94 
95  if (is_null($output)) {
96  $output = array();
97  }
98 
99  //print "x".$lastline." - ".join(',',$output)." - ".$return_var."y";exit;
100 
101  /*
102  $outputfile=$conf->admin->dir_temp.'/dol_avscan_file.out.'.session_id();
103  $handle = fopen($outputfile, 'w');
104  if ($handle)
105  {
106  $handlein = popen($fullcommand, 'r');
107  while (!feof($handlein))
108  {
109  $read = fgets($handlein);
110  fwrite($handle,$read);
111  }
112  pclose($handlein);
113 
114  $errormsg = fgets($handle,2048);
115  $this->output=$errormsg;
116 
117  fclose($handle);
118 
119  if (! empty($conf->global->MAIN_UMASK))
120  @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
121  }
122  else
123  {
124  $langs->load("errors");
125  dol_syslog("Failed to open file ".$outputfile,LOG_ERR);
126  $this->error="ErrorFailedToWriteInDir";
127  $return=-1;
128  }
129  */
130 
131  dol_syslog("AntiVir::dol_avscan_file Result return_var=".$return_var." output=".join(',', $output));
132 
133  $returncodevirus = 1;
134  if ($return_var == $returncodevirus) { // Virus found
135  $this->errors = $output;
136  return -99;
137  }
138 
139  if ($return_var > 0) { // If other error
140  $this->errors = $output;
141  return -98;
142  }
143 
144  // If return code = 0
145  return 1;
146  }
147 
148 
149 
156  public function getCliCommand($file)
157  {
158  global $conf;
159 
160  $maxreclevel = 5; // maximal recursion level
161  $maxfiles = 1000; // maximal number of files to be scanned within archive
162  $maxratio = 200; // maximal compression ratio
163  $bz2archivememlim = 0; // limit memory usage for bzip2 (0/1)
164  $maxfilesize = 10485760; // archived files larger than this value (in bytes) will not be scanned
165 
166  $command = $conf->global->MAIN_ANTIVIRUS_COMMAND;
167  $param = $conf->global->MAIN_ANTIVIRUS_PARAM;
168 
169  $param = preg_replace('/%maxreclevel/', $maxreclevel, $param);
170  $param = preg_replace('/%maxfiles/', $maxfiles, $param);
171  $param = preg_replace('/%maxratio/', $maxratio, $param);
172  $param = preg_replace('/%bz2archivememlim/', $bz2archivememlim, $param);
173  $param = preg_replace('/%maxfilesize/', $maxfilesize, $param);
174  $param = preg_replace('/%file/', trim($file), $param);
175 
176  if (!preg_match('/%file/', $conf->global->MAIN_ANTIVIRUS_PARAM)) {
177  $param = $param." ".escapeshellarg(trim($file));
178  }
179 
180  if (preg_match("/\s/", $command)) {
181  $command = escapeshellarg($command); // Use quotes on command. Using escapeshellcmd fails.
182  }
183 
184  $ret = $command.' '.$param;
185  //$ret=$command.' '.$param.' 2>&1';
186  //print "xx".$ret."xx";exit;
187 
188  return $ret;
189  }
190 }
AntiVir\__construct
__construct($db)
Constructor.
Definition: antivir.class.php:57
db
$conf db
API class for accounts.
Definition: inc.php:41
AntiVir\getCliCommand
getCliCommand($file)
Get full Command Line to run.
Definition: antivir.class.php:156
AntiVir
Class to scan for virus.
Definition: antivir.class.php:30
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
AntiVir\dol_avscan_file
dol_avscan_file($file)
Scan a file with antivirus.
Definition: antivir.class.php:71