<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

include_once("auth.php");

if(array_key_exists('mailnum', $_GET))
{
	setcookie('mailnum', $_GET['mailnum'] * 1, time() + (3600 * 24 * 365));
	$_COOKIE['mailnum'] = $_GET['mailnum'] * 1;
}


include("email_functions.php");
include("email_config.php");
include("email_header.php");


$id = safe_query2 ( "SELECT 1 FROM pop3 WHERE account=? AND neu=1", new SQlString($cnf_user) );
if ( db_fetch_array($id) ){
    echo ( ltext("wmail_notcreated") );
    exit();
}

/* POP3-Connect: */
if (!e_connect()) { 
echo ltext("wmail_pop3");
exit();
}

$numMessages = imap_num_msg($mbox);
$numkey = 0;
if($numMessages == 0)
{
	echo $lang_text["wbm_msglist_nomails"];
}
else
{
	$id = safe_query2( "SELECT admin.spamfilter AS adm_spam, kunden.spamfilter AS user_spam,".
										 " kunden.list_size AS list_size FROM admin, kunden WHERE kunde=?",
										 new SQLString( $kunde ) );
	$res = db_fetch_array($id);
	//$pagesize = $res['list_size'];
	$adm_spam = $res['adm_spam'];
	$user_spam = $res['user_spam'];
	
	if(array_key_exists('mailnum', $_COOKIE))
	{
		$pagesize = $_COOKIE['mailnum'] * 1;
	}
	
	if(!$pagesize)
	{
		$pagesize = 20;	
	}
	
	if(!$page)
	{
		$page = 0;	
	}
	
	$page_start = $page * $pagesize;
	$page_end = ($page + 1) * $pagesize - 1;
	for($i = $numMessages; $i >= 1; $i--)
	{
		$showit = true;
		//$ht = imap_fetchheader($mbox, $i, FT_PREFETCHTEXT);
		if($adm_spam && $user_spam && array_key_exists('show', $_GET))
		{
			//$re = '/^X-Spam-Flag\: YES$/';
			//echo $re;
			//$showit = preg_match('/X-Spam-Flag\: YES/', imap_fetchheader($mbox, $i, FT_PREFETCHTEXT));

			if($show == 'spam')
			{
				$showit = preg_match('/X-Spam-Flag\: YES/', imap_fetchheader($mbox, $i, FT_PREFETCHTEXT));	
			}
			
			if($show == 'nospam')
			{
				$showit = !preg_match('/X-Spam-Flag\: YES/', imap_fetchheader($mbox, $i, FT_PREFETCHTEXT));	
			}
		}
		
		
		if($showit)
		{
			$header = imap_headerinfo($mbox, $i);
			$subject = $header->subject;
			$date = dateFormat(strtotime($header->date),"datetime");
			$bodystruct = imap_fetchstructure($mbox, $i);
			$size = $bodystruct->bytes;
			$sortdate = dateFormat(strtotime($header->date),"sortdate");
			
			//$struct_header = headerArray($bodystruct);
			//if($struct_header["parameters"]["charset"])
			//{
			$subject = decode_subject($subject);
			//}
			
			//print_r($struct_header);
			
			$fromname = $header->from[0]->personal;
			$host = $header->from[0]->host;
			$mailbox = $header->from[0]->mailbox;
			if ($host == ".MISSING-HOST-NAME.")
			{
				$host="localhost";
			}
			$fromaddress = "<".$mailbox . "@" . $host.">";
			
			$sender = (isset($fromaddress)) ? $fromname." ".$fromaddress : $fromname;
			$sender = htmlspecialchars(mimeHeaderDecode($sender));
			
			/* Generate Messagelist ($msglist): */
			$msglist["msgid"][$numkey] = $i;
			$msglist["subject"][$numkey] = htmlentities(strtolower($subject));
			$msglist["subject_output"][$numkey] = htmlentities($subject);
			if($msglist["subject_output"][$numkey] == "")
			{
				$msglist["subject_output"][$numkey] = "[".$lang_text["wbm_nosubject"]."]";
			}
			$msglist["sender"][$numkey] = htmlentities(strtolower($sender));
			$msglist["sender_output"][$numkey]  = $sender;
			$msglist["date"][$numkey] = $sortdate;
			$msglist["date_output"][$numkey] = $date;
			$msglist["size"][$numkey] = $size;
			$msglist["size_output"][$numkey] = sizeFormat($size);
			$msglist["status1"][$numkey] = $status1;
			$msglist["status2"][$numkey] = $status2;
			$numkey++;
		}
	}
	
	$numMessages = $numkey;
	
	if($page_end > $numMessages)
	{
		$page_end = $numMessages - 1;
	}
	$pages = ceil($numMessages / $pagesize); 
	
	//echo "-- $numkey -- $numMessages --";
	
	/* Sort generated Messaglist */
	if($orderby == "subject" || $orderby == "sender" || $orderby == "date" || $orderby == "size")
	{
		if($sort == "" && $sort != "desc")
		{
			// ASC
			$sort = "asc";
			array_multisort($msglist["$orderby"], SORT_REGULAR, SORT_ASC, $msglist["msgid"], $msglist["subject"], $msglist["subject_output"], $msglist["sender"], $msglist["sender_output"], $msglist["date"], $msglist["date_output"], $msglist["size"], $msglist["size_output"]);
			$sortlink["$orderby"] = "email_postfach.php?orderby=$orderby&sort=desc" . (array_key_exists('show', $_GET)?"&show=$show":'');
		}
		if($sort == "desc")
		{
			// DESC
			$sort = "desc";
			array_multisort($msglist["$orderby"], SORT_REGULAR, SORT_DESC, $msglist["msgid"], $msglist["subject"], $msglist["subject_output"], $msglist["sender"], $msglist["sender_output"], $msglist["date"], $msglist["date_output"], $msglist["size"], $msglist["size_output"]);
			$sortlink["$orderby"] = "email_postfach.php?orderby=$orderby" . (array_key_exists('show', $_GET)?"&show=$show":'');
		}
	}
?>

  <form action="email_postfach.php" name="sel_spam" method="get">
  <table class="tblbgcolor" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
  <tr>
  <? if($adm_spam && $user_spam): ?>
  <td valign="middle"><SELECT name="show">
  <OPTION value="all"<?=(($show == 'all' || !$show)?' selected':'')?>><?=ltext('wbm_show_all')?>
  <OPTION value="spam"<?=($show == 'spam'?' selected':'')?>><?=ltext('wbm_show_spam')?>
  <OPTION value="nospam"<?=($show == 'nospam'?' selected':'')?>><?=ltext('wbm_show_nospam')?>
  </select></td>
  <? endif; ?>
  <td valign="middle"><SELECT name="mailnum">
  <OPTION value="20"<?=(($pagesize == 20 || !$pagesize)?' selected':'')?>><?=ltext('wbm_show_page20')?>
  <OPTION value="50"<?=(($pagesize == 50)?' selected':'')?>><?=ltext('wbm_show_page50')?>
  <OPTION value="100"<?=(($pagesize == 100)?' selected':'')?>><?=ltext('wbm_show_page100')?>
  <OPTION value="100000000"<?=(($pagesize == 100000000)?' selected':'')?>><?=ltext('wbm_show_pageall')?>
  </select></td>
  <td valign="middle"><?=std_button(ltext('wbm_show'), 'javascript:document.forms[\'sel_spam\'].submit()')?></td>
  </tr>
  </table>
  </form>
  <form action="email_delete.php" name="del_form" method="post">
  <table width="100%" class="tblbgcolor" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
  <tr><td>
  <? echo ltext('wbm_msglist_status', array(($page_end - $page_start + 1), $numMessages)); 
  if ($numMessages > 0) {
  ?>
  <table width="100%" class="tblbgcolor" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
  <tr>
  <td background="<?=$strSkinName?>/pics/bg_tbl_header.jpg">&nbsp;</td>
  <td background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><a class="data" href="<? if($sortlink["subject"]) { echo $sortlink["subject"]; } else { echo "email_postfach.php?orderby=subject" . (array_key_exists('show', $_GET)?"&show=$show":''); } ?>"><? echo $lang_text["wbm_subject"] ?><? if($sortlink["subject"]) { ?><img src="<?=$strSkinName?>/webmail_icons/<? echo $sort ?>.gif" border="0"><? } ?></a></td>
  <td background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><a class="data" href="<? if($sortlink["sender"]) { echo $sortlink["sender"]; } else { echo "email_postfach.php?orderby=sender" . (array_key_exists('show', $_GET)?"&show=$show":''); } ?>"><? echo $lang_text["wbm_sender"] ?><? if($sortlink["sender"]) { ?><img src="<?=$strSkinName?>/webmail_icons/<? echo $sort ?>.gif" border="0"><? } ?></a></td>
  <td background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><a class="data" href="<? if($sortlink["date"]) { echo $sortlink["date"]; } else { echo "email_postfach.php?orderby=date" . (array_key_exists('show', $_GET)?"&show=$show":''); } ?>"><? echo $lang_text["wbm_date"] ?><? if($sortlink["date"]) { ?><img src="<?=$strSkinName?>/webmail_icons/<? echo $sort ?>.gif" border="0"><? } ?></a></td>
  <td background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><a class="data" href="<? if($sortlink["size"]) { echo $sortlink["size"]; } else { echo "email_postfach.php?orderby=size" . (array_key_exists('show', $_GET)?"&show=$show":''); } ?>"><? echo $lang_text["wbm_size"] ?><? if($sortlink["size"]) { ?><img src="<?=$strSkinName?>/webmail_icons/<? echo $sort ?>.gif" border="0"><? } ?></a></td>
  </tr>
<?
	//for($i = 0; $i < $numMessages; $i++)
	for($i = $page_start; $i <= $page_end; $i++)
	{
		//if($i < $page_start || $i > $page_end)
		//{
		//	continue;
		//}
?>
    <tr>
    <td><input type="checkbox" name="ids[]" id="ids" value="<? echo $msglist["msgid"][$i] ?>"></td>
    <td><a class="data" href="email_message.php?id=<? echo $msglist["msgid"][$i] ?>"><? echo $msglist["status1"][$i] ?><? echo $msglist["status"][$i] ?><? echo $msglist["subject_output"][$i] ?><? echo $msglist["status2"][$i] ?></a></td>
    <td><? echo $msglist["status1"][$i] ?><? echo $msglist["sender_output"][$i] ?><? echo $msglist["status2"][$i] ?></td>
    <td><? echo $msglist["status1"][$i] ?><? echo $msglist["date_output"][$i] ?><? echo $msglist["status2"][$i] ?></td>
    <td><? echo $msglist["status1"][$i] ?><? echo $msglist["size_output"][$i]?><? echo $msglist["status2"][$i] ?></td>
    </tr>
<?
	}
?>
  </td></tr>
  </table>
  
  <table border="0" width="100%">
  <tr>
  <td valign="top" width="10%">
	  <a class="data" href="javascript:deActivateAll()"><nobr><? echo $lang_text["wbm_msglist_select"] ?></nobr></a>
	  <br>&nbsp;<br>
	  <nobr><? echo ltext("wbm_msglist_selected") ?><br></nobr>
	  <? echo std_button(ltext("wbm_delete"), 'javascript:deleteMessages()'); ?>
  </td>
  <td valign="top" align="right">
<? if($page > 0): ?><a class="data" href="email_postfach.php?page=<?=($page-1)?><? if($orderby): ?>&orderby=<?=$orderby?><? endif; ?><? if($sort == 'desc'): ?>&sort=<?=$sort?><? endif; ?><? if(array_key_exists('show', $_GET)): ?>&show=<?=$show?><? endif; ?>"><?=ltext('g_prev')?></a><? endif; ?>
<? for($j = 0; $j < $pages; $j++): ?>  
<? if($page != $j): ?><a class="data" href="email_postfach.php?page=<?=$j?><? if($orderby): ?>&orderby=<?=$orderby?><? endif; ?><? if($sort == 'desc'): ?>&sort=<?=$sort?><? endif; ?><? if(array_key_exists('show', $_GET)): ?>&show=<?=$show?><? endif; ?>"><? endif; ?>[<?=($j+1)?>]<? if($page != $j): ?></a><? endif; ?>
<? endfor; ?> 
<? if($page < ($pages - 1)): ?><a class="data" href="email_postfach.php?page=<?=($page+1)?><? if($orderby): ?>&orderby=<?=$orderby?><? endif; ?><? if($sort == 'desc'): ?>&sort=<?=$sort?><? endif; ?><? if(array_key_exists('show', $_GET)): ?>&show=<?=$show?><? endif; ?>"><?=ltext('g_next')?></a><? endif; ?>
  </td>
  </tr>
  </table>
<?}?>
  
  
  </td></tr></table>
  </form>
<?
}
/* POP3-Disconnect: */


imap_close($mbox);
include("email_footer.php");
?>
