imap_mailbox.php 20 KB

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