Application->makeClass('POP3Helper', $connection_info);
/* @var $pop3_helper POP3Helper */
$connection_status = $pop3_helper->initMailbox();
if (is_string($connection_status)) {
return $connection_status;
}
if (defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML('Reading MAILBOX: ' . $connection_info['username']);
}
// Figure out if all messages are huge
$only_big_messages = true;
$max_message_size = $this->maxMegabytes * (1024 * 1024);
foreach ($pop3_helper->messageSizes as $message_size) {
if (($message_size <= $max_message_size) && ($max_message_size > 0)) {
$only_big_messages = false;
break;
}
}
$count = $total_size = 0;
foreach ($pop3_helper->messageSizes as $message_number => $message_size) {
// Too many messages?
if (($count++ > $this->maxMessages) && ($this->maxMessages > 0)) {
break;
}
// Message too big?
if (!$only_big_messages && ($message_size > $max_message_size) && ($max_message_size > 0)) {
$this->_displayLogMessage('message #' . $message_number . ' too big, skipped');
continue;
}
// Processed enough for today?
if (($total_size > $max_message_size) && ($max_message_size > 0)) {
break;
}
$total_size += $message_size;
$pop3_helper->getEmail($message_number, $message_source);
$processed = $this->normalize($message_source, $verify_callback, $process_callback, $callback_params, $include_attachment_contents);
if ($processed) {
// delete message from server immediatly after retrieving & processing
$pop3_helper->deleteEmail($message_number);
$this->_displayLogMessage('message #' . $message_number . ': processed');
}
else {
$this->_displayLogMessage('message #' . $message_number . ': skipped');
}
}
$pop3_helper->close();
return 'success';
}
/**
* Displays log message
*
* @param string $text
*/
function _displayLogMessage($text)
{
if (defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML($text);
}
}
/**
* Takes an RFC822 formatted date, returns a unix timestamp (allowing for zone)
*
* @param string $rfcdate
* @return int
*/
function rfcToTime($rfcdate)
{
$date = strtotime($rfcdate);
if ($date == -1) {
return false;
}
return $date;
}
/**
* Gets recipients from all possible headers
*
* @return string
*/
function getRecipients()
{
$ret = '';
// headers that could contain recipients
$recipient_headers = Array (
'to', 'cc', 'envelope-to', 'resent-to', 'delivered-to',
'apparently-to', 'envelope-to', 'x-envelope-to', 'received',
);
foreach ($recipient_headers as $recipient_header) {
if (!array_key_exists($recipient_header, $this->headers)) {
continue;
}
if (!is_array($this->headers["$recipient_header"])) {
$ret .= ' ' . $this->headers["$recipient_header"];
} else {
$ret .= ' ' . implode(' ', $this->headers["$recipient_header"]);
}
}
return $ret;
}
/**
* "Flattens" the multi-demensinal headers array into a single dimension one
*
* @param Array $input
* @param string $add
* @return Array
*/
function flattenHeadersArray($input, $add = '')
{
$output = Array ();
foreach ($input as $key => $value) {
if (!empty($add)) {
$newkey = ucfirst( strtolower($add) );
} elseif (is_numeric($key)) {
$newkey = '';
} else {
$newkey = ucfirst( strtolower($key) );
}
if (is_array($value)) {
$output = array_merge($output, $this->flattenHeadersArray($value, $newkey));
} else {
$output[] = (!empty($newkey) ? $newkey . ': ' : '') . $value;
}
}
return $output;
}
/**
* Processes given message using given callbacks
*
* @param string $message
* @param Array $verify_callback
* @param Array $process_callback
* @param bool $include_attachment_contents
* @return bool
*/
function normalize($message, $verify_callback, $process_callback, $callback_params, $include_attachment_contents = true)
{
// Decode message
$this->decodeMime($message, $include_attachment_contents);
// Init vars; $good will hold all the correct infomation from now on
$good = Array ();
// trim() some stuff now instead of later
$this->headers['from'] = trim($this->headers['from']);
$this->headers['to'] = trim($this->headers['to']);
$this->headers['cc'] = array_key_exists('cc', $this->headers) ? trim($this->headers['cc']) : '';
$this->headers['x-forward-to'] = array_key_exists('x-forward-to', $this->headers) ? $this->headers['x-forward-to'] : '';
$this->headers['subject'] = trim($this->headers['subject']);
$this->headers['received'] = is_array($this->headers['received']) ? $this->headers['received'] : Array ($this->headers['received']);
if (array_key_exists('return-path', $this->headers) && is_array($this->headers['return-path'])) {
$this->headers['return-path'] = implode(' ', $this->flattenHeadersArray($this->headers['return-path']));
}
// Create our own message-ID if it's missing
$message_id = array_key_exists('message-id', $this->headers) ? trim($this->headers['message-id']) : '';
$good['emailid'] = $message_id ? $message_id : md5($message) . "@in-portal";
// Stops us looping in stupid conversations with other mail software
if (isset($this->headers['x-loop-detect']) && $this->headers['x-loop-detect'] > 2) {
return false;
}
$esender =& $this->Application->recallObject('EmailSender');
/* @var $esender kEmailSendingHelper */
// Get the return address
$return_path = '';
if (array_key_exists('return-path', $this->headers)) {
$return_path = $esender->ExtractRecipientEmail($this->headers['return-path']);
}
if (!$return_path) {
if (array_key_exists('reply-to', $this->headers)) {
$return_path = $esender->ExtractRecipientEmail( $this->headers['reply-to'] );
}
else {
$return_path = $esender->ExtractRecipientEmail( $this->headers['from'] );
}
}
// Get the sender's name & email
$good['fromemail'] = $esender->ExtractRecipientEmail($this->headers['from']);
$good['fromname'] = $esender->ExtractRecipientName($this->headers['from'], $good['fromemail']);
// Get the list of recipients
if (!$verify_callback[0]->$verify_callback[1]($callback_params)) {
// error: mail is propably spam
return false;
}
// Handle the subject
$good['subject'] = $this->headers['subject'];
// Priorities rock
$good['priority'] = array_key_exists('x-priority', $this->headers) ? (int)$this->headers['x-priority'] : 0;
switch ($good['priority']) {
case 1: case 5: break;
default:
$good['priority'] = 3;
}
// If we have attachments it's about time we tell the user about it
if (array_key_exists('attachments', $this->parsedMessage) && is_array($this->parsedMessage['attachments'])) {
$good['attach'] = count( $this->parsedMessage['attachments'] );
} else {
$good['attach'] = 0;
}
// prepare message text (for replies, etc)
if (isset($this->parsedMessage['text'][0]) && trim($this->parsedMessage['text'][0]['body']) != '') {
$message_body = trim($this->parsedMessage['text'][0]['body']);
$message_type = 'text';
} elseif (isset($this->parsedMessage['html']) && trim($this->parsedMessage['html'][0]['body']) != '') {
$message_body = trim($this->parsedMessage['html'][0]['body']);
$message_type = 'html';
} else {
$message_body = '[no message]';
$message_type = 'text';
}
// remove scripts
$message_body = preg_replace("/