forked from filsh/PHP-Bounce-Handler
-
Notifications
You must be signed in to change notification settings - Fork 2
/
testdriver1.php
344 lines (312 loc) · 11.7 KB
/
testdriver1.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
/**
* Web based test driver.
*
* Simple tests for the emails. Really needs moving to proper PHPUnit tests.
*
* PHP version 5
*
* @category Email
* @package BounceHandler
* @author Multiple <[email protected]>
* @license http://opensource.org/licenses/BSD-2-Clause BSD
* @link https://github.com/cfortune/PHP-Bounce-Handler/
*/
require_once"bounce_driver.class.php";
error_reporting(E_ALL);
$testDriver=new TestDriver1();
$testDriver->main();
/**
* Class TestDriver1.
*
* Tests the bounce handler in a simple web based manner.
*
* @category Email
* @package BounceHandler
* @author Multiple <[email protected]>
* @license http://opensource.org/licenses/BSD-2-Clause BSD
* @link https://github.com/cfortune/PHP-Bounce-Handler/
*/
class TestDriver1
{
/**
* The bounce handler object
*
* @var Bouncehandler $_bouncehandler
*/
private $_bouncehandler;
/**
* Constructor
*/
public function __construct()
{
$this->_bouncehandler = new Bouncehandler();
}
/**
* Main Class
*
* @return void
*/
public function main()
{
$files = $this->_getSortedFileList('eml');
$this->_htmlHead();
if (!empty($_GET['testall'])) {
$this->_testAllFiles($files);
} elseif (isset($_GET['eml'])) {
if (true===in_array($_GET['eml'], $files)) {
echo "<h2>" . $_GET['eml'] . "</h2>";
echo '<p><a href="'.$_SERVER['PHP_SELF'];
echo '?">View a different bounce</a></p>';
$bounce = file_get_contents("eml/" . $_GET['eml']);
$this->_testSingle($bounce);
}
}
echo "<OL><LI><a href=\"" . $_SERVER['PHP_SELF'];
echo "?testall=true\">Test All Sample Bounce E-mails</a>\n\n";
echo "<LI>Or, select a bounce email to view the parsed results:</OL>\n";
if (is_array($files)) {
reset($files);
echo "<P>Files:</P>\n";
foreach ($files as $file) {
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?eml=";
echo urlencode($file) . "\">$file</a><br>\n";
}
}
}
/**
* Display the HTML prologue
*
* @return void
*/
private function _htmlHead()
{
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
. PHP_EOL;
echo '<html>' . PHP_EOL;
echo '<head>' . PHP_EOL;
echo '<style>' . PHP_EOL;
echo 'span.dropt {border-bottom: thin dotted; }
span.dropt:hover {text-decoration: none; z-index: 6; }
span.dropt span {position: absolute; left: -9999px;
margin: 20px 0 0 0px; padding: 3px 3px 3px 3px;
border-style:solid; border-color:black; border-width:1px; z-index: 6;}
span.dropt:hover span {left: 2%;width:40%;}
span.dropt span {position: absolute; left: -9999px;
margin: 4px 0 0 0px; padding: 3px 3px 3px 3px;
border-style:solid; border-color:black; border-width:1px;}
span.dropt:hover span {margin: 20px 0 0 170px; background: #ffffff; z-index:6;}'
. PHP_EOL;
echo '</style>';
echo '</head>';
echo '<body>';
}
/**
* Get a list of sorted files.
*
* @param string $d Directory name
*
* @return array List of alphabetically sorted files.
*/
function _getSortedFileList($d)
{
$fs = array();
if ($h = opendir($d)) {
while (false !== ($f = readdir($h))) {
if ($f == '.' || $f == '..') {
continue;
}
$fs[] = $f;
}
closedir($h);
sort($fs, SORT_STRING);
}
return $fs;
}
/**
* Test all files.
*
* @param array $files List of files
*
* @return void
*/
function _testAllFiles($files)
{
echo '<p>File Tests:</p>' . PHP_EOL;
echo '<table border="1">' . PHP_EOL;
echo '<thead>' . PHP_EOL;
echo '<tr><th>File</th><th>Action</th>';
echo '<th colspan="3">Status</th>';
echo '<th>Recipient</th></tr>'. PHP_EOL;
echo '</thead>' . PHP_EOL;
echo '<tbody>' . PHP_EOL;
foreach ($files as $file) {
echo '<tr>';
echo '<td><a href="' . $_SERVER['PHP_SELF'] . '?eml='
. urlencode($file) . '">' . $file . '</a></td>';
$bounce = file_get_contents("eml/" . $file);
$multiArray = $this->_bouncehandler->get_the_facts($bounce);
if (!empty($multiArray[0]['action'])
&& !empty($multiArray[0]['status'])
&& !empty($multiArray[0]['recipient'])
) {
echo '<td>' . $multiArray[0]['action'] . '</td>';
echo '<td>' . $multiArray[0]['status'] . '</td>';
/**
* Get the human readable status message description
*/
$human
= $this->_bouncehandler->fetch_status_message_as_array(
$multiArray[0]['status']
);
if ('' === $human['description']) {
echo '<td>' . htmlspecialchars($human['title'])
. '</td>';
} else {
echo '<td><span class="dropt">' . htmlspecialchars(
$human['title']
) . '<span>' . htmlspecialchars(
$human['description']
) . '</span></span></td>';
}
if ('' === $human['sub_description']) {
echo '<td>' . htmlspecialchars($human['sub_title'])
. '</td>';
} else {
echo '<td><span class="dropt">' . htmlspecialchars(
$human['sub_title']
) . '<span>' . htmlspecialchars(
$human['sub_description']
) . '</span></span></td>';
}
echo '<td>' . htmlspecialchars($multiArray[0]['recipient'])
. '</td>';
} else {
echo '<td colspan="5" style="color:red;font-weight:bold;">';
echo 'Unable to parse data<br />>';
echo '<pre>' . PHP_EOL;
echo htmlspecialchars(print_r($multiArray, true));
echo '</pre>' . PHP_EOL;
echo htmlspecialchars(
print_r($$this->_bouncehandler->output, true)
);
echo '</td>';
}
echo '</tr>' . PHP_EOL;
}
echo '</tbody>';
echo '</table>';
}
/**
* Tests a single email.
*
* @param string $bounce Contents of the bounce email.
*
* @return void
*/
private function _testSingle($bounce)
{
$multiArray = $this->_bouncehandler->get_the_facts($bounce);
echo "<TEXTAREA COLS=100 ROWS=" . (count($multiArray) * 8) . ">";
print_r($multiArray);
echo "</TEXTAREA>";
$bounce = $this->_bouncehandler->init_bouncehandler($bounce, 'string');
list($head, $body) = preg_split("/\r\n\r\n/", $bounce, 2);
echo '<h2>Raw email:</h2><br />';
echo "<TEXTAREA COLS=100 ROWS=12>";
echo htmlspecialchars($bounce);
echo "</TEXTAREA><br />";
echo "<h2>Parsed head</h2>\n";
$head_hash = $this->_bouncehandler->parse_head($head);
echo "<TEXTAREA COLS=100 ROWS=" . (count($head_hash) * 2.7) . ">";
print_r($head_hash);
echo "</TEXTAREA><br />";
if ($this->_bouncehandler->is_RFC1892_multipart_report($head_hash)) {
echo '<h2 style="color:red;">';
echo 'Looks like an RFC1892 multipart report';
echo '</h2>';
} else if ($this->_bouncehandler->looks_like_an_FBL) {
echo '<h2 style="color:red;">';
echo 'Looks like a feedback loop';
if ($this->_bouncehandler->is_hotmail_fbl) {
echo ' in Hotmail Doofus Format (HDF?)';
} else {
echo ' in Abuse Feedback Reporting format (ARF)';
}
echo '</h2>';
echo "<TEXTAREA COLS=100 ROWS=12>";
print_r($this->_bouncehandler->fbl_hash);
echo "</TEXTAREA>";
} else {
echo "<h2 style='color:red;'>Not an RFC1892 multipart report</H2>";
echo "<TEXTAREA COLS=100 ROWS=100>";
print_r($body);
echo "</TEXTAREA>";
exit;
}
echo "<h2>Here is the parsed report</h2>\n";
echo '<p>Postfix adds an appropriate X- header (X-Postfix-Sender:), ';
echo 'so you do not need to create one via phpmailer. RFC\'s call ';
echo 'for an optional Original-recipient field, but mandatory ';
echo 'Final-recipient field is a fair substitute.</p>';
$boundary = $head_hash['Content-type']['boundary'];
$mime_sections
= $this->_bouncehandler->parse_body_into_mime_sections(
$body, $boundary
);
$rpt_hash
= $this->_bouncehandler->parse_machine_parsable_body_part(
$mime_sections['machine_parsable_body_part']
);
echo "<TEXTAREA COLS=100 ROWS=" . (count($rpt_hash) * 16) . ">";
print_r($rpt_hash);
echo "</TEXTAREA>";
echo "<h2>Here is the error status code</h2>\n";
echo "<P>It's all in the status code, if you can find one.</P>";
for ($i = 0; $i < count($rpt_hash['per_recipient']); $i++) {
echo "<P>Report #" . ($i + 1) . "<BR>\n";
echo $this->_bouncehandler->find_recipient(
$rpt_hash['per_recipient'][$i]
);
$scode = $rpt_hash['per_recipient'][$i]['Status'];
echo "<PRE>$scode</PRE>";
echo $this->_bouncehandler->fetch_status_messages($scode);
echo "</P>\n";
}
echo '<h2>The Diagnostic-code</h2>';
echo '<p>is not the same as the reported status code, but it seems ';
echo 'to be more descriptive, so it should be extracted (if possible).';
for ($i = 0; $i < count($rpt_hash['per_recipient']); $i++) {
echo "<P>Report #" . ($i + 1) . " <BR>\n";
echo $this->_bouncehandler->find_recipient(
$rpt_hash['per_recipient'][$i]
);
$dcode = $rpt_hash['per_recipient'][$i]['Diagnostic-code']['text'];
if ($dcode) {
echo "<PRE>$dcode</PRE>";
echo $this->_bouncehandler->fetch_status_messages($dcode);
} else {
echo "<PRE>couldn't decode</PRE>";
}
echo "</P>\n";
}
echo '<h2>Grab original To: and From:</h2>\n';
echo '<p>Just in case we don\'t have an Original-recipient: field, or ';
echo 'a X-Postfix-Sender: field, we can retrieve information from ';
echo 'the (optional) returned message body part</p>'.PHP_EOL;
$head
= $this->_bouncehandler->get_head_from_returned_message_body_part(
$mime_sections
);
echo "<P>From: " . $head['From'];
echo "<br>To: " . $head['To'];
echo "<br>Subject: " . $head['Subject'] . "</P>";
echo "<h2>Here is the body in RFC1892 parts</h2>\n";
echo '<[>Three parts: [first_body_part], ';
echo '[machine_parsable_body_part], and ';
echo ' [returned_message_body_part]</p>';
echo "<TEXTAREA cols=100 rows=100>";
print_r($mime_sections);
echo "</TEXTAREA>";
}
}