<?php
/*
This file is part of Thomas Bley's moregroupware webmail2/calendar2/admin2 module. (tb-mgw)

More information:
-----------------

Simple Groupware Solutions Thomas Bley
Fichtenstr. 23
Germany, 85640 Putzbrunn

Contact:
--------

info@simple-groupware.de

http://www.simple-groupware.de


Copyright 2002-2003 Thomas Bley
tb-mgw is distributed under the terms of the GNU General Public License 

tb-mgw is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

tb-mgw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with tb-mgw; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Virusscan-inc
// Moregroupware.org


function scan_file($filename) {
  global $MGW;

  $safetmpfname = tempnam ("", "mgw_wm2_checkviruses_");

  if ($MGW->settings["wm2_antivirusprogoptions"]=="wm2_virusscanner_f-prot_linux") {
    $antivirusprogphrase = "No viruses or suspicious files/boot sectors were found.";
	$cmd = "exec 2>&1 && ".$MGW->settings["wm2_antivirusprog"]." -ai -archive -packed -dumb \"".$filename."\" >".$safetmpfname;
  }
  if ($MGW->settings["wm2_antivirusprogoptions"]=="wm2_virusscanner_f-prot_win32") {
	$antivirusprogphrase = "No viruses or suspicious files/boot sectors were found.";
	$cmd = $MGW->settings["wm2_antivirusprog"]." /ai /archive /packed /dumb \"".$filename."\" >".$safetmpfname;
  }
  if ($MGW->settings["wm2_antivirusprogoptions"]=="wm2_virusscanner_mcafee_win32") {
	$antivirusprogphrase = "Possibly Infected: .....       0";
	$cmd = $MGW->settings["wm2_antivirusprog"]." /report ".str_replace("/","\\",$safetmpfname)." \"".str_replace("/","\\",$filename)."\"";
  }
  if ($MGW->settings["wm2_antivirusprogoptions"]=="wm2_virusscanner_mcafee_linux") {
	$antivirusprogphrase = "Possibly Infected: .....       0";
	$cmd = "exec 2>&1 && ".$MGW->settings["wm2_antivirusprog"]." --summary \"".$filename."\" >".$safetmpfname;
  }
  if ($MGW->settings["wm2_antivirusprogoptions"]=="wm2_virusscanner_kav_linux") {
	$antivirusprogphrase = "Known viruses :      0";
	$cmd = "exec 2>&1 && ".$MGW->settings["wm2_antivirusprog"]." \"".$filename."\" >".$safetmpfname;
  }
  if ($MGW->settings["wm2_antivirusprogoptions"]=="wm2_virusscanner_sophos_win32") {
	$antivirusprogphrase = "No viruses were discovered.";
	$cmd = $MGW->settings["wm2_antivirusprog"]." -nb  -dn -nc -archive \"".$filename."\" >".$safetmpfname; 
  }  
  if ($MGW->settings["wm2_antivirusprogoptions"]=="wm2_virusscanner_clam_linux") {
	$antivirusprogphrase = "Infected files: 0";
	$cmd = "exec 2>&1 && ".$MGW->settings["wm2_antivirusprog"]." --unzip --unrar --arj --lha --unzoo --deb --tgz --tar -i --quiet --log=".$safetmpfname." \"".$filename."\"";
  }
  passthru($cmd);
  
  $fp = fopen($safetmpfname, "r");
  $text = fread ($fp, filesize ($safetmpfname));
  fclose($fp);
  unlink($safetmpfname);

  if (!strpos($text, $antivirusprogphrase) === false) {
    $virusfound = false;
  } else {
    $virusfound = true;
  }

  $text = virusscan_report_replace($text);

  return array($virusfound,$text);
}

function virusscan_report_replace($text) {
  
  $text = str_replace("\\","/",$text);
  $text = preg_replace("/(\015\012)|(\015)|(\012)/","\n",$text);

  $text = str_replace("\n","<br>",htmlspecialchars(wordwrap($text,80,"\n",1)));
 
  return $text;
}
    


?>
