private function getNewImapMails(MailAccount $account, $max = 0)
 {
     $received = 0;
     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()));
     $mailboxes = MailAccountImapFolders::getMailAccountImapFolders($account->getId());
     if (is_array($mailboxes)) {
         foreach ($mailboxes as $box) {
             if ($max > 0 && $received >= $max) {
                 break;
             }
             if ($box->getCheckFolder()) {
                 if ($imap->selectMailbox(utf8_decode($box->getFolderName()))) {
                     $oldUids = $account->getUids($box->getFolderName());
                     $numMessages = $imap->getNumberOfMessages(utf8_decode($box->getFolderName()));
                     if (!is_array($oldUids) || count($oldUids) == 0 || PEAR::isError($numMessages) || $numMessages == 0) {
                         $lastReceived = 0;
                         if (PEAR::isError($numMessages)) {
                             //Logger::log($numMessages->getMessage());
                             continue;
                         }
                     } else {
                         $lastReceived = 0;
                         $maxUID = $account->getMaxUID($box->getFolderName());
                         $imin = 1;
                         $imax = $numMessages;
                         $i = floor(($imax + $imin) / 2);
                         while (true) {
                             $summary = $imap->getSummary($i);
                             if (PEAR::isError($summary)) {
                                 $i--;
                                 if ($i == 0) {
                                     break;
                                 }
                                 continue;
                             }
                             $iprev = $i;
                             $uid = $summary[0]['UID'];
                             if ($maxUID > $uid) {
                                 $imin = $i;
                                 $lastReceived = $imin;
                             } else {
                                 if ($maxUID < $uid) {
                                     $imax = $i;
                                 } else {
                                     $lastReceived = $i;
                                     break;
                                 }
                             }
                             $i = floor(($imax + $imin) / 2);
                             if ($i == $iprev) {
                                 break;
                             }
                         }
                     }
                     $uids = MailContents::getUidsFromAccount($account->getId(), $box->getFolderName());
                     // get mails since last received (last received is not included)
                     for ($i = $lastReceived; ($max == 0 || $received < $max) && $i < $numMessages; $i++) {
                         $index = $i + 1;
                         $summary = $imap->getSummary($index);
                         if (PEAR::isError($summary)) {
                             Logger::log($summary->getMessage());
                         } else {
                             if (!in_array($summary[0]['UID'], $uids)) {
                                 if ($imap->isDraft($index)) {
                                     $state = 2;
                                 } else {
                                     $state = 0;
                                 }
                                 $messages = $imap->getMessages($index);
                                 if (PEAR::isError($messages)) {
                                     continue;
                                 }
                                 $content = array_var($messages, $index, '');
                                 if ($content != '') {
                                     try {
                                         $stop_checking = self::SaveMail($content, $account, $summary[0]['UID'], $state, $box->getFolderName());
                                         if ($stop_checking) {
                                             break;
                                         }
                                         $received++;
                                     } catch (Exception $e) {
                                         $mail_file = ROOT . "/tmp/unsaved_mail_" . $summary[0]['UID'] . ".eml";
                                         $res = file_put_contents($mail_file, $content);
                                         if ($res === false) {
                                             $mail_file = ROOT . "/tmp/unsaved_mail_" . gen_id() . ".eml";
                                             $res = file_put_contents($mail_file, $content);
                                             if ($res === false) {
                                                 Logger::log("Could not save mail, and original could not be saved as {$mail_file}, exception:\n" . $e->getMessage());
                                             } else {
                                                 Logger::log("Could not save mail, original mail saved as {$mail_file}, exception:\n" . $e->getMessage());
                                             }
                                         } else {
                                             Logger::log("Could not save mail, original mail saved as {$mail_file}, exception:\n" . $e->getMessage());
                                         }
                                     }
                                 }
                                 // if content
                             }
                         }
                     }
                 }
             }
         }
     }
     $imap->disconnect();
     return $received;
 }
 private function getNewImapMails(MailAccount $account, $max = 0)
 {
     $received = 0;
     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)) {
         throw new Exception($ret->getMessage());
     }
     $ret = $imap->login($account->getEmail(), self::ENCRYPT_DECRYPT($account->getPassword()), null, false);
     $mailboxes = MailAccountImapFolders::getMailAccountImapFolders($account->getId());
     if (is_array($mailboxes)) {
         foreach ($mailboxes as $box) {
             if ($max > 0 && $received >= $max) {
                 break;
             }
             if ($box->getCheckFolder()) {
                 //if the account is configured to mark as read emails on server call selectMailBox else call examineMailBox.
                 if ($account->getMarkReadOnServer() > 0 ? $imap->selectMailbox(utf8_decode($box->getFolderName())) : $imap->examineMailbox(utf8_decode($box->getFolderName()))) {
                     $oldUids = $account->getUids($box->getFolderName(), 1);
                     $numMessages = $imap->getNumberOfMessages(utf8_decode($box->getFolderName()));
                     if (!is_array($oldUids) || count($oldUids) == 0 || PEAR::isError($numMessages) || $numMessages == 0) {
                         if (PEAR::isError($numMessages)) {
                             continue;
                         }
                     }
                     // determine the starting uid and number of message
                     $max_uid = $account->getMaxUID($box->getFolderName());
                     $max_summary = null;
                     if ($max_uid) {
                         $max_summary = $imap->getSummary($max_uid, true);
                         if (PEAR::isError($max_summary)) {
                             Logger::log($max_summary->getMessage());
                             throw new Exception($max_summary->getMessage());
                         }
                     }
                     //check if our last mail is on mail server
                     if ($max_summary) {
                         $is_last_mail_on_mail_server = true;
                     } else {
                         $is_last_mail_on_mail_server = false;
                     }
                     //Server Data
                     $server_max_summary = $imap->getSummary($numMessages);
                     $server_max_uid = null;
                     if (PEAR::isError($server_max_summary)) {
                         Logger::log($server_max_summary->getMessage());
                     } else {
                         $server_max_uid = $server_max_summary[0]['UID'];
                     }
                     $server_min_summary = $imap->getSummary(1, false);
                     $server_min_uid = null;
                     if (PEAR::isError($server_min_summary)) {
                         Logger::log($server_min_summary->getMessage());
                     } else {
                         $server_min_uid = $server_min_summary[0]['UID'];
                     }
                     if ($max_uid) {
                         if ($is_last_mail_on_mail_server) {
                             $lastReceived = $max_summary[0]['MSG_NUM'];
                         } else {
                             if ($max_uid < $server_min_uid) {
                                 $lastReceived = 1;
                             } else {
                                 // $max_uid is betwen $server_min_uid and $server_max_uid
                                 if ($server_max_uid) {
                                     $diff_uids = $server_max_uid - $max_uid;
                                     $lastReceived = $numMessages - $diff_uids;
                                 } else {
                                     //get the complete server list of uids and msgids since $max_uid
                                     $server_uids_list = $imap->getMessagesListUid($max_uid . ':*');
                                     if (count($server_uids_list)) {
                                         $lastReceived = $server_uids_list[0]["msg_id"];
                                     } else {
                                         $lastReceived = 1;
                                     }
                                 }
                             }
                             $lastReceived = $lastReceived - 1;
                         }
                     } else {
                         //we don't have any mails on the system yet
                         $lastReceived = 0;
                     }
                     if ($lastReceived < 0) {
                         $lastReceived = 0;
                     }
                     // get mails since last received (last received is not included)
                     for ($i = $lastReceived; ($max == 0 || $received < $max) && $i < $numMessages; $i++) {
                         $index = $i + 1;
                         $summary = $imap->getSummary($index);
                         if (PEAR::isError($summary)) {
                             Logger::log($summary->getMessage());
                         } else {
                             if ($summary[0]['UID']) {
                                 if ($imap->isDraft($index)) {
                                     $state = 2;
                                 } else {
                                     $state = 0;
                                 }
                                 //get the state (read/unread) from the server
                                 if ($imap->isSeen($index)) {
                                     $read = 1;
                                 } else {
                                     $read = 0;
                                 }
                                 $messages = $imap->getMessages($index);
                                 if (PEAR::isError($messages)) {
                                     continue;
                                 }
                                 $content = array_var($messages, $index, '');
                                 if ($content != '') {
                                     try {
                                         $stop_checking = self::SaveMail($content, $account, $summary[0]['UID'], $state, $box->getFolderName(), $read, $received);
                                         if ($stop_checking) {
                                             break;
                                         }
                                         //$received++;
                                     } catch (Exception $e) {
                                         $mail_file = ROOT . "/tmp/unsaved_mail_" . $summary[0]['UID'] . ".eml";
                                         $res = file_put_contents($mail_file, $content);
                                         if ($res === false) {
                                             $mail_file = ROOT . "/tmp/unsaved_mail_" . gen_id() . ".eml";
                                             $res = file_put_contents($mail_file, $content);
                                             if ($res === false) {
                                                 Logger::log("Could not save mail, and original could not be saved as {$mail_file}, exception:\n" . $e->getMessage());
                                             } else {
                                                 Logger::log("Could not save mail, original mail saved as {$mail_file}, exception:\n" . $e->getMessage());
                                             }
                                         } else {
                                             Logger::log("Could not save mail, original mail saved as {$mail_file}, exception:\n" . $e->getMessage());
                                         }
                                     }
                                 }
                                 // if content
                             }
                         }
                     }
                 }
             }
         }
     }
     $imap->disconnect();
     return $received;
 }