-
Notifications
You must be signed in to change notification settings - Fork 2
/
pmdc-demo.php
290 lines (273 loc) · 11.3 KB
/
pmdc-demo.php
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/* =====================================================================
This file is part of "phpmaildirclass"
https://github.com/AnanasPfirsichSaft/phpmaildirclass
MIT License
Copyright (c) 2018 AnanasPfirsichSaft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */
error_reporting(-1);
function D($a,$b=''){static $c = 0; if($b==='+'){$c++;$b=$c;} echo '<pre>'.gettype($a).' {'.@strlen($a).'} '.strtoupper($b).chr(10).str_repeat('=',12).chr(10).htmlspecialchars(print_r($a,true)).chr(10).str_repeat('=',12).'</pre>'.chr(10);}
// Use HTTP authentication based on a user database you specify a few lines below...
/*
if ( !isset($_SERVER['PHP_AUTH_USER']) ){
header('WWW-Authenticate: Basic realm="phpmaildirclass demo"');
header($_SERVER['SERVER_PROTOCOL'].' 401 Unauthorized');
die();
}
else{
$userdb = file_get_contents('/home/wwwroot/.htpasswd');
preg_match('/^'.$_SERVER['PHP_AUTH_USER'].':(.+?)$/m',$userdb,$matches);
if ( is_array($matches) && sizeof($matches) === 2 ){
if ( hash_equals($matches[1],crypt($_SERVER['PHP_AUTH_PW'],$matches[1])) ){
// do nothing
}
else{
header('WWW-Authenticate: Basic realm="phpmaildirclass demo"');
header($_SERVER['SERVER_PROTOCOL'].' 401 Unauthorized');
die();
}
}
else{
header('WWW-Authenticate: Basic realm="phpmaildirclass demo"');
header($_SERVER['SERVER_PROTOCOL'].' 401 Unauthorized');
die();
}
*/
// If you want to use http authentication, also uncomment the last curly brace at the
// end of this script. Or you will run into a 'parse error'.
// Without http authentication you are likely have to set a static username.
$_static_user = 'demo';
require_once('./pmdc-class.php');
$_TIME = microtime(true);
$_action = ( isset($_GET['action']) ) ? $_GET['action'] : 'view_folder';
$_folder = ( isset($_GET['folder']) ) ? $_GET['folder'] : '.';
$_mid = ( isset($_GET['mid']) ) ? intval($_GET['mid']) : false;
$_part = ( isset($_GET['part']) ) ? intval($_GET['part']) : false;
$_user = ( isset($_SERVER['PHP_AUTH_USER']) ) ? strtolower($_SERVER['PHP_AUTH_USER']) : $_static_user;
// Some variables you can adapt to your needs...
$webmail_allowed_subfolders = array(
'.',
'.Sent',
'.Spam',
'.Trash'
);
$webmail_root_path = array( // user is "demo", if static name is applied
'/home/'.$_user.'/Maildir/'.$_folder.'/cur',
'/home/'.$_user.'/Maildir/'.$_folder.'/new'
);
$webmail_folders = array( // 'path_relative_to_webmail_root' => array( 'displayed name', 'shown for these users' )
'.'=>array('Posteingang','*'),
'.Sent'=>array('Gesendet','*'),
'.Spam'=>array('Spam','demo'),
'.Trash'=>array('Papierkorb','foo,bar'),
);
Mail_Parser_l10n::$lang = 'de';
setlocale(LC_TIME,array('de_DE.utf-8'));
// ... now following the rest. Maybe you just want to translate the german strings ;)
$_folder = preg_replace('/[^a-z0-9\.\-]+/i','',$_folder);
$_folder = preg_replace('/[\.]{2,}/','',$_folder);
if ( !in_array($_folder,$webmail_allowed_subfolders,true) )
$_folder = '.';
$webmail_stack = array();
$webmail_stack_meta = array();
$webmail_stack_date = array();
foreach ( $webmail_root_path as $path ){
if ( !is_dir($path) || !is_readable($path) ){
trigger_error('"'.$path.'" is not readable',E_USER_WARNING);
continue;
}
$files = scandir($path);
foreach ( $files as $file ){
if ( $file === '.' || $file === '..' )
continue;
$full = $path.'/'.$file;
$full_hash = md5($full);
$webmail_stack_meta[$full_hash] = $full;
$webmail_stack_date[$full_hash] = filemtime($full);
}
}
arsort($webmail_stack_date);
foreach ( $webmail_stack_date as $key=>$value )
array_push($webmail_stack,$webmail_stack_meta[$key]);
preg_match('/^(\d+)(k|m|g)$/im',ini_get('memory_limit'),$matches);
if ( is_array($matches) && sizeof($matches) === 3 ){
switch ( strtolower($matches[2]) ){
case 'k': $value = $matches[1]*pow(10,3); break;
case 'm': $value = $matches[1]*pow(10,6); break;
case 'g': $value = $matches[1]*pow(10,9); break;
default: break;
}
}
else
$value = ini_get('memory_limit');
define('MEMORY_LIMIT_AS_INT',$value);
unset($files,$userdb,$matches,$path,$file,$key,$value,$full,$full_hash,$webmail_stack_meta,$webmail_stack_date);
if ( $_action === 'view_part' && isset($webmail_stack[$_mid]) ){
$mail = new Mail_Parser;
if ( !$mail->process($webmail_stack[$_mid]) )
die('ERR');
if ( $_part !== 255 ){
$header = $mail->fetch('header');
$body = $mail->fetch('body');
if ( !isset($body[$_part]) )
die('ERR');
$body_part =& $body[$_part];
$meta = $mail->determine_meta_of_part($header,$body_part);
$buffer = $mail->fetch('body',$_part);
$mail->decode_string($buffer,$meta);
$charset_str = '';
if ( substr($meta['type'],0,10) === 'multipart/' )
$meta['type'] = 'text/plain';
if ( substr($meta['type'],0,5) === 'text/' )
$charset_str = '; charset='.$mail->charset_output;
if ( $meta['type'] === 'text/html' )
header('Content-Security-Policy: default-src \'self\'; style-src \'unsafe-inline\'');
}
else{
$meta['type'] = 'text/plain; ';
$charset_str = 'charset=utf-8';
if ( filesize($webmail_stack[$_mid]) < MEMORY_LIMIT_AS_INT )
$buffer = file_get_contents($webmail_stack[$_mid]);
else
die('ERR');
}
header('Cache-Control: no-cache,no-store,must-revalidate,maxage=0,s-maxage=0');
header('Expires: '.gmdate('r',mktime(0,0,0,1,1,date('Y'))));
header('Content-Length: '.strlen($buffer));
header('Content-Type: '.$meta['type'].$charset_str);
echo $buffer;
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' value='text/html; charset=utf-8' />
<title>phpmaildirclass demo for user <?php echo htmlspecialchars($_user,ENT_NOQUOTES|ENT_HTML5,'utf-8'); ?></title>
<link rel='stylesheet' type='text/css' href='pmdc.css' />
</head>
<body>
<div class='main'>
<h1>phpmaildirclass demo</h1>
<div class='main_inner'>
<div class='folder_select'>
<form action='pmdc-demo.php' method='get'>
<input type='hidden' name='action' value='view_folder' />
<select name='folder'>
<?php
foreach ( $webmail_folders as $folder=>$data ){
if ( $data[1] !== '*' && !in_array($_user,explode(',',$data[1]),true) )
continue;
$is_current = ( $_folder === $folder ) ? ' selected="selected"' : '';
echo '<option value=\''.urlencode($folder).'\''.$is_current.'>'.htmlspecialchars($data[0]).'</option>'.chr(10);
}
unset($folder,$data,$is_current);
?>
</select>
<input type='submit' value='Open' />
</form>
</div>
<div class='username'>
<?php echo htmlspecialchars($_user,ENT_NOQUOTES|ENT_HTML5,'utf-8'); ?>
</div>
<br /><br /><hr />
<table>
<tr>
<th>Von</th>
<th>Datum</th>
<th>Betreff</th>
<th>Größe</th>
</tr>
<?php
foreach ( $webmail_stack as $mail_key=>$mail_file ){
$color = ( $mail_key%2 === 0 ) ? 'light' : 'dark';
$hilight = ( $_mid === $mail_key ) ? 'current' : '';
$mail = new Mail_Parser;
if ( !$mail->process($mail_file,Mail_Parser::PARSE_HEADER_ONLY) )
continue;
$header = $mail->fetch('header');
$body = array();
$meta = array();
$from = $mail->decode_address_line($mail->get_header_line($header,'from'));
$date = $mail->get_header_line($header,'date');
$subject = $mail->get_header_line($header,'subject');
if ( strlen($subject) === 0 )
$subject = '(keine Betreffszeile angegeben)';
$mail->decode_string($subject,$meta);
echo "\t<tr class='".$color."'>\n";
echo "\t\t<td>".htmlspecialchars($from[0],ENT_NOQUOTES|ENT_HTML5,'utf-8')."</td>\n";
echo "\t\t<td>".strftime('%a, %d %b %Y %H:%M',strtotime($date))."</td>\n";
echo "\t\t<td class='".$hilight."'><a href='pmdc-demo.php?action=view_mail&folder=".urlencode($_folder)."&mid=".$mail_key."'>".htmlspecialchars($subject,ENT_NOQUOTES|ENT_HTML5,'utf-8')."</a></td>\n";
echo "\t\t<td>".$mail->human_readable_byte_size(filesize($mail_file))."</td>\n";
echo "\t</tr>\n";
unset($mail,$header,$body,$meta,$from,$date,$subject);
}
?>
</table>
<hr />
<?php
if ( is_integer($_mid) && isset($webmail_stack[$_mid]) ){
$mail = new Mail_Parser;
$mail->process($webmail_stack[$_mid]);
$header = $mail->fetch('header');
$body = $mail->fetch('body');
$meta = array();
//$mail->fetch('debug');
//D($body);
$from = $mail->decode_address_line($mail->get_header_line($header,'from'));
$to = $mail->decode_address_line($mail->get_header_line($header,'to'));
$date = $mail->get_header_line($header,'date');
$subject = $mail->get_header_line($header,'subject');
if ( strlen($subject) === 0 )
$subject = '(keine Betreffszeile angegeben)';
$mail->decode_string($subject,$meta);
echo "\t<div class='itembox'><strong>Von:</strong> ".htmlspecialchars($from[0].' <'.$from[1].'>',ENT_NOQUOTES|ENT_HTML5,'utf-8')."</div>\n";
echo "\t<div class='itembox'><strong>An:</strong> ".htmlspecialchars($to[0].' <'.$to[1].'>',ENT_NOQUOTES|ENT_HTML5,'utf-8')."</div>\n";
echo "\t<div class='itembox'><strong>Datum:</strong> ".strftime('%a, %d %b %Y %H:%M',strtotime($date))."</div>\n";
echo "\t<div class='itembox'><strong>Betreff:</strong> ".htmlspecialchars($subject,ENT_NOQUOTES|ENT_HTML5,'utf-8')."</div>\n";
echo "\t<div class='textbox'>\n";
echo "\t\t<div class='textbox_head'>Quellcode-Ansicht</div>\n";
echo "\t\t<a href='pmdc-demo.php?action=view_part&folder=".urlencode($_folder)."&mid=".$_mid."&part=255' target='_blank'>In neuem Fenster öffnen</a>\n";
echo "\t</div>\n";
foreach ( $body as $body_id=>$body_part ){
$meta = $mail->determine_meta_of_part($header,$body_part);
if ( $meta['type'] === 'text/plain' ){
$message = $mail->fetch('body',$body_id);
$mail->decode_string($message,$meta);
echo "\t<div class='textbox'>\n";
echo "\t\t<div class='textbox_head'>".$mail->human_readable_content_type($meta['type'])." (".$mail->human_readable_byte_size($body_part['length']).")</div>\n";
echo "\t\t<pre class='textbox_body'>".str_replace("\n"," ⏎\n",htmlspecialchars($message,ENT_NOQUOTES|ENT_HTML5,'utf-8'))."</pre>\n";
echo "\t</div>\n";
}
else{
echo "\t<div class='textbox'>\n";
echo "\t\t<div class='textbox_head'>".$mail->human_readable_content_type($meta['type'])." (".$mail->human_readable_byte_size($body_part['length']).")</div>\n";
echo "\t\t<a href='pmdc-demo.php?action=view_part&folder=".urlencode($_folder)."&mid=".$_mid."&part=".$body_id."' target='_blank'>In neuem Fenster öffnen</a>\n";
echo "\t</div>\n";
}
}
unset($mail,$header,$body,$body_id,$body_part,$from,$to,$date,$subject,$meta);
}
?>
</div><!-- main_inner -->
<div class="footer">phpmaildirclass demo - <?php echo '('.round(microtime(true)-$_TIME,3).'s)'; ?></div>
</div>
</body>
</html>
<?php
//}
?>