tags.
// It also substitutes URLs for links and email addresses for mailto:
// links.
function renderPlainText($text) {
global $makeHttpLinks, $makeMailtoLinks;
// Throw in tags
$retval = str_replace("\n", " \n", $text);
if($makeHttpLinks) {
// Replace urls with links
$retval = preg_replace('/\s(\w+:\/\/)(\S+)/',
' \\1\\2', $retval);
}
if($makeMailtoLinks) {
// Replace email addresses with mailto: links
$retval = preg_replace('/\s(\w+@)(\S+)/',
' \\1\\2', $retval);
}
return $retval;
}
//-------------------------------------------------------------------
// returnAttachment()
// This function returns a given attachment from an item.
function returnAttachment($itemId, $attachId) {
global $vers, $parttypes, $srvStr, $accountUser, $accountPass;
$inbox = imap_open($srvStr, $accountUser, $accountPass);
$msgStructure = imap_fetchstructure($inbox, $itemId);
$part = $msgStructure->parts[$attachId-1];
$ctype = $parttypes[$part->type]."/".$part->subtype;
$filename = "filename";
foreach($part->parameters as $param) {
if($param->attribute=="NAME")
$filename = $param->value;
}
header("content-type: ".$ctype);
header("content-disposition: attachment; filename=".$filename);
// Returned data depends on whether the attachment is binary or text
if($part->type>0) {
// Binary attachment - convert from base64 to binary
echo base64_decode(imap_fetchbody($inbox, $itemId, $attachId));
} else {
// Text attachment - just display it as-is
echo imap_fetchbody($inbox, $itemId, $attachId);
}
imap_close($inbox);
return;
}
//-------------------------------------------------------------------
// showArticle()
// This function displays a post in an html page. This
// functionality exists to complement permalink/guid behaviour
// in RSS and also to enable compatibility with readers like
// Thunderbird, that always load the permalink instead of
// displaying the summary.
function showArticle($articleId) {
global $vers, $srvStr, $accountUser, $accountPass, $mungeSenderEmail;
$inbox = imap_open($srvStr, $accountUser, $accountPass);
header('Content-type: text/html');
// Retrieve post information from the message header
$headers = imap_headerinfo($inbox, $articleId);
$subject = htmlentities($headers->subject);
$author = htmlentities($headers->fromaddress);
// If author email munging is enabled...
if($mungeSenderEmail) {
$author = mungeEmailAddress($author);
}
// Format the date according to the standard
$entryDate = date("D, d M Y H:i:s O", $headers->udate);
// Get the message body.
// Negotiate the presence of attachments.
$msgStructure = imap_fetchstructure($inbox, $articleId);
if(count($msgStructure->parts)>1) {
$body = imap_fetchbody($inbox, $articleId, "1");
if($msgStructure->parts[0]->subtype=="PLAIN")
$body = renderPlainText($body);
$body .= "
Attachments:
\n";
$partCount = 0;
foreach($msgStructure->parts as $part) {
$partCount++;
if (isset($part->disposition)) {
foreach($part->parameters as $param) {
if($param->attribute=="NAME")
// Generate the link for retrieving attachments
$body .="";
$body .=$param->value." \n";
break;
}
}
}
} else {
$body = imap_body($inbox, $articleId);
}
// If the body is plain-text, run the HTML rendering function
if($msgStructure->subtype=="PLAIN")
$body = renderPlainText($body);
imap_close($inbox);
// The HTML used for displaying post content.
?>
\n";
echo "\n";
echo " \n";
echo " $feedTitle\n";
echo " $feedLink\n";
echo " $feedDesc\n";
echo " $feedLang\n";
echo " IMAP2RSS v.$vers\n";
echo " $feedEditor ($feedEditorMail)\n";
echo " $feedEditor ($feedEditorMail)\n";
echo " $pubDate\n";
// Calculate the number of items to include in the feed.
$msgCount = imap_num_msg($inbox);
if($maxMsgNum && $msgCount>$maxMsgNum)
$lowerLimit = $msgCount - $maxMsgNum;
else
$lowerLimit = 0;
// Generate item entries
for($i=$msgCount; $i>$lowerLimit; $i--) {
$headers = imap_headerinfo($inbox, $i);
$subject = reEncodeString(htmlentities($headers->subject));
// Use htmlentities() because sometimes the address appears
// inside angle brackets.
$author = reEncodeString(htmlentities($headers->fromaddress));
// If author email munging is enabled...
if($mungeSenderEmail) {
$author = mungeEmailAddress($author);
}
// Format the date according to the standard
$entryDate = date("D, d M Y H:i:s O", $headers->udate);
// Set the item link depending on whether there is a custom
// configuration in use or not.
$itemUrl = $feedLink.((isset($_GET['conf']))?"&":"?")."itemId=$i";
// Negotiate the presence of attachments.
$msgStructure = imap_fetchstructure($inbox, $i);
if(count($msgStructure->parts)>1) {
$body = imap_fetchbody($inbox, $i, "1");
// If the body is plain-text, run the HTML rendering function
if($msgStructure->parts[0]->subtype=="PLAIN")
$body = renderPlainText($body);
$body .= "
Attachments:
\n";
$partCount = 0;
foreach($msgStructure->parts as $part) {
$partCount++;
if (isset($part->disposition)) {
foreach($part->parameters as $param) {
if($param->attribute=="NAME")
// Generate the link for retrieving attachments
$body .="";
$body .=$param->value." \n";
break;
}
}
}
} else {
$body = imap_body($inbox, $i);
// If the body is plain-text, run the HTML rendering function
if($msgStructure->subtype=="PLAIN")
$body = renderPlainText($body);
}
// Clean up output to avoid problems with the XML produced
$body = reEncodeString(htmlentities($body));
echo " \n";
echo " $subject\n";
echo " $itemUrl\n";
echo " $entryDate\n";
echo " $body\n";
echo " $author\n";
echo " $itemUrl\n";
echo " \n";
}
echo " \n";
echo " ";
imap_close($inbox);
}
// display page body
// If an itemId has been set, display that item in an HTML page.
// If an itemId and an attachId have been set, return that attachment
// If not, show the entire feed.
if(isset($_GET['itemId'])) {
if(isset($_GET['attachId'])) {
returnAttachment($_GET['itemId'], $_GET['attachId']);
} else {
showArticle($_GET['itemId']);
}
} else {
generateFeed();
}
?>