imap_mailbox.php 14 KB

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