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] == " ") $id++; //space means new element
60 else if ($str[$i]=="("){ //new part
62 $endPos = iml_ClosingParenPos($str, $i);
63 $partLen = $endPos - $i;
64 $part = substr($str, $i, $partLen);
65 $a[$id] = iml_ParseBSString($part); //send part string
67 echo "{>".$endPos."}";
71 }else $a[$id].=$str[$i]; //add to current element in array
73 if ($str[$i]=="\\") $i++; //escape backslashes
74 else $a[$id].=$str[$i]; //add to current element in array
82 function iml_GetRawStructureArray($str){
83 $line=substr($str, 1, strlen($str) - 2);
84 $line = str_replace(")(", ") (", $line);
86 $struct = iml_ParseBSString($line);
87 if ((strcasecmp($struct[0], "message")==0) && (strcasecmp($struct[1], "rfc822")==0)){
88 $struct = array($struct);
93 function iml_GetPartArray($a, $part){
94 if (!is_array($a)) return false;
95 if (strpos($part, ".") > 0){
96 $original_part = $part;
97 $pos = strpos($part, ".");
98 $rest = substr($original_part, $pos+1);
99 $part = substr($original_part, 0, $pos);
100 if ((strcasecmp($a[0], "message")==0) && (strcasecmp($a[1], "rfc822")==0)){
103 //echo "m - part: $original_part current: $part rest: $rest array: ".implode(" ", $a)."<br>\n";
104 return iml_GetPartArray($a[$part-1], $rest);
106 if ((strcasecmp($a[0], "message")==0) && (strcasecmp($a[1], "rfc822")==0)){
109 //echo "s - part: $part rest: $rest array: ".implode(" ", $a)."<br>\n";
110 if (is_array($a[$part-1])) return $a[$part-1];
112 }else if (($part==0) || (empty($part))){
117 function iml_GetNumParts($a, $part){
119 $parent=iml_GetPartArray($a, $part);
121 if ((strcasecmp($parent[0], "message")==0) && (strcasecmp($parent[1], "rfc822")==0)){
122 $parent = $parent[8];
127 while (( list ($key, $val) = each ($parent) )&&($is_array)){
128 $is_array=is_array($parent[$key]);
137 function iml_GetPartTypeString($a, $part){
138 $part_a=iml_GetPartArray($a, $part);
140 if (is_array($part_a[0])){
141 $type_str = "MULTIPART/";
143 while(list($n,$element)=each($part_a)){
144 if (!is_array($part_a[$n])){
145 $type_str.=$part_a[$n];
150 }else return $part_a[0]."/".$part_a[1];
154 function iml_GetFirstTextPart($structure,$part){
155 if ($part==0) $part="";
157 while ($typeCode!=0){
158 $typeCode = iml_GetPartTypeCode($structure, $part);
160 $part .= (empty($part)?"":".")."1";
161 }else if ($typeCode > 0){
162 $parts_a = explode(".", $part);
163 $lastPart = count($parts_a) - 1;
164 $parts_a[$lastPart] = (int)$parts_a[$lastPart] + 1;
165 $part = implode(".", $parts_a);
166 }else if ($typeCode == -1){
174 function iml_GetPartTypeCode($a, $part){
175 $types=array(0=>"text",1=>"multipart",2=>"message",3=>"application",4=>"audio",5=>"image",6=>"video",7=>"other");
177 $part_a=iml_GetPartArray($a, $part);
179 if (is_array($part_a[0])) $str="multipart";
180 else $str=$part_a[0];
183 while ( list($key, $val) = each($types)) if (strcasecmp($val, $str)==0) $code=$key;
188 function iml_GetPartEncodingCode($a, $part){
189 $encodings=array("7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE", "OTHER");
191 $part_a=iml_GetPartArray($a, $part);
193 if (is_array($part_a[0])) return -1;
194 else $str=$part_a[5];
197 while ( list($key, $val) = each($encodings)) if (strcasecmp($val, $str)==0) $code=$key;
204 function iml_GetPartEncodingString($a, $part){
205 $part_a=iml_GetPartArray($a, $part);
207 if (is_array($part_a[0])) return -1;
208 else return $part_a[5];
212 function iml_GetPartSize($a, $part){
213 $part_a=iml_GetPartArray($a, $part);
215 if (is_array($part_a[0])) return -1;
216 else return $part_a[6];
220 function iml_GetPartID($a, $part){
221 $part_a=iml_GetPartArray($a, $part);
223 if (is_array($part_a[0])) return -1;
224 else return $part_a[3];
228 function iml_GetPartDisposition($a, $part){
229 $part_a=iml_GetPartArray($a, $part);
231 if (is_array($part_a[0])) return -1;
233 $id = count($part_a) - 2;
234 if (is_array($part_a[$id])) return $part_a[$id][0];
240 function iml_GetPartName($a, $part){
241 $part_a=iml_GetPartArray($a, $part);
243 if (is_array($part_a[0])) return -1;
246 if (is_array($part_a[2])){
247 //first look in content type
249 while ( list($key, $val) = each ($part_a[2])){
250 if ((strcasecmp($val, "NAME")==0)||(strcasecmp($val, "FILENAME")==0))
251 $name=$part_a[2][$key+1];
255 //check in content disposition
256 $id = count($part_a) - 2;
257 if ((is_array($part_a[$id])) && (is_array($part_a[$id][1]))){
258 $array = $part_a[$id][1];
259 while ( list($key, $val) = each($array)){
260 if ((strcasecmp($val, "NAME")==0)||(strcasecmp($val, "FILENAME")==0))
261 $name=$array[$key+1];
271 function iml_GetPartCharset($a, $part){
272 $part_a=iml_GetPartArray($a, $part);
274 if (is_array($part_a[0])) return -1;
276 if (is_array($part_a[2])){
278 while ( list($key, $val) = each ($part_a[2])) if (strcasecmp($val, "charset")==0) $name=$part_a[2][$key+1];
286 function iml_GetPartList($a, $part){
287 //echo "MOO?"; flush();
289 $num_parts = iml_GetNumParts($a, $part);
290 //echo "($num_parts)"; flush();
291 if ($num_parts !== false){
292 //echo "<!-- ($num_parts parts)//-->\n";
293 for ($i = 0; $i<$num_parts; $i++){
294 $part_code = $part.(empty($part)?"":".").($i+1);
295 $part_type = iml_GetPartTypeCode($a, $part_code);
296 $part_disposition = iml_GetPartDisposition($a, $part_code);
297 //echo "<!-- part: $part_code type: $part_type //-->\n";
298 if (strcasecmp($part_disposition, "attachment")!=0 &&
299 (($part_type == 1) || ($part_type==2))){
300 $data = array_merge($data, iml_GetPartList($a, $part_code));
302 $data[$part_code]["typestring"] = iml_GetPartTypeString($a, $part_code);
303 $data[$part_code]["disposition"] = $part_disposition;
304 $data[$part_code]["size"] = iml_GetPartSize($a, $part_code);
305 $data[$part_code]["name"] = iml_GetPartName($a, $part_code);
306 $data[$part_code]["id"] = iml_GetPartID($a, $part_code);
313 function iml_GetNextPart($part){
314 if (strpos($part, ".")===false) return $part++;
316 $parts_a = explode(".", $part);
317 $num_levels = count($parts_a);
318 $parts_a[$num_levels-1]++;
319 return implode(".", $parts_a);