dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
32{
36 public $error = '';
37
41 public $errors = array();
42
46 public $output;
47
51 public $db;
52
58 public function __construct($db)
59 {
60 $this->db = $db;
61 }
62
63 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
72 public function dol_avscan_file($file)
73 {
74 // phpcs:enable
75 global $conf;
76
77 if (preg_match('/\.virus$/i', $file)) {
78 $this->errors[] = 'File has an extension saying file is a virus';
79 return -97;
80 }
81
82 $fullcommand = $this->getCliCommand($file);
83 //$fullcommand="/usr/bin/clamdscan --fdpass '/tmp/phpuxoAEo'"
84 //$fullcommand='"c:\Program Files (x86)\ClamWin\bin\clamscan.exe" --database="C:\Program Files (x86)\ClamWin\lib" "c:\temp\aaa.txt"';
85 //var_dump($fullcommand);
86
87 $safemode = ini_get("safe_mode");
88 // Create a clean fullcommand
89 dol_syslog("AntiVir::dol_avscan_file Run command=".$fullcommand." with safe_mode ".($safemode ? "on" : "off"));
90 // Run CLI command.
91 include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
92 $utils = new Utils($this->db);
93 $outputfile = $conf->user->dir_temp.'/antivir.tmp';
94
95 $result = $utils->executeCLI($fullcommand, $outputfile);
96
97 $return_var = $result['result'];
98 $output = $result['output'];
99 $errorstring = $result['error'];
100
101 if (is_null($output)) {
102 $output = array();
103 }
104
105 dol_syslog("AntiVir::dol_avscan_file Result return_var=".$return_var." output=".$output);
106
107 $returncodevirus = 1;
108 if ($return_var == $returncodevirus) { // Virus found
109 $this->errors = array($errorstring, $output);
110 return -99;
111 }
112
113 if ($return_var > 0) { // If other error
114 $this->errors = array($errorstring, $output);
115 return -98;
116 }
117
118 // If return code = 0
119 return 1;
120 }
121
122
123
130 public function getCliCommand($file)
131 {
132 $maxreclevel = 5; // maximal recursion level
133 $maxfiles = 1000; // maximal number of files to be scanned within archive
134 $maxratio = 200; // maximal compression ratio
135 $bz2archivememlim = 0; // limit memory usage for bzip2 (0/1)
136 $maxfilesize = 10485760; // archived files larger than this value (in bytes) will not be scanned
137
138 $command = getDolGlobalString('MAIN_ANTIVIRUS_COMMAND');
139 $param = getDolGlobalString('MAIN_ANTIVIRUS_PARAM');
140
141 $param = preg_replace('/%maxreclevel/', (string) $maxreclevel, $param);
142 $param = preg_replace('/%maxfiles/', (string) $maxfiles, $param);
143 $param = preg_replace('/%maxratio/', (string) $maxratio, $param);
144 $param = preg_replace('/%bz2archivememlim/', (string) $bz2archivememlim, $param);
145 $param = preg_replace('/%maxfilesize/', (string) $maxfilesize, $param);
146 $param = preg_replace('/%file/', trim($file), $param);
147
148 if (!preg_match('/%file/', getDolGlobalString('MAIN_ANTIVIRUS_PARAM'))) {
149 $param = $param." ".escapeshellarg(trim($file));
150 }
151
152 if (preg_match("/\s/", $command)) {
153 $command = escapeshellarg($command); // Force use of quotes on command. Using escapeshellcmd fails.
154 }
155
156 $forbidden_chars_to_replace = array("*", "?", "\"", "<", ">", "|", "[", "]", ";", '°', '$');
157 $ret = dol_sanitizePathName($command).' '.dol_string_nospecial($param, '_', $forbidden_chars_to_replace);
158
159 //$ret=$command.' '.$param.' 2>&1';
160 //print "xx".$ret."xx";exit;
161
162 return $ret;
163 }
164}
Class to scan for virus.
__construct($db)
Constructor.
dol_avscan_file($file)
Scan a file with antivirus.
getCliCommand($file)
Get full Command Line to run.
Class to manage utility methods.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.