isLoggedIn();
$auth = ($uid!==false) && ($m->getStatus($uid)>0);
if (!$cmdline && !$auth) {
$m->status('401 Unauthorized');
$m->header('Unauthorized');
$t = $m->template();
$t->tag('h1',array(),"401: Unauthorized");
$t->paragraph('You need to be logged in to view messages. :(');
$m->footer();
exit();
}
@$method = $_SERVER['REQUEST_METHOD'];
if ( ($method=='PUT') || ($method=='POST') || $cmdline ) {
// We're going to be uploading a new message.
// so uniqid isn't 'secure', it doesn't need to be, it's to prevent
// random collisions.
$tmpfile = "$BASE/tmp/".uniqid(getmypid().'.');
$infile = ($cmdline?'php://stdin':'php://input');
$out = fopen($tmpfile, "w");
$in = fopen($infile, "r");
while ($data = fread($in, 1024))
fwrite($out, $data);
fclose($out);
fclose($in);
//apache_request_headers()
require_once('MimeMailParser.class.php');
$parser = new MimeMailParser();
$parser->setPath($tmpfile);
$id = preg_replace('/<(.*)>/', '$1',
$parser->getHeader('message-id'));
$id = str_replace('/', '', $id); // for security reasons
$msg_file = "$BASE/msg/$id";
rename($tmpfile, $msg_file);
if (!$cmdline) {
$m->status('201 Created');
header("Location: ".$m->baseUrl().'messages/'.$id);
}
exit();
}
global $PAGE, $BASE;
$page_parts = explode('/',$PAGE);
@$msg = $page_parts[1];
if ($msg == '') {
$m->header('Message Index');
$t = $m->template();
$t->tag('h1',array(),"Message Index");
require_once('MimeMailParser.class.php');
$parser = new MimeMailParser();
$messages = array();
$dh = opendir("$BASE/msg");
while (($file = readdir($dh)) !== false) {
$path = "$BASE/msg/$file";
if (is_file($path)) {
$parser->setPath($path);
$date_string = $parser->getHeader('date');
$date = strtotime($date_string);
if (!isset($messages[$date])) $messages[$date] = array();
$messages[$date][] =
array('id'=>$file,
'subject'=>$parser->getHeader('subject'));
}
}
closedir($dh);
$t->openTag('table');
foreach ($messages as $date => $message_array) {
foreach ($message_array as $message) {
$url = $m->baseUrl().'messages/'.$message['id'];
$subject = htmlentities($message['subject']);
$date_str = date('Y-m-d H:i:s',$date);
$t->row(array(
$t->link($url, $subject, true),
$t->link($url, $date_str, true)
));
}
}
$t->closeTag('table');
$m->footer();
exit();
}
@$msg_file = "$BASE/msg/$msg";
if (!is_file($msg_file)) {
$m->status('404 Not Found');
$m->header('Message not found | MessageManager');
$t = $m->template();
$t->tag('h1',array(),'404: Not Found');
$t->paragraph('The message '.htmlentities($msg).'
was not '.
'found in our database.');
$m->footer();
exit();
}
////////////////////////////////////////////////////////////////////////////////
// In the interest of code reusability, most of the following code is //
// independent of message manager. This section is stubs to bind into //
// MessageManager. //
$msg_file = $msg_file;
$msg_id = $msg;
@$part = $page_parts[2];
@$subpart = $page_parts[3];
function url($id, $part='',$subpart='') {
global $m;
return $m->baseUrl().'messages/'.$id.'/'.($part?"$part/$subpart":'');
}
// With the exception of one line (tagged with XXX), the following code is //
// not specific to MessageManager. //
// At some point I may contemplate making this use the template engine, but //
// I like the idea of it being self-standing. //
////////////////////////////////////////////////////////////////////////////////
require_once('MimeMailParser.class.php');
$parser = new MimeMailParser();
$parser->setPath($msg_file);
function messageLink($id) {
if (is_array($id)) { $id = $id[1]; }
return '<'.$id.'>';
}
function parseMessageIDs($string) {
$base = $_SERVER['REQUEST_URL'];
$safe = htmlentities($string);
$html = preg_replace_callback(
'/<([^>]*)>/',
'messageLink',
$safe);
return $html;
}
function row($c1, $c2) {
echo '
'.htmlentities($attachment->getContentType())." | "; echo ''; echo htmlentities($attachment->getFilename()); echo " | "; echo "