updated functions
[squirrelmail.git] / functions / mailbox.php3
1 <?
2 /**
3 ** mailbox.php3
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
51 function getMessageFlags($imapConnection, $i, &$flags) {
52 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
53 fputs($imapConnection, "messageFetch FETCH $i:$i FLAGS\n");
54 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
55 if (strpos($read, "FLAGS")) {
56 $read = ereg_replace("\(", "", $read);
57 $read = ereg_replace("\)", "", $read);
58 $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
59 $read = trim($read);
60 $flags = explode(" ", $read);;
61 $i = 0;
62 while ($i < count($flags)) {
63 $flags[$i] = substr($flags[$i], 1, strlen($flags[$i]));
64 $i++;
65 }
66 } else {
67 $flags[0] = "None";
68 }
69 $read = fgets($imapConnection, 1024);
70 }
71 }
72
73 function getEmailAddr($sender) {
74 if (strpos($sender, "EMAILSTART--") == false)
75 return "";
76
77 $start = strpos($sender, "EMAILSTART--");
78 $emailAddr = substr($sender, $start, strlen($sender));
79
80 return $emailAddr;
81 }
82
83 function getSender($sender) {
84 if (strpos($sender, "EMAILSTART--") == false)
85 return "";
86
87 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
88 $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
89 return "$first$second";
90 }
91
92 function getSenderName($sender) {
93 $name = getSender($sender);
94 $emailAddr = getEmailAddr($sender);
95 $emailStart = strpos($emailAddr, "EMAILSTART--");
96 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
97
98 if (($emailAddr == "") && ($name == "")) {
99 $from = $sender;
100 }
101 else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
102 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
103 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
104 $from = $emailAddr;
105 }
106 else if (strlen($name) > 0) {
107 $from = $name;
108 }
109 else if (strlen($emailAddr > 0)) {
110 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
111 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
112 $from = $emailAddr;
113 }
114
115 $from = trim($from);
116
117 // strip out any quotes if they exist
118 if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
119 $from = substr($from, 1, strlen($from) - 2);
120
121 return $from;
122 }
123 ?>