imap_mailbox.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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,$handle_errors = true) {
  11. sqimap_mailbox_select ($imap_stream, $mailbox);
  12. fputs ($imap_stream, "a001 EXPUNGE\r\n");
  13. $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
  14. }
  15. /******************************************************************************
  16. ** Checks whether or not the specified mailbox exists
  17. ******************************************************************************/
  18. function sqimap_mailbox_exists ($imap_stream, $mailbox) {
  19. fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
  20. $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  21. if ($mailbox) {
  22. return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false
  23. }
  24. }
  25. /******************************************************************************
  26. ** Selects a mailbox
  27. ******************************************************************************/
  28. function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
  29. global $auto_expunge;
  30. fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
  31. $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  32. if ($recent) {
  33. for ($i=0; $i<count($read); $i++) {
  34. if (strpos(strtolower($read[$i]), "recent")) {
  35. $r = explode(" ", $read[$i]);
  36. }
  37. }
  38. return $r[1];
  39. }
  40. if ($auto_expunge) {
  41. fputs ($imap_stream, "a001 EXPUNGE\r\n");
  42. $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
  43. }
  44. }
  45. /******************************************************************************
  46. ** Creates a folder
  47. ******************************************************************************/
  48. function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
  49. if (strtolower($type) == "noselect") {
  50. $dm = sqimap_get_delimiter($imap_stream);
  51. $mailbox = $mailbox.$dm;
  52. }
  53. fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
  54. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  55. sqimap_subscribe ($imap_stream, $mailbox);
  56. }
  57. /******************************************************************************
  58. ** Subscribes to an existing folder
  59. ******************************************************************************/
  60. function sqimap_subscribe ($imap_stream, $mailbox) {
  61. fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
  62. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  63. }
  64. /******************************************************************************
  65. ** Unsubscribes to an existing folder
  66. ******************************************************************************/
  67. function sqimap_unsubscribe ($imap_stream, $mailbox) {
  68. global $imap_server_type;
  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. ** The four parts are:
  84. **
  85. ** raw - Raw LIST/LSUB response from the IMAP server
  86. ** formatted - nicely formatted folder name
  87. ** unformatted - unformatted, but with delimiter at end removed
  88. ** unformatted-dm - folder name as it appears in raw response
  89. ** unformatted-disp - unformatted without $folder_prefix
  90. **
  91. ******************************************************************************/
  92. function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
  93. global $folder_prefix;
  94. // Process each folder line
  95. for ($g=0; $g < count($line); $g++) {
  96. // Store the raw IMAP reply
  97. $boxes[$g]["raw"] = $line[$g];
  98. // Count number of delimiters ($dm) in folder name
  99. $mailbox = trim($line_lsub[$g]);
  100. $dm_count = countCharInString($mailbox, $dm);
  101. if (substr($mailbox, -1) == $dm)
  102. $dm_count--; // If name ends in delimiter - decrement count by one
  103. // Format folder name, but only if it's a INBOX.* or have
  104. // a parent.
  105. $boxesbyname[$mailbox] = $g;
  106. $parentfolder = readMailboxParent($mailbox, $dm);
  107. if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
  108. (ereg("^".$folder_prefix, $mailbox)) ||
  109. ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  110. $indent = $dm_count - (countCharInString($folder_prefix, $dm));
  111. if ($indent)
  112. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
  113. else
  114. $boxes[$g]["formatted"] = '';
  115. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  116. } else {
  117. $boxes[$g]["formatted"] = $mailbox;
  118. }
  119. $boxes[$g]["unformatted-dm"] = $mailbox;
  120. if (substr($mailbox, -1) == $dm)
  121. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  122. $boxes[$g]["unformatted"] = $mailbox;
  123. $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
  124. $boxes[$g]["id"] = $g;
  125. ereg("\(([^)]*)\)",$line[$g],$regs);
  126. $flags = trim(strtolower(str_replace("\\", "",$regs[1])));
  127. if ($flags) {
  128. $boxes[$g]["flags"] = explode(" ", $flags);
  129. }
  130. }
  131. return $boxes;
  132. }
  133. /* patch from dave_michmerhuizen@yahoo.com
  134. * allows case insensativity when sorting folders
  135. */
  136. function _icmp ($a, $b) {
  137. return strcasecmp($a, $b);
  138. }
  139. /******************************************************************************
  140. ** Returns sorted mailbox lists in several different ways.
  141. ** See comment on sqimap_mailbox_parse() for info about the returned array.
  142. ******************************************************************************/
  143. function sqimap_mailbox_list ($imap_stream) {
  144. global $load_prefs_php, $prefs_php, $config_php;
  145. global $data_dir, $username, $list_special_folders_first;
  146. global $trash_folder, $sent_folder;
  147. global $move_to_trash, $move_to_sent;
  148. $inbox_in_list = false;
  149. $inbox_subscribed = false;
  150. if (!isset($load_prefs_php)) include "../src/load_prefs.php";
  151. else global $folder_prefix;
  152. if (!function_exists ("ary_sort")) include "../functions/array.php";
  153. $dm = sqimap_get_delimiter ($imap_stream);
  154. /** LSUB array **/
  155. $inbox_subscribed = false;
  156. fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
  157. $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  158. /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
  159. unset($lsub_ary[count($lsub_ary)-1]);
  160. for ($i=0;$i < count($lsub_ary); $i++) {
  161. $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
  162. if ($sorted_lsub_ary[$i] == "INBOX")
  163. $inbox_subscribed = true;
  164. }
  165. $new_ary = array();
  166. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  167. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  168. $new_ary[] = $sorted_lsub_ary[$i];
  169. }
  170. }
  171. $sorted_lsub_ary = $new_ary;
  172. if (isset($sorted_lsub_ary)) {
  173. usort($sorted_lsub_ary, "_icmp");
  174. //sort($sorted_lsub_ary);
  175. }
  176. /** LIST array **/
  177. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  178. if (substr($sorted_lsub_ary[$i], -1) == $dm)
  179. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  180. else
  181. $mbx = $sorted_lsub_ary[$i];
  182. fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
  183. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  184. $sorted_list_ary[$i] = $read[0];
  185. if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
  186. $inbox_in_list = true;
  187. }
  188. /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
  189. if ($inbox_subscribed == false || $inbox_in_list == false) {
  190. fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
  191. $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  192. $pos = count($sorted_list_ary);
  193. $sorted_list_ary[$pos] = $inbox_ary[0];
  194. $pos = count($sorted_lsub_ary);
  195. $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
  196. }
  197. $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
  198. /** Now, lets sort for special folders **/
  199. $boxesnew = Array();
  200. // Find INBOX
  201. for ($i = 0; $i < count($boxes); $i++) {
  202. if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
  203. $boxesnew[] = $boxes[$i];
  204. $boxes[$i]["used"] = true;
  205. $i = count($boxes);
  206. }
  207. }
  208. if ($list_special_folders_first == true) {
  209. // Then list special folders and their subfolders
  210. for ($i = 0 ; $i <= count($boxes) ; $i++) {
  211. if ($move_to_trash &&
  212. eregi("^" . quotemeta($trash_folder) . "(" .
  213. quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) {
  214. $boxesnew[] = $boxes[$i];
  215. $boxes[$i]["used"] = true;
  216. }
  217. elseif ($move_to_sent &&
  218. eregi("^" . quotemeta($sent_folder) . "(" .
  219. quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) {
  220. $boxesnew[] = $boxes[$i];
  221. $boxes[$i]["used"] = true;
  222. }
  223. }
  224. // Put INBOX.* folders ahead of the rest
  225. for ($i = 0; $i <= count($boxes); $i++) {
  226. if (eregi("^inbox\.", $boxes[$i]["unformatted"]) &&
  227. ($boxes[$i]["used"] == false)) {
  228. $boxesnew[] = $boxes[$i];
  229. $boxes[$i]["used"] = true;
  230. }
  231. }
  232. }
  233. // Rest of the folders
  234. for ($i = 0; $i < count($boxes); $i++) {
  235. if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
  236. ($boxes[$i]["used"] == false)) {
  237. $boxesnew[] = $boxes[$i];
  238. $boxes[$i]["used"] = true;
  239. }
  240. }
  241. return $boxesnew;
  242. }
  243. /******************************************************************************
  244. ** Returns a list of all folders, subscribed or not
  245. ******************************************************************************/
  246. function sqimap_mailbox_list_all ($imap_stream) {
  247. global $list_special_folders_first, $folder_prefix;
  248. if (!function_exists ("ary_sort"))
  249. include ("../functions/array.php");
  250. $dm = sqimap_get_delimiter ($imap_stream);
  251. fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
  252. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  253. $g = 0;
  254. $phase = "inbox";
  255. for ($i = 0; $i < count($read_ary); $i++) {
  256. if (substr ($read_ary[$i], 0, 4) != "a001") {
  257. // Store the raw IMAP reply
  258. $boxes[$g]["raw"] = $read_ary[$i];
  259. // Count number of delimiters ($dm) in folder name
  260. $mailbox = find_mailbox_name($read_ary[$i]);
  261. $dm_count = countCharInString($mailbox, $dm);
  262. if (substr($mailbox, -1) == $dm)
  263. $dm_count--; // If name ends in delimiter - decrement count by one
  264. // Format folder name, but only if it's a INBOX.* or have
  265. // a parent.
  266. $boxesbyname[$mailbox] = $g;
  267. $parentfolder = readMailboxParent($mailbox, $dm);
  268. if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
  269. (ereg("^".$folder_prefix, $mailbox)) ||
  270. ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  271. if ($dm_count)
  272. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
  273. else
  274. $boxes[$g]["formatted"] = '';
  275. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  276. } else {
  277. $boxes[$g]["formatted"] = $mailbox;
  278. }
  279. $boxes[$g]["unformatted-dm"] = $mailbox;
  280. if (substr($mailbox, -1) == $dm)
  281. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  282. $boxes[$g]["unformatted"] = $mailbox;
  283. $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
  284. $boxes[$g]["id"] = $g;
  285. /** Now lets get the flags for this mailbox **/
  286. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  287. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  288. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  289. $flags = substr($flags, 0, strpos($flags, ")"));
  290. $flags = str_replace("\\", "", $flags);
  291. $flags = trim(strtolower($flags));
  292. if ($flags) {
  293. $boxes[$g]["flags"] = explode(" ", $flags);
  294. }
  295. }
  296. $g++;
  297. }
  298. if(is_array($boxes)) {
  299. $boxes = ary_sort ($boxes, "unformatted", 1);
  300. }
  301. return $boxes;
  302. }
  303. ?>