<?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
// Moregroupware.org

$myEnv["module"] = "webmail2";
$myEnv["stdsmenu"] = false;
$myEnv["output"] = "html";

include("../../config.inc.php");
include(INCLUDEPATH . "container.inc.php");
include(WM2_INC_DIR.'virusscan_inc.php');

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$myvars = array("thefilename","msg_id","partno","save_in","account","message_id");
foreach ($myvars as $thevar) {
  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
}
$MGW = $_SESSION["MGW"];
$user_id = $MGW->userid;

$msg_no = $msg_id;


if ($account == 0) {
  if (!has_read_rights_mail($msg_no,$user_id)) { die("Access denied."); }

  $sql = "select * from mgw_webmail2_messages where message_id=".(int)$msg_no;
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  $row = $res->FetchRow();

  $ext = strtolower(substr($thefilename,-strpos(strrev($thefilename),'.')));
  if (isset($mime_types[$ext][0]) and $mime_types[$ext][0]=="img") { $addon = "img/"; } else {$addon = "attachments/";}

  $thenewfilename = CACHE_FOLDER.$addon.getdirnr($msg_id)."/".$row["message_id"]."-".$row["msg_cache"]."-".str_replace(".","_",$partno)."-".$thefilename;

} else {
  $ext = strtolower(substr($thefilename,-strpos(strrev($thefilename),'.')));
  if (isset($mime_types[$ext][0]) and $mime_types[$ext][0]=="img") { $addon = "img/"; } else {$addon = "attachments/";}

  $thenewfilename = CACHE_FOLDER."imap/".$addon.$user_id."-".$message_id."-".str_replace(".","_",$partno)."-".$thefilename;
}

if (file_exists($thenewfilename)) {
  $MGW->settings["wm2_antivirusprog"] = str_replace("\\","/",$MGW->settings["wm2_antivirusprog"]);
  $MGW->settings["wm2_antivirusprog"] = str_replace("//","/",$MGW->settings["wm2_antivirusprog"]);

  if (file_exists($MGW->settings["wm2_antivirusprog"])) {

	echo '
		<html><head><title>Webmail by Thomas Bley - Simple Groupware Solutions - more.groupware</title>
		<script>
			function showalert(text) {
				new_window = open(\'\', \'teammember_virusscan\', \'width=640,height=440,toolbar=no,menubar=no,scrollbars=yes\');
				new_window.document.open();
				new_window.document.write(text);
				new_window.document.close();
			}
		</script>
		</head>		
		<body leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0" link="#1F4687" vlink="#1F4687" alink="#1F4687">
		<center>
		<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%"><tr><td align="center" valign="middle">
		<img src="templates/default/media/virus2.gif" alt="" border="0"><br>
		'.$hashmap["virusscan_please_wait"].'
    ';
	flush();

    list($virusfound,$text) = scan_file($thenewfilename);
    
    if (!$virusfound) {
	  echo '
	    '.$hashmap["virusscan_finished"].'<br>
		<b>'.$hashmap["virusscan_no_viruses_were_found"].'</b><br>
		<a href="javascript:showalert(\''.addslashes($text).'\');">'.$hashmap["virusscan_show_logfile"].'</a><br>
	  ';
	  echo '
		'.$hashmap["virusscan_continue_download"].' <a href="'.($save_in!="files"?"download.php":"download2.php").'?message_id='.$message_id.'&account='.$account.'&thefilename='.urlencode($thefilename).'&msg_id='.$msg_id.'&partno='.$partno.'&'.SID.'">'.$hashmap["virusscan_click_here"].'</a>
		</td></tr></table>
		</center>
		</body></html>
	  ';
    } else {
	  echo '
	    '.$hashmap["virusscan_finished"].'<br>
		<font color="#ff0000"><b>'.$hashmap["virusscan_virus_found"].'</b></font><br>
		<a href="javascript:showalert();">'.$hashmap["virusscan_show_logfile"].'</a><br>
		<script>
		function showalert() {
		    mystring="";
			newWindow = window.open(mystring, \'teammember\', \'width=640,height=440,toolbar=no,menubar=no,scrollbars=yes\');
	  ';
	  echo 'newWindow.document.write("'.addslashes($text).'<br>");'."\n";
	  echo '		
		}
		</script>
		</td></tr></table>
		</center>
		</body></html>
	  ';
    }
  } else {
    die("<br><center>".$hashmap["webmail2_virus_scanner_not_found"]."</center>");
  }
} else {
  die("<br>".$hashmap["download_given_attachment_isnt_in_email"]."<br>".$hashmap["download_number_in_db"].$msg_no."<br>partno=".$partno."<br>".$hashmap["download_filename_disabled"]);
}
?>
