imap_mailbox.php 11 KB

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