summaryrefslogtreecommitdiff
path: root/apps/mm/controllers/Messages.class.php
blob: d9decc91dfd7c9fae629840262459a0e6400459a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
require_once('Login.class.php');
require_once('Auth.class.php');
require_once('Database.class.php');
require_once('Message.class.php');
require_once('MimeMailParser.class.php');

Router::register('messages', 'Messages', 'index');
Router::register('messages/index', 'Messages', 'index');
Router::register('messages/*', 'Messages', 'message');

class Messages extends Controller {
	private $msgdir;

	public function __construct() {
		$db = Database::getInstance();
		$this->msgdir = $db->getSysConf('msgdir');
	}
	
	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) {
		$uid = Login::isLoggedIn();
		if ($uid===false || !Auth::getInstance($uid)->isUser()) {
			$this->http401($routed, $remainder);
			return;
		}
		
		$msg_id = $remainder[0];// We can trust the router that this is set
		$msg = new Message($msg_id);
		if ($msg === false) {
			$this->http404($routed, $remainder);
			return;
		}
		
		@$part = $remainder[1];
		@$subpart = $remainder[2];
		
		switch ($part) {
		case '':
			$this->showView('messages/frame',
			                array('msg'=>$msg));
			break;
		case 'body':
			require_once('Mime.class.php');
			header('Content-type: '.Mime::ext2mime(PAGE_EXT));
			$map = array('html'=>'html',
			             'txt' =>'text');
			echo $msg->getMessageBody($map[PAGE_EXT]);
			break;
		case 'attachment':
			$attachment_id = $subpart;
			$attachments = $msg->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) {
		$this->showView('messages/401', array('uid'=>Login::isLoggedIn()));
	}
}