imap_mailbox.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
  20. $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  21. if ($mailbox) {
  22. if (ereg ("$mailbox", $mbx[0])) {
  23. return true;
  24. } else {
  25. return false;
  26. }
  27. }
  28. }
  29. /******************************************************************************
  30. ** Selects a mailbox
  31. ******************************************************************************/
  32. function sqimap_mailbox_select ($imap_stream, $mailbox, $hide) {
  33. fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
  34. $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  35. }
  36. /******************************************************************************
  37. ** Creates a folder
  38. ******************************************************************************/
  39. function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
  40. if (strtolower($type) == "noselect") {
  41. $dm = sqimap_get_delimiter($imap_stream);
  42. $mailbox = $mailbox.$dm;
  43. }
  44. fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
  45. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  46. sqimap_subscribe ($imap_stream, $mailbox);
  47. }
  48. /******************************************************************************
  49. ** Subscribes to an existing folder
  50. ******************************************************************************/
  51. function sqimap_subscribe ($imap_stream, $mailbox) {
  52. fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
  53. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  54. }
  55. /******************************************************************************
  56. ** Unsubscribes to an existing folder
  57. ******************************************************************************/
  58. function sqimap_unsubscribe ($imap_stream, $mailbox) {
  59. global $imap_server_type;
  60. fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
  61. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  62. }
  63. /******************************************************************************
  64. ** This function simply deletes the given folder
  65. ******************************************************************************/
  66. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  67. fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
  68. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  69. sqimap_unsubscribe ($imap_stream, $mailbox);
  70. }
  71. /******************************************************************************
  72. ** Formats a mailbox into 4 parts for the $boxes array
  73. ******************************************************************************/
  74. function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
  75. global $folder_prefix;
  76. for ($g=0; $g < count($line); $g++) {
  77. $boxes[$g]["raw"] = $line[$g];
  78. $mailbox = $line_lsub[$g];
  79. $dm_count = countCharInString($mailbox, $dm);
  80. if (substr($mailbox, -1) == $dm)
  81. $dm_count--;
  82. for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++)
  83. $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
  84. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  85. $boxes[$g]["unformatted-dm"] = $mailbox;
  86. if (substr($mailbox, -1) == $dm)
  87. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  88. $boxes[$g]["unformatted"] = $mailbox;
  89. $boxes[$g]["id"] = $g;
  90. $flags = substr($line[$g], strpos($line[$g], "(")+1);
  91. $flags = substr($flags, 0, strpos($flags, ")"));
  92. $flags = str_replace("\\", "", $flags);
  93. $flags = trim(strtolower($flags));
  94. if ($flags) {
  95. $boxes[$g]["flags"] = explode(" ", $flags);
  96. }
  97. }
  98. return $boxes;
  99. }
  100. /******************************************************************************
  101. ** Returns sorted mailbox lists in several different ways.
  102. ** The array returned looks like this:
  103. ******************************************************************************/
  104. function sqimap_mailbox_list ($imap_stream) {
  105. global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
  106. global $trash_folder, $sent_folder;
  107. global $move_to_trash, $move_to_sent;
  108. $inbox_in_list = false;
  109. $inbox_subscribed = false;
  110. if (!isset($load_prefs_php)) include "../src/load_prefs.php";
  111. else global $folder_prefix;
  112. if (!function_exists ("ary_sort")) include "../functions/array.php";
  113. $dm = sqimap_get_delimiter ($imap_stream);
  114. /** LSUB array **/
  115. $inbox_subscribed = false;
  116. fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
  117. $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  118. for ($i=0;$i < count($lsub_ary); $i++) {
  119. $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
  120. if ($sorted_lsub_ary[$i] == "INBOX")
  121. $inbox_subscribed = true;
  122. }
  123. if (isset($sorted_lsub_ary)) {
  124. sort($sorted_lsub_ary);
  125. }
  126. /** LIST array **/
  127. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  128. if (substr($sorted_lsub_ary[$i], -1) == $dm)
  129. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  130. else
  131. $mbx = $sorted_lsub_ary[$i];
  132. fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
  133. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  134. $sorted_list_ary[$i] = $read[0];
  135. if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
  136. $inbox_in_list = true;
  137. }
  138. /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
  139. if ($inbox_subscribed == false || $inbox_in_list == false) {
  140. fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
  141. $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  142. $pos = count($sorted_list_ary);
  143. $sorted_list_ary[$pos] = $inbox_ary[0];
  144. $pos = count($sorted_lsub_ary);
  145. $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
  146. }
  147. $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
  148. /** Now, lets sort for special folders **/
  149. for ($i = 0; $i < count($boxes); $i++) {
  150. if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
  151. $boxesnew[0] = $boxes[$i];
  152. $boxes[$i]["used"] = true;
  153. $i = count($boxes);
  154. }
  155. }
  156. if ($list_special_folders_first == true) {
  157. for ($i = count($boxes)-1; $i >= 0 ; $i--) {
  158. if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
  159. $pos = count($boxesnew);
  160. $boxesnew[$pos] = $boxes[$i];
  161. $boxes[$i]["used"] = true;
  162. $trash_found = true;
  163. }
  164. else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
  165. $pos = count($boxesnew);
  166. $boxesnew[$pos] = $boxes[$i];
  167. $boxes[$i]["used"] = true;
  168. $sent_found = true;
  169. }
  170. if (($sent_found && $trash_found) || ($sent_found && !$move_to_trash) || ($trash_found && !$move_to_sent) || (!$move_to_sent && !$move_to_trash))
  171. $i = -1;
  172. }
  173. }
  174. for ($i = 0; $i < count($boxes); $i++) {
  175. if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
  176. ($boxes[$i]["used"] == false)) {
  177. $pos = count($boxesnew);
  178. $boxesnew[$pos] = $boxes[$i];
  179. $boxes[$i]["used"] = true;
  180. }
  181. }
  182. return $boxesnew;
  183. }
  184. /******************************************************************************
  185. ** Returns a list of all folders, subscribed or not
  186. ******************************************************************************/
  187. function sqimap_mailbox_list_all ($imap_stream) {
  188. global $list_special_folders_first, $folder_prefix;
  189. if (!function_exists ("ary_sort"))
  190. include ("../functions/array.php");
  191. $dm = sqimap_get_delimiter ($imap_stream);
  192. fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
  193. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  194. $g = 0;
  195. $phase = "inbox";
  196. for ($i = 0; $i < count($read_ary); $i++) {
  197. if (substr ($read_ary[$i], 0, 4) != "a001") {
  198. $boxes[$g]["raw"] = $read_ary[$i];
  199. $mailbox = find_mailbox_name($read_ary[$i]);
  200. $dm_count = countCharInString($mailbox, $dm);
  201. if (substr($mailbox, -1) == $dm)
  202. $dm_count--;
  203. for ($j = 0; $j < $dm_count; $j++)
  204. $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
  205. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  206. $boxes[$g]["unformatted-dm"] = $mailbox;
  207. if (substr($mailbox, -1) == $dm)
  208. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  209. $boxes[$g]["unformatted"] = $mailbox;
  210. $boxes[$g]["id"] = $g;
  211. /** Now lets get the flags for this mailbox **/
  212. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  213. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  214. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  215. $flags = substr($flags, 0, strpos($flags, ")"));
  216. $flags = str_replace("\\", "", $flags);
  217. $flags = trim(strtolower($flags));
  218. if ($flags) {
  219. $boxes[$g]["flags"] = explode(" ", $flags);
  220. }
  221. }
  222. $g++;
  223. }
  224. if ($boxes) {
  225. $boxes = ary_sort ($boxes, "unformatted", 1);
  226. }
  227. return $boxes;
  228. }
  229. ?>