imap_mailbox.php 12 KB

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