imap_mailbox.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. global $imap_server_type;
  58. /** This is a hack for UW server
  59. ** Sometimes a folder will have a / at the end. If that's the case,
  60. ** the unsubscribe doesn't work for a box named "mailbox/". We have
  61. ** to strip off the / at the end. There may be a better way of doing
  62. ** this, but this is the best I've found so far. (lme - April 26, 2000)
  63. **/
  64. if ($imap_server_type == "uw") {
  65. if (substr($mailbox, -1) == "/") {
  66. $mailbox = substr($mailbox, 0, strlen($mailbox)-1);
  67. }
  68. }
  69. fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
  70. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  71. }
  72. /******************************************************************************
  73. ** This function simply deletes the given folder
  74. ******************************************************************************/
  75. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  76. fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
  77. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  78. sqimap_unsubscribe ($imap_stream, $mailbox);
  79. }
  80. /******************************************************************************
  81. ** Formats a mailbox into 4 parts for the $boxes array
  82. ******************************************************************************/
  83. function sqimap_mailbox_parse ($line, $dm) {
  84. global $folder_prefix;
  85. for ($g=0; $g < count($line); $g++) {
  86. $boxes[$g]["raw"] = $line[$g];
  87. $mailbox = find_mailbox_name($line[$g]);
  88. $dm_count = countCharInString($mailbox, $dm);
  89. if (substr($mailbox, -1) == $dm)
  90. $dm_count--;
  91. for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++)
  92. $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
  93. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  94. $boxes[$g]["unformatted-dm"] = $mailbox;
  95. if (substr($mailbox, -1) == $dm)
  96. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  97. $boxes[$g]["unformatted"] = $mailbox;
  98. $boxes[$g]["id"] = $g;
  99. $flags = substr($line[$g], strpos($line[$g], "(")+1);
  100. $flags = substr($flags, 0, strpos($flags, ")"));
  101. $flags = str_replace("\\", "", $flags);
  102. $flags = trim(strtolower($flags));
  103. if ($flags) {
  104. $boxes[$g]["flags"] = explode(" ", $flags);
  105. }
  106. /**** I'm not sure why this was even in here to begin with.. (lme)
  107. for ($i=0; $i < count($boxes[$g]["flags"]); $i++) {
  108. if ($boxes[$g]["flags"][$i] == "noselect") {
  109. $boxes[$g]["unformatted-dm"] = $boxes[$g]["unformatted-dm"].$dm;
  110. // echo $boxes[$g]["unformatted-dm"]." - debug<br>";
  111. }
  112. }
  113. ****/
  114. }
  115. return $boxes;
  116. }
  117. /******************************************************************************
  118. ** Returns sorted mailbox lists in several different ways.
  119. ** The array returned looks like this:
  120. ******************************************************************************/
  121. function sqimap_mailbox_list ($imap_stream) {
  122. global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
  123. global $trash_folder, $sent_folder;
  124. global $move_to_trash, $move_to_sent;
  125. $inbox_in_list = false;
  126. $inbox_subscribed = false;
  127. if (!isset($load_prefs_php)) include "../src/load_prefs.php";
  128. else global $folder_prefix;
  129. if (!function_exists ("ary_sort")) include "../functions/array.php";
  130. $dm = sqimap_get_delimiter ($imap_stream);
  131. /** LIST array **/
  132. fputs ($imap_stream, "a001 LIST \"\" \"$folder_prefix*\"\r\n");
  133. $list_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  134. for ($i=0;$i < count($list_ary); $i++) {
  135. $sorted_list_ary[$i]["name"] = find_mailbox_name($list_ary[$i]);
  136. $sorted_list_ary[$i]["raw"] = $list_ary[$i];
  137. if ($sorted_list_ary[$i]["name"] == "INBOX")
  138. $inbox_in_list = true;
  139. }
  140. if (isset($sorted_list_ary)) {
  141. $list_sorted = array_cleave ($sorted_list_ary, "name");
  142. asort($list_sorted);
  143. }
  144. /** LSUB array **/
  145. $inbox_subscribed = false;
  146. fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
  147. $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  148. for ($i=0;$i < count($lsub_ary); $i++) {
  149. $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
  150. if (substr($sorted_lsub_ary[$i], -1) == $dm)
  151. $sorted_lsub_ary[$i] = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  152. if ($sorted_lsub_ary[$i] == "INBOX")
  153. $inbox_subscribed = true;
  154. }
  155. if (isset($sorted_lsub_ary)) {
  156. sort($sorted_lsub_ary);
  157. }
  158. /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
  159. if ($inbox_subscribed == false || $inbox_in_list == false) {
  160. fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
  161. $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  162. $merged[0] = $inbox_ary[0];
  163. $k = 1;
  164. } else {
  165. $k = 0;
  166. }
  167. $i = $j = 0;
  168. if (isset($list_sorted) && isset($sorted_lsub_ary)) {
  169. reset ($list_sorted);
  170. for (reset($list_sorted); $key = key($list_sorted), isset($key);) {
  171. if ($sorted_lsub_ary[$i] == $list_sorted[$key]) {
  172. $merged[$k] = $sorted_list_ary[$key]["raw"];
  173. $k++;
  174. $i++;
  175. next($list_sorted);
  176. } else if ($sorted_lsub_ary[$i] < $list_sorted[$key]) {
  177. $i++;
  178. } else {
  179. next($list_sorted);
  180. }
  181. }
  182. }
  183. $boxes = sqimap_mailbox_parse ($merged, $dm);
  184. /** Now, lets sort for special folders **/
  185. for ($i = 0; $i < count($boxes); $i++) {
  186. if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
  187. $boxesnew[0] = $boxes[$i];
  188. $boxes[$i]["used"] = true;
  189. }
  190. }
  191. if ($list_special_folders_first == true) {
  192. for ($i = 0; $i < count($boxes); $i++) {
  193. if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
  194. $pos = count($boxesnew);
  195. $boxesnew[$pos] = $boxes[$i];
  196. $boxes[$i]["used"] = true;
  197. }
  198. else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
  199. $pos = count($boxesnew);
  200. $boxesnew[$pos] = $boxes[$i];
  201. $boxes[$i]["used"] = true;
  202. }
  203. }
  204. }
  205. for ($i = 0; $i < count($boxes); $i++) {
  206. if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
  207. ($boxes[$i]["used"] == false)) {
  208. $pos = count($boxesnew);
  209. $boxesnew[$pos] = $boxes[$i];
  210. $boxes[$i]["used"] = true;
  211. }
  212. }
  213. return $boxesnew;
  214. }
  215. /******************************************************************************
  216. ** Returns a list of all folders, subscribed or not
  217. ******************************************************************************/
  218. function sqimap_mailbox_list_all ($imap_stream) {
  219. global $list_special_folders_first, $folder_prefix;
  220. if (!function_exists ("ary_sort"))
  221. include ("../functions/array.php");
  222. $dm = sqimap_get_delimiter ($imap_stream);
  223. fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
  224. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  225. $g = 0;
  226. $phase = "inbox";
  227. for ($i = 0; $i < count($read_ary); $i++) {
  228. if (substr ($read_ary[$i], 0, 4) != "a001") {
  229. $boxes[$g]["raw"] = $read_ary[$i];
  230. $mailbox = find_mailbox_name($read_ary[$i]);
  231. $dm_count = countCharInString($mailbox, $dm);
  232. if (substr($mailbox, -1) == $dm)
  233. $dm_count--;
  234. for ($j = 0; $j < $dm_count; $j++)
  235. $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
  236. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  237. $boxes[$g]["unformatted-dm"] = $mailbox;
  238. if (substr($mailbox, -1) == $dm)
  239. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  240. $boxes[$g]["unformatted"] = $mailbox;
  241. $boxes[$g]["id"] = $g;
  242. /** Now lets get the flags for this mailbox **/
  243. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  244. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  245. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  246. $flags = substr($flags, 0, strpos($flags, ")"));
  247. $flags = str_replace("\\", "", $flags);
  248. $flags = trim(strtolower($flags));
  249. if ($flags) {
  250. $boxes[$g]["flags"] = explode(" ", $flags);
  251. }
  252. }
  253. $g++;
  254. }
  255. $boxes = ary_sort ($boxes, "unformatted", 1);
  256. return $boxes;
  257. }
  258. ?>