added DELETE for when viewing a message
[squirrelmail.git] / functions / imap.php
CommitLineData
3302d0d4 1<?
2 /**
a09387f4 3 ** imap.php
3302d0d4 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 }
aa42fbfb 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 return strrev($temp);
32 }
8c7dfc99 33
2aa12d5e 34 // handles logging onto an imap server.
35 function loginToImapServer($username, $key, $imapServerAddress) {
36 $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
37 if (!$imapConnection) {
38 echo "Error connecting to IMAP Server.<br>";
39 echo "$errorNumber : $errorString<br>";
40 exit;
41 }
42 $serverInfo = fgets($imapConnection, 256);
43
44 // login
45 fputs($imapConnection, "1 login $username $key\n");
46 $read = fgets($imapConnection, 1024);
47
48 if (strpos($read, "NO")) {
49 error_username_password_incorrect();
50 exit;
51 }
52
53 return $imapConnection;
54 }
55
8c7dfc99 56 /** must be sent in the form: user.<USER>.<FOLDER> **/
57 function createFolder($imapConnection, $folder) {
58 fputs($imapConnection, "1 create \"$folder\"\n");
59 }
60
61 /** must be sent in the form: user.<USER>.<FOLDER> **/
597d8f1d 62 function removeFolder($imapConnection, $folder) {
8c7dfc99 63 fputs($imapConnection, "1 delete \"$folder\"\n");
64 }
54e3c1d8 65
66 /** Sends back two arrays, boxesFormatted and boxesUnformatted **/
67 function getFolderList($imapConnection, &$boxesFormatted, &$boxesUnformatted) {
68 fputs($imapConnection, "1 list \"\" *\n");
69 $str = imapReadData($imapConnection);
70
71 for ($i = 0;$i < count($str); $i++) {
72 $mailbox = chop($str[$i]);
73 $mailbox = findMailboxName($mailbox);
74 $periodCount = countCharInString($mailbox, ".");
75
76 // indent the correct number of spaces.
77 for ($j = 0;$j < $periodCount;$j++)
78 $boxesFormatted[$i] = "$boxesFormatted[$i]&nbsp;&nbsp;";
79
80 $boxesFormatted[$i] = $boxesFormatted[$i] . readShortMailboxName($mailbox, ".");
81 $boxesUnformatted[$i] = $mailbox;
82 }
83 }
84
de80e95e 85 function deleteMessages($imapConnection, $a, $b, $numMessages, $trash_folder, $move_to_trash, $auto_expunge, $mailbox) {
86 /** check if they would like to move it to the trash folder or not */
87 if ($move_to_trash == true) {
88 $success = copyMessages($imapConnection, $a, $b, $trash_folder);
89 if ($success == true)
90 setMessageFlag($imapConnection, $a, $b, "Deleted");
91 } else {
92 setMessageFlag($imapConnection, $a, $b, "Deleted");
93 }
94 if ($auto_expunge == true)
95 expungeBox($imapConnection, $mailbox, $numMessages);
96 }
3302d0d4 97?>