imap_mailbox.php 12 KB

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