This commit has been accessed 658 times via Git panel.
commit ca95a2490c38ae1b18f66486432be48f639d05c4
tree 9cad005d8587d7001b997299e1a80d77f094cf6a
parent 385090d544148bc54f1331eff9c6d10b34f0cee8
author Engels Antonio <engels@majcms.org> 1300439122 +0800
committer Engels Antonio <engels@majcms.org> 1300439122 +0800
Improve headers, message body and attachments handling
diff --git a/core.php b/core.php
index a0c973d..eab0489 100644
--- a/core.php
+++ b/core.php
@@ -150,4 +150,19 @@ if ($server_post_max_size > $server_memory_limit) {
$max_file_size = $server_memory_limit;
}
+function header_mime2text ($header) {
+
+ $header_decode = imap_mime_header_decode(str_replace(array('\r\n', '\r', '\n'), ' ', $header));
+
+ foreach ($header_decode as $header_decoded) {
+
+ $header_text = $header_text . $header_decoded->text;
+ }
+
+ $header_text = mb_convert_encoding($header_text,"UTF-8","auto");
+ $header_text = mb_convert_encoding($header_text,"UTF-8","UTF-8");
+
+ return ($header_text);
+}
+
?>
diff --git a/folders.php b/folders.php
index 9ffce98..09c36ac 100644
--- a/folders.php
+++ b/folders.php
@@ -71,12 +71,13 @@ if (is_array($folders)) {
reset($folders);
sort($folders);
+/*
$folder_images = glob("images/folder*");
foreach ($folder_images as $folder_image) {
echo "\n\n<script>\n\tvar img_preload = new Image();\n\timg_preload.src = \"$folder_image\";\n</script>";
}
-
+*/
echo "\n\n<div class=\"folders\"><table border=\"0\" cellspacing=\"1\" callpadding=\"2\">";
foreach ($folders as $folder) {
diff --git a/inbox.php b/inbox.php
index bc7e5ac..c4b19ed 100644
--- a/inbox.php
+++ b/inbox.php
@@ -52,12 +52,14 @@ imap_headers($mbox);
$mbox_sort = imap_sort($mbox,"SORTARRIVAL",1);
+/*
$mail_images = glob("images/mail*");
foreach ($mail_images as $mail_image) {
echo "\n\n<script>\n\tvar img_preload = new Image();\n\timg_preload.src = \"$mail_image\";\n</script>";
}
-
+*/
+
require_once("core.php");
echo "<title>Kartero - $folder ($msgCount) - " . $_SESSION['logged_uid'] . "</title>";
@@ -101,40 +103,37 @@ foreach ($mbox_sort as $mbox_num => $mbox_row) {
}
$val = imap_fetch_overview($mbox,$mbox_row);
- $msgno=$val[0]->msgno;
- $seen=$val[0]->seen;
- $recent=$val[0]->recent;
- $answered=$val[0]->answered;
- $size=$val[0]->size;
+ $msgno = $val[0]->msgno;
+ $seen = $val[0]->seen;
+ $recent = $val[0]->recent;
+ $answered = $val[0]->answered;
+ $size = $val[0]->size;
if ($folder == "Sent") {
- $from=$val[0]->to;
+ $from = header_mime2text($val[0]->to);
}
else {
- $from=$val[0]->from;
+ $from = header_mime2text($val[0]->from);
}
- $subj=$val[0]->subject;
- $date=$val[0]->date;
-
- $subj = imap_utf8($subj);
- $subj = utf8_decode($subj);
-
- $from = imap_utf8($from);
- $from = utf8_decode($from);
-
$from = htmlentities($from,ENT_QUOTES);
$from = "<b>" . $from;
$from = str_replace(""","",$from);
$from = str_replace(">","",$from);
$from = str_replace(" <","</b><br>",$from);
+ $from = ereg_replace("\"","",$from);
+
+ $subj = header_mime2text($val[0]->subject);
+ if (strlen($subj) > 75) {
+ $subj = substr($subj,0,74) . "...";
+ }
+
+ $date=$val[0]->date;
$date = strtotime($date);
$date = time_elapsed_string($date);
$size = HumanReadableFilesize($size);
-
- $from = ereg_replace("\"","",$from);
if ($bgColor == "#ffffff") {
$bgColor = "#fdfdfd";
@@ -142,12 +141,6 @@ foreach ($mbox_sort as $mbox_num => $mbox_row) {
else {
$bgColor = "#ffffff";
}
-
- if (strlen($subj) > 75) {
- $subj = substr($subj,0,74) . "...";
- }
-
- $subj = htmlentities($subj,ENT_QUOTES);
if ($seen == "0") {
$status_img = "mail-unread.png";
diff --git a/index.php b/index.php
index da542d1..07a08ef 100644
--- a/index.php
+++ b/index.php
@@ -883,9 +883,9 @@ if (isset($_SESSION['logged_uid'])) {
else {
include("inbox.php");
-#echo "<script type=\"text/javascript\" src=\"jquery-1.3.2.min.js\"></script>";
+//echo "<script type=\"text/javascript\" src=\"jquery-1.3.2.min.js\"></script>";
-#echo "<script type=\"text/javascript\"> $(document).ready(function() { setInterval(function() { $('#main').fadeOut(\"fast\").load('inbox.php').fadeIn(\"slow\"); }, 5000); }); </script>";
+//echo "<script type=\"text/javascript\"> $(document).ready(function() { setInterval(function() { $('#main').fadeOut(\"fast\").load('inbox.php').fadeIn(\"slow\"); }, 5000); }); </script>";
}
echo "</div></td>";
diff --git a/read.php b/read.php
index 0e5a0b2..4668c1f 100644
--- a/read.php
+++ b/read.php
@@ -25,12 +25,20 @@ if (isset($_POST['msg']) and !empty($_POST['msg']) and is_numeric($_POST['msg'])
$header = imap_headerinfo($mbox, $msgno);
- $header_subject = htmlentities($header->subject,ENT_QUOTES);
- $header_fromaddress = htmlentities($header->fromaddress,ENT_QUOTES);
- $header_date = htmlentities($header->date,ENT_QUOTES);
+ $header_subject = header_mime2text($header->subject);
+
+ $header_fromaddress = header_mime2text($header->fromaddress);
+ $header_fromaddress = htmlentities($header_fromaddress,ENT_QUOTES);
+
+ $header_toaddress = header_mime2text($header->toaddress);
+ $header_toaddress = htmlentities($header_toaddress,ENT_QUOTES);
+
+ $header_date = header_mime2text($header->date);
+
$header_date_age = time_elapsed_string(strtotime($header_date));
- $header_toaddress = htmlentities($header->toaddress,ENT_QUOTES);
- $header_ccaddress = htmlentities($header->ccaddress,ENT_QUOTES);
+
+ $header_ccaddress = header_mime2text($header->ccaddress);
+ $header_ccaddress = htmlentities($header_ccaddress,ENT_QUOTES);
$header_fetch = imap_fetchheader($mbox,$msgno);
@@ -215,91 +223,59 @@ if (isset($_POST['msg']) and !empty($_POST['msg']) and is_numeric($_POST['msg'])
if ($dataHtml != "") {
$msgBody = $dataHtml;
- } else {
+ }
+ else {
$msgBody = $dataTxt;
- $msgBody = imap_utf8($msgBody);
- $msgBody = utf8_decode($msgBody);
$msgBody = ereg_replace("\n","<br>",$msgBody);
$msgBody = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i","$1http://$2",$msgBody);
$msgBody = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</a>",$msgBody);
$msgBody = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<a href=\"mailto:$1\">$1</a>",$msgBody);
}
- echo $msgBody;
+ //$msgBody = imap_utf8($msgBody);
+ //$msgBody = utf8_decode($msgBody);
- $struct = imap_fetchstructure($mbox, $msgno);
+ $msgBody = mb_convert_encoding($msgBody,"UTF-8","auto");
+ $msgBody = mb_convert_encoding($msgBody,"UTF-8","UTF-8");
- $contentParts = count($struct->parts);
-
- if ($contentParts >= 2) {
-
- for ($i=2;$i<=$contentParts;$i++) {
- $att[$i-2] = imap_bodystruct($mbox,$msgno,$i);
- }
-
- for ($k=0;$k<sizeof($att);$k++) {
-
- if ($att[$k]->parameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value == "US-ASCII") {
-
- if ($att[$k]->parameters[1]->value != "") {
- $attachDisplay[$k] = $att[$k]->parameters[1]->value;
- }
-
- }
- elseif ($att[$k]->parameters[0]->value != "iso-8859-1" && $att[$k]->parameters[0]->value != "ISO-8859-1") {
- $attachDisplay[$k] = $att[$k]->parameters[0]->value;
- }
- }
- }
+ echo $msgBody;
echo "</div>";
-/*
- foreach ($attachDisplay as $attachD => $attachE) {
-
- if (strtolower($attachE) == "utf-8") {
- unset($attachDisplay[$attachD]);
- }
-
- if (strtolower($attachE) == "windows-1252") {
- unset($attachDisplay[$attachD]);
- }
-
- if (strtolower($attachE) == "iso-8859-15") {
- unset($attachDisplay[$attachD]);
- }
- }
-*/
- reset($attachDisplay);
$do_fromaddress = extract_emails_from($header->fromaddress);
$do_toaddress = extract_emails_from($header->toaddress);
- $filePath = $_SERVER['SERVER_NAME'] . $do_fromaddress[0] . $do_toaddress[0] . $header->subject;
-
+ $filePath = $_SERVER['SERVER_NAME'] . $do_fromaddress[0] . $do_toaddress[0] . $header_subject;
$filePath = "filedrop/" . sha1($filePath);
- if (sizeof($attachDisplay) > 0) {
-
- for ($j=0;$j<sizeof($attachDisplay);$j++) {
-
- if ($dataHtml != "") {
-
- $file = $j;
- $fileContent = base64_decode(imap_fetchbody($mbox,$msgno,$file+2));
- $fileName = preg_replace("/[^a-z0-9_\-\.]/i","",$attachDisplay[$j]);
-
- if (!file_exists($filePath)) {
- mkdir($filePath,0700,1);
- }
-
- if (!file_exists("$filePath/$fileName")) {
- file_put_contents("$filePath/$fileName",$fileContent);
- }
+ $struct = imap_fetchstructure($mbox, $msgno);
- $fileSize = filesize("$filePath/$fileName");
+ $contentParts = count($struct->parts);
- if ($fileSize <= 0) {
- unlink("$filePath/$fileName");
+ for ($n = 0; $n <= $contentParts; $n++) {
+
+ $contentPart = imap_bodystruct($mbox,$msgno,$n);
+
+ if (($contentPart->disposition == "attachment") or ($contentPart->disposition == "inline")) {
+
+ if ($contentPart->bytes > 0) {
+
+ $contentDP = $contentPart->dparameters;
+
+ if ($contentDP[0]->attribute == "filename") {
+
+ $fileName = $contentDP[0]->value;
+ $fileName = preg_replace("/[^a-z0-9_\-\.]/i","",$fileName);
+
+ $fileContent = base64_decode(imap_fetchbody($mbox,$msgno,$n));
+
+ if (!file_exists($filePath)) {
+ mkdir($filePath,0700,1);
+ }
+
+ if (!file_exists("$filePath/$fileName")) {
+ file_put_contents("$filePath/$fileName",$fileContent);
+ }
}
}
}
tree 9cad005d8587d7001b997299e1a80d77f094cf6a
parent 385090d544148bc54f1331eff9c6d10b34f0cee8
author Engels Antonio <engels@majcms.org> 1300439122 +0800
committer Engels Antonio <engels@majcms.org> 1300439122 +0800
Improve headers, message body and attachments handling
diff --git a/core.php b/core.php
index a0c973d..eab0489 100644
--- a/core.php
+++ b/core.php
@@ -150,4 +150,19 @@ if ($server_post_max_size > $server_memory_limit) {
$max_file_size = $server_memory_limit;
}
+function header_mime2text ($header) {
+
+ $header_decode = imap_mime_header_decode(str_replace(array('\r\n', '\r', '\n'), ' ', $header));
+
+ foreach ($header_decode as $header_decoded) {
+
+ $header_text = $header_text . $header_decoded->text;
+ }
+
+ $header_text = mb_convert_encoding($header_text,"UTF-8","auto");
+ $header_text = mb_convert_encoding($header_text,"UTF-8","UTF-8");
+
+ return ($header_text);
+}
+
?>
diff --git a/folders.php b/folders.php
index 9ffce98..09c36ac 100644
--- a/folders.php
+++ b/folders.php
@@ -71,12 +71,13 @@ if (is_array($folders)) {
reset($folders);
sort($folders);
+/*
$folder_images = glob("images/folder*");
foreach ($folder_images as $folder_image) {
echo "\n\n<script>\n\tvar img_preload = new Image();\n\timg_preload.src = \"$folder_image\";\n</script>";
}
-
+*/
echo "\n\n<div class=\"folders\"><table border=\"0\" cellspacing=\"1\" callpadding=\"2\">";
foreach ($folders as $folder) {
diff --git a/inbox.php b/inbox.php
index bc7e5ac..c4b19ed 100644
--- a/inbox.php
+++ b/inbox.php
@@ -52,12 +52,14 @@ imap_headers($mbox);
$mbox_sort = imap_sort($mbox,"SORTARRIVAL",1);
+/*
$mail_images = glob("images/mail*");
foreach ($mail_images as $mail_image) {
echo "\n\n<script>\n\tvar img_preload = new Image();\n\timg_preload.src = \"$mail_image\";\n</script>";
}
-
+*/
+
require_once("core.php");
echo "<title>Kartero - $folder ($msgCount) - " . $_SESSION['logged_uid'] . "</title>";
@@ -101,40 +103,37 @@ foreach ($mbox_sort as $mbox_num => $mbox_row) {
}
$val = imap_fetch_overview($mbox,$mbox_row);
- $msgno=$val[0]->msgno;
- $seen=$val[0]->seen;
- $recent=$val[0]->recent;
- $answered=$val[0]->answered;
- $size=$val[0]->size;
+ $msgno = $val[0]->msgno;
+ $seen = $val[0]->seen;
+ $recent = $val[0]->recent;
+ $answered = $val[0]->answered;
+ $size = $val[0]->size;
if ($folder == "Sent") {
- $from=$val[0]->to;
+ $from = header_mime2text($val[0]->to);
}
else {
- $from=$val[0]->from;
+ $from = header_mime2text($val[0]->from);
}
- $subj=$val[0]->subject;
- $date=$val[0]->date;
-
- $subj = imap_utf8($subj);
- $subj = utf8_decode($subj);
-
- $from = imap_utf8($from);
- $from = utf8_decode($from);
-
$from = htmlentities($from,ENT_QUOTES);
$from = "<b>" . $from;
$from = str_replace(""","",$from);
$from = str_replace(">","",$from);
$from = str_replace(" <","</b><br>",$from);
+ $from = ereg_replace("\"","",$from);
+
+ $subj = header_mime2text($val[0]->subject);
+ if (strlen($subj) > 75) {
+ $subj = substr($subj,0,74) . "...";
+ }
+
+ $date=$val[0]->date;
$date = strtotime($date);
$date = time_elapsed_string($date);
$size = HumanReadableFilesize($size);
-
- $from = ereg_replace("\"","",$from);
if ($bgColor == "#ffffff") {
$bgColor = "#fdfdfd";
@@ -142,12 +141,6 @@ foreach ($mbox_sort as $mbox_num => $mbox_row) {
else {
$bgColor = "#ffffff";
}
-
- if (strlen($subj) > 75) {
- $subj = substr($subj,0,74) . "...";
- }
-
- $subj = htmlentities($subj,ENT_QUOTES);
if ($seen == "0") {
$status_img = "mail-unread.png";
diff --git a/index.php b/index.php
index da542d1..07a08ef 100644
--- a/index.php
+++ b/index.php
@@ -883,9 +883,9 @@ if (isset($_SESSION['logged_uid'])) {
else {
include("inbox.php");
-#echo "<script type=\"text/javascript\" src=\"jquery-1.3.2.min.js\"></script>";
+//echo "<script type=\"text/javascript\" src=\"jquery-1.3.2.min.js\"></script>";
-#echo "<script type=\"text/javascript\"> $(document).ready(function() { setInterval(function() { $('#main').fadeOut(\"fast\").load('inbox.php').fadeIn(\"slow\"); }, 5000); }); </script>";
+//echo "<script type=\"text/javascript\"> $(document).ready(function() { setInterval(function() { $('#main').fadeOut(\"fast\").load('inbox.php').fadeIn(\"slow\"); }, 5000); }); </script>";
}
echo "</div></td>";
diff --git a/read.php b/read.php
index 0e5a0b2..4668c1f 100644
--- a/read.php
+++ b/read.php
@@ -25,12 +25,20 @@ if (isset($_POST['msg']) and !empty($_POST['msg']) and is_numeric($_POST['msg'])
$header = imap_headerinfo($mbox, $msgno);
- $header_subject = htmlentities($header->subject,ENT_QUOTES);
- $header_fromaddress = htmlentities($header->fromaddress,ENT_QUOTES);
- $header_date = htmlentities($header->date,ENT_QUOTES);
+ $header_subject = header_mime2text($header->subject);
+
+ $header_fromaddress = header_mime2text($header->fromaddress);
+ $header_fromaddress = htmlentities($header_fromaddress,ENT_QUOTES);
+
+ $header_toaddress = header_mime2text($header->toaddress);
+ $header_toaddress = htmlentities($header_toaddress,ENT_QUOTES);
+
+ $header_date = header_mime2text($header->date);
+
$header_date_age = time_elapsed_string(strtotime($header_date));
- $header_toaddress = htmlentities($header->toaddress,ENT_QUOTES);
- $header_ccaddress = htmlentities($header->ccaddress,ENT_QUOTES);
+
+ $header_ccaddress = header_mime2text($header->ccaddress);
+ $header_ccaddress = htmlentities($header_ccaddress,ENT_QUOTES);
$header_fetch = imap_fetchheader($mbox,$msgno);
@@ -215,91 +223,59 @@ if (isset($_POST['msg']) and !empty($_POST['msg']) and is_numeric($_POST['msg'])
if ($dataHtml != "") {
$msgBody = $dataHtml;
- } else {
+ }
+ else {
$msgBody = $dataTxt;
- $msgBody = imap_utf8($msgBody);
- $msgBody = utf8_decode($msgBody);
$msgBody = ereg_replace("\n","<br>",$msgBody);
$msgBody = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i","$1http://$2",$msgBody);
$msgBody = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</a>",$msgBody);
$msgBody = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<a href=\"mailto:$1\">$1</a>",$msgBody);
}
- echo $msgBody;
+ //$msgBody = imap_utf8($msgBody);
+ //$msgBody = utf8_decode($msgBody);
- $struct = imap_fetchstructure($mbox, $msgno);
+ $msgBody = mb_convert_encoding($msgBody,"UTF-8","auto");
+ $msgBody = mb_convert_encoding($msgBody,"UTF-8","UTF-8");
- $contentParts = count($struct->parts);
-
- if ($contentParts >= 2) {
-
- for ($i=2;$i<=$contentParts;$i++) {
- $att[$i-2] = imap_bodystruct($mbox,$msgno,$i);
- }
-
- for ($k=0;$k<sizeof($att);$k++) {
-
- if ($att[$k]->parameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value == "US-ASCII") {
-
- if ($att[$k]->parameters[1]->value != "") {
- $attachDisplay[$k] = $att[$k]->parameters[1]->value;
- }
-
- }
- elseif ($att[$k]->parameters[0]->value != "iso-8859-1" && $att[$k]->parameters[0]->value != "ISO-8859-1") {
- $attachDisplay[$k] = $att[$k]->parameters[0]->value;
- }
- }
- }
+ echo $msgBody;
echo "</div>";
-/*
- foreach ($attachDisplay as $attachD => $attachE) {
-
- if (strtolower($attachE) == "utf-8") {
- unset($attachDisplay[$attachD]);
- }
-
- if (strtolower($attachE) == "windows-1252") {
- unset($attachDisplay[$attachD]);
- }
-
- if (strtolower($attachE) == "iso-8859-15") {
- unset($attachDisplay[$attachD]);
- }
- }
-*/
- reset($attachDisplay);
$do_fromaddress = extract_emails_from($header->fromaddress);
$do_toaddress = extract_emails_from($header->toaddress);
- $filePath = $_SERVER['SERVER_NAME'] . $do_fromaddress[0] . $do_toaddress[0] . $header->subject;
-
+ $filePath = $_SERVER['SERVER_NAME'] . $do_fromaddress[0] . $do_toaddress[0] . $header_subject;
$filePath = "filedrop/" . sha1($filePath);
- if (sizeof($attachDisplay) > 0) {
-
- for ($j=0;$j<sizeof($attachDisplay);$j++) {
-
- if ($dataHtml != "") {
-
- $file = $j;
- $fileContent = base64_decode(imap_fetchbody($mbox,$msgno,$file+2));
- $fileName = preg_replace("/[^a-z0-9_\-\.]/i","",$attachDisplay[$j]);
-
- if (!file_exists($filePath)) {
- mkdir($filePath,0700,1);
- }
-
- if (!file_exists("$filePath/$fileName")) {
- file_put_contents("$filePath/$fileName",$fileContent);
- }
+ $struct = imap_fetchstructure($mbox, $msgno);
- $fileSize = filesize("$filePath/$fileName");
+ $contentParts = count($struct->parts);
- if ($fileSize <= 0) {
- unlink("$filePath/$fileName");
+ for ($n = 0; $n <= $contentParts; $n++) {
+
+ $contentPart = imap_bodystruct($mbox,$msgno,$n);
+
+ if (($contentPart->disposition == "attachment") or ($contentPart->disposition == "inline")) {
+
+ if ($contentPart->bytes > 0) {
+
+ $contentDP = $contentPart->dparameters;
+
+ if ($contentDP[0]->attribute == "filename") {
+
+ $fileName = $contentDP[0]->value;
+ $fileName = preg_replace("/[^a-z0-9_\-\.]/i","",$fileName);
+
+ $fileContent = base64_decode(imap_fetchbody($mbox,$msgno,$n));
+
+ if (!file_exists($filePath)) {
+ mkdir($filePath,0700,1);
+ }
+
+ if (!file_exists("$filePath/$fileName")) {
+ file_put_contents("$filePath/$fileName",$fileContent);
+ }
}
}
}