1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
<?php
/**
* The IMP_Fetchmail_imap driver implements the IMAP_Fetchmail class for use
* with IMAP/POP3 servers.
*
* $Horde: imp/lib/Fetchmail/imap.php,v 1.5.10.12 2008/01/02 11:31:28 jan Exp $
*
* Copyright 2002-2008 The Horde Project (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
*
* @author Nuno Loureiro <nuno@co.sapo.pt>
* @author Michael Slusarz <slusarz@horde.org>
* @package IMP
*/
class IMP_Fetchmail_imap extends IMP_Fetchmail {
/**
* The internal protocols list.
*
* @var array
*/
var $_protocols;
/**
* The current protocol being used.
*
* @var string
*/
var $_currprotocol;
/**
* The server string to use to connect to the remote mail server.
*
* @var string
*/
var $_serverstring;
/**
* Returns a description of the driver.
*
* @see IMP_Fetchmail::description()
*/
function description()
{
return _("IMAP/POP3 Mail Servers");
}
/**
* Constructor.
*
* @param array $params The configuration parameter array.
*/
function IMP_Fetchmail_imap($params)
{
/* Set up the protocols array. */
$this->_protocols = $this->_protocolList();
parent::IMP_Fetchmail($params);
}
/**
* Return a list of protocols supported by this driver.
*
* @see IMP_Fetchmail::getProtocolList()
*/
function getProtocolList()
{
$output = array();
foreach ($this->_protocols as $key => $val) {
$output[$key] = $val['name'];
}
return $output;
}
/**
* Returns the list of IMAP/POP3 protocols that this driver supports, and
* associated configuration options.
* This needs to be in a separate function because PHP will not allow
* gettext strings to appear in member variables.
*
* @access private
*
* @return array The protocol configuration list.
*/
function _protocolList()
{
return array_merge(
IMP_IMAP::protocolList(),
array('pop3auto' => array(
'name' => _("POP3 (Auto Detect Protocols)"),
'auto' => array('pop3', 'pop3notls', 'pop3sslvalid', 'pop3ssl'),
'base' => 'POP3')
),
array('imapauto' => array(
'name' => _("IMAP (Auto Detect Protocols)"),
'auto' => array('imap', 'imapnotls', 'imapsslvalid', 'imapssl'),
'base' => 'IMAP')
)
);
}
/**
* Checks if the remote mailbox exists.
*
* @access private
*
* @param resource $stream A valid IMAP resource stream.
*
* @return boolean Does the remote mailbox exist?
*/
function _remoteMboxExists($stream)
{
if (empty($this->_params['rmailbox'])) {
return false;
}
if ($this->_params['rmailbox'] == 'INBOX') {
/* INBOX always exists and is a special case. */
return true;
}
return (bool)@imap_list($stream, $this->_serverstring, $this->_params['rmailbox']);
}
/**
* Attempts to connect to the mail server
*
* @access private
*
* @return mixed Returns an IMAP Stream or PEAR_Error on failure.
*/
function _connect()
{
/* Create the server string now. */
$this->_serverstring = '{' . $this->_params['server'] . ':' . $this->_protocols[$this->_currprotocol]['port'] . '/'. $this->_protocols[$this->_currprotocol]['string'] . '}';
$server_string = $this->_serverstring;
if ($this->_protocols[$this->_currprotocol]['base'] == 'IMAP') {
$server_string .= $this->_params['rmailbox'];
}
$stream = @imap_open($server_string, $this->_params['username'], $this->_params['password']);
if ($stream === false) {
$errstr = imap_last_error();
if ($errstr) {
return PEAR::raiseError(_("Cannot connect to the remote mail server: ") . $errstr);
} else {
return PEAR::raiseError(_("Cannot connect to the remote mail server."));
}
}
return $stream;
}
/**
* Gets the mailbody and calls the custom filter function.
* Remove bare newlines and sets message color.
*
* @access private
*
* @param resource $stream IMAP connection stream.
* @param integer $uid UID of message to fetch.
*
* @return string Corrected mail content.
*/
function _getMailMessage($stream, $uid)
{
/* Get the message headers. */
$header = @imap_fetchheader($stream, $uid, FT_UID);
/* Get the message body. */
$body = @imap_body($stream, $uid, FT_UID | FT_PEEK);
return parent::_processMailMessage($header, $body);
}
/**
* Gets the mail using the data in this object.
*
* @see IMP_Fetchmail::getMail()
*/
function getMail()
{
if (isset($this->_protocols[$this->_params['protocol']]['auto'])) {
$protocols = $this->_protocols[$this->_params['protocol']]['auto'];
} else {
$protocols = array($this->_params['protocol']);
}
foreach ($protocols as $val) {
$this->_currprotocol = $val;
$ret = $this->_getMail();
if (!is_a($ret, 'PEAR_Error')) {
break;
}
}
return $ret;
}
/**
* Internal function used to get mail from a single server.
*
* @return mixed Returns PEAR_Error on error, the number of messages
* fetched on success.
*/
function _getMail()
{
$numMsgs = 0;
$protocols = array();
$stream = $this->_connect();
if (is_a($stream, 'PEAR_Error')) {
return $stream;
}
/* Check to see if remote mailbox exists. */
$useimap = ($this->_protocols[$this->_currprotocol]['base'] == 'IMAP');
if ($useimap) {
if (!$this->_remoteMboxExists($stream)) {
@imap_close($stream);
return PEAR::raiseError(_("Invalid Remote Mailbox"));
}
}
$msg_count = @imap_num_msg($stream);
if (!$msg_count) {
@imap_close($stream);
return 0;
}
$overview = @imap_fetch_overview($stream, '1:' . $msg_count);
foreach ($overview as $h) {
if (($this->_params['onlynew'] &&
($h->recent || !$h->seen) &&
!$h->deleted) ||
(!$this->_params['onlynew'] && !$h->deleted)) {
/* Check message size. */
if (!$this->_checkMessageSize($h->size, isset($h->subject) ? $h->subject : '', $h->from)) {
continue;
}
/* Get the complete message. */
$mail_source = $this->_getMailMessage($stream, $h->uid);
/* Append to the server. */
if ($this->_addMessage($mail_source)) {
$flags = array();
$numMsgs++;
/* Remove the mail if 'del' is set. */
if ($this->_params['del']) {
$flags[] = "\\Deleted";
}
/* Mark message seen if 'markseen' is set. */
if ($useimap && $this->_params['markseen']) {
$flags[] = "\\Seen";
}
if (!empty($flags)) {
@imap_setflag_full($stream, $h->uid, implode(' ', $flags), ST_UID);
}
}
}
}
/* Expunge all deleted messages now. */
if ($this->_params['del']) {
@imap_close($stream, CL_EXPUNGE);
} else {
@imap_close($stream);
}
return $numMsgs;
}
}
|