imap_mailbox.php 20 KB

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