3302d0d4 |
1 | <? |
2 | /** |
a09387f4 |
3 | ** mailbox.php |
3302d0d4 |
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, $i, &$from, &$subject, &$date) { |
24 | fputs($imapConnection, "messageFetch FETCH $i:$i RFC822.HEADER.LINES (From Subject Date)\n"); |
25 | $read = fgets($imapConnection, 1024); |
26 | /* I have to replace <> with [] because HTML uses <> as tags, thus not printing what's in <> */ |
27 | $read = ereg_replace("<", "[", $read); |
28 | $read = ereg_replace(">", "]", $read); |
29 | |
30 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
31 | if (substr($read, 0, 5) == "From:") { |
32 | $read = ereg_replace("<", "EMAILSTART--", $read); |
33 | $read = ereg_replace(">", "--EMAILEND", $read); |
34 | $from = substr($read, 5, strlen($read) - 6); |
35 | } |
36 | else if (substr($read, 0, 5) == "Date:") { |
37 | $read = ereg_replace("<", "[", $read); |
38 | $read = ereg_replace(">", "]", $read); |
39 | $date = substr($read, 5, strlen($read) - 6); |
40 | } |
41 | else if (substr($read, 0, 8) == "Subject:") { |
42 | $read = ereg_replace("<", "[", $read); |
43 | $read = ereg_replace(">", "]", $read); |
44 | $subject = substr($read, 8, strlen($read) - 9); |
45 | } |
46 | |
47 | $read = fgets($imapConnection, 1024); |
48 | } |
49 | } |
50 | |
61a4ac35 |
51 | function setMessageFlag($imapConnection, $i, $q, $flag) { |
52 | fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n"); |
aa4c3749 |
53 | } |
54 | |
3302d0d4 |
55 | function getMessageFlags($imapConnection, $i, &$flags) { |
56 | /** * 2 FETCH (FLAGS (\Answered \Seen)) */ |
57 | fputs($imapConnection, "messageFetch FETCH $i:$i FLAGS\n"); |
aa4c3749 |
58 | $read = fgets($imapConnection, 1024); |
3302d0d4 |
59 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
60 | if (strpos($read, "FLAGS")) { |
61 | $read = ereg_replace("\(", "", $read); |
62 | $read = ereg_replace("\)", "", $read); |
63 | $read = substr($read, strpos($read, "FLAGS")+6, strlen($read)); |
64 | $read = trim($read); |
65 | $flags = explode(" ", $read);; |
54a8ae32 |
66 | $s = 0; |
67 | while ($s < count($flags)) { |
68 | $flags[$s] = substr($flags[$s], 1, strlen($flags[$s])); |
69 | $s++; |
3302d0d4 |
70 | } |
71 | } else { |
72 | $flags[0] = "None"; |
73 | } |
74 | $read = fgets($imapConnection, 1024); |
75 | } |
76 | } |
77 | |
78 | function getEmailAddr($sender) { |
79 | if (strpos($sender, "EMAILSTART--") == false) |
80 | return ""; |
81 | |
82 | $start = strpos($sender, "EMAILSTART--"); |
83 | $emailAddr = substr($sender, $start, strlen($sender)); |
84 | |
85 | return $emailAddr; |
86 | } |
87 | |
88 | function getSender($sender) { |
89 | if (strpos($sender, "EMAILSTART--") == false) |
90 | return ""; |
91 | |
92 | $first = substr($sender, 0, strpos($sender, "EMAILSTART--")); |
93 | $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender)); |
94 | return "$first$second"; |
95 | } |
96 | |
97 | function getSenderName($sender) { |
98 | $name = getSender($sender); |
99 | $emailAddr = getEmailAddr($sender); |
100 | $emailStart = strpos($emailAddr, "EMAILSTART--"); |
101 | $emailEnd = strpos($emailAddr, "--EMAILEND") - 10; |
102 | |
103 | if (($emailAddr == "") && ($name == "")) { |
104 | $from = $sender; |
105 | } |
106 | else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){ |
107 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
108 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
109 | $from = $emailAddr; |
110 | } |
111 | else if (strlen($name) > 0) { |
112 | $from = $name; |
113 | } |
114 | else if (strlen($emailAddr > 0)) { |
115 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
116 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
117 | $from = $emailAddr; |
118 | } |
119 | |
120 | $from = trim($from); |
121 | |
122 | // strip out any quotes if they exist |
123 | if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\"")) |
124 | $from = substr($from, 1, strlen($from) - 2); |
125 | |
126 | return $from; |
127 | } |
aa4c3749 |
128 | |
129 | /** returns "true" if the copy was completed successfully. |
130 | ** returns "false" with an error message if unsuccessful. |
131 | **/ |
132 | function copyMessages($imapConnection, $from_id, $to_id, $folder) { |
133 | fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n"); |
134 | $read = fgets($imapConnection, 1024); |
135 | while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) { |
136 | $read = fgets($imapConnection, 1024); |
137 | } |
138 | |
139 | if (substr($read, 0, 15) == "mailboxStore NO") { |
140 | echo "ERROR... $read<BR>"; |
141 | return false; |
142 | } else if (substr($read, 0, 15) == "mailboxStore OK") { |
143 | return true; |
144 | } |
145 | |
146 | echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>"; |
147 | return false; |
148 | } |
61a4ac35 |
149 | |
150 | /** expunges a mailbox **/ |
151 | function expungeBox($imapConnection, $mailbox) { |
152 | selectMailbox($imapConnection, $mailbox, $num); |
153 | fputs($imapConnection, "1 EXPUNGE\n"); |
154 | } |
a09387f4 |
155 | ?> |