imap_mailbox.php 20 KB

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