| [4e17e6c] | 1 | <?php |
|---|
| 2 | ///////////////////////////////////////////////////////// |
|---|
| 3 | // |
|---|
| 4 | // Iloha IMAP Library (IIL) |
|---|
| 5 | // |
|---|
| 6 | // (C)Copyright 2002 Ryo Chijiiwa <Ryo@IlohaMail.org> |
|---|
| 7 | // |
|---|
| 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 |
|---|
| 11 | // |
|---|
| 12 | ///////////////////////////////////////////////////////// |
|---|
| 13 | |
|---|
| 14 | /******************************************************** |
|---|
| 15 | |
|---|
| 16 | FILE: include/imap.inc |
|---|
| 17 | PURPOSE: |
|---|
| 18 | Provide alternative IMAP library that doesn't rely on the standard |
|---|
| 19 | C-Client based version. This allows IlohaMail to function regardless |
|---|
| 20 | of whether or not the PHP build it's running on has IMAP functionality |
|---|
| 21 | built-in. |
|---|
| 22 | USEAGE: |
|---|
| 23 | Function containing "_C_" in name require connection handler to be |
|---|
| 24 | passed as one of the parameters. To obtain connection handler, use |
|---|
| 25 | iil_Connect() |
|---|
| [0284c23] | 26 | VERSION: |
|---|
| 27 | IlohaMail-0.9-20050415 |
|---|
| 28 | CHANGES: |
|---|
| 29 | File altered by Thomas Bruederli <roundcube@gmail.com> |
|---|
| 30 | to fit enhanced equirements by the RoundCube Webmail: |
|---|
| 31 | - Added list of server capabilites and check these before invoking commands |
|---|
| 32 | - Added junk flag to iilBasicHeader |
|---|
| 33 | - Enhanced error reporting on fsockopen() |
|---|
| 34 | - Additional parameter for SORT command |
|---|
| 35 | - Removed Call-time pass-by-reference because deprecated |
|---|
| 36 | - Parse charset from content-type in iil_C_FetchHeaders() |
|---|
| 37 | - Enhanced heaer sorting |
|---|
| 38 | - Pass message as reference in iil_C_Append (to save memory) |
|---|
| [f88d417] | 39 | - Added BCC and REFERENCE to the list of headers to fetch in iil_C_FetchHeaders() |
|---|
| 40 | - Leave messageID unchanged in iil_C_FetchHeaders() |
|---|
| 41 | - Avoid stripslahes in iil_Connect() |
|---|
| [0a020ca] | 42 | - Escape quotes and backslashes in iil_C_Login() |
|---|
| [0d361b9] | 43 | - Added patch to iil_SortHeaders() by Richard Green |
|---|
| [8c2e58b] | 44 | - Removed <br> from error messages (better for logging) |
|---|
| [e6f3605] | 45 | - Added patch to iil_C_Sort() enabling UID SORT commands |
|---|
| 46 | - Added function iil_C_ID2UID() |
|---|
| [6eeb17d] | 47 | - Casting date parts in iil_StrToTime() to avoid mktime() warnings |
|---|
| [baf1355] | 48 | - Also acceppt LIST responses in iil_C_ListSubscribed() |
|---|
| [24053e0] | 49 | - Sanity check of $message_set in iil_C_FetchHeaders(), iil_C_FetchHeaderIndex(), iil_C_FetchThreadHeaders() |
|---|
| [f7bfec9] | 50 | - Implemented UID FETCH in iil_C_FetchHeaders() |
|---|
| [9e0bb6b] | 51 | - Abort do-loop on socket errors (fgets returns false) |
|---|
| [5bc0ab1] | 52 | - $ICL_SSL is not boolean anymore but contains the connection schema (ssl or tls) |
|---|
| [0284c23] | 53 | - Removed some debuggers (echo ...) |
|---|
| [ca4c087] | 54 | File altered by Aleksander Machniak <alec@alec.pl> |
|---|
| [9b90b33] | 55 | - trim(chop()) replaced by trim() |
|---|
| [b7c1685] | 56 | - added iil_Escape()/iil_UnEscape() with support for " and \ in folder names |
|---|
| [d1e8e3f] | 57 | - support \ character in username in iil_C_Login() |
|---|
| [02548b9] | 58 | - fixed iil_MultLine(): use iil_ReadBytes() instead of iil_ReadLine() |
|---|
| 59 | - fixed iil_C_FetchStructureString() to handle many literal strings in response |
|---|
| 60 | - removed hardcoded data size in iil_ReadLine() |
|---|
| [49e5f72] | 61 | - added iil_PutLine() wrapper for fputs() |
|---|
| 62 | - code cleanup and identation fixes |
|---|
| [d5c5399] | 63 | - removed flush() calls in iil_C_HandlePartBody() to prevent from memory leak (#1485187) |
|---|
| [876b15d] | 64 | - don't return "??" from iil_C_GetQuota() |
|---|
| [11ef977] | 65 | - RFC3501 [7.1] don't call CAPABILITY if was returned in server |
|---|
| 66 | optional resposne in iil_Connect(), added iil_C_GetCapability() |
|---|
| [109314c] | 67 | - remove 'undisclosed-recipients' string from 'To' header |
|---|
| [81b573d] | 68 | - iil_C_HandlePartBody(): added 6th argument and fixed endless loop |
|---|
| [2b4bae1] | 69 | - added iil_PutLineC() |
|---|
| [7ae46f2] | 70 | - fixed iil_C_Sort() to support very long and/or divided responses |
|---|
| [487edf8] | 71 | - added BYE/BAD response simple support for endless loop prevention |
|---|
| [d2993ee] | 72 | - added 3rd argument in iil_StartsWith* functions |
|---|
| [ceb52fe0] | 73 | - fix iil_C_FetchPartHeader() in some cases by use of iil_C_HandlePartBody() |
|---|
| [322b79d] | 74 | - allow iil_C_HandlePartBody() to fetch whole message |
|---|
| [9b9f267] | 75 | - optimize iil_C_FetchHeaders() to use only one FETCH command |
|---|
| [030c848] | 76 | - added 4th argument to iil_Connect() |
|---|
| 77 | - allow setting rootdir and delimiter before connect |
|---|
| [78928070] | 78 | - support multiquota result |
|---|
| [7a229b9] | 79 | - include BODYSTRUCTURE in iil_C_FetchHeaders() |
|---|
| 80 | - added iil_C_FetchMIMEHeaders() function |
|---|
| [5c771cb] | 81 | - added \* flag support |
|---|
| [2f7405e3] | 82 | - use PREG instead of EREG |
|---|
| 83 | - removed caching functions |
|---|
| [aa320ed] | 84 | - handling connection startup response |
|---|
| [8234b4c] | 85 | - added UID EXPUNGE support |
|---|
| [6bc59a7] | 86 | - fixed problem with double quotes and spaces in folder names in LIST and LSUB |
|---|
| [2dd7ee3] | 87 | - rewritten iil_C_FetchHeaderIndex() |
|---|
| [4e17e6c] | 88 | |
|---|
| 89 | ********************************************************/ |
|---|
| 90 | |
|---|
| [31ecc4f] | 91 | /** |
|---|
| 92 | * @todo Possibly clean up more CS. |
|---|
| [3d695da] | 93 | * @todo Try to replace most double-quotes with single-quotes. |
|---|
| [31ecc4f] | 94 | * @todo Split this file into smaller files. |
|---|
| 95 | * @todo Refactor code. |
|---|
| [06583c3] | 96 | * @todo Replace echo-debugging (make it adhere to config setting and log) |
|---|
| [31ecc4f] | 97 | */ |
|---|
| [0284c23] | 98 | |
|---|
| [4e17e6c] | 99 | |
|---|
| [8150d28] | 100 | if (!isset($IMAP_USE_HEADER_DATE) || !$IMAP_USE_HEADER_DATE) { |
|---|
| 101 | $IMAP_USE_INTERNAL_DATE = true; |
|---|
| [6ccd45a] | 102 | } |
|---|
| 103 | |
|---|
| [5b3dd41] | 104 | $GLOBALS['IMAP_FLAGS'] = array( |
|---|
| 105 | 'SEEN' => '\\Seen', |
|---|
| 106 | 'DELETED' => '\\Deleted', |
|---|
| 107 | 'RECENT' => '\\Recent', |
|---|
| 108 | 'ANSWERED' => '\\Answered', |
|---|
| 109 | 'DRAFT' => '\\Draft', |
|---|
| 110 | 'FLAGGED' => '\\Flagged', |
|---|
| 111 | 'FORWARDED' => '$Forwarded', |
|---|
| [5c771cb] | 112 | 'MDNSENT' => '$MDNSent', |
|---|
| 113 | '*' => '\\*', |
|---|
| 114 | ); |
|---|
| [5b3dd41] | 115 | |
|---|
| [4e17e6c] | 116 | $iil_error; |
|---|
| 117 | $iil_errornum; |
|---|
| 118 | $iil_selected; |
|---|
| 119 | |
|---|
| [6ccd45a] | 120 | /** |
|---|
| 121 | * @todo Change class vars to public/private |
|---|
| 122 | */ |
|---|
| 123 | class iilConnection |
|---|
| 124 | { |
|---|
| [4e17e6c] | 125 | var $fp; |
|---|
| 126 | var $error; |
|---|
| 127 | var $errorNum; |
|---|
| 128 | var $selected; |
|---|
| 129 | var $message; |
|---|
| 130 | var $host; |
|---|
| 131 | var $exists; |
|---|
| 132 | var $recent; |
|---|
| 133 | var $rootdir; |
|---|
| 134 | var $delimiter; |
|---|
| [f3b6599] | 135 | var $capability = array(); |
|---|
| [5b3dd41] | 136 | var $permanentflags = array(); |
|---|
| [11ef977] | 137 | var $capability_readed = false; |
|---|
| [4e17e6c] | 138 | } |
|---|
| 139 | |
|---|
| [6ccd45a] | 140 | /** |
|---|
| 141 | * @todo Change class vars to public/private |
|---|
| 142 | */ |
|---|
| 143 | class iilBasicHeader |
|---|
| 144 | { |
|---|
| [4e17e6c] | 145 | var $id; |
|---|
| 146 | var $uid; |
|---|
| 147 | var $subject; |
|---|
| 148 | var $from; |
|---|
| 149 | var $to; |
|---|
| 150 | var $cc; |
|---|
| 151 | var $replyto; |
|---|
| 152 | var $in_reply_to; |
|---|
| 153 | var $date; |
|---|
| 154 | var $messageID; |
|---|
| 155 | var $size; |
|---|
| 156 | var $encoding; |
|---|
| [ed54070] | 157 | var $charset; |
|---|
| [4e17e6c] | 158 | var $ctype; |
|---|
| 159 | var $flags; |
|---|
| 160 | var $timestamp; |
|---|
| 161 | var $f; |
|---|
| [7a229b9] | 162 | var $body_structure; |
|---|
| [4e17e6c] | 163 | var $internaldate; |
|---|
| [ed54070] | 164 | var $references; |
|---|
| [ff08eed] | 165 | var $priority; |
|---|
| [ed54070] | 166 | var $mdn_to; |
|---|
| 167 | var $mdn_sent = false; |
|---|
| [5b3dd41] | 168 | var $is_draft = false; |
|---|
| [ed54070] | 169 | var $seen = false; |
|---|
| 170 | var $deleted = false; |
|---|
| 171 | var $recent = false; |
|---|
| 172 | var $answered = false; |
|---|
| [4dae735] | 173 | var $forwarded = false; |
|---|
| [ed54070] | 174 | var $junk = false; |
|---|
| [e189a6c] | 175 | var $flagged = false; |
|---|
| [cc97ea0] | 176 | var $others = array(); |
|---|
| [4e17e6c] | 177 | } |
|---|
| 178 | |
|---|
| [6ccd45a] | 179 | /** |
|---|
| 180 | * @todo Change class vars to public/private |
|---|
| 181 | */ |
|---|
| 182 | class iilThreadHeader |
|---|
| 183 | { |
|---|
| [4e17e6c] | 184 | var $id; |
|---|
| 185 | var $sbj; |
|---|
| 186 | var $irt; |
|---|
| 187 | var $mid; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| [6ccd45a] | 190 | function iil_xor($string, $string2) { |
|---|
| [49e5f72] | 191 | $result = ''; |
|---|
| 192 | $size = strlen($string); |
|---|
| 193 | for ($i=0; $i<$size; $i++) { |
|---|
| 194 | $result .= chr(ord($string[$i]) ^ ord($string2[$i])); |
|---|
| 195 | } |
|---|
| 196 | return $result; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | function iil_PutLine($fp, $string, $endln=true) { |
|---|
| [d559cba] | 200 | global $my_prefs; |
|---|
| 201 | |
|---|
| [4bac289] | 202 | if (!empty($my_prefs['debug_mode'])) |
|---|
| [d559cba] | 203 | write_log('imap', 'C: '. rtrim($string)); |
|---|
| 204 | |
|---|
| [2b4bae1] | 205 | return fputs($fp, $string . ($endln ? "\r\n" : '')); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | // iil_PutLine replacement with Command Continuation Requests (RFC3501 7.5) support |
|---|
| 209 | function iil_PutLineC($fp, $string, $endln=true) { |
|---|
| 210 | if ($endln) |
|---|
| 211 | $string .= "\r\n"; |
|---|
| 212 | |
|---|
| 213 | $res = 0; |
|---|
| 214 | if ($parts = preg_split('/(\{[0-9]+\}\r\n)/m', $string, -1, PREG_SPLIT_DELIM_CAPTURE)) { |
|---|
| 215 | for($i=0, $cnt=count($parts); $i<$cnt; $i++) { |
|---|
| 216 | if(preg_match('/^\{[0-9]+\}\r\n$/', $parts[$i+1])) { |
|---|
| 217 | $res += iil_PutLine($fp, $parts[$i].$parts[$i+1], false); |
|---|
| 218 | $line = iil_ReadLine($fp, 1000); |
|---|
| [25f779d] | 219 | // handle error in command |
|---|
| 220 | if ($line[0] != '+') |
|---|
| 221 | return false; |
|---|
| [2b4bae1] | 222 | $i++; |
|---|
| 223 | } |
|---|
| 224 | else |
|---|
| 225 | $res += iil_PutLine($fp, $parts[$i], false); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | return $res; |
|---|
| [4e17e6c] | 229 | } |
|---|
| 230 | |
|---|
| [aa320ed] | 231 | function iil_ReadLine($fp, $size=1024) { |
|---|
| [d559cba] | 232 | global $my_prefs; |
|---|
| 233 | |
|---|
| [49e5f72] | 234 | $line = ''; |
|---|
| 235 | |
|---|
| 236 | if (!$fp) { |
|---|
| 237 | return $line; |
|---|
| 238 | } |
|---|
| [02548b9] | 239 | |
|---|
| [49e5f72] | 240 | if (!$size) { |
|---|
| 241 | $size = 1024; |
|---|
| 242 | } |
|---|
| [02548b9] | 243 | |
|---|
| [49e5f72] | 244 | do { |
|---|
| 245 | $buffer = fgets($fp, $size); |
|---|
| 246 | if ($buffer === false) { |
|---|
| 247 | break; |
|---|
| 248 | } |
|---|
| [4bac289] | 249 | if (!empty($my_prefs['debug_mode'])) |
|---|
| [d559cba] | 250 | write_log('imap', 'S: '. chop($buffer)); |
|---|
| [49e5f72] | 251 | $line .= $buffer; |
|---|
| 252 | } while ($buffer[strlen($buffer)-1] != "\n"); |
|---|
| 253 | |
|---|
| 254 | return $line; |
|---|
| [4e17e6c] | 255 | } |
|---|
| 256 | |
|---|
| [6bc59a7] | 257 | function iil_MultLine($fp, $line, $escape=false) { |
|---|
| [4e17e6c] | 258 | $line = chop($line); |
|---|
| [2f7405e3] | 259 | if (preg_match('/\{[0-9]+\}$/', $line)) { |
|---|
| [6ccd45a] | 260 | $out = ''; |
|---|
| [a527781] | 261 | |
|---|
| [4e17e6c] | 262 | preg_match_all('/(.*)\{([0-9]+)\}$/', $line, $a); |
|---|
| 263 | $bytes = $a[2][0]; |
|---|
| [a527781] | 264 | while (strlen($out) < $bytes) { |
|---|
| [49e5f72] | 265 | $line = iil_ReadBytes($fp, $bytes); |
|---|
| 266 | $out .= $line; |
|---|
| [4e17e6c] | 267 | } |
|---|
| [6bc59a7] | 268 | |
|---|
| 269 | $line = $a[1][0] . '"' . ($escape ? iil_Escape($out) : $out) . '"'; |
|---|
| [4e17e6c] | 270 | } |
|---|
| 271 | return $line; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| [6ccd45a] | 274 | function iil_ReadBytes($fp, $bytes) { |
|---|
| [d559cba] | 275 | global $my_prefs; |
|---|
| [49e5f72] | 276 | $data = ''; |
|---|
| 277 | $len = 0; |
|---|
| 278 | do { |
|---|
| [72cd3c1] | 279 | $d = fread($fp, $bytes-$len); |
|---|
| [4bac289] | 280 | if (!empty($my_prefs['debug_mode'])) |
|---|
| 281 | write_log('imap', 'S: '. $d); |
|---|
| [72cd3c1] | 282 | $data .= $d; |
|---|
| [4bac289] | 283 | $data_len = strlen($data); |
|---|
| 284 | if ($len == $data_len) { |
|---|
| [49e5f72] | 285 | break; //nothing was read -> exit to avoid apache lockups |
|---|
| 286 | } |
|---|
| [4bac289] | 287 | $len = $data_len; |
|---|
| [49e5f72] | 288 | } while ($len < $bytes); |
|---|
| 289 | |
|---|
| 290 | return $data; |
|---|
| [4e17e6c] | 291 | } |
|---|
| 292 | |
|---|
| [6ccd45a] | 293 | function iil_ReadReply($fp) { |
|---|
| 294 | do { |
|---|
| [9b90b33] | 295 | $line = trim(iil_ReadLine($fp, 1024)); |
|---|
| [6ccd45a] | 296 | } while ($line[0] == '*'); |
|---|
| [4e17e6c] | 297 | |
|---|
| 298 | return $line; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| [6ccd45a] | 301 | function iil_ParseResult($string) { |
|---|
| [25f779d] | 302 | $a = explode(' ', $string); |
|---|
| [6ccd45a] | 303 | if (count($a) > 2) { |
|---|
| 304 | if (strcasecmp($a[1], 'OK') == 0) { |
|---|
| [49e5f72] | 305 | return 0; |
|---|
| [6ccd45a] | 306 | } else if (strcasecmp($a[1], 'NO') == 0) { |
|---|
| [49e5f72] | 307 | return -1; |
|---|
| [6ccd45a] | 308 | } else if (strcasecmp($a[1], 'BAD') == 0) { |
|---|
| [49e5f72] | 309 | return -2; |
|---|
| [d2993ee] | 310 | } else if (strcasecmp($a[1], 'BYE') == 0) { |
|---|
| 311 | return -3; |
|---|
| [49e5f72] | 312 | } |
|---|
| [6ccd45a] | 313 | } |
|---|
| [d2993ee] | 314 | return -4; |
|---|
| [4e17e6c] | 315 | } |
|---|
| 316 | |
|---|
| [487edf8] | 317 | // check if $string starts with $match (or * BYE/BAD) |
|---|
| 318 | function iil_StartsWith($string, $match, $error=false) { |
|---|
| [4e17e6c] | 319 | $len = strlen($match); |
|---|
| [6ccd45a] | 320 | if ($len == 0) { |
|---|
| [49e5f72] | 321 | return false; |
|---|
| 322 | } |
|---|
| [6ccd45a] | 323 | if (strncmp($string, $match, $len) == 0) { |
|---|
| [49e5f72] | 324 | return true; |
|---|
| 325 | } |
|---|
| [2f7405e3] | 326 | if ($error && preg_match('/^\* (BYE|BAD) /i', $string)) { |
|---|
| [d2993ee] | 327 | return true; |
|---|
| 328 | } |
|---|
| [6ccd45a] | 329 | return false; |
|---|
| [4e17e6c] | 330 | } |
|---|
| 331 | |
|---|
| [4bac289] | 332 | function iil_StartsWithI($string, $match, $error=false) { |
|---|
| [4e17e6c] | 333 | $len = strlen($match); |
|---|
| [6ccd45a] | 334 | if ($len == 0) { |
|---|
| [49e5f72] | 335 | return false; |
|---|
| 336 | } |
|---|
| [6ccd45a] | 337 | if (strncasecmp($string, $match, $len) == 0) { |
|---|
| [49e5f72] | 338 | return true; |
|---|
| 339 | } |
|---|
| [4bac289] | 340 | if ($error && preg_match('/^\* (BYE|BAD) /i', $string)) { |
|---|
| [d2993ee] | 341 | return true; |
|---|
| [f86e8f5f] | 342 | |
|---|
| [d2993ee] | 343 | } |
|---|
| [6ccd45a] | 344 | return false; |
|---|
| [4e17e6c] | 345 | } |
|---|
| 346 | |
|---|
| [e16938e] | 347 | function iil_Escape($string) |
|---|
| 348 | { |
|---|
| [49e5f72] | 349 | return strtr($string, array('"'=>'\\"', '\\' => '\\\\')); |
|---|
| [e16938e] | 350 | } |
|---|
| 351 | |
|---|
| [b7c1685] | 352 | function iil_UnEscape($string) |
|---|
| 353 | { |
|---|
| 354 | return strtr($string, array('\\"'=>'"', '\\\\' => '\\')); |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| [11ef977] | 357 | function iil_C_GetCapability(&$conn, $name) |
|---|
| 358 | { |
|---|
| 359 | if (in_array($name, $conn->capability)) { |
|---|
| [76265ef] | 360 | return true; |
|---|
| [11ef977] | 361 | } |
|---|
| 362 | else if ($conn->capability_readed) { |
|---|
| 363 | return false; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | // get capabilities (only once) because initial |
|---|
| 367 | // optional CAPABILITY response may differ |
|---|
| 368 | $conn->capability = array(); |
|---|
| 369 | |
|---|
| 370 | iil_PutLine($conn->fp, "cp01 CAPABILITY"); |
|---|
| 371 | do { |
|---|
| 372 | $line = trim(iil_ReadLine($conn->fp, 1024)); |
|---|
| 373 | $a = explode(' ', $line); |
|---|
| 374 | if ($line[0] == '*') { |
|---|
| 375 | while (list($k, $w) = each($a)) { |
|---|
| 376 | if ($w != '*' && $w != 'CAPABILITY') |
|---|
| 377 | $conn->capability[] = strtoupper($w); |
|---|
| 378 | } |
|---|
| 379 | } |
|---|
| 380 | } while ($a[0] != 'cp01'); |
|---|
| 381 | |
|---|
| 382 | $conn->capability_readed = true; |
|---|
| 383 | |
|---|
| 384 | if (in_array($name, $conn->capability)) { |
|---|
| [76265ef] | 385 | return true; |
|---|
| [11ef977] | 386 | } |
|---|
| 387 | |
|---|
| 388 | return false; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| [f86e8f5f] | 391 | function iil_C_ClearCapability(&$conn) |
|---|
| 392 | { |
|---|
| 393 | $conn->capability = array(); |
|---|
| 394 | $conn->capability_readed = false; |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| [6ccd45a] | 397 | function iil_C_Authenticate(&$conn, $user, $pass, $encChallenge) { |
|---|
| 398 | |
|---|
| 399 | $ipad = ''; |
|---|
| 400 | $opad = ''; |
|---|
| [4e17e6c] | 401 | |
|---|
| 402 | // initialize ipad, opad |
|---|
| [6ccd45a] | 403 | for ($i=0;$i<64;$i++) { |
|---|
| [3d695da] | 404 | $ipad .= chr(0x36); |
|---|
| 405 | $opad .= chr(0x5C); |
|---|
| [4e17e6c] | 406 | } |
|---|
| [49e5f72] | 407 | |
|---|
| [4e17e6c] | 408 | // pad $pass so it's 64 bytes |
|---|
| 409 | $padLen = 64 - strlen($pass); |
|---|
| [6ccd45a] | 410 | for ($i=0;$i<$padLen;$i++) { |
|---|
| 411 | $pass .= chr(0); |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| [4e17e6c] | 414 | // generate hash |
|---|
| [1fb78c57] | 415 | $hash = md5(iil_xor($pass,$opad) . pack("H*", md5(iil_xor($pass, $ipad) . base64_decode($encChallenge)))); |
|---|
| [6ccd45a] | 416 | |
|---|
| [4e17e6c] | 417 | // generate reply |
|---|
| [1fb78c57] | 418 | $reply = base64_encode($user . ' ' . $hash); |
|---|
| [4e17e6c] | 419 | |
|---|
| 420 | // send result, get reply |
|---|
| [49e5f72] | 421 | iil_PutLine($conn->fp, $reply); |
|---|
| [4e17e6c] | 422 | $line = iil_ReadLine($conn->fp, 1024); |
|---|
| 423 | |
|---|
| 424 | // process result |
|---|
| [d2993ee] | 425 | $result = iil_ParseResult($line); |
|---|
| 426 | if ($result == 0) { |
|---|
| [6ccd45a] | 427 | $conn->error .= ''; |
|---|
| 428 | $conn->errorNum = 0; |
|---|
| [4e17e6c] | 429 | return $conn->fp; |
|---|
| 430 | } |
|---|
| [d2993ee] | 431 | |
|---|
| 432 | if ($result == -3) fclose($conn->fp); // BYE response |
|---|
| 433 | |
|---|
| [39508cb] | 434 | $conn->error .= 'Authentication for ' . $user . ' failed (AUTH): "'; |
|---|
| [6ccd45a] | 435 | $conn->error .= htmlspecialchars($line) . '"'; |
|---|
| [d2993ee] | 436 | $conn->errorNum = $result; |
|---|
| 437 | |
|---|
| 438 | return $result; |
|---|
| [4e17e6c] | 439 | } |
|---|
| 440 | |
|---|
| [6ccd45a] | 441 | function iil_C_Login(&$conn, $user, $password) { |
|---|
| [4e17e6c] | 442 | |
|---|
| [49e5f72] | 443 | iil_PutLine($conn->fp, 'a001 LOGIN "'.iil_Escape($user).'" "'.iil_Escape($password).'"'); |
|---|
| [0a020ca] | 444 | |
|---|
| [6ccd45a] | 445 | do { |
|---|
| 446 | $line = iil_ReadReply($conn->fp); |
|---|
| 447 | if ($line === false) { |
|---|
| 448 | break; |
|---|
| 449 | } |
|---|
| [d2993ee] | 450 | } while (!iil_StartsWith($line, 'a001 ', true)); |
|---|
| 451 | |
|---|
| 452 | // process result |
|---|
| 453 | $result = iil_ParseResult($line); |
|---|
| 454 | |
|---|
| 455 | if ($result == 0) { |
|---|
| [6ccd45a] | 456 | $conn->error .= ''; |
|---|
| 457 | $conn->errorNum = 0; |
|---|
| [d2993ee] | 458 | return $conn->fp; |
|---|
| [4e17e6c] | 459 | } |
|---|
| [d2993ee] | 460 | |
|---|
| [6ccd45a] | 461 | fclose($conn->fp); |
|---|
| 462 | |
|---|
| 463 | $conn->error .= 'Authentication for ' . $user . ' failed (LOGIN): "'; |
|---|
| 464 | $conn->error .= htmlspecialchars($line)."\""; |
|---|
| [d2993ee] | 465 | $conn->errorNum = $result; |
|---|
| [6ccd45a] | 466 | |
|---|
| [4e17e6c] | 467 | return $result; |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| [6ccd45a] | 470 | function iil_ParseNamespace2($str, &$i, $len=0, $l) { |
|---|
| 471 | if (!$l) { |
|---|
| 472 | $str = str_replace('NIL', '()', $str); |
|---|
| [49e5f72] | 473 | } |
|---|
| [6ccd45a] | 474 | if (!$len) { |
|---|
| 475 | $len = strlen($str); |
|---|
| [49e5f72] | 476 | } |
|---|
| [6ccd45a] | 477 | $data = array(); |
|---|
| [4e17e6c] | 478 | $in_quotes = false; |
|---|
| [6ccd45a] | 479 | $elem = 0; |
|---|
| 480 | for ($i;$i<$len;$i++) { |
|---|
| [4e17e6c] | 481 | $c = (string)$str[$i]; |
|---|
| [6ccd45a] | 482 | if ($c == '(' && !$in_quotes) { |
|---|
| [4e17e6c] | 483 | $i++; |
|---|
| 484 | $data[$elem] = iil_ParseNamespace2($str, $i, $len, $l++); |
|---|
| 485 | $elem++; |
|---|
| [6ccd45a] | 486 | } else if ($c == ')' && !$in_quotes) { |
|---|
| [49e5f72] | 487 | return $data; |
|---|
| 488 | } else if ($c == '\\') { |
|---|
| [4e17e6c] | 489 | $i++; |
|---|
| [6ccd45a] | 490 | if ($in_quotes) { |
|---|
| [49e5f72] | 491 | $data[$elem] .= $c.$str[$i]; |
|---|
| 492 | } |
|---|
| [6ccd45a] | 493 | } else if ($c == '"') { |
|---|
| [4e17e6c] | 494 | $in_quotes = !$in_quotes; |
|---|
| [6ccd45a] | 495 | if (!$in_quotes) { |
|---|
| [49e5f72] | 496 | $elem++; |
|---|
| 497 | } |
|---|
| [6ccd45a] | 498 | } else if ($in_quotes) { |
|---|
| [4e17e6c] | 499 | $data[$elem].=$c; |
|---|
| 500 | } |
|---|
| 501 | } |
|---|
| 502 | return $data; |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| [6ccd45a] | 505 | function iil_C_NameSpace(&$conn) { |
|---|
| [4e17e6c] | 506 | global $my_prefs; |
|---|
| [030c848] | 507 | |
|---|
| 508 | if (isset($my_prefs['rootdir']) && is_string($my_prefs['rootdir'])) { |
|---|
| 509 | $conn->rootdir = $my_prefs['rootdir']; |
|---|
| 510 | return true; |
|---|
| 511 | } |
|---|
| [4e17e6c] | 512 | |
|---|
| [11ef977] | 513 | if (!iil_C_GetCapability($conn, 'NAMESPACE')) { |
|---|
| [6ccd45a] | 514 | return false; |
|---|
| 515 | } |
|---|
| 516 | |
|---|
| [49e5f72] | 517 | iil_PutLine($conn->fp, "ns1 NAMESPACE"); |
|---|
| [6ccd45a] | 518 | do { |
|---|
| [4e17e6c] | 519 | $line = iil_ReadLine($conn->fp, 1024); |
|---|
| [6ccd45a] | 520 | if (iil_StartsWith($line, '* NAMESPACE')) { |
|---|
| 521 | $i = 0; |
|---|
| [b7c1685] | 522 | $line = iil_UnEscape($line); |
|---|
| [4e17e6c] | 523 | $data = iil_ParseNamespace2(substr($line,11), $i, 0, 0); |
|---|
| 524 | } |
|---|
| [d2993ee] | 525 | } while (!iil_StartsWith($line, 'ns1', true)); |
|---|
| [4e17e6c] | 526 | |
|---|
| [6ccd45a] | 527 | if (!is_array($data)) { |
|---|
| 528 | return false; |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| [4e17e6c] | 531 | $user_space_data = $data[0]; |
|---|
| [6ccd45a] | 532 | if (!is_array($user_space_data)) { |
|---|
| 533 | return false; |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| [4e17e6c] | 536 | $first_userspace = $user_space_data[0]; |
|---|
| [6ccd45a] | 537 | if (count($first_userspace)!=2) { |
|---|
| 538 | return false; |
|---|
| 539 | } |
|---|
| 540 | |
|---|
| 541 | $conn->rootdir = $first_userspace[0]; |
|---|
| 542 | $conn->delimiter = $first_userspace[1]; |
|---|
| [030c848] | 543 | $my_prefs['rootdir'] = substr($conn->rootdir, 0, -1); |
|---|
| 544 | $my_prefs['delimiter'] = $conn->delimiter; |
|---|
| [4e17e6c] | 545 | |
|---|
| 546 | return true; |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| [030c848] | 549 | function iil_Connect($host, $user, $password, $options=null) { |
|---|
| [49e5f72] | 550 | global $iil_error, $iil_errornum; |
|---|
| [4e17e6c] | 551 | global $ICL_SSL, $ICL_PORT; |
|---|
| 552 | global $my_prefs, $IMAP_USE_INTERNAL_DATE; |
|---|
| 553 | |
|---|
| [6ccd45a] | 554 | $iil_error = ''; |
|---|
| [4e17e6c] | 555 | $iil_errornum = 0; |
|---|
| [030c848] | 556 | |
|---|
| 557 | // set some imap options |
|---|
| 558 | if (is_array($options)) { |
|---|
| 559 | foreach($options as $optkey => $optval) { |
|---|
| 560 | if ($optkey == 'imap') { |
|---|
| 561 | $auth_method = $optval; |
|---|
| 562 | } else if ($optkey == 'rootdir') { |
|---|
| 563 | $my_prefs['rootdir'] = $optval; |
|---|
| 564 | } else if ($optkey == 'delimiter') { |
|---|
| 565 | $my_prefs['delimiter'] = $optval; |
|---|
| [d559cba] | 566 | } else if ($optkey == 'debug_mode') { |
|---|
| 567 | $my_prefs['debug_mode'] = $optval; |
|---|
| [030c848] | 568 | } |
|---|
| 569 | } |
|---|
| [4e17e6c] | 570 | } |
|---|
| [030c848] | 571 | |
|---|
| 572 | if (empty($auth_method)) |
|---|
| [46b48e6d] | 573 | $auth_method = 'check'; |
|---|
| [030c848] | 574 | |
|---|
| [4e17e6c] | 575 | $message = "INITIAL: $auth_method\n"; |
|---|
| 576 | |
|---|
| 577 | $result = false; |
|---|
| 578 | |
|---|
| [f86e8f5f] | 579 | // initialize connection |
|---|
| [6ccd45a] | 580 | $conn = new iilConnection; |
|---|
| 581 | $conn->error = ''; |
|---|
| 582 | $conn->errorNum = 0; |
|---|
| 583 | $conn->selected = ''; |
|---|
| 584 | $conn->user = $user; |
|---|
| 585 | $conn->host = $host; |
|---|
| [4e17e6c] | 586 | |
|---|
| [6ccd45a] | 587 | if ($my_prefs['sort_field'] == 'INTERNALDATE') { |
|---|
| [49e5f72] | 588 | $IMAP_USE_INTERNAL_DATE = true; |
|---|
| 589 | } else if ($my_prefs['sort_field'] == 'DATE') { |
|---|
| 590 | $IMAP_USE_INTERNAL_DATE = false; |
|---|
| 591 | } |
|---|
| [4e17e6c] | 592 | //echo '<!-- conn sort_field: '.$my_prefs['sort_field'].' //-->'; |
|---|
| 593 | |
|---|
| 594 | //check input |
|---|
| [6ccd45a] | 595 | if (empty($host)) { |
|---|
| [d2993ee] | 596 | $iil_error = "Empty host"; |
|---|
| 597 | $iil_errornum = -1; |
|---|
| 598 | return false; |
|---|
| [49e5f72] | 599 | } |
|---|
| [6ccd45a] | 600 | if (empty($user)) { |
|---|
| [d2993ee] | 601 | $iil_error = "Empty user"; |
|---|
| 602 | $iil_errornum = -1; |
|---|
| 603 | return false; |
|---|
| [49e5f72] | 604 | } |
|---|
| [6ccd45a] | 605 | if (empty($password)) { |
|---|
| [d2993ee] | 606 | $iil_error = "Empty password"; |
|---|
| 607 | $iil_errornum = -1; |
|---|
| [49e5f72] | 608 | return false; |
|---|
| 609 | } |
|---|
| [f86e8f5f] | 610 | |
|---|
| [6ccd45a] | 611 | if (!$ICL_PORT) { |
|---|
| [49e5f72] | 612 | $ICL_PORT = 143; |
|---|
| [6ccd45a] | 613 | } |
|---|
| [4e17e6c] | 614 | //check for SSL |
|---|
| [f86e8f5f] | 615 | if ($ICL_SSL && $ICL_SSL != 'tls') { |
|---|
| [3d695da] | 616 | $host = $ICL_SSL . '://' . $host; |
|---|
| [4e17e6c] | 617 | } |
|---|
| [f86e8f5f] | 618 | |
|---|
| [6ccd45a] | 619 | $conn->fp = fsockopen($host, $ICL_PORT, $errno, $errstr, 10); |
|---|
| 620 | if (!$conn->fp) { |
|---|
| [49e5f72] | 621 | $iil_error = "Could not connect to $host at port $ICL_PORT: $errstr"; |
|---|
| [7342d7ef] | 622 | $iil_errornum = -2; |
|---|
| [4e17e6c] | 623 | return false; |
|---|
| 624 | } |
|---|
| 625 | |
|---|
| [aa320ed] | 626 | stream_set_timeout($conn->fp, 10); |
|---|
| 627 | $line = stream_get_line($conn->fp, 8192, "\r\n"); |
|---|
| [f3b6599] | 628 | |
|---|
| [aa320ed] | 629 | // Connected to wrong port or connection error? |
|---|
| 630 | if (!preg_match('/^\* (OK|PREAUTH)/i', $line)) { |
|---|
| 631 | if ($line) |
|---|
| 632 | $iil_error = "Wrong startup greeting ($host:$ICL_PORT): $line"; |
|---|
| 633 | else |
|---|
| 634 | $iil_error = "Empty startup greeting ($host:$ICL_PORT)"; |
|---|
| 635 | $iil_errornum = -2; |
|---|
| 636 | return false; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| [ca4c087] | 639 | // RFC3501 [7.1] optional CAPABILITY response |
|---|
| [11ef977] | 640 | if (preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) { |
|---|
| 641 | $conn->capability = explode(' ', strtoupper($matches[1])); |
|---|
| 642 | } |
|---|
| [4e17e6c] | 643 | |
|---|
| [76265ef] | 644 | $conn->message .= $line; |
|---|
| 645 | |
|---|
| [f86e8f5f] | 646 | // TLS connection |
|---|
| 647 | if ($ICL_SSL == 'tls' && iil_C_GetCapability($conn, 'STARTTLS')) { |
|---|
| 648 | if (version_compare(PHP_VERSION, '5.1.0', '>=')) { |
|---|
| 649 | iil_PutLine($conn->fp, 'stls000 STARTTLS'); |
|---|
| 650 | |
|---|
| 651 | $line = iil_ReadLine($conn->fp, 4096); |
|---|
| 652 | if (!iil_StartsWith($line, 'stls000 OK')) { |
|---|
| 653 | $iil_error = "Server responded to STARTTLS with: $line"; |
|---|
| 654 | $iil_errornum = -2; |
|---|
| 655 | return false; |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | if (!stream_socket_enable_crypto($conn->fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { |
|---|
| 659 | $iil_error = "Unable to negotiate TLS"; |
|---|
| 660 | $iil_errornum = -2; |
|---|
| 661 | return false; |
|---|
| 662 | } |
|---|
| 663 | |
|---|
| 664 | // Now we're authenticated, capabilities need to be reread |
|---|
| 665 | iil_C_ClearCapability($conn); |
|---|
| 666 | } |
|---|
| 667 | } |
|---|
| 668 | |
|---|
| [ca4c087] | 669 | if (strcasecmp($auth_method, "check") == 0) { |
|---|
| 670 | //check for supported auth methods |
|---|
| [11ef977] | 671 | if (iil_C_GetCapability($conn, 'AUTH=CRAM-MD5') || iil_C_GetCapability($conn, 'AUTH=CRAM_MD5')) { |
|---|
| 672 | $auth_method = 'auth'; |
|---|
| 673 | } |
|---|
| 674 | else { |
|---|
| 675 | //default to plain text auth |
|---|
| 676 | $auth_method = 'plain'; |
|---|
| 677 | } |
|---|
| [ca4c087] | 678 | } |
|---|
| 679 | |
|---|
| [3d695da] | 680 | if (strcasecmp($auth_method, 'auth') == 0) { |
|---|
| [6ccd45a] | 681 | $conn->message .= "Trying CRAM-MD5\n"; |
|---|
| [3d695da] | 682 | |
|---|
| [4e17e6c] | 683 | //do CRAM-MD5 authentication |
|---|
| [49e5f72] | 684 | iil_PutLine($conn->fp, "a000 AUTHENTICATE CRAM-MD5"); |
|---|
| [9b90b33] | 685 | $line = trim(iil_ReadLine($conn->fp, 1024)); |
|---|
| [1fb78c57] | 686 | |
|---|
| [3d695da] | 687 | $conn->message .= "$line\n"; |
|---|
| [1fb78c57] | 688 | |
|---|
| [3d695da] | 689 | if ($line[0] == '+') { |
|---|
| 690 | $conn->message .= 'Got challenge: ' . htmlspecialchars($line) . "\n"; |
|---|
| [39508cb] | 691 | |
|---|
| [4e17e6c] | 692 | //got a challenge string, try CRAM-5 |
|---|
| 693 | $result = iil_C_Authenticate($conn, $user, $password, substr($line,2)); |
|---|
| [d2993ee] | 694 | |
|---|
| 695 | // stop if server sent BYE response |
|---|
| 696 | if($result == -3) { |
|---|
| 697 | $iil_error = $conn->error; |
|---|
| 698 | $iil_errornum = $conn->errorNum; |
|---|
| 699 | return false; |
|---|
| 700 | } |
|---|
| [39508cb] | 701 | $conn->message .= "Tried CRAM-MD5: $result \n"; |
|---|
| [6ccd45a] | 702 | } else { |
|---|
| [39508cb] | 703 | $conn->message .='No challenge ('.htmlspecialchars($line)."), try plain\n"; |
|---|
| [1fb78c57] | 704 | $auth = 'plain'; |
|---|
| [4e17e6c] | 705 | } |
|---|
| 706 | } |
|---|
| 707 | |
|---|
| [6ccd45a] | 708 | if ((!$result)||(strcasecmp($auth, "plain") == 0)) { |
|---|
| [4e17e6c] | 709 | //do plain text auth |
|---|
| 710 | $result = iil_C_Login($conn, $user, $password); |
|---|
| [d2993ee] | 711 | $conn->message .= "Tried PLAIN: $result \n"; |
|---|
| [4e17e6c] | 712 | } |
|---|
| 713 | |
|---|
| 714 | $conn->message .= $auth; |
|---|
| 715 | |
|---|
| [d2993ee] | 716 | if (!is_int($result)) { |
|---|
| [4e17e6c] | 717 | iil_C_Namespace($conn); |
|---|
| 718 | return $conn; |
|---|
| [6ccd45a] | 719 | } else { |
|---|
| [4e17e6c] | 720 | $iil_error = $conn->error; |
|---|
| 721 | $iil_errornum = $conn->errorNum; |
|---|
| 722 | return false; |
|---|
| 723 | } |
|---|
| 724 | } |
|---|
| 725 | |
|---|
| [6ccd45a] | 726 | function iil_Close(&$conn) { |
|---|
| [49e5f72] | 727 | if (iil_PutLine($conn->fp, "I LOGOUT")) { |
|---|
| [4e17e6c] | 728 | fgets($conn->fp, 1024); |
|---|
| 729 | fclose($conn->fp); |
|---|
| 730 | $conn->fp = false; |
|---|
| 731 | } |
|---|
| 732 | } |
|---|
| 733 | |
|---|
| [6ccd45a] | 734 | function iil_ExplodeQuotedString($delimiter, $string) { |
|---|
| [6bc59a7] | 735 | $result = array(); |
|---|
| 736 | $strlen = strlen($string); |
|---|
| 737 | |
|---|
| 738 | for ($q=$p=$i=0; $i < $strlen; $i++) { |
|---|
| 739 | if ($string[$i] == "\"" && $string[$i-1] != "\\") { |
|---|
| 740 | $q = $q ? false : true; |
|---|
| 741 | } |
|---|
| 742 | else if (!$q && preg_match("/$delimiter/", $string[$i])) { |
|---|
| 743 | $result[] = substr($string, $p, $i - $p); |
|---|
| 744 | $p = $i + 1; |
|---|
| 745 | } |
|---|
| [06583c3] | 746 | } |
|---|
| [6bc59a7] | 747 | |
|---|
| 748 | $result[] = substr($string, $p); |
|---|
| [4e17e6c] | 749 | return $result; |
|---|
| 750 | } |
|---|
| 751 | |
|---|
| [6ccd45a] | 752 | function iil_CheckForRecent($host, $user, $password, $mailbox) { |
|---|
| 753 | if (empty($mailbox)) { |
|---|
| [49e5f72] | 754 | $mailbox = 'INBOX'; |
|---|
| [6ccd45a] | 755 | } |
|---|
| 756 | |
|---|
| 757 | $conn = iil_Connect($host, $user, $password, 'plain'); |
|---|
| 758 | $fp = $conn->fp; |
|---|
| 759 | if ($fp) { |
|---|
| [49e5f72] | 760 | iil_PutLine($fp, "a002 EXAMINE \"".iil_Escape($mailbox)."\""); |
|---|
| [6ccd45a] | 761 | do { |
|---|
| [4e17e6c] | 762 | $line=chop(iil_ReadLine($fp, 300)); |
|---|
| [6ccd45a] | 763 | $a=explode(' ', $line); |
|---|
| [06583c3] | 764 | if (($a[0] == '*') && (strcasecmp($a[2], 'RECENT') == 0)) { |
|---|
| [6ccd45a] | 765 | $result = (int) $a[1]; |
|---|
| [d2993ee] | 766 | } |
|---|
| 767 | } while (!iil_StartsWith($a[0], 'a002', true)); |
|---|
| [4e17e6c] | 768 | |
|---|
| [49e5f72] | 769 | iil_PutLine($fp, "a003 LOGOUT"); |
|---|
| [4e17e6c] | 770 | fclose($fp); |
|---|
| [6ccd45a] | 771 | } else { |
|---|
| 772 | $result = -2; |
|---|
| 773 | } |
|---|
| 774 | |
|---|
| [4e17e6c] | 775 | return $result; |
|---|
| 776 | } |
|---|
| 777 | |
|---|
| [6ccd45a] | 778 | function iil_C_Select(&$conn, $mailbox) { |
|---|
| [49e5f72] | 779 | |
|---|
| [6ccd45a] | 780 | if (empty($mailbox)) { |
|---|
| [49e5f72] | 781 | return false; |
|---|
| [6ccd45a] | 782 | } |
|---|
| [49e5f72] | 783 | if (strcmp($conn->selected, $mailbox) == 0) { |
|---|
| [5b3dd41] | 784 | return true; |
|---|
| [6ccd45a] | 785 | } |
|---|
| 786 | |
|---|
| [49e5f72] | 787 | if (iil_PutLine($conn->fp, "sel1 SELECT \"".iil_Escape($mailbox).'"')) { |
|---|
| [6ccd45a] | 788 | do { |
|---|
| [49e5f72] | 789 | $line = chop(iil_ReadLine($conn->fp, 300)); |
|---|
| 790 | $a = explode(' ', $line); |
|---|
| [6ccd45a] | 791 | if (count($a) == 3) { |
|---|
| [06583c3] | 792 | if (strcasecmp($a[2], 'EXISTS') == 0) { |
|---|
| [49e5f72] | 793 | $conn->exists = (int) $a[1]; |
|---|
| [06583c3] | 794 | } |
|---|
| [4bac289] | 795 | else if (strcasecmp($a[2], 'RECENT') == 0) { |
|---|
| [5b3dd41] | 796 | $conn->recent = (int) $a[1]; |
|---|
| 797 | } |
|---|
| 798 | } |
|---|
| 799 | else if (preg_match('/\[?PERMANENTFLAGS\s+\(([^\)]+)\)\]/U', $line, $match)) { |
|---|
| 800 | $conn->permanentflags = explode(' ', $match[1]); |
|---|
| [4e17e6c] | 801 | } |
|---|
| [d2993ee] | 802 | } while (!iil_StartsWith($line, 'sel1', true)); |
|---|
| [4e17e6c] | 803 | |
|---|
| [49e5f72] | 804 | $a = explode(' ', $line); |
|---|
| [4e17e6c] | 805 | |
|---|
| [06583c3] | 806 | if (strcasecmp($a[1], 'OK') == 0) { |
|---|
| [4e17e6c] | 807 | $conn->selected = $mailbox; |
|---|
| 808 | return true; |
|---|
| [6ccd45a] | 809 | } |
|---|
| [4e17e6c] | 810 | } |
|---|
| [49e5f72] | 811 | return false; |
|---|
| [4e17e6c] | 812 | } |
|---|
| 813 | |
|---|
| [6ccd45a] | 814 | function iil_C_CheckForRecent(&$conn, $mailbox) { |
|---|
| 815 | if (empty($mailbox)) { |
|---|
| [49e5f72] | 816 | $mailbox = 'INBOX'; |
|---|
| [6ccd45a] | 817 | } |
|---|
| 818 | |
|---|
| [4e17e6c] | 819 | iil_C_Select($conn, $mailbox); |
|---|
| [6ccd45a] | 820 | if ($conn->selected == $mailbox) { |
|---|
| [49e5f72] | 821 | return $conn->recent; |
|---|
| [6ccd45a] | 822 | } |
|---|
| [49e5f72] | 823 | return false; |
|---|
| [4e17e6c] | 824 | } |
|---|
| 825 | |
|---|
| [6ccd45a] | 826 | function iil_C_CountMessages(&$conn, $mailbox, $refresh = false) { |
|---|
| 827 | if ($refresh) { |
|---|
| [0803fb0] | 828 | $conn->selected = ''; |
|---|
| [6ccd45a] | 829 | } |
|---|
| [49e5f72] | 830 | |
|---|
| [4e17e6c] | 831 | iil_C_Select($conn, $mailbox); |
|---|
| [6ccd45a] | 832 | if ($conn->selected == $mailbox) { |
|---|
| 833 | return $conn->exists; |
|---|
| 834 | } |
|---|
| 835 | return false; |
|---|
| [4e17e6c] | 836 | } |
|---|
| 837 | |
|---|
| [6ccd45a] | 838 | function iil_SplitHeaderLine($string) { |
|---|
| 839 | $pos=strpos($string, ':'); |
|---|
| 840 | if ($pos>0) { |
|---|
| 841 | $res[0] = substr($string, 0, $pos); |
|---|
| 842 | $res[1] = trim(substr($string, $pos+1)); |
|---|
| [4e17e6c] | 843 | return $res; |
|---|
| 844 | } |
|---|
| [49e5f72] | 845 | return $string; |
|---|
| [4e17e6c] | 846 | } |
|---|
| 847 | |
|---|
| [11b80e9] | 848 | function iil_StrToTime($date) { |
|---|
| 849 | |
|---|
| 850 | // support non-standard "GMTXXXX" literal |
|---|
| 851 | $date = preg_replace('/GMT\s*([+-][0-9]+)/', '\\1', $date); |
|---|
| 852 | // if date parsing fails, we have a date in non-rfc format. |
|---|
| 853 | // remove token from the end and try again |
|---|
| 854 | while ((($ts = @strtotime($date))===false) || ($ts < 0)) |
|---|
| 855 | { |
|---|
| 856 | $d = explode(' ', $date); |
|---|
| 857 | array_pop($d); |
|---|
| 858 | if (!$d) break; |
|---|
| 859 | $date = implode(' ', $d); |
|---|
| [6ccd45a] | 860 | } |
|---|
| [11b80e9] | 861 | |
|---|
| 862 | $ts = (int) $ts; |
|---|
| 863 | |
|---|
| 864 | return $ts < 0 ? 0 : $ts; |
|---|
| [4e17e6c] | 865 | } |
|---|
| 866 | |
|---|
| [6ccd45a] | 867 | function iil_C_Sort(&$conn, $mailbox, $field, $add='', $is_uid=FALSE, |
|---|
| [06583c3] | 868 | $encoding = 'US-ASCII') { |
|---|
| [e2e745f] | 869 | |
|---|
| [4e17e6c] | 870 | $field = strtoupper($field); |
|---|
| [6ccd45a] | 871 | if ($field == 'INTERNALDATE') { |
|---|
| 872 | $field = 'ARRIVAL'; |
|---|
| [49e5f72] | 873 | } |
|---|
| 874 | |
|---|
| [3d695da] | 875 | $fields = array('ARRIVAL' => 1,'CC' => 1,'DATE' => 1, |
|---|
| 876 | 'FROM' => 1, 'SIZE' => 1, 'SUBJECT' => 1, 'TO' => 1); |
|---|
| [e6f3605] | 877 | |
|---|
| [6ccd45a] | 878 | if (!$fields[$field]) { |
|---|
| 879 | return false; |
|---|
| 880 | } |
|---|
| [e2e745f] | 881 | |
|---|
| 882 | /* Do "SELECT" command */ |
|---|
| 883 | if (!iil_C_Select($conn, $mailbox)) { |
|---|
| 884 | return false; |
|---|
| 885 | } |
|---|
| [6ccd45a] | 886 | |
|---|
| [e6f3605] | 887 | $is_uid = $is_uid ? 'UID ' : ''; |
|---|
| [4e17e6c] | 888 | |
|---|
| [6ccd45a] | 889 | if (!empty($add)) { |
|---|
| 890 | $add = " $add"; |
|---|
| [49e5f72] | 891 | } |
|---|
| [e6f3605] | 892 | |
|---|
| [3d695da] | 893 | $command = 's ' . $is_uid . 'SORT (' . $field . ') '; |
|---|
| [e2e745f] | 894 | $command .= $encoding . ' ALL' . $add; |
|---|
| [3d695da] | 895 | $line = $data = ''; |
|---|
| [4bac289] | 896 | |
|---|
| [e2e745f] | 897 | if (!iil_PutLineC($conn->fp, $command)) { |
|---|
| [6ccd45a] | 898 | return false; |
|---|
| [e16938e] | 899 | } |
|---|
| [6ccd45a] | 900 | do { |
|---|
| [99a4539] | 901 | $line = chop(iil_ReadLine($conn->fp)); |
|---|
| [06583c3] | 902 | if (iil_StartsWith($line, '* SORT')) { |
|---|
| [99a4539] | 903 | $data .= substr($line, 7); |
|---|
| [7ae46f2] | 904 | } else if (preg_match('/^[0-9 ]+$/', $line)) { |
|---|
| 905 | $data .= $line; |
|---|
| 906 | } |
|---|
| [d2993ee] | 907 | } while (!iil_StartsWith($line, 's ', true)); |
|---|
| [4e17e6c] | 908 | |
|---|
| [697cc52] | 909 | $result_code = iil_ParseResult($line); |
|---|
| 910 | |
|---|
| 911 | if ($result_code != 0) { |
|---|
| 912 | $conn->error = 'iil_C_Sort: ' . $line . "\n"; |
|---|
| 913 | return false; |
|---|
| [4e17e6c] | 914 | } |
|---|
| 915 | |
|---|
| [99a4539] | 916 | return preg_split('/\s+/', $data, -1, PREG_SPLIT_NO_EMPTY); |
|---|
| [4e17e6c] | 917 | } |
|---|
| 918 | |
|---|
| [2dd7ee3] | 919 | function iil_C_FetchHeaderIndex(&$conn, $mailbox, $message_set, $index_field='', $skip_deleted=true) { |
|---|
| 920 | |
|---|
| [24053e0] | 921 | list($from_idx, $to_idx) = explode(':', $message_set); |
|---|
| [2dd7ee3] | 922 | if (empty($message_set) || |
|---|
| 923 | (isset($to_idx) && $to_idx != '*' && (int)$from_idx > (int)$to_idx)) { |
|---|
| [24053e0] | 924 | return false; |
|---|
| [49e5f72] | 925 | } |
|---|
| [2dd7ee3] | 926 | |
|---|
| 927 | $index_field = empty($index_field) ? 'DATE' : strtoupper($index_field); |
|---|
| [4e17e6c] | 928 | |
|---|
| [6ccd45a] | 929 | $fields_a['DATE'] = 1; |
|---|
| [2dd7ee3] | 930 | $fields_a['INTERNALDATE'] = 4; |
|---|
| [6ccd45a] | 931 | $fields_a['FROM'] = 1; |
|---|
| 932 | $fields_a['REPLY-TO'] = 1; |
|---|
| 933 | $fields_a['SENDER'] = 1; |
|---|
| 934 | $fields_a['TO'] = 1; |
|---|
| 935 | $fields_a['SUBJECT'] = 1; |
|---|
| 936 | $fields_a['UID'] = 2; |
|---|
| 937 | $fields_a['SIZE'] = 2; |
|---|
| 938 | $fields_a['SEEN'] = 3; |
|---|
| [2dd7ee3] | 939 | $fields_a['RECENT'] = 3; |
|---|
| 940 | $fields_a['DELETED'] = 3; |
|---|
| 941 | |
|---|
| 942 | if (!($mode = $fields_a[$index_field])) { |
|---|
| 943 | return false; |
|---|
| [6ccd45a] | 944 | } |
|---|
| [2dd7ee3] | 945 | |
|---|
| [4e17e6c] | 946 | /* Do "SELECT" command */ |
|---|
| [6ccd45a] | 947 | if (!iil_C_Select($conn, $mailbox)) { |
|---|
| [2dd7ee3] | 948 | return false; |
|---|
| [49e5f72] | 949 | } |
|---|
| [2dd7ee3] | 950 | |
|---|
| 951 | // build FETCH command string |
|---|
| 952 | $key = 'fhi0'; |
|---|
| 953 | $deleted = $skip_deleted ? ' FLAGS' : ''; |
|---|
| 954 | |
|---|
| 955 | if ($mode == 1) |
|---|
| 956 | $request = " FETCH $message_set (BODY.PEEK[HEADER.FIELDS ($index_field)]$deleted)"; |
|---|
| 957 | else if ($mode == 2) { |
|---|
| 958 | if ($index_field == 'SIZE') |
|---|
| 959 | $request = " FETCH $message_set (RFC822.SIZE$deleted)"; |
|---|
| 960 | else |
|---|
| 961 | $request = " FETCH $message_set ($index_field$deleted)"; |
|---|
| 962 | } else if ($mode == 3) |
|---|
| 963 | $request = " FETCH $message_set (FLAGS)"; |
|---|
| 964 | else // 4 |
|---|
| 965 | $request = " FETCH $message_set (INTERNALDATE$deleted)"; |
|---|
| [d5ff9276] | 966 | |
|---|
| [2dd7ee3] | 967 | $request = $key . $request; |
|---|
| [d5ff9276] | 968 | |
|---|
| [2dd7ee3] | 969 | if (!iil_PutLine($conn->fp, $request)) |
|---|
| 970 | return false; |
|---|
| [d5ff9276] | 971 | |
|---|
| [2dd7ee3] | 972 | $result = array(); |
|---|
| 973 | |
|---|
| 974 | do { |
|---|
| 975 | $line = chop(iil_ReadLine($conn->fp, 200)); |
|---|
| 976 | $line = iil_MultLine($conn->fp, $line); |
|---|
| 977 | |
|---|
| 978 | if (preg_match('/^\* ([0-9]+) FETCH/', $line, $m)) { |
|---|
| 979 | |
|---|
| 980 | $id = $m[1]; |
|---|
| 981 | $flags = NULL; |
|---|
| 982 | |
|---|
| 983 | if ($skip_deleted && preg_match('/FLAGS \(([^)]+)\)/', $line, $matches)) { |
|---|
| 984 | $flags = explode(' ', strtoupper($matches[1])); |
|---|
| 985 | if (in_array('\\DELETED', $flags)) { |
|---|
| 986 | $deleted[$id] = $id; |
|---|
| 987 | continue; |
|---|
| [4e17e6c] | 988 | } |
|---|
| 989 | } |
|---|
| 990 | |
|---|
| [2dd7ee3] | 991 | if ($mode == 1) { |
|---|
| 992 | if (preg_match('/BODY\[HEADER\.FIELDS \((DATE|FROM|REPLY-TO|SENDER|TO|SUBJECT)\)\] (.*)/', $line, $matches)) { |
|---|
| 993 | $value = preg_replace(array('/^"*[a-z]+:/i', '/\s+$/sm'), array('', ''), $matches[2]); |
|---|
| 994 | $value = trim($value); |
|---|
| 995 | if ($index_field == 'DATE') { |
|---|
| 996 | $result[$id] = iil_StrToTime($value); |
|---|
| 997 | } else { |
|---|
| 998 | $result[$id] = $value; |
|---|
| 999 | } |
|---|
| 1000 | } else { |
|---|
| 1001 | $result[$id] = ''; |
|---|
| 1002 | } |
|---|
| 1003 | } else if ($mode == 2) { |
|---|
| 1004 | if (preg_match('/\((UID|RFC822\.SIZE) ([0-9]+)/', $line, $matches)) { |
|---|
| 1005 | $result[$id] = trim($matches[2]); |
|---|
| 1006 | } else { |
|---|
| 1007 | $result[$id] = 0; |
|---|
| [6ccd45a] | 1008 | } |
|---|
| [2dd7ee3] | 1009 | } else if ($mode == 3) { |
|---|
| 1010 | if (!$flags && preg_match('/FLAGS \(([^)]+)\)/', $line, $matches)) { |
|---|
| 1011 | $flags = explode(' ', $matches[1]); |
|---|
| [49e5f72] | 1012 | } |
|---|
| [2dd7ee3] | 1013 | $result[$id] = in_array('\\'.$index_field, $flags) ? 1 : 0; |
|---|
| 1014 | } else if ($mode == 4) { |
|---|
| 1015 | if (preg_match('/INTERNALDATE "([^"]+)"/', $line, $matches)) { |
|---|
| [11b80e9] | 1016 | $result[$id] = iil_StrToTime($matches[1]); |
|---|
| [6ccd45a] | 1017 | } else { |
|---|
| [2dd7ee3] | 1018 | $result[$id] = 0; |
|---|
| [4e17e6c] | 1019 | } |
|---|
| 1020 | } |
|---|
| [2dd7ee3] | 1021 | } |
|---|
| 1022 | } while (!iil_StartsWith($line, $key, true)); |
|---|
| [4e17e6c] | 1023 | |
|---|
| [2dd7ee3] | 1024 | /* |
|---|
| [4e17e6c] | 1025 | //check number of elements... |
|---|
| [2dd7ee3] | 1026 | if (is_numeric($from_idx) && is_numeric($to_idx)) { |
|---|
| [4e17e6c] | 1027 | //count how many we should have |
|---|
| [2dd7ee3] | 1028 | $should_have = $to_idx - $from_idx + 1; |
|---|
| [4e17e6c] | 1029 | |
|---|
| 1030 | //if we have less, try and fill in the "gaps" |
|---|
| [3d695da] | 1031 | if (count($result) < $should_have) { |
|---|
| [2dd7ee3] | 1032 | for ($i=$from_idx; $i<=$to_idx; $i++) { |
|---|
| [49e5f72] | 1033 | if (!isset($result[$i])) { |
|---|
| 1034 | $result[$i] = ''; |
|---|
| 1035 | } |
|---|
| 1036 | } |
|---|
| [4e17e6c] | 1037 | } |
|---|
| 1038 | } |
|---|
| [2dd7ee3] | 1039 | */ |
|---|
| [4e17e6c] | 1040 | return $result; |
|---|
| 1041 | } |
|---|
| 1042 | |
|---|
| [6ccd45a] | 1043 | function iil_CompressMessageSet($message_set) { |
|---|
| [4e17e6c] | 1044 | //given a comma delimited list of independent mid's, |
|---|
| 1045 | //compresses by grouping sequences together |
|---|
| 1046 | |
|---|
| 1047 | //if less than 255 bytes long, let's not bother |
|---|
| [6ccd45a] | 1048 | if (strlen($message_set)<255) { |
|---|
| 1049 | return $message_set; |
|---|
| 1050 | } |
|---|
| 1051 | |
|---|
| [4e17e6c] | 1052 | //see if it's already been compress |
|---|
| [6ccd45a] | 1053 | if (strpos($message_set, ':') !== false) { |
|---|
| 1054 | return $message_set; |
|---|
| 1055 | } |
|---|
| 1056 | |
|---|
| [4e17e6c] | 1057 | //separate, then sort |
|---|
| [3d695da] | 1058 | $ids = explode(',', $message_set); |
|---|
| [4e17e6c] | 1059 | sort($ids); |
|---|
| 1060 | |
|---|
| 1061 | $result = array(); |
|---|
| [3d695da] | 1062 | $start = $prev = $ids[0]; |
|---|
| 1063 | |
|---|
| [6ccd45a] | 1064 | foreach ($ids as $id) { |
|---|
| [4e17e6c] | 1065 | $incr = $id - $prev; |
|---|
| [3d695da] | 1066 | if ($incr > 1) { //found a gap |
|---|
| 1067 | if ($start == $prev) { |
|---|
| [06583c3] | 1068 | $result[] = $prev; //push single id |
|---|
| 1069 | } else { |
|---|
| [3d695da] | 1070 | $result[] = $start . ':' . $prev; //push sequence as start_id:end_id |
|---|
| [06583c3] | 1071 | } |
|---|
| [49e5f72] | 1072 | $start = $id; //start of new sequence |
|---|
| [4e17e6c] | 1073 | } |
|---|
| 1074 | $prev = $id; |
|---|
| 1075 | } |
|---|
| [49e5f72] | 1076 | |
|---|
| [4e17e6c] | 1077 | //handle the last sequence/id |
|---|
| [6ccd45a] | 1078 | if ($start==$prev) { |
|---|
| 1079 | $result[] = $prev; |
|---|
| [49e5f72] | 1080 | } else { |
|---|
| 1081 | $result[] = $start.':'.$prev; |
|---|
| 1082 | } |
|---|
| [6ccd45a] | 1083 | |
|---|
| [4e17e6c] | 1084 | //return as comma separated string |
|---|
| [6ccd45a] | 1085 | return implode(',', $result); |
|---|
| [4e17e6c] | 1086 | } |
|---|
| 1087 | |
|---|
| [6ccd45a] | 1088 | function iil_C_UIDsToMIDs(&$conn, $mailbox, $uids) { |
|---|
| 1089 | if (!is_array($uids) || count($uids) == 0) { |
|---|
| 1090 | return array(); |
|---|
| [49e5f72] | 1091 | } |
|---|
| [3d695da] | 1092 | return iil_C_Search($conn, $mailbox, 'UID ' . implode(',', $uids)); |
|---|
| [4e17e6c] | 1093 | } |
|---|
| 1094 | |
|---|
| [6ccd45a] | 1095 | function iil_C_UIDToMID(&$conn, $mailbox, $uid) { |
|---|
| [4e17e6c] | 1096 | $result = iil_C_UIDsToMIDs($conn, $mailbox, array($uid)); |
|---|
| [3d695da] | 1097 | if (count($result) == 1) { |
|---|
| [6ccd45a] | 1098 | return $result[0]; |
|---|
| 1099 | } |
|---|
| [49e5f72] | 1100 | return false; |
|---|
| [4e17e6c] | 1101 | } |
|---|
| 1102 | |
|---|
| [6ccd45a] | 1103 | function iil_C_FetchUIDs(&$conn,$mailbox) { |
|---|
| [4e17e6c] | 1104 | global $clock; |
|---|
| 1105 | |
|---|
| [30233b8] | 1106 | $num = iil_C_CountMessages($conn, $mailbox); |
|---|
| [6ccd45a] | 1107 | if ($num == 0) { |
|---|
| 1108 | return array(); |
|---|
| [49e5f72] | 1109 | } |
|---|
| [06583c3] | 1110 | $message_set = '1' . ($num>1?':' . $num:''); |
|---|
| [4e17e6c] | 1111 | |
|---|
| [2f7405e3] | 1112 | return iil_C_FetchHeaderIndex($conn, $mailbox, $message_set, 'UID'); |
|---|
| [4e17e6c] | 1113 | } |
|---|
| 1114 | |
|---|
| [6ccd45a] | 1115 | function iil_SortThreadHeaders($headers, $index_a, $uids) { |
|---|
| [4e17e6c] | 1116 | asort($index_a); |
|---|
| 1117 | $result = array(); |
|---|
| [6ccd45a] | 1118 | foreach ($index_a as $mid=>$foobar) { |
|---|
| [4e17e6c] | 1119 | $uid = $uids[$mid]; |
|---|
| 1120 | $result[$uid] = $headers[$uid]; |
|---|
| 1121 | } |
|---|
| 1122 | return $result; |
|---|
| 1123 | } |
|---|
| 1124 | |
|---|
| [6ccd45a] | 1125 | function iil_C_FetchThreadHeaders(&$conn, $mailbox, $message_set) { |
|---|
| [4e17e6c] | 1126 | global $clock; |
|---|
| 1127 | global $index_a; |
|---|
| 1128 | |
|---|
| [24053e0] | 1129 | list($from_idx, $to_idx) = explode(':', $message_set); |
|---|
| [6ccd45a] | 1130 | if (empty($message_set) || (isset($to_idx) |
|---|
| 1131 | && (int)$from_idx > (int)$to_idx)) { |
|---|
| [24053e0] | 1132 | return false; |
|---|
| [49e5f72] | 1133 | } |
|---|
| [4e17e6c] | 1134 | |
|---|
| 1135 | $result = array(); |
|---|
| [6ccd45a] | 1136 | $uids = iil_C_FetchUIDs($conn, $mailbox); |
|---|
| 1137 | $debug = false; |
|---|
| [4e17e6c] | 1138 | |
|---|
| 1139 | $message_set = iil_CompressMessageSet($message_set); |
|---|
| [6ccd45a] | 1140 | |
|---|
| [4e17e6c] | 1141 | /* if we're missing any, get them */ |
|---|
| [6ccd45a] | 1142 | if ($message_set) { |
|---|
| [4e17e6c] | 1143 | /* FETCH date,from,subject headers */ |
|---|
| [6ccd45a] | 1144 | $key = 'fh'; |
|---|
| 1145 | $fp = $conn->fp; |
|---|
| 1146 | $request = $key . " FETCH $message_set "; |
|---|
| [49e5f72] | 1147 | $request .= "(BODY.PEEK[HEADER.FIELDS (SUBJECT MESSAGE-ID IN-REPLY-TO)])"; |
|---|
| [6ccd45a] | 1148 | $mid_to_id = array(); |
|---|
| [49e5f72] | 1149 | if (!iil_PutLine($fp, $request)) { |
|---|
| [6ccd45a] | 1150 | return false; |
|---|
| [49e5f72] | 1151 | } |
|---|
| [6ccd45a] | 1152 | do { |
|---|
| [4e17e6c] | 1153 | $line = chop(iil_ReadLine($fp, 1024)); |
|---|
| [6ccd45a] | 1154 | if ($debug) { |
|---|
| 1155 | echo $line . "\n"; |
|---|
| [49e5f72] | 1156 | } |
|---|
| [2f7405e3] | 1157 | if (preg_match('/\{[0-9]+\}$/', $line)) { |
|---|
| [6ccd45a] | 1158 | $a = explode(' ', $line); |
|---|
| [4e17e6c] | 1159 | $new = array(); |
|---|
| 1160 | |
|---|
| 1161 | $new_thhd = new iilThreadHeader; |
|---|
| 1162 | $new_thhd->id = $a[1]; |
|---|
| [6ccd45a] | 1163 | do { |
|---|
| [3d695da] | 1164 | $line = chop(iil_ReadLine($fp, 1024), "\r\n"); |
|---|
| [6ccd45a] | 1165 | if (iil_StartsWithI($line, 'Message-ID:') |
|---|
| [49e5f72] | 1166 | || (iil_StartsWithI($line,'In-Reply-To:')) |
|---|
| 1167 | || (iil_StartsWithI($line,'SUBJECT:'))) { |
|---|
| [3d695da] | 1168 | |
|---|
| 1169 | $pos = strpos($line, ':'); |
|---|
| [4e17e6c] | 1170 | $field_name = substr($line, 0, $pos); |
|---|
| [3d695da] | 1171 | $field_val = substr($line, $pos+1); |
|---|
| 1172 | |
|---|
| [4e17e6c] | 1173 | $new[strtoupper($field_name)] = trim($field_val); |
|---|
| [3d695da] | 1174 | |
|---|
| [2f7405e3] | 1175 | } else if (preg_match('/^\s+/', $line)) { |
|---|
| [3d695da] | 1176 | $new[strtoupper($field_name)] .= trim($line); |
|---|
| [4e17e6c] | 1177 | } |
|---|
| [6ccd45a] | 1178 | } while ($line[0] != ')'); |
|---|
| 1179 | |
|---|
| [4e17e6c] | 1180 | $new_thhd->sbj = $new['SUBJECT']; |
|---|
| 1181 | $new_thhd->mid = substr($new['MESSAGE-ID'], 1, -1); |
|---|
| 1182 | $new_thhd->irt = substr($new['IN-REPLY-TO'], 1, -1); |
|---|
| 1183 | |
|---|
| 1184 | $result[$uids[$new_thhd->id]] = $new_thhd; |
|---|
| 1185 | } |
|---|
| [6ccd45a] | 1186 | } while (!iil_StartsWith($line, 'fh')); |
|---|
| [4e17e6c] | 1187 | } |
|---|
| 1188 | |
|---|
| 1189 | /* sort headers */ |
|---|
| [6ccd45a] | 1190 | if (is_array($index_a)) { |
|---|
| [4e17e6c] | 1191 | $result = iil_SortThreadHeaders($result, $index_a, $uids); |
|---|
| 1192 | } |
|---|
| 1193 | |
|---|
| 1194 | //echo 'iil_FetchThreadHeaders:'."\n"; |
|---|
| 1195 | //print_r($result); |
|---|
| 1196 | |
|---|
| 1197 | return $result; |
|---|
| 1198 | } |
|---|
| 1199 | |
|---|
| [6ccd45a] | 1200 | function iil_C_BuildThreads2(&$conn, $mailbox, $message_set, &$clock) { |
|---|
| [4e17e6c] | 1201 | global $index_a; |
|---|
| 1202 | |
|---|
| [24053e0] | 1203 | list($from_idx, $to_idx) = explode(':', $message_set); |
|---|
| [6ccd45a] | 1204 | if (empty($message_set) || (isset($to_idx) |
|---|
| [49e5f72] | 1205 | && (int)$from_idx > (int)$to_idx)) { |
|---|
| [24053e0] | 1206 | return false; |
|---|
| [6ccd45a] | 1207 | } |
|---|
| 1208 | |
|---|
| 1209 | $result = array(); |
|---|
| 1210 | $roots = array(); |
|---|
| [4e17e6c] | 1211 | $root_mids = array(); |
|---|
| [6ccd45a] | 1212 | $sub_mids = array(); |
|---|
| 1213 | $strays = array(); |
|---|
| 1214 | $messages = array(); |
|---|
| 1215 | $fp = $conn->fp; |
|---|
| 1216 | $debug = false; |
|---|
| [4e17e6c] | 1217 | |
|---|
| [2f7405e3] | 1218 | $sbj_filter_pat = '/[a-z]{2,3}(\[[0-9]*\])?:(\s*)/i'; |
|---|
| [4e17e6c] | 1219 | |
|---|
| 1220 | /* Do "SELECT" command */ |
|---|
| [6ccd45a] | 1221 | if (!iil_C_Select($conn, $mailbox)) { |
|---|
| 1222 | return false; |
|---|
| [49e5f72] | 1223 | } |
|---|
| [6ccd45a] | 1224 | |
|---|
| [4e17e6c] | 1225 | /* FETCH date,from,subject headers */ |
|---|
| 1226 | $mid_to_id = array(); |
|---|
| [6ccd45a] | 1227 | $messages = array(); |
|---|
| 1228 | $headers = iil_C_FetchThreadHeaders($conn, $mailbox, $message_set); |
|---|
| 1229 | if ($clock) { |
|---|
| 1230 | $clock->register('fetched headers'); |
|---|
| 1231 | } |
|---|
| 1232 | |
|---|
| 1233 | if ($debug) { |
|---|
| 1234 | print_r($headers); |
|---|
| 1235 | } |
|---|
| 1236 | |
|---|
| [4e17e6c] | 1237 | /* go through header records */ |
|---|
| [6ccd45a] | 1238 | foreach ($headers as $header) { |
|---|
| [4e17e6c] | 1239 | //$id = $header['i']; |
|---|
| 1240 | //$new = array('id'=>$id, 'MESSAGE-ID'=>$header['m'], |
|---|
| 1241 | // 'IN-REPLY-TO'=>$header['r'], 'SUBJECT'=>$header['s']); |
|---|
| [6ccd45a] | 1242 | $id = $header->id; |
|---|
| [3d695da] | 1243 | $new = array('id' => $id, 'MESSAGE-ID' => $header->mid, |
|---|
| [49e5f72] | 1244 | 'IN-REPLY-TO' => $header->irt, 'SUBJECT' => $header->sbj); |
|---|
| [4e17e6c] | 1245 | |
|---|
| 1246 | /* add to message-id -> mid lookup table */ |
|---|
| 1247 | $mid_to_id[$new['MESSAGE-ID']] = $id; |
|---|
| 1248 | |
|---|
| 1249 | /* if no subject, use message-id */ |
|---|
| [6ccd45a] | 1250 | if (empty($new['SUBJECT'])) { |
|---|
| 1251 | $new['SUBJECT'] = $new['MESSAGE-ID']; |
|---|
| 1252 | } |
|---|
| 1253 | |
|---|
| [4e17e6c] | 1254 | /* if subject contains 'RE:' or has in-reply-to header, it's a reply */ |
|---|
| [2f7405e3] | 1255 | $sbj_pre = ''; |
|---|
| [4e17e6c] | 1256 | $has_re = false; |
|---|
| [2f7405e3] | 1257 | if (preg_match($sbj_filter_pat, $new['SUBJECT'])) { |
|---|
| [6ccd45a] | 1258 | $has_re = true; |
|---|
| 1259 | } |
|---|
| [2f7405e3] | 1260 | if ($has_re || $new['IN-REPLY-TO']) { |
|---|
| [49e5f72] | 1261 | $sbj_pre = 'RE:'; |
|---|
| [6ccd45a] | 1262 | } |
|---|
| 1263 | |
|---|
| [4e17e6c] | 1264 | /* strip out 're:', 'fw:' etc */ |
|---|
| [6ccd45a] | 1265 | if ($has_re) { |
|---|
| [2f7405e3] | 1266 | $sbj = preg_replace($sbj_filter_pat, '', $new['SUBJECT']); |
|---|
| [6ccd45a] | 1267 | } else { |
|---|
| 1268 | $sbj = $new['SUBJECT']; |
|---|
| 1269 | } |
|---|
| [49e5f72] | 1270 | $new['SUBJECT'] = $sbj_pre.$sbj; |
|---|
| [4e17e6c] | 1271 | |
|---|
| 1272 | |
|---|
| 1273 | /* if subject not a known thread-root, add to list */ |
|---|
| [6ccd45a] | 1274 | if ($debug) { |
|---|
| 1275 | echo $id . ' ' . $new['SUBJECT'] . "\t" . $new['MESSAGE-ID'] . "\n"; |
|---|
| 1276 | } |
|---|
| [49e5f72] | 1277 | $root_id = $roots[$sbj]; |
|---|
| [4e17e6c] | 1278 | |
|---|
| [6ccd45a] | 1279 | if ($root_id && ($has_re || !$root_in_root[$root_id])) { |
|---|
| 1280 | if ($debug) { |
|---|
| 1281 | echo "\tfound root: $root_id\n"; |
|---|
| 1282 | } |
|---|
| [49e5f72] | 1283 | $sub_mids[$new['MESSAGE-ID']] = $root_id; |
|---|
| [6ccd45a] | 1284 | $result[$root_id][] = $id; |
|---|
| [49e5f72] | 1285 | } else if (!isset($roots[$sbj]) || (!$has_re && $root_in_root[$root_id])) { |
|---|
| [4e17e6c] | 1286 | /* try to use In-Reply-To header to find root |
|---|
| 1287 | unless subject contains 'Re:' */ |
|---|
| [6ccd45a] | 1288 | if ($has_re&&$new['IN-REPLY-TO']) { |
|---|
| 1289 | if ($debug) { |
|---|
| 1290 | echo "\tlooking: ".$new['IN-REPLY-TO']."\n"; |
|---|
| 1291 | } |
|---|
| [4e17e6c] | 1292 | //reply to known message? |
|---|
| 1293 | $temp = $sub_mids[$new['IN-REPLY-TO']]; |
|---|
| 1294 | |
|---|
| [6ccd45a] | 1295 | if ($temp) { |
|---|
| [4e17e6c] | 1296 | //found it, root:=parent's root |
|---|
| [6ccd45a] | 1297 | if ($debug) { |
|---|
| 1298 | echo "\tfound parent: ".$new['SUBJECT']."\n"; |
|---|
| 1299 | } |
|---|
| [49e5f72] | 1300 | $result[$temp][] = $id; |
|---|
| [4e17e6c] | 1301 | $sub_mids[$new['MESSAGE-ID']] = $temp; |
|---|
| [6ccd45a] | 1302 | $sbj = ''; |
|---|
| 1303 | } else { |
|---|
| [4e17e6c] | 1304 | //if we can't find referenced parent, it's a "stray" |
|---|
| 1305 | $strays[$id] = $new['IN-REPLY-TO']; |
|---|
| 1306 | } |
|---|
| 1307 | } |
|---|
| 1308 | |
|---|
| 1309 | //add subject as root |
|---|
| [6ccd45a] | 1310 | if ($sbj) { |
|---|
| 1311 | if ($debug) { |
|---|
| 1312 | echo "\t added to root\n"; |
|---|
| 1313 | } |
|---|
| [49e5f72] | 1314 | $roots[$sbj] = $id; |
|---|
| [6ccd45a] | 1315 | $root_in_root[$id] = !$has_re; |
|---|
| [4e17e6c] | 1316 | $sub_mids[$new['MESSAGE-ID']] = $id; |
|---|
| [6ccd45a] | 1317 | $result[$id] = array($id); |
|---|
| [4e17e6c] | 1318 | } |
|---|
| [6ccd45a] | 1319 | if ($debug) { |
|---|
| 1320 | echo $new['MESSAGE-ID'] . "\t" . $sbj . "\n"; |
|---|
| [49e5f72] | 1321 | } |
|---|
| [4e17e6c] | 1322 | } |
|---|
| 1323 | } |
|---|
| 1324 | |
|---|
| 1325 | //now that we've gone through all the messages, |
|---|
| 1326 | //go back and try and link up the stray threads |
|---|
| [3d695da] | 1327 | if (count($strays) > 0) { |
|---|
| [6ccd45a] | 1328 | foreach ($strays as $id=>$irt) { |
|---|
| [4e17e6c] | 1329 | $root_id = $sub_mids[$irt]; |
|---|
| [6ccd45a] | 1330 | if (!$root_id || $root_id==$id) { |
|---|
| 1331 | continue; |
|---|
| 1332 | } |
|---|
| [49e5f72] | 1333 | $result[$root_id] = array_merge($result[$root_id],$result[$id]); |
|---|
| [4e17e6c] | 1334 | unset($result[$id]); |
|---|
| 1335 | } |
|---|
| 1336 | } |
|---|
| 1337 | |
|---|
| [6ccd45a] | 1338 | if ($clock) { |
|---|
| 1339 | $clock->register('data prepped'); |
|---|
| 1340 | } |
|---|
| 1341 | |
|---|
| 1342 | if ($debug) { |
|---|
| 1343 | print_r($roots); |
|---|
| 1344 | } |
|---|
| [49e5f72] | 1345 | |
|---|
| [4e17e6c] | 1346 | return $result; |
|---|
| 1347 | } |
|---|
| 1348 | |
|---|
| [6ccd45a] | 1349 | function iil_SortThreads(&$tree, $index, $sort_order = 'ASC') { |
|---|
| 1350 | if (!is_array($tree) || !is_array($index)) { |
|---|
| 1351 | return false; |
|---|
| [49e5f72] | 1352 | } |
|---|
| [6ccd45a] | 1353 | |
|---|
| [4e17e6c] | 1354 | //create an id to position lookup table |
|---|
| 1355 | $i = 0; |
|---|
| [6ccd45a] | 1356 | foreach ($index as $id=>$val) { |
|---|
| [4e17e6c] | 1357 | $i++; |
|---|
| 1358 | $index[$id] = $i; |
|---|
| 1359 | } |
|---|
| 1360 | $max = $i+1; |
|---|
| 1361 | |
|---|
| 1362 | //for each tree, set array key to position |
|---|
| 1363 | $itree = array(); |
|---|
| [6ccd45a] | 1364 | foreach ($tree as $id=>$node) { |
|---|
| 1365 | if (count($tree[$id])<=1) { |
|---|
| [4e17e6c] | 1366 | //for "threads" with only one message, key is position of that message |
|---|
| [6ccd45a] | 1367 | $n = $index[$id]; |
|---|
| [4e17e6c] | 1368 | $itree[$n] = array($n=>$id); |
|---|
| [6ccd45a] | 1369 | } else { |
|---|
| [4e17e6c] | 1370 | //for "threads" with multiple messages, |
|---|
| [6ccd45a] | 1371 | $min = $max; |
|---|
| [4e17e6c] | 1372 | $new_a = array(); |
|---|
| [6ccd45a] | 1373 | foreach ($tree[$id] as $mid) { |
|---|
| [4e17e6c] | 1374 | $new_a[$index[$mid]] = $mid; //create new sub-array mapping position to id |
|---|
| [6ccd45a] | 1375 | $pos = $index[$mid]; |
|---|
| 1376 | if ($pos&&$pos<$min) { |
|---|
| 1377 | $min = $index[$mid]; //find smallest position |
|---|
| 1378 | } |
|---|
| [4e17e6c] | 1379 | } |
|---|
| 1380 | $n = $min; //smallest position of child is thread position |
|---|
| 1381 | |
|---|
| 1382 | //assign smallest position to root level key |
|---|
| 1383 | //set children array to one created above |
|---|
| 1384 | ksort($new_a); |
|---|
| 1385 | $itree[$n] = $new_a; |
|---|
| 1386 | } |
|---|
| 1387 | } |
|---|
| 1388 | |
|---|
| 1389 | //sort by key, this basically sorts all threads |
|---|
| 1390 | ksort($itree); |
|---|
| [3d695da] | 1391 | $i = 0; |
|---|
| 1392 | $out = array(); |
|---|
| [6ccd45a] | 1393 | foreach ($itree as $k=>$node) { |
|---|
| [4e17e6c] | 1394 | $out[$i] = $itree[$k]; |
|---|
| 1395 | $i++; |
|---|
| 1396 | } |
|---|
| 1397 | |
|---|
| 1398 | return $out; |
|---|
| 1399 | } |
|---|
| 1400 | |
|---|
| [6ccd45a] | 1401 | function iil_IndexThreads(&$tree) { |
|---|
| [4e17e6c] | 1402 | /* creates array mapping mid to thread id */ |
|---|
| 1403 | |
|---|
| [6ccd45a] | 1404 | if (!is_array($tree)) { |
|---|
| 1405 | return false; |
|---|
| 1406 | } |
|---|
| 1407 | |
|---|
| [4e17e6c] | 1408 | $t_index = array(); |
|---|
| [6ccd45a] | 1409 | foreach ($tree as $pos=>$kids) { |
|---|
| 1410 | foreach ($kids as $kid) $t_index[$kid] = $pos; |
|---|
| [4e17e6c] | 1411 | } |
|---|
| 1412 | |
|---|
| 1413 | return $t_index; |
|---|
| 1414 | } |
|---|
| 1415 | |
|---|
| [cc97ea0] | 1416 | function iil_C_FetchHeaders(&$conn, $mailbox, $message_set, $uidfetch=false, $bodystr=false, $add='') |
|---|
| [d5ff9276] | 1417 | { |
|---|
| [4e17e6c] | 1418 | global $IMAP_USE_INTERNAL_DATE; |
|---|
| 1419 | |
|---|
| [6ccd45a] | 1420 | $result = array(); |
|---|
| 1421 | $fp = $conn->fp; |
|---|
| [4e17e6c] | 1422 | |
|---|
| 1423 | /* Do "SELECT" command */ |
|---|
| [6ccd45a] | 1424 | if (!iil_C_Select($conn, $mailbox)) { |
|---|
| [4e17e6c] | 1425 | $conn->error = "Couldn't select $mailbox"; |
|---|
| 1426 | return false; |
|---|
| 1427 | } |
|---|
| [6bc59a7] | 1428 | |
|---|
| [2dd7ee3] | 1429 | $message_set = iil_CompressMessageSet($message_set); |
|---|
| 1430 | |
|---|
| [cc97ea0] | 1431 | if ($add) |
|---|
| [2f7405e3] | 1432 | $add = ' '.strtoupper(trim($add)); |
|---|
| [4e17e6c] | 1433 | |
|---|
| [9b9f267] | 1434 | /* FETCH uid, size, flags and headers */ |
|---|
| 1435 | $key = 'FH12'; |
|---|
| 1436 | $request = $key . ($uidfetch ? ' UID' : '') . " FETCH $message_set "; |
|---|
| [7a229b9] | 1437 | $request .= "(UID RFC822.SIZE FLAGS INTERNALDATE "; |
|---|
| 1438 | if ($bodystr) |
|---|
| 1439 | $request .= "BODYSTRUCTURE "; |
|---|
| 1440 | $request .= "BODY.PEEK[HEADER.FIELDS "; |
|---|
| [a9a8ef2] | 1441 | $request .= "(DATE FROM TO SUBJECT REPLY-TO IN-REPLY-TO CC BCC "; |
|---|
| 1442 | $request .= "CONTENT-TRANSFER-ENCODING CONTENT-TYPE MESSAGE-ID "; |
|---|
| [cc97ea0] | 1443 | $request .= "REFERENCES DISPOSITION-NOTIFICATION-TO X-PRIORITY".$add.")])"; |
|---|
| [6ccd45a] | 1444 | |
|---|
| [49e5f72] | 1445 | if (!iil_PutLine($fp, $request)) { |
|---|
| [a9a8ef2] | 1446 | return false; |
|---|
| 1447 | } |
|---|
| [6ccd45a] | 1448 | do { |
|---|
| [7a229b9] | 1449 | $line = iil_ReadLine($fp, 1024); |
|---|
| 1450 | $line = iil_MultLine($fp, $line); |
|---|
| [55c2a4e] | 1451 | |
|---|
| [6ccd45a] | 1452 | $a = explode(' ', $line); |
|---|
| 1453 | if (($line[0] == '*') && ($a[2] == 'FETCH')) { |
|---|
| 1454 | $id = $a[1]; |
|---|
| 1455 | |
|---|
| 1456 | $result[$id] = new iilBasicHeader; |
|---|
| 1457 | $result[$id]->id = $id; |
|---|
| 1458 | $result[$id]->subject = ''; |
|---|
| 1459 | $result[$id]->messageID = 'mid:' . $id; |
|---|
| [d5ff9276] | 1460 | |
|---|
| [7a229b9] | 1461 | $lines = array(); |
|---|
| 1462 | $ln = 0; |
|---|
| [4e17e6c] | 1463 | /* |
|---|
| [9b9f267] | 1464 | Sample reply line: |
|---|
| 1465 | * 321 FETCH (UID 2417 RFC822.SIZE 2730 FLAGS (\Seen) |
|---|
| [7a229b9] | 1466 | INTERNALDATE "16-Nov-2008 21:08:46 +0100" BODYSTRUCTURE (...) |
|---|
| 1467 | BODY[HEADER.FIELDS ... |
|---|
| [9b9f267] | 1468 | */ |
|---|
| [7a229b9] | 1469 | |
|---|
| 1470 | if (preg_match('/^\* [0-9]+ FETCH \((.*) BODY/s', $line, $matches)) { |
|---|
| [9b9f267] | 1471 | $str = $matches[1]; |
|---|
| 1472 | |
|---|
| [7a229b9] | 1473 | // swap parents with quotes, then explode |
|---|
| [2f7405e3] | 1474 | $str = preg_replace('/[()]/', '"', $str); |
|---|
| [9b9f267] | 1475 | $a = iil_ExplodeQuotedString(' ', $str); |
|---|
| 1476 | |
|---|
| [7a229b9] | 1477 | // did we get the right number of replies? |
|---|
| [9b9f267] | 1478 | $parts_count = count($a); |
|---|
| [55c2a4e] | 1479 | if ($parts_count>=6) { |
|---|
| [9b9f267] | 1480 | for ($i=0; $i<$parts_count; $i=$i+2) { |
|---|
| 1481 | if (strcasecmp($a[$i],'UID') == 0) |
|---|
| 1482 | $result[$id]->uid = $a[$i+1]; |
|---|
| 1483 | else if (strcasecmp($a[$i],'RFC822.SIZE') == 0) |
|---|
| 1484 | $result[$id]->size = $a[$i+1]; |
|---|
| 1485 | else if (strcasecmp($a[$i],'INTERNALDATE') == 0) |
|---|
| 1486 | $time_str = $a[$i+1]; |
|---|
| 1487 | else if (strcasecmp($a[$i],'FLAGS') == 0) |
|---|
| 1488 | $flags_str = $a[$i+1]; |
|---|
| 1489 | } |
|---|
| 1490 | |
|---|
| 1491 | $time_str = str_replace('"', '', $time_str); |
|---|
| 1492 | |
|---|
| 1493 | // if time is gmt... |
|---|
| 1494 | $time_str = str_replace('GMT','+0000',$time_str); |
|---|
| 1495 | |
|---|
| 1496 | $result[$id]->internaldate = $time_str; |
|---|
| [11b80e9] | 1497 | $result[$id]->timestamp = iil_StrToTime($time_str); |
|---|
| [9b9f267] | 1498 | $result[$id]->date = $time_str; |
|---|
| 1499 | } |
|---|
| [7a229b9] | 1500 | |
|---|
| 1501 | // BODYSTRUCTURE |
|---|
| 1502 | if($bodystr) { |
|---|
| 1503 | while (!preg_match('/ BODYSTRUCTURE (.*) BODY\[HEADER.FIELDS/s', $line, $m)) { |
|---|
| 1504 | $line2 = iil_ReadLine($fp, 1024); |
|---|
| 1505 | $line .= iil_MultLine($fp, $line2); |
|---|
| 1506 | } |
|---|
| 1507 | $result[$id]->body_structure = $m[1]; |
|---|
| 1508 | } |
|---|
| 1509 | |
|---|
| 1510 | // the rest of the result |
|---|
| [07f0b94] | 1511 | preg_match('/ BODY\[HEADER.FIELDS \(.*?\)\]\s*(.*)$/s', $line, $m); |
|---|
| [7a229b9] | 1512 | $reslines = explode("\n", trim($m[1], '"')); |
|---|
| 1513 | // re-parse (see below) |
|---|
| 1514 | foreach ($reslines as $line) { |
|---|
| 1515 | if (ord($line[0])<=32) { |
|---|
| [55c2a4e] | 1516 | $lines[$ln] .= (empty($lines[$ln])?'':"\n").trim($line); |
|---|
| [7a229b9] | 1517 | } else { |
|---|
| 1518 | $lines[++$ln] = trim($line); |
|---|
| 1519 | } |
|---|
| 1520 | } |
|---|
| [9b9f267] | 1521 | } |
|---|
| 1522 | |
|---|
| 1523 | /* |
|---|
| [4e17e6c] | 1524 | Start parsing headers. The problem is, some header "lines" take up multiple lines. |
|---|
| 1525 | So, we'll read ahead, and if the one we're reading now is a valid header, we'll |
|---|
| 1526 | process the previous line. Otherwise, we'll keep adding the strings until we come |
|---|
| 1527 | to the next valid header line. |
|---|
| 1528 | */ |
|---|
| [9b9f267] | 1529 | |
|---|
| [6ccd45a] | 1530 | do { |
|---|
| 1531 | $line = chop(iil_ReadLine($fp, 300), "\r\n"); |
|---|
| [9b9f267] | 1532 | |
|---|
| [55c2a4e] | 1533 | // The preg_match below works around communigate imap, which outputs " UID <number>)". |
|---|
| 1534 | // Without this, the while statement continues on and gets the "FH0 OK completed" message. |
|---|
| 1535 | // If this loop gets the ending message, then the outer loop does not receive it from radline on line 1249. |
|---|
| 1536 | // This in causes the if statement on line 1278 to never be true, which causes the headers to end up missing |
|---|
| 1537 | // If the if statement was changed to pick up the fh0 from this loop, then it causes the outer loop to spin |
|---|
| 1538 | // An alternative might be: |
|---|
| 1539 | // if (!preg_match("/:/",$line) && preg_match("/\)$/",$line)) break; |
|---|
| 1540 | // however, unsure how well this would work with all imap clients. |
|---|
| 1541 | if (preg_match("/^\s*UID [0-9]+\)$/", $line)) { |
|---|
| 1542 | break; |
|---|
| 1543 | } |
|---|
| 1544 | |
|---|
| 1545 | // handle FLAGS reply after headers (AOL, Zimbra?) |
|---|
| 1546 | if (preg_match('/\s+FLAGS \((.*)\)\)$/', $line, $matches)) { |
|---|
| 1547 | $flags_str = $matches[1]; |
|---|
| 1548 | break; |
|---|
| 1549 | } |
|---|
| 1550 | |
|---|
| [6ccd45a] | 1551 | if (ord($line[0])<=32) { |
|---|
| [55c2a4e] | 1552 | $lines[$ln] .= (empty($lines[$ln])?'':"\n").trim($line); |
|---|
| [90180ef] | 1553 | } else { |
|---|
| [7a229b9] | 1554 | $lines[++$ln] = trim($line); |
|---|
| [4e17e6c] | 1555 | } |
|---|
| [a9a8ef2] | 1556 | // patch from "Maksim Rubis" <siburny@hotmail.com> |
|---|
| [90180ef] | 1557 | } while (trim($line[0]) != ')' && strncmp($line, $key, strlen($key))); |
|---|
| [9b9f267] | 1558 | |
|---|
| [a9a8ef2] | 1559 | if (strncmp($line, $key, strlen($key))) { |
|---|
| [7a229b9] | 1560 | // process header, fill iilBasicHeader obj. |
|---|
| 1561 | // initialize |
|---|
| [a9a8ef2] | 1562 | if (is_array($headers)) { |
|---|
| 1563 | reset($headers); |
|---|
| 1564 | while (list($k, $bar) = each($headers)) { |
|---|
| 1565 | $headers[$k] = ''; |
|---|
| 1566 | } |
|---|
| 1567 | } |
|---|
| [07f0b94] | 1568 | |
|---|
| [7a229b9] | 1569 | // create array with header field:data |
|---|
| [a9a8ef2] | 1570 | while ( list($lines_key, $str) = each($lines) ) { |
|---|
| 1571 | list($field, $string) = iil_SplitHeaderLine($str); |
|---|
| 1572 | |
|---|
| [14432db] | 1573 | $field = strtolower($field); |
|---|
| [2f7405e3] | 1574 | $string = preg_replace('/\n\s*/', ' ', $string); |
|---|
| [a9a8ef2] | 1575 | |
|---|
| 1576 | switch ($field) { |
|---|
| [fba1f5a] | 1577 | case 'date'; |
|---|
| [9b9f267] | 1578 | if (!$IMAP_USE_INTERNAL_DATE) { |
|---|
| 1579 | $result[$id]->date = $string; |
|---|
| 1580 | $result[$id]->timestamp = iil_StrToTime($string); |
|---|
| 1581 | } |
|---|
| [fba1f5a] | 1582 | break; |
|---|
| 1583 | case 'from': |
|---|
| 1584 | $result[$id]->from = $string; |
|---|
| 1585 | break; |
|---|
| 1586 | case 'to': |
|---|
| [109314c] | 1587 | $result[$id]->to = preg_replace('/undisclosed-recipients:[;,]*/', '', $string); |
|---|
| [fba1f5a] | 1588 | break; |
|---|
| 1589 | case 'subject': |
|---|
| [14432db] | 1590 | $result[$id]->subject = $string; |
|---|
| [fba1f5a] | 1591 | break; |
|---|
| 1592 | case 'reply-to': |
|---|
| [14432db] | 1593 | $result[$id]->replyto = $string; |
|---|
| [fba1f5a] | 1594 | break; |
|---|
| 1595 | case 'cc': |
|---|
| [14432db] | 1596 | $result[$id]->cc = $string; |
|---|
| [fba1f5a] | 1597 | break; |
|---|
| 1598 | case 'bcc': |
|---|
| [14432db] | 1599 | $result[$id]->bcc = $string; |
|---|
| [fba1f5a] | 1600 | break; |
|---|
| 1601 | case 'content-transfer-encoding': |
|---|
| [14432db] | 1602 | $result[$id]->encoding = $string; |
|---|
| [fba1f5a] | 1603 | break; |
|---|
| 1604 | case 'content-type': |
|---|
| [a9a8ef2] | 1605 | $ctype_parts = explode(";", $string); |
|---|
| [fba1f5a] | 1606 | $result[$id]->ctype = array_shift($ctype_parts); |
|---|
| [31ecc4f] | 1607 | foreach ($ctype_parts as $ctype_add) { |
|---|
| [a9a8ef2] | 1608 | if (preg_match('/charset="?([a-z0-9\-\.\_]+)"?/i', |
|---|
| 1609 | $ctype_add, $regs)) { |
|---|
| 1610 | $result[$id]->charset = $regs[1]; |
|---|
| [611a6a7] | 1611 | } |
|---|
| [a9a8ef2] | 1612 | } |
|---|
| 1613 | break; |
|---|
| [fba1f5a] | 1614 | case 'in-reply-to': |
|---|
| [2f7405e3] | 1615 | $result[$id]->in_reply_to = preg_replace('/[\n<>]/', '', $string); |
|---|
| [fba1f5a] | 1616 | break; |
|---|
| 1617 | case 'references': |
|---|
| 1618 | $result[$id]->references = $string; |
|---|
| 1619 | break; |
|---|
| [31ecc4f] | 1620 | case 'return-receipt-to': |
|---|
| 1621 | case 'disposition-notification-to': |
|---|
| 1622 | case 'x-confirm-reading-to': |
|---|
| [14432db] | 1623 | $result[$id]->mdn_to = $string; |
|---|
| [31ecc4f] | 1624 | break; |
|---|
| [fba1f5a] | 1625 | case 'message-id': |
|---|
| 1626 | $result[$id]->messageID = $string; |
|---|
| 1627 | break; |
|---|
| [ff08eed] | 1628 | case 'x-priority': |
|---|
| 1629 | if (preg_match('/^(\d+)/', $string, $matches)) |
|---|
| 1630 | $result[$id]->priority = intval($matches[1]); |
|---|
| 1631 | break; |
|---|
| [cc97ea0] | 1632 | default: |
|---|
| 1633 | if (strlen($field) > 2) |
|---|
| 1634 | $result[$id]->others[$field] = $string; |
|---|
| 1635 | break; |
|---|
| [a9a8ef2] | 1636 | } // end switch () |
|---|
| 1637 | } // end while () |
|---|
| 1638 | } else { |
|---|
| 1639 | $a = explode(' ', $line); |
|---|
| 1640 | } |
|---|
| [55c2a4e] | 1641 | |
|---|
| 1642 | // process flags |
|---|
| 1643 | if (!empty($flags_str)) { |
|---|
| [2f7405e3] | 1644 | $flags_str = preg_replace('/[\\\"]/', '', $flags_str); |
|---|
| [55c2a4e] | 1645 | $flags_a = explode(' ', $flags_str); |
|---|
| 1646 | |
|---|
| 1647 | if (is_array($flags_a)) { |
|---|
| 1648 | reset($flags_a); |
|---|
| 1649 | while (list(,$val)=each($flags_a)) { |
|---|
| 1650 | if (strcasecmp($val,'Seen') == 0) { |
|---|
| 1651 | $result[$id]->seen = true; |
|---|
| 1652 | } else if (strcasecmp($val, 'Deleted') == 0) { |
|---|
| 1653 | $result[$id]->deleted=true; |
|---|
| 1654 | } else if (strcasecmp($val, 'Recent') == 0) { |
|---|
| 1655 | $result[$id]->recent = true; |
|---|
| 1656 | } else if (strcasecmp($val, 'Answered') == 0) { |
|---|
| 1657 | $result[$id]->answered = true; |
|---|
| 1658 | } else if (strcasecmp($val, '$Forwarded') == 0) { |
|---|
| 1659 | $result[$id]->forwarded = true; |
|---|
| 1660 | } else if (strcasecmp($val, 'Draft') == 0) { |
|---|
| 1661 | $result[$id]->is_draft = true; |
|---|
| 1662 | } else if (strcasecmp($val, '$MDNSent') == 0) { |
|---|
| 1663 | $result[$id]->mdn_sent = true; |
|---|
| 1664 | } else if (strcasecmp($val, 'Flagged') == 0) { |
|---|
| 1665 | $result[$id]->flagged = true; |
|---|
| 1666 | } |
|---|
| 1667 | } |
|---|
| 1668 | $result[$id]->flags = $flags_a; |
|---|
| 1669 | } |
|---|
| 1670 | } |
|---|
| [4e17e6c] | 1671 | } |
|---|
| [d5ff9276] | 1672 | } while (strcmp($a[0], $key) != 0); |
|---|
| 1673 | |
|---|
| [4e17e6c] | 1674 | return $result; |
|---|
| 1675 | } |
|---|
| 1676 | |
|---|
| [cc97ea0] | 1677 | function iil_C_FetchHeader(&$conn, $mailbox, $id, $uidfetch=false, $bodystr=false, $add='') { |
|---|
| [9b9f267] | 1678 | |
|---|
| [cc97ea0] | 1679 | $a = iil_C_FetchHeaders($conn, $mailbox, $id, $uidfetch, $bodystr, $add); |
|---|
| [6ccd45a] | 1680 | if (is_array($a)) { |
|---|
| [a9a8ef2] | 1681 | return array_shift($a); |
|---|
| 1682 | } |
|---|
| [6ccd45a] | 1683 | return false; |
|---|
| [4e17e6c] | 1684 | } |
|---|
| 1685 | |
|---|
| [6ccd45a] | 1686 | function iil_SortHeaders($a, $field, $flag) { |
|---|
| 1687 | if (empty($field)) { |
|---|
| 1688 | $field = 'uid'; |
|---|
| [49e5f72] | 1689 | } |
|---|
| [6ccd45a] | 1690 | $field = strtolower($field); |
|---|
| 1691 | if ($field == 'date' || $field == 'internaldate') { |
|---|
| 1692 | $field = 'timestamp'; |
|---|
| [49e5f72] | 1693 | } |
|---|
| [6ccd45a] | 1694 | if (empty($flag)) { |
|---|
| 1695 | $flag = 'ASC'; |
|---|
| [49e5f72] | 1696 | } |
|---|
| [6ccd45a] | 1697 | |
|---|
| 1698 | $flag = strtoupper($flag); |
|---|
| 1699 | $stripArr = ($field=='subject') ? array('Re: ','Fwd: ','Fw: ','"') : array('"'); |
|---|
| [4647e1b] | 1700 | |
|---|
| [4e17e6c] | 1701 | $c=count($a); |
|---|
| [3d695da] | 1702 | if ($c > 0) { |
|---|
| [4e17e6c] | 1703 | /* |
|---|
| 1704 | Strategy: |
|---|
| 1705 | First, we'll create an "index" array. |
|---|
| 1706 | Then, we'll use sort() on that array, |
|---|
| 1707 | and use that to sort the main array. |
|---|
| 1708 | */ |
|---|
| 1709 | |
|---|
| [3062b3e] | 1710 | // create "index" array |
|---|
| [6ccd45a] | 1711 | $index = array(); |
|---|
| [4e17e6c] | 1712 | reset($a); |
|---|
| [6ccd45a] | 1713 | while (list($key, $val)=each($a)) { |
|---|
| [4647e1b] | 1714 | |
|---|
| [6ccd45a] | 1715 | if ($field == 'timestamp') { |
|---|
| [11b80e9] | 1716 | $data = iil_StrToTime($val->date); |
|---|
| 1717 | if (!$data) { |
|---|
| [0d361b9] | 1718 | $data = $val->timestamp; |
|---|
| [49e5f72] | 1719 | } |
|---|
| [6ccd45a] | 1720 | } else { |
|---|
| [0d361b9] | 1721 | $data = $val->$field; |
|---|
| [6ccd45a] | 1722 | if (is_string($data)) { |
|---|
| 1723 | $data=strtoupper(str_replace($stripArr, '', $data)); |
|---|
| [49e5f72] | 1724 | } |
|---|
| [6ccd45a] | 1725 | } |
|---|
| [4e17e6c] | 1726 | $index[$key]=$data; |
|---|
| 1727 | } |
|---|
| 1728 | |
|---|
| 1729 | // sort index |
|---|
| [d5ff9276] | 1730 | $i = 0; |
|---|
| [6ccd45a] | 1731 | if ($flag == 'ASC') { |
|---|
| [49e5f72] | 1732 | asort($index); |
|---|
| 1733 | } else { |
|---|
| 1734 | arsort($index); |
|---|
| [6ccd45a] | 1735 | } |
|---|
| 1736 | |
|---|
| [4e17e6c] | 1737 | // form new array based on index |
|---|
| [6ccd45a] | 1738 | $result = array(); |
|---|
| [4e17e6c] | 1739 | reset($index); |
|---|
| [6ccd45a] | 1740 | while (list($key, $val)=each($index)) { |
|---|
| [9fee0ed] | 1741 | $result[$key]=$a[$key]; |
|---|
| [4e17e6c] | 1742 | $i++; |
|---|
| 1743 | } |
|---|
| 1744 | } |
|---|
| 1745 | |
|---|
| 1746 | return $result; |
|---|
| 1747 | } |
|---|
| 1748 | |
|---|
| [8234b4c] | 1749 | function iil_C_Expunge(&$conn, $mailbox, $messages=NULL) { |
|---|
| [49e5f72] | 1750 | |
|---|
| [6ccd45a] | 1751 | if (iil_C_Select($conn, $mailbox)) { |
|---|
| [3d695da] | 1752 | $c = 0; |
|---|
| [8234b4c] | 1753 | $command = $messages ? "UID EXPUNGE $messages" : "EXPUNGE"; |
|---|
| 1754 | |
|---|
| 1755 | iil_PutLine($conn->fp, "exp1 $command"); |
|---|
| [6ccd45a] | 1756 | do { |
|---|
| [49e5f72] | 1757 | $line=chop(iil_ReadLine($conn->fp, 100)); |
|---|
| [d5ff9276] | 1758 | if ($line[0] == '*') { |
|---|
| [49e5f72] | 1759 | $c++; |
|---|
| 1760 | } |
|---|
| [d2993ee] | 1761 | } while (!iil_StartsWith($line, 'exp1', true)); |
|---|
| [4e17e6c] | 1762 | |
|---|
| [6ccd45a] | 1763 | if (iil_ParseResult($line) == 0) { |
|---|
| 1764 | $conn->selected = ''; //state has changed, need to reselect |
|---|
| [4e17e6c] | 1765 | //$conn->exists-=$c; |
|---|
| 1766 | return $c; |
|---|
| 1767 | } |
|---|
| [6ccd45a] | 1768 | $conn->error = $line; |
|---|
| [4e17e6c] | 1769 | } |
|---|
| 1770 | |
|---|
| 1771 | return -1; |
|---|
| 1772 | } |
|---|
| 1773 | |
|---|
| [6ccd45a] | 1774 | function iil_C_ModFlag(&$conn, $mailbox, $messages, $flag, $mod) { |
|---|
| 1775 | if ($mod != '+' && $mod != '-') { |
|---|
| 1776 | return -1; |
|---|
| 1777 | } |
|---|
| 1778 | |
|---|
| 1779 | $fp = $conn->fp; |
|---|
| [5b3dd41] | 1780 | $flags = $GLOBALS['IMAP_FLAGS']; |
|---|
| [6ccd45a] | 1781 | |
|---|
| 1782 | $flag = strtoupper($flag); |
|---|
| 1783 | $flag = $flags[$flag]; |
|---|
| 1784 | |
|---|
| 1785 | if (iil_C_Select($conn, $mailbox)) { |
|---|
| [3d695da] | 1786 | $c = 0; |
|---|
| [aeed58d] | 1787 | iil_PutLine($fp, "flg UID STORE $messages " . $mod . "FLAGS (" . $flag . ")"); |
|---|
| [6ccd45a] | 1788 | do { |
|---|
| [4e17e6c] | 1789 | $line=chop(iil_ReadLine($fp, 100)); |
|---|
| [6ccd45a] | 1790 | if ($line[0] == '*') { |
|---|
| 1791 | $c++; |
|---|
| [49e5f72] | 1792 | } |
|---|
| [d2993ee] | 1793 | } while (!iil_StartsWith($line, 'flg', true)); |
|---|
| [520c36a] | 1794 | |
|---|
| [6ccd45a] | 1795 | if (iil_ParseResult($line) == 0) { |
|---|
| [4e17e6c] | 1796 | return $c; |
|---|
| 1797 | } |
|---|
| [6ccd45a] | 1798 | $conn->error = $line; |
|---|
| [4e17e6c] | 1799 | return -1; |
|---|
| 1800 | } |
|---|
| [6ccd45a] | 1801 | $conn->error = 'Select failed'; |
|---|
| 1802 | return -1; |
|---|
| [4e17e6c] | 1803 | } |
|---|
| 1804 | |
|---|
| [6ccd45a] | 1805 | function iil_C_Flag(&$conn, $mailbox, $messages, $flag) { |
|---|
| 1806 | return iil_C_ModFlag($conn, $mailbox, $messages, $flag, '+'); |
|---|
| [4e17e6c] | 1807 | } |
|---|
| 1808 | |
|---|
| [6ccd45a] | 1809 | function iil_C_Unflag(&$conn, $mailbox, $messages, $flag) { |
|---|
| 1810 | return iil_C_ModFlag($conn, $mailbox, $messages, $flag, '-'); |
|---|
| [4e17e6c] | 1811 | } |
|---|
| 1812 | |
|---|
| [6ccd45a] | 1813 | function iil_C_Delete(&$conn, $mailbox, $messages) { |
|---|
| 1814 | return iil_C_ModFlag($conn, $mailbox, $messages, 'DELETED', '+'); |
|---|
| [4e17e6c] | 1815 | } |
|---|
| 1816 | |
|---|
| [6ccd45a] | 1817 | function iil_C_Undelete(&$conn, $mailbox, $messages) { |
|---|
| 1818 | return iil_C_ModFlag($conn, $mailbox, $messages, 'DELETED', '-'); |
|---|
| [4e17e6c] | 1819 | } |
|---|
| 1820 | |
|---|
| [6ccd45a] | 1821 | function iil_C_Unseen(&$conn, $mailbox, $messages) { |
|---|
| 1822 | return iil_C_ModFlag($conn, $mailbox, $messages, 'SEEN', '-'); |
|---|
| [4e17e6c] | 1823 | } |
|---|
| 1824 | |
|---|
| [6ccd45a] | 1825 | function iil_C_Copy(&$conn, $messages, $from, $to) { |
|---|
| [4e17e6c] | 1826 | $fp = $conn->fp; |
|---|
| 1827 | |
|---|
| [6ccd45a] | 1828 | if (empty($from) || empty($to)) { |
|---|
| 1829 | return -1; |
|---|
| [49e5f72] | 1830 | } |
|---|
| [6ccd45a] | 1831 | |
|---|
| 1832 | if (iil_C_Select($conn, $from)) { |
|---|
| [4e17e6c] | 1833 | $c=0; |
|---|
| 1834 | |
|---|
| [aeed58d] | 1835 | iil_PutLine($fp, "cpy1 UID COPY $messages \"".iil_Escape($to)."\""); |
|---|
| [4e17e6c] | 1836 | $line=iil_ReadReply($fp); |
|---|
| 1837 | return iil_ParseResult($line); |
|---|
| [6ccd45a] | 1838 | } else { |
|---|
| [4e17e6c] | 1839 | return -1; |
|---|
| 1840 | } |
|---|
| 1841 | } |
|---|
| 1842 | |
|---|
| [6ccd45a] | 1843 | function iil_C_CountUnseen(&$conn, $folder) { |
|---|
| [99a4539] | 1844 | $index = iil_C_Search($conn, $folder, 'ALL UNSEEN'); |
|---|
| 1845 | if (is_array($index)) |
|---|
| 1846 | return count($index); |
|---|
| 1847 | return false; |
|---|
| [4e17e6c] | 1848 | } |
|---|
| 1849 | |
|---|
| [6ccd45a] | 1850 | function iil_C_UID2ID(&$conn, $folder, $uid) { |
|---|
| 1851 | if ($uid > 0) { |
|---|
| [4e17e6c] | 1852 | $id_a = iil_C_Search($conn, $folder, "UID $uid"); |
|---|
| [f7028b2] | 1853 | if (is_array($id_a) && count($id_a) == 1) { |
|---|
| [6ccd45a] | 1854 | return $id_a[0]; |
|---|
| [4e17e6c] | 1855 | } |
|---|
| 1856 | } |
|---|
| 1857 | return false; |
|---|
| 1858 | } |
|---|
| 1859 | |
|---|
| [6ccd45a] | 1860 | function iil_C_ID2UID(&$conn, $folder, $id) { |
|---|
| [99a4539] | 1861 | |
|---|
| [6ccd45a] | 1862 | if ($id == 0) { |
|---|
| 1863 | return -1; |
|---|
| [49e5f72] | 1864 | } |
|---|
| 1865 | $result = -1; |
|---|
| [6ccd45a] | 1866 | if (iil_C_Select($conn, $folder)) { |
|---|
| [99a4539] | 1867 | $key = 'fuid'; |
|---|
| 1868 | if (iil_PutLine($conn->fp, "$key FETCH $id (UID)")) { |
|---|
| [6ccd45a] | 1869 | do { |
|---|
| [99a4539] | 1870 | $line = chop(iil_ReadLine($conn->fp, 1024)); |
|---|
| [2f7405e3] | 1871 | if (preg_match("/^\* $id FETCH \(UID (.*)\)/i", $line, $r)) { |
|---|
| [6ccd45a] | 1872 | $result = $r[1]; |
|---|
| 1873 | } |
|---|
| [99a4539] | 1874 | } while (!iil_StartsWith($line, $key, true)); |
|---|
| [e6f3605] | 1875 | } |
|---|
| 1876 | } |
|---|
| 1877 | return $result; |
|---|
| 1878 | } |
|---|
| 1879 | |
|---|
| [6ccd45a] | 1880 | function iil_C_Search(&$conn, $folder, $criteria) { |
|---|
| [99a4539] | 1881 | |
|---|
| [6ccd45a] | 1882 | if (iil_C_Select($conn, $folder)) { |
|---|
| [99a4539] | 1883 | $data = ''; |
|---|
| [4e17e6c] | 1884 | |
|---|
| [49e5f72] | 1885 | $query = 'srch1 SEARCH ' . chop($criteria); |
|---|
| [99a4539] | 1886 | if (!iil_PutLineC($conn->fp, $query)) { |
|---|
| [25f779d] | 1887 | return false; |
|---|
| 1888 | } |
|---|
| [6ccd45a] | 1889 | do { |
|---|
| [99a4539] | 1890 | $line = trim(iil_ReadLine($conn->fp)); |
|---|
| 1891 | if (iil_StartsWith($line, '* SEARCH')) { |
|---|
| 1892 | $data .= substr($line, 8); |
|---|
| 1893 | } else if (preg_match('/^[0-9 ]+$/', $line)) { |
|---|
| 1894 | $data .= $line; |
|---|
| [4e17e6c] | 1895 | } |
|---|
| [d2993ee] | 1896 | } while (!iil_StartsWith($line, 'srch1', true)); |
|---|
| [697cc52] | 1897 | |
|---|
| [3d695da] | 1898 | $result_code = iil_ParseResult($line); |
|---|
| 1899 | if ($result_code == 0) { |
|---|
| [99a4539] | 1900 | return preg_split('/\s+/', $data, -1, PREG_SPLIT_NO_EMPTY); |
|---|
| [4e17e6c] | 1901 | } |
|---|
| [6ccd45a] | 1902 | $conn->error = 'iil_C_Search: ' . $line . "\n"; |
|---|
| [49e5f72] | 1903 | return false; |
|---|
| [4e17e6c] | 1904 | } |
|---|
| [6ccd45a] | 1905 | $conn->error = "iil_C_Search: Couldn't select \"$folder\"\n"; |
|---|
| 1906 | return false; |
|---|
| [4e17e6c] | 1907 | } |
|---|
| 1908 | |
|---|
| [6ccd45a] | 1909 | function iil_C_Move(&$conn, $messages, $from, $to) { |
|---|
| [3bfab3b] | 1910 | |
|---|
| 1911 | if (!$from || !$to) { |
|---|
| 1912 | return -1; |
|---|
| 1913 | } |
|---|
| [8234b4c] | 1914 | |
|---|
| 1915 | $r = iil_C_Copy($conn, $messages, $from, $to); |
|---|
| 1916 | |
|---|
| [3bfab3b] | 1917 | if ($r==0) { |
|---|
| 1918 | return iil_C_Delete($conn, $from, $messages); |
|---|
| 1919 | } |
|---|
| [6ccd45a] | 1920 | return $r; |
|---|
| [4e17e6c] | 1921 | } |
|---|
| 1922 | |
|---|
| [c2b197a] | 1923 | /** |
|---|
| 1924 | * Gets the delimiter, for example: |
|---|
| 1925 | * INBOX.foo -> . |
|---|
| 1926 | * INBOX/foo -> / |
|---|
| 1927 | * INBOX\foo -> \ |
|---|
| 1928 | * |
|---|
| 1929 | * @return mixed A delimiter (string), or false. |
|---|
| 1930 | * @param object $conn The current connection. |
|---|
| 1931 | * @see iil_Connect() |
|---|
| 1932 | */ |
|---|
| [6ccd45a] | 1933 | function iil_C_GetHierarchyDelimiter(&$conn) { |
|---|
| [030c848] | 1934 | |
|---|
| 1935 | global $my_prefs; |
|---|
| 1936 | |
|---|
| [6ccd45a] | 1937 | if ($conn->delimiter) { |
|---|
| [030c848] | 1938 | return $conn->delimiter; |
|---|
| 1939 | } |
|---|
| 1940 | if (!empty($my_prefs['delimiter'])) { |
|---|
| 1941 | return ($conn->delimiter = $my_prefs['delimiter']); |
|---|
| [6ccd45a] | 1942 | } |
|---|
| 1943 | |
|---|
| 1944 | $fp = $conn->fp; |
|---|
| [4e17e6c] | 1945 | $delimiter = false; |
|---|
| 1946 | |
|---|
| 1947 | //try (LIST "" ""), should return delimiter (RFC2060 Sec 6.3.8) |
|---|
| [49e5f72] | 1948 | if (!iil_PutLine($fp, 'ghd LIST "" ""')) { |
|---|
| [6ccd45a] | 1949 | return false; |
|---|
| [49e5f72] | 1950 | } |
|---|
| [6ccd45a] | 1951 | |
|---|
| 1952 | do { |
|---|
| [4e17e6c] | 1953 | $line=iil_ReadLine($fp, 500); |
|---|
| [c2b197a] | 1954 | if ($line[0] == '*') { |
|---|
| [4e17e6c] | 1955 | $line = rtrim($line); |
|---|
| [b7c1685] | 1956 | $a=iil_ExplodeQuotedString(' ', iil_UnEscape($line)); |
|---|
| [c2b197a] | 1957 | if ($a[0] == '*') { |
|---|
| [6ccd45a] | 1958 | $delimiter = str_replace('"', '', $a[count($a)-2]); |
|---|
| [49e5f72] | 1959 | } |
|---|
| [4e17e6c] | 1960 | } |
|---|
| [d2993ee] | 1961 | } while (!iil_StartsWith($line, 'ghd', true)); |
|---|
| [4e17e6c] | 1962 | |
|---|
| [6ccd45a] | 1963 | if (strlen($delimiter)>0) { |
|---|
| 1964 | return $delimiter; |
|---|
| 1965 | } |
|---|
| [b7c1685] | 1966 | |
|---|
| [4e17e6c] | 1967 | //if that fails, try namespace extension |
|---|
| 1968 | //try to fetch namespace data |
|---|
| [49e5f72] | 1969 | iil_PutLine($conn->fp, "ns1 NAMESPACE"); |
|---|
| [6ccd45a] | 1970 | do { |
|---|
| [4e17e6c] | 1971 | $line = iil_ReadLine($conn->fp, 1024); |
|---|
| [c2b197a] | 1972 | if (iil_StartsWith($line, '* NAMESPACE')) { |
|---|
| [4e17e6c] | 1973 | $i = 0; |
|---|
| [b7c1685] | 1974 | $line = iil_UnEscape($line); |
|---|
| [4e17e6c] | 1975 | $data = iil_ParseNamespace2(substr($line,11), $i, 0, 0); |
|---|
| 1976 | } |
|---|
| [d2993ee] | 1977 | } while (!iil_StartsWith($line, 'ns1', true)); |
|---|
| [4e17e6c] | 1978 | |
|---|
| [6ccd45a] | 1979 | if (!is_array($data)) { |
|---|
| 1980 | return false; |
|---|
| 1981 | } |
|---|
| 1982 | |
|---|
| [4e17e6c] | 1983 | //extract user space data (opposed to global/shared space) |
|---|
| 1984 | $user_space_data = $data[0]; |
|---|
| [6ccd45a] | 1985 | if (!is_array($user_space_data)) { |
|---|
| 1986 | return false; |
|---|
| 1987 | } |
|---|
| 1988 | |
|---|
| [4e17e6c] | 1989 | //get first element |
|---|
| 1990 | $first_userspace = $user_space_data[0]; |
|---|
| [6ccd45a] | 1991 | if (!is_array($first_userspace)) { |
|---|
| 1992 | return false; |
|---|
| [49e5f72] | 1993 | } |
|---|
| [6ccd45a] | 1994 | |
|---|
| [4e17e6c] | 1995 | //extract delimiter |
|---|
| 1996 | $delimiter = $first_userspace[1]; |
|---|
| 1997 | |
|---|
| 1998 | return $delimiter; |
|---|
| 1999 | } |
|---|
| 2000 | |
|---|
| [6ccd45a] | 2001 | function iil_C_ListMailboxes(&$conn, $ref, $mailbox) { |
|---|
| [4e17e6c] | 2002 | global $IGNORE_FOLDERS; |
|---|
| 2003 | |
|---|
| 2004 | $ignore = $IGNORE_FOLDERS[strtolower($conn->host)]; |
|---|
| 2005 | |
|---|
| 2006 | $fp = $conn->fp; |
|---|
| [e16938e] | 2007 | |
|---|
| [6ccd45a] | 2008 | if (empty($mailbox)) { |
|---|
| 2009 | $mailbox = '*'; |
|---|
| [e16938e] | 2010 | } |
|---|
| 2011 | |
|---|
| [6ccd45a] | 2012 | if (empty($ref) && $conn->rootdir) { |
|---|
| 2013 | $ref = $conn->rootdir; |
|---|
| 2014 | } |
|---|
| 2015 | |
|---|
| [e16938e] | 2016 | // send command |
|---|
| [49e5f72] | 2017 | if (!iil_PutLine($fp, "lmb LIST \"".$ref."\" \"".iil_Escape($mailbox)."\"")) { |
|---|
| [6ccd45a] | 2018 | return false; |
|---|
| [e16938e] | 2019 | } |
|---|
| [6ccd45a] | 2020 | |
|---|
| 2021 | $i = 0; |
|---|
| [e16938e] | 2022 | // get folder list |
|---|
| [6ccd45a] | 2023 | do { |
|---|
| 2024 | $line = iil_ReadLine($fp, 500); |
|---|
| [6bc59a7] | 2025 | $line = iil_MultLine($fp, $line, true); |
|---|
| [4e17e6c] | 2026 | |
|---|
| [6ccd45a] | 2027 | $a = explode(' ', $line); |
|---|
| 2028 | if (($line[0] == '*') && ($a[1] == 'LIST')) { |
|---|
| [4e17e6c] | 2029 | $line = rtrim($line); |
|---|
| [49e5f72] | 2030 | // split one line |
|---|
| [6ccd45a] | 2031 | $a = iil_ExplodeQuotedString(' ', $line); |
|---|
| [49e5f72] | 2032 | // last string is folder name |
|---|
| [6bc59a7] | 2033 | $folder = preg_replace(array('/^"/', '/"$/'), '', iil_UnEscape($a[count($a)-1])); |
|---|
| [6ccd45a] | 2034 | |
|---|
| [49e5f72] | 2035 | if (empty($ignore) || (!empty($ignore) |
|---|
| [2f7405e3] | 2036 | && !preg_match('/'.preg_quote(ignore, '/').'/i', $folder))) { |
|---|
| [49e5f72] | 2037 | $folders[$i] = $folder; |
|---|
| 2038 | } |
|---|
| [6ccd45a] | 2039 | |
|---|
| [49e5f72] | 2040 | // second from last is delimiter |
|---|
| 2041 | $delim = trim($a[count($a)-2], '"'); |
|---|
| 2042 | // is it a container? |
|---|
| 2043 | $i++; |
|---|
| [4e17e6c] | 2044 | } |
|---|
| [d2993ee] | 2045 | } while (!iil_StartsWith($line, 'lmb', true)); |
|---|
| [4e17e6c] | 2046 | |
|---|
| [6ccd45a] | 2047 | if (is_array($folders)) { |
|---|
| [e16938e] | 2048 | if (!empty($ref)) { |
|---|
| [49e5f72] | 2049 | // if rootdir was specified, make sure it's the first element |
|---|
| 2050 | // some IMAP servers (i.e. Courier) won't return it |
|---|
| 2051 | if ($ref[strlen($ref)-1]==$delim) |
|---|
| [e16938e] | 2052 | $ref = substr($ref, 0, strlen($ref)-1); |
|---|
| [49e5f72] | 2053 | if ($folders[0]!=$ref) |
|---|
| [e16938e] | 2054 | array_unshift($folders, $ref); |
|---|
| 2055 | } |
|---|
| 2056 | return $folders; |
|---|
| [49e5f72] | 2057 | } else if (iil_ParseResult($line) == 0) { |
|---|
| [4e17e6c] | 2058 | return array('INBOX'); |
|---|
| [6ccd45a] | 2059 | } else { |
|---|
| [4e17e6c] | 2060 | $conn->error = $line; |
|---|
| 2061 | return false; |
|---|
| 2062 | } |
|---|
| 2063 | } |
|---|
| 2064 | |
|---|
| [6ccd45a] | 2065 | function iil_C_ListSubscribed(&$conn, $ref, $mailbox) { |
|---|
| [4e17e6c] | 2066 | global $IGNORE_FOLDERS; |
|---|
| 2067 | |
|---|
| 2068 | $ignore = $IGNORE_FOLDERS[strtolower($conn->host)]; |
|---|
| 2069 | |
|---|
| 2070 | $fp = $conn->fp; |
|---|
| [6ccd45a] | 2071 | if (empty($mailbox)) { |
|---|
| 2072 | $mailbox = '*'; |
|---|
| 2073 | } |
|---|
| 2074 | if (empty($ref) && $conn->rootdir) { |
|---|
| 2075 | $ref = $conn->rootdir; |
|---|
| 2076 | } |
|---|
| [4e17e6c] | 2077 | $folders = array(); |
|---|
| 2078 | |
|---|
| [49e5f72] | 2079 | // send command |
|---|
| 2080 | if (!iil_PutLine($fp, 'lsb LSUB "' . $ref . '" "' . iil_Escape($mailbox).'"')) { |
|---|
| [4e17e6c] | 2081 | $conn->error = "Couldn't send LSUB command\n"; |
|---|
| 2082 | return false; |
|---|
| 2083 | } |
|---|
| [6ccd45a] | 2084 | |
|---|
| 2085 | $i = 0; |
|---|
| 2086 | |
|---|
| [49e5f72] | 2087 | // get folder list |
|---|
| [6ccd45a] | 2088 | do { |
|---|
| 2089 | $line = iil_ReadLine($fp, 500); |
|---|
| [6bc59a7] | 2090 | $line = iil_MultLine($fp, $line, true); |
|---|
| [6ccd45a] | 2091 | $a = explode(' ', $line); |
|---|
| 2092 | |
|---|
| 2093 | if (($line[0] == '*') && ($a[1] == 'LSUB' || $a[1] == 'LIST')) { |
|---|
| [4e17e6c] | 2094 | $line = rtrim($line); |
|---|
| [6ccd45a] | 2095 | |
|---|
| [49e5f72] | 2096 | // split one line |
|---|
| [6ccd45a] | 2097 | $a = iil_ExplodeQuotedString(' ', $line); |
|---|
| [49e5f72] | 2098 | // last string is folder name |
|---|
| [6bc59a7] | 2099 | $folder = preg_replace(array('/^"/', '/"$/'), '', iil_UnEscape($a[count($a)-1])); |
|---|
| [2ff8af1] | 2100 | |
|---|
| [6ccd45a] | 2101 | if ((!in_array($folder, $folders)) && (empty($ignore) |
|---|
| [2f7405e3] | 2102 | || (!empty($ignore) && !preg_match('/'.preg_quote(ignore, '/').'/i', $folder)))) { |
|---|
| [6ccd45a] | 2103 | $folders[$i] = $folder; |
|---|
| [49e5f72] | 2104 | } |
|---|
| [6ccd45a] | 2105 | |
|---|
| [49e5f72] | 2106 | // second from last is delimiter |
|---|
| 2107 | $delim = trim($a[count($a)-2], '"'); |
|---|
| [6ccd45a] | 2108 | |
|---|
| [49e5f72] | 2109 | // is it a container? |
|---|
| 2110 | $i++; |
|---|
| [4e17e6c] | 2111 | } |
|---|
| [d2993ee] | 2112 | } while (!iil_StartsWith($line, 'lsb', true)); |
|---|
| [4e17e6c] | 2113 | |
|---|
| [6ccd45a] | 2114 | if (is_array($folders)) { |
|---|
| [49e5f72] | 2115 | if (!empty($ref)) { |
|---|
| 2116 | // if rootdir was specified, make sure it's the first element |
|---|
| 2117 | // some IMAP servers (i.e. Courier) won't return it |
|---|
| 2118 | if ($ref[strlen($ref)-1]==$delim) { |
|---|
| 2119 | $ref = substr($ref, 0, strlen($ref)-1); |
|---|
| 2120 | } |
|---|
| 2121 | if ($folders[0]!=$ref) { |
|---|
| 2122 | array_unshift($folders, $ref); |
|---|
| 2123 | } |
|---|
| 2124 | } |
|---|
| 2125 | return $folders; |
|---|
| [4e17e6c] | 2126 | } |
|---|
| [6ccd45a] | 2127 | $conn->error = $line; |
|---|
| 2128 | return false; |
|---|
| [4e17e6c] | 2129 | } |
|---|
| 2130 | |
|---|
| [6ccd45a] | 2131 | function iil_C_Subscribe(&$conn, $folder) { |
|---|
| [4e17e6c] | 2132 | $fp = $conn->fp; |
|---|
| 2133 | |
|---|
| [49e5f72] | 2134 | $query = 'sub1 SUBSCRIBE "' . iil_Escape($folder). '"'; |
|---|
| 2135 | iil_PutLine($fp, $query); |
|---|
| [3d695da] | 2136 | |
|---|
| [9b90b33] | 2137 | $line = trim(iil_ReadLine($fp, 10000)); |
|---|
| [4e17e6c] | 2138 | return iil_ParseResult($line); |
|---|
| 2139 | } |
|---|
| 2140 | |
|---|
| [6ccd45a] | 2141 | function iil_C_UnSubscribe(&$conn, $folder) { |
|---|
| [4e17e6c] | 2142 | $fp = $conn->fp; |
|---|
| 2143 | |
|---|
| [49e5f72] | 2144 | $query = 'usub1 UNSUBSCRIBE "' . iil_Escape($folder) . '"'; |
|---|
| 2145 | iil_PutLine($fp, $query); |
|---|
| [6ccd45a] | 2146 | |
|---|
| [9b90b33] | 2147 | $line = trim(iil_ReadLine($fp, 10000)); |
|---|
| [4e17e6c] | 2148 | return iil_ParseResult($line); |
|---|
| 2149 | } |
|---|
| 2150 | |
|---|
| [7a229b9] | 2151 | function iil_C_FetchMIMEHeaders(&$conn, $mailbox, $id, $parts) { |
|---|
| 2152 | |
|---|
| 2153 | $fp = $conn->fp; |
|---|
| 2154 | |
|---|
| 2155 | if (!iil_C_Select($conn, $mailbox)) { |
|---|
| 2156 | return false; |
|---|
| 2157 | } |
|---|
| 2158 | |
|---|
| 2159 | $result = false; |
|---|
| 2160 | $parts = (array) $parts; |
|---|
| 2161 | $key = 'fmh0'; |
|---|
| 2162 | $peeks = ''; |
|---|
| 2163 | $idx = 0; |
|---|
| 2164 | |
|---|
| 2165 | // format request |
|---|
| 2166 | foreach($parts as $part) |
|---|
| [803b1b7] | 2167 | $peeks[] = "BODY.PEEK[$part.MIME]"; |
|---|
| [7a229b9] | 2168 | |
|---|
| 2169 | $request = "$key FETCH $id (" . implode(' ', $peeks) . ')'; |
|---|
| 2170 | |
|---|
| 2171 | // send request |
|---|
| 2172 | if (!iil_PutLine($fp, $request)) { |
|---|
| 2173 | return false; |
|---|
| 2174 | } |
|---|
| 2175 | |
|---|
| 2176 | do { |
|---|
| 2177 | $line = iil_ReadLine($fp, 1000); |
|---|
| 2178 | $line = iil_MultLine($fp, $line); |
|---|
| 2179 | |
|---|
| [157fcb8] | 2180 | if (preg_match('/BODY\[([0-9\.]+)\.MIME\]/', $line, $matches)) { |
|---|
| [7a229b9] | 2181 | $idx = $matches[1]; |
|---|
| [157fcb8] | 2182 | $result[$idx] = preg_replace('/^(\* '.$id.' FETCH \()?\s*BODY\['.$idx.'\.MIME\]\s+/', '', $line); |
|---|
| [7a229b9] | 2183 | $result[$idx] = trim($result[$idx], '"'); |
|---|
| 2184 | $result[$idx] = rtrim($result[$idx], "\t\r\n\0\x0B"); |
|---|
| 2185 | } |
|---|
| 2186 | } while (!iil_StartsWith($line, $key, true)); |
|---|
| 2187 | |
|---|
| 2188 | return $result; |
|---|
| 2189 | } |
|---|
| 2190 | |
|---|
| [40dfead] | 2191 | function iil_C_FetchPartHeader(&$conn, $mailbox, $id, $is_uid=false, $part=NULL) { |
|---|
| [ceb52fe0] | 2192 | |
|---|
| [f14ac8c] | 2193 | $part = empty($part) ? 'HEADER' : $part.'.MIME'; |
|---|
| 2194 | |
|---|
| [40dfead] | 2195 | return iil_C_HandlePartBody($conn, $mailbox, $id, $is_uid, $part); |
|---|
| [4e17e6c] | 2196 | } |
|---|
| 2197 | |
|---|
| [40dfead] | 2198 | function iil_C_HandlePartBody(&$conn, $mailbox, $id, $is_uid=false, $part='', $encoding=NULL, $print=NULL, $file=NULL) { |
|---|
| [49e5f72] | 2199 | |
|---|
| [6ccd45a] | 2200 | $fp = $conn->fp; |
|---|
| 2201 | $result = false; |
|---|
| [9b9f267] | 2202 | |
|---|
| [1c5be6f] | 2203 | switch ($encoding) { |
|---|
| 2204 | case 'base64': |
|---|
| 2205 | $mode = 1; |
|---|
| 2206 | break; |
|---|
| 2207 | case 'quoted-printable': |
|---|
| 2208 | $mode = 2; |
|---|
| 2209 | break; |
|---|
| 2210 | case 'x-uuencode': |
|---|
| 2211 | case 'x-uue': |
|---|
| 2212 | case 'uue': |
|---|
| 2213 | case 'uuencode': |
|---|
| 2214 | $mode = 3; |
|---|
| 2215 | break; |
|---|
| 2216 | default: |
|---|
| 2217 | $mode = 0; |
|---|
| 2218 | } |
|---|
| 2219 | |
|---|
| [6ccd45a] | 2220 | if (iil_C_Select($conn, $mailbox)) { |
|---|
| [49e5f72] | 2221 | $reply_key = '* ' . $id; |
|---|
| [4769ead] | 2222 | |
|---|
| [49e5f72] | 2223 | // format request |
|---|
| [1c5be6f] | 2224 | $key = 'ftch0'; |
|---|
| [40dfead] | 2225 | $request = $key . ($is_uid ? ' UID' : '') . " FETCH $id (BODY.PEEK[$part])"; |
|---|
| [49e5f72] | 2226 | // send request |
|---|
| 2227 | if (!iil_PutLine($fp, $request)) { |
|---|
| [6ccd45a] | 2228 | return false; |
|---|
| [49e5f72] | 2229 | } |
|---|
| [1c5be6f] | 2230 | |
|---|
| [49e5f72] | 2231 | // receive reply line |
|---|
| 2232 | do { |
|---|
| 2233 | $line = chop(iil_ReadLine($fp, 1000)); |
|---|
| 2234 | $a = explode(' ', $line); |
|---|
| [4769ead] | 2235 | } while (!($end = iil_StartsWith($line, $key, true)) && $a[2] != 'FETCH'); |
|---|
| [49e5f72] | 2236 | $len = strlen($line); |
|---|
| [d2993ee] | 2237 | |
|---|
| 2238 | // handle empty "* X FETCH ()" response |
|---|
| 2239 | if ($line[$len-1] == ')' && $line[$len-2] != '(') { |
|---|
| [49e5f72] | 2240 | // one line response, get everything between first and last quotes |
|---|
| 2241 | if (substr($line, -4, 3) == 'NIL') { |
|---|
| 2242 | // NIL response |
|---|
| 2243 | $result = ''; |
|---|
| 2244 | } else { |
|---|
| 2245 | $from = strpos($line, '"') + 1; |
|---|
| 2246 | $to = strrpos($line, '"'); |
|---|
| 2247 | $len = $to - $from; |
|---|
| 2248 | $result = substr($line, $from, $len); |
|---|
| 2249 | } |
|---|
| [5f571eb] | 2250 | |
|---|
| [1c5be6f] | 2251 | if ($mode == 1) |
|---|
| 2252 | $result = base64_decode($result); |
|---|
| 2253 | else if ($mode == 2) |
|---|
| 2254 | $result = quoted_printable_decode($result); |
|---|
| 2255 | else if ($mode == 3) |
|---|
| 2256 | $result = convert_uudecode($result); |
|---|
| 2257 | |
|---|
| [49e5f72] | 2258 | } else if ($line[$len-1] == '}') { |
|---|
| 2259 | //multi-line request, find sizes of content and receive that many bytes |
|---|
| 2260 | $from = strpos($line, '{') + 1; |
|---|
| 2261 | $to = strrpos($line, '}'); |
|---|
| 2262 | $len = $to - $from; |
|---|
| 2263 | $sizeStr = substr($line, $from, $len); |
|---|
| 2264 | $bytes = (int)$sizeStr; |
|---|
| [5801afd] | 2265 | $prev = ''; |
|---|
| [4bac289] | 2266 | |
|---|
| [81b573d] | 2267 | while ($bytes > 0) { |
|---|
| 2268 | $line = iil_ReadLine($fp, 1024); |
|---|
| [49e5f72] | 2269 | $len = strlen($line); |
|---|
| [6ccd45a] | 2270 | |
|---|
| [81b573d] | 2271 | if ($len > $bytes) { |
|---|
| 2272 | $line = substr($line, 0, $bytes); |
|---|
| [4bac289] | 2273 | $len = strlen($line); |
|---|
| [49e5f72] | 2274 | } |
|---|
| [4bac289] | 2275 | $bytes -= $len; |
|---|
| [81b573d] | 2276 | |
|---|
| [49e5f72] | 2277 | if ($mode == 1) { |
|---|
| [eebfa5e] | 2278 | $line = rtrim($line, "\t\r\n\0\x0B"); |
|---|
| [5801afd] | 2279 | // create chunks with proper length for base64 decoding |
|---|
| 2280 | $line = $prev.$line; |
|---|
| 2281 | $length = strlen($line); |
|---|
| 2282 | if ($length % 4) { |
|---|
| 2283 | $length = floor($length / 4) * 4; |
|---|
| 2284 | $prev = substr($line, $length); |
|---|
| 2285 | $line = substr($line, 0, $length); |
|---|
| 2286 | } |
|---|
| 2287 | else |
|---|
| 2288 | $prev = ''; |
|---|
| 2289 | |
|---|
| [81b573d] | 2290 | if ($file) |
|---|
| 2291 | fwrite($file, base64_decode($line)); |
|---|
| [1c5be6f] | 2292 | else if ($print) |
|---|
| [81b573d] | 2293 | echo base64_decode($line); |
|---|
| [1c5be6f] | 2294 | else |
|---|
| 2295 | $result .= base64_decode($line); |
|---|
| 2296 | } else if ($mode == 2) { |
|---|
| [eebfa5e] | 2297 | $line = rtrim($line, "\t\r\0\x0B"); |
|---|
| [1c5be6f] | 2298 | if ($file) |
|---|
| 2299 | fwrite($file, quoted_printable_decode($line)); |
|---|
| 2300 | else if ($print) |
|---|
| 2301 | echo quoted_printable_decode($line); |
|---|
| 2302 | else |
|---|
| 2303 | $result .= quoted_printable_decode($line); |
|---|
| 2304 | } else if ($mode == 3) { |
|---|
| [eebfa5e] | 2305 | $line = rtrim($line, "\t\r\n\0\x0B"); |
|---|
| [b31a0af] | 2306 | if ($line == 'end' || preg_match('/^begin\s+[0-7]+\s+.+$/', $line)) |
|---|
| [6748044] | 2307 | continue; |
|---|
| [1c5be6f] | 2308 | if ($file) |
|---|
| 2309 | fwrite($file, convert_uudecode($line)); |
|---|
| 2310 | else if ($print) |
|---|
| 2311 | echo convert_uudecode($line); |
|---|
| 2312 | else |
|---|
| 2313 | $result .= convert_uudecode($line); |
|---|
| 2314 | } else { |
|---|
| [eebfa5e] | 2315 | $line = rtrim($line, "\t\r\n\0\x0B"); |
|---|
| [1c5be6f] | 2316 | if ($file) |
|---|
| 2317 | fwrite($file, $line . "\n"); |
|---|
| 2318 | else if ($print) |
|---|
| 2319 | echo $line . "\n"; |
|---|
| 2320 | else |
|---|
| 2321 | $result .= $line . "\n"; |
|---|
| [81b573d] | 2322 | } |
|---|
| [49e5f72] | 2323 | } |
|---|
| 2324 | } |
|---|
| [eebfa5e] | 2325 | |
|---|
| [81b573d] | 2326 | // read in anything up until last line |
|---|
| [4769ead] | 2327 | if (!$end) |
|---|
| 2328 | do { |
|---|
| 2329 | $line = iil_ReadLine($fp, 1024); |
|---|
| 2330 | } while (!iil_StartsWith($line, $key, true)); |
|---|
| [5f571eb] | 2331 | |
|---|
| [49e5f72] | 2332 | if ($result) { |
|---|
| 2333 | $result = rtrim($result, "\t\r\n\0\x0B"); |
|---|
| [81b573d] | 2334 | if ($file) { |
|---|
| 2335 | fwrite($file, $result); |
|---|
| [1c5be6f] | 2336 | } else if ($print) { |
|---|
| 2337 | echo $result; |
|---|
| 2338 | } else |
|---|
| 2339 | return $result; // substr($result, 0, strlen($result)-1); |
|---|
| [5f571eb] | 2340 | |
|---|
| 2341 | return true; |
|---|
| [49e5f72] | 2342 | } |
|---|
| [4e17e6c] | 2343 | } |
|---|
| 2344 | |
|---|
| [81b573d] | 2345 | return false; |
|---|
| [4e17e6c] | 2346 | } |
|---|
| 2347 | |
|---|
| [6ccd45a] | 2348 | function iil_C_CreateFolder(&$conn, $folder) { |
|---|
| [4e17e6c] | 2349 | $fp = $conn->fp; |
|---|
| [49e5f72] | 2350 | if (iil_PutLine($fp, 'c CREATE "' . iil_Escape($folder) . '"')) { |
|---|
| [6ccd45a] | 2351 | do { |
|---|
| [4e17e6c] | 2352 | $line=iil_ReadLine($fp, 300); |
|---|
| [9db29ee] | 2353 | } while (!iil_StartsWith($line, 'c ', true)); |
|---|
| [6ccd45a] | 2354 | return (iil_ParseResult($line) == 0); |
|---|
| [4e17e6c] | 2355 | } |
|---|
| [6ccd45a] | 2356 | return false; |
|---|
| [4e17e6c] | 2357 | } |
|---|
| 2358 | |
|---|
| [6ccd45a] | 2359 | function iil_C_RenameFolder(&$conn, $from, $to) { |
|---|
| [4e17e6c] | 2360 | $fp = $conn->fp; |
|---|
| [49e5f72] | 2361 | if (iil_PutLine($fp, 'r RENAME "' . iil_Escape($from) . '" "' . iil_Escape($to) . '"')) { |
|---|
| [6ccd45a] | 2362 | do { |
|---|
| [3d695da] | 2363 | $line = iil_ReadLine($fp, 300); |
|---|
| [9db29ee] | 2364 | } while (!iil_StartsWith($line, 'r ', true)); |
|---|
| [6ccd45a] | 2365 | return (iil_ParseResult($line) == 0); |
|---|
| 2366 | } |
|---|
| [49e5f72] | 2367 | return false; |
|---|
| [4e17e6c] | 2368 | } |
|---|
| 2369 | |
|---|
| [6ccd45a] | 2370 | function iil_C_DeleteFolder(&$conn, $folder) { |
|---|
| [4e17e6c] | 2371 | $fp = $conn->fp; |
|---|
| [49e5f72] | 2372 | if (iil_PutLine($fp, 'd DELETE "' . iil_Escape($folder). '"')) { |
|---|
| [6ccd45a] | 2373 | do { |
|---|
| [4e17e6c] | 2374 | $line=iil_ReadLine($fp, 300); |
|---|
| [9db29ee] | 2375 | } while (!iil_StartsWith($line, 'd ', true)); |
|---|
| [6ccd45a] | 2376 | return (iil_ParseResult($line) == 0); |
|---|
| [4e17e6c] | 2377 | } |
|---|
| [6ccd45a] | 2378 | return false; |
|---|
| [4e17e6c] | 2379 | } |
|---|
| 2380 | |
|---|
| [6ccd45a] | 2381 | function iil_C_Append(&$conn, $folder, &$message) { |
|---|
| [3d695da] | 2382 | if (!$folder) { |
|---|
| [49e5f72] | 2383 | return false; |
|---|
| 2384 | } |
|---|
| [4e17e6c] | 2385 | $fp = $conn->fp; |
|---|
| 2386 | |
|---|
| [6ccd45a] | 2387 | $message = str_replace("\r", '', $message); |
|---|
| [4e17e6c] | 2388 | $message = str_replace("\n", "\r\n", $message); |
|---|
| 2389 | |
|---|
| 2390 | $len = strlen($message); |
|---|
| [3d695da] | 2391 | if (!$len) { |
|---|
| [49e5f72] | 2392 | return false; |
|---|
| [3d695da] | 2393 | } |
|---|
| [49e5f72] | 2394 | |
|---|
| [9db29ee] | 2395 | $request = 'a APPEND "' . iil_Escape($folder) .'" (\\Seen) {' . $len . '}'; |
|---|
| [6ccd45a] | 2396 | |
|---|
| [49e5f72] | 2397 | if (iil_PutLine($fp, $request)) { |
|---|
| [9db29ee] | 2398 | $line = iil_ReadLine($fp, 100); |
|---|
| 2399 | $sent = iil_PutLine($fp, $message); |
|---|
| [6ccd45a] | 2400 | do { |
|---|
| [9db29ee] | 2401 | $line=iil_ReadLine($fp); |
|---|
| 2402 | } while (!iil_StartsWith($line, 'a ', true)); |
|---|
| [4e17e6c] | 2403 | |
|---|
| [6ccd45a] | 2404 | $result = (iil_ParseResult($line) == 0); |
|---|
| 2405 | if (!$result) { |
|---|
| [06583c3] | 2406 | $conn->error .= $line . "\n"; |
|---|
| [49e5f72] | 2407 | } |
|---|
| [4e17e6c] | 2408 | return $result; |
|---|
| 2409 | } |
|---|
| [49e5f72] | 2410 | |
|---|
| [6ccd45a] | 2411 | $conn->error .= "Couldn't send command \"$request\"\n"; |
|---|
| [49e5f72] | 2412 | return false; |
|---|
| [4e17e6c] | 2413 | } |
|---|
| 2414 | |
|---|
| [6ccd45a] | 2415 | function iil_C_AppendFromFile(&$conn, $folder, $path) { |
|---|
| 2416 | if (!$folder) { |
|---|
| 2417 | return false; |
|---|
| 2418 | } |
|---|
| 2419 | |
|---|
| [4e17e6c] | 2420 | //open message file |
|---|
| 2421 | $in_fp = false; |
|---|
| [6ccd45a] | 2422 | if (file_exists(realpath($path))) { |
|---|
| [49e5f72] | 2423 | $in_fp = fopen($path, 'r'); |
|---|
| 2424 | } |
|---|
| [6ccd45a] | 2425 | if (!$in_fp) { |
|---|
| [8c2e58b] | 2426 | $conn->error .= "Couldn't open $path for reading\n"; |
|---|
| [4e17e6c] | 2427 | return false; |
|---|
| 2428 | } |
|---|
| 2429 | |
|---|
| [6ccd45a] | 2430 | $fp = $conn->fp; |
|---|
| [4e17e6c] | 2431 | $len = filesize($path); |
|---|
| [6ccd45a] | 2432 | if (!$len) { |
|---|
| [49e5f72] | 2433 | return false; |
|---|
| [6ccd45a] | 2434 | } |
|---|
| 2435 | |
|---|
| [4e17e6c] | 2436 | //send APPEND command |
|---|
| [9db29ee] | 2437 | $request = 'a APPEND "' . iil_Escape($folder) . '" (\\Seen) {' . $len . '}'; |
|---|
| [4e17e6c] | 2438 | $bytes_sent = 0; |
|---|
| [49e5f72] | 2439 | if (iil_PutLine($fp, $request)) { |
|---|
| [3d695da] | 2440 | $line = iil_ReadLine($fp, 100); |
|---|
| [4e17e6c] | 2441 | |
|---|
| 2442 | //send file |
|---|
| [6ccd45a] | 2443 | while (!feof($in_fp)) { |
|---|
| [3d695da] | 2444 | $buffer = fgets($in_fp, 4096); |
|---|
| [4e17e6c] | 2445 | $bytes_sent += strlen($buffer); |
|---|
| [49e5f72] | 2446 | iil_PutLine($fp, $buffer, false); |
|---|
| [4e17e6c] | 2447 | } |
|---|
| 2448 | fclose($in_fp); |
|---|
| 2449 | |
|---|
| [9db29ee] | 2450 | iil_PutLine($fp, ''); // \r\n |
|---|
| [4e17e6c] | 2451 | |
|---|
| 2452 | //read response |
|---|
| [6ccd45a] | 2453 | do { |
|---|
| [9db29ee] | 2454 | $line = iil_ReadLine($fp); |
|---|
| 2455 | } while (!iil_StartsWith($line, 'a ', true)); |
|---|
| [4e17e6c] | 2456 | |
|---|
| [6ccd45a] | 2457 | $result = (iil_ParseResult($line) == 0); |
|---|
| 2458 | if (!$result) { |
|---|
| [06583c3] | 2459 | $conn->error .= $line . "\n"; |
|---|
| [6ccd45a] | 2460 | } |
|---|
| [49e5f72] | 2461 | |
|---|
| 2462 | return $result; |
|---|
| [4e17e6c] | 2463 | } |
|---|
| [49e5f72] | 2464 | |
|---|
| [6ccd45a] | 2465 | $conn->error .= "Couldn't send command \"$request\"\n"; |
|---|
| 2466 | return false; |
|---|
| [4e17e6c] | 2467 | } |
|---|
| 2468 | |
|---|
| [40dfead] | 2469 | function iil_C_FetchStructureString(&$conn, $folder, $id, $is_uid=false) { |
|---|
| [6ccd45a] | 2470 | $fp = $conn->fp; |
|---|
| 2471 | $result = false; |
|---|
| [02548b9] | 2472 | |
|---|
| [6ccd45a] | 2473 | if (iil_C_Select($conn, $folder)) { |
|---|
| 2474 | $key = 'F1247'; |
|---|
| [e1367cf] | 2475 | |
|---|
| [40dfead] | 2476 | if (iil_PutLine($fp, $key . ($is_uid ? ' UID' : '') ." FETCH $id (BODYSTRUCTURE)")) { |
|---|
| [6ccd45a] | 2477 | do { |
|---|
| [02548b9] | 2478 | $line = iil_ReadLine($fp, 5000); |
|---|
| 2479 | $line = iil_MultLine($fp, $line); |
|---|
| [5f571eb] | 2480 | if (!preg_match("/^$key/", $line)) |
|---|
| [e1367cf] | 2481 | $result .= $line; |
|---|
| [9db29ee] | 2482 | } while (!iil_StartsWith($line, $key, true)); |
|---|
| [02548b9] | 2483 | |
|---|
| [5f571eb] | 2484 | $result = trim(substr($result, strpos($result, 'BODYSTRUCTURE')+13, -1)); |
|---|
| [4e17e6c] | 2485 | } |
|---|
| 2486 | } |
|---|
| 2487 | return $result; |
|---|
| 2488 | } |
|---|
| 2489 | |
|---|
| [6ccd45a] | 2490 | function iil_C_GetQuota(&$conn) { |
|---|
| [4e17e6c] | 2491 | /* |
|---|
| [6ccd45a] | 2492 | * GETQUOTAROOT "INBOX" |
|---|
| 2493 | * QUOTAROOT INBOX user/rchijiiwa1 |
|---|
| 2494 | * QUOTA user/rchijiiwa1 (STORAGE 654 9765) |
|---|
| [78928070] | 2495 | * OK Completed |
|---|
| [6ccd45a] | 2496 | */ |
|---|
| 2497 | $fp = $conn->fp; |
|---|
| 2498 | $result = false; |
|---|
| [78928070] | 2499 | $quota_lines = array(); |
|---|
| [4e17e6c] | 2500 | |
|---|
| [78928070] | 2501 | // get line(s) containing quota info |
|---|
| [49e5f72] | 2502 | if (iil_PutLine($fp, 'QUOT1 GETQUOTAROOT "INBOX"')) { |
|---|
| [6ccd45a] | 2503 | do { |
|---|
| [4e17e6c] | 2504 | $line=chop(iil_ReadLine($fp, 5000)); |
|---|
| [06583c3] | 2505 | if (iil_StartsWith($line, '* QUOTA ')) { |
|---|
| [78928070] | 2506 | $quota_lines[] = $line; |
|---|
| [49e5f72] | 2507 | } |
|---|
| [d2993ee] | 2508 | } while (!iil_StartsWith($line, 'QUOT1', true)); |
|---|
| [4e17e6c] | 2509 | } |
|---|
| 2510 | |
|---|
| [78928070] | 2511 | // return false if not found, parse if found |
|---|
| 2512 | $min_free = PHP_INT_MAX; |
|---|
| 2513 | foreach ($quota_lines as $key => $quota_line) { |
|---|
| [2f7405e3] | 2514 | $quota_line = preg_replace('/[()]/', '', $quota_line); |
|---|
| [3d695da] | 2515 | $parts = explode(' ', $quota_line); |
|---|
| [06583c3] | 2516 | $storage_part = array_search('STORAGE', $parts); |
|---|
| [78928070] | 2517 | |
|---|
| 2518 | if (!$storage_part) continue; |
|---|
| 2519 | |
|---|
| 2520 | $used = intval($parts[$storage_part+1]); |
|---|
| 2521 | $total = intval($parts[$storage_part+2]); |
|---|
| 2522 | $free = $total - $used; |
|---|
| 2523 | |
|---|
| 2524 | // return lowest available space from all quotas |
|---|
| 2525 | if ($free < $min_free) { |
|---|
| 2526 | $min_free = $free; |
|---|
| 2527 | $result['used'] = $used; |
|---|
| 2528 | $result['total'] = $total; |
|---|
| 2529 | $result['percent'] = min(100, round(($used/max(1,$total))*100)); |
|---|
| [6ccd45a] | 2530 | $result['free'] = 100 - $result['percent']; |
|---|
| [4e17e6c] | 2531 | } |
|---|
| 2532 | } |
|---|
| 2533 | return $result; |
|---|
| 2534 | } |
|---|
| 2535 | |
|---|
| [6ccd45a] | 2536 | function iil_C_ClearFolder(&$conn, $folder) { |
|---|
| [4e17e6c] | 2537 | $num_in_trash = iil_C_CountMessages($conn, $folder); |
|---|
| [6ccd45a] | 2538 | if ($num_in_trash > 0) { |
|---|
| [aeed58d] | 2539 | iil_C_Delete($conn, $folder, '1:*'); |
|---|
| [49e5f72] | 2540 | } |
|---|
| [4e17e6c] | 2541 | return (iil_C_Expunge($conn, $folder) >= 0); |
|---|
| 2542 | } |
|---|
| [e16938e] | 2543 | |
|---|
| [8150d28] | 2544 | ?> |
|---|