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