<?php
/*
PHPwebmail is a webmail client written in php.
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.
*/
session_start();
require("not_registered.php");
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
</head>
<body bgcolor="<?php echo $bg_color_main; ?>" text="<?php echo $fg_color; ?>" link="<?php echo $link_color; ?>" vlink="<?php echo $link_color; ?>" alink="<?php echo $alink_color; ?>">
<font face="courier, fixed" size=2>
<?php
function DisectThisPart($this_part, $part_no) {
// I found the raw code of this great function by hide@address.com via the annotated PHP manual at www.php.net
// It's webpage is located at http://www.bitsense.net/PHPNotes/IMAP/imap_fetchstructure.asp
// get user's name and the current message details
global $name, $mbox, $message, $body_part, $textmode;
// set a few MIME variables
$mime_enc[0] = "7BIT";
$mime_enc[1] = "8BIT";
$mime_enc[2] = "BINARY";
$mime_enc[3] = "BASE64";
$mime_enc[4] = "QUOTED-PRINTABLE";
$mime_enc[5] = "OTHER ";
$mime_types[0] = "text";
$mime_types[1] = "multipart ";
$mime_types[2] = "message ";
$mime_types[3] = "application ";
$mime_types[4] = "audio";
$mime_types[5] = "image";
$mime_types[6] = "video ";
$mime_types[7] = "other ";
if ($this_part->ifdisposition) {
// See if it has a disposition
// The only thing I know of that this
// would be used for would be an attachment
// Lets check anyway
if ($this_part->disposition == "ATTACHMENT" || "INLINE") {
/*
these line used for debugging only
echo "Disposition: $this_part->disposition<BR>";
*/
// First see if they sent a filename
$att_name = "unknown";
for ($lcv = 0; $lcv < count($this_part->parameters); $lcv++) {
$param = $this_part->parameters[$lcv];
if ($param->attribute == "CHARSET") {
$att_name = $param->value;
/*
these line used for debugging only
echo "CHARSET $att_name <BR>";
*/
}
if ($param->attribute == "NAME") {
$att_name = $param->value;
/*
these lines used for debugging only
echo "FILENAME $att_name <BR>";
echo "ENCODING ".$mime_enc[$this_part->encoding]."<BR>";
echo "TYPE ".$mime_types[$this_part->type*1]."<BR>";
echo "<br>";
*/
break;
}
}
// save attachment to /tmp/$user
// create a user-dir
$the_path = "tmp/".$name;
$the_file = $the_path."/".urlencode($att_name);
if (!file_exists($the_path)) mkdir ($the_path, 0700);
// decode and save file
$fp = fopen($the_file, "w+");
if (!$fp) echo "Error - could not save attachment.<BR><BR>";
else {
// decode and write out the next body part
$body_part++;
if ($body_part == 2) echo "<b>Attachments:</b><BR>";
$the_body = imap_fetchbody($mbox, $message, $body_part);
switch ($mime_enc[$this_part->encoding]) {
case "7BIT":
// do nothing
break;
case "8BIT":
$the_body = imap_qprint(imap_8bit($the_body));
break;
case "BINARY":
// do nothing
break;
case "BASE64":
$the_body = imap_base64($the_body);
break;
case "QUOTED-PRINTABLE":
$the_body = imap_qprint($the_body);
break;
case "OTHER":
// hmmm...(do nothing)
break;
default:
// hmmm...(do nothing)
} // switch ends
fwrite ($fp,$the_body);
fclose ($fp);
echo "<br> <b><a href=".$the_file." target=\"_blank\">";
if ($textmode) echo "Attachment: "; else echo "<img src=\"images/attach.gif\" border=0> ";
echo "$att_name</a></b><br>";
} // else ends
} // if ends
} else {
// Not an attachment, lets see what this part is...
// I am just interested in attachments so I leave this raw framework untouched
switch ($this_part->type) {
case TYPETEXT:
$mime_type = "text";
break;
case TYPEMULTIPART:
$mime_type = "multipart";
// Hey, why not use this function to deal with all the parts
// of this multipart part :)
for ($i = 0; $i < count($this_part->parts); $i++) {
if ($part_no != "") {
$part_no = $part_no.".";
}
for ($i = 0; $i < count($this_part->parts); $i++) {
DisectThisPart($this_part->parts[$i], $part_no.($i + 1));
}
}
break;
case TYPEMESSAGE:
$mime_type = "message";
break;
case TYPEAPPLICATION:
$mime_type = "application";
break;
case TYPEAUDIO:
$mime_type = "audio";
break;
case TYPEIMAGE:
$mime_type = "image";
break;
case TYPEVIDEO:
$mime_type = "video";
break;
case TYPEMODEL:
$mime_type = "model";
break;
default:
$mime_type = "unknown";
// hmmm...
} // switch ends
$full_mime_type = $mime_type."/".$this_part->subtype;
// Decide what you what to do with this part
// If you want to show it, figure out the encoding and echo away
switch ($this_part->encoding) {
case ENCBASE64:
// use imap_base64 to decode
break;
case ENCQUOTEDPRINTABLE:
// use imap_qprint to decode
break;
case ENCOTHER:
// not sure if this needs decoding at all
break;
default:
// it is either not encoded or we don't know about it
} // switch ends
} // else ends
} // function DisectThisPart ends
// main entry point
if ($db_link = mysql_connect($sql_host,$sql_user,$sql_pw)) {
@mysql_select_db("phpwebmail");
$sql_query = "SELECT folder,textmode FROM prefs WHERE user='$name'";
$result = mysql_query($sql_query);
// catch SQL errors
if (mysql_errno()) echo ("ERROR ".mysql_error());
$row = mysql_fetch_row($result);
$folder_pre = $row[0];
$textmode = $row[1];
if ($folder_pre && substr($folder_pre,-1)!="/") $folder_pre .= "/";
mysql_close($db_link);
} // if db_link ends
if ($message == 0) {
echo ("<B>Error:</B> No message specified<BR>\n");
echo ("<BR><BR>");
} else {
// some mime decoding and encoding functions
require("mime.php");
// read in the message
$mbox = imap_open("{".$imap_server.":143}".$folder,$name,$passwd);
//$mbox = 0;
if ($mbox == 0) {
print("<B>Error:</B> Could not log in to ".$name."@".$imap_server."<BR>");
print("<B>Session ID:</B> ".$sesid."<br>");
} else {
$head = imap_headerinfo($mbox,$message,40,40,$imap_server);
$date = $head->date;
$from = stripslashes(strip_tags(htmlspecialchars($head->fromaddress)));
$to = stripslashes(strip_tags(htmlspecialchars($head->toaddress)));
$cc = stripslashes(strip_tags(htmlspecialchars($head->ccaddress)));
$subject = decode_mime_string($head->subject);
// get the first body part of the message (= Messagetext)
// after determining if HTML mail was sent as multipart
$full_header = imap_fetchbody($mbox, $message, 0);
if (eregi ("Content-Type: multipart/alternative", $full_header)) $body_part = 2; else $body_part = 1;
// but what if there is no message text, just an attachment (MS Outlook)
if (eregi ("Content-Type: application/", $full_header)) $no_text = 1;
if (!$no_text) {
$body = imap_fetchbody($mbox, $message, $body_part);
$body = stripslashes(quoted_printable_decode($body));
// reverse unnecessary linebreaks "=CR"
$body = preg_replace ("'=\n'", "", $body);
// remove unwanted MIME headers from body (MS OE HTML mail!)
$body = eregi_replace("------=_NextPart_.*quoted-printable.*------=_NextPart_.*quoted-printable","",$body);
$body = eregi_replace("------=_NextPart_.*--","",$body);
// HTML mail show the formatted part nicely
if (eregi("<html>", $body)) {
if (eregi("Content-Type: text/html", $body)) {
$begin_html = strpos(strtoupper($body),"<HTML>");
$end_html = strpos(strtoupper($body),"</HTML>");
$body = substr($body, $begin_html, $end_html - $begin_html);
}
} else {
// show plain text nicely
// make real blanks (for fixed width text mail)
$body = wordwrap ($body);
$body = preg_replace ("' '", " ", $body);
$body = nl2br($body);
} // if ... else ends
} // if textbody ends
echo "<B>Date: ".$date."<BR>
From: ".$from." <a href=\"address.php?".SID."&from_email=".urlencode($from)."\">";
if ($textmode) echo "add to address book"; else echo "<img src=\"images/_add.gif\" border=0 align=\"bottom\" alt=\"add to address book\">";
echo "</a><BR>
To: </b>".$to."<b><BR>
Cc: </b>".$cc."<b><BR>
Subject: ".$subject."</B><BR>
<BR>\n".$body."<BR><BR>";
// Attachments
$structure = imap_fetchstructure($mbox, $message);
$attach = sizeof($structure->parts)-1;
if ($attach>=1) DisectThisPart($structure,".");
// some Attachments (from MS Outlook) without a message body text need a
// disection of the main message (no MIME parts!)
if ($no_text) DisectThisPart($structure,0);
$dummy = imap_close($mbox);
}
}
echo $folder;
?>
<hr>
<FORM ACTION="move_mail.php?<?=SID?>" target="message_text" METHOD="POST">
<font face="arial" size=2><b>Move this message to
<?php if (!$textmode) echo "<img src=images/folder.gif> "; ?>
</b>
<SELECT NAME="select_folder" onChange="parent.frames[2].location.href='move_mail.php?<?=SID?>&message=<?php echo $message; ?>&folder_new=' + this.options[this.selectedIndex].value">
<?php
$mailbox = imap_open("{".$imap_server.":143}$folder", $name, $passwd);
if ($mailbox) {
$mailboxes = imap_lsub($mailbox, "{".$imap_server.":143}", "*");
if (count($mailboxes) > 0) {
for ($index = 0; $index < count($mailboxes); $index++) {
$folder1 = substr(stristr($mailboxes[$index],"}"),1);
$the_folder = $folder1;
$has_pre = strstr($the_folder,$folder_pre);
if ($has_pre) $the_folder = substr($has_pre,strlen($folder_pre));
print("<OPTION VALUE=\"".urlencode($folder1)."\"");
if (strtolower($folder) == strtolower($has_pre) || strtolower($folder) == strtolower($folder1)) print(" SELECTED ");
print(">".$the_folder."\n");
}
} else {
print("<OPTION VALUE=\"INBOX\">INBOX");
}
imap_close($mailbox);
} else {
print("<OPTION VALUE=\"Error\">Error");
}
?>
</SELECT>
</FORM>
</BODY></HTML>