imap_mailbox.php 11 KB

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