Ejemplo n.º 1
0
 /**
  * fetch mails to parse and/or store
  *
  * @return void
  */
 public function fetchMails()
 {
     $statusArray = array('added' => array(), 'exists' => array(), 'failed' => array());
     $mailboxId = $this->attribute('id');
     $mailboxDeleteMailsFromServer = (bool) $this->attribute('delete_mails_from_server');
     if (is_object($this->TransportObject)) {
         $transport = $this->TransportObject;
         try {
             // it is possible that not all pop3 server understand this
             // array( message_num => unique_id );
             // array( 1 => '000001fc4420e93a', 2 => '000001fd4420e93a' );
             $uniqueIdentifierArray = $transport->listUniqueIdentifiers();
         } catch (Exception $e) {
             $uniqueIdentifiers = false;
             CjwNewsletterLog::writeError('CjwNewsletterMailbox::fetchMails', 'mailbox', 'listUniqueIdentifiers-failed', array('error-code' => $e->getMessage()));
         }
         try {
             // array( message_id => message_size );
             // array( 2 => 1700, 5 => 1450 );
             $messageIdArray = $transport->listMessages();
         } catch (Exception $e) {
             $messageIdNumbers = false;
             CjwNewsletterLog::writeError('CjwNewsletterMailbox::fetchMails', 'mailbox', 'listMessages-failed', array('error-code' => $e->getMessage()));
         }
         // array( message_id => message_identifier )
         $messageNumberArray = array();
         // only fetch messages from server which are not in the db
         // use message_identifier for check
         $existingMessageIdentifierArray = $this->extractAllExistingIdentifiers($uniqueIdentifierArray);
         foreach ($messageIdArray as $messageId => $messageSize) {
             if (isset($uniqueIdentifierArray[$messageId])) {
                 $uniqueIdentifier = $uniqueIdentifierArray[$messageId];
             } else {
                 $uniqueIdentifier = false;
             }
             if (array_key_exists($uniqueIdentifier, $existingMessageIdentifierArray)) {
                 $statusArray['exists'][$messageId] = $uniqueIdentifier;
             } else {
                 $messageNumberArray[$messageId] = $uniqueIdentifier;
             }
         }
         if (count($messageNumberArray) > 0) {
             // only fetch x item at once to avoid script timeout ... if call from admin frontend
             // the cronjob may be has other settings
             $fetchLimit = 50;
             $counter = 0;
             foreach ($messageNumberArray as $messageId => $messageIdentifier) {
                 if ($counter >= $fetchLimit) {
                     break;
                 } else {
                     // create mailobject from message id
                     // $mailboxDeleteMailsFromServer == true, set delete flag for current message
                     $mailObject = $transport->fetchByMessageNr($messageId, $mailboxDeleteMailsFromServer);
                     // convert mailobject to string with own function
                     $messageString = $this->convertMailToString($mailObject);
                     if ($messageIdentifier === false) {
                         $messageIdentifier = 'cjwnl_' . md5($messageString);
                     }
                     // if messageString has content
                     if ($messageString != null) {
                         // add item to DB / Filesystem
                         $addResult = CjwNewsletterMailboxItem::addMailboxItem($mailboxId, $messageIdentifier, $messageId, $messageString);
                         if (is_object($addResult)) {
                             $statusArray['added'][$messageId] = $messageIdentifier;
                         } else {
                             $statusArray['exists'][$messageId] = $messageIdentifier;
                         }
                         unset($addResult);
                     } else {
                         $statusArray['failed'][$messageId] = $messageIdentifier;
                     }
                     unset($messageString);
                     unset($mailObject);
                 }
                 $counter++;
             }
             // delete messages with delete flag from mailbox
             switch ($this->attribute('type')) {
                 case 'imap':
                     $transport->expunge();
                     break;
             }
         } else {
             return $statusArray;
         }
     }
     return $statusArray;
 }