imap_mailbox.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. if (!isset($load_prefs_php)) include "../src/load_prefs.php";
  103. else global $folder_prefix;
  104. if (!function_exists ("ary_sort")) include "../functions/array.php";
  105. $dm = sqimap_get_delimiter ($imap_stream);
  106. /** LIST array **/
  107. fputs ($imap_stream, "a001 LIST \"\" \"$folder_prefix*\"\r\n");
  108. $list_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  109. for ($i=0;$i < count($list_ary); $i++) {
  110. $sorted_list_ary[$i]["name"] = find_mailbox_name($list_ary[$i]);
  111. $sorted_list_ary[$i]["raw"] = $list_ary[$i];
  112. if ($sorged_list_ary[$i]["name"] == "INBOX")
  113. $inbox_in_list = true;
  114. }
  115. if (isset($sorted_list_ary)) {
  116. $list_sorted = array_cleave ($sorted_list_ary, "name");
  117. asort($list_sorted);
  118. }
  119. /** LSUB array **/
  120. $inbox_subscribed = false;
  121. fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
  122. $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  123. for ($i=0;$i < count($lsub_ary); $i++) {
  124. $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
  125. if (substr($sorted_lsub_ary[$i], -1) == $dm)
  126. $sorted_lsub_ary[$i] = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  127. if ($sorted_lsub_ary[$i] == "INBOX")
  128. $inbox_subscribed = true;
  129. }
  130. if (isset($sorted_lsub_ary)) {
  131. sort($sorted_lsub_ary);
  132. }
  133. /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
  134. if ($inbox_subscribed == false || $inbox_in_list == false) {
  135. fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
  136. $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  137. $merged[0] = $inbox_ary[0];
  138. $k = 1;
  139. } else {
  140. $k = 0;
  141. }
  142. $i = $j = 0;
  143. if (isset($list_sorted) && isset($sorted_lsub_ary)) {
  144. reset ($list_sorted);
  145. for (reset($list_sorted); $key = key($list_sorted), isset($key);) {
  146. if ($sorted_lsub_ary[$i] == $list_sorted[$key]) {
  147. $merged[$k] = $sorted_list_ary[$key]["raw"];
  148. $k++;
  149. $i++;
  150. next($list_sorted);
  151. } else if ($sorted_lsub_ary[$i] < $list_sorted[$key]) {
  152. $i++;
  153. } else {
  154. next($list_sorted);
  155. }
  156. }
  157. }
  158. return sqimap_mailbox_parse ($merged, $dm);
  159. }
  160. /*
  161. $original = $boxes;
  162. for ($i = 0; $i < count($original); $i++) {
  163. $boxes[$i]["unformatted"] = strtolower($boxes[$i]["unformatted"]);
  164. }
  165. $boxes = ary_sort($boxes, "unformatted", 1);
  166. for ($i = 0; $i < count($boxes); $i++) {
  167. for ($j = 0; $j < count($original); $j++) {
  168. if ($boxes[$i]["id"] == $original[$j]["id"]) {
  169. $boxes[$i] = $original[$j];
  170. }
  171. }
  172. }
  173. for ($i = 0; $i < count($boxes); $i++) {
  174. if ($boxes[$i]["unformatted"] == $special_folders[0]) {
  175. $boxesnew[0] = $boxes[$i];
  176. $boxes[$i]["used"] = true;
  177. }
  178. }
  179. if ($list_special_folders_first == true) {
  180. for ($i = 0; $i < count($boxes); $i++) {
  181. for ($j = 1; $j < count($special_folders); $j++) {
  182. if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
  183. $pos = count($boxesnew);
  184. $boxesnew[$pos] = $boxes[$i];
  185. $boxes[$i]["used"] = true;
  186. }
  187. }
  188. }
  189. }
  190. for ($i = 0; $i < count($boxes); $i++) {
  191. if (($boxes[$i]["unformatted"] != $special_folders[0]) &&
  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. /******************************************************************************
  202. ** Returns a list of all folders, subscribed or not
  203. ******************************************************************************/
  204. function sqimap_mailbox_list_all ($imap_stream) {
  205. global $special_folders, $list_special_folders_first, $folder_prefix;
  206. if (!function_exists ("ary_sort"))
  207. include ("../functions/array.php");
  208. $dm = sqimap_get_delimiter ($imap_stream);
  209. fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
  210. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  211. $g = 0;
  212. $phase = "inbox";
  213. for ($i = 0; $i < count($read_ary); $i++) {
  214. if (substr ($read_ary[$i], 0, 4) != "a001") {
  215. $boxes[$g]["raw"] = $read_ary[$i];
  216. $mailbox = find_mailbox_name($read_ary[$i]);
  217. $dm_count = countCharInString($mailbox, $dm);
  218. if (substr($mailbox, -1) == $dm)
  219. $dm_count--;
  220. for ($j = 0; $j < $dm_count; $j++)
  221. $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
  222. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  223. $boxes[$g]["unformatted-dm"] = $mailbox;
  224. if (substr($mailbox, -1) == $dm)
  225. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  226. $boxes[$g]["unformatted"] = $mailbox;
  227. $boxes[$g]["id"] = $g;
  228. /** Now lets get the flags for this mailbox **/
  229. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  230. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  231. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  232. $flags = substr($flags, 0, strpos($flags, ")"));
  233. $flags = str_replace("\\", "", $flags);
  234. $flags = trim(strtolower($flags));
  235. if ($flags) {
  236. $boxes[$g]["flags"] = explode(" ", $flags);
  237. }
  238. }
  239. $g++;
  240. }
  241. $boxes = ary_sort ($boxes, "unformatted", 1);
  242. return $boxes;
  243. }
  244. ?>