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

// Operations
// 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("action","account","msg_no","check");
foreach ($myvars as $thevar) {
  ${$thevar} = (isset($_GET[$thevar])) ? $_GET[$thevar] : (isset($_POST[$thevar]) ? $_POST[$thevar] : "");
}
$MGW = $_SESSION["MGW"];
$user_id = $MGW->userid;
$username = $MGW->username;

require_once(INCLUDEPATH . "crypt.inc.php");
require_once(WM2_INC_DIR.'imap/imap.php');
require_once(WM2_INC_DIR.'class.pop3.php');

$myC = new Crypto();

$sql = "select * from mgw_webmail2_accounts where mailboxname='".addslashes($account)."' and user_id=".(int)$user_id;
if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
$row = $res->FetchRow();
    
if (isset($row["servertype"]) and $row["servertype"]!="") {
  if($row["servertype"]=="wm2_servertype_pop3") {
    $my_imap = "ext";
    $default_protocol = "pop3";

    if ($row["enable_ssl"]=="checked") {
      $default_port = "995";
    } else {
      $default_port = "110";
	}
  } elseif($row["servertype"]=="wm2_servertype_imap") {
    $my_imap = "int";
    $default_protocol = "imap";

    if ($row["enable_ssl"]=="checked") {
      $default_port = "993";
    } else {
      $default_port = "143";
	}
  }
   
  $password = $myC->decrypt($username, $row["password"]);

  @set_time_limit(30);
  $email = my_imap_connect($my_imap,$default_port,$default_protocol,$row,$password,"INBOX");
  $imap_conn = $email;

  $fetch_host = $row["pophost"];
  $fetch_username = $row["username"];
  
  if (($my_imap=="int" and (!isset($imap_conn_error) or $imap_conn_error=="")) or ($my_imap=="ext" and (!isset($email->error) or $email->error==""))) {
    $header = my_imap_header($email,$imap_conn,$my_imap,$msg_no);
		
    if (strlen($header->message_id) < 10) {
      if (!isset($header->fromaddress)) {$header->fromaddress="";}
      if (!isset($header->subject)) {$header->subject="";}
      if (!isset($header->toaddress)) {$header->toaddress="";}
      if (!isset($header->date)) {$header->date="";}
	  $header->toaddress = trim($header->toaddress);

	  $check2 = crc32(decode_mime_string(trim($header->subject)).decode_mime_string(trim($header->toaddress)).decode_mime_string(trim($header->fromaddress)).$header->date);
    } else {
      $check2 = $header->message_id;
    }
// echo $check."\n"; echo $check2;
    if ($check2 == $check) {
	  my_imap_delete($email,$imap_conn,$my_imap,$msg_no);
    }

    my_imap_close($email,$imap_conn,$my_imap);

    if (isset($imap_conn_error) and $imap_conn_error!="") {
      $error = $imap_conn_error;
    }
    if (isset($email->error) and $email->error!="") {
      $error = $email->error;
    }
  }
}
Header("Location: fetchmail_small.php?show=true&".SID);
?>
