| 1 |
<?php |
| 2 |
|
| 3 |
class pPop3MailServer extends pMailServer { |
| 4 |
|
| 5 |
private $connection = null; |
| 6 |
|
| 7 |
public function __construct($connection) { |
| 8 |
$this->connection = $connection; |
| 9 |
$this->connection->open(); |
| 10 |
} |
| 11 |
|
| 12 |
public function countMessages() { |
| 13 |
$total_messages = 0; |
| 14 |
|
| 15 |
$response = $this->connection->write('STAT', '#^\+OK\s+(\d+)\s+#'); |
| 16 |
DebugEcho("pop count: " . $response[count($response) - 1]); |
| 17 |
preg_match('#^\+OK\s+(\d+)\s+#', $response[count($response) - 1], $match); |
| 18 |
$total_messages = $match[1]; |
| 19 |
|
| 20 |
return (int) $total_messages; |
| 21 |
} |
| 22 |
|
| 23 |
public function deleteMessages($uid) { |
| 24 |
settype($uid, 'array'); |
| 25 |
foreach ($uid as $id) { |
| 26 |
$this->connection->write('DELE ' . $id, 1); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
public function fetchMessageSource($uid) { |
| 31 |
$response = $this->connection->write('RETR ' . $uid); |
| 32 |
array_shift($response); |
| 33 |
$response = join("\r\n", $response); |
| 34 |
|
| 35 |
return $response; |
| 36 |
} |
| 37 |
|
| 38 |
public function listMessages($limit = NULL, $page = NULL) { |
| 39 |
|
| 40 |
if (!$limit) { |
| 41 |
$start = 1; |
| 42 |
$end = NULL; |
| 43 |
} else { |
| 44 |
if (!$page) { |
| 45 |
$page = 1; |
| 46 |
} |
| 47 |
$start = ($limit * ($page - 1)) + 1; |
| 48 |
$end = $start + $limit - 1; |
| 49 |
} |
| 50 |
|
| 51 |
$total_messages = $this->countMessages(); |
| 52 |
|
| 53 |
if ($start > $total_messages) { |
| 54 |
return array(); |
| 55 |
} |
| 56 |
|
| 57 |
if ($end === NULL || $end > $total_messages) { |
| 58 |
$end = $total_messages; |
| 59 |
} |
| 60 |
|
| 61 |
$sizes = array(); |
| 62 |
$response = $this->connection->write('LIST'); |
| 63 |
array_shift($response); |
| 64 |
foreach ($response as $line) { |
| 65 |
preg_match('#^(\d+)\s+(\d+)$#', $line, $match); |
| 66 |
$sizes[$match[1]] = $match[2]; |
| 67 |
} |
| 68 |
|
| 69 |
$output = array(); |
| 70 |
for ($i = $start; $i <= $end; $i++) { |
| 71 |
$response = $this->connection->write('TOP ' . $i . ' 1'); |
| 72 |
array_shift($response); |
| 73 |
$value = array_pop($response); |
| 74 |
// Some servers add an extra blank line after the 1 requested line |
| 75 |
if (trim($value) == '') { |
| 76 |
array_pop($response); |
| 77 |
} |
| 78 |
|
| 79 |
$response = trim(join("\r\n", $response)); |
| 80 |
$headers = fMailbox::parseHeaders($response); |
| 81 |
|
| 82 |
$received = ''; |
| 83 |
if (array_key_exists('received', $headers) && count($headers['received']) > 0) { |
| 84 |
$received = $headers['received'][0]; |
| 85 |
} else { |
| 86 |
DebugEcho('pPop3MailServer::listMessages missing "received" header'); |
| 87 |
} |
| 88 |
|
| 89 |
$date = ''; |
| 90 |
if (array_key_exists('date', $headers)) { |
| 91 |
$date = $headers['date']; |
| 92 |
} else { |
| 93 |
DebugEcho('pPop3MailServer::listMessages missing "date" header'); |
| 94 |
} |
| 95 |
|
| 96 |
$subject = ''; |
| 97 |
if (array_key_exists('subject', $headers)) { |
| 98 |
$subject = $headers['subject']; |
| 99 |
} else { |
| 100 |
DebugEcho('pPop3MailServer::listMessages missing "subject" header'); |
| 101 |
} |
| 102 |
|
| 103 |
$output[$i] = array( |
| 104 |
'uid' => $i, |
| 105 |
'received' => self::cleanDate(preg_replace('#^.*;\s*([^;]+)$#', '\1', $received)), |
| 106 |
'size' => $sizes[$i], |
| 107 |
'date' => $date, |
| 108 |
'from' => self::joinEmails(array($headers['from'])), |
| 109 |
'subject' => $subject |
| 110 |
); |
| 111 |
if (isset($headers['message-id'])) { |
| 112 |
$output[$i]['message_id'] = $headers['message-id']; |
| 113 |
} else { |
| 114 |
DebugEcho('pPop3MailServer::listMessages missing "message-id" header'); |
| 115 |
} |
| 116 |
|
| 117 |
if (isset($headers['to'])) { |
| 118 |
$output[$i]['to'] = self::joinEmails($headers['to']); |
| 119 |
} else { |
| 120 |
DebugEcho('pPop3MailServer::listMessages missing "to" header'); |
| 121 |
} |
| 122 |
|
| 123 |
if (isset($headers['in-reply-to'])) { |
| 124 |
$output[$i]['in_reply_to'] = $headers['in-reply-to']; |
| 125 |
} else { |
| 126 |
DebugEcho('pPop3MailServer::listMessages missing "in-reply-to" header'); |
| 127 |
} |
| 128 |
} |
| 129 |
return $output; |
| 130 |
} |
| 131 |
|
| 132 |
public function close() { |
| 133 |
if ($this->connection->isPersistant()) { |
| 134 |
$this->connection->write('QUIT', 1); |
| 135 |
} |
| 136 |
$this->connection->close(); |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|