imap.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?
  2. /**
  3. ** imap.php
  4. **
  5. ** Functions for the IMAP connection
  6. **
  7. **/
  8. /** Read from the connection until we get either an OK or BAD message. **/
  9. function imapReadData($connection, $pre, $handle_errors, &$response, &$message) {
  10. require ("../config/config.php");
  11. $read = fgets($connection, 1024);
  12. $counter = 0;
  13. while ((substr($read, 0, strlen("$pre OK")) != "$pre OK") &&
  14. (substr($read, 0, strlen("$pre BAD")) != "$pre BAD") &&
  15. (substr($read, 0, strlen("$pre NO")) != "$pre NO")) {
  16. $data[$counter] = $read;
  17. $read = fgets($connection, 1024);
  18. $counter++;
  19. }
  20. if (substr($read, 0, strlen("$pre OK")) == "$pre OK") {
  21. $response = "OK";
  22. $message = trim(substr($read, strlen("$pre OK"), strlen($read)));
  23. } else if (substr($read, 0, strlen("$pre BAD")) == "$pre BAD") {
  24. $response = "BAD";
  25. $message = trim(substr($read, strlen("$pre BAD"), strlen($read)));
  26. } else {
  27. $response = "NO";
  28. $message = trim(substr($read, strlen("$pre NO"), strlen($read)));
  29. }
  30. if ($handle_errors == true) {
  31. if ($response == "NO") {
  32. echo "<BR><B><FONT FACE=\"Arial,Helvetica\" COLOR=FF0000>ERROR</FONT FACE=\"Arial,Helvetica\"><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>: Could not complete request.</B> </FONT FACE=\"Arial,Helvetica\"><BR><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>&nbsp;&nbsp;<B>Reason given:</B> $message</FONT FACE=\"Arial,Helvetica\"><BR><BR>";
  33. exit;
  34. } else if ($response == "BAD") {
  35. echo "<BR><B><FONT FACE=\"Arial,Helvetica\" COLOR=FF0000>ERROR</FONT FACE=\"Arial,Helvetica\"><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>: Bad or malformed request.</B></FONT FACE=\"Arial,Helvetica\"><BR><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>&nbsp;&nbsp;<B>Server responded:</B> $message</FONT FACE=\"Arial,Helvetica\"><BR><BR>";
  36. exit;
  37. }
  38. }
  39. return $data;
  40. }
  41. /** Parse the incoming mailbox name and return a string that is the FOLDER.MAILBOX **/
  42. function findMailboxName($mailbox) {
  43. $mailbox = trim($mailbox);
  44. if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
  45. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  46. $pos = strrpos($mailbox, "\"") + 1;
  47. $box = substr($mailbox, $pos, strlen($mailbox));
  48. } else {
  49. $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
  50. }
  51. return $box;
  52. }
  53. /** Finds the delimeter between mailboxes **/
  54. function findMailboxDelimeter($imapConnection) {
  55. fputs($imapConnection, ". list \"\" \"\"\n");
  56. $read = fgets($imapConnection, 1024);
  57. $pos = strrpos($read, "\"");
  58. $read = substr($read, 0, $pos);
  59. $pos = strrpos($read, "\"");
  60. $read = substr($read, 0, $pos);
  61. $pos = strrpos($read, "\"");
  62. $read = substr($read, 0, $pos);
  63. $pos = strrpos($read, "\"");
  64. $read = substr($read, $pos+1, strlen($read));
  65. $tmp = fgets($imapConnection, 1024);
  66. return $read;
  67. }
  68. function getMailboxFlags($mailbox) {
  69. $mailbox = trim($mailbox);
  70. $mailbox = substr($mailbox, strpos($mailbox, "(")+1, strlen($mailbox));
  71. $mailbox = substr($mailbox, 0, strpos($mailbox, ")"));
  72. $mailbox = str_replace("\\", "", $mailbox);
  73. $mailbox = strtolower($mailbox);
  74. $mailbox = explode(" ", $mailbox);
  75. return $mailbox;
  76. }
  77. // handles logging onto an imap server.
  78. function loginToImapServer($username, $key, $imapServerAddress, $hide) {
  79. require("../config/config.php");
  80. $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
  81. if (!$imapConnection) {
  82. echo "Error connecting to IMAP Server.<br>";
  83. echo "$errorNumber : $errorString<br>";
  84. exit;
  85. }
  86. $serverInfo = fgets($imapConnection, 256);
  87. // login
  88. fputs($imapConnection, "a001 LOGIN $username $key\n");
  89. $read = fgets($imapConnection, 1024);
  90. if ($debug_login == true) {
  91. echo "SERVER SAYS: $read<BR>";
  92. }
  93. if (substr($read, 0, 7) != "a001 OK") {
  94. if (!$hide) {
  95. if (substr($read, 0, 8) == "a001 BAD") {
  96. echo "Bad request: $read<BR>";
  97. exit;
  98. }
  99. else if (substr($read, 0, 7) == "a001 NO") {
  100. echo "<BR>";
  101. echo "<TABLE COLS=1 WIDTH=70% NOBORDER BGCOLOR=\"$color[4]\" ALIGN=CENTER>";
  102. echo " <TR>";
  103. echo " <TD BGCOLOR=\"$color[0]\">";
  104. echo " <FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[2]\"><B><CENTER>ERROR</CENTER></B></FONT>";
  105. echo " </TD></TR><TR><TD>";
  106. echo " <CENTER><FONT FACE=\"Arial,Helvetica\"><BR>Unknown user or password incorrect.<BR><A HREF=\"login.php\" TARGET=_top>Click here to try again</A>.</FONT></CENTER>";
  107. echo " </TD></TR>";
  108. echo "</TABLE>";
  109. echo "</BODY></HTML>";
  110. exit;
  111. }
  112. else {
  113. echo "Unknown error: $read<BR>";
  114. exit;
  115. }
  116. } else {
  117. exit;
  118. }
  119. }
  120. return $imapConnection;
  121. }
  122. /** must be sent in the form: user.<USER>.<FOLDER> **/
  123. function createFolder($imapConnection, $folder, $type) {
  124. require ("../config/config.php");
  125. if (strtolower($type) == "noselect") {
  126. $dm = findMailboxDelimeter($imapConnection);
  127. $folder = "$folder$dm";
  128. } else {
  129. $folder = "$folder";
  130. }
  131. fputs($imapConnection, "1 create \"$folder\"\n");
  132. $data = imapReadData($imapConnection, "1", false, $response, $message);
  133. if ($response == "NO") {
  134. echo "<BR><B><FONT FACE=\"Arial,Helvetica\" COLOR=FF0000>ERROR</FONT FACE=\"Arial,Helvetica\"><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>: Could not complete request.</B> </FONT FACE=\"Arial,Helvetica\"><BR><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>&nbsp;&nbsp;<B>Reason given:</B> $message</FONT FACE=\"Arial,Helvetica\"><BR><BR>";
  135. echo "<FONT FACE=\"Arial,Helvetica\">Possible solutions:<BR><LI>You may need to specify that the folder is a subfolder of INBOX</LI>";
  136. echo "<LI>Try renaming the folder to something different.</LI>";
  137. exit;
  138. } else if ($response == "BAD") {
  139. echo "<B><FONT FACE=\"Arial,Helvetica\" COLOR=FF0000>ERROR</FONT FACE=\"Arial,Helvetica\"><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>: Bad or malformed request.</B></FONT FACE=\"Arial,Helvetica\"><BR><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>&nbsp;&nbsp;<B>Server responded:</B> $message</FONT FACE=\"Arial,Helvetica\"><BR><BR>";
  140. exit;
  141. }
  142. }
  143. function removeFolder($imapConnection, $folder) {
  144. fputs($imapConnection, "1 delete \"$folder\"\n");
  145. $data = imapReadData($imapConnection, "1", false, $response, $message);
  146. if ($response == "NO") {
  147. echo "<FONT FACE=\"Arial,Helvetica\" COLOR=FF0000><B>ERROR</B>: Could not delete the folder $folder.</FONT>";
  148. echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\">Probable causes:</FONT><BR>";
  149. echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\"><LI>This folder may contain subfolders. Delete all subfolders first</LI></FONT>";
  150. exit;
  151. } else if ($response == "BAD") {
  152. echo "<B><FONT COLOR=FF0000>ERROR</FONT><FONT COLOR=CC0000>: Bad or malformed request.</B></FONT><BR><FONT COLOR=CC0000>&nbsp;&nbsp;<B>Server responded:</B> $message</FONT><BR><BR>";
  153. exit;
  154. }
  155. }
  156. /** Sends back two arrays, boxesFormatted and boxesUnformatted **/
  157. function getFolderList($imapConnection, &$boxes) {
  158. require ("../config/config.php");
  159. fputs($imapConnection, "1 list \"\" *\n");
  160. $str = imapReadData($imapConnection, "1", true, $response, $message);
  161. $dm = findMailboxDelimeter($imapConnection);
  162. $g = 0;
  163. for ($i = 0;$i < count($str); $i++) {
  164. $mailbox = chop($str[$i]);
  165. if (substr(findMailboxName($mailbox), 0, 1) != ".") {
  166. $boxes[$g]["RAW"] = $mailbox;
  167. $mailbox = findMailboxName($mailbox);
  168. $periodCount = countCharInString($mailbox, $dm);
  169. // indent the correct number of spaces.
  170. for ($j = 0;$j < $periodCount;$j++)
  171. $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . "&nbsp;&nbsp;";
  172. $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . readShortMailboxName($mailbox, $dm);
  173. $boxes[$g]["UNFORMATTED"] = $mailbox;
  174. $boxes[$g]["ID"] = $g;
  175. $g++;
  176. }
  177. }
  178. $original = $boxes;
  179. for ($i = 0; $i < count($original); $i++) {
  180. $boxes[$i]["UNFORMATTED"] = strtolower($boxes[$i]["UNFORMATTED"]);
  181. }
  182. $boxes = ary_sort($boxes, "UNFORMATTED", 1);
  183. for ($i = 0; $i < count($original); $i++) {
  184. for ($j = 0; $j < count($original); $j++) {
  185. if ($boxes[$i]["ID"] == $original[$j]["ID"]) {
  186. $boxes[$i]["UNFORMATTED"] = $original[$j]["UNFORMATTED"];
  187. $boxes[$i]["FORMATTED"] = $original[$j]["FORMATTED"];
  188. $boxes[$i]["RAW"] = $original[$j]["RAW"];
  189. }
  190. }
  191. }
  192. for ($i = 0; $i < count($boxes); $i++) {
  193. if ($boxes[$i]["UNFORMATTED"] == $special_folders[0]) {
  194. $boxesnew[0]["FORMATTED"] = $boxes[$i]["FORMATTED"];
  195. $boxesnew[0]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
  196. $boxesnew[0]["RAW"] = trim($boxes[$i]["RAW"]);
  197. $boxes[$i]["USED"] = true;
  198. }
  199. }
  200. if ($list_special_folders_first == true) {
  201. for ($i = 0; $i < count($boxes); $i++) {
  202. for ($j = 1; $j < count($special_folders); $j++) {
  203. if (substr($boxes[$i]["UNFORMATTED"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
  204. $pos = count($boxesnew);
  205. $boxesnew[$pos]["FORMATTED"] = $boxes[$i]["FORMATTED"];
  206. $boxesnew[$pos]["RAW"] = trim($boxes[$i]["RAW"]);
  207. $boxesnew[$pos]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
  208. $boxes[$i]["USED"] = true;
  209. }
  210. }
  211. }
  212. }
  213. for ($i = 0; $i < count($boxes); $i++) {
  214. if (($boxes[$i]["UNFORMATTED"] != $special_folders[0]) &&
  215. ($boxes[$i]["UNFORMATTED"] != ".mailboxlist") &&
  216. ($boxes[$i]["USED"] == false)) {
  217. $pos = count($boxesnew);
  218. $boxesnew[$pos]["FORMATTED"] = $boxes[$i]["FORMATTED"];
  219. $boxesnew[$pos]["RAW"] = trim($boxes[$i]["RAW"]);
  220. $boxesnew[$pos]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
  221. $boxes[$i]["USED"] = true;
  222. }
  223. }
  224. $boxes = $boxesnew;
  225. }
  226. function deleteMessages($imapConnection, $a, $b, $numMessages, $trash_folder, $move_to_trash, $auto_expunge, $mailbox) {
  227. /** check if they would like to move it to the trash folder or not */
  228. if ($move_to_trash == true) {
  229. $success = copyMessages($imapConnection, $a, $b, $trash_folder);
  230. if ($success == true)
  231. setMessageFlag($imapConnection, $a, $b, "Deleted");
  232. } else {
  233. setMessageFlag($imapConnection, $a, $b, "Deleted");
  234. }
  235. }
  236. function stripComments($line) {
  237. if (strpos($line, ";")) {
  238. $line = substr($line, 0, strpos($line, ";"));
  239. }
  240. if (strpos($line, "(") && strpos($line, ")")) {
  241. $full_line = $full_line . substr($line, 0, strpos($line, "("));
  242. $full_line = $full_line . substr($line, strpos($line, ")")+1, strlen($line) - strpos($line, ")"));
  243. } else {
  244. $full_line = $line;
  245. }
  246. return $full_line;
  247. }
  248. ?>