2 /////////////////////////////////////////////////////////
4 // Iloha MIME Library (IML)
6 // (C)Copyright 2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
8 // This file is part of IlohaMail. IlohaMail is free software released
9 // under the GPL license. See enclosed file COPYING for details, or
10 // see http://www.fsf.org/copyleft/gpl.html
12 /////////////////////////////////////////////////////////
14 /********************************************************
16 FILE: include/mime.inc
18 Provide functions for handling mime messages.
20 Use iil_C_FetchStructureString to get IMAP structure stirng, then pass that through
21 iml_GetRawStructureArray() to get root node to a nested data structure.
22 Pass root node to the iml_GetPart*() functions to retreive individual bits of info.
24 ********************************************************/
29 $MIME_APPLICATION = 3;
35 function iml_ClosingParenPos($str, $start) {
40 for ($i=$start; $i<$len; $i++) {
41 if ($str[$i] == '"' && $str[$i-1] != "\\")
42 $in_quote = ($in_quote + 1) % 2;
44 if ($str[$i]=="(") $level++;
45 else if (($level > 0) && ($str[$i]==")")) $level--;
46 else if (($level == 0) && ($str[$i]==")")) return $i;
51 function iml_ParseBSString($str){
58 for ($i=0; $i<$len; $i++) {
59 if ($str[$i] == '"') {
60 $in_quote = ($in_quote + 1) % 2;
61 } else if (!$in_quote) {
62 if ($str[$i] == " ") { //space means new element
64 while ($str[$i+1] == " ") $i++; // skip additional spaces
65 } else if ($str[$i]=="(") { //new part
67 $endPos = iml_ClosingParenPos($str, $i);
68 $partLen = $endPos - $i;
69 if ($partLen < 0) break;
70 $part = substr($str, $i, $partLen);
71 $a[$id] = iml_ParseBSString($part); //send part string
74 $a[$id].=$str[$i]; //add to current element in array
75 } else if ($in_quote) {
77 $i++; //escape backslashes
78 if ($str[$i] == '"' || $str[$i] == "\\")
81 $a[$id] .= $str[$i]; //add to current element in array
89 function iml_GetRawStructureArray($str){
90 $line=substr($str, 1, strlen($str) - 2);
91 $line = str_replace(")(", ") (", $line);
93 $struct = iml_ParseBSString($line);
94 if ((strcasecmp($struct[0], "message")==0) && (strcasecmp($struct[1], "rfc822")==0)){
95 $struct = array($struct);
100 function iml_GetPartArray($a, $part){
101 if (!is_array($a)) return false;
102 if (strpos($part, ".") > 0){
103 $original_part = $part;
104 $pos = strpos($part, ".");
105 $rest = substr($original_part, $pos+1);
106 $part = substr($original_part, 0, $pos);
107 if ((strcasecmp($a[0], "message")==0) && (strcasecmp($a[1], "rfc822")==0)){
110 //echo "m - part: $original_part current: $part rest: $rest array: ".implode(" ", $a)."<br>\n";
111 return iml_GetPartArray($a[$part-1], $rest);
113 if ((strcasecmp($a[0], "message")==0) && (strcasecmp($a[1], "rfc822")==0)){
116 //echo "s - part: $part rest: $rest array: ".implode(" ", $a)."<br>\n";
117 if (is_array($a[$part-1])) return $a[$part-1];
119 }else if (($part==0) || (empty($part))){
124 function iml_GetNumParts($a, $part){
126 $parent=iml_GetPartArray($a, $part);
128 if ((strcasecmp($parent[0], "message")==0) && (strcasecmp($parent[1], "rfc822")==0)){
129 $parent = $parent[8];
134 while (( list ($key, $val) = each ($parent) )&&($is_array)){
135 $is_array=is_array($parent[$key]);
144 function iml_GetPartTypeString($a, $part){
145 $part_a=iml_GetPartArray($a, $part);
147 if (is_array($part_a[0])){
148 $type_str = "MULTIPART/";
150 while(list($n,$element)=each($part_a)){
151 if (!is_array($part_a[$n])){
152 $type_str.=$part_a[$n];
157 }else return $part_a[0]."/".$part_a[1];
161 function iml_GetFirstTextPart($structure,$part){
162 if ($part==0) $part="";
164 while ($typeCode!=0){
165 $typeCode = iml_GetPartTypeCode($structure, $part);
167 $part .= (empty($part)?"":".")."1";
168 }else if ($typeCode > 0){
169 $parts_a = explode(".", $part);
170 $lastPart = count($parts_a) - 1;
171 $parts_a[$lastPart] = (int)$parts_a[$lastPart] + 1;
172 $part = implode(".", $parts_a);
173 }else if ($typeCode == -1){
181 function iml_GetPartTypeCode($a, $part){
182 $types=array(0=>"text",1=>"multipart",2=>"message",3=>"application",4=>"audio",5=>"image",6=>"video",7=>"other");
184 $part_a=iml_GetPartArray($a, $part);
186 if (is_array($part_a[0])) $str="multipart";
187 else $str=$part_a[0];
190 while ( list($key, $val) = each($types)) if (strcasecmp($val, $str)==0) $code=$key;
195 function iml_GetPartEncodingCode($a, $part){
196 $encodings=array("7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE", "OTHER");
198 $part_a=iml_GetPartArray($a, $part);
200 if (is_array($part_a[0])) return -1;
201 else $str=$part_a[5];
204 while ( list($key, $val) = each($encodings)) if (strcasecmp($val, $str)==0) $code=$key;
211 function iml_GetPartEncodingString($a, $part){
212 $part_a=iml_GetPartArray($a, $part);
214 if (is_array($part_a[0])) return -1;
215 else return $part_a[5];
219 function iml_GetPartSize($a, $part){
220 $part_a=iml_GetPartArray($a, $part);
222 if (is_array($part_a[0])) return -1;
223 else return $part_a[6];
227 function iml_GetPartID($a, $part){
228 $part_a=iml_GetPartArray($a, $part);
230 if (is_array($part_a[0])) return -1;
231 else return $part_a[3];
235 function iml_GetPartDisposition($a, $part){
236 $part_a=iml_GetPartArray($a, $part);
238 if (is_array($part_a[0])) return -1;
240 $id = count($part_a) - 2;
241 if (is_array($part_a[$id])) return $part_a[$id][0];
247 function iml_GetPartName($a, $part){
248 $part_a=iml_GetPartArray($a, $part);
250 if (is_array($part_a[0])) return -1;
253 if (is_array($part_a[2])){
254 //first look in content type
256 while ( list($key, $val) = each ($part_a[2])){
257 if ((strcasecmp($val, "NAME")==0)||(strcasecmp($val, "FILENAME")==0))
258 $name=$part_a[2][$key+1];
262 //check in content disposition
263 $id = count($part_a) - 2;
264 if ((is_array($part_a[$id])) && (is_array($part_a[$id][1]))){
265 $array = $part_a[$id][1];
266 while ( list($key, $val) = each($array)){
267 if ((strcasecmp($val, "NAME")==0)||(strcasecmp($val, "FILENAME")==0))
268 $name=$array[$key+1];
278 function iml_GetPartCharset($a, $part){
279 $part_a=iml_GetPartArray($a, $part);
281 if (is_array($part_a[0])) return -1;
283 if (is_array($part_a[2])){
285 while ( list($key, $val) = each ($part_a[2])) if (strcasecmp($val, "charset")==0) $name=$part_a[2][$key+1];
293 function iml_GetPartList($a, $part){
294 //echo "MOO?"; flush();
296 $num_parts = iml_GetNumParts($a, $part);
297 //echo "($num_parts)"; flush();
298 if ($num_parts !== false){
299 //echo "<!-- ($num_parts parts)//-->\n";
300 for ($i = 0; $i<$num_parts; $i++){
301 $part_code = $part.(empty($part)?"":".").($i+1);
302 $part_type = iml_GetPartTypeCode($a, $part_code);
303 $part_disposition = iml_GetPartDisposition($a, $part_code);
304 //echo "<!-- part: $part_code type: $part_type //-->\n";
305 if (strcasecmp($part_disposition, "attachment")!=0 &&
306 (($part_type == 1) || ($part_type==2))){
307 $data = array_merge($data, iml_GetPartList($a, $part_code));
309 $data[$part_code]["typestring"] = iml_GetPartTypeString($a, $part_code);
310 $data[$part_code]["disposition"] = $part_disposition;
311 $data[$part_code]["size"] = iml_GetPartSize($a, $part_code);
312 $data[$part_code]["name"] = iml_GetPartName($a, $part_code);
313 $data[$part_code]["id"] = iml_GetPartID($a, $part_code);
320 function iml_GetNextPart($part){
321 if (strpos($part, ".")===false) return $part++;
323 $parts_a = explode(".", $part);
324 $num_levels = count($parts_a);
325 $parts_a[$num_levels-1]++;
326 return implode(".", $parts_a);