<?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
*/

// Download
// Moregroupware.org

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

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

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$myvars = array("thefilename","msg_id","partno","account","message_id","save_in");
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;
}

$ext = strtolower(substr($thenewfilename,-strpos(strrev($thenewfilename),'.')));
  
$showlink = true;
if (trim($MGW->settings["wm2_disable_ext"])!="") {
  $myext = explode(",",strtolower(trim($MGW->settings["wm2_disable_ext"])));
  foreach ($myext as $ext2) {
    $ext2 = trim(str_replace(array("\r","\n"),array("",""),$ext2));
    if ($ext2 == $ext) {
      $showlink = false;
      break;
    }
  }
}

if ($showlink and file_exists($thenewfilename)) {

  $id = mgw_genID('mgw__seq_files');
  $id2 = 0;
  $all_rights = 0;

  $filesize = filesize($thenewfilename);
  $mime_type = "application/octet-stream";

  if (!file_exists(STORE_FOLDER."../files/".$id2."-".$thefilename)) {
    copy($thenewfilename,STORE_FOLDER."../files/".$id2."-".$thefilename);
      
    $sql="INSERT INTO mgw_files (id, type, resides_in, owner, name, size, mime_type, owner_rights, all_rights, created, modified) VALUES ($id, 'f', ".$id2.", ".$_SESSION["MGW"]->userid.", ".$conn->Quote($thefilename).", ".(int)$filesize.", '$mime_type', 7, $all_rights, ".$conn->DBTimeStamp(time()).", ".$conn->DBTimeStamp(time()).")";
    if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

    $sql="SELECT groupid FROM mgw_groups_members WHERE userid=".$_SESSION["MGW"]->userid;
    if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    
    while($res_arr = $res->FetchRow()) {
	  $gid = $res_arr["groupid"];

      $grid = mgw_genID('mgw__seq_files_grouprights');
	  $sql="INSERT INTO mgw_files_grouprights (id, for_file, for_group, rights) VALUES ($grid, $id, $gid, 0)";
      if(!$conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    }
  } else {
    die("file already exists.");
  }
    
  header('Location: ../files/index.php?chgDirId='.$id2.'&'.SID);
      
} 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"]);
}
?>
