imap_mailbox.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?
  2. /**
  3. ** imap_mailbox.php
  4. **
  5. ** This impliments all functions that manipulate mailboxes
  6. **/
  7. /******************************************************************************
  8. ** Expunges a mailbox
  9. ******************************************************************************/
  10. function sqimap_mailbox_expunge ($imap_stream, $mailbox) {
  11. sqimap_mailbox_select ($imap_stream, $mailbox);
  12. fputs ($imap_stream, "a001 EXPUNGE\r\n");
  13. $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  14. }
  15. /******************************************************************************
  16. ** Checks whether or not the specified mailbox exists
  17. ******************************************************************************/
  18. function sqimap_mailbox_exists ($imap_stream, $mailbox) {
  19. $boxes = sqimap_mailbox_list ($imap_stream);
  20. $found = false;
  21. for ($i = 0; $i < count ($boxes); $i++) {
  22. if ($boxes[$i]["unformatted"] == $mailbox)
  23. $found = true;
  24. }
  25. return $found;
  26. }
  27. /******************************************************************************
  28. ** Selects a mailbox
  29. ******************************************************************************/
  30. function sqimap_mailbox_select ($imap_stream, $mailbox) {
  31. fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
  32. $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  33. }
  34. /******************************************************************************
  35. ** Creates a folder
  36. ******************************************************************************/
  37. function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
  38. if (strtolower($type) == "noselect") {
  39. $dm = sqimap_get_delimiter($imap_stream);
  40. $mailbox = $mailbox.$dm;
  41. }
  42. fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
  43. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  44. sqimap_subscribe ($imap_stream, $mailbox);
  45. }
  46. /******************************************************************************
  47. ** Subscribes to an existing folder
  48. ******************************************************************************/
  49. function sqimap_subscribe ($imap_stream, $mailbox) {
  50. fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
  51. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  52. }
  53. /******************************************************************************
  54. ** Unsubscribes to an existing folder
  55. ******************************************************************************/
  56. function sqimap_unsubscribe ($imap_stream, $mailbox) {
  57. fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
  58. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  59. }
  60. /******************************************************************************
  61. ** This function simply deletes the given folder
  62. ******************************************************************************/
  63. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  64. fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
  65. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  66. sqimap_unsubscribe ($imap_stream, $mailbox);
  67. }
  68. /******************************************************************************
  69. ** Returns sorted mailbox lists in several different ways.
  70. ** The array returned looks like this:
  71. ******************************************************************************/
  72. function sqimap_mailbox_list ($imap_stream) {
  73. global $special_folders, $list_special_folders_first;
  74. if (!function_exists ("ary_sort"))
  75. include ("../functions/array.php");
  76. $dm = sqimap_get_delimiter ($imap_stream);
  77. fputs ($imap_stream, "a001 LIST \"\" INBOX\r\n");
  78. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  79. $g = 0;
  80. $phase = "inbox";
  81. for ($i = 0; $i < count($read_ary); $i++) {
  82. if (substr ($read_ary[$i], 0, 4) != "a001") {
  83. $boxes[$g]["raw"] = $read_ary[$i];
  84. $mailbox = find_mailbox_name($read_ary[$i]);
  85. $dm_count = countCharInString($mailbox, $dm);
  86. if (substr($mailbox, -1) == $dm)
  87. $dm_count--;
  88. for ($j = 0; $j < $dm_count; $j++)
  89. $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
  90. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  91. $boxes[$g]["unformatted-dm"] = $mailbox;
  92. if (substr($mailbox, -1) == $dm)
  93. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  94. $boxes[$g]["unformatted"] = $mailbox;
  95. $boxes[$g]["id"] = $g;
  96. /** Now lets get the flags for this mailbox **/
  97. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  98. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  99. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  100. $flags = substr($flags, 0, strpos($flags, ")"));
  101. $flags = str_replace("\\", "", $flags);
  102. $flags = trim(strtolower($flags));
  103. if ($flags) {
  104. $boxes[$g]["flags"] = explode(" ", $flags);
  105. }
  106. }
  107. $g++;
  108. if (!$read_ary[$i+1]) {
  109. if ($phase == "inbox") {
  110. fputs ($imap_stream, "a001 LSUB \"\" *\r\n");
  111. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  112. $phase = "lsub";
  113. $i--;
  114. }
  115. }
  116. }
  117. $original = $boxes;
  118. /** Get the folders into lower case so sorting is not case sensative */
  119. for ($i = 0; $i < count($original); $i++) {
  120. $boxes[$i]["unformatted"] = strtolower($boxes[$i]["unformatted"]);
  121. }
  122. /** Sort them **/
  123. $boxes = ary_sort($boxes, "unformatted", 1);
  124. /** Get them back from the original array, still sorted by the id **/
  125. for ($i = 0; $i < count($boxes); $i++) {
  126. for ($j = 0; $j < count($original); $j++) {
  127. if ($boxes[$i]["id"] == $original[$j]["id"]) {
  128. $boxes[$i] = $original[$j];
  129. }
  130. }
  131. }
  132. for ($i = 0; $i < count($boxes); $i++) {
  133. if ($boxes[$i]["unformatted"] == $special_folders[0]) {
  134. $boxesnew[0] = $boxes[$i];
  135. $boxes[$i]["used"] = true;
  136. }
  137. }
  138. if ($list_special_folders_first == true) {
  139. for ($i = 0; $i < count($boxes); $i++) {
  140. for ($j = 1; $j < count($special_folders); $j++) {
  141. if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
  142. $pos = count($boxesnew);
  143. $boxesnew[$pos] = $boxes[$i];
  144. $boxes[$i]["used"] = true;
  145. }
  146. }
  147. }
  148. }
  149. for ($i = 0; $i < count($boxes); $i++) {
  150. if (($boxes[$i]["unformatted"] != $special_folders[0]) &&
  151. ($boxes[$i]["used"] == false)) {
  152. $pos = count($boxesnew);
  153. $boxesnew[$pos] = $boxes[$i];
  154. $boxes[$i]["used"] = true;
  155. }
  156. }
  157. return $boxesnew;
  158. }
  159. ?>