public function cron() { $db = new Database(); $result = $db->execute('SELECT * FROM stream_email'); $result->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'EmailEntity'); $emailStreams = $result->fetchAll(); $articles = array(); /** @var EmailEntity $emailEntity */ foreach ($emailStreams as $emailEntity) { $firstEmail = $this->getFirstArticle($emailEntity); $lastEmail = $this->getLastArticle($emailEntity); $connection = $this->connect($emailEntity->getServer(), $emailEntity->getPort(), $emailEntity->getAccount(), $emailEntity->getPassword()); $stream = $connection['conn']; $date = date("d M Y", strtotime($emailEntity->getFirstUpdate())); $emails = imap_search($stream, 'SINCE "' . $date . '"'); if (count($emails)) { foreach ($emails as $email) { $overview = imap_fetch_overview($stream, $email, 0); $structure = imap_fetchstructure($stream, $overview[0]->uid, FT_UID); switch ($structure->encoding) { case 4: $body = imap_qprint(imap_fetchbody($stream, imap_msgno($stream, $overview[0]->uid), 1)); break; case 3: $body = base64_decode(imap_fetchbody($stream, imap_msgno($stream, $overview[0]->uid), 1)); break; case 1: $body = imap_qprint(imap_fetchbody($stream, imap_msgno($stream, $overview[0]->uid), 1)); break; case 0: $body = quoted_printable_decode(imap_fetchbody($stream, imap_msgno($stream, $overview[0]->uid), 1)); break; default: $body = imap_fetchbody($stream, imap_msgno($stream, $overview[0]->uid), 1); } $article = new ArticleEntity(); $subject = isset($overview[0]->subject) ? $this->decode_imap_text($overview[0]->subject) : 'Sans object'; //$structure->encoding : ENCODAGE $article->setTitle($subject . ' - ' . $this->decode_imap_text($overview[0]->from)); $article->setContent($this->getBody($overview[0]->uid, $stream)); $article->setArticleDate(date(Database::DATE_FORMAT, strtotime($overview[0]->date))); $article->setStreamType(ArticleModel::EMAIL); $article->setStreamId($emailEntity->getId()); $article->setUrl(''); $articles[] = $article; } } /** @var ArticleEntity $article */ foreach ($articles as $article) { if (!$firstEmail || strtotime($article->getArticleDate()) < strtotime($firstEmail->getArticleDate()) || !$lastEmail || strtotime($article->getArticleDate()) > strtotime($lastEmail->getArticleDate())) { $article->persist(); } } } return $articles; }
private function getLastArticle(TwitterEntity $twitterStream) { $result = $this->db->execute('SELECT * FROM article WHERE stream_id = ? AND streamType = ? ORDER BY articleDate DESC LIMIT 1', array($twitterStream->getId(), ArticleModel::TWITTER)); $result->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'ArticleEntity'); if ($articleEntity = $result->fetch()) { return $articleEntity; } $articleEntity = new ArticleEntity(); $articleEntity->setArticleDate(time()); return $articleEntity; }