From ad4a7ff9159c2c64cea98d7189f46fa7d6174fc2 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 4 Sep 2011 21:13:47 -0400 Subject: Screw it, I'm tired of trying to break this into individual commits --- src/controllers/Messages.class.php | 100 +++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/controllers/Messages.class.php (limited to 'src/controllers/Messages.class.php') diff --git a/src/controllers/Messages.class.php b/src/controllers/Messages.class.php new file mode 100644 index 0000000..86403ae --- /dev/null +++ b/src/controllers/Messages.class.php @@ -0,0 +1,100 @@ +msgdir = BASEPATH.'/msg'; + } + + public function index($routed, $remainder) { + $parser = new MimeMailParser(); + $messages = array(); + $dh = opendir($this->msgdir); + while (($file = readdir($dh)) !== false) { + $path = $this->msgdir."/$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'), + 'from'=>$parser->getHeader('from')); + } + } + closedir($dh); + + $this->showView('messages/index', array('messages' => $messages)); + exit(); + } + + public function message($routed, $remainder) { + global $mm; + $uid = $mm->isLoggedIn(); + if ($uid===false || !$mm->getAuthObj($uid)->isUser()) { + $this->http401($routed, $remainder); + return; + } + + $msg_id = $remainder[0];// We can trust the router that this is set + $msg_file = $this->msgdir."/$msg_id"; + if (!is_file($msg_file)) { + $this->http404($routed, $remainder); + return; + } + + @$part = $remainder[1]; + @$subpart = $remainder[2]; + $parser = new MimeMailParser(); + $parser->setPath($msg_file); + + switch ($part) { + case '': + $this->showView('messages/frame', + array('msg_id'=>$msg_id, + 'parser'=>$parser, + 'msgdir'=>$this->msgdir, + )); + break; + case 'body': + require_once('Mime.class.php'); + header('Content-type: '.Mime::ext2mime(PAGE_EXT)); + $map = array('html'=>'html', + 'txt' =>'text'); + echo $parser->getMessageBody($map[PAGE_EXT]); + 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; + default: + array_push($routed, array_shift($remainder)); + $this->http404($routed, $remainder); + } + } + + public function http401($routed, $remainder) { + global $mm; + $this->showView('messages/401', array('uid'=>$mm->isLoggedIn())); + } +} \ No newline at end of file -- cgit v1.2.3-2-g168b