imap_mailbox.php 15 KB

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