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){
39 for ($i=$start;$i<$len;$i++){
40 if ($str[$i]=="\"") $in_quote = ($in_quote + 1) % 2;
42 if ($str[$i]=="(") $level++;
43 else if (($level > 0) && ($str[$i]==")")) $level--;
44 else if (($level == 0) && ($str[$i]==")")) return $i;
49 function iml_ParseBSString($str){
56 for ($i=0; $i<$len; $i++){
57 if ($str[$i] == "\"") $in_quote = ($in_quote + 1) % 2;
59 if ($str[$i] == " "){ //space means new element
61 while ($str[$i+1] == " ") $i++; // skip additional spaces
62 } else if ($str[$i]=="("){ //new part
64 $endPos = iml_ClosingParenPos($str, $i);
65 $partLen = $endPos - $i;
66 $part = substr($str, $i, $partLen);
67 $a[$id] = iml_ParseBSString($part); //send part string
69 echo "{>".$endPos."}";
73 }else $a[$id].=$str[$i]; //add to current element in array
75 if ($str[$i]=="\\") $i++; //escape backslashes
76 else $a[$id].=$str[$i]; //add to current element in array
84 function iml_GetRawStructureArray($str){
85 $line=substr($str, 1, strlen($str) - 2);
86 $line = str_replace(")(", ") (", $line);
88 $struct = iml_ParseBSString($line);
89 if ((strcasecmp($struct[0], "message")==0) && (strcasecmp($struct[1], "rfc822")==0)){
90 $struct = array($struct);
95 function iml_GetPartArray($a, $part){
96 if (!is_array($a)) return false;
97 if (strpos($part, ".") > 0){
98 $original_part = $part;
99 $pos = strpos($part, ".");
100 $rest = substr($original_part, $pos+1);
101 $part = substr($original_part, 0, $pos);
102 if ((strcasecmp($a[0], "message")==0) && (strcasecmp($a[1], "rfc822")==0)){
105 //echo "m - part: $original_part current: $part rest: $rest array: ".implode(" ", $a)."<br>\n";
106 return iml_GetPartArray($a[$part-1], $rest);
108 if ((strcasecmp($a[0], "message")==0) && (strcasecmp($a[1], "rfc822")==0)){
111 //echo "s - part: $part rest: $rest array: ".implode(" ", $a)."<br>\n";
112 if (is_array($a[$part-1])) return $a[$part-1];
114 }else if (($part==0) || (empty($part))){
119 function iml_GetNumParts($a, $part){
121 $parent=iml_GetPartArray($a, $part);
123 if ((strcasecmp($parent[0], "message")==0) && (strcasecmp($parent[1], "rfc822")==0)){
124 $parent = $parent[8];
129 while (( list ($key, $val) = each ($parent) )&&($is_array)){
130 $is_array=is_array($parent[$key]);
139 function iml_GetPartTypeString($a, $part){
140 $part_a=iml_GetPartArray($a, $part);
142 if (is_array($part_a[0])){
143 $type_str = "MULTIPART/";
145 while(list($n,$element)=each($part_a)){
146 if (!is_array($part_a[$n])){
147 $type_str.=$part_a[$n];
152 }else return $part_a[0]."/".$part_a[1];
156 function iml_GetFirstTextPart($structure,$part){
157 if ($part==0) $part="";
159 while ($typeCode!=0){
160 $typeCode = iml_GetPartTypeCode($structure, $part);
162 $part .= (empty($part)?"":".")."1";
163 }else if ($typeCode > 0){
164 $parts_a = explode(".", $part);
165 $lastPart = count($parts_a) - 1;
166 $parts_a[$lastPart] = (int)$parts_a[$lastPart] + 1;
167 $part = implode(".", $parts_a);
168 }else if ($typeCode == -1){
176 function iml_GetPartTypeCode($a, $part){
177 $types=array(0=>"text",1=>"multipart",2=>"message",3=>"application",4=>"audio",5=>"image",6=>"video",7=>"other");
179 $part_a=iml_GetPartArray($a, $part);
181 if (is_array($part_a[0])) $str="multipart";
182 else $str=$part_a[0];
185 while ( list($key, $val) = each($types)) if (strcasecmp($val, $str)==0) $code=$key;
190 function iml_GetPartEncodingCode($a, $part){
191 $encodings=array("7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE", "OTHER");
193 $part_a=iml_GetPartArray($a, $part);
195 if (is_array($part_a[0])) return -1;
196 else $str=$part_a[5];
199 while ( list($key, $val) = each($encodings)) if (strcasecmp($val, $str)==0) $code=$key;
206 function iml_GetPartEncodingString($a, $part){
207 $part_a=iml_GetPartArray($a, $part);
209 if (is_array($part_a[0])) return -1;
210 else return $part_a[5];
214 function iml_GetPartSize($a, $part){
215 $part_a=iml_GetPartArray($a, $part);
217 if (is_array($part_a[0])) return -1;
218 else return $part_a[6];
222 function iml_GetPartID($a, $part){
223 $part_a=iml_GetPartArray($a, $part);
225 if (is_array($part_a[0])) return -1;
226 else return $part_a[3];
230 function iml_GetPartDisposition($a, $part){
231 $part_a=iml_GetPartArray($a, $part);
233 if (is_array($part_a[0])) return -1;
235 $id = count($part_a) - 2;
236 if (is_array($part_a[$id])) return $part_a[$id][0];
242 function iml_GetPartName($a, $part){
243 $part_a=iml_GetPartArray($a, $part);
245 if (is_array($part_a[0])) return -1;
248 if (is_array($part_a[2])){
249 //first look in content type
251 while ( list($key, $val) = each ($part_a[2])){
252 if ((strcasecmp($val, "NAME")==0)||(strcasecmp($val, "FILENAME")==0))
253 $name=$part_a[2][$key+1];
257 //check in content disposition
258 $id = count($part_a) - 2;
259 if ((is_array($part_a[$id])) && (is_array($part_a[$id][1]))){
260 $array = $part_a[$id][1];
261 while ( list($key, $val) = each($array)){
262 if ((strcasecmp($val, "NAME")==0)||(strcasecmp($val, "FILENAME")==0))
263 $name=$array[$key+1];
273 function iml_GetPartCharset($a, $part){
274 $part_a=iml_GetPartArray($a, $part);
276 if (is_array($part_a[0])) return -1;
278 if (is_array($part_a[2])){
280 while ( list($key, $val) = each ($part_a[2])) if (strcasecmp($val, "charset")==0) $name=$part_a[2][$key+1];
288 function iml_GetPartList($a, $part){
289 //echo "MOO?"; flush();
291 $num_parts = iml_GetNumParts($a, $part);
292 //echo "($num_parts)"; flush();
293 if ($num_parts !== false){
294 //echo "<!-- ($num_parts parts)//-->\n";
295 for ($i = 0; $i<$num_parts; $i++){
296 $part_code = $part.(empty($part)?"":".").($i+1);
297 $part_type = iml_GetPartTypeCode($a, $part_code);
298 $part_disposition = iml_GetPartDisposition($a, $part_code);
299 //echo "<!-- part: $part_code type: $part_type //-->\n";
300 if (strcasecmp($part_disposition, "attachment")!=0 &&
301 (($part_type == 1) || ($part_type==2))){
302 $data = array_merge($data, iml_GetPartList($a, $part_code));
304 $data[$part_code]["typestring"] = iml_GetPartTypeString($a, $part_code);
305 $data[$part_code]["disposition"] = $part_disposition;
306 $data[$part_code]["size"] = iml_GetPartSize($a, $part_code);
307 $data[$part_code]["name"] = iml_GetPartName($a, $part_code);
308 $data[$part_code]["id"] = iml_GetPartID($a, $part_code);
315 function iml_GetNextPart($part){
316 if (strpos($part, ".")===false) return $part++;
318 $parts_a = explode(".", $part);
319 $num_levels = count($parts_a);
320 $parts_a[$num_levels-1]++;
321 return implode(".", $parts_a);