]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_mime_struct.php
de283d24619a68bb87423b2f4d49eceebdff6813
[roundcube.git] / program / include / rcube_mime_struct.php
1 <?php
2
3
4 /*
5  +-----------------------------------------------------------------------+
6  | program/include/rcube_mime_struct.php                                 |
7  |                                                                       |
8  | This file is part of the Roundcube Webmail client                     |
9  | Copyright (C) 2005-2011, The Roundcube Dev Team                       |
10  | Licensed under the GNU GPL                                            |
11  |                                                                       |
12  | PURPOSE:                                                              |
13  |   Provide functions for handling mime messages structure              |
14  |                                                                       |
15  |   Based on Iloha MIME Library. See http://ilohamail.org/ for details  |
16  |                                                                       |
17  +-----------------------------------------------------------------------+
18  | Author: Aleksander Machniak <alec@alec.pl>                            |
19  | Author: Ryo Chijiiwa <Ryo@IlohaMail.org>                              |
20  +-----------------------------------------------------------------------+
21
22  $Id: rcube_mime_struct.php 4980 2011-07-27 18:21:49Z alec $
23
24 */
25
26 /**
27  * Helper class to process IMAP's BODYSTRUCTURE string
28  *
29  * @package    Mail
30  * @author     Aleksander Machniak <alec@alec.pl>
31  */
32 class rcube_mime_struct
33 {
34     private $structure;
35
36
37     function __construct($str=null)
38     {
39         if ($str)
40             $this->structure = $this->parseStructure($str);
41     }
42
43     /*
44      * Parses IMAP's BODYSTRUCTURE string into array
45     */
46     function parseStructure($str)
47     {
48         $line = substr($str, 1, strlen($str) - 2);
49         $line = str_replace(')(', ') (', $line);
50
51             $struct = rcube_imap_generic::tokenizeResponse($line);
52         if (!is_array($struct[0]) && (strcasecmp($struct[0], 'message') == 0)
53                     && (strcasecmp($struct[1], 'rfc822') == 0)) {
54                     $struct = array($struct);
55             }
56
57         return $struct;
58     }
59
60     /*
61      * Parses IMAP's BODYSTRUCTURE string into array and loads it into class internal variable
62     */
63     function loadStructure($str)
64     {
65         if (empty($str))
66             return true;
67
68         $this->structure = $this->parseStructure($str);
69         return (!empty($this->structure));
70     }
71
72     function getPartType($part)
73     {
74             $part_a = $this->getPartArray($this->structure, $part);
75             if (!empty($part_a)) {
76                     if (is_array($part_a[0]))
77                 return 'multipart';
78                     else if ($part_a[0])
79                 return $part_a[0];
80             }
81
82         return 'other';
83     }
84
85     function getPartEncoding($part)
86     {
87             $part_a = $this->getPartArray($this->structure, $part);
88             if ($part_a) {
89                     if (!is_array($part_a[0]))
90                 return $part_a[5];
91             }
92
93         return '';
94     }
95
96     function getPartCharset($part)
97     {
98             $part_a = $this->getPartArray($this->structure, $part);
99             if ($part_a) {
100                     if (is_array($part_a[0]))
101                 return '';
102                     else {
103                             if (is_array($part_a[2])) {
104                                     $name = '';
105                                     while (list($key, $val) = each($part_a[2]))
106                         if (strcasecmp($val, 'charset') == 0)
107                             return $part_a[2][$key+1];
108                             }
109                     }
110             }
111
112         return '';
113     }
114
115     function getPartArray($a, $part)
116     {
117             if (!is_array($a)) {
118             return false;
119         }
120             if (strpos($part, '.') > 0) {
121                     $original_part = $part;
122                     $pos = strpos($part, '.');
123                     $rest = substr($original_part, $pos+1);
124                     $part = substr($original_part, 0, $pos);
125                     if ((strcasecmp($a[0], 'message') == 0) && (strcasecmp($a[1], 'rfc822') == 0)) {
126                             $a = $a[8];
127                     }
128                     return self::getPartArray($a[$part-1], $rest);
129             }
130         else if ($part>0) {
131                     if (!is_array($a[0]) && (strcasecmp($a[0], 'message') == 0)
132                 && (strcasecmp($a[1], 'rfc822') == 0)) {
133                             $a = $a[8];
134                     }
135                     if (is_array($a[$part-1]))
136                 return $a[$part-1];
137                     else
138                 return $a;
139             }
140         else if (($part==0) || (empty($part))) {
141                     return $a;
142             }
143     }
144
145 }