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
|
<?php
/////////////////////////////////////////////////////////
//
// Iloha MIME Library (IML)
//
// (C)Copyright 2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
//
// This file is part of IlohaMail. IlohaMail is free software released
// under the GPL license. See enclosed file COPYING for details, or
// see http://www.fsf.org/copyleft/gpl.html
//
/////////////////////////////////////////////////////////
/********************************************************
FILE: include/mime.inc
PURPOSE:
Provide functions for handling mime messages.
USAGE:
Use iil_C_FetchStructureString to get IMAP structure stirng, then pass that through
iml_GetRawStructureArray() to get root node to a nested data structure.
Pass root node to the iml_GetPart*() functions to retreive individual bits of info.
********************************************************/
$MIME_INVALID = -1;
$MIME_TEXT = 0;
$MIME_MULTIPART = 1;
$MIME_MESSAGE = 2;
$MIME_APPLICATION = 3;
$MIME_AUDIO = 4;
$MIME_IMAGE = 5;
$MIME_VIDEO = 6;
$MIME_OTHER = 7;
function iml_ClosingParenPos($str, $start) {
$level=0;
$len = strlen($str);
$in_quote = 0;
for ($i=$start; $i<$len; $i++) {
if ($str[$i] == '"' && $str[$i-1] != "\\")
$in_quote = ($in_quote + 1) % 2;
if (!$in_quote) {
if ($str[$i]=="(") $level++;
else if (($level > 0) && ($str[$i]==")")) $level--;
else if (($level == 0) && ($str[$i]==")")) return $i;
}
}
}
function iml_ParseBSString($str){
$id = 0;
$a = array();
$len = strlen($str);
$in_quote = 0;
for ($i=0; $i<$len; $i++) {
if ($str[$i] == '"') {
$in_quote = ($in_quote + 1) % 2;
} else if (!$in_quote) {
if ($str[$i] == " ") { //space means new element
$id++;
while ($str[$i+1] == " ") $i++; // skip additional spaces
} else if ($str[$i]=="(") { //new part
$i++;
$endPos = iml_ClosingParenPos($str, $i);
$partLen = $endPos - $i;
if ($partLen < 0) break;
$part = substr($str, $i, $partLen);
$a[$id] = iml_ParseBSString($part); //send part string
$i = $endPos;
} else
$a[$id].=$str[$i]; //add to current element in array
} else if ($in_quote) {
if ($str[$i]=="\\") {
$i++; //escape backslashes
if ($str[$i] == '"' || $str[$i] == "\\")
$a[$id] .= $str[$i];
} else
$a[$id] .= $str[$i]; //add to current element in array
}
}
reset($a);
return $a;
}
function iml_GetRawStructureArray($str){
$line=substr($str, 1, strlen($str) - 2);
$line = str_replace(")(", ") (", $line);
$struct = iml_ParseBSString($line);
if ((strcasecmp($struct[0], "message")==0) && (strcasecmp($struct[1], "rfc822")==0)){
$struct = array($struct);
}
return $struct;
}
function iml_GetPartArray($a, $part){
if (!is_array($a)) return false;
if (strpos($part, ".") > 0){
$original_part = $part;
$pos = strpos($part, ".");
$rest = substr($original_part, $pos+1);
$part = substr($original_part, 0, $pos);
if ((strcasecmp($a[0], "message")==0) && (strcasecmp($a[1], "rfc822")==0)){
$a = $a[8];
}
//echo "m - part: $original_part current: $part rest: $rest array: ".implode(" ", $a)."<br>\n";
return iml_GetPartArray($a[$part-1], $rest);
}else if ($part>0){
if ((strcasecmp($a[0], "message")==0) && (strcasecmp($a[1], "rfc822")==0)){
$a = $a[8];
}
//echo "s - part: $part rest: $rest array: ".implode(" ", $a)."<br>\n";
if (is_array($a[$part-1])) return $a[$part-1];
else return $a;
}else if (($part==0) || (empty($part))){
return $a;
}
}
function iml_GetNumParts($a, $part){
if (is_array($a)){
$parent=iml_GetPartArray($a, $part);
if ((strcasecmp($parent[0], "message")==0) && (strcasecmp($parent[1], "rfc822")==0)){
$parent = $parent[8];
}
$is_array=true;
$c=0;
while (( list ($key, $val) = each ($parent) )&&($is_array)){
$is_array=is_array($parent[$key]);
if ($is_array) $c++;
}
return $c;
}
return false;
}
function iml_GetPartTypeString($a, $part){
$part_a=iml_GetPartArray($a, $part);
if ($part_a){
if (is_array($part_a[0])){
$type_str = "MULTIPART/";
reset($part_a);
while(list($n,$element)=each($part_a)){
if (!is_array($part_a[$n])){
$type_str.=$part_a[$n];
break;
}
}
return $type_str;
}else return $part_a[0]."/".$part_a[1];
}else return false;
}
function iml_GetFirstTextPart($structure,$part){
if ($part==0) $part="";
$typeCode = -1;
while ($typeCode!=0){
$typeCode = iml_GetPartTypeCode($structure, $part);
if ($typeCode == 1){
$part .= (empty($part)?"":".")."1";
}else if ($typeCode > 0){
$parts_a = explode(".", $part);
$lastPart = count($parts_a) - 1;
$parts_a[$lastPart] = (int)$parts_a[$lastPart] + 1;
$part = implode(".", $parts_a);
}else if ($typeCode == -1){
return "";
}
}
return $part;
}
function iml_GetPartTypeCode($a, $part){
$types=array(0=>"text",1=>"multipart",2=>"message",3=>"application",4=>"audio",5=>"image",6=>"video",7=>"other");
$part_a=iml_GetPartArray($a, $part);
if ($part_a){
if (is_array($part_a[0])) $str="multipart";
else $str=$part_a[0];
$code=7;
while ( list($key, $val) = each($types)) if (strcasecmp($val, $str)==0) $code=$key;
return $code;
}else return -1;
}
function iml_GetPartEncodingCode($a, $part){
$encodings=array("7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE", "OTHER");
$part_a=iml_GetPartArray($a, $part);
if ($part_a){
if (is_array($part_a[0])) return -1;
else $str=$part_a[5];
$code=5;
while ( list($key, $val) = each($encodings)) if (strcasecmp($val, $str)==0) $code=$key;
return $code;
}else return -1;
}
function iml_GetPartEncodingString($a, $part){
$part_a=iml_GetPartArray($a, $part);
if ($part_a){
if (is_array($part_a[0])) return -1;
else return $part_a[5];
}else return -1;
}
function iml_GetPartSize($a, $part){
$part_a=iml_GetPartArray($a, $part);
if ($part_a){
if (is_array($part_a[0])) return -1;
else return $part_a[6];
}else return -1;
}
function iml_GetPartID($a, $part){
$part_a=iml_GetPartArray($a, $part);
if ($part_a){
if (is_array($part_a[0])) return -1;
else return $part_a[3];
}else return -1;
}
function iml_GetPartDisposition($a, $part){
$part_a=iml_GetPartArray($a, $part);
if ($part_a){
if (is_array($part_a[0])) return -1;
else{
$id = count($part_a) - 2;
if (is_array($part_a[$id])) return $part_a[$id][0];
else return "";
}
}else return "";
}
function iml_GetPartName($a, $part){
$part_a=iml_GetPartArray($a, $part);
if ($part_a){
if (is_array($part_a[0])) return -1;
else{
$name = "";
if (is_array($part_a[2])){
//first look in content type
$name="";
while ( list($key, $val) = each ($part_a[2])){
if ((strcasecmp($val, "NAME")==0)||(strcasecmp($val, "FILENAME")==0))
$name=$part_a[2][$key+1];
}
}
if (empty($name)){
//check in content disposition
$id = count($part_a) - 2;
if ((is_array($part_a[$id])) && (is_array($part_a[$id][1]))){
$array = $part_a[$id][1];
while ( list($key, $val) = each($array)){
if ((strcasecmp($val, "NAME")==0)||(strcasecmp($val, "FILENAME")==0))
$name=$array[$key+1];
}
}
}
return $name;
}
}else return "";
}
function iml_GetPartCharset($a, $part){
$part_a=iml_GetPartArray($a, $part);
if ($part_a){
if (is_array($part_a[0])) return -1;
else{
if (is_array($part_a[2])){
$name="";
while ( list($key, $val) = each ($part_a[2])) if (strcasecmp($val, "charset")==0) $name=$part_a[2][$key+1];
return $name;
}
else return "";
}
}else return "";
}
function iml_GetPartList($a, $part){
//echo "MOO?"; flush();
$data = array();
$num_parts = iml_GetNumParts($a, $part);
//echo "($num_parts)"; flush();
if ($num_parts !== false){
//echo "<!-- ($num_parts parts)//-->\n";
for ($i = 0; $i<$num_parts; $i++){
$part_code = $part.(empty($part)?"":".").($i+1);
$part_type = iml_GetPartTypeCode($a, $part_code);
$part_disposition = iml_GetPartDisposition($a, $part_code);
//echo "<!-- part: $part_code type: $part_type //-->\n";
if (strcasecmp($part_disposition, "attachment")!=0 &&
(($part_type == 1) || ($part_type==2))){
$data = array_merge($data, iml_GetPartList($a, $part_code));
}else{
$data[$part_code]["typestring"] = iml_GetPartTypeString($a, $part_code);
$data[$part_code]["disposition"] = $part_disposition;
$data[$part_code]["size"] = iml_GetPartSize($a, $part_code);
$data[$part_code]["name"] = iml_GetPartName($a, $part_code);
$data[$part_code]["id"] = iml_GetPartID($a, $part_code);
}
}
}
return $data;
}
function iml_GetNextPart($part){
if (strpos($part, ".")===false) return $part++;
else{
$parts_a = explode(".", $part);
$num_levels = count($parts_a);
$parts_a[$num_levels-1]++;
return implode(".", $parts_a);
}
}
?>
|