imap_mailbox.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. if (! isset($mailbox))
  22. return false;
  23. fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
  24. $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  25. return isset($mbx[0]);
  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. if (isset($line[$g]))
  100. $boxes[$g]["raw"] = $line[$g];
  101. else
  102. $boxes[$g]["raw"] = "";
  103. // Count number of delimiters ($dm) in folder name
  104. $mailbox = trim($line_lsub[$g]);
  105. $dm_count = countCharInString($mailbox, $dm);
  106. if (substr($mailbox, -1) == $dm)
  107. $dm_count--; // If name ends in delimiter - decrement count by one
  108. // Format folder name, but only if it's a INBOX.* or have
  109. // a parent.
  110. $boxesbyname[$mailbox] = $g;
  111. $parentfolder = readMailboxParent($mailbox, $dm);
  112. if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
  113. (ereg("^".$folder_prefix, $mailbox)) ||
  114. ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  115. $indent = $dm_count - (countCharInString($folder_prefix, $dm));
  116. if ($indent)
  117. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
  118. else
  119. $boxes[$g]["formatted"] = '';
  120. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  121. } else {
  122. $boxes[$g]["formatted"] = $mailbox;
  123. }
  124. $boxes[$g]['unformatted-dm'] = $mailbox;
  125. if (substr($mailbox, -1) == $dm)
  126. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  127. $boxes[$g]['unformatted'] = $mailbox;
  128. $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
  129. $boxes[$g]['id'] = $g;
  130. if (isset($line[$g]))
  131. ereg("\(([^)]*)\)",$line[$g],$regs);
  132. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  133. if ($flags) {
  134. $boxes[$g]['flags'] = explode(' ', $flags);
  135. }
  136. else
  137. $boxes[$g]['flags'] = array();
  138. }
  139. return $boxes;
  140. }
  141. /* Apparently you must call a user function with usort instead
  142. * of calling a built-in directly. Stupid.
  143. * Patch from dave_michmerhuizen@yahoo.com
  144. * Allows case insensitivity when sorting folders
  145. */
  146. function user_strcasecmp($a, $b)
  147. {
  148. return strcasecmp($a, $b);
  149. }
  150. /******************************************************************************
  151. ** Returns sorted mailbox lists in several different ways.
  152. ** See comment on sqimap_mailbox_parse() for info about the returned array.
  153. ******************************************************************************/
  154. function sqimap_mailbox_list ($imap_stream) {
  155. global $load_prefs_php, $prefs_php, $config_php;
  156. global $data_dir, $username, $list_special_folders_first;
  157. global $trash_folder, $sent_folder;
  158. global $move_to_trash, $move_to_sent;
  159. $inbox_in_list = false;
  160. $inbox_subscribed = false;
  161. if (!isset($load_prefs_php)) include "../src/load_prefs.php";
  162. else global $folder_prefix;
  163. if (!function_exists ("ary_sort")) include "../functions/array.php";
  164. $dm = sqimap_get_delimiter ($imap_stream);
  165. /** LSUB array **/
  166. $inbox_subscribed = false;
  167. fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
  168. $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  169. /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
  170. /** LUKE: This introduced errors.. do a check first **/
  171. if (substr($lsub_ary[count($lsub_ary)-1], 0, 4) == "* OK") {
  172. unset($lsub_ary[count($lsub_ary)-1]);
  173. }
  174. for ($i=0;$i < count($lsub_ary); $i++) {
  175. $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
  176. if ($sorted_lsub_ary[$i] == "INBOX")
  177. $inbox_subscribed = true;
  178. }
  179. $new_ary = array();
  180. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  181. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  182. $new_ary[] = $sorted_lsub_ary[$i];
  183. }
  184. }
  185. $sorted_lsub_ary = $new_ary;
  186. if (isset($sorted_lsub_ary)) {
  187. usort($sorted_lsub_ary, "user_strcasecmp");
  188. //sort($sorted_lsub_ary);
  189. }
  190. /** LIST array **/
  191. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  192. if (substr($sorted_lsub_ary[$i], -1) == $dm)
  193. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  194. else
  195. $mbx = $sorted_lsub_ary[$i];
  196. fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
  197. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  198. if (isset($sorted_list_ary[$i]))
  199. $sorted_list_ary[$i] = "";
  200. if (isset($read[0]))
  201. $sorted_list_ary[$i] = $read[0];
  202. else
  203. $sorget_list_ary[$i] = "";
  204. if (isset($sorted_list_ary[$i]) && find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
  205. $inbox_in_list = true;
  206. }
  207. /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
  208. if ($inbox_subscribed == false || $inbox_in_list == false) {
  209. fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
  210. $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  211. $pos = count($sorted_list_ary);
  212. $sorted_list_ary[$pos] = $inbox_ary[0];
  213. $pos = count($sorted_lsub_ary);
  214. $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
  215. }
  216. $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
  217. /** Now, lets sort for special folders **/
  218. $boxesnew = Array();
  219. // Find INBOX
  220. for ($i = 0; $i < count($boxes); $i++) {
  221. if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
  222. $boxesnew[] = $boxes[$i];
  223. $used[$i] = true;
  224. $i = count($boxes);
  225. }
  226. }
  227. if ($list_special_folders_first == true) {
  228. // Then list special folders and their subfolders
  229. for ($i = 0 ; $i < count($boxes) ; $i++) {
  230. if ($move_to_trash &&
  231. eregi('^' . quotemeta($trash_folder) . '(' .
  232. quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
  233. $boxesnew[] = $boxes[$i];
  234. $used[$i] = true;
  235. }
  236. elseif ($move_to_sent &&
  237. eregi('^' . quotemeta($sent_folder) . '(' .
  238. quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
  239. $boxesnew[] = $boxes[$i];
  240. $used[$i] = true;
  241. }
  242. }
  243. // Put INBOX.* folders ahead of the rest
  244. for ($i = 0; $i < count($boxes); $i++) {
  245. if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
  246. (!isset($used[$i]) || $used[$i] == false)) {
  247. $boxesnew[] = $boxes[$i];
  248. $used[$i] = true;
  249. }
  250. }
  251. }
  252. // Rest of the folders
  253. for ($i = 0; $i < count($boxes); $i++) {
  254. if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
  255. (!isset($used[$i]) || $used[$i] == false)) {
  256. $boxesnew[] = $boxes[$i];
  257. $used[$i] = true;
  258. }
  259. }
  260. return $boxesnew;
  261. }
  262. /******************************************************************************
  263. ** Returns a list of all folders, subscribed or not
  264. ******************************************************************************/
  265. function sqimap_mailbox_list_all ($imap_stream) {
  266. global $list_special_folders_first, $folder_prefix;
  267. if (!function_exists ("ary_sort"))
  268. include ("../functions/array.php");
  269. $dm = sqimap_get_delimiter ($imap_stream);
  270. fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
  271. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  272. $g = 0;
  273. $phase = "inbox";
  274. for ($i = 0; $i < count($read_ary); $i++) {
  275. if (substr ($read_ary[$i], 0, 4) != "a001") {
  276. // Store the raw IMAP reply
  277. $boxes[$g]["raw"] = $read_ary[$i];
  278. // Count number of delimiters ($dm) in folder name
  279. $mailbox = find_mailbox_name($read_ary[$i]);
  280. $dm_count = countCharInString($mailbox, $dm);
  281. if (substr($mailbox, -1) == $dm)
  282. $dm_count--; // If name ends in delimiter - decrement count by one
  283. // Format folder name, but only if it's a INBOX.* or have
  284. // a parent.
  285. $boxesbyname[$mailbox] = $g;
  286. $parentfolder = readMailboxParent($mailbox, $dm);
  287. if((eregi('^inbox'.quotemeta($dm), $mailbox)) ||
  288. (ereg('^'.$folder_prefix, $mailbox)) ||
  289. ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  290. if ($dm_count)
  291. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
  292. else
  293. $boxes[$g]["formatted"] = '';
  294. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  295. } else {
  296. $boxes[$g]["formatted"] = $mailbox;
  297. }
  298. $boxes[$g]["unformatted-dm"] = $mailbox;
  299. if (substr($mailbox, -1) == $dm)
  300. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  301. $boxes[$g]["unformatted"] = $mailbox;
  302. $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
  303. $boxes[$g]["id"] = $g;
  304. /** Now lets get the flags for this mailbox **/
  305. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  306. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  307. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  308. $flags = substr($flags, 0, strpos($flags, ")"));
  309. $flags = str_replace('\\', '', $flags);
  310. $flags = trim(strtolower($flags));
  311. if ($flags) {
  312. $boxes[$g]['flags'] = explode(" ", $flags);
  313. }
  314. else
  315. {
  316. $boxes[$g]['flags'] = array();
  317. }
  318. }
  319. $g++;
  320. }
  321. if(is_array($boxes)) {
  322. $boxes = ary_sort ($boxes, "unformatted", 1);
  323. }
  324. return $boxes;
  325. }
  326. ?>