imap_mailbox.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /** LUKE: This introduced errors.. do a check first **/
  165. if (substr($lsub_ary[count($lsub_ary)-1], 0, 4) == "* OK") {
  166. unset($lsub_ary[count($lsub_ary)-1]);
  167. }
  168. for ($i=0;$i < count($lsub_ary); $i++) {
  169. $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
  170. if ($sorted_lsub_ary[$i] == "INBOX")
  171. $inbox_subscribed = true;
  172. }
  173. $new_ary = array();
  174. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  175. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  176. $new_ary[] = $sorted_lsub_ary[$i];
  177. }
  178. }
  179. $sorted_lsub_ary = $new_ary;
  180. if (isset($sorted_lsub_ary)) {
  181. usort($sorted_lsub_ary, "user_strcasecmp");
  182. //sort($sorted_lsub_ary);
  183. }
  184. /** LIST array **/
  185. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  186. if (substr($sorted_lsub_ary[$i], -1) == $dm)
  187. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  188. else
  189. $mbx = $sorted_lsub_ary[$i];
  190. fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
  191. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  192. $sorted_list_ary[$i] = $read[0];
  193. if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
  194. $inbox_in_list = true;
  195. }
  196. /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
  197. if ($inbox_subscribed == false || $inbox_in_list == false) {
  198. fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
  199. $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  200. $pos = count($sorted_list_ary);
  201. $sorted_list_ary[$pos] = $inbox_ary[0];
  202. $pos = count($sorted_lsub_ary);
  203. $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
  204. }
  205. $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
  206. /** Now, lets sort for special folders **/
  207. $boxesnew = Array();
  208. // Find INBOX
  209. for ($i = 0; $i < count($boxes); $i++) {
  210. if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
  211. $boxesnew[] = $boxes[$i];
  212. $used[$i] = true;
  213. $i = count($boxes);
  214. }
  215. }
  216. if ($list_special_folders_first == true) {
  217. // Then list special folders and their subfolders
  218. for ($i = 0 ; $i < count($boxes) ; $i++) {
  219. if ($move_to_trash &&
  220. eregi("^" . quotemeta($trash_folder) . "(" .
  221. quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) {
  222. $boxesnew[] = $boxes[$i];
  223. $used[$i] = true;
  224. }
  225. elseif ($move_to_sent &&
  226. eregi("^" . quotemeta($sent_folder) . "(" .
  227. quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) {
  228. $boxesnew[] = $boxes[$i];
  229. $used[$i] = true;
  230. }
  231. }
  232. // Put INBOX.* folders ahead of the rest
  233. for ($i = 0; $i < count($boxes); $i++) {
  234. if (eregi("^inbox\.", $boxes[$i]["unformatted"]) &&
  235. (!isset($used[$i]) || $used[$i] == false)) {
  236. $boxesnew[] = $boxes[$i];
  237. $used[$i] = true;
  238. }
  239. }
  240. }
  241. // Rest of the folders
  242. for ($i = 0; $i < count($boxes); $i++) {
  243. if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
  244. (!isset($used[$i]) || $used[$i] == false)) {
  245. $boxesnew[] = $boxes[$i];
  246. $used[$i] = true;
  247. }
  248. }
  249. return $boxesnew;
  250. }
  251. /******************************************************************************
  252. ** Returns a list of all folders, subscribed or not
  253. ******************************************************************************/
  254. function sqimap_mailbox_list_all ($imap_stream) {
  255. global $list_special_folders_first, $folder_prefix;
  256. if (!function_exists ("ary_sort"))
  257. include ("../functions/array.php");
  258. $dm = sqimap_get_delimiter ($imap_stream);
  259. fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
  260. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  261. $g = 0;
  262. $phase = "inbox";
  263. for ($i = 0; $i < count($read_ary); $i++) {
  264. if (substr ($read_ary[$i], 0, 4) != "a001") {
  265. // Store the raw IMAP reply
  266. $boxes[$g]["raw"] = $read_ary[$i];
  267. // Count number of delimiters ($dm) in folder name
  268. $mailbox = find_mailbox_name($read_ary[$i]);
  269. $dm_count = countCharInString($mailbox, $dm);
  270. if (substr($mailbox, -1) == $dm)
  271. $dm_count--; // If name ends in delimiter - decrement count by one
  272. // Format folder name, but only if it's a INBOX.* or have
  273. // a parent.
  274. $boxesbyname[$mailbox] = $g;
  275. $parentfolder = readMailboxParent($mailbox, $dm);
  276. if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
  277. (ereg("^".$folder_prefix, $mailbox)) ||
  278. ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  279. if ($dm_count)
  280. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
  281. else
  282. $boxes[$g]["formatted"] = '';
  283. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  284. } else {
  285. $boxes[$g]["formatted"] = $mailbox;
  286. }
  287. $boxes[$g]["unformatted-dm"] = $mailbox;
  288. if (substr($mailbox, -1) == $dm)
  289. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  290. $boxes[$g]["unformatted"] = $mailbox;
  291. $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
  292. $boxes[$g]["id"] = $g;
  293. /** Now lets get the flags for this mailbox **/
  294. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  295. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  296. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  297. $flags = substr($flags, 0, strpos($flags, ")"));
  298. $flags = str_replace("\\", "", $flags);
  299. $flags = trim(strtolower($flags));
  300. if ($flags) {
  301. $boxes[$g]["flags"] = explode(" ", $flags);
  302. }
  303. }
  304. $g++;
  305. }
  306. if(is_array($boxes)) {
  307. $boxes = ary_sort ($boxes, "unformatted", 1);
  308. }
  309. return $boxes;
  310. }
  311. ?>