imap.php 3.1 KB

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