imap.php 3.6 KB

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