Exemplo n.º 1
0
 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("&lt;", "&gt;"), 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;
     }
 }