ERROR = _("MBOX connect:") . ' ' . _("No server specified");
return false;
}
$this->MBOXSTR = "{".$server.":".$port."/pop3/ssl/novalidate-cert}";
$this->MBOX = imap_open( $this->MBOXSTR, $login, $password );
if (!$this->MBOX) {
print_r(imap_errors());
$this->ERROR = imap_errors();
return false;
}
return true;
}
function get_count() {
/* XXX: Apparently Gmail does some funky backend shit when processing a
* user's account. Probably something to do with their schnazzy
* filesystem. Basically their pop server isn't competely synced with
* whatever the browser sees. As such, for some reason only completely
* untouched gmail msgs can be seen.
*/
$status = imap_status( $this->MBOX, $this->MBOXSTR, SA_ALL );
if ($status) {
if ($this->DEBUG)
{
echo "Messages: " . $status->messages . "
\n";
echo "Recent: " . $status->recent . "
\n";
echo "Unseen: " . $status->unseen . "
\n";
echo "UIDnext: " . $status->uidnext . "
\n";
echo "UIDvalidity:" . $status->uidvalidity . "
\n";
}
return $status->unseen;
}
else
{
$this->ERROR = imap_last_error();
}
return false;
}
function parse_header($msgNum)
{
$header = imap_header( $this->MBOX, $msgNum );
$message = array();
$fullsubject = '';
// most 'froms' are just aliased names; we want an actual address
$from = $header->from[0]->mailbox."@".$header->from[0]->host;
$message['fromaddress'] = $from;
$message['toaddress'] = $header->toaddress;
// Subject may have different charset, and mixes of charsets too (argh)
$elements = imap_mime_header_decode($header->subject);
for( $i =0; $i < count($elements); $i++)
{
$charset = $elements[$i]->charset;
$text = $elements[$i]->text;
if ($this->DEBUG)
{
echo "charset: " . $charset . "
";
echo "text: " . $text . "
";
}
if(strcasecmp($charset,"utf-8") != 0)
{
if (strcasecmp($charset,"default") == 0) $charset = "US-ASCII";
$text = iconv($charset,"UTF-8",$text);
if ($this->DEBUG) echo "
converted subject to UTF-8
";
}
$fullsubject .= $text;
}
$message['subject'] = $fullsubject;
$message['date'] = $header->date;
if ($this->DEBUG)
{
echo "
PARSE HEADER produced:";
var_dump( $header );
echo "
";
echo "From: ".$message['fromaddress']."
";
echo "To: ".$message['toaddress']."
";
echo "Date: ".$message['date']."
";
echo "Subject: ".$message['subject']."
";
echo "
";
}
return $message;
}
function parse_content($msgNum)
{
$retval = imap_fetchstructure( $this->MBOX, $msgNum );
$message = array();
// find how many parts object has
$numparts = count( $retval->parts );
if ($this->DEBUG)
{
echo "FETCHSTRUCTURE (".$numparts." part(s)):
";
var_dump( $retval );
}
$i = 0;
foreach ($retval->parts as $part)
{
$i++;
$charset = '';
if ($this->DEBUG)
{
echo "
Processing part ".$i."...
\n";
echo "part dump:
";
var_dump($part);
echo "
";
echo "Of type: ".$part->type."
";
echo "Of subtype: ".$part->subtype."
";
echo "Of encoding: ".$part->encoding."
";
}
$message['part'.$i]['type'] = $part->type;
$message['part'.$i]['subtype'] = $part->subtype;
$message['part'.$i]['encoding'] = $part->encoding;
if ($part->ifdparameters)
{
if ($part->dparameters[0]->attribute == "FILENAME")
{
$message['part'.$i]['filename']=$part->dparameters[0]->value;
}
}
elseif ($part->ifparameters)
{
if ($part->parameters[0]->attribute == "CHARSET")
{
$message['part'.$i]['charset'] = $part->parameters[0]->value;
}
}
// XXX: not sure how robust id calculation is ...
$message['part'.$i]['body'] = imap_fetchbody($this->MBOX, $msgNum, $i);
}
$message['parts'] = $i;
return $message;
}
// helper func to better understand imap_fetchbody id's
/*
function print_structure($msgNum) {
$msg =& new message_components($this->MBOX);
$msg->fetch_structure($msgNum);
echo '
'; var_dump($msg->pid[$msgNum]); echo ''; } */ function fix_coding($type, $body) { if ($this->DEBUG) { echo "fixing coding for type " . $type . "