imap_mailbox.php 20 KB

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