[2] | 1 | <?php |
---|
| 2 | class message_components { |
---|
| 3 | |
---|
| 4 | /** |
---|
| 5 | *+----------------------------------------------------------------------------------------------------+ |
---|
| 6 | *| IMAP message scanner - scans information provided by imap_fetchstructure() | |
---|
| 7 | *| | |
---|
| 8 | *| Author: Richard York | |
---|
| 9 | *| mailto:richy at smilingsouls.net | |
---|
| 10 | *| http://www.smilingsouls.net | |
---|
| 11 | *| | |
---|
| 12 | *| (c) Copyright 2004, Richard York, All Rights Reseverd | |
---|
| 13 | *+----------------------------------------------------------------------------------------------------+ |
---|
| 14 | ** |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | var $mailbox; // (resource) Imap stream |
---|
| 18 | |
---|
| 19 | var $data_types; // (array)(string) Various message part types |
---|
| 20 | var $encoding_types; // (array)(string) Various encoding types |
---|
| 21 | |
---|
| 22 | // first array uses message id as key |
---|
| 23 | // nested array is offset corresponding with the number of parts |
---|
| 24 | |
---|
| 25 | var $structure; // (array)(object) Contains the complete body structure |
---|
| 26 | var $pid; // (array)(array)(str) part id |
---|
| 27 | var $file_type; // (array)(array)(str) mime type |
---|
| 28 | var $disposition; // (array)(array)(str) inline | attachment |
---|
| 29 | var $fsize; // (array)(array)(int) part size in bytes |
---|
| 30 | var $encoding; // (array)(array)(str) message encoding |
---|
| 31 | var $charset; // (array)(array)(int) message charset |
---|
| 32 | var $fname; // (array)(array)(str) original file name |
---|
| 33 | var $inline_id; // (array)(array)(str) string containing the id for multipart/related |
---|
| 34 | var $has_attachments; // (array)(array)(bool) |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * CONSTRUCTOR |
---|
| 38 | * |
---|
| 39 | * void message_components(resource imap stream) |
---|
| 40 | ** |
---|
| 41 | */ |
---|
| 42 | |
---|
| 43 | function message_components($mailbox) |
---|
| 44 | { |
---|
| 45 | $this->data_types = array(); |
---|
| 46 | |
---|
| 47 | $this->data_types[0] = 'text'; |
---|
| 48 | $this->data_types[1] = 'multipart'; |
---|
| 49 | $this->data_types[2] = 'message'; |
---|
| 50 | $this->data_types[3] = 'application'; |
---|
| 51 | $this->data_types[4] = 'audio'; |
---|
| 52 | $this->data_types[5] = 'image'; |
---|
| 53 | $this->data_types[6] = 'video'; |
---|
| 54 | $this->data_types[7] = 'other'; |
---|
| 55 | |
---|
| 56 | $this->encoding_types = array(); |
---|
| 57 | |
---|
| 58 | $this->encoding_types[0] = '7bit'; |
---|
| 59 | $this->encoding_types[1] = '8bit'; |
---|
| 60 | $this->encoding_types[2] = 'binary'; |
---|
| 61 | $this->encoding_types[3] = 'base64'; |
---|
| 62 | $this->encoding_types[4] = 'quoted-printable'; |
---|
| 63 | $this->encoding_types[5] = 'other'; |
---|
| 64 | |
---|
| 65 | $this->mailbox = $mailbox; |
---|
| 66 | |
---|
| 67 | return; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * void fetch_structure(int message id[, array recursive subpart[, array recursive parent part id[, int recursive counter[, bool recursive is a sub part[, bool recursive skip part]]]]]) |
---|
| 72 | * Indexes the structure of a message body. |
---|
| 73 | * |
---|
| 74 | * Gather message information returned by imap_fetchstructure() |
---|
| 75 | * Recursively iterate through each parts array |
---|
| 76 | * Concatenate part numbers in the following format `1.1` each part id is separated by a period, each referring |
---|
| 77 | * to a part or subpart of a multipart message. Create part numbers as such that they are compatible with imap_fetchbody() |
---|
| 78 | ** |
---|
| 79 | */ |
---|
| 80 | |
---|
| 81 | function fetch_structure($mid, $sub_part = null, $sub_pid = null, $n = 0, $is_sub_part = false, $skip_part = false) |
---|
| 82 | { |
---|
| 83 | if (!is_array($sub_part)) |
---|
| 84 | { |
---|
| 85 | $this->structure[$mid] = imap_fetchstructure($this->mailbox, $mid, FT_UID); |
---|
| 86 | } |
---|
| 87 | if (isset($this->structure[$mid]->parts) || is_array($sub_part)) |
---|
| 88 | { |
---|
| 89 | if ($is_sub_part == false) |
---|
| 90 | { |
---|
| 91 | $parts = $this->structure[$mid]->parts; |
---|
| 92 | } |
---|
| 93 | else |
---|
| 94 | { |
---|
| 95 | $parts = $sub_part; |
---|
| 96 | $n++; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | for($p = 0, $i = 1; $p < count($parts); $n++, $p++, $i++) |
---|
| 100 | { |
---|
| 101 | // Skip the following... |
---|
| 102 | // Skip multipart/mixed! |
---|
| 103 | // Skip subsequent multipart/alternative if this part is message/rfc822 |
---|
| 104 | // Skip multipart/related |
---|
| 105 | |
---|
| 106 | $ftype = (empty($parts[$p]->type))? $this->data_types[0].'/'.strtolower($parts[$p]->subtype) : $this->data_types[$parts[$p]->type].'/'.strtolower($parts[$p]->subtype); |
---|
| 107 | $encoding = (empty($parts[$p]->encoding))? $this->encoding_types[0] : $this->encoding_types[$parts[$p]->encoding]; |
---|
| 108 | if(!preg_match("/5./",phpversion())) |
---|
[1489] | 109 | $charset = $parts[$p]->parameters[0]->value; |
---|
[2] | 110 | else |
---|
[1739] | 111 | $charset = ( isset( $parts->p->parameters[0]->value ) ) ? $parts->p->parameters[0]->value : NULL; |
---|
[2] | 112 | $skip_next = ($ftype == 'message/rfc822')? true : false; |
---|
| 113 | |
---|
[3018] | 114 | if ($ftype == 'multipart/report' || $ftype == 'multipart/mixed' || $skip_part == true && $ftype == 'multipart/alternative' || ( $ftype == 'multipart/related' && strtolower( $parts[$p]->parts[0]->subtype ) == 'alternative' ) ) |
---|
[2] | 115 | { |
---|
| 116 | $n--; |
---|
| 117 | } |
---|
| 118 | else |
---|
[3018] | 119 | { |
---|
| 120 | $this->pid[$mid][$n] = ($is_sub_part == false || $skip_part && $ftype == 'multipart/related' )? $i : ($sub_pid == '' ? '1' : $sub_pid).'.'.$i; |
---|
[2] | 121 | $this->file_type[$mid][$n] = $ftype; |
---|
| 122 | $this->encoding[$mid][$n] = $encoding; |
---|
| 123 | $this->charset[$mid][$n] = $charset; |
---|
| 124 | $this->fsize[$mid][$n] = (!isset($parts[$p]->bytes) || empty($parts[$p]->bytes))? 0 : $parts[$p]->bytes; |
---|
[13] | 125 | $hasAttachment = false; |
---|
[2] | 126 | # Force inline disposition if none is present |
---|
| 127 | //if ($parts[$p]->ifdisposition == true) |
---|
| 128 | //{ por niltonneto, as vezes, inline anexos nao eram exibidos. |
---|
[1739] | 129 | $this->disposition[$mid][$n] = ( isset( $parts[$p]->disposition ) ) ? strtolower($parts[$p]->disposition) : NULL; |
---|
[2] | 130 | |
---|
| 131 | //if (strtolower($parts[$p]->disposition) == 'attachment') |
---|
| 132 | //{ por jakjr, as vezes, inline anexos nao eram exibidos. |
---|
| 133 | if ($parts[$p]->ifdparameters == true) |
---|
| 134 | { |
---|
| 135 | $params = $parts[$p]->dparameters; |
---|
| 136 | |
---|
| 137 | foreach ($params as $param) |
---|
| 138 | { |
---|
| 139 | if((strtolower($param->attribute) == 'filename') || (strtolower($param->attribute) == 'name')) |
---|
| 140 | { |
---|
| 141 | $this->fname[$mid][$n] = $param->value; |
---|
[13] | 142 | $hasAttachment = true; |
---|
[2] | 143 | break; |
---|
[13] | 144 | } |
---|
[2] | 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | // Alguns web-mails utilizam o parameters |
---|
[13] | 149 | if ($parts[$p]->ifparameters == true && !$hasAttachment) |
---|
[2] | 150 | { |
---|
| 151 | $params = $parts[$p]->parameters; |
---|
| 152 | foreach ($params as $param) |
---|
| 153 | { |
---|
| 154 | if((strtolower($param->attribute) == 'filename') || (strtolower($param->attribute) == 'name' |
---|
| 155 | || strtolower($param->attribute) == 'filename*') || strtolower($param->attribute) == 'name*') |
---|
| 156 | { |
---|
| 157 | if(strstr(strtolower($param->value), "iso-8859-1''")){ |
---|
| 158 | |
---|
| 159 | $value = explode("''",$param->value); |
---|
| 160 | $this->fname[$mid][$n] = rawurldecode($value[1]); |
---|
| 161 | } |
---|
| 162 | else |
---|
| 163 | $this->fname[$mid][$n] = $param->value; |
---|
| 164 | |
---|
| 165 | break; |
---|
| 166 | } |
---|
[10] | 167 | if(strtolower($param->attribute) == 'charset'){ |
---|
| 168 | if($this->charset[$mid][$n] == '') |
---|
[13] | 169 | $this->charset[$mid][$n] = $param->value; |
---|
[10] | 170 | } |
---|
[2] | 171 | } |
---|
[10] | 172 | } |
---|
[2] | 173 | //} |
---|
| 174 | /*} |
---|
| 175 | else |
---|
| 176 | { |
---|
| 177 | $this->disposition[$mid][$n] = 'inline'; |
---|
| 178 | }*/ |
---|
| 179 | |
---|
| 180 | if ($parts[$p]->ifid == true) |
---|
| 181 | { |
---|
| 182 | $this->inline_id[$mid][$n] = $parts[$p]->id; |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | if (isset($parts[$p]->parts) && is_array($parts[$p]->parts)) |
---|
| 187 | { |
---|
| 188 | $this->has_attachments[$mid][$n] = true; |
---|
| 189 | $n = $this->fetch_structure($mid, $parts[$p]->parts, $this->pid[$mid][$n], $n, true, $skip_next); |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
| 193 | $this->has_attachments[$mid][$n] = false; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | if ($is_sub_part == true) |
---|
| 198 | { |
---|
| 199 | return $n; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | // $parts is not an array... message is flat |
---|
| 204 | else |
---|
| 205 | { |
---|
| 206 | $this->pid[$mid][0] = 1; |
---|
| 207 | |
---|
| 208 | if (empty($this->structure[$mid]->type)) |
---|
| 209 | { |
---|
| 210 | $this->structure[$mid]->type = (int) 0; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | if (isset($this->structure[$mid]->subtype)) |
---|
| 214 | { |
---|
| 215 | $this->file_type[$mid][0] = $this->data_types[$this->structure[$mid]->type].'/'.strtolower($this->structure[$mid]->subtype); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | if (empty($this->structure[$mid]->encoding)) |
---|
| 219 | { |
---|
| 220 | $this->structure[$mid]->encoding = (int) 0; |
---|
| 221 | } |
---|
| 222 | |
---|
[13] | 223 | $this->encoding[$mid][0] = $this->encoding_types[$this->structure[$mid]->encoding]; |
---|
[2] | 224 | if(!preg_match("/5./",phpversion())) |
---|
| 225 | $this->charset[$mid][0] = $this->structure[$mid]->parameters[0]->value; |
---|
| 226 | else |
---|
[1739] | 227 | $this->charset[$mid][0] = ( isset( $this->structure->mid->parameters[0]->value ) ) ? $this->structure->mid->parameters[0]->value : NULL; |
---|
[2] | 228 | |
---|
| 229 | if (isset($this->structure[$mid]->bytes)) |
---|
| 230 | { |
---|
| 231 | $this->fsize[$mid][0] = strtolower($this->structure[$mid]->bytes); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | if (isset($this->structure[$mid]->ifdparameters)) |
---|
| 235 | { |
---|
[1739] | 236 | $params = ( isset( $this->structure[$mid]->dparameters ) ) ? $this->structure[$mid]->dparameters : NULL; |
---|
[2] | 237 | $n = 0; |
---|
| 238 | if($params) |
---|
| 239 | foreach ($params as $param) |
---|
| 240 | { |
---|
| 241 | if((strtolower($param->attribute) == 'filename') || (strtolower($param->attribute) == 'name')) |
---|
| 242 | { |
---|
| 243 | $this->fname[$mid][$n] = $param->value; |
---|
| 244 | break; |
---|
| 245 | } |
---|
| 246 | $n++; |
---|
| 247 | } |
---|
| 248 | } |
---|
[13] | 249 | if (isset($this->structure[$mid]->ifparameters)) |
---|
| 250 | { |
---|
| 251 | $params = $this->structure[$mid]->parameters; |
---|
| 252 | $n = 0; |
---|
| 253 | if($params) |
---|
| 254 | foreach ($params as $param) |
---|
| 255 | { |
---|
| 256 | if(strtolower($param->attribute) == 'charset'){ |
---|
| 257 | if($this->charset[$mid][$n] == '') |
---|
| 258 | $this->charset[$mid][$n] = $param->value; |
---|
[3397] | 259 | $n++; |
---|
[13] | 260 | } |
---|
| 261 | } |
---|
| 262 | } |
---|
[1739] | 263 | $this->disposition[$mid][0] = ( isset( $this->structure[$mid]->disposition ) ) ? $this->structure[$mid]->disposition : NULL; |
---|
[2] | 264 | //$this->disposition[$mid][0] = 'inline'; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | return; |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | /* |
---|
| 272 | // Example usage -- dump part ids for the specified message.. |
---|
| 273 | |
---|
| 274 | $msg =& new message_components($mb); |
---|
| 275 | $msg->fetch_structure(12905); |
---|
| 276 | |
---|
| 277 | echo '<pre>'; |
---|
| 278 | //print_r($msg->pid[12905]); |
---|
| 279 | echo count ($msg->fname[12905]); |
---|
| 280 | echo '</pre>'; |
---|
| 281 | |
---|
| 282 | // also important to note that the offset numbering in the sub array isn't precise... $msg->pid[$mid][0].. |
---|
| 283 | // I have a bug somewhere in there.. but I use foreach when accessing these arrays anyway. |
---|
| 284 | */ |
---|
[1037] | 285 | ?> |
---|