Esempio n. 1
0
 function GetMessage($folderid, $id, $truncsize, $bodypreference = false, $optionbodypreference = false, $mimesupport = 0)
 {
     debugLog("IMAP-GetMessage: (fid: '" . $this->_folders[$folderid] . "'  id: '" . $id . "'  truncsize: {$truncsize})");
     // Get flags, etc
     $stat = $this->StatMessage($folderid, $id);
     if ($stat) {
         $this->imap_reopenFolder($folderid);
         if (defined("IMAP_USE_FETCHHEADER") && IMAP_USE_FETCHHEADER === false) {
             $mail = @imap_fetchbody($this->_mbox, $id, "", FT_UID | FT_PEEK);
         } else {
             $mail = @imap_fetchheader($this->_mbox, $id, FT_UID) . @imap_body($this->_mbox, $id, FT_PEEK | FT_UID);
         }
         // Return in case any errors occured...
         $errors = imap_errors();
         if (is_array($errors)) {
             foreach ($errors as $e) {
                 debugLog("IMAP-errors: {$e}");
                 $fields = explode(':', $e);
                 switch (strtolower(trim($fields[0]))) {
                     case 'security problem':
                         // don't care about security problems!
                         break;
                     default:
                         return false;
                 }
             }
         }
         $mobj = new Mail_mimeDecode($mail);
         $message = $mobj->decode(array('decode_headers' => true, 'decode_bodies' => true, 'rfc_822bodies' => true, 'include_bodies' => true, 'input' => $mail, 'crlf' => "\n", 'charset' => BACKEND_CHARSET));
         $output = new SyncMail();
         // start AS12 Stuff (bodypreference === false) case = old behaviour
         if ($bodypreference === false) {
             $body = $this->getBody($message);
             $body = str_replace("\n", "\r\n", str_replace("\r", "", $body));
             // truncate body, if requested
             if (strlen($body) > $truncsize) {
                 $body = utf8_truncate($body, $truncsize);
                 $output->bodytruncated = 1;
             } else {
                 $body = $body;
                 $output->bodytruncated = 0;
             }
             $output->bodysize = strlen($body);
             $output->body = $body;
         } else {
             if (isset($bodypreference[1]) && !isset($bodypreference[1]["TruncationSize"])) {
                 $bodypreference[1]["TruncationSize"] = 1024 * 1024;
             }
             if (isset($bodypreference[2]) && !isset($bodypreference[2]["TruncationSize"])) {
                 $bodypreference[2]["TruncationSize"] = 1024 * 1024;
             }
             if (isset($bodypreference[3]) && !isset($bodypreference[3]["TruncationSize"])) {
                 $bodypreference[3]["TruncationSize"] = 1024 * 1024;
             }
             if (isset($bodypreference[4]) && !isset($bodypreference[4]["TruncationSize"])) {
                 $bodypreference[4]["TruncationSize"] = 1024 * 1024;
             }
             $output->airsyncbasebody = new SyncAirSyncBaseBody();
             debugLog("airsyncbasebody!");
             $body = "";
             $this->getBodyRecursive($message, "html", $body);
             if ($body != "") {
                 $output->airsyncbasenativebodytype = 2;
             } else {
                 $output->airsyncbasenativebodytype = 1;
                 $this->getBodyRecursive($message, "plain", $body);
             }
             $body = str_replace("\n", "\r\n", str_replace("\r", "", $body));
             if (isset($bodypreference[4]) && ($mimesupport == 2 || $mimesupport == 1 && strtolower($message->ctype_secondary) == 'signed')) {
                 debugLog("MIME Body");
                 $output->airsyncbasebody->type = 4;
                 $rawmessage = $mobj->decode(array('decode_headers' => false, 'decode_bodies' => true, 'rfc_822bodies' => true, 'include_bodies' => true, 'input' => $mail, 'crlf' => "\n", 'charset' => BACKEND_CHARSET));
                 $body = "";
                 foreach ($rawmessage->headers as $key => $value) {
                     if ($key != "content-type" && $key != "mime-version" && $key != "content-transfer-encoding" && !is_array($value)) {
                         $body .= $key . ":";
                         // Split -> Explode replace
                         $tokens = explode(" ", trim($value));
                         $line = "";
                         foreach ($tokens as $valu) {
                             if (strlen($line) + strlen($valu) + 2 > 60) {
                                 $line .= "\n";
                                 $body .= $line;
                                 $line = " " . $valu;
                             } else {
                                 $line .= " " . $valu;
                             }
                         }
                         $body .= $line . "\n";
                     }
                 }
                 unset($rawmessage);
                 $mimemsg = new Mail_mime(array('head_encoding' => 'quoted-printable', 'text_encoding' => 'quoted-printable', 'html_encoding' => 'base64', 'head_charset' => 'utf-8', 'text_charset' => 'utf-8', 'html_charset' => 'utf-8', 'eol' => "\n", 'delay_file_io' => false));
                 $this->getAllAttachmentsRecursive($message, $mimemsg);
                 if ($output->airsyncbasenativebodytype == 1) {
                     $this->getBodyRecursive($message, "plain", $plain);
                     $this->getBodyRecursive($message, "html", $html);
                     if ($html == "") {
                         $this->getBodyRecursive($message, "plain", $html);
                     }
                     if ($html == "" && $plain == "" && strlen($mobj->_body) != "") {
                         $body .= "Content-Type:" . $message->headers['content-type'] . "\r\n";
                         $body .= "Content-Transfer-Encoding:" . $message->headers['content-transfer-encoding'] . "\r\n";
                         $body .= "\n\n" . $mobj->_body;
                         $output->airsyncbasebody->data = $body;
                     }
                     $mimemsg->setTXTBody(str_replace("\n", "\r\n", str_replace("\r", "", w2u($plain))));
                     $html = '<html>' . '<head>' . '<meta name="Generator" content="Z-Push">' . '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . '</head>' . '<body>' . str_replace("\n", "<BR>", str_replace("\r", "", str_replace("\r\n", "<BR>", w2u($html)))) . '</body>' . '</html>';
                     $mimemsg->setHTMLBody(str_replace("\n", "\r\n", str_replace("\r", "", $html)));
                 }
                 if ($output->airsyncbasenativebodytype == 2) {
                     $this->getBodyRecursive($message, "plain", $plain);
                     if ($plain == "") {
                         $this->getBodyRecursive($message, "html", $plain);
                         // remove css-style tags
                         $plain = preg_replace("/<style.*?<\\/style>/is", "", $plain);
                         // remove all other html
                         $plain = preg_replace("/<br.*>/is", "<br>", $plain);
                         $plain = preg_replace("/<br >/is", "<br>", $plain);
                         $plain = preg_replace("/<br\\/>/is", "<br>", $plain);
                         $plain = str_replace("<br>", "\r\n", $plain);
                         $plain = strip_tags($plain);
                     }
                     $mimemsg->setTXTBody(str_replace("\n", "\r\n", str_replace("\r", "", w2u($plain))));
                     $this->getBodyRecursive($message, "html", $html);
                     $mimemsg->setHTMLBody(str_replace("\n", "\r\n", str_replace("\r", "", w2u($html))));
                 }
                 if (!isset($output->airsyncbasebody->data)) {
                     $output->airsyncbasebody->data = $body . $mimemsg->txtheaders() . "\n\n" . $mimemsg->get();
                 }
                 $output->airsyncbasebody->estimateddatasize = byte_strlen($output->airsyncbasebody->data);
             } else {
                 if (isset($bodypreference[2])) {
                     debugLog("HTML Body");
                     // Send HTML if requested and native type was html
                     $output->airsyncbasebody->type = 2;
                     $this->getBodyRecursive($message, "plain", $plain);
                     $this->getBodyRecursive($message, "html", $html);
                     if ($html == "") {
                         $this->getBodyRecursive($message, "plain", $html);
                     }
                     if ($html == "" && $plain == "" && byte_strlen($mobj->_body) > 0) {
                         $plain = $html = $mobj->_quotedPrintableDecode($mobj->_body);
                     }
                     if ($output->airsyncbasenativebodytype == 2) {
                         $html = w2u($html);
                     } else {
                         $html = '<html>' . '<head>' . '<meta name="Generator" content="Z-Push">' . '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . '</head>' . '<body>' . str_replace("\n", "<BR>", str_replace("\r", "<BR>", str_replace("\r\n", "<BR>", w2u($plain)))) . '</body>' . '</html>';
                     }
                     if (isset($bodypreference[2]["TruncationSize"]) && strlen($html) > $bodypreference[2]["TruncationSize"]) {
                         $html = utf8_truncate($html, $bodypreference[2]["TruncationSize"]);
                         $output->airsyncbasebody->truncated = 1;
                     }
                     $output->airsyncbasebody->data = $html;
                     $output->airsyncbasebody->estimateddatasize = byte_strlen($html);
                 } else {
                     // Send Plaintext as Fallback or if original body is plaintext
                     debugLog("Plaintext Body");
                     $plain = $this->getBody($message);
                     $plain = w2u(str_replace("\n", "\r\n", str_replace("\r", "", $plain)));
                     $output->airsyncbasebody->type = 1;
                     if (isset($bodypreference[1]["TruncationSize"]) && strlen($plain) > $bodypreference[1]["TruncationSize"]) {
                         $plain = utf8_truncate($plain, $bodypreference[1]["TruncationSize"]);
                         $output->airsyncbasebody->truncated = 1;
                     }
                     $output->airsyncbasebody->estimateddatasize = byte_strlen($plain);
                     $output->airsyncbasebody->data = $plain;
                 }
             }
             // In case we have nothing for the body, send at least a blank...
             // dw2412 but only in case the body is not rtf!
             if ($output->airsyncbasebody->type != 3 && (!isset($output->airsyncbasebody->data) || byte_strlen($output->airsyncbasebody->data) == 0)) {
                 $output->airsyncbasebody->data = " ";
             }
         }
         // end AS12 Stuff
         // small dirty correction for (i.e. Tobit David) since it has the opinion the UTC Timezone abbreviation is UT :-(
         $output->datereceived = isset($message->headers["date"]) ? strtotime($message->headers["date"] . (substr($message->headers["date"], -3) == " UT" ? "C" : "")) : null;
         $output->importance = isset($message->headers["x-priority"]) ? preg_replace("/\\D+/", "", $message->headers["x-priority"]) : null;
         $output->messageclass = "IPM.Note";
         if (strtolower($message->ctype_primary) == "multipart") {
             switch (strtolower($message->ctype_secondary)) {
                 case 'signed':
                     $output->messageclass = "IPM.Note.SMIME.MultipartSigned";
                     break;
                 default:
                     $output->messageclass = "IPM.Note";
             }
         }
         $output->subject = isset($message->headers["subject"]) ? trim(w2u($message->headers["subject"])) : "";
         $output->read = $stat["flags"];
         if (isset($message->headers["to"]) && (preg_match('/^\\"{0,1}(.+[^ ^\\"]){0,1}\\"{0,1}[ ]{0,1}<(.*)>$/', $message->headers["to"], $addrparts) || preg_match('/^(.*@.*)$/', $message->headers["to"], $addrparts))) {
             $output->to = trim(w2u('"' . ($addrparts[1] != "" || $addrparts[2] == "" ? $addrparts[1] : $addrparts[2]) . '" <' . (!isset($addrparts[2]) || $addrparts[2] == "" ? $addrparts[1] : $addrparts[2]) . '>'));
             $output->displayto = trim(w2u($addrparts[1] != "" ? $addrparts[1] : $addrparts[2]));
             unset($addrparts);
         } else {
             $output->to = trim(w2u('"Unknown@localhost" <Unknown@localhost>'));
         }
         if (isset($message->headers["from"]) && (preg_match('/^\\"{0,1}(.+[^ ^\\"]){0,1}\\"{0,1}[ ]{0,1}<(.*)>$/', $message->headers["from"], $addrparts) || preg_match('/^(.*@.*)$/', $message->headers["from"], $addrparts))) {
             $output->from = trim(w2u('"' . ($addrparts[1] != "" || $addrparts[2] == "" ? $addrparts[1] : $addrparts[2]) . '" <' . (!isset($addrparts[2]) || $addrparts[2] == "" ? $addrparts[1] : $addrparts[2]) . '>'));
             unset($addrparts);
         } else {
             $output->from = trim(w2u('"Unknown@localhost" <Unknown@localhost>'));
         }
         $output->cc = isset($message->headers["cc"]) ? trim(w2u($message->headers["cc"])) : null;
         $output->reply_to = isset($message->headers["reply-to"]) ? trim(w2u($message->headers["reply-to"])) : null;
         // start AS12 Stuff
         $output->poommailflag = new SyncPoommailFlag();
         $output->poommailflag->flagstatus = 0;
         $output->internetcpid = 65001;
         $output->contentclass = "urn:content-classes:message";
         // end AS12 Stuff
         // Attachments are only searched in the top-level part
         if (isset($message->parts)) {
             $this->getAttachmentDetailsRecursive($message, $output, $folderid, $id);
         }
         // unset mimedecoder & mail
         unset($mobj);
         unset($mail);
         return $output;
     }
     return false;
 }