public static function receiveMail($web) { self::$_web = $web; @set_time_limit(0); ignore_user_abort(true); 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) { return implode(",", $obj->getError()); } else { $totalNum = $obj->countMessages($conn, "INBOX"); if (0 < $totalNum) { $messagesStr = "1:" . $totalNum; } else { $messagesStr = ""; } if ($messagesStr != "") { $headers = $obj->fetchHeaders($conn, "INBOX", $messagesStr); $headers = $obj->sortHeaders($headers, "DATE", "DESC"); } else { $headers = false; } if ($headers == false) { $headers = array(); } $count = 0; if (0 < count($headers)) { while (list($key, $val) = each($headers)) { $header = $headers[$key]; $time = $header->timestamp + 28800; if ($web["lastrectime"] == 0 || $web["lastrectime"] < $time) { $count++; $data = array(); $data["subject"] = str_replace(array("<", ">"), array("<", ">"), EmailLangUtil::langDecodeSubject($header->subject, CHARSET)); $data["sendtime"] = $time; $data["towebmail"] = $web["address"]; $data["issend"] = 1; $data["fromid"] = $data["secrettoids"] = ""; $data["fromwebmail"] = EmailLangUtil::langGetParseAddressList($header->from); if (isset($header->to) && !empty($header->to)) { $data["toids"] = EmailLangUtil::langGetParseAddressList($header->to, ","); } else { $data["toids"] = ""; } if (isset($header->cc) && !empty($header->cc)) { $data["copytoids"] = EmailLangUtil::langGetParseAddressList($header->cc, ","); } else { $data["copytoids"] = ""; } $body = self::getBody($header->id, $conn, $obj, $header); $data["content"] = implode("", $body); $data["size"] = EmailUtil::getEmailSize($data["content"]); $bodyId = EmailBody::model()->add($data, true); if ($bodyId) { $email = array("toid" => $web["uid"], "isread" => 0, "fid" => $web["fid"], "isweb" => 1, "bodyid" => $bodyId); Email::model()->add($email); } } } EmailWeb::model()->updateByPk($web["webid"], array("lastrectime" => TIMESTAMP)); } return $count; } }
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); } } }