Ejemplo n.º 1
0
 function deleteMailsFromServer(MailAccount $account)
 {
     $count = 0;
     if ($account->getDelFromServer() > 0) {
         $max_date = DateTimeValueLib::now();
         $max_date->add('d', -1 * $account->getDelFromServer());
         if ($account->getIsImap()) {
             if ($account->getIncomingSsl()) {
                 $imap = new Net_IMAP($ret, "ssl://" . $account->getServer(), $account->getIncomingSslPort());
             } else {
                 $imap = new Net_IMAP($ret, "tcp://" . $account->getServer());
             }
             if (PEAR::isError($ret)) {
                 Logger::log($ret->getMessage());
                 throw new Exception($ret->getMessage());
             }
             $ret = $imap->login($account->getEmail(), self::ENCRYPT_DECRYPT($account->getPassword()));
             $result = array();
             if ($ret === true) {
                 $mailboxes = MailAccountImapFolders::getMailAccountImapFolders($account->getId());
                 if (is_array($mailboxes)) {
                     foreach ($mailboxes as $box) {
                         if ($box->getCheckFolder()) {
                             $numMessages = $imap->getNumberOfMessages(utf8_decode($box->getFolderName()));
                             for ($i = 1; $i <= $numMessages; $i++) {
                                 $summary = $imap->getSummary($i);
                                 if (is_array($summary)) {
                                     $m_date = DateTimeValueLib::makeFromString($summary[0]['INTERNALDATE']);
                                     if ($m_date instanceof DateTimeValue && $max_date->getTimestamp() > $m_date->getTimestamp()) {
                                         if (MailContents::mailRecordExists($account->getId(), $summary[0]['UID'], $box->getFolderName(), null)) {
                                             $imap->deleteMessages($i);
                                             $count++;
                                         }
                                     } else {
                                         break;
                                     }
                                 }
                             }
                             $imap->expunge();
                         }
                     }
                 }
             }
         } else {
             //require_once "Net/POP3.php";
             $pop3 = new Net_POP3();
             // Connect to mail server
             if ($account->getIncomingSsl()) {
                 $pop3->connect("ssl://" . $account->getServer(), $account->getIncomingSslPort());
             } else {
                 $pop3->connect($account->getServer());
             }
             if (PEAR::isError($ret = $pop3->login($account->getEmail(), self::ENCRYPT_DECRYPT($account->getPassword()), 'USER'))) {
                 throw new Exception($ret->getMessage());
             }
             $emails = $pop3->getListing();
             foreach ($emails as $email) {
                 if (MailContents::mailRecordExists($account->getId(), $email['uidl'], null, null)) {
                     $headers = $pop3->getParsedHeaders($email['msg_id']);
                     $date = DateTimeValueLib::makeFromString(array_var($headers, 'Date'));
                     if ($date instanceof DateTimeValue && $max_date->getTimestamp() > $date->getTimestamp()) {
                         $pop3->deleteMsg($email['msg_id']);
                         $count++;
                     }
                 }
             }
             $pop3->disconnect();
         }
     }
     return $count;
 }