imap_mailbox.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. <?php
  2. /**
  3. * imap_mailbox.php
  4. *
  5. * Copyright (c) 1999-2001 The SquirrelMail Development Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This impliments all functions that manipulate mailboxes
  9. *
  10. * $Id$
  11. */
  12. /*************************
  13. ** Expunges a mailbox **
  14. *************************/
  15. function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true)
  16. {
  17. fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
  18. $read = sqimap_read_data($imap_stream, sqimap_session_id(),
  19. $handle_errors, $response, $message);
  20. }
  21. /******************************************************************************
  22. ** Checks whether or not the specified mailbox exists
  23. ******************************************************************************/
  24. function sqimap_mailbox_exists ($imap_stream, $mailbox)
  25. {
  26. if (! isset($mailbox)) {
  27. return false;
  28. }
  29. fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
  30. $mbx = sqimap_read_data($imap_stream, sqimap_session_id(),
  31. true, $response, $message);
  32. return isset($mbx[0]);
  33. }
  34. /******************************************************************************
  35. ** Selects a mailbox
  36. ******************************************************************************/
  37. function sqimap_mailbox_select ($imap_stream, $mailbox,
  38. $hide=true, $recent=false)
  39. {
  40. global $auto_expunge;
  41. if ( $mailbox == 'None' ) {
  42. return;
  43. }
  44. fputs ($imap_stream, sqimap_session_id() . " SELECT \"$mailbox\"\r\n");
  45. $read = sqimap_read_data($imap_stream, sqimap_session_id(),
  46. true, $response, $message);
  47. if ($recent) {
  48. for ($i=0; $i<count($read); $i++) {
  49. if (strpos(strtolower($read[$i]), 'recent')) {
  50. $r = explode(' ', $read[$i]);
  51. }
  52. }
  53. return $r[1];
  54. }
  55. if ($auto_expunge) {
  56. fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
  57. $tmp = sqimap_read_data($imap_stream, sqimap_session_id(),
  58. false, $a, $b);
  59. }
  60. }
  61. /******************************************************************************
  62. ** Creates a folder
  63. ******************************************************************************/
  64. function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
  65. {
  66. global $delimiter;
  67. if (strtolower($type) == 'noselect') {
  68. $mailbox = $mailbox.$delimiter;
  69. }
  70. fputs ($imap_stream, sqimap_session_id() . " CREATE \"$mailbox\"\r\n");
  71. $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
  72. true, $response, $message);
  73. sqimap_subscribe ($imap_stream, $mailbox);
  74. }
  75. /******************************************************************************
  76. ** Subscribes to an existing folder
  77. ******************************************************************************/
  78. function sqimap_subscribe ($imap_stream, $mailbox)
  79. {
  80. fputs ($imap_stream, sqimap_session_id() . " SUBSCRIBE \"$mailbox\"\r\n");
  81. $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
  82. true, $response, $message);
  83. }
  84. /******************************************************************************
  85. ** Unsubscribes to an existing folder
  86. ******************************************************************************/
  87. function sqimap_unsubscribe ($imap_stream, $mailbox)
  88. {
  89. global $imap_server_type;
  90. fputs ($imap_stream, sqimap_session_id() . " UNSUBSCRIBE \"$mailbox\"\r\n");
  91. $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
  92. true, $response, $message);
  93. }
  94. /******************************************************************************
  95. ** This function simply deletes the given folder
  96. ******************************************************************************/
  97. function sqimap_mailbox_delete ($imap_stream, $mailbox)
  98. {
  99. fputs ($imap_stream, sqimap_session_id() . " DELETE \"$mailbox\"\r\n");
  100. $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
  101. true, $response, $message);
  102. sqimap_unsubscribe ($imap_stream, $mailbox);
  103. }
  104. /***********************************************************************
  105. ** Determines if the user is subscribed to the folder or not
  106. **********************************************************************/
  107. function sqimap_mailbox_is_subscribed($imap_stream, $folder)
  108. {
  109. $boxes = sqimap_mailbox_list ($imap_stream);
  110. foreach ($boxes as $ref) {
  111. if ($ref['unformatted'] == $folder) {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. /******************************************************************************
  118. ** Formats a mailbox into 4 parts for the $boxes array
  119. **
  120. ** The four parts are:
  121. **
  122. ** raw - Raw LIST/LSUB response from the IMAP server
  123. ** formatted - nicely formatted folder name
  124. ** unformatted - unformatted, but with delimiter at end removed
  125. ** unformatted-dm - folder name as it appears in raw response
  126. ** unformatted-disp - unformatted without $folder_prefix
  127. **
  128. ******************************************************************************/
  129. function sqimap_mailbox_parse ($line, $line_lsub)
  130. {
  131. global $folder_prefix, $delimiter;
  132. /* Process each folder line */
  133. for ($g=0; $g < count($line); $g++) {
  134. /* Store the raw IMAP reply */
  135. if (isset($line[$g])) {
  136. $boxes[$g]["raw"] = $line[$g];
  137. }
  138. else {
  139. $boxes[$g]["raw"] = "";
  140. }
  141. /* Count number of delimiters ($delimiter) in folder name */
  142. $mailbox = trim($line_lsub[$g]);
  143. $dm_count = countCharInString($mailbox, $delimiter);
  144. if (substr($mailbox, -1) == $delimiter) {
  145. /* If name ends in delimiter - decrement count by one */
  146. $dm_count--;
  147. }
  148. /* Format folder name, but only if it's a INBOX.* or have */
  149. /* a parent. */
  150. $boxesbyname[$mailbox] = $g;
  151. $parentfolder = readMailboxParent($mailbox, $delimiter);
  152. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  153. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  154. ( isset($boxesbyname[$parentfolder]) &&
  155. (strlen($parentfolder) > 0) ) ) {
  156. $indent = $dm_count - (countCharInString($folder_prefix, $delimiter));
  157. if ($indent > 0) {
  158. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
  159. }
  160. else {
  161. $boxes[$g]["formatted"] = '';
  162. }
  163. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
  164. }
  165. else {
  166. $boxes[$g]["formatted"] = $mailbox;
  167. }
  168. $boxes[$g]['unformatted-dm'] = $mailbox;
  169. if (substr($mailbox, -1) == $delimiter) {
  170. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  171. }
  172. $boxes[$g]['unformatted'] = $mailbox;
  173. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  174. $mailbox = substr($mailbox, strlen($folder_prefix));
  175. }
  176. $boxes[$g]['unformatted-disp'] = $mailbox;
  177. $boxes[$g]['id'] = $g;
  178. $boxes[$g]['flags'] = array();
  179. if (isset($line[$g])) {
  180. ereg("\(([^)]*)\)",$line[$g],$regs);
  181. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  182. if ($flags) {
  183. $boxes[$g]['flags'] = explode(' ', $flags);
  184. }
  185. }
  186. }
  187. return $boxes;
  188. }
  189. /**
  190. * Sorting function used to sort mailbox names.
  191. * + Original patch from dave_michmerhuizen@yahoo.com
  192. * + Allows case insensitivity when sorting folders
  193. * + Takes care of the delimiter being sorted to the end, causing
  194. * subfolders to be listed in below folders that are prefixed
  195. * with their parent folders name.
  196. * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
  197. * Without special sort function: foobar between foo and foo.bar
  198. * With special sort function: foobar AFTER foo and foo.bar :)
  199. */
  200. function user_strcasecmp($a, $b) {
  201. global $delimiter;
  202. /* Calculate the length of some strings. */
  203. $a_length = strlen($a);
  204. $b_length = strlen($b);
  205. $min_length = min($a_length, $b_length);
  206. $delimiter_length = strlen($delimiter);
  207. /* Set the initial result value. */
  208. $result = 0;
  209. /* Check the strings... */
  210. for ($c = 0; $c < $min_length; ++$c) {
  211. $a_del = substr($a, $c, $delimiter_length);
  212. $b_del = substr($b, $c, $delimiter_length);
  213. if (($a_del == $delimiter) && ($b_del == $delimiter)) {
  214. $result = 0;
  215. } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
  216. $result = 1;
  217. } else if (($a_del != $delimiter) && ($b_del != $delimiter)) {
  218. $result = -1;
  219. } else {
  220. $result = strcasecmp($a{$c}, $b{$c});
  221. }
  222. if ($result != 0) {
  223. break;
  224. }
  225. }
  226. /* If one string is a prefix of the other... */
  227. if ($result == 0) {
  228. if ($a_length < $b_length) {
  229. $result = -1;
  230. } else if ($a_length > $b_length) {
  231. $result = 1;
  232. }
  233. }
  234. return ($result);
  235. }
  236. /******************************************************************************
  237. ** Returns sorted mailbox lists in several different ways.
  238. ** See comment on sqimap_mailbox_parse() for info about the returned array.
  239. ******************************************************************************/
  240. function sqimap_mailbox_list ($imap_stream) {
  241. global $data_dir, $username, $list_special_folders_first;
  242. global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
  243. global $move_to_trash, $move_to_sent, $save_as_draft;
  244. global $delimiter;
  245. $inbox_in_list = false;
  246. $inbox_subscribed = false;
  247. require_once('../src/load_prefs.php');
  248. require_once('../functions/array.php');
  249. /** LSUB array **/
  250. fputs ($imap_stream, sqimap_session_id() .
  251. " LSUB \"$folder_prefix\" \"*\"\r\n");
  252. $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
  253. true, $response, $message);
  254. /* Section about removing the last element was removed */
  255. /* We don't return "* OK" anymore from sqimap_read_data */
  256. $sorted_lsub_ary = array();
  257. for ($i=0;$i < count($lsub_ary); $i++) {
  258. /* Workaround for EIMS */
  259. /* Doesn't work if the mailbox name is multiple lines */
  260. if (isset($lsub_ary[$i + 1]) &&
  261. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  262. $lsub_ary[$i], $regs)) {
  263. $i ++;
  264. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
  265. '"' . $regs[2];
  266. }
  267. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  268. $sorted_lsub_ary[] = $temp_mailbox_name;
  269. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  270. $inbox_subscribed = true;
  271. }
  272. }
  273. $new_ary = array();
  274. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  275. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  276. $new_ary[] = $sorted_lsub_ary[$i];
  277. }
  278. }
  279. $sorted_lsub_ary = $new_ary;
  280. if (isset($sorted_lsub_ary)) {
  281. usort($sorted_lsub_ary, 'user_strcasecmp');
  282. }
  283. /** LIST array **/
  284. $sorted_list_ary = array();
  285. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  286. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  287. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  288. }
  289. else {
  290. $mbx = $sorted_lsub_ary[$i];
  291. }
  292. fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mbx\"\r\n");
  293. $read = sqimap_read_data ($imap_stream, sqimap_session_id(),
  294. true, $response, $message);
  295. /* Another workaround for EIMS */
  296. if (isset($read[1]) &&
  297. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  298. $read[0], $regs)) {
  299. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
  300. '"' . $regs[2];
  301. }
  302. if (isset($sorted_list_ary[$i])) {
  303. $sorted_list_ary[$i] = "";
  304. }
  305. if (isset($read[0])) {
  306. $sorted_list_ary[$i] = $read[0];
  307. }
  308. else {
  309. $sorted_list_ary[$i] = "";
  310. }
  311. if (isset($sorted_list_ary[$i]) &&
  312. strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX") {
  313. $inbox_in_list = true;
  314. }
  315. }
  316. /**
  317. * Just in case they're not subscribed to their inbox,
  318. * we'll get it for them anyway
  319. */
  320. if ($inbox_subscribed == false || $inbox_in_list == false) {
  321. fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"INBOX\"\r\n");
  322. $inbox_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
  323. true, $response, $message);
  324. /* Another workaround for EIMS */
  325. if (isset($inbox_ary[1]) &&
  326. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  327. $inbox_ary[0], $regs)) {
  328. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  329. '"' . $regs[2];
  330. }
  331. $sorted_list_ary[] = $inbox_ary[0];
  332. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  333. }
  334. $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  335. /** Now, lets sort for special folders **/
  336. $boxesnew = Array();
  337. /* Find INBOX */
  338. for ($i = 0; $i < count($boxes); $i++) {
  339. if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
  340. $boxesnew[] = $boxes[$i];
  341. $used[$i] = true;
  342. $i = count($boxes);
  343. }
  344. }
  345. /* List special folders and their subfolders, if requested. */
  346. if ($list_special_folders_first == true) {
  347. /* First list the trash folder. */
  348. for ($i = 0 ; $i < count($boxes) ; $i++) {
  349. if ($move_to_trash &&
  350. eregi('^' . quotemeta($trash_folder) . '(' .
  351. quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
  352. $boxesnew[] = $boxes[$i];
  353. $used[$i] = true;
  354. }
  355. }
  356. /* Then list the sent folder. */
  357. for ($i = 0 ; $i < count($boxes) ; $i++) {
  358. if ($move_to_sent &&
  359. eregi('^' . quotemeta($sent_folder) . '(' .
  360. quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
  361. $boxesnew[] = $boxes[$i];
  362. $used[$i] = true;
  363. }
  364. }
  365. /* Lastly, list the list the draft folder. */
  366. for ($i = 0 ; $i < count($boxes) ; $i++) {
  367. if ($save_as_draft &&
  368. eregi('^' . quotemeta($draft_folder) . '(' .
  369. quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
  370. $boxesnew[] = $boxes[$i];
  371. $used[$i] = true;
  372. }
  373. }
  374. /* Put INBOX.* folders ahead of the rest. */
  375. for ($i = 0; $i < count($boxes); $i++) {
  376. if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
  377. (!isset($used[$i]) || $used[$i] == false)) {
  378. $boxesnew[] = $boxes[$i];
  379. $used[$i] = true;
  380. }
  381. }
  382. }
  383. /* Rest of the folders */
  384. for ($i = 0; $i < count($boxes); $i++) {
  385. if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
  386. (!isset($used[$i]) || $used[$i] == false)) {
  387. $boxesnew[] = $boxes[$i];
  388. $used[$i] = true;
  389. }
  390. }
  391. return $boxesnew;
  392. }
  393. /******************************************************************************
  394. ** Returns a list of all folders, subscribed or not
  395. ******************************************************************************/
  396. function sqimap_mailbox_list_all ($imap_stream)
  397. {
  398. global $list_special_folders_first, $folder_prefix;
  399. global $delimiter;
  400. if (!function_exists ("ary_sort")) {
  401. include_once('../functions/array.php');
  402. }
  403. $ssid = sqimap_session_id();
  404. $lsid = strlen( $ssid );
  405. fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
  406. $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
  407. $g = 0;
  408. $phase = "inbox";
  409. for ($i = 0; $i < count($read_ary); $i++) {
  410. /* Another workaround for EIMS */
  411. if (isset($read_ary[$i + 1]) &&
  412. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  413. $read_ary[$i], $regs)) {
  414. $i ++;
  415. $read_ary[$i] = $regs[1] . '"' .
  416. addslashes(trim($read_ary[$i])) .
  417. '"' . $regs[2];
  418. }
  419. if (substr ($read_ary[$i], 0, $lsid) != $ssid ) {
  420. /* Store the raw IMAP reply */
  421. $boxes[$g]["raw"] = $read_ary[$i];
  422. /* Count number of delimiters ($delimiter) in folder name */
  423. $mailbox = find_mailbox_name($read_ary[$i]);
  424. $dm_count = countCharInString($mailbox, $delimiter);
  425. if (substr($mailbox, -1) == $delimiter) {
  426. /* If name ends in delimiter - decrement count by one */
  427. $dm_count--;
  428. }
  429. /* Format folder name, but only if it's a INBOX.* or have */
  430. /* a parent. */
  431. $boxesbyname[$mailbox] = $g;
  432. $parentfolder = readMailboxParent($mailbox, $delimiter);
  433. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  434. (ereg('^'.$folder_prefix, $mailbox)) ||
  435. ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  436. if ($dm_count) {
  437. $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
  438. }
  439. else {
  440. $boxes[$g]["formatted"] = '';
  441. }
  442. $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
  443. }
  444. else {
  445. $boxes[$g]["formatted"] = $mailbox;
  446. }
  447. $boxes[$g]["unformatted-dm"] = $mailbox;
  448. if (substr($mailbox, -1) == $delimiter) {
  449. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  450. }
  451. $boxes[$g]["unformatted"] = $mailbox;
  452. $boxes[$g]["unformatted-disp"] =
  453. ereg_replace('^' . $folder_prefix, '', $mailbox);
  454. $boxes[$g]["id"] = $g;
  455. /** Now lets get the flags for this mailbox **/
  456. fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
  457. $read_mlbx = sqimap_read_data ($imap_stream, sqimap_session_id(),
  458. true, $response, $message);
  459. /* Another workaround for EIMS */
  460. if (isset($read_mlbx[1]) &&
  461. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  462. $read_mlbx[0], $regs)) {
  463. $read_mlbx[0] = $regs[1] . '"' .
  464. addslashes(trim($read_mlbx[1])) .
  465. '"' . $regs[2];
  466. }
  467. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
  468. $flags = substr($flags, 0, strpos($flags, ")"));
  469. $flags = str_replace('\\', '', $flags);
  470. $flags = trim(strtolower($flags));
  471. if ($flags) {
  472. $boxes[$g]['flags'] = explode(" ", $flags);
  473. }
  474. else {
  475. $boxes[$g]['flags'] = array();
  476. }
  477. }
  478. $g++;
  479. }
  480. if(is_array($boxes)) {
  481. $boxes = ary_sort ($boxes, "unformatted", 1);
  482. }
  483. return $boxes;
  484. }
  485. ?>