-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail_parser.php
More file actions
283 lines (249 loc) · 9.75 KB
/
Copy pathmail_parser.php
File metadata and controls
283 lines (249 loc) · 9.75 KB
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
<?php
/**
* Description of mail_parser
*
* @author Victor Angelier <vangelier@hotmail.com>
* @copyright 2014 Ignite the Future
* @dependencies php-pecl-mailparse, php-mbstring
*/
/**
* Install howto
*
* Create 'procmailrc' file in /etc/
* Add the following lines
* :0c
* | /usr/bin/php /usr/local/bin/mailparser.php
*
* Create: /usr/local/bin/mailparser.php
*
<?php
require_once('mail_parser.php');
$rawEmail = "";
if (($fp = fopen('php://stdin', 'r')) !== false) {
while (!feof($fp)) {
$rawEmail .= fread($fp, 1024);
}
fclose($fp);
}
$c = new mail_parser($rawEmail);
$c->parse(); // <== do something good with the output
?>
*/
class mail_parser {
//Temporary filename
protected $temp_filename = null;
//Raw file data
protected $raw_data = null;
//Mime message
protected $message = null;
//Temp dir
public $tempdir = "/tmp/";
//Location where to save attachements
public $attachment_store = "/home/mailparser/attachments/";
public function __construct($raw_mail_message = "") {
if($this->message == null){
//Set raw message data
$this->raw_data = $raw_mail_message;
//Set temporary filename
$this->temp_filename = md5(rand(1,2342423423));
//Write down the mail message
if((file_put_contents($this->tempdir.$this->temp_filename, $raw_mail_message)) == false){
echo "Cannot write to {$this->tempdir}{$this->temp_filename}";
exit(0);
}else{
if(($this->message = mailparse_msg_parse_file($this->tempdir . $this->temp_filename)) !== false){
}else{
echo "Erro parsing file";
exit(0);
}
}
}
}
/**
* Parse the mail
*
* @return array
*/
public function parse(){
$from = $this->get_from();
$to = $this->get_to();
$gpgkey = $this->get_gpg($to[0]["address"]);
$result = array(
"from" => $from,
"to" => $to,
"cc" => $this->get_cc(),
"datetime" => $this->get_datetime(),
"subject" => $this->get_subject(),
"body" => $this->get_text_body(),
"gpg_pub_key" => $gpgkey,
"attachments" => $this->save_attachments()
);
return $result;
}
/**
* Get the mail's date and time (Dutch style)
* @return type
*/
private function get_datetime(){
$parts = mailparse_msg_get_part_data($this->message);
return date("d-m-Y H:i", strtotime($parts['headers']['date']));
}
/**
* Get the subject of the mail message
* @return type
*/
private function get_subject(){
$parts = mailparse_msg_get_part_data($this->message);
return $parts['headers']['subject'];
}
/**
* Method to save attachments
* @return bool
*/
private function save_attachments(){
$result = array();
//Loop through all files;
foreach($this->get_message_structure() as $file){
$parts = explode(".", $file);
if(count($parts) == 2 && $parts[1] >= 2){
//Attachement part
$mime_part = mailparse_msg_get_part($this->message, "{$file}");
$body_parts = mailparse_msg_get_part_data($mime_part);
//Check if we have files
if(isset($body_parts['disposition-filename'])){
$filename = $body_parts['disposition-filename'];
if(file_put_contents($this->attachment_store.$filename, base64_decode(substr($this->raw_data, $body_parts['starting-pos-body'], $body_parts['ending-pos-body']))) !== false){
array_push($result, array(
"content_type" => $body_parts['content-type'],
"full_path" => $this->attachment_store.$filename,
"filename" => $filename,
"checksum" => sha1_file($this->attachment_store.$filename)
)
);
}
}
}
}
if(count($result) > 0){
return $result;
}else{
return false;
}
}
/**
* Get the ASCII version of the body
* @return string
*/
private function get_text_body(){
//Mime 1.1.1 OR 1.1
if(($txt_body = mailparse_msg_get_part($this->message, "1.1.1")) !== false){
$body_parts = mailparse_msg_get_part_data($txt_body);
$body = substr($this->raw_data, $body_parts['starting-pos-body'], $body_parts['ending-pos-body']);
$raw = quoted_printable_decode($body);
$lines = explode("--", $raw);
return $lines[0]; //This is always the text/plain body
}else{
if(($txt_body = mailparse_msg_get_part($this->message, "1.1")) !== false){
$body_parts = mailparse_msg_get_part_data($txt_body);
$body = substr($this->raw_data, $body_parts['starting-pos-body'], $body_parts['ending-pos-body']);
$raw = quoted_printable_decode($body);
$lines = explode("--", $raw);
return $lines[0]; //This is always the text/plain body
}else{
if(($txt_body = mailparse_msg_get_part($this->message, "1")) !== false){
$body_parts = mailparse_msg_get_part_data($txt_body);
$body = substr($this->raw_data, $body_parts['starting-pos-body'], $body_parts['ending-pos-body']);
$raw = quoted_printable_decode($body);
$lines = explode("--", $raw);
return $lines[0]; //This is always the text/plain body
}else{
return "Could not get body.";
}
}
}
return "Could not get body.";
}
/**
* Returns all mime parts of this message
* @return array
*/
private function get_message_structure(){
return mailparse_msg_get_structure($this->message);
}
/**
* Get the CC part of the mail (name and e-mail address)
* @return type
*/
public function get_cc(){
$parts = mailparse_msg_get_part_data($this->message);
if(isset($parts['headers']['cc'])){
return mailparse_rfc822_parse_addresses($parts['headers']['cc']);
}
}
/**
* Get the FROM part of the mail (name and e-mail address)
* @return type
*/
public function get_from(){
$parts = mailparse_msg_get_part_data($this->message);
return mailparse_rfc822_parse_addresses($parts['headers']['from']);
}
/**
* Get the TO part of the mail (name and e-mail address)
* @return type
*/
public function get_to(){
$parts = mailparse_msg_get_part_data($this->message);
return mailparse_rfc822_parse_addresses($parts['headers']['to']);
}
/**
* Get the users GPG Public Key
* @param string $email
*/
public function get_gpg($email = ""){
if($email != ""){
if(($xml = @file_get_contents("https://pgp.mit.edu/pks/lookup?op=vindex&search={$email}")) !== false){
if(preg_match_all("/\<a href=\".*\"?+\>/", $xml, $matches) !== false){
if(is_array($matches[0]) && count($matches[0]) > 0){
$valid_date = "";
$link = "";
foreach($matches[0] as $match){
$parts = explode("</a>", $match);
//Check for dates and valid until date
preg_match_all("/([0-9]{4}\-[0-9]{2}\-[0-9]{2})/", $match, $dates);
if(is_array($dates[1]) && count($dates[1]) > 1){
//Check validity
if(strtotime(trim($dates[1][1])) > time()){
$valid_date = strtotime(trim($dates[1][1]));
//We found the right key;
preg_match("/href=\".*\"/", $parts[0], $match2);
if(is_array($match2) && count($match2) > 0){
$link = $match2[0];
break;
}
}
}
}
if($link != ""){
//We have matches
$link = str_replace(array(
"href=",
"\""
), "", $link); //We only want the 1st one
$link = html_entity_decode($link);
if(($key = @file_get_contents("https://pgp.mit.edu".$link)) !== false){
$gpg_pub_key = substr($key, strpos($key, "-----BEGIN PGP PUBLIC KEY BLOCK-----"), (strlen($key)-strpos($key, "-----END PGP PUBLIC KEY BLOCK-----
")));
return array(
"key" => trim(strip_tags($gpg_pub_key)),
"date" => date("Y-m-d", $valid_date)
);
}
}
}
}
}
}
return false;
}
}