dolibarr  7.0.0-beta
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@capnetworks.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 <http://www.gnu.org/licenses/>.
19  * or see http://www.gnu.org/
20  */
21 
32 class AntiVir
33 {
34  var $error;
35  var $errors;
36  var $output;
37  var $db;
38 
44  function __construct($db)
45  {
46  $this->db=$db;
47  }
48 
57  function dol_avscan_file($file)
58  {
59  global $conf;
60 
61  $return = 0;
62 
63  if (preg_match('/\.virus$/i', $file))
64  {
65  $this->errors='File has an extension saying file is a virus';
66  return -97;
67  }
68 
69  $fullcommand=$this->getCliCommand($file);
70  //$fullcommand='"c:\Program Files (x86)\ClamWin\bin\clamscan.exe" --database="C:\Program Files (x86)\ClamWin\lib" "c:\temp\aaa.txt"';
71  $fullcommand.=' 2>&1'; // This is to get error output
72 
73  $output=array();
74  $return_var=0;
75  $safemode=ini_get("safe_mode");
76  // Create a clean fullcommand
77  dol_syslog("AntiVir::dol_avscan_file Run command=".$fullcommand." with safe_mode ".($safemode?"on":"off"));
78  // Run CLI command. If run of Windows, you can get return with echo %ERRORLEVEL%
79  $lastline=exec($fullcommand, $output, $return_var);
80 
81  //print "x".$lastline." - ".join(',',$output)." - ".$return_var."y";exit;
82 
83  /*
84  $outputfile=$conf->admin->dir_temp.'/dol_avscan_file.out.'.session_id();
85  $handle = fopen($outputfile, 'w');
86  if ($handle)
87  {
88  $handlein = popen($fullcommand, 'r');
89  while (!feof($handlein))
90  {
91  $read = fgets($handlein);
92  fwrite($handle,$read);
93  }
94  pclose($handlein);
95 
96  $errormsg = fgets($handle,2048);
97  $this->output=$errormsg;
98 
99  fclose($handle);
100 
101  if (! empty($conf->global->MAIN_UMASK))
102  @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
103  }
104  else
105  {
106  $langs->load("errors");
107  dol_syslog("Failed to open file ".$outputfile,LOG_ERR);
108  $this->error="ErrorFailedToWriteInDir";
109  $return=-1;
110  }
111  */
112 
113  dol_syslog("AntiVir::dol_avscan_file Result return_var=".$return_var." output=".join(',',$output));
114 
115  $returncodevirus=1;
116  if ($return_var == $returncodevirus) // Virus found
117  {
118  $this->errors=$output;
119  return -99;
120  }
121 
122  if ($return_var > 0) // If other error
123  {
124  $this->errors=$output;
125  return -98;
126  }
127 
128  // If return code = 0
129  return 1;
130  }
131 
132 
133 
140  function getCliCommand($file)
141  {
142  global $conf;
143 
144  $maxreclevel = 5 ; // maximal recursion level
145  $maxfiles = 1000; // maximal number of files to be scanned within archive
146  $maxratio = 200; // maximal compression ratio
147  $bz2archivememlim = 0; // limit memory usage for bzip2 (0/1)
148  $maxfilesize = 10485760; // archived files larger than this value (in bytes) will not be scanned
149 
150  $command=$conf->global->MAIN_ANTIVIRUS_COMMAND;
151  $param=$conf->global->MAIN_ANTIVIRUS_PARAM;
152 
153  $param=preg_replace('/%maxreclevel/',$maxreclevel,$param);
154  $param=preg_replace('/%maxfiles/',$maxfiles,$param);
155  $param=preg_replace('/%maxratio/',$maxratio,$param);
156  $param=preg_replace('/%bz2archivememlim/',$bz2archivememlim,$param);
157  $param=preg_replace('/%maxfilesize/',$maxfilesize,$param);
158  $param=preg_replace('/%file/',trim($file),$param);
159 
160  if (! preg_match('/%file/',$conf->global->MAIN_ANTIVIRUS_PARAM))
161  $param=$param." ".escapeshellarg(trim($file));
162 
163  if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command. Using escapeshellcmd fails.
164 
165  $ret=$command.' '.$param;
166  //$ret=$command.' '.$param.' 2>&1';
167  //print "xx".$ret."xx";exit;
168 
169  return $ret;
170  }
171 
172 }
173 
Class to scan for virus.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
__construct($db)
Constructor.
dol_avscan_file($file)
Scan a file with antivirus.
getCliCommand($file)
Get full Command Line to run.