<?
/* Copyright (C) 2004  Marco Avidano
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * See http://www.gnu.org/licenses/gpl.html for the complete text of the license.
 */

header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");
@set_time_limit(0);
@error_reporting (E_ALL ^ E_NOTICE);
session_start();

//***************************************//
//*** CONFIGURE NAMEKO HERE *** START ***//
$DEFAULT_COLORSET="GREY"; //GREY,VIOLET,GREEN,BLUE,BROWN
$DEFAULT_FONTSIZE=10; //integer from 7 to 16
$DEFAULT_CHARSET="english-iso-8859-1"; //see README_CHARSET
$DEFAULT_MAIL_PER_PAGE=10;
$SERVERS=array(
 "localhost"=>"127.0.0.1"
);
$USERS_POLICY="deny"; //allow, deny
$USERS_LIST=array(
 "wizzent"
);
//*** CONFIGURE NAMEKO HERE *** STOP ***//
//**************************************//

$VER=array(
 "NAME"=>"Nameko",
 "WEB"=>"Wiz's Shelf",
 "URL"=>"http://wiz.homelinux.net/",
 "MAJOR"=>"0.6",
 "MINOR"=>"1",
 "BUILD"=>"115"
);
$SECTION_RIGHT="Today is ".date("D, jS M Y"); if($_REQUEST[op] && $_SESSION[MESSAGES]) $SECTION_RIGHT="You are <u>$_SESSION[username]</u>@$_SESSION[server] | Your e-mail address is <u>$_SESSION[email]</u> | $SECTION_RIGHT";
$SECTION_LEFT=array(
 "0"=>"Login",
 "1"=>"Check Login",
 "2"=>"Inbox - ".count($_SESSION[MESSAGES][CONTENT])." message".((count($_SESSION[MESSAGES][CONTENT])>1)?"s":"")." - ".sprintf("%.2f",$_SESSION[MESSAGES][SIZE]/1024)." KB",
 "3"=>"Read message",
 "4"=>"Compose new mail",
 "5"=>"Delete messages",
 "6"=>"Send message",
 "999"=>"Credits"
);
$COLORSET=array(
 "GREY"=>array("GROUND"=>"#999999","DARK"=>"#DFDFDF","MEDIUM"=>"#E8E8E8","LIGHT"=>"#F7F7F7","LINE"=>"#7F7F7F","LINKS"=>"#000000"),
 "VIOLET"=>array("GROUND"=>"#CA6597","DARK"=>"#F6C5DB","MEDIUM"=>"#F3DAE8","LIGHT"=>"#FDEEF6","LINE"=>"#FE00BF","LINKS"=>"#FE4100"),
 "GREEN"=>array("GROUND"=>"#64C969","DARK"=>"#C4F5C9","MEDIUM"=>"#D9F2D9","LIGHT"=>"#EDFCED","LINE"=>"#2FFE00","LINKS"=>"#F21DAB"),
 "BLUE"=>array("GROUND"=>"#6699CC","DARK"=>"#C7DDF8","MEDIUM"=>"#DBEAF5","LIGHT"=>"#F0F8FF","LINE"=>"#00BFFF","LINKS"=>"#3E00FE"),
 "BROWN"=>array("GROUND"=>"#C98A64","DARK"=>"#F5D9C4","MEDIUM"=>"#F2E1D9","LIGHT"=>"#FCF2ED","LINE"=>"#FE2300","LINKS"=>"#2727F9")
);
$CHARSETS = array("afrikaans-iso-8859-1"=>"iso-8859-1", "afrikaans-utf-8"=>"utf-8", "albanian-iso-8859-1"=>"iso-8859-1", "albanian-utf-8"=>"utf-8", "arabic-utf-8"=>"utf-8", "arabic-windows-1256"=>"windows-1256",
 "azerbaijani-iso-8859-9"=>"iso-8859-9", "azerbaijani-utf-8"=>"utf-8", "bosnian-utf-8"=>"utf-8", "bosnian-windows-1250"=>"windows-1250", "brazilian_portuguese-iso-8859-1"=>"iso-8859-1", "brazilian_portuguese-utf-8"=>"utf-8",
 "bulgarian-koi8-r"=>"koi8-r", "bulgarian-utf-8"=>"utf-8", "bulgarian-windows-1251"=>"windows-1251", "catalan-iso-8859-1"=>"iso-8859-1", "catalan-utf-8"=>"utf-8", "chinese_big5-utf-8"=>"utf-8", "chinese_big5"=>"big5",
 "chinese_gb-utf-8"=>"utf-8", "chinese_gb"=>"gb2312", "croatian-iso-8859-2"=>"iso-8859-2", "croatian-utf-8"=>"utf-8", "croatian-windows-1250"=>"windows-1250", "czech-iso-8859-2"=>"iso-8859-2", "czech-utf-8"=>"utf-8",
 "czech-windows-1250"=>"windows-1250", "danish-iso-8859-1"=>"iso-8859-1", "danish-utf-8"=>"utf-8", "dutch-iso-8859-1"=>"iso-8859-1", "dutch-utf-8"=>"utf-8", "english-iso-8859-1"=>"iso-8859-1", "english-utf-8"=>"utf-8",
 "estonian-iso-8859-1"=>"iso-8859-1", "estonian-utf-8"=>"utf-8", "finnish-iso-8859-1"=>"iso-8859-1", "finnish-utf-8"=>"utf-8", "french-iso-8859-1"=>"iso-8859-1", "french-utf-8"=>"utf-8", "galician-iso-8859-1"=>"iso-8859-1",
 "galician-utf-8"=>"utf-8", "georgian-utf-8"=>"utf-8", "german-iso-8859-1"=>"iso-8859-1", "german-utf-8"=>"utf-8", "greek-iso-8859-7"=>"iso-8859-7", "greek-utf-8"=>"utf-8", "hebrew-iso-8859-8-i"=>"iso-8859-8-i", "hindi-utf-8"=>"utf-8",
 "hungarian-iso-8859-2"=>"iso-8859-2", "hungarian-utf-8"=>"utf-8", "indonesian-iso-8859-1"=>"iso-8859-1", "indonesian-utf-8"=>"utf-8", "italian-iso-8859-1"=>"iso-8859-1", "italian-utf-8"=>"utf-8", "japanese-euc"=>"euc-jp",
 "japanese-sjis"=>"SHIFT_JIS", "japanese-utf-8"=>"utf-8", "korean-ks_c_5601-1987"=>"ks_c_5601-1987", "latvian-utf-8"=>"utf-8", "latvian-windows-1257"=>"windows-1257", "lithuanian-utf-8"=>"utf-8", "lithuanian-windows-1257"=>"windows-1257",
 "malay-iso-8859-1"=>"iso-8859-1", "malay-utf-8"=>"utf-8", "norwegian-iso-8859-1"=>"iso-8859-1", "norwegian-utf-8"=>"utf-8", "persian-utf-8"=>"utf-8", "persian-windows-1256"=>"windows-1256", "polish-iso-8859-2"=>"iso-8859-2", "polish-utf-8"=>"utf-8",
 "portuguese-iso-8859-1"=>"iso-8859-1", "portuguese-utf-8"=>"utf-8", "romanian-iso-8859-1"=>"iso-8859-1", "romanian-utf-8"=>"utf-8", "russian-dos-866"=>"koi8-r", "russian-koi8-r"=>"koi8-r", "russian-utf-8"=>"utf-8", "russian-windows-1251"=>"windows-1251",
 "serbian_cyrillic-utf-8"=>"utf-8", "serbian_cyrillic-windows-1251"=>"utf-8", "serbian_latin-utf-8"=>"utf-8", "serbian_latin-windows-1250"=>"utf-8", "slovak-iso-8859-2"=>"iso-8859-2", "slovak-utf-8"=>"utf-8", "slovak-windows-1250"=>"windows-1250",
 "slovenian-iso-8859-2"=>"iso-8859-2", "slovenian-utf-8"=>"utf-8", "slovenian-windows-1250"=>"windows-1250", "spanish-iso-8859-1"=>"iso-8859-1", "spanish-utf-8"=>"utf-8", "swedish-iso-8859-1"=>"iso-8859-1", "swedish-utf-8"=>"utf-8",
 "thai-tis-620"=>"tis-620", "thai-utf-8"=>"utf-8", "turkish-iso-8859-9"=>"iso-8859-9", "turkish-utf-8"=>"utf-8", "ukrainian-utf-8"=>"utf-8", "ukrainian-windows-1251"=>"windows-1251");

if(!$_SESSION[COLORS]) $_SESSION[COLORS]=$DEFAULT_COLORSET;
if(!$_SESSION[FONTSIZE]) $_SESSION[FONTSIZE]=$DEFAULT_FONTSIZE;
if(!$_SESSION[CHARSET]) $_SESSION[CHARSET]=$DEFAULT_CHARSET;

if($_GET[colorset]) $_SESSION[COLORS]=$_GET[colorset];
$COLORS=$_SESSION[COLORS];
if($_GET[fontsize]) $_SESSION[FONTSIZE]=$_GET[fontsize];
if($_POST[charset]) $_SESSION[CHARSET]=$_POST[charset];

$WIZ=$_SERVER[SCRIPT_NAME];
if((!$_SESSION[AUTH] && $_REQUEST[op]>1 && $_REQUEST[op]<100) || !$_REQUEST[op]) $_REQUEST[op]=0;
if(!$_SESSION[MPP]) $_SESSION[MPP]=$DEFAULT_MAIL_PER_PAGE;
$BOUNDARY="----------NamekoWebmailBoundary";
$LEGAL_CHARS="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._@-";
$SOCK="";
?>
<html>
<head>
 <title><?="$VER[NAME] $VER[MAJOR].$VER[MINOR]"?></title>
 <meta http-equiv="Content-Type" content="text/html; charset=<?=$CHARSETS[$_SESSION[CHARSET]]?>" />
 <style type="text/css"><!--
  BODY,TABLE,TR,TD,INPUT,TEXTAREA,OPTION,SELECT { font-family:tahoma,sans-serif;font-size:<?=$_SESSION[FONTSIZE]?>pt;color:#333333;text-decoration:none; }
  A:LINK,A:VISITED,.title { font-family:tahoma,sans-serif;font-size:<?=$_SESSION[FONTSIZE]?>pt;color:<?=$COLORSET[$COLORS][LINKS]?>;text-decoration:none; }
  A:HOVER { text-decoration:underline; }
 --></style>
</head>

<body bgcolor="<?=$COLORSET[$COLORS][GROUND]?>">
<table cellspacing="0" cellpadding="0" width="100%" height="90%" border="0" bgcolor="<?=$COLORSET[$COLORS][MEDIUM]?>">
 <tr valign="middle" height="25">
  <td width="75%">&nbsp; <span style="font-size:<?=($_SESSION[FONTSIZE]+4)?>pt;"><b><?="$VER[NAME] $VER[MAJOR].$VER[MINOR]"?></b></span></td>
  <td width="25%" align="right"><a href="<?=$VER[URL]?>" target='_blank'><b>Powered by <?=$VER[WEB]?></b></a> &nbsp;</td>
 </tr>
 <tr valign="top" height="100%">
  <td colspan="2" style="padding:10pt;border-top:2pt solid #111111">
   <table height="100%" cellspacing="0" cellpadding="0" width="100%" border="0" bgcolor="<?=$COLORSET[$COLORS][LIGHT]?>">
    <tr valign="middle" height="20"><td style="border-bottom:1pt solid <?=$COLORSET[$COLORS][LINE]?>"><table cellspacing="0" cellpadding="0" width="100%" height="100%" border="0"><tr valign="middle">
     <td width="35%" style="font-size:<?=($_SESSION[FONTSIZE]+2)?>pt;"><b>&nbsp; .: <?=$SECTION_LEFT[$_REQUEST[op]]?> :.</b></td>
     <td width="65%" align="right"><b><?=$SECTION_RIGHT?></b> &nbsp;</td>
    </tr></table></td></tr>
    <tr>
<?
switch($_REQUEST[op]) {
//1:CHECK LOGIN
case "1":
 $_SESSION[LIMIT]="";
 $auth_fields=array("username","password","email","server","auth_type");
 foreach($auth_fields as $af) {
  if($_POST[$af]) $_SESSION[$af]=$_POST[$af];
  if(!$_SESSION[$af]) $fields_not_filled=1;
 }
 echo("<td valign='top'><p>&nbsp;</p><p>&nbsp;</p>");
 if($fields_not_filled) {
  ShowMessage("ERROR!","<p>You must fill all the fields of the login form!</p>\n<p><a href='$WIZ'><b>Try again</b></a></p>");
 } else {
  if(is_array($USERS_LIST) && count($USERS_LIST)>0 && (($USERS_POLICY=="allow" && in_array($_SESSION[username],$USERS_LIST)) || ($USERS_POLICY!="allow" && !in_array($_SESSION[username],$USERS_LIST)))) {
   ShowMessage("ERROR!","<p>Access denied for user <b>$_SESSION[username]</b> by access control list!</p>\n<p><a href='$WIZ'><b>Try again (with another user)</b></a></p>");
  } else {
   switch(POP3OpenConnectionAndLogin()) {
    case "1": //BAD CONNECTION
     ShowMessage("ERROR!","<p>Connect to address ".$_SESSION[server].":<br>connection refused</p>\n<p><a href='$WIZ'><b>Try again</b></a></p>");
     break;
    case "2": //LOGIN FAILED
     ShowMessage("ERROR!","<p>Authentication failed (bad password?)<br>Connection closed by foreign host.</p>\n<p><a href='$WIZ'><b>Try again</b></a></p>");
     break;
    case "3": //LOGIN OK, RETRIEVE MESSAGES
     $retr_code=POP3RetrieveHeaders(count($_SESSION[MESSAGES][CONTENT])+1);
     switch($retr_code) {
      case "31":
       $_SESSION[AUTH]=1;
       ShowMessage("OK!","<p>Messages fully retrieved!<br>Waiting while creating the GUI...</p>\n<p>If you are not automatically redirect to the next page into 5 seconds,<br><a href='$WIZ?op=2'><b>click here</b></a>!</p>\n<script language='JavaScript'>window.location='$WIZ?op=2'</script>");
       break;
      default: //UNKNOWN ANSWER
       ShowMessage("ERROR!","<p>Retrieving messages from server ".$_SESSION[server].":<br>unknown answer from the server ($retr_code)</p>\n<p><a href='$WIZ'><b>Try again</b></a></p>");
       break;
     }
     break;
    default: //UNKNOWN ANSWER
     ShowMessage("ERROR!","<p>Connect to address ".$_SESSION[server].":<br>unknown answer from the server</p>\n<p><a href='$WIZ'><b>Try again</b></a></p>");
     break;
   }
   if($SOCK) POP3CloseConnection();
  }
 }
 echo("</td>");
 break;

//2:SHOW MESSAGE LIST
case "2":
 $msgnum=count($_SESSION[MESSAGES][CONTENT]);
 if($_GET[toggle_delete]) $_SESSION[MESSAGES][CONTENT][$_GET[toggle_delete]][DELETE]*=(-1);
 if(isset($_GET[change_limit])) $_SESSION[LIMIT]=$_GET[change_limit];
 if($_GET[change_mpp]) {
  $_SESSION[MPP]=$_GET[change_mpp];
  $_SESSION[LIMIT]=0;
 }
 if($_POST[auto_mark_deletion]) {
  for($i=0;$i<$msgnum;$i++) {
   $header=MessageParseHeader(split("\r\n",$_SESSION[MESSAGES][CONTENT][$i][HEADER]));
   if(strstr($header[subject],$_POST[auto_mark_deletion]) || strstr($header[from],$_POST[auto_mark_deletion])) $_SESSION[MESSAGES][CONTENT][$i][DELETE]=$message[DELETE]=1;
  }
 }
 if($_GET[mark_all_messages]) {
  for($i=$msgnum;$i>0;$i--) $_SESSION[MESSAGES][CONTENT][$i][DELETE]=$_GET[mark_all_messages];
 }
 $marked_messages_num=0;
 for($i=0;$i<($msgnum+1);$i++) if($_SESSION[MESSAGES][CONTENT][$i][DELETE]>0) $marked_messages_num++;
 echo("<td align='center' valign='top' style='padding:10pt;'><br>
  <table width='100%' cellspacing='0' cellpadding='0' border='0' style='border-collapse:collapse;border:solid ".$COLORSET[$COLORS][LINE].";border-width:1pt 0pt 1pt 0pt;'>
   <tr bgcolor='".$COLORSET[$COLORS][DARK]."'>
    <td width='23%' class='title' style='padding:5pt;'><b>From</b></td>
    <td width='50%' class='title' style='padding:5pt;'><b>Subject</b></td>
    <td width='20%' class='title' style='padding:5pt;'><b>Date</b></td>
    <td width='6%' class='title' style='padding:5pt;' align='right'><b>Size</b></td>
    <th width='1%' class='title' style='padding:5pt;'>&nbsp;</th>
   </tr>");
 $i=($_SESSION[LIMIT])?$_SESSION[LIMIT]:0;
 $i_max=(($i+$_SESSION[MPP])>$msgnum)?$msgnum:$i+$_SESSION[MPP];
 if($msgnum>0) {
  for($i;$i<$i_max;$i++) {
   $ir=$msgnum-$i;
   $header=MessageParseHeader(split("\r\n",$_SESSION[MESSAGES][CONTENT][$ir][HEADER]));
   while(strstr($header[date],"  ")) $header[date]=str_replace("  "," ",$header[date]);
   $messageDate=split(" ",$header[date]);
   echo("<tr bgcolor='".$COLORSET[$COLORS][MEDIUM]."' onMouseOver='javascript:this.style.backgroundColor=\"".$COLORSET[$COLORS][LINE]."\"' onMouseOut='javascript:this.style.backgroundColor=\"".$COLORSET[$COLORS][MEDIUM]."\"'>
    <td width='23%' style='padding:5pt;border:solid ".$COLORSET[$COLORS][LINE].";border-width:1pt 0pt 1pt 0pt;'>".htmlentities($header[from])."</td>
    <td width='50%' style='padding:5pt;border:solid ".$COLORSET[$COLORS][LINE].";border-width:1pt 0pt 1pt 0pt;'><a href='$WIZ?op=3&id=$ir' style='".(($_SESSION[MESSAGES][CONTENT][$ir][READ]==1)?"":"font-weight:bold;")."'>".(($header[subject])?htmlentities($header[subject]):"[no subject]")."</a></td>
    <td width='20%' style='padding:5pt;border:solid ".$COLORSET[$COLORS][LINE].";border-width:1pt 0pt 1pt 0pt;'>".sprintf("%02d",$messageDate[1])." $messageDate[2] $messageDate[3]</td>
    <td width='6%' style='padding:5pt;border:solid ".$COLORSET[$COLORS][LINE].";border-width:1pt 0pt 1pt 0pt;' align='right'>".sprintf("%.2f",$_SESSION[MESSAGES][CONTENT][$ir][SIZE]/1024)." KB</td>
    <th width='1%' style='padding:0pt 5pt 0pt 5pt;border:solid ".$COLORSET[$COLORS][LINE].";border-width:1pt 0pt 1pt 0pt;'><input type='checkbox' name='del[$ir]' value='1' ".(($_SESSION[MESSAGES][CONTENT][$ir][DELETE]>0)?"checked":"")." onClick='javascript:window.location=\"$WIZ?op=2&toggle_delete=$ir\"'></th>
   </tr>");
  }
 } else {
  echo("<tr bgcolor='".$COLORSET[$COLORS][MEDIUM]."'>
   <th width='100%' colspan='5' style='padding:5pt;border:solid ".$COLORSET[$COLORS][LINE].";border-width:1pt 0pt 1pt 0pt;'><br><br>Inbox empty<br><br>&nbsp;</th>
  </tr>");
 }
 echo("<tr bgcolor='".$COLORSET[$COLORS][DARK]."'><td class='title' style='padding:5pt;' colspan='5'>
  <b>Tools</b><br>
   Show <select onChange='javascript:window.location=\"$WIZ?op=2&change_mpp=\"+this.value' style='height:15pt;'>");
   for($k=5;$k<51;$k+=5) echo("<option value='$k' ".(($_SESSION[MPP]==$k)?"selected":"").">$k</option>");
   echo("</select> messages per page, <select onChange='javascript:window.location=\"$WIZ?op=2&change_limit=\"+this.value' style='height:15pt;'>");
   if(!$msgnum) echo("<option>no messages in inbox</option>");
   else { for($k=0;$k<$msgnum;$k+=$_SESSION[MPP]) echo("<option value='$k' ".(($_SESSION[LIMIT]==$k)?"selected":"").">from ".($k+1)." to ".((($k+$_SESSION[MPP])>$msgnum)?$msgnum:($k+$_SESSION[MPP]))."</option>"); }
   echo("</select> <input type='button' value='&lt; Prev' style='height:15pt;width:50pt;' onClick='javascript:window.location=\"$WIZ?op=2&change_limit=".($_SESSION[LIMIT]-$_SESSION[MPP])."\"' ".(($_SESSION[LIMIT]<$_SESSION[MPP])?"disabled":"")."> <input type='button' value='Next &gt;' style='height:15pt;width:50pt;' onClick='javascript:window.location=\"$WIZ?op=2&change_limit=".($_SESSION[LIMIT]+$_SESSION[MPP])."\"' ".((($_SESSION[LIMIT]+$_SESSION[MPP])>$msgnum)?"disabled":"")."><br>
   <form method='post' action='$WIZ?op=2'>Auto mark for deletion messages that contains <input type='text' name='auto_mark_deletion' style='width='40pt;height:15pt;'> in From or Subject field <input type='submit' value='Mark' style='height:15pt;'></form>
   <input type='button' value='Mark all messages' style='height:15pt;' onClick='javascript:window.location=\"$WIZ?op=2&mark_all_messages=1\"'>
   <input type='button' value='Unmark all messages' style='height:15pt;' onClick='javascript:window.location=\"$WIZ?op=2&mark_all_messages=-1\"'>
   <input type='button' value='Delete marked messages ($marked_messages_num)' style='height:15pt;' onClick='javascript:if(confirm(\"Do you really want to delete all marked messages?\")) window.location=\"$WIZ?op=5\"' ".(($marked_messages_num<1)?"disabled":"").">
  </td></tr>
 </table>\n</td>");
 break;

//3:READ MAIL
case "3":
 if($_GET[toggle_delete]) $_SESSION[MESSAGES][CONTENT][$_GET[toggle_delete]][DELETE]*=(-1);
 $_SESSION[MESSAGES][CONTENT][$_GET[id]][READ]=1;
 $getid=$_GET[id];
 $header=MessageParseHeader(split("\r\n",$_SESSION[MESSAGES][CONTENT][$getid][HEADER]));
 echo("<td align='center' valign='top' style='padding:10pt;'><br>
  <table width='100%' cellspacing='0' cellpadding='0' border='0' style='border-collapse:collapse;border:solid ".$COLORSET[$COLORS][LINE].";border-width:1pt 0pt 1pt 0pt;'>
   <tr bgcolor='".$COLORSET[$COLORS][DARK]."' valign='top'>
    <td width='75%' style='padding:5pt;'>
     <div style='padding-bottom:5pt;'><b><big>".htmlentities($header[subject])."</big></b></div>
     <table border='0' width='100%' cellspacong='2' cellpadding='1'>
      <tr><td><b>From:</b> ".htmlentities($header[from])."</td></tr>
      <tr><td><b>To:</b> ".htmlentities($header[to])."</td></tr>
      <tr><td><b>Date:</b> ".htmlentities($header[date])."</td></tr>");
      if($header["disposition-notification-to"]) echo("<tr><td><font color='#DD0000'><b>Sender ask for read notification to address ".$header["disposition-notification-to"]."!</b></font></td></tr>");
      echo("<tr><td colspan='2'><input type='button' value='Show/hide other headers' style='width:200pt;height:15pt;' onClick='javascript:document.all.allheaders.style.display=(document.all.allheaders.style.display==\"none\")?\"\":\"none\"'></td></tr>
     </table>
     <table border='0' width='100%' cellspacong='2' cellpadding='1' style='display:none;' id='allheaders'>");
 $_SESSION[refw][date]=htmlentities($header[date]);
 $_SESSION[refw][from]=GetAddressFromFromHeader($header[from]);
 $_SESSION[refw][subject]=htmlentities($header[subject]);
 foreach($header as $hd_name=>$hd_content) if(($hd_name!="from") && ($hd_name!="subject") && ($hd_name!="to") && ($hd_name!="date") && (trim($hd_content)!="")) echo("<tr><td><table border='0' cellpadding='0' cellspacing='0'><tr valign='top'><td style='padding-right:5pt;'><b>".ucfirst($hd_name).":<b></td><td>".nl2br(htmlentities($hd_content))."</td></tr></table></td></tr>\n");
 if($_SESSION[MESSAGES][CONTENT][$_GET[id]][DELETE]>0) {
  $del_message="<font color='#FF0000'><b>This message is marked for deletion</b></font><br>";
  $del_button="Unmark";
 } else {
  $del_button="Mark";
 }
 echo("</table>
    </td>
    <td width='25%' style='padding:5pt;' align='right'>
     $del_message
     <input type='button' value='Reply' style='width:130pt;' onClick='window.location=\"$WIZ?op=4&act=re\"'><br>
     <input type='button' value='Forward' style='width:130pt;' onClick='window.location=\"$WIZ?op=4&act=fw\"'><br>
     <input type='button' value='$del_button for deletion' style='width:130pt;' onClick='javascript:window.location=\"$WIZ?op=3&id=$_GET[id]&toggle_delete=$_GET[id]\"'><br>
     <input type='button' value='Back to Inbox' style='width:130pt;' onClick='javascript:window.location=\"$WIZ?op=2\"'><br>
    </td>
   </tr>
  </table>
  <div align='left'>");
 $message=MessageRetrieveContent($header,POP3RetrieveMessage($_GET[id]));
 if(count($message["attachments"])) {
  echo("<br><table width='100%' cellspacing='0' cellpadding='0' border='0' style='border-collapse:collapse;border:solid ".$COLORSET[$COLORS][LINE].";border-width:1pt 0pt 1pt 0pt;'><tr bgcolor='".$COLORSET[$COLORS][DARK]."' valign='top'><td><b>Attachments</b>:<br>\n");
  foreach($message["attachments"] as $atc) {
   $ctparts=split(";",$atc["header"]["content-type"]);
   foreach($ctparts as $ctpart) {
    if(strstr($ctpart,"name=")) {
     $filename=split("=",$ctpart);
     $filename=str_replace("\"","",$filename[1]);
    }
   }
   for($d=0;$d<strlen($filename);$d++) {
    if(!strstr($GLOBALS[LEGAL_CHARS],$filename[$d])) $filename[$d]="_";
   }
   echo("&nbsp; &middot; <a href='tmp_nameko/$filename' target='_blank'>$filename</a><br>");
   if($attach_handle=@fopen("tmp_nameko/$filename","w")) @fwrite($attach_handle,$atc[content]);
   @fclose($attach_handle);
   @chmod("tmp_nameko/$filename",0600);
  }
  echo("</td></tr></table>\n");
 }
 echo("<p>");
 $button_show_plain="<input type='button' value='Show text/plain message' style='width:130pt;' onClick='javascript:window.location=\"$WIZ?op=3&id=$_GET[id]\"'><br><br>\n";
 $button_show_html="<input type='button' value='Show HTML message' style='width:130pt;' onClick='javascript:window.location=\"$WIZ?op=3&id=$_GET[id]&show_html=1\"'><br><br>\n";
 if($message["text-plain"] && $message["text-html"]) {
  if($_GET[show_html]) echo($button_show_plain . $message["text-html"]);
  else echo($button_show_html . $message["text-plain"]);
 } elseif($message["text-plain"] && !$message["text-html"]) {
  echo($message["text-plain"]);
 } elseif(!$message["text-plain"] && $message["text-html"]) {
  echo($message["text-html"]);
 } else {
  echo("<i>This e-mail doesn't contain any text.</i>");
 }
 echo("</p></div></td>");
 $_SESSION[refw][text]=split("\n",wordwrap(strip_tags($message["text-plain"]), 60, "\n", 1));
 for($x=0;$x<count($_SESSION[refw][text]);$x++) $_SESSION[refw][text][$x]="> ".$_SESSION[refw][text][$x];
 $_SESSION[refw][text]=implode("\n",$_SESSION[refw][text]);
 break;

//4:COMPOSE MAIL
case "4":
 if($_GET[act]) {
  $refwtext="Original message sent on the ".$_SESSION[refw][date]." by ".$_SESSION[refw][from]."\n".$_SESSION[refw][text];
  if($_GET[act]=="re") {
   $refwto=$_SESSION[refw][from];
   $refwsubject="Re: ".$_SESSION[refw][subject];
  } elseif ($_GET[act]=="fw") {
   $refwsubject="Fw: ".$_SESSION[refw][subject];
  }
 }
 echo("<td valign='top' style='padding:10pt;'><form method='post' action='$WIZ?op=6' enctype='multipart/form-data'>
  <table width='100%' cellspacing='0' cellpadding='0' border='0' style='border-collapse:collapse;border:solid ".$COLORSET[$COLORS][LINE].";border-width:1pt 0pt 1pt 0pt;'><tr valign='top' bgcolor='".$COLORSET[$COLORS][DARK]."'>
   <td width='75%'><table width='100%' cellspacing='0' cellpadding='4' border='0'>
    <tr>
     <th align='right'>From: </th>
     <td><input type='text' name='from' style='width:300pt;' value='$_SESSION[email]'></td>
    </tr>
    <tr>
     <th align='right'>To: </th>
     <td><input type='text' name='to' style='width:300pt;' value='$refwto'></td>
    </tr>
    <tr>
     <th align='right'>Cc: </th>
     <td><input type='text' name='cc' style='width:300pt;'></td>
    </tr>
    <tr>
     <th align='right'>Bcc: </th>
     <td><input type='text' name='bcc' style='width:300pt;'></td>
    </tr>
    <tr>
     <th align='right'>Subject: </th>
     <td><input type='text' name='subject' style='width:300pt;' value='$refwsubject'></td>
    </tr>
   </table></td>
   <td width='25%'><table width='100%' cellspacing='0' cellpadding='4' border='0'>
    <tr><td><b>Options:</b></td></tr>
    <tr><td><input type='checkbox' name='notification' value='1'> Ask for notification</td></tr>
    <tr><td><b>Attachments:</b></td></tr>
    <tr><td>
     <b>#1</b> <input type='file' name='atc[0]'><br>
     <b>#2</b> <input type='file' name='atc[1]'><br>
     <b>#3</b> <input type='file' name='atc[2]'>
    </td></tr>
   </table></td>
  </tr></table>
  <p><b>Message</b><br><textarea name='message' style='width:100%;height:300pt;'>$refwtext</textarea></p>
  <p align='center'><input type='submit' value='Send e-mail' style='width:130pt;'> <input type='reset' value='Reset form' style='width:130pt;'> <input type='button' value='Back to Inbox' style='width:130pt;' onClick='javascript:window.location=\"$WIZ?op=2\"'><br>
 </form></td>");
 break;

//5:DELETE MESSAGES
case "5":
 echo("<td valign='top'><br>");
 POP3DeleteMessages();
 echo("<p><input type='button' value='Back to Inbox' style='height:15pt;' onClick='javascript:window.location=\"$WIZ?op=1\"'>
  <script language='javascript'>window.location=\"$WIZ?op=1\"</script>
 </td>");
 break;

//6:SEND EMAIL
case "6":
 echo("<td valign='top'><p>&nbsp;</p><p>&nbsp;</p>");
 $email_fields=array("from","to","cc","bcc");
 foreach($email_fields as $ef) {
  $eas=split('[;,]',$_POST[$ef]);
  foreach($eas as $ea) {
   if($ea=trim($ea)) {
    if(!ValidateEmailAddress($ea)) {
     $invalid_email_address.="<b>$ea</b> in <b>".ucfirst($ef)."</b> field<br>";
     echo("<br>");
    }
    $email_address[$ef].="$ea, ";
   }
  }
  $email_address[$ef]=substr($email_address[$ef],0,-2);
 }
 if($invalid_email_address || !$email_address[to]) {
  ShowMessage("ERROR!","<p>Error during delivering mail.</p>\n<p>E-mail address(es)<br>$invalid_email_address is(are) not valid e-mail address(es).</p>\n<p><a href='javascript:history.back()'><b>Back</b></a></p>");
 } else {
  $content_type="text/plain";
  $message=explode("\n",$_POST[message]);
  foreach($message as $line) $text.=stripslashes(rtrim($line))."\n";
  for($i=0;$i<3;$i++) {
   if($_FILES['atc']['name'][$i]) {
    $content_type="multipart/mixed;\n  charset=\"iso-8859-1\";\n  boundary=\"$BOUNDARY\"";
    $headertext="This is a multi-part message in MIME format.\n\n--$BOUNDARY\nContent-Type: text/plain;\n  charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable\n\n";
    $footertext="--$BOUNDARY--\n";
    $fattach=@fopen($_FILES['atc']['tmp_name'][$i],"r");
    $attach=chunk_split(base64_encode(@fread($fattach,@filesize($_FILES['atc']['tmp_name'][$i]))));
    @fclose($fattach);
    $text.="\n--$BOUNDARY\nContent-Type: ".$_FILES['atc']['type'][$i].";\n  charset=\"iso-8859-1\";\n  name=\"".$_FILES['atc']['name'][$i]."\"\nContent-Transfer-Encoding: base64\nContent-Disposition: attachment; filename=\"".$_FILES['atc']['name'][$i]."\"\n\n$attach\n";
   }
  }
  $text=$headertext.$text.$footertext;
  $headermail="From: $email_address[from]\nReply-to: $email_address[from]\n".(($email_address[cc])?"Cc: $email_address[cc]\n":"").(($email_address[bcc])?"Bcc: $email_address[bcc]\n":"")."MIME-Version: 1.0\nContent-Type: $content_type\nX-Sender-IP-Address: $_SERVER[REMOTE_ADDR]\nX-Mailer: $VER[NAME] $VER[MAJOR].$VER[MINOR]\n";
  if($_POST[notification]) $headermail.="Disposition-Notification-To: $email_address[from]\n";
  if(@mail("$email_address[to]","$_POST[subject]","$text","$headermail","-f$email_address[from]")) {
   ShowMessage("SUCCESS!","<p>Mail correctly delivered.</p>\n<p><a href='$WIZ?op=2'><b>Back to Inbox</b></a></p>");
  } else {
   ShowMessage("ERROR!","<p>Error during delivering mail.</p>\n<p>Mail command failed.</p>\n<p><a href='javascript:history.back()'><b>Back</b></a></p>");
  }
 }
 echo("</td>");
 break;

//999:CREDITS
case "999":
 echo("<td valign='top'>
  <p><br><b>Maintainer</b>:<br>
   &nbsp; <span class='title'>Marco Avidano</span>
  </p>
  <p><b>Thanks to</b>:<br>
   &nbsp; <span class='title'>Sven</span> (bug report [ver. 0.6.1])<br>
   &nbsp; <span class='title'>Fred Bonani</span> (bug report [ver. 0.6.1, 0.4.4, 0.4.3])<br>
   &nbsp; <span class='title'>Steven Albarracin</span> (bug report [ver. 0.6.1, 0.5.3, 0.4.4])<br>
   &nbsp; <span class='title'>Simon Hickman</span> (bug report [ver. 0.4.3])<br>
   &nbsp; <span class='title'>Philip Chapman-Bell</span> (bug report [ver. 0.2.2])<br>
   &nbsp; <span class='title'>Roberto Bertuol</span><br>
  </p>
  <p><b>Some information about this program</b>:<br>
   &nbsp; Project name: <span class='title'>$VER[NAME]</span><br>
   &nbsp; Major version: <span class='title'>$VER[MAJOR]</span><br>
   &nbsp; Minor version: <span class='title'>$VER[MINOR]</span><br>
   &nbsp; Build: <span class='title'>$VER[BUILD]</span><br>
   &nbsp; Shortly: <span class='title'>$VER[MAJOR].$VER[MINOR]</span><br>
   &nbsp; Web site: <span class='title'>$VER[WEB]</span><br>
   &nbsp; URL: <a href='$VER[URL]' target='_blank'>$VER[URL]</a>
  </p>
  <p><b>Support my work</b>:<br>
   <span class='title'>Nameko</span> is totally free. I to not make any profit by its development and maintenance.<br>
   If you think it's great and useful, you can make me a little donation through Paypal.<br>
   Not a lot: only 2 or 3 Euros (or dollars, it's the same for me), and I'll drink a pineapple juice to yours health!<br>
   If you want go to:<br>
    <a href='https://www.paypal.com/xclick/business=paypal%40wiz.homelinux.net&item_name=Wiz%27s+Shelf+-+Nameko&no_note=1&tax=0&currency_code=EUR' target='_blank'>https://www.paypal.com/xclick/business=paypal%40wiz.homelinux.net&item_name=Wiz%27s+Shelf+-+Nameko&no_note=1&tax=0&currency_code=EUR</a><br>
   and fill the Paypal form to make me a donation.<br>
   Thank you in advance!
  </p>
  <p><b>License</b>:<br>
   Copyright (C) 2004  Marco Avidano<br>
   This program is free software; you can redistribute it and/or modify<br>
   it under the terms of the GNU General Public License as published by<br>
   the Free Software Foundation; either version 2 of the License, or<br>
   (at your option) any later version.<br>
   This program is distributed in the hope that it will be useful,<br>
   but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br>
   GNU General Public License for more details.<br>
   You should have received a copy of the GNU General Public License<br>
   along with this program; if not, write to the Free Software<br>
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA<br>
   See <a href='http://www.gnu.org/licenses/gpl.html' target='_blank'>http://www.gnu.org/licenses/gpl.html</a> for the complete text of the license.
  </p>
  <p><b>Something else</b>:<br>
   &nbsp; &quot;I will suppose, then, not that a Deity, who is sovereignly good and the fountain<br>
   &nbsp; of truth but that some Malignant Demon, who is at once potent and deceitful,<br>
   &nbsp; has employed all his artifice to deceive me.<br>
   &nbsp; I will suppose that Paradise, the sky, the earth, colors, and all external things,<br>
   &nbsp; are nothing better than the illusions of dreams, by means of which this being<br>
   &nbsp; has laid snares for my credulity.<br>
   &nbsp; I will consider myself as without hands, eyes, flesh, blood, or any of the senses,<br>
   &nbsp; nevetheless I will falsely believe that I am in possession of all of these.&quot;<br>
   &nbsp; - Descartes, &quot;Of the Things of Which We May Doubt&quot;, 1641.
  </p>
  <p>&nbsp;</p>
 </td>");
 break;

//DEFAULT:LOGIN
default:
 $temp_fontsize=$_SESSION[FONTSIZE];
 $temp_colorset=$_SESSION[COLORS];
 $temp_charset=$_SESSION[CHARSET];
 session_unset();
 $_SESSION[COLORS]=$temp_colorset;
 $_SESSION[FONTSIZE]=$temp_fontsize;
 $_SESSION[CHARSET]=$temp_charset;
 echo("<td align='center' valign='middle'>");
 if(!FlushTmpDir()) {
  echo("<p><font color='#F00000'><b>An error occour trying to flush the temporary directory.<br>The attachment download should not work.<br>You can fix this error creating a directory<br>called &quot;tmp_nameko&quot; where you put the $VER[NAME] script.<br>For any request contact <a href='$VER[URL]' target='_blank'>$VER[WEB]</a>.</b></font></p>\n");
 }
 echo("<form action='$WIZ?op=1' method='post'>
   <table cellspacing='0' cellpadding='0' border='0' style='border-collapse:collapse;border:1pt solid ".$COLORSET[$COLORS][LINE].";'>
    <tr><th style='padding:5pt;' bgcolor='".$COLORSET[$COLORS][DARK]."'>Welcome to $VER[NAME] $VER[MAJOR].$VER[MINOR]</th></tr>
    <tr><td style='padding:5pt 5pt 2pt 5pt;' align='right'><b>Username</b> &nbsp; <input type='text' name='username' style='width:200pt;'></td></tr>
    <tr><td style='padding:2pt 5pt 2pt 5pt;' align='right'><b>Password / Secret</b> &nbsp; <input type='password' name='password' style='width:200pt;'></td></tr>
    <tr><td style='padding:2pt 5pt 2pt 5pt;' align='right'><b>E-mail address</b>  &nbsp; <input type='text' name='email' style='width:200pt;'></td></tr>
    <tr><td style='padding:2pt 5pt 2pt 5pt;' align='right'><b>Auth method</b>  &nbsp; <select name='auth_type' style='width:200pt;'><option value='standard'>Standard user/pass</option><option value='apop'>APOP method</option></select></td></tr>
    <tr><td style='padding:2pt 5pt 2pt 5pt;' align='right'><b>Charset</b>  &nbsp; <select name='charset' style='width:200pt;'>");
      foreach($CHARSETS as $name=>$code) echo("<option value='$name' ".(($name==$_SESSION[CHARSET])?"selected":"").">$name</option>\n");
    echo("</select></td></tr>
    <tr><td style='padding:2pt 5pt 2pt 5pt;' align='right'><b>Server</b> &nbsp; ");
 if($SERVERS) {
  echo("<select name='server' style='width:200pt;'>");
  foreach($SERVERS as $server_name=>$server_addr) echo("<option value='$server_addr'>$server_name</option>\n");
  echo("</select>");
 } else {
  echo("<input type='text' name='server' style='width:200pt;'>");
 }
 echo("</td></tr>
    <tr><th style='padding:5pt;'><input type='submit' value='L O G I N' style='width:200pt;'></td></tr>
   </table>
  </form>
 </td>");
 break;
}
?>
    </tr>
    <tr valign="middle" height="20"><td style="border-top:1pt solid <?=$COLORSET[$COLORS][LINE]?>"><table cellspacing="0" cellpadding="0" width="100%" height="100%" border="0"><tr valign="middle">
     <td width="90" style="font-size:<?=($_SESSION[FONTSIZE]+2)?>pt;"><b>&nbsp; .: Utils :.</b></td>
     <td align="right"><b>
      Font size: <select name="fontsize" onChange="javascript:window.location='<?=$WIZ?>?op=<?=$_REQUEST[op]?>&id=<?=$_REQUEST[id]?>&fontsize='+this.value"><? for($i=7;$i<16;$i++) echo("<option value='$i' ".(($i==$_SESSION[FONTSIZE])?"selected":"").">$i pt</option>\n"); ?></select> &nbsp; | &nbsp;
      Colorset: <select name="colorset" onChange="javascript:window.location='<?=$WIZ?>?op=<?=$_REQUEST[op]?>&id=<?=$_REQUEST[id]?>&colorset='+this.value"><? foreach($COLORSET as $cs_n=>$cs_d) echo("<option value='$cs_n' ".(($cs_n==$COLORS)?"selected":"").">$cs_n</option>\n"); ?></select> &nbsp; | &nbsp;
      Functions: <a href='<?=$WIZ?>'>Login/Logout</a> |
      <? if($_SESSION[MESSAGES]) echo("
       <a href='$WIZ?op=1'>Check for new mail</a> |
       <a href='$WIZ?op=2'>Back to Inbox</a> |
       <a href='$WIZ?op=4'>Compose new e-mail</a> |
      ");
      ?>
      <a href='<?=$WIZ?>?op=999'>Credits</a> |
     </b></td>
    </tr></table></td></tr>
   </table>
  </td>
 </tr>
</table>
</body>
</html>

<?
function ShowMessage($title,$message) {
 $COLORSET=$GLOBALS[COLORSET];
 $COLORS=$GLOBALS[COLORS];
 echo("<table width='250' align='center' cellspacing='0' cellpadding='0' border='0' style='border-collapse:collapse;border:1pt solid ".$COLORSET[$COLORS][LINE].";'>
  <tr><th style='padding:5pt;' bgcolor='".$COLORSET[$COLORS][DARK]."'>$title</th></tr>
  <tr><td style='padding:10pt;' align='center'>$message</td></tr>
 </table>");
 return;
}

function POP3OpenConnectionAndLogin() {
 if(!$GLOBALS[SOCK]=@fsockopen($_SESSION[server],110)) return "1";
 $rs=@fgets($GLOBALS[SOCK],512);
 if($_SESSION[auth_type]=="apop") {
  $rs=split(" ",$rs);
  $secret=md5(trim($rs[count($rs)-1]).$_SESSION[password]);
  @fputs($GLOBALS[SOCK],"apop ".$_SESSION[username]." $secret\r\n");
  if(!strstr(@fgets($GLOBALS[SOCK],512),"+OK")) return "2";
 } else {
  @fputs($GLOBALS[SOCK],"user ".$_SESSION[username]."\r\n");
  $rs=@fgets($GLOBALS[SOCK],512);
  @fputs($GLOBALS[SOCK],"pass ".$_SESSION[password]."\r\n");
  if(!strstr(@fgets($GLOBALS[SOCK],512),"+OK")) return "2";
 }
 return "3";
}

function POP3RetrieveHeaders($start) {
 if($start==0) $start=1;
 @fputs($GLOBALS[SOCK],"stat\r\n");
 $stat=split(" ",@fgets($GLOBALS[SOCK],512));
 if($stat[0]!="+OK") return "391";
 $_SESSION[MESSAGES][SIZE]=$stat[2];
 for($i=$start;$i<($stat[1]+1);$i++) {
  @fputs($GLOBALS[SOCK],"list $i\r\n");
  $list=split(" ",@fgets($GLOBALS[SOCK],512));
  if($list[0]!="+OK") return "392";
  $_SESSION[MESSAGES][CONTENT][$i][SIZE]=$list[2];
  $header="";
  fputs($GLOBALS[SOCK],"top $i 0\r\n");
  while(($rs=fgets($GLOBALS[SOCK],512))!=".\r\n") $header.=$rs;
  $_SESSION[MESSAGES][CONTENT][$i][HEADER]=$header;
  $_SESSION[MESSAGES][CONTENT][$i][READ]=0;
  $_SESSION[MESSAGES][CONTENT][$i][DELETE]=(-1);
 }
 return "31";
}

function POP3RetrieveMessage($i) {
 POP3OpenConnectionAndLogin();
 fputs($GLOBALS[SOCK],"retr $i\r\n");
 if(strstr(@fgets($GLOBALS[SOCK],512),"+OK")) {
  while(($rs=@fgets($GLOBALS[SOCK],512))!="\r\n") { }
  while(($rs=@fgets($GLOBALS[SOCK],512))!=".\r\n") $message[]=trim($rs);
 } else {
  $message="Error trying to retrieve message ID <b>#$i</b>";
 }
 if($GLOBALS[SOCK]) POP3CloseConnection();
 return $message;
}

function POP3DeleteMessages() {
 POP3OpenConnectionAndLogin();
 for($i=1,$j=1;$i<count($_SESSION[MESSAGES][CONTENT])+1;$i++) {
  if($_SESSION[MESSAGES][CONTENT][$i][DELETE]>0) {
   fputs($GLOBALS[SOCK],"dele $i\r\n");
   $del=split(" ",fgets($GLOBALS[SOCK],512));
   echo(((trim($del[0])=="+OK")?"Message number <b>$i</b> deleted":"Error deleting message number <b>$i</b>")."<br>\n");
  } else {
   $new_message_list[$j]=$_SESSION[MESSAGES][CONTENT][$i];
   $j++;
  }
 }
 $_SESSION[MESSAGES][CONTENT]=$new_message_list;
 echo("<p><b>Wait while realoading messages...</b></p>\n");
 if($GLOBALS[SOCK]) POP3CloseConnection();
}

function POP3CloseConnection() {
 @fputs($GLOBALS[SOCK],"quit\r\n");
 @fclose($GLOBALS[SOCK]);
 sleep(1);
 return;
}

function MessageParseHeader($header) {
 for($j=0;$j<count($header);$j++) {
  $hd=split(":",$header[$j],2);
  if(preg_match_all("/\s/",$hd[0],$matches) || !$hd[1]) {
   if($last_header) $parsed_header[$last_header].="\r\n".trim($header[$j]);
  } else {
   $last_header=strtolower($hd[0]);
   $parsed_header[$last_header].=(($parsed_header[$last_header])?"\r\n":"").trim($hd[1]);
  }
 }
 if(is_array($parsed_header)) foreach($parsed_header as $hd_name=>$hd_content) {
  $start_enc_tag=$stop_enc_tag=0;
  $pre_text=$enc_text=$post_text="";
  while(1) {
   if(strstr($hd_content,"=?") && strstr($hd_content,"?=") && substr_count($hd_content,"?")>3) {
    $start_enc_tag=strpos($hd_content,"=?");
    $pre_text=substr($hd_content,0,$start_enc_tag);
    do {
     $stop_enc_tag=strpos($hd_content,"?=",$stop_enc_tag)+2;
     $enc_text=substr($hd_content,$start_enc_tag,$stop_enc_tag);
    } while (!(substr_count($enc_text,"?")>3));
    $enc_text=explode("?",$enc_text,5);
    switch(strtoupper($enc_text[2])) {
     case "B":
      $dec_text=base64_decode($enc_text[3]);
      break;
     case "Q":
     default:
      $dec_text=quoted_printable_decode($enc_text[3]);
      $dec_text=str_replace("_"," ",$dec_text);
      break;
    }
    $post_text=substr($hd_content,$stop_enc_tag);
    if(substr(ltrim($post_text),0,2)=="=?") $post_text=ltrim($post_text);
    $hd_content=$pre_text.$dec_text.$post_text;
    $parsed_header[$hd_name]=$hd_content;
   } else break;
  }
 }
 return $parsed_header;
}

function MessageRetrieveContent($header,$message) {
 $content_transfer_encoding=strtolower(trim($header["content-transfer-encoding"]));
 if($content_transfer_encoding=="") $content_transfer_encoding="8bit";
 $message=TextDecode($content_transfer_encoding,$message);

 $content_type=split(";",$header["content-type"],2);
 for($i=0;$i<count($content_type);$i++) $content_type[$i]=trim($content_type[$i]);
 if($content_type[0]=="") $content_type[0]="text/plain";
 if(stristr($content_type[0],"multipart/") || stristr($content_type[0],"message/")) $content_type[0]="multipart";

 if($header["content-disposition"]) {
  $GLOBALS[parsed_message]["attachments"][]=array("header"=>$header, "content"=>@implode("\n",$message));
 } else {
  switch(trim(strtolower($content_type[0]))) {
   case "text/plain":
    $GLOBALS[parsed_message]["text-plain"]=nl2br(htmlentities(implode("\n",$message)));
    break;
   case "text/html":
    $GLOBALS[parsed_message]["text-html"]=implode("\n",$message);
    break;
   case "multipart":
    $content_type[1]=split(";",$content_type[1]);
    foreach($content_type[1] as $ct_pars) {
     if(strstr($ct_pars,"boundary")) {
      $ct_pars=split("=",trim($ct_pars),2);
      if(strtolower($ct_pars[0])=="boundary") $boundary=str_replace("\"","",$ct_pars[1]);
     }
    }
    if($boundary) {
     $parts=MessageSplitMultipart($boundary,$message);
     foreach($parts as $part) ParsePart($part);
    } else {
     $GLOBALS[parsed_message]["text-plain"]="<p>".$GLOBALS[VER][NAME]." ".$GLOBALS[VER][MAJOR].".".$GLOBALS[VER][MINOR]."<br>Sorry, I'm unable to read this mail.<br>Please, report this error to <a href='http://wiz.homelinux.net/bugreport.php?prj=Nameko'>Wiz's Shelf Staff</a></p>";
    }
    break;
   default:
    $GLOBALS[parsed_message]["attachments"][]=array("header"=>$header, "content"=>@implode("\n",$message));
    break;
  }
 }
 return($GLOBALS[parsed_message]);
}

function MessageSplitMultipart($boundary,$text) {
 $parts=array();
 $tmp=array();
 foreach($text as $line) {
  if(strstr($line,"--$boundary")) {
   $parts[]=$tmp;
   $tmp=array();
  } else $tmp[]=$line;
 }
 for($i=0;$i<count($parts);$i++) $parts[$i]=explode("\n",trim(implode("\n",$parts[$i])));
 return $parts;
}

function ParsePart($text) {
 $headerpart=array();
 $contentpart=array();
 foreach($text as $riga) {
  if(!$riga) $noheader++;
  if($noheader) $contentpart[]=$riga;
  else $headerpart[]=$riga;
 }
 MessageRetrieveContent(MessageParseHeader($headerpart),explode("\n",trim(implode("\n",$contentpart))));
}

function TextDecode($encoding,$text) {
 switch($encoding) {
  case "quoted-printable":
   $dec_text=explode("\n",quoted_printable_decode(implode("\n",$text)));
   break;
  case "base64":
   for($i=0;$i<count($text);$i++) $text[$i]=trim($text[$i]);
   $dec_text=explode("\n",base64_decode(@implode("",$text)));
   break;
  case "7bit":
  case "8bit":
  case "binary":
  default:
   $dec_text=$text;
   break;
 }
 return $dec_text;
}

function ValidateEmailAddress($addr) {
 $addr_len=strlen($addr);
 $at_pos=strpos($addr,"@");
 $lastdot_pos=strrpos($addr,".");
 for($i=0;$i<$addr_len;$i++) {
  if(!strstr($GLOBALS[LEGAL_CHARS],$addr[$i])) return 0;
 }
 if(($addr[0]==".") || ($addr[0]=="-") || ($at_pos<1) || ($at_pos>($addr_len-5)) || ($at_pos>($lastdot_pos-2)) || ($lastdot_pos>$addr_len-3) || (substr_count($addr,"@")>1)) return 0;
 return 1;
}

function GetAddressFromFromHeader($addr) {
 $atpos=strpos($addr,"@");
 $minpos=strpos($addr,"<");
 $majpos=strpos($addr,">");
 $fromstart=0;
 $fromend=strlen($addr);
 if($minpos<$atpos && $majpos>$atpos) {
  $fromstart=$minpos+1;
  $fromend=$majpos;
 }
 return substr($addr,$fromstart,$fromend-$fromstart);
}

function FlushTmpDir() {
 if(!is_dir("tmp_nameko")) @mkdir("tmp_nameko",0700);
 if(is_dir("tmp_nameko")) {
  $tmpdir=opendir("tmp_nameko");
  while(($tmpfile=readdir($tmpdir))!=false) {
   if(($tmpfile!="..")&&($tmpfile!=".")) {
    $tmpfiledate=filectime("tmp_nameko/$tmpfile");
    if((filectime("tmp_nameko/$tmpfile"))<(time()-300)) @unlink("tmp_nameko/$tmpfile");
   }
  }
  closedir($tmpdir);
  return 1;
 } else {
  return 0;
 }
}
?>
