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