You're right Bunzo 8)
[squirrelmail.git] / plugins / filters / sqimap_read_data.php
1 /******************************************************************************
2 ** Reads the output from the IMAP stream. If handle_errors is set to true,
3 ** this will also handle all errors that are received. If it is not set,
4 ** the errors will be sent back through $response and $message
5 ******************************************************************************/
6 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
7 global $color, $squirrelmail_language, $imap_general_debug;
8
9 $data = array();
10 $size = 0;
11
12 do {
13 $read = fgets($imap_stream, 9096);
14 if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
15 break; // found end of reply
16 }
17
18 // Continue if needed for this single line
19 while (strpos($read, "\n") === false) {
20 $read .= fgets($imap_stream, 9096);
21 }
22
23 $data[] = $read;
24
25 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
26 $size = $regs[1];
27 if ($imap_general_debug) {
28 echo "<small><tt><font color=\"#CC0000\">Size is $size</font></tt></small><br>\n";
29 }
30
31 $total_size = 0;
32 do {
33 $read = fgets($imap_stream, 9096);
34 if ($imap_general_debug) {
35 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
36 flush();
37 }
38 $data[] = $read;
39 $total_size += strlen($read);
40 } while ($total_size < $size);
41
42 $size = 0;
43 }
44 // For debugging purposes
45 if ($imap_general_debug) {
46 echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
47 flush();
48 }
49 } while (true);
50
51 $response = $regs[1];
52 $message = trim($regs[2]);
53
54 if ($imap_general_debug) echo '--<br>';
55
56 if ($handle_errors == false)
57 return $data;
58
59 if ($response == 'NO') {
60 // ignore this error from m$ exchange, it is not fatal (aka bug)
61 if (strstr($message, 'command resulted in') === false) {
62 set_up_language($squirrelmail_language);
63 echo "<br><b><font color=$color[2]>\n";
64 echo _("ERROR : Could not complete request.");
65 echo "</b><br>\n";
66 echo _("Reason Given: ");
67 echo $message . "</font><br>\n";
68 exit;
69 }
70 } else if ($response == 'BAD') {
71 set_up_language($squirrelmail_language);
72 echo "<br><b><font color=$color[2]>\n";
73 echo _("ERROR : Bad or malformed request.");
74 echo "</b><br>\n";
75 echo _("Server responded: ");
76 echo $message . "</font><br>\n";
77 exit;
78 }
79
80 return $data;
81 }