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