Ejemplo n.º 1
0
 public static function getBody($id, &$conn, &$obj, $header)
 {
     $structure_str = $obj->fetchStructureString($conn, "INBOX", $id);
     $structure = EmailMimeUtil::getRawStructureArray($structure_str);
     $num_parts = EmailMimeUtil::getNumParts($structure);
     $parent_type = EmailMimeUtil::getPartTypeCode($structure);
     if ($parent_type == 1 && $num_parts == 1) {
         $part = 1;
         $num_parts = EmailMimeUtil::getNumParts($structure, $part);
         $parent_type = EmailMimeUtil::getPartTypeCode($structure, $part);
     } else {
         $part = null;
     }
     $body = array();
     $attach = "";
     if (0 < $num_parts) {
         $attach .= "<table width=100%>\n";
         for ($i = 1; $i <= $num_parts; $i++) {
             if ($parent_type == 1) {
                 $code = $part . (empty($part) ? "" : ".") . $i;
             } elseif ($parent_type == 2) {
                 $code = $part . (empty($part) ? "" : ".") . $i;
             }
             $type = EmailMimeUtil::getPartTypeCode($structure, $code);
             $name = EmailMimeUtil::getPartName($structure, $code);
             if (is_string($name) && !empty($name)) {
                 $name = htmlspecialchars(EmailLangUtil::langDecodeSubject($name, CHARSET));
                 $fileExt = StringUtil::getFileExt($name);
                 $fileType = AttachUtil::attachType($fileExt);
             } else {
                 $fileType = AttachUtil::attachType(1);
             }
             $typestring = EmailMimeUtil::getPartTypeString($structure, $code);
             list($dummy, $subtype) = explode("/", $typestring);
             $bytes = EmailMimeUtil::getPartSize($structure, $code);
             $disposition = EmailMimeUtil::getPartDisposition($structure, $code);
             if ($type == 1 || $type == 2 || $type == 3 && strcasecmp($subtype, "ms-tnef") == 0) {
                 continue;
             } else {
                 $href = Yii::app()->urlManager->createUrl("email/web/show", array("webid" => self::$_web["webid"], "folder" => "INBOX", "id" => $id, "part" => $code));
             }
             $attach .= "<tr><td align=\"center\"><img src=\"{$fileType}\" border=0></td>";
             $attach .= "<td><a href=\"" . $href . "\" " . ($type == 1 || $type == 2 || $type == 3 && strcasecmp($subtype, "ms-tnef") == 0 ? "" : "target=_blank") . ">";
             $attach .= "<span class=\"small\">" . $name . "</span></a>";
             if (0 < $bytes) {
                 $attach .= "<td>[" . ConvertUtil::sizeCount($bytes) . "]</td>\n";
             }
             if (is_string($typestring)) {
                 $attach .= "<td>" . htmlspecialchars($typestring) . "</td>\n";
             }
             $attach .= "\n</tr>\n";
         }
         $attach .= "</table>\n";
     }
     $typeCode = EmailMimeUtil::getPartTypeCode($structure, $part);
     list($dummy, $subType) = explode("/", EmailMimeUtil::getPartTypeString($structure, $part));
     if ($typeCode == 3 && strcasecmp($subType, "ms-tnef") == 0) {
         $type = $dummy;
     } elseif ($typeCode == 0) {
         $typeString = EmailMimeUtil::getPartTypeString($structure, $part);
         if (empty($part) && !empty($header->ctype) && strcmp($typeString, $header->ctype) != 0) {
             $typeString = $header->ctype;
         }
         list($type, $subType) = explode("/", $typeString);
         $body[] = self::fetchBody($obj, $conn, "INBOX", $id, $structure, $part);
     } else {
         if ($typeCode == 1 && empty($part) && $structure[0][0] == "message") {
             $part = "1.1";
             $typeString = EmailMimeUtil::getPartTypeString($structure, $part);
             list($type, $subType) = explode("/", $typeString);
             $typeCode = EmailMimeUtil::getPartTypeCode($structure, $part);
             $disposition = EmailMimeUtil::getPartDisposition($structure, $part);
             $body[] = self::fetchBody($obj, $conn, "INBOX", $id, $structure, $part);
         } else {
             if ($typeCode == 1 || $typeCode == 2) {
                 $typeString = EmailMimeUtil::getPartTypeString($structure, $part);
                 list($type, $subType) = explode("/", $typeString);
                 $mode = 0;
                 $subtypes = array("mixed" => 1, "signed" => 1, "related" => 1, "array" => 2, "alternative" => 2);
                 $subType = strtolower($subType);
                 if (0 < $subtypes[$subType]) {
                     $mode = $subtypes[$subType];
                 } elseif (strcasecmp($subType, "rfc822") == 0) {
                     $temp_num = EmailMimeUtil::getNumParts($structure, $part);
                     if (0 < $temp_num) {
                         $mode = 2;
                     }
                 } elseif (strcasecmp($subType, "encrypted") == 0) {
                     $encrypted_type = EmailMimeUtil::getPartTypeString($structure, $part . ".1");
                     if (stristr($encrypted_type, "pgp-encrypted") !== false) {
                         $mode = -1;
                     }
                 }
                 if ($mode == -1) {
                     $part = $part . (empty($part) ? "" : ".") . "2";
                     $typeString = EmailMimeUtil::getPartTypeString($structure, $part);
                     list($type, $subType) = explode("/", $typeString);
                     $typeCode = EmailMimeUtil::getPartTypeCode($structure, $part);
                     $disposition = EmailMimeUtil::getPartDisposition($structure, $part);
                     $body[] = self::fetchBody($obj, $conn, "INBOX", $id, $structure, $part);
                 } elseif (0 < $mode) {
                     $originalPart = $part;
                     for ($i = 1; $i <= $num_parts; $i++) {
                         $part = $originalPart . (empty($originalPart) ? "" : ".") . $i;
                         $typeString = EmailMimeUtil::getPartTypeString($structure, $part);
                         list($type, $subType) = explode("/", $typeString);
                         $typeCode = EmailMimeUtil::getPartTypeCode($structure, $part);
                         $disposition = EmailMimeUtil::getPartDisposition($structure, $part);
                         if (strcasecmp($disposition, "attachment") != 0) {
                             if ($mode == 1 && $typeCode == 0) {
                                 $body[] = self::fetchBody($obj, $conn, "INBOX", $id, $structure, $part);
                             } elseif ($mode == 2) {
                                 $body[] = self::fetchBody($obj, $conn, "INBOX", $id, $structure, $part);
                             } else {
                                 if ($typeCode == 5 && strcasecmp($disposition, "inline") == 0) {
                                     $href = Yii::app()->urlManager->createUrl("email/web/show", array("webid" => self::$_web["webid"], "folder" => "INBOX", "id" => $id, "part" => $part));
                                     $body[] = "<img src='{$href}'>";
                                 } elseif ($typeCode == 1) {
                                     $part = EmailMimeUtil::getFirstTextPart($structure, $part);
                                     $next_part = EmailMimeUtil::getNextPart($part);
                                     $next_type = EmailMimeUtil::getPartTypeString($structure, $next_part);
                                     if (stristr($next_type, "html") !== false) {
                                         $part = $next_part;
                                     }
                                     $i++;
                                     $body[] = self::fetchBody($obj, $conn, "INBOX", $id, $structure, $part);
                                 }
                             }
                         } elseif ($typeCode == 5) {
                             $href = Yii::app()->urlManager->createUrl("email/web/show", array("webid" => self::$_web["webid"], "folder" => "INBOX", "id" => $id, "part" => $part));
                             $body[] = "<img src='{$href}'>";
                         }
                     }
                 } else {
                     if (strcasecmp($subType, "rfc822") != 0) {
                         $part = EmailMimeUtil::getFirstTextPart($structure, "");
                         $next_part = EmailMimeUtil::getNextPart($part);
                         $next_type = EmailMimeUtil::getPartTypeString($structure, $next_part);
                         if (stristr($next_type, "html") !== false) {
                             $typeString = "text/html";
                             $type = "text";
                             $subType = "html";
                             $part = $next_part;
                         }
                     }
                     $body[] = self::fetchBody($obj, $conn, "INBOX", $id, $structure, $part);
                 }
             } else {
                 $type = EmailMimeUtil::getPartTypeCode($structure, $part);
                 $partName = EmailMimeUtil::getPartName($structure, $part);
                 $typeString = EmailMimeUtil::getPartTypeString($structure, $part);
                 $bytes = EmailMimeUtil::getPartSize($structure, $part);
                 $disposition = EmailMimeUtil::getPartDisposition($structure, $part);
                 $name = EmailLangUtil::langDecodeSubject($partName, CHARSET);
                 $fileExt = StringUtil::getFileExt($name);
                 $fileType = AttachUtil::attachType($fileExt);
                 $size = ConvertUtil::sizeCount($bytes);
                 $href = Yii::app()->urlManager->createUrl("email/web/show", array("webid" => self::$_web["webid"], "folder" => "INBOX", "id" => $id, "part" => $part));
                 //$body[] = "\t\t\t\t\t<table>\r\n\t\t\t\t\t\t<tr>\r\n\t\t\t\t\t\t\t<td align=\"center\">\r\n\t\t\t\t\t\t\t\t<a href=\"$href\" target=\"_blank\"><img src=\"$fileType\" border=0 /><br/>$name<br/>[$size]<br/></a>\r\n\t\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t</tr>\r\n\t\t\t\t\t</table><br/>";
                 $body[] = '<table>
                     <tr>
                         <td align="center">
                             <a href="$href" target="_blank"><img src="' . $fileType . '" border=0 /><br/>' . $name . '<br/>[' . $size . ']<br/></a>
                         </td>
                     </tr>
                 </table><br/>';
             }
         }
     }
     $body[] = $attach;
     return $body;
 }
Ejemplo n.º 2
0
 public function actionShow()
 {
     $webId = intval(EnvUtil::getRequest("webid"));
     $id = intval(EnvUtil::getRequest("id"));
     $folder = EnvUtil::getRequest("folder");
     $part = EnvUtil::getRequest("part");
     $cid = EnvUtil::getRequest("cid");
     $web = EmailWeb::model()->fetchByPk($webId);
     if (intval($web["uid"]) !== $this->uid) {
         exit;
     }
     list($prefix) = explode(".", $web["server"]);
     $user = User::model()->fetchByUid($web["uid"]);
     $pwd = StringUtil::authCode($web["password"], "DECODE", $user["salt"]);
     if ($prefix == "imap") {
         $obj = new WebMailImap();
     } else {
         $obj = new WebMailPop();
     }
     $conn = $obj->connect($web["server"], $web["username"], $pwd, $web["ssl"], $web["port"], "plain");
     if (!$conn) {
         exit("Login failed");
     } else {
         if (strpos(getenv("HTTP_USER_AGENT"), "MSIE")) {
             $dispositionMode = "inline";
         } else {
             $dispositionMode = "attachment";
         }
         $header = $obj->fetchHeader($conn, $folder, $id);
         if (!$header) {
             exit;
         }
         $structure_str = $obj->fetchStructureString($conn, $folder, $id);
         $structure = EmailMimeUtil::getRawStructureArray($structure_str);
         if (!$part && $cid) {
             $parts_list = EmailMimeUtil::getPartList($structure, "");
             if (is_array($parts_list)) {
                 reset($parts_list);
                 while (list($part_id, $part_a) = each($parts_list)) {
                     if ($part_a["id"] == $cid) {
                         $part = $part_id;
                     }
                 }
             }
             if (!isset($part)) {
                 exit;
             }
         }
         if (isset($source)) {
         } elseif (isset($show_header)) {
         } elseif (isset($printer_friendly)) {
         } elseif (isset($tneffid)) {
         } else {
             $header_obj = $header;
             $type = EmailMimeUtil::getPartTypeCode($structure, $part);
             if (empty($part) || $part == 0) {
                 $typestr = $header_obj->ctype;
             } else {
                 $typestr = EmailMimeUtil::getPartTypeString($structure, $part);
             }
             list($majortype, $subtype) = explode("/", $typestr);
             if ($type == EmailMimeUtil::MIME_APPLICATION) {
                 $name = str_replace("/", ".", EmailMimeUtil::getPartName($structure, $part));
                 header("Content-type: {$typestr}; name=\"" . $name . "\"");
                 header("Content-Disposition: " . $dispositionMode . "; filename=\"" . $name . "\"");
                 header("Expires: 0");
                 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                 header("Pragma: public");
             } elseif ($type == EmailMimeUtil::MIME_MESSAGE) {
                 $name = str_replace("/", ".", EmailMimeUtil::getPartName($structure, $part));
                 header("Content-Type: text/plain; name=\"" . $name . "\"");
             } elseif ($type != EmailMimeUtil::MIME_INVALID) {
                 $charset = EmailMimeUtil::getPartCharset($structure, $part);
                 $name = str_replace("/", ".", EmailMimeUtil::getPartName($structure, $part));
                 $header = "Content-type: {$typestr}";
                 if (!empty($charset)) {
                     $header .= "; charset=\"" . $charset . "\"";
                 }
                 if (!empty($name)) {
                     $header .= "; name=\"" . $name . "\"";
                 }
                 header($header);
                 if ($type != EmailMimeUtil::MIME_TEXT && $type != EmailMimeUtil::MIME_IMAGE) {
                     header("Content-Disposition: " . $dispositionMode . "; filename=\"" . $name . "\"");
                     header("Expires: 0");
                     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                     header("Pragma: public");
                 } elseif (!empty($name)) {
                     header("Content-Disposition: inline; filename=\"" . $name . "\"");
                 }
             }
             if ($type == EmailMimeUtil::MIME_TEXT && strcasecmp($subtype, "html") == 0) {
                 $is_html = true;
                 $img_url = Yii::app()->urlManager->createUrl("email/web/show", array("webid" => $webId, "folder" => $folder, "id" => $id, "cid" => ""));
             } else {
                 $is_html = false;
                 $img_url = "";
             }
             if (isset($print)) {
                 $obj->printPartBody($conn, $folder, $id, $part);
             } else {
                 $encoding = EmailMimeUtil::getPartEncodingCode($structure, $part);
                 if (isset($raw) && $raw) {
                     $obj->printPartBody($conn, $folder, $id, $part);
                 } elseif ($encoding == 3) {
                     if ($is_html) {
                         $body = $obj->fetchPartBody($conn, $folder, $id, $part);
                         $body = preg_replace("/[^a-zA-Z0-9\\/\\+]/", "", $body);
                         $body = base64_decode($body);
                         $body = preg_replace("/src=\"cid:/", "src=\"" . $img_url, $body);
                         RyosImapUtil::sanitizeHTML($body);
                         echo $body;
                     } else {
                         $obj->printBase64Body($conn, $folder, $id, $part);
                     }
                 } elseif ($encoding == 4) {
                     $body = $obj->fetchPartBody($conn, $folder, $id, $part);
                     $body = quoted_printable_decode(str_replace("=\r\n", "", $body));
                     if ($is_html) {
                         RyosImapUtil::sanitizeHTML($body);
                         $body = preg_replace("/src=\"cid:/", "src=\"" . $img_url, $body);
                     }
                     echo $body;
                 } elseif ($is_html) {
                     $body = $obj->fetchPartBody($conn, $folder, $id, $part);
                     RyosImapUtil::sanitizeHTML($body);
                     $body = preg_replace("/src=\"cid:/", "src=\"" . $img_url, $body);
                     echo $body;
                 } else {
                     $obj->printPartBody($conn, $folder, $id, $part);
                 }
             }
             $obj->close($conn);
         }
     }
 }