imap_mailbox.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 is a recursive function that checks to see if the folder has any
  62. ** subfolders, and if so it calls itself on the subfolders first, then
  63. ** removes the parent folder.
  64. ******************************************************************************/
  65. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  66. global $boxes;
  67. $dm = sqimap_get_delimiter($imap_stream);
  68. for ($i = 0; $i < count($boxes); $i++) {
  69. if (strstr($boxes[$i]["unformatted"], $mailbox . $dm)) {
  70. $new_delete = $boxes[$i]["unformatted"];
  71. $boxes = removeElement($boxes, $i);
  72. // sqimap_mailbox_delete ($imap_stream, $new_delete);
  73. }
  74. }
  75. sqimap_unsubscribe ($imap_stream, $mailbox);
  76. fputs ($imap_stream, "a001 DELETE \"$mailbox\"\n");
  77. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  78. }
  79. /******************************************************************************
  80. ** Returns sorted mailbox lists in several different ways.
  81. ** The array returned looks like this:
  82. ******************************************************************************/
  83. function sqimap_mailbox_list ($imap_stream) {
  84. global $special_folders, $list_special_folders_first;
  85. if (!function_exists ("ary_sort"))
  86. include ("../functions/array.php");
  87. $dm = sqimap_get_delimiter ($imap_stream);
  88. fputs ($imap_stream, "a001 LIST \"\" INBOX\n");
  89. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  90. $g = 0;
  91. $phase = "inbox";
  92. for ($i = 0; $i < count($read_ary); $i++) {
  93. if (substr ($read_ary[$i], 0, 4) != "a001") {
  94. $boxes[$g]["raw"] = $read_ary[$i];
  95. $mailbox = find_mailbox_name($read_ary[$i]);
  96. $dm_count = countCharInString($mailbox, $dm);
  97. if (substr($mailbox, -1) == $dm)
  98. $dm_count--;
  99. for ($j = 0; $j < $dm_count; $j++)
  100. $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
  101. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  102. if (substr($mailbox, -1) == $dm)
  103. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  104. $boxes[$g]["unformatted"] = $mailbox;
  105. $boxes[$g]["id"] = $g;
  106. /** Now lets get the flags for this mailbox **/
  107. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\n");
  108. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  109. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  110. $flags = substr($flags, 0, strpos($flags, ")"));
  111. $flags = str_replace("\\", "", $flags);
  112. $flags = trim(strtolower($flags));
  113. if ($flags) {
  114. $boxes[$g]["flags"] = explode(" ", $flags);
  115. }
  116. }
  117. $g++;
  118. if (!$read_ary[$i+1]) {
  119. if ($phase == "inbox") {
  120. fputs ($imap_stream, "a001 LSUB \"\" *\n");
  121. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  122. $phase = "lsub";
  123. $i--;
  124. }
  125. }
  126. }
  127. $original = $boxes;
  128. /** Get the folders into lower case so sorting is not case sensative */
  129. for ($i = 0; $i < count($original); $i++) {
  130. $boxes[$i]["unformatted"] = strtolower($boxes[$i]["unformatted"]);
  131. }
  132. /** Sort them **/
  133. $boxes = ary_sort($boxes, "unformatted", 1);
  134. /** Get them back from the original array, still sorted by the id **/
  135. for ($i = 0; $i < count($boxes); $i++) {
  136. for ($j = 0; $j < count($original); $j++) {
  137. if ($boxes[$i]["id"] == $original[$j]["id"]) {
  138. $boxes[$i] = $original[$j];
  139. }
  140. }
  141. }
  142. for ($i = 0; $i < count($boxes); $i++) {
  143. if ($boxes[$i]["unformatted"] == $special_folders[0]) {
  144. $boxesnew[0] = $boxes[$i];
  145. $boxes[$i]["used"] = true;
  146. }
  147. }
  148. if ($list_special_folders_first == true) {
  149. for ($i = 0; $i < count($boxes); $i++) {
  150. for ($j = 1; $j < count($special_folders); $j++) {
  151. if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
  152. $pos = count($boxesnew);
  153. $boxesnew[$pos] = $boxes[$i];
  154. $boxes[$i]["used"] = true;
  155. }
  156. }
  157. }
  158. }
  159. for ($i = 0; $i < count($boxes); $i++) {
  160. if (($boxes[$i]["unformatted"] != $special_folders[0]) &&
  161. ($boxes[$i]["used"] == false)) {
  162. $pos = count($boxesnew);
  163. $boxesnew[$pos] = $boxes[$i];
  164. $boxes[$i]["used"] = true;
  165. }
  166. }
  167. return $boxesnew;
  168. }
  169. ?>