83b77febe01a0546424d2da1c4b143c87d59595e
[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 function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
24
25 $rel_start = $start;
26
27 if (($start > $end) || ($start < 1)) {
28 echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
29 exit;
30 }
31
32 while ($rel_start <= $end) {
33 if ($end - $rel_start > 50) {
34 $rel_end = $rel_start + 50;
35 } else {
36 $rel_end = $end;
37 }
38 fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
39 $read = fgets($imapConnection, 1024);
40
41 $from_num = $rel_start - 1;
42 $date_num = $rel_start - 1;
43 $subj_num = $rel_start - 1;
44 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
45 if (substr($read, 0, 5) == "From:") {
46 $read = ereg_replace("<", "EMAILSTART--", $read);
47 $read = ereg_replace(">", "--EMAILEND", $read);
48 $from[$from_num] = substr($read, 5, strlen($read) - 6);
49 $from_num++;
50 }
51 else if (substr($read, 0, 5) == "Date:") {
52 $read = ereg_replace("<", "[", $read);
53 $read = ereg_replace(">", "]", $read);
54 $date[$date_num] = substr($read, 5, strlen($read) - 6);
55 $date_num++;
56 }
57 else if (substr($read, 0, 8) == "Subject:") {
58 $read = ereg_replace("<", "[", $read);
59 $read = ereg_replace(">", "]", $read);
60 $subject[$subj_num] = substr($read, 8, strlen($read) - 9);
61 $subj_num++;
62 }
63 $read = fgets($imapConnection, 1024);
64 }
65 $rel_start = $rel_start + 50;
66 }
67 }
68
69 function setMessageFlag($imapConnection, $i, $q, $flag) {
70 fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
71 }
72
73 function getMessageFlags($imapConnection, $j, &$flags) {
74 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
75 fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
76 $read = fgets($imapConnection, 1024);
77 $count = 0;
78 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
79 if (strpos($read, "FLAGS")) {
80 $read = ereg_replace("\(", "", $read);
81 $read = ereg_replace("\)", "", $read);
82 $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
83 $read = trim($read);
84 $flags = explode(" ", $read);;
85 $s = 0;
86 while ($s < count($flags)) {
87 $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
88 $s++;
89 }
90 } else {
91 $flags[0] = "None";
92 }
93 $count++;
94 $read = fgets($imapConnection, 1024);
95 }
96 }
97
98 function getEmailAddr($sender) {
99 if (strpos($sender, "EMAILSTART--") == false)
100 return "";
101
102 $start = strpos($sender, "EMAILSTART--");
103 $emailAddr = substr($sender, $start, strlen($sender));
104
105 return $emailAddr;
106 }
107
108 function getSender($sender) {
109 if (strpos($sender, "EMAILSTART--") == false)
110 return "";
111
112 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
113 $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
114 return "$first$second";
115 }
116
117 function getSenderName($sender) {
118 $name = getSender($sender);
119 $emailAddr = getEmailAddr($sender);
120 $emailStart = strpos($emailAddr, "EMAILSTART--");
121 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
122
123 if (($emailAddr == "") && ($name == "")) {
124 $from = $sender;
125 }
126 else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
127 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
128 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
129 $from = $emailAddr;
130 }
131 else if (strlen($name) > 0) {
132 $from = $name;
133 }
134 else if (strlen($emailAddr > 0)) {
135 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
136 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
137 $from = $emailAddr;
138 }
139
140 $from = trim($from);
141
142 // strip out any quotes if they exist
143 if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
144 $from = substr($from, 1, strlen($from) - 2);
145
146 return $from;
147 }
148
149 /** returns "true" if the copy was completed successfully.
150 ** returns "false" with an error message if unsuccessful.
151 **/
152 function copyMessages($imapConnection, $from_id, $to_id, $folder) {
153 fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
154 $read = fgets($imapConnection, 1024);
155 while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
156 $read = fgets($imapConnection, 1024);
157 }
158
159 if (substr($read, 0, 15) == "mailboxStore NO") {
160 echo "ERROR... $read<BR>";
161 return false;
162 } else if (substr($read, 0, 15) == "mailboxStore OK") {
163 return true;
164 }
165
166 echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
167 return false;
168 }
169
170 /** expunges a mailbox **/
171 function expungeBox($imapConnection, $mailbox) {
172 selectMailbox($imapConnection, $mailbox, $num);
173 fputs($imapConnection, "1 EXPUNGE\n");
174 }
175
176 function getFolderNameMinusINBOX($mailbox) {
177 if (substr($mailbox, 0, 6) == "INBOX.")
178 $box = substr($mailbox, 6, strlen($mailbox));
179 else
180 $box = $mailbox;
181
182 return $box;
183 }
184 ?>