imap_mailbox.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  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 $load_prefs_php, $prefs_php, $config_php, $data_dir, $username;
  74. if (!isset($load_prefs_php))
  75. include "../src/load_prefs.php";
  76. else
  77. global $folder_prefix;
  78. global $special_folders, $list_special_folders_first, $default_folder_prefix;
  79. if (!function_exists ("ary_sort"))
  80. include ("../functions/array.php");
  81. $dm = sqimap_get_delimiter ($imap_stream);
  82. fputs ($imap_stream, "a001 LIST \"\" INBOX\r\n");
  83. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  84. $g = 0;
  85. $phase = "inbox";
  86. for ($i = 0; $i < count($read_ary); $i++) {
  87. if (substr ($read_ary[$i], 0, 4) != "a001") {
  88. $boxes[$g]["raw"] = $read_ary[$i];
  89. $mailbox = find_mailbox_name($read_ary[$i]);
  90. $dm_count = countCharInString($mailbox, $dm);
  91. if (substr($mailbox, -1) == $dm)
  92. $dm_count--;
  93. for ($j = 0; $j < $dm_count; $j++)
  94. $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
  95. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  96. $boxes[$g]["unformatted-dm"] = $mailbox;
  97. if (substr($mailbox, -1) == $dm)
  98. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  99. $boxes[$g]["unformatted"] = $mailbox;
  100. $boxes[$g]["id"] = $g;
  101. /** Now lets get the flags for this mailbox **/
  102. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  103. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  104. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  105. $flags = substr($flags, 0, strpos($flags, ")"));
  106. $flags = str_replace("\\", "", $flags);
  107. $flags = trim(strtolower($flags));
  108. if ($flags) {
  109. $boxes[$g]["flags"] = explode(" ", $flags);
  110. }
  111. }
  112. $g++;
  113. if (!$read_ary[$i+1]) {
  114. if ($phase == "inbox") {
  115. if ($folder_prefix && (substr($folder_prefix, -1) != $dm))
  116. $folder_prefix = $folder_prefix . $dm;
  117. fputs ($imap_stream, "a001 LSUB \"$folder_prefix\" *\r\n");
  118. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  119. $phase = "lsub";
  120. $i--;
  121. }
  122. }
  123. }
  124. $original = $boxes;
  125. /** Get the folders into lower case so sorting is not case sensative */
  126. for ($i = 0; $i < count($original); $i++) {
  127. $boxes[$i]["unformatted"] = strtolower($boxes[$i]["unformatted"]);
  128. }
  129. /** Sort them **/
  130. $boxes = ary_sort($boxes, "unformatted", 1);
  131. /** Get them back from the original array, still sorted by the id **/
  132. for ($i = 0; $i < count($boxes); $i++) {
  133. for ($j = 0; $j < count($original); $j++) {
  134. if ($boxes[$i]["id"] == $original[$j]["id"]) {
  135. $boxes[$i] = $original[$j];
  136. }
  137. }
  138. }
  139. for ($i = 0; $i < count($boxes); $i++) {
  140. if ($boxes[$i]["unformatted"] == $special_folders[0]) {
  141. $boxesnew[0] = $boxes[$i];
  142. $boxes[$i]["used"] = true;
  143. }
  144. }
  145. if ($list_special_folders_first == true) {
  146. for ($i = 0; $i < count($boxes); $i++) {
  147. for ($j = 1; $j < count($special_folders); $j++) {
  148. if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
  149. $pos = count($boxesnew);
  150. $boxesnew[$pos] = $boxes[$i];
  151. $boxes[$i]["used"] = true;
  152. }
  153. }
  154. }
  155. }
  156. for ($i = 0; $i < count($boxes); $i++) {
  157. if (($boxes[$i]["unformatted"] != $special_folders[0]) &&
  158. ($boxes[$i]["used"] == false)) {
  159. $pos = count($boxesnew);
  160. $boxesnew[$pos] = $boxes[$i];
  161. $boxes[$i]["used"] = true;
  162. }
  163. }
  164. return $boxesnew;
  165. }
  166. /******************************************************************************
  167. ** Returns a list of all folders, subscribed or not
  168. ******************************************************************************/
  169. function sqimap_mailbox_list_all ($imap_stream) {
  170. global $special_folders, $list_special_folders_first, $folder_prefix;
  171. if (!function_exists ("ary_sort"))
  172. include ("../functions/array.php");
  173. $dm = sqimap_get_delimiter ($imap_stream);
  174. fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
  175. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  176. $g = 0;
  177. $phase = "inbox";
  178. for ($i = 0; $i < count($read_ary); $i++) {
  179. if (substr ($read_ary[$i], 0, 4) != "a001") {
  180. $boxes[$g]["raw"] = $read_ary[$i];
  181. $mailbox = find_mailbox_name($read_ary[$i]);
  182. $dm_count = countCharInString($mailbox, $dm);
  183. if (substr($mailbox, -1) == $dm)
  184. $dm_count--;
  185. for ($j = 0; $j < $dm_count; $j++)
  186. $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
  187. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  188. $boxes[$g]["unformatted-dm"] = $mailbox;
  189. if (substr($mailbox, -1) == $dm)
  190. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  191. $boxes[$g]["unformatted"] = $mailbox;
  192. $boxes[$g]["id"] = $g;
  193. /** Now lets get the flags for this mailbox **/
  194. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  195. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  196. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  197. $flags = substr($flags, 0, strpos($flags, ")"));
  198. $flags = str_replace("\\", "", $flags);
  199. $flags = trim(strtolower($flags));
  200. if ($flags) {
  201. $boxes[$g]["flags"] = explode(" ", $flags);
  202. }
  203. }
  204. $g++;
  205. }
  206. $boxes = ary_sort ($boxes, "unformatted", 1);
  207. return $boxes;
  208. }
  209. ?>