Made special character handling better in From and Subject
[squirrelmail.git] / functions / mailbox.php
1 <?
2 /**
3 ** mailbox.php
4 **
5 ** This contains functions that request information about a mailbox. Including
6 ** reading and parsing headers, getting folder information, etc.
7 **
8 **/
9
10 function selectMailbox($imapConnection, $mailbox, &$numberOfMessages) {
11 // select mailbox
12 fputs($imapConnection, "mailboxSelect SELECT \"$mailbox\"\n");
13 $read = fgets($imapConnection, 1024);
14 while ((substr($read, 0, 16) != "mailboxSelect OK") && (substr($read, 0, 17) != "mailboxSelect BAD")) {
15 if (substr(Chop($read), -6) == "EXISTS") {
16 $array = explode(" ", $read);
17 $numberOfMessages = $array[1];
18 }
19 $read = fgets($imapConnection, 1024);
20 }
21 }
22
23 /** This function sends a request to the IMAP server for headers, 50 at a time
24 ** until $end is reached. I originally had it do them all at one time, but found
25 ** it slightly faster to do it this way.
26 **
27 ** Originally we had getMessageHeaders get the headers for one message at a time.
28 ** Doing it in bunches gave us a speed increase from 9 seconds (for a box of 800
29 ** messages) to about 3.5 seconds.
30 **/
31 function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
32 $rel_start = $start;
33 if (($start > $end) || ($start < 1)) {
34 echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
35 exit;
36 }
37
38 while ($rel_start <= $end) {
39 if ($end - $rel_start > 50) {
40 $rel_end = $rel_start + 50;
41 } else {
42 $rel_end = $end;
43 }
44 fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
45 $read = fgets($imapConnection, 1024);
46
47 $from_num = $rel_start - 1;
48 $date_num = $rel_start - 1;
49 $subj_num = $rel_start - 1;
50 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
51 if (substr($read, 0, 5) == "From:") {
52 $read = ereg_replace("<", "EMAILSTART--", $read);
53 $read = ereg_replace(">", "--EMAILEND", $read);
54 $from[$from_num] = substr($read, 5, strlen($read) - 6);
55 $from_num++;
56 }
57 else if (substr($read, 0, 5) == "Date:") {
58 $read = ereg_replace("<", "&lt;", $read);
59 $read = ereg_replace(">", "&gt;", $read);
60 $date[$date_num] = substr($read, 5, strlen($read) - 6);
61 $date_num++;
62 }
63 else if (substr($read, 0, 8) == "Subject:") {
64 $read = ereg_replace("<", "&lt;", $read);
65 $read = ereg_replace(">", "&gt;", $read);
66 $subject[$subj_num] = substr($read, 8, strlen($read) - 9);
67 $subj_num++;
68 }
69 $read = fgets($imapConnection, 1024);
70 }
71 $rel_start = $rel_start + 50;
72 }
73 }
74
75 function setMessageFlag($imapConnection, $i, $q, $flag) {
76 fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
77 }
78
79 /** This function gets the flags for message $j. It does only one message at a
80 ** time, rather than doing groups of messages (like getMessageHeaders does).
81 ** I found it one or two seconds quicker (on a box of 800 messages) to do it
82 ** individually. I'm not sure why it happens like that, but that's what my
83 ** testing found. Perhaps later I will be proven wrong and this will change.
84 **/
85 function getMessageFlags($imapConnection, $j, &$flags) {
86 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
87 fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
88 $read = fgets($imapConnection, 1024);
89 $count = 0;
90 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
91 if (strpos($read, "FLAGS")) {
92 $read = ereg_replace("\(", "", $read);
93 $read = ereg_replace("\)", "", $read);
94 $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
95 $read = trim($read);
96 $flags = explode(" ", $read);;
97 $s = 0;
98 while ($s < count($flags)) {
99 $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
100 $s++;
101 }
102 } else {
103 $flags[0] = "None";
104 }
105 $count++;
106 $read = fgets($imapConnection, 1024);
107 }
108 }
109
110 function getEmailAddr($sender) {
111 if (strpos($sender, "EMAILSTART--") == false)
112 return "";
113
114 $start = strpos($sender, "EMAILSTART--");
115 $emailAddr = substr($sender, $start, strlen($sender));
116
117 return $emailAddr;
118 }
119
120 function getSender($sender) {
121 if (strpos($sender, "EMAILSTART--") == false)
122 return "";
123
124 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
125 $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
126 return "$first$second";
127 }
128
129 function getSenderName($sender) {
130 $name = getSender($sender);
131 $emailAddr = getEmailAddr($sender);
132 $emailStart = strpos($emailAddr, "EMAILSTART--");
133 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
134
135 if (($emailAddr == "") && ($name == "")) {
136 $from = $sender;
137 }
138 else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
139 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
140 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
141 $from = $emailAddr;
142 }
143 else if (strlen($name) > 0) {
144 $from = $name;
145 }
146 else if (strlen($emailAddr > 0)) {
147 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
148 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
149 $from = $emailAddr;
150 }
151
152 $from = trim($from);
153
154 // strip out any quotes if they exist
155 if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
156 $from = substr($from, 1, strlen($from) - 2);
157
158 return $from;
159 }
160
161 /** returns "true" if the copy was completed successfully.
162 ** returns "false" with an error message if unsuccessful.
163 **/
164 function copyMessages($imapConnection, $from_id, $to_id, $folder) {
165 fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
166 $read = fgets($imapConnection, 1024);
167 while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
168 $read = fgets($imapConnection, 1024);
169 }
170
171 if (substr($read, 0, 15) == "mailboxStore NO") {
172 echo "ERROR... $read<BR>";
173 return false;
174 } else if (substr($read, 0, 15) == "mailboxStore OK") {
175 return true;
176 }
177
178 echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
179 return false;
180 }
181
182 /** expunges a mailbox **/
183 function expungeBox($imapConnection, $mailbox) {
184 selectMailbox($imapConnection, $mailbox, $num);
185 fputs($imapConnection, "1 EXPUNGE\n");
186 }
187
188 function getFolderNameMinusINBOX($mailbox) {
189 if (substr($mailbox, 0, 6) == "INBOX.")
190 $box = substr($mailbox, 6, strlen($mailbox));
191 else
192 $box = $mailbox;
193
194 return $box;
195 }
196 ?>