4f11ed9072d7f43691a5f7232e6204fa280d4137
[squirrelmail.git] / functions / imap.php
1 <?
2 /**
3 ** imap.php
4 **
5 ** Functions for the IMAP connection
6 **
7 **/
8
9 /** Read from the connection until we get either an OK or BAD message. **/
10 function imapReadData($connection) {
11 $read = fgets($connection, 1024);
12 $counter = 0;
13 while ((substr($read, strpos($read, " ") + 1, 2) != "OK") && (substr($read, strpos($read, " ") + 1, 3) != "BAD")) {
14 $data[$counter] = $read;
15 $read = fgets($connection, 1024);
16 $counter++;
17 }
18 return $data;
19 }
20
21 /** Parse the incoming mailbox name and return a string that is the FOLDER.MAILBOX **/
22 function findMailboxName($mailbox) {
23 // start at -2 so that we skip the initial quote at the end of the mailbox name
24 $i = -2;
25 $char = substr($mailbox, $i, 1);
26 while ($char != "\"") {
27 $i--;
28 $temp .= $char;
29 $char = substr($mailbox, $i, 1);
30 }
31 echo $tmp;
32 return strrev($temp);
33 }
34
35 // handles logging onto an imap server.
36 function loginToImapServer($username, $key, $imapServerAddress) {
37 $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
38 if (!$imapConnection) {
39 echo "Error connecting to IMAP Server.<br>";
40 echo "$errorNumber : $errorString<br>";
41 exit;
42 }
43 $serverInfo = fgets($imapConnection, 256);
44
45 // login
46 fputs($imapConnection, "1 login $username $key\n");
47 $read = fgets($imapConnection, 1024);
48
49 if (strpos($read, "NO")) {
50 error_username_password_incorrect();
51 exit;
52 }
53
54 return $imapConnection;
55 }
56
57 /** must be sent in the form: user.<USER>.<FOLDER> **/
58 function createFolder($imapConnection, $folder) {
59 fputs($imapConnection, "1 create \"$folder\"\n");
60 }
61
62 /** must be sent in the form: user.<USER>.<FOLDER> **/
63 function removeFolder($imapConnection, $folder) {
64 fputs($imapConnection, "1 delete \"$folder\"\n");
65 }
66
67 /** Sends back two arrays, boxesFormatted and boxesUnformatted **/
68 function getFolderList($imapConnection, &$boxesFormatted, &$boxesUnformatted) {
69 fputs($imapConnection, "1 list \"\" *\n");
70 $str = imapReadData($imapConnection);
71
72 for ($i = 0;$i < count($str); $i++) {
73 $mailbox = chop($str[$i]);
74 $mailbox = findMailboxName($mailbox);
75 $periodCount = countCharInString($mailbox, ".");
76
77 // indent the correct number of spaces.
78 for ($j = 0;$j < $periodCount;$j++)
79 $boxesFormatted[$i] = "$boxesFormatted[$i]&nbsp;&nbsp;";
80
81 $boxesFormatted[$i] = $boxesFormatted[$i] . readShortMailboxName($mailbox, ".");
82 $boxesUnformatted[$i] = $mailbox;
83 }
84 }
85
86 function deleteMessages($imapConnection, $a, $b, $numMessages, $trash_folder, $move_to_trash, $auto_expunge, $mailbox) {
87 /** check if they would like to move it to the trash folder or not */
88 if ($move_to_trash == true) {
89 $success = copyMessages($imapConnection, $a, $b, $trash_folder);
90 if ($success == true)
91 setMessageFlag($imapConnection, $a, $b, "Deleted");
92 } else {
93 setMessageFlag($imapConnection, $a, $b, "Deleted");
94 }
95 }
96 function stripComments($line) {
97 if (strpos($line, ";")) {
98 $line = substr($line, 0, strpos($line, ";"));
99 }
100
101 if (strpos($line, "(") && strpos($line, ")")) {
102 $full_line = $full_line . substr($line, 0, strpos($line, "("));
103 $full_line = $full_line . substr($line, strpos($line, ")")+1, strlen($line) - strpos($line, ")"));
104 } else {
105 $full_line = $line;
106 }
107 return $full_line;
108 }
109 ?>