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

// Fetchmail
// Moregroupware.org

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

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

error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
 
$myvars = array("show");
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;

if (isModuleInstalled("webmail2")) {

$havefetched_nr = 0;
$havefetched = array();

if (!isset($MGW->settings["wm2_fetchnotifytime"])) { 
  $waittime = 120;
} else {
  $waittime = $MGW->settings["wm2_fetchnotifytime"];
}

if (!isset($MGW->settings["wm2_fetchnotifytime2"])) { 
  $MGW->settings["wm2_fetchnotifytime2"] = time();
}

if (isset($MGW->settings["wm2_fetchnotifytime3"])) { 
  $havefetched_nr = $MGW->settings["wm2_fetchnotifytime3"];
}

$inbox = getboxid("inbox",0,$user_id);
if ($inbox=="") {
  create_boxes(array(array("inbox",0,$user_id)));
  $inbox = getboxid("inbox",0,$user_id);
}
$inbox_unread = getmessagecount_unread($inbox);

if (isset($MGW->settings["wm2_enablefetchnotify"]) and $MGW->settings["wm2_enablefetchnotify"] and ((($MGW->settings["wm2_fetchnotifytime2"]+$MGW->settings["wm2_fetchnotifytime"]) <= time()) or ($show=="true"))) {
  $havefetched_nr = 0;
  
  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();

  $MGW->settings["wm2_fetchnotifytime2"] = time();
  $waittime = $MGW->settings["wm2_fetchnotifytime"];
  $sql = "select * from mgw_webmail2_accounts where mailboxname!='' and activated='checked' and servertype!='wm2_servertype_imap_only' and user_id=".(int)$user_id;
  if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__,__FILE__));
  while($row = $res->FetchRow()) {

    $my_imap = "int";
    $default_protocol = "imap";
    $default_port = "143";

    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") {
      if ($row["enable_ssl"]=="checked") {
        $default_port = "993";
      }
    }
   
    $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==""))) {

	  $popcount = my_imap_get_num_messages($email,$imap_conn,$my_imap);

      $registerleaves = false;
	  $leavemessages = array();
      
      if ($row["leavemessages"]=="checked") {
	    $leavemessages = explode(",",$row["leaveoldmails"]);
	    $registerleaves = true;
	    $keepmessages = true;
	  } else {$keepmessages = false;}

	  $msg_no = 0;
	
      while ($popcount!=0) {
        $popcount = $popcount-1;
        $msg_no++;

	    $header = my_imap_header($email,$imap_conn,$my_imap,$msg_no);
		
	    $header->message_id = getmessage_id($header);

        $oversize = false;
        if ($MGW->settings["wm2_fetch_limit"] != "" and is_numeric($MGW->settings["wm2_fetch_limit"]) and $MGW->settings["wm2_fetch_limit"]!=0 and ($MGW->settings["wm2_fetch_limit"]*1024) < $header->Size) {
          $oversize = true;
        }

	    if (!$oversize and (!$keepmessages or !in_array(str_replace(",",".",$header->message_id),$leavemessages))) {

          if (!isset($header->fromaddress)) {$header->fromaddress="";}
          if (!isset($header->subject)) {$header->subject="";}
          if (!isset($header->toaddress)) {$header->toaddress="";}
          if (!isset($header->date)) {$header->date="";}
          if (!isset($header->priority) or $header->priority==3) {$header->priority="";}
		  $header->toaddress = trim($header->toaddress);

          $check = $header->message_id;
          
          $header->fromaddress = shorten2($header->fromaddress);
          $header->fromaddress = shorten(htmlspecialchars(decode_mime_string(stripslashes($header->fromaddress))));
          $header->toaddress = shorten2($header->toaddress);
          $header->toaddress = shorten(htmlspecialchars(decode_mime_string(stripslashes($header->toaddress))));

		  $header->subject = decode_mime_string(str_replace(array("&gt;", "&lt;","&quot;", "&amp;"), array(">","<", "\"", "&"), $header->subject));
          if (isset($header->Size)) {$size = $header->Size;} else {$size = "0";}

		  if ($size!=0 and $size!="") {$size = format_filesize($size);}  
	      $havefetched[$row["mailboxname"]][] = array($header->fromaddress,$header->subject,$header->toaddress,$header->priority,$size,$check,$msg_no);
		  $havefetched_nr++;
	    }
 	  }
	  my_imap_close($email,$imap_conn,$my_imap);
    }
    if (isset($imap_conn_error) and $imap_conn_error!="") {$havefetched[$row["mailboxname"]][] = $imap_conn_error;}
    if (isset($email->error) and $email->error!="") {$havefetched[$row["mailboxname"]][] = $email->error;}
  }

  $MGW->settings["wm2_fetchnotifytime3"] = $havefetched_nr;
}

if (isset($MGW->settings["wm2_enablefetchnotify"]) and $MGW->settings["wm2_enablefetchnotify"]) {
  if ($havefetched_nr != 0) {
    $body = '<a class="navlink" style="font-size:90%;" onclick="fetchmails(); return false;" href="#">E-Mail ('.$inbox_unread.'/'.$havefetched_nr.')</a>';
  } else {
    $body = '<a class="navlink" style="font-size:90%;" onclick="showmails(); return false;" href="#">E-Mail ('.$inbox_unread.'/'.$havefetched_nr.')</a>';
  }
} else {
  $body = '<a class="navlink" style="font-size:90%;" onclick="showmails(); return false;" href="#">E-Mail ('.$inbox_unread.')</a>';
}


if ($show=="true") {
  $bigtable = "";
  foreach ($havefetched as $key=>$havefetched2) {
    $bigtable .= "<tr bgcolor='#e4e4e4'><td>".$key."</td>";
    $first = true;
    foreach ($havefetched2 as $havefetched3) {
      if ($first) {$first = false;} else {$bigtable .= "<tr bgcolor='#e4e4e4'><td>&nbsp;</td>";}
      if (isset($havefetched3) and is_array($havefetched3)) {
	    foreach ($havefetched3 as $key2=>$havefetched4) {
          if ($key2 <5) {
	      $bigtable .= "<td>&nbsp;</td><td bgcolor='#e4e4e4'>".$havefetched4."</td>";
        }
	  }
	  }
	  $bigtable .= '<td>&nbsp;</td><td><a href="operations.php?action=delete&account='.$key.'&check='.urlencode($havefetched3[5]).'&msg_no='.$havefetched3[6].'&'.SID.'"><img src="templates/default/media/n_mini_delete.gif" alt="" width="11" height="11" border="0"></a></td></tr>';
    }
  }

  $smarty->assign("myimgpathmaster", "templates/default/media");
  $smarty->assign("bigtable", $bigtable);

  $smarty->display("fetchmail_small.tpl");
} else {
  $smarty->assign("myimgpathmaster", "templates/default/media");
  $smarty->assign("waittime", $waittime);
  $smarty->assign("body", $body);

  $smarty->display("fetchmail_small2.tpl");
}
} else {
  $smarty->display("fetchmail_small2.tpl");
}
?>