imap_mailbox.php 11 KB

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