imap_mailbox.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 ($mailbox) {
  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. /******************************************************************************
  136. ** Returns sorted mailbox lists in several different ways.
  137. ** See comment on sqimap_mailbox_parse() for info about the returned array.
  138. ******************************************************************************/
  139. function sqimap_mailbox_list ($imap_stream) {
  140. global $load_prefs_php, $prefs_php, $config_php;
  141. global $data_dir, $username, $list_special_folders_first;
  142. global $trash_folder, $sent_folder;
  143. global $move_to_trash, $move_to_sent;
  144. $inbox_in_list = false;
  145. $inbox_subscribed = false;
  146. if (!isset($load_prefs_php)) include "../src/load_prefs.php";
  147. else global $folder_prefix;
  148. if (!function_exists ("ary_sort")) include "../functions/array.php";
  149. $dm = sqimap_get_delimiter ($imap_stream);
  150. /** LSUB array **/
  151. $inbox_subscribed = false;
  152. fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
  153. $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  154. /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
  155. unset($lsub_ary[count($lsub_ary)-1]);
  156. for ($i=0;$i < count($lsub_ary); $i++) {
  157. $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
  158. if ($sorted_lsub_ary[$i] == "INBOX")
  159. $inbox_subscribed = true;
  160. }
  161. $new_ary = array();
  162. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  163. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  164. $new_ary[] = $sorted_lsub_ary[$i];
  165. }
  166. }
  167. $sorted_lsub_ary = $new_ary;
  168. if (isset($sorted_lsub_ary)) {
  169. usort($sorted_lsub_ary, "strcasecmp");
  170. //sort($sorted_lsub_ary);
  171. }
  172. /** LIST array **/
  173. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  174. if (substr($sorted_lsub_ary[$i], -1) == $dm)
  175. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  176. else
  177. $mbx = $sorted_lsub_ary[$i];
  178. fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
  179. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  180. $sorted_list_ary[$i] = $read[0];
  181. if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
  182. $inbox_in_list = true;
  183. }
  184. /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
  185. if ($inbox_subscribed == false || $inbox_in_list == false) {
  186. fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
  187. $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  188. $pos = count($sorted_list_ary);
  189. $sorted_list_ary[$pos] = $inbox_ary[0];
  190. $pos = count($sorted_lsub_ary);
  191. $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
  192. }
  193. $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
  194. /** Now, lets sort for special folders **/
  195. $boxesnew = Array();
  196. // Find INBOX
  197. for ($i = 0; $i < count($boxes); $i++) {
  198. if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
  199. $boxesnew[] = $boxes[$i];
  200. $boxes[$i]["used"] = true;
  201. $i = count($boxes);
  202. }
  203. }
  204. if ($list_special_folders_first == true) {
  205. // Then list special folders and their subfolders
  206. for ($i = 0 ; $i < count($boxes) ; $i++) {
  207. if ($move_to_trash &&
  208. eregi("^" . quotemeta($trash_folder) . "(" .
  209. quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) {
  210. $boxesnew[] = $boxes[$i];
  211. $boxes[$i]["used"] = true;
  212. }
  213. elseif ($move_to_sent &&
  214. eregi("^" . quotemeta($sent_folder) . "(" .
  215. quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) {
  216. $boxesnew[] = $boxes[$i];
  217. $boxes[$i]["used"] = true;
  218. }
  219. }
  220. // Put INBOX.* folders ahead of the rest
  221. for ($i = 0; $i < count($boxes); $i++) {
  222. if (eregi("^inbox\.", $boxes[$i]["unformatted"]) &&
  223. (!isset($boxes[$i]["used"]) || $boxes[$i]["used"] == false)) {
  224. $boxesnew[] = $boxes[$i];
  225. $boxes[$i]["used"] = true;
  226. }
  227. }
  228. }
  229. // Rest of the folders
  230. for ($i = 0; $i < count($boxes); $i++) {
  231. if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
  232. ($boxes[$i]["used"] == false)) {
  233. $boxesnew[] = $boxes[$i];
  234. $boxes[$i]["used"] = true;
  235. }
  236. }
  237. return $boxesnew;
  238. }
  239. /******************************************************************************
  240. ** Returns a list of all folders, subscribed or not
  241. ******************************************************************************/
  242. function sqimap_mailbox_list_all ($imap_stream) {
  243. global $list_special_folders_first, $folder_prefix;
  244. if (!function_exists ("ary_sort"))
  245. include ("../functions/array.php");
  246. $dm = sqimap_get_delimiter ($imap_stream);
  247. fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
  248. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  249. $g = 0;
  250. $phase = "inbox";
  251. for ($i = 0; $i < count($read_ary); $i++) {
  252. if (substr ($read_ary[$i], 0, 4) != "a001") {
  253. // Store the raw IMAP reply
  254. $boxes[$g]["raw"] = $read_ary[$i];
  255. // Count number of delimiters ($dm) in folder name
  256. $mailbox = find_mailbox_name($read_ary[$i]);
  257. $dm_count = countCharInString($mailbox, $dm);
  258. if (substr($mailbox, -1) == $dm)
  259. $dm_count--; // If name ends in delimiter - decrement count by one
  260. // Format folder name, but only if it's a INBOX.* or have
  261. // a parent.
  262. $boxesbyname[$mailbox] = $g;
  263. $parentfolder = readMailboxParent($mailbox, $dm);
  264. if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
  265. (ereg("^".$folder_prefix, $mailbox)) ||
  266. ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  267. if ($dm_count)
  268. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
  269. else
  270. $boxes[$g]["formatted"] = '';
  271. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  272. } else {
  273. $boxes[$g]["formatted"] = $mailbox;
  274. }
  275. $boxes[$g]["unformatted-dm"] = $mailbox;
  276. if (substr($mailbox, -1) == $dm)
  277. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  278. $boxes[$g]["unformatted"] = $mailbox;
  279. $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
  280. $boxes[$g]["id"] = $g;
  281. /** Now lets get the flags for this mailbox **/
  282. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  283. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  284. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  285. $flags = substr($flags, 0, strpos($flags, ")"));
  286. $flags = str_replace("\\", "", $flags);
  287. $flags = trim(strtolower($flags));
  288. if ($flags) {
  289. $boxes[$g]["flags"] = explode(" ", $flags);
  290. }
  291. }
  292. $g++;
  293. }
  294. if(is_array($boxes)) {
  295. $boxes = ary_sort ($boxes, "unformatted", 1);
  296. }
  297. return $boxes;
  298. }
  299. ?>