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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
<?php
// the first ~20 lines are so that this can be called from the command line,
// with mail piped in. This allows us to hook it into a local mail handler.
global $BASE, $m;
$cmdline = isset($argv[0]); // called from the command line
@$method = $_SERVER['REQUEST_METHOD']; // What HTTP method was used
if (!isset($BASE)) {
$pages = dirname(__FILE__);
$src = dirname($pages);
$BASE = dirname($src);
set_include_path(get_include_path()
.PATH_SEPARATOR. "$BASE/src/lib"
.PATH_SEPARATOR. "$BASE/src/ext"
);
}
if (!$cmdline) {
require_once('MessageManager.class.php');
$m = new MessageManager($BASE.'/conf.php');
}
$uid = $m->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 <q>'.htmlentities($msg).'</q> 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 '<<a href="'.url($id).'">'.$id.'</a>>';
}
function parseMessageIDs($string) {
$base = $_SERVER['REQUEST_URL'];
$safe = htmlentities($string);
$html = preg_replace_callback(
'/<([^>]*)>/',
'messageLink',
$safe);
return $html;
}
function row($c1, $c2) {
echo '<tr><td>'.$c1.'</td><td>'.$c2."</td></tr>\n";
}
switch ($part) {
case '': // Show a frame for all the other parts
$m->header('View Message | MessageManager');
$t = $m->template();
echo "<table>\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 "</table>\n";
echo "<div class='message-body'>\n";
if ($parser->getMessageBodyPart('html')!==false) {
echo "<h2>HTML</h2>\n";
echo '<iframe src="'.url($msg_id,'body','html').'" ></iframe>'."\n";
}
if ($parser->getMessageBodyPart('text')!==false) {
echo "<h2>Plain Text</h2>\n";
echo '<iframe src="'.url($msg_id,'body','text').'" ></iframe>'."\n";
}
echo "</div>\n";
echo "<h2>Attachments</h2>\n";
echo "<table>\n";
$attachments = $parser->getAttachments();
foreach ($attachments as $id => $attachment) {
echo "<tr>";
echo '<td>'.htmlentities($attachment->getContentType())."</td>";
echo '<td><a href="'.url($msg_id,'attachment',$id).'">';
echo htmlentities($attachment->getFilename());
echo "</a></td>";
echo "</tr>\n";
}
echo "</table>\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;
}
|