imap_mailbox.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 = $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, $imap_server_type;
  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. if ($imap_server_type == "uw") {
  132. fputs ($imap_stream, "a001 LSUB \"~\" \"*\"\r\n");
  133. }
  134. else {
  135. fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
  136. }
  137. $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  138. for ($i=0;$i < count($lsub_ary); $i++) {
  139. $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
  140. if ($sorted_lsub_ary[$i] == "INBOX")
  141. $inbox_subscribed = true;
  142. }
  143. $new_ary = array();
  144. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  145. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  146. $new_ary[] = $sorted_lsub_ary[$i];
  147. }
  148. }
  149. $sorted_lsub_ary = $new_ary;
  150. if (isset($sorted_lsub_ary)) {
  151. sort($sorted_lsub_ary);
  152. }
  153. /** LIST array **/
  154. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  155. if (substr($sorted_lsub_ary[$i], -1) == $dm)
  156. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  157. else
  158. $mbx = $sorted_lsub_ary[$i];
  159. fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
  160. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  161. $sorted_list_ary[$i] = $read[0];
  162. if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
  163. $inbox_in_list = true;
  164. }
  165. /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
  166. if ($inbox_subscribed == false || $inbox_in_list == false) {
  167. fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
  168. $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  169. $pos = count($sorted_list_ary);
  170. $sorted_list_ary[$pos] = $inbox_ary[0];
  171. $pos = count($sorted_lsub_ary);
  172. $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
  173. }
  174. $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
  175. /** Now, lets sort for special folders **/
  176. for ($i = 0; $i < count($boxes); $i++) {
  177. if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
  178. $boxesnew[0] = $boxes[$i];
  179. $boxes[$i]["used"] = true;
  180. $i = count($boxes);
  181. }
  182. }
  183. if ($list_special_folders_first == true) {
  184. for ($i = count($boxes)-1; $i >= 0 ; $i--) {
  185. if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
  186. $pos = count($boxesnew);
  187. $boxesnew[$pos] = $boxes[$i];
  188. $boxes[$i]["used"] = true;
  189. $trash_found = true;
  190. }
  191. else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
  192. $pos = count($boxesnew);
  193. $boxesnew[$pos] = $boxes[$i];
  194. $boxes[$i]["used"] = true;
  195. $sent_found = true;
  196. }
  197. if (($sent_found && $trash_found) || ($sent_found && !$move_to_trash) || ($trash_found && !$move_to_sent) || (!$move_to_sent && !$move_to_trash))
  198. $i = -1;
  199. }
  200. }
  201. for ($i = 0; $i < count($boxes); $i++) {
  202. if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
  203. ($boxes[$i]["used"] == false)) {
  204. $pos = count($boxesnew);
  205. $boxesnew[$pos] = $boxes[$i];
  206. $boxes[$i]["used"] = true;
  207. }
  208. }
  209. return $boxesnew;
  210. }
  211. /******************************************************************************
  212. ** Returns a list of all folders, subscribed or not
  213. ******************************************************************************/
  214. function sqimap_mailbox_list_all ($imap_stream) {
  215. global $list_special_folders_first, $folder_prefix;
  216. if (!function_exists ("ary_sort"))
  217. include ("../functions/array.php");
  218. $dm = sqimap_get_delimiter ($imap_stream);
  219. fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
  220. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  221. $g = 0;
  222. $phase = "inbox";
  223. for ($i = 0; $i < count($read_ary); $i++) {
  224. if (substr ($read_ary[$i], 0, 4) != "a001") {
  225. $boxes[$g]["raw"] = $read_ary[$i];
  226. $mailbox = find_mailbox_name($read_ary[$i]);
  227. $dm_count = countCharInString($mailbox, $dm);
  228. if (substr($mailbox, -1) == $dm)
  229. $dm_count--;
  230. for ($j = 0; $j < $dm_count; $j++)
  231. $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
  232. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  233. $boxes[$g]["unformatted-dm"] = $mailbox;
  234. if (substr($mailbox, -1) == $dm)
  235. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  236. $boxes[$g]["unformatted"] = $mailbox;
  237. $boxes[$g]["id"] = $g;
  238. /** Now lets get the flags for this mailbox **/
  239. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  240. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  241. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  242. $flags = substr($flags, 0, strpos($flags, ")"));
  243. $flags = str_replace("\\", "", $flags);
  244. $flags = trim(strtolower($flags));
  245. if ($flags) {
  246. $boxes[$g]["flags"] = explode(" ", $flags);
  247. }
  248. }
  249. $g++;
  250. }
  251. if ($boxes) {
  252. $boxes = ary_sort ($boxes, "unformatted", 1);
  253. }
  254. return $boxes;
  255. }
  256. ?>