PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/antivir.class.php

https://github.com/asterix14/dolibarr
PHP | 167 lines | 60 code | 25 blank | 82 comment | 5 complexity | e47c72166a14a05e553a42357127aa38 MD5 | raw file
Possible License(s): LGPL-2.0
  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@dolibarr.fr>
  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 2 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. /**
  22. * \file htdocs/core/class/antivir.class.php
  23. * \brief File of class to scan viruses
  24. * \author Laurent Destailleur.
  25. */
  26. /**
  27. * \class AntiVir
  28. * \brief Class to scan for virus
  29. */
  30. class AntiVir
  31. {
  32. var $error;
  33. var $errors;
  34. var $output;
  35. var $db;
  36. /**
  37. * Constructor
  38. *
  39. * @param DoliDB $db Database handler
  40. * @return AntiVir
  41. */
  42. function AntiVir($db)
  43. {
  44. $this->db=$db;
  45. }
  46. /**
  47. * Scan a file with antivirus.
  48. * This function runs the command defined in setup. This antivirus command must return 0 if OK.
  49. *
  50. * @param file File to scan
  51. * @return int <0 if KO (-98 if error, -99 if virus), 0 if OK
  52. */
  53. function dol_avscan_file($file)
  54. {
  55. global $conf;
  56. $return = 0;
  57. $fullcommand=$this->getCliCommand($file);
  58. //$fullcommand='"c:\Program Files (x86)\ClamWin\bin\clamscan.exe" --database="C:\Program Files (x86)\ClamWin\lib" "c:\temp\aaa.txt"';
  59. $fullcommand.=' 2>&1'; // This is to get error output
  60. $output=array();
  61. $return_var=0;
  62. $safemode=ini_get("safe_mode");
  63. // Create a clean fullcommand
  64. dol_syslog("AntiVir::dol_avscan_file Run command=".$fullcommand." with safe_mode ".($safe_mode?"on":"off"));
  65. // Run CLI command. If run of Windows, you can get return with echo %ERRORLEVEL%
  66. $lastline=exec($fullcommand, $output, $return_var);
  67. //print "x".$lastline." - ".join(',',$output)." - ".$return_var."y";exit;
  68. /*
  69. $outputfile=$conf->admin->dir_temp.'/dol_avscan_file.out.'.session_id();
  70. $handle = fopen($outputfile, 'w');
  71. if ($handle)
  72. {
  73. $handlein = popen($fullcommand, 'r');
  74. while (!feof($handlein))
  75. {
  76. $read = fgets($handlein);
  77. fwrite($handle,$read);
  78. }
  79. pclose($handlein);
  80. $errormsg = fgets($handle,2048);
  81. $this->output=$errormsg;
  82. fclose($handle);
  83. if (! empty($conf->global->MAIN_UMASK))
  84. @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  85. }
  86. else
  87. {
  88. $langs->load("errors");
  89. dol_syslog("Failed to open file ".$outputfile,LOG_ERR);
  90. $this->error="ErrorFailedToWriteInDir";
  91. $return=-1;
  92. }
  93. */
  94. dol_syslog("AntiVir::dol_avscan_file Result return_var=".$return_var." output=".join(',',$output));
  95. $returncodevirus=1;
  96. if ($return_var == $returncodevirus) // Virus found
  97. {
  98. $this->errors=$output;
  99. return -99;
  100. }
  101. if ($return_var > 0) // If other error
  102. {
  103. $this->errors=$output;
  104. return -98;
  105. }
  106. // If return code = 0
  107. return 1;
  108. }
  109. /**
  110. * \brief get full Command Line to run
  111. * \param file File to scan
  112. * \return string Full command line to run
  113. */
  114. function getCliCommand($file)
  115. {
  116. global $conf;
  117. $maxreclevel = 5 ; // maximal recursion level
  118. $maxfiles = 1000; // maximal number of files to be scanned within archive
  119. $maxratio = 200; // maximal compression ratio
  120. $bz2archivememlim = 0; // limit memory usage for bzip2 (0/1)
  121. $maxfilesize = 10485760; // archived files larger than this value (in bytes) will not be scanned
  122. $command=$conf->global->MAIN_ANTIVIRUS_COMMAND;
  123. $param=$conf->global->MAIN_ANTIVIRUS_PARAM;
  124. $param=preg_replace('/%maxreclevel/',$maxreclevel,$param);
  125. $param=preg_replace('/%maxfiles/',$maxfiles,$param);
  126. $param=preg_replace('/%maxratio/',$maxratio,$param);
  127. $param=preg_replace('/%bz2archivememlim/',$bz2archivememlim,$param);
  128. $param=preg_replace('/%maxfilesize/',$maxfilesize,$param);
  129. $param=preg_replace('/%file/',trim($file),$param);
  130. if (! preg_match('/%file/',$conf->global->MAIN_ANTIVIRUS_PARAM))
  131. $param=$param." ".escapeshellarg(trim($file));
  132. if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command. Using escapeshellcmd fails.
  133. $ret=$command.' '.$param;
  134. //$ret=$command.' '.$param.' 2>&1';
  135. //print "xx".$ret."xx";exit;
  136. return $ret;
  137. }
  138. }
  139. ?>