imap_mailbox.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\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\"\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\"\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\"\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\"\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. sqimap_unsubscribe ($imap_stream, $mailbox);
  65. fputs ($imap_stream, "a001 DELETE \"$mailbox\"\n");
  66. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  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\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. if (substr($mailbox, -1) == $dm)
  92. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  93. $boxes[$g]["unformatted"] = $mailbox;
  94. $boxes[$g]["id"] = $g;
  95. /** Now lets get the flags for this mailbox **/
  96. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\n");
  97. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  98. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  99. $flags = substr($flags, 0, strpos($flags, ")"));
  100. $flags = str_replace("\\", "", $flags);
  101. $flags = trim(strtolower($flags));
  102. if ($flags) {
  103. $boxes[$g]["flags"] = explode(" ", $flags);
  104. }
  105. }
  106. $g++;
  107. if (!$read_ary[$i+1]) {
  108. if ($phase == "inbox") {
  109. fputs ($imap_stream, "a001 LSUB \"\" *\n");
  110. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  111. $phase = "lsub";
  112. $i--;
  113. }
  114. }
  115. }
  116. $original = $boxes;
  117. /** Get the folders into lower case so sorting is not case sensative */
  118. for ($i = 0; $i < count($original); $i++) {
  119. $boxes[$i]["unformatted"] = strtolower($boxes[$i]["unformatted"]);
  120. }
  121. /** Sort them **/
  122. $boxes = ary_sort($boxes, "unformatted", 1);
  123. /** Get them back from the original array, still sorted by the id **/
  124. for ($i = 0; $i < count($boxes); $i++) {
  125. for ($j = 0; $j < count($original); $j++) {
  126. if ($boxes[$i]["id"] == $original[$j]["id"]) {
  127. $boxes[$i] = $original[$j];
  128. }
  129. }
  130. }
  131. for ($i = 0; $i < count($boxes); $i++) {
  132. if ($boxes[$i]["unformatted"] == $special_folders[0]) {
  133. $boxesnew[0] = $boxes[$i];
  134. $boxes[$i]["used"] = true;
  135. }
  136. }
  137. if ($list_special_folders_first == true) {
  138. for ($i = 0; $i < count($boxes); $i++) {
  139. for ($j = 1; $j < count($special_folders); $j++) {
  140. if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
  141. $pos = count($boxesnew);
  142. $boxesnew[$pos] = $boxes[$i];
  143. $boxes[$i]["used"] = true;
  144. }
  145. }
  146. }
  147. }
  148. for ($i = 0; $i < count($boxes); $i++) {
  149. if (($boxes[$i]["unformatted"] != $special_folders[0]) &&
  150. ($boxes[$i]["used"] == false)) {
  151. $pos = count($boxesnew);
  152. $boxesnew[$pos] = $boxes[$i];
  153. $boxes[$i]["used"] = true;
  154. }
  155. }
  156. return $boxesnew;
  157. }
  158. ?>