From 7831268eb119a34cd370a5e9d7502052f033880b Mon Sep 17 00:00:00 2001 From: lkehresman Date: Wed, 5 Jan 2000 16:48:14 +0000 Subject: [PATCH] Made MIME much more efficient git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@103 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- functions/mailbox.php | 9 ++--- functions/mime.php | 78 +++++++++++++++++-------------------------- functions/smtp.php | 9 ++++- functions/strings.php | 3 ++ src/download.php | 58 ++++++++++++++++++++++++-------- src/read_body.php | 8 ++--- 6 files changed, 92 insertions(+), 73 deletions(-) diff --git a/functions/mailbox.php b/functions/mailbox.php index bb281879..4cfec184 100644 --- a/functions/mailbox.php +++ b/functions/mailbox.php @@ -264,7 +264,7 @@ } /** ENCODING TYPE **/ - else if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") { + else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") { $header["ENCODING"] = strtolower(trim(substr($read[$i], 26))); } @@ -440,7 +440,7 @@ $encoding = "us-ascii"; $i = 0; while (trim($read[$i]) != "") { - if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") { + if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") { $encoding = strtolower(trim(substr($read[$i], 26))); } else if (substr($read[$i], 0, 13) == "Content-Type:") { @@ -465,6 +465,7 @@ $i++; $read[$i] = trim($read[$i]); } + $i--; /** Detect the boundary of a multipart message **/ if (strpos(strtolower(trim($line)), "boundary=")) { @@ -506,10 +507,6 @@ $i++; } - if ( ($encoding == "us-ascii") && ($type0 != "text") && ($type0 != "message") ) { - $encoding = "base64"; - } - /** remove the header from the entity **/ $i = 0; while (trim($read[$i]) != "") { diff --git a/functions/mime.php b/functions/mime.php index 8186b747..d6e98d85 100644 --- a/functions/mime.php +++ b/functions/mime.php @@ -45,15 +45,6 @@ } $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities); $entities = $ent; - } else if ($ent_type0 == "text") { - while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") { - $entity_body[$p] = $body[$j]; - $j++; - $p++; - } - $count = count($entities); - $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename); - } else { $j++; $entity_body = ""; @@ -68,8 +59,16 @@ $i++; } } else { + /** If this isn't a multipart message **/ + $j = 0; + $entity_body = ""; + while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && ($j < count($body))) { + $entity_body .= $body[$j]; + $j++; + } + $count = count($entities); - $entities[$count] = getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename); + $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename); } return $entities; @@ -83,26 +82,7 @@ $msg["CHARSET"] = $charset; $msg["FILENAME"] = $filename; - $msg["BODY"][0] = $body; - if ($type0 == "text") { - // error correcting if they didn't follow RFC standards - if (trim($type1) == "") - $type1 = "plain"; - - if ($type1 == "plain") { - for ($p = 0;$p < count($body);$p++) { - $msg["BODY"][$p] = parsePlainTextMessage($body[$p]); - } - } else if ($type1 == "html") { - for ($p = 0;$p < count($body);$p++) { - $msg["BODY"][$p] = parseHTMLMessage($body[$p]); - } - } else { - $msg["BODY"] = $body; - } - } else { - $msg["BODY"][0] = $body; - } + $msg["BODY"] = $body; return $msg; } @@ -137,6 +117,8 @@ HTML Tags, Attachments at the bottom, etc. **/ function formatBody($message) { + include ("../config/config.php"); + /** this if statement checks for the entity to show as the primary message. To add more of them, just put them in the order that is their priority. **/ @@ -144,21 +126,27 @@ $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); } else if (containsType($message, "text", "plain", $ent_num)) { $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); + $body = "" . nl2br($body) . ""; } // add other primary displaying message types here else { // find any type that's displayable if (containsType($message, "text", "any_type", $ent_num)) { $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); + $body = "" . nl2br($body) . ""; } else if (containsType($message, "message", "any_type", $ent_num)) { $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); + $body = "" . nl2br($body) . ""; } } + $body .= "
"; + /** Display the ATTACHMENTS: message if there's more than one part **/ if (count($message["ENTITIES"]) > 1) { - $pos = count($body); - $body[$pos] .= "
ATTACHMENTS:
"; + $body .= "
"; + $body .= "ATTACHMENTS:"; + $body .= "
"; $num = 0; for ($i = 0; $i < count($message["ENTITIES"]); $i++) { @@ -180,8 +168,9 @@ $urlMailbox = urlencode($message["INFO"]["MAILBOX"]); $id = $message["INFO"]["ID"]; - $body[$pos] .= "   " . $display_filename . "  (TYPE: $type0/$type1)
"; + $body .= "   " . $display_filename . "  (TYPE: $type0/$type1)
"; } + $body .= "
"; } return $body; } @@ -191,25 +180,20 @@ /** this function decodes the body depending on the encoding type. **/ function decodeBody($body, $encoding) { $encoding = strtolower($encoding); + if ($encoding == "us-ascii") { $newbody = $body; // if only they all were this easy + } else if ($encoding == "quoted-printable") { - for ($q=0; $q < count($body); $q++) { - if (substr(trim($body[$q]), -1) == "=") { - $body[$q] = trim($body[$q]); - $body[$q] = substr($body[$q], 0, strlen($body[$q])-1); - } else if (substr(trim($body[$q]), -3) == "=20") { - $body[$q] = trim($body[$q]); - $body[$q] = substr($body[$q], 0, strlen($body[$q])-3); - $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno - } - } - for ($q=0;$q < count($body);$q++) { - $body[$q] = ereg_replace("=3D", "=", $body[$q]); - } - $newbody = $body; + echo "$body"; + $body = ereg_replace("=3D", "=", $body); + $body = ereg_replace("=\n", "", $body); + $body = ereg_replace("=20", "\n", $body); + $newbody= $body; + } else if ($encoding == "base64") { $newbody = base64_decode($body); + } else { $newbody = $body; } diff --git a/functions/smtp.php b/functions/smtp.php index ad6aa4c2..2274bc10 100644 --- a/functions/smtp.php +++ b/functions/smtp.php @@ -17,6 +17,8 @@ } function sendMessage($smtpServerAddress, $smtpPort, $username, $domain, $t, $c, $b, $subject, $body, $version) { + include("../config/config.php"); + $to = parseAddrs($t); $cc = parseAddrs($c); $bcc = parseAddrs($b); @@ -29,6 +31,8 @@ echo "Error connecting to SMTP Server.
"; echo "$errorNumber : $errorString
"; exit; + } else { + $tmp = fgets($smtpConnection, 1024); } $to_list = getLineOfAddrs($to); @@ -36,6 +40,7 @@ /** Lets introduce ourselves */ fputs($smtpConnection, "HELO $domain\n"); + /** Ok, who is sending the message? */ fputs($smtpConnection, "MAIL FROM:<$from>\n"); @@ -52,9 +57,11 @@ /** Lets start sending the actual message */ fputs($smtpConnection, "DATA\n"); + fputs($smtpConnection, "Subject: $subject\n"); // Subject fputs($smtpConnection, "From: <$from>\n"); // Subject fputs($smtpConnection, "To: <$to_list>\n"); // Who it's TO + if ($cc_list) { fputs($smtpConnection, "Cc: <$cc_list>\n"); // Who the CCs are } @@ -64,9 +71,9 @@ fputs($smtpConnection, "Content-Type: text/plain\n"); fputs($smtpConnection, "$body\n"); // send the body of the message + fputs($smtpConnection, ".\n"); // end the DATA part fputs($smtpConnection, "QUIT\n"); // log off - echo ""; } ?> \ No newline at end of file diff --git a/functions/strings.php b/functions/strings.php index 66cd5de8..dce7f1ae 100644 --- a/functions/strings.php +++ b/functions/strings.php @@ -59,6 +59,9 @@ /** Returns an array of email addresses **/ function parseAddrs($text) { + if (trim($text) == "") { + return; + } $text = str_replace(" ", "", $text); $text = str_replace(",", ";", $text); $array = explode(";", $text); diff --git a/src/download.php b/src/download.php index b69c01bb..07e6e142 100644 --- a/src/download.php +++ b/src/download.php @@ -7,6 +7,19 @@ include("../functions/mailbox.php"); include("../functions/date.php"); + function viewText($color, $body, $id, $entid, $mailbox) { + echo "\n"; + displayPageHeader($color, "None"); + + echo "
"; + echo "
Viewing a plain text attachment
"; + echo "
"; + $urlmailbox = urlencode($mailbox); + echo "Download this as a file

"; + echo nl2br($body); + echo "
"; + } + $imapConnection = loginToImapServer($username, $key, $imapServerAddress); selectMailbox($imapConnection, $mailbox, $numMessages); @@ -17,28 +30,45 @@ $type0 = $message["ENTITIES"][$passed_ent_id]["TYPE0"]; $type1 = $message["ENTITIES"][$passed_ent_id]["TYPE1"]; $filename = $message["ENTITIES"][$passed_ent_id]["FILENAME"]; - $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"][0], $message["ENTITIES"][$passed_ent_id]["ENCODING"]); + if (strlen($filename) < 1) { + $filename = "message" . time(); + } - switch ($type0) { - case "image": - if (($type1 == "jpeg") || ($type1 == "jpg") || ($type1 == "gif") || ($type1 == "png")) { - /** Add special instructions to view images inline here **/ + if ($absolute_dl == "true") { + switch($type0) { + case "text": + $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"], $message["ENTITIES"][$passed_ent_id]["ENCODING"]); + header("Content-type: $type0/$type1"); + header("Content-Disposition: attachment; filename=\"$filename\""); + if ($type1 != "html") + echo nl2br($body); + break; + default: + $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"], $message["ENTITIES"][$passed_ent_id]["ENCODING"]); header("Content-type: $type0/$type1"); header("Content-Disposition: attachment; filename=\"$filename\""); echo $body; - } else { + break; + } + } else { + switch ($type0) { + case "text": + $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"], $message["ENTITIES"][$passed_ent_id]["ENCODING"]); + viewText($color, $body, $passed_id, $passed_ent_id, $mailbox); + break; + case "message": + $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"], $message["ENTITIES"][$passed_ent_id]["ENCODING"]); + viewText($color, $body, $passed_id, $passed_ent_id, $mailbox); + break; + default: + $body = decodeBody($message["ENTITIES"][$passed_ent_id]["BODY"], $message["ENTITIES"][$passed_ent_id]["ENCODING"]); header("Content-type: $type0/$type1"); header("Content-Disposition: attachment; filename=\"$filename\""); echo $body; - } - break; - default: - header("Content-type: $type0/$type1"); - header("Content-Disposition: attachment; filename=\"$filename\""); - echo $body; - break; + break; + } } fputs($imapConnection, "1 logout\n"); -?> \ No newline at end of file +?> diff --git a/src/read_body.php b/src/read_body.php index 1ec73d19..32fd6ca0 100644 --- a/src/read_body.php +++ b/src/read_body.php @@ -147,12 +147,10 @@ $body = formatBody($message); echo "
"; - for ($i = 0; $i < count($body); $i++) { - echo "$body[$i]"; - } + echo "$body"; - echo "
\n"; - echo "  "; + echo " \n"; + echo "  "; echo "\n"; ?> \ No newline at end of file -- 2.25.1