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 ''.$c1.''.$c2."\n"; } switch ($part) { case '': // Show a frame for all the other parts $m->header('View Message | MessageManager'); $t = $m->template(); echo "\n"; row('To:' , htmlentities($parser->getHeader('to' ))); row('From:' , htmlentities($parser->getHeader('from' ))); row('Subject:' , htmlentities($parser->getHeader('subject' ))); row('In-Reply-to:', parseMessageIDs($parser->getHeader('in-reply-to'))); row('References:' , parseMessageIDs($parser->getHeader('references' ))); echo "
\n"; echo "
\n"; if ($parser->getMessageBodyPart('html')!==false) { echo "

HTML

\n"; echo ''."\n"; } if ($parser->getMessageBodyPart('text')!==false) { echo "

Plain Text

\n"; echo ''."\n"; } echo "
\n"; echo "

Attachments

\n"; echo "\n"; $attachments = $parser->getAttachments(); foreach ($attachments as $id => $attachment) { echo ""; echo '"; echo '"; echo "\n"; } echo "
'.htmlentities($attachment->getContentType())."'; echo htmlentities($attachment->getFilename()); echo "
\n"; $m->footer();// XXX: this is specific to MessageManager break; case 'body': $type = $subpart; switch ($type) { case 'text': header('Content-type: text/plain'); break; case 'html': header('Content-type: text/html' ); break; default: } echo $parser->getMessageBody($type); break; case 'attachment': $attachment_id = $subpart; $attachments = $parser->getAttachments(); $attachment = $attachments[$attachment_id]; $type = $attachment->getContentType(); $filename = $attachment->getFilename(); header('Content-Type: '.$type); header('Content-Disposition: attachment; filename='.$filename ); while($bytes = $attachment->read()) { echo $bytes; } break; }