imap_mailbox.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /**
  3. ** imap_mailbox.php
  4. **
  5. ** This impliments all functions that manipulate mailboxes
  6. **
  7. ** $Id$
  8. **/
  9. if (defined ('imap_mailbox_php'))
  10. return;
  11. define ('imap_mailbox_php', true);
  12. /******************************************************************************
  13. ** Expunges a mailbox
  14. ******************************************************************************/
  15. function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
  16. fputs ($imap_stream, "a001 EXPUNGE\r\n");
  17. $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
  18. }
  19. /******************************************************************************
  20. ** Checks whether or not the specified mailbox exists
  21. ******************************************************************************/
  22. function sqimap_mailbox_exists ($imap_stream, $mailbox) {
  23. if (! isset($mailbox))
  24. return false;
  25. fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
  26. $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  27. return isset($mbx[0]);
  28. }
  29. /******************************************************************************
  30. ** Selects a mailbox
  31. ******************************************************************************/
  32. function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
  33. global $auto_expunge;
  34. fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
  35. $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  36. if ($recent) {
  37. for ($i=0; $i<count($read); $i++) {
  38. if (strpos(strtolower($read[$i]), "recent")) {
  39. $r = explode(" ", $read[$i]);
  40. }
  41. }
  42. return $r[1];
  43. }
  44. if ($auto_expunge) {
  45. fputs ($imap_stream, "a001 EXPUNGE\r\n");
  46. $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
  47. }
  48. }
  49. /******************************************************************************
  50. ** Creates a folder
  51. ******************************************************************************/
  52. function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
  53. if (strtolower($type) == "noselect") {
  54. $dm = sqimap_get_delimiter($imap_stream);
  55. $mailbox = $mailbox.$dm;
  56. }
  57. fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
  58. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  59. sqimap_subscribe ($imap_stream, $mailbox);
  60. }
  61. /******************************************************************************
  62. ** Subscribes to an existing folder
  63. ******************************************************************************/
  64. function sqimap_subscribe ($imap_stream, $mailbox) {
  65. fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
  66. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  67. }
  68. /******************************************************************************
  69. ** Unsubscribes to an existing folder
  70. ******************************************************************************/
  71. function sqimap_unsubscribe ($imap_stream, $mailbox) {
  72. global $imap_server_type;
  73. fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
  74. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  75. }
  76. /******************************************************************************
  77. ** This function simply deletes the given folder
  78. ******************************************************************************/
  79. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  80. fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
  81. $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
  82. sqimap_unsubscribe ($imap_stream, $mailbox);
  83. }
  84. /******************************************************************************
  85. ** Formats a mailbox into 4 parts for the $boxes array
  86. **
  87. ** The four parts are:
  88. **
  89. ** raw - Raw LIST/LSUB response from the IMAP server
  90. ** formatted - nicely formatted folder name
  91. ** unformatted - unformatted, but with delimiter at end removed
  92. ** unformatted-dm - folder name as it appears in raw response
  93. ** unformatted-disp - unformatted without $folder_prefix
  94. **
  95. ******************************************************************************/
  96. function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
  97. global $folder_prefix;
  98. // Process each folder line
  99. for ($g=0; $g < count($line); $g++) {
  100. // Store the raw IMAP reply
  101. if (isset($line[$g]))
  102. $boxes[$g]["raw"] = $line[$g];
  103. else
  104. $boxes[$g]["raw"] = "";
  105. // Count number of delimiters ($dm) in folder name
  106. $mailbox = trim($line_lsub[$g]);
  107. $dm_count = countCharInString($mailbox, $dm);
  108. if (substr($mailbox, -1) == $dm)
  109. $dm_count--; // If name ends in delimiter - decrement count by one
  110. // Format folder name, but only if it's a INBOX.* or have
  111. // a parent.
  112. $boxesbyname[$mailbox] = $g;
  113. $parentfolder = readMailboxParent($mailbox, $dm);
  114. if((strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  115. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  116. (isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  117. $indent = $dm_count - (countCharInString($folder_prefix, $dm));
  118. if ($indent > 0)
  119. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
  120. else
  121. $boxes[$g]["formatted"] = '';
  122. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  123. } else {
  124. $boxes[$g]["formatted"] = $mailbox;
  125. }
  126. $boxes[$g]['unformatted-dm'] = $mailbox;
  127. if (substr($mailbox, -1) == $dm)
  128. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  129. $boxes[$g]['unformatted'] = $mailbox;
  130. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix)
  131. $mailbox = substr($mailbox, strlen($folder_prefix));
  132. $boxes[$g]['unformatted-disp'] = $mailbox;
  133. $boxes[$g]['id'] = $g;
  134. $boxes[$g]['flags'] = array();
  135. if (isset($line[$g])) {
  136. ereg("\(([^)]*)\)",$line[$g],$regs);
  137. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  138. if ($flags)
  139. $boxes[$g]['flags'] = explode(' ', $flags);
  140. }
  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 $data_dir, $username, $list_special_folders_first;
  159. global $trash_folder, $sent_folder;
  160. global $move_to_trash, $move_to_sent, $folder_prefix;
  161. $inbox_in_list = false;
  162. $inbox_subscribed = false;
  163. include "../src/load_prefs.php";
  164. include "../functions/array.php";
  165. $dm = sqimap_get_delimiter ($imap_stream);
  166. /** LSUB array **/
  167. fputs ($imap_stream, "a001 LSUB \"$folder_prefix\" \"*\"\r\n");
  168. $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  169. // Section about removing the last element was removed
  170. // We don't return "* OK" anymore from sqimap_read_data
  171. $sorted_lsub_ary = array();
  172. for ($i=0;$i < count($lsub_ary); $i++) {
  173. // Workaround for EIMS
  174. // Doesn't work if the mailbox name is multiple lines
  175. if (isset($lsub_ary[$i + 1]) &&
  176. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  177. $lsub_ary[$i], $regs)) {
  178. $i ++;
  179. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
  180. '"' . $regs[2];
  181. }
  182. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  183. $sorted_lsub_ary[] = $temp_mailbox_name;
  184. if (strtoupper($temp_mailbox_name) == 'INBOX')
  185. $inbox_subscribed = true;
  186. }
  187. $new_ary = array();
  188. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  189. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  190. $new_ary[] = $sorted_lsub_ary[$i];
  191. }
  192. }
  193. $sorted_lsub_ary = $new_ary;
  194. if (isset($sorted_lsub_ary)) {
  195. usort($sorted_lsub_ary, 'user_strcasecmp');
  196. //sort($sorted_lsub_ary);
  197. }
  198. /** LIST array **/
  199. $sorted_list_ary = array();
  200. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  201. if (substr($sorted_lsub_ary[$i], -1) == $dm)
  202. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  203. else
  204. $mbx = $sorted_lsub_ary[$i];
  205. fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
  206. $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  207. // Another workaround for EIMS
  208. if (isset($read[1]) &&
  209. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  210. $read[0], $regs)) {
  211. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
  212. '"' . $regs[2];
  213. }
  214. if (isset($sorted_list_ary[$i]))
  215. $sorted_list_ary[$i] = "";
  216. if (isset($read[0]))
  217. $sorted_list_ary[$i] = $read[0];
  218. else
  219. $sorted_list_ary[$i] = "";
  220. if (isset($sorted_list_ary[$i]) &&
  221. strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX")
  222. $inbox_in_list = true;
  223. }
  224. /** Just in case they're not subscribed to their inbox, we'll get it
  225. for them anyway **/
  226. if ($inbox_subscribed == false || $inbox_in_list == false) {
  227. fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
  228. $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  229. // Another workaround for EIMS
  230. if (isset($inbox_ary[1]) &&
  231. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  232. $inbox_ary[0], $regs)) {
  233. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  234. '"' . $regs[2];
  235. }
  236. $sorted_list_ary[] = $inbox_ary[0];
  237. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  238. }
  239. $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
  240. /** Now, lets sort for special folders **/
  241. $boxesnew = Array();
  242. // Find INBOX
  243. for ($i = 0; $i < count($boxes); $i++) {
  244. if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
  245. $boxesnew[] = $boxes[$i];
  246. $used[$i] = true;
  247. $i = count($boxes);
  248. }
  249. }
  250. if ($list_special_folders_first == true) {
  251. // Then list special folders and their subfolders
  252. for ($i = 0 ; $i < count($boxes) ; $i++) {
  253. if ($move_to_trash &&
  254. eregi('^' . quotemeta($trash_folder) . '(' .
  255. quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
  256. $boxesnew[] = $boxes[$i];
  257. $used[$i] = true;
  258. }
  259. elseif ($move_to_sent &&
  260. eregi('^' . quotemeta($sent_folder) . '(' .
  261. quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
  262. $boxesnew[] = $boxes[$i];
  263. $used[$i] = true;
  264. }
  265. }
  266. // Put INBOX.* folders ahead of the rest
  267. for ($i = 0; $i < count($boxes); $i++) {
  268. if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
  269. (!isset($used[$i]) || $used[$i] == false)) {
  270. $boxesnew[] = $boxes[$i];
  271. $used[$i] = true;
  272. }
  273. }
  274. }
  275. // Rest of the folders
  276. for ($i = 0; $i < count($boxes); $i++) {
  277. if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
  278. (!isset($used[$i]) || $used[$i] == false)) {
  279. $boxesnew[] = $boxes[$i];
  280. $used[$i] = true;
  281. }
  282. }
  283. return $boxesnew;
  284. }
  285. /******************************************************************************
  286. ** Returns a list of all folders, subscribed or not
  287. ******************************************************************************/
  288. function sqimap_mailbox_list_all ($imap_stream) {
  289. global $list_special_folders_first, $folder_prefix;
  290. if (!function_exists ("ary_sort"))
  291. include ("../functions/array.php");
  292. $dm = sqimap_get_delimiter ($imap_stream);
  293. fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
  294. $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
  295. $g = 0;
  296. $phase = "inbox";
  297. for ($i = 0; $i < count($read_ary); $i++) {
  298. // Another workaround for EIMS
  299. if (isset($read_ary[$i + 1]) &&
  300. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  301. $read_ary[$i], $regs)) {
  302. $i ++;
  303. $read_ary[$i] = $regs[1] . '"' .
  304. addslashes(trim($read_ary[$i])) .
  305. '"' . $regs[2];
  306. }
  307. if (substr ($read_ary[$i], 0, 4) != "a001") {
  308. // Store the raw IMAP reply
  309. $boxes[$g]["raw"] = $read_ary[$i];
  310. // Count number of delimiters ($dm) in folder name
  311. $mailbox = find_mailbox_name($read_ary[$i]);
  312. $dm_count = countCharInString($mailbox, $dm);
  313. if (substr($mailbox, -1) == $dm)
  314. $dm_count--; // If name ends in delimiter - decrement count by one
  315. // Format folder name, but only if it's a INBOX.* or have
  316. // a parent.
  317. $boxesbyname[$mailbox] = $g;
  318. $parentfolder = readMailboxParent($mailbox, $dm);
  319. if((eregi('^inbox'.quotemeta($dm), $mailbox)) ||
  320. (ereg('^'.$folder_prefix, $mailbox)) ||
  321. ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  322. if ($dm_count)
  323. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
  324. else
  325. $boxes[$g]["formatted"] = '';
  326. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
  327. } else {
  328. $boxes[$g]["formatted"] = $mailbox;
  329. }
  330. $boxes[$g]["unformatted-dm"] = $mailbox;
  331. if (substr($mailbox, -1) == $dm)
  332. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  333. $boxes[$g]["unformatted"] = $mailbox;
  334. $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
  335. $boxes[$g]["id"] = $g;
  336. /** Now lets get the flags for this mailbox **/
  337. fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
  338. $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
  339. // Another workaround for EIMS
  340. if (isset($read_mlbx[1]) &&
  341. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  342. $read_mlbx[0], $regs)) {
  343. $read_mlbx[0] = $regs[1] . '"' .
  344. addslashes(trim($read_mlbx[1])) .
  345. '"' . $regs[2];
  346. }
  347. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  348. $flags = substr($flags, 0, strpos($flags, ")"));
  349. $flags = str_replace('\\', '', $flags);
  350. $flags = trim(strtolower($flags));
  351. if ($flags) {
  352. $boxes[$g]['flags'] = explode(" ", $flags);
  353. }
  354. else
  355. {
  356. $boxes[$g]['flags'] = array();
  357. }
  358. }
  359. $g++;
  360. }
  361. if(is_array($boxes)) {
  362. $boxes = ary_sort ($boxes, "unformatted", 1);
  363. }
  364. return $boxes;
  365. }
  366. ?>