imap_mailbox.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 ( $imap_server_type == 'uw' ) {
  20. $boxs = $box;
  21. $i = strpos( $sent_folder, $delimiter, strlen( $folder_prefix ) );
  22. if ( $i === FALSE ) {
  23. $i = strlen( $box );
  24. }
  25. } else {
  26. $boxs = $box . $delimiter;
  27. // Skip next second delimiter
  28. $i = strpos( $sent_folder, $delimiter );
  29. $i = strpos( $sent_folder, $delimiter, $i + 1 );
  30. if ( $i === FALSE ) {
  31. $i = strlen( $box );
  32. } else {
  33. $i++;
  34. }
  35. }
  36. $ret = ( (strtolower($box) == 'inbox') ||
  37. ( substr( $trash_folder, 0, $i ) == substr( $boxs, 0, $i ) &&
  38. $move_to_trash) ||
  39. ( substr( $sent_folder, 0, $i ) == substr( $boxs, 0, $i ) &&
  40. $move_to_sent) ||
  41. ($box == $draft_folder &&
  42. $save_as_draft) );
  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 = false;
  305. $inbox_subscribed = false;
  306. require_once('../src/load_prefs.php');
  307. require_once('../functions/array.php');
  308. /** LSUB array **/
  309. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
  310. TRUE, $response, $message);
  311. /* Section about removing the last element was removed */
  312. /* We don't return "* OK" anymore from sqimap_read_data */
  313. $sorted_lsub_ary = array();
  314. for ($i=0;$i < count($lsub_ary); $i++) {
  315. /* Workaround for EIMS */
  316. /* Doesn't work if the mailbox name is multiple lines */
  317. if (isset($lsub_ary[$i + 1]) &&
  318. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  319. $lsub_ary[$i], $regs)) {
  320. $i ++;
  321. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
  322. '"' . $regs[2];
  323. }
  324. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  325. $sorted_lsub_ary[] = $temp_mailbox_name;
  326. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  327. $inbox_subscribed = TRUE;
  328. }
  329. }
  330. $new_ary = array();
  331. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  332. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  333. $new_ary[] = $sorted_lsub_ary[$i];
  334. }
  335. }
  336. $sorted_lsub_ary = $new_ary;
  337. if (isset($sorted_lsub_ary)) {
  338. usort($sorted_lsub_ary, 'user_strcasecmp');
  339. }
  340. /** LIST array **/
  341. $sorted_list_ary = array();
  342. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  343. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  344. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  345. }
  346. else {
  347. $mbx = $sorted_lsub_ary[$i];
  348. }
  349. $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
  350. TRUE, $response, $message);
  351. /* Another workaround for EIMS */
  352. if (isset($read[1]) &&
  353. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  354. $read[0], $regs)) {
  355. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
  356. '"' . $regs[2];
  357. }
  358. if (isset($sorted_list_ary[$i])) {
  359. $sorted_list_ary[$i] = '';
  360. }
  361. if (isset($read[0])) {
  362. $sorted_list_ary[$i] = $read[0];
  363. }
  364. else {
  365. $sorted_list_ary[$i] = '';
  366. }
  367. if (isset($sorted_list_ary[$i]) &&
  368. strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
  369. $inbox_in_list = TRUE;
  370. }
  371. }
  372. /**
  373. * Just in case they're not subscribed to their inbox,
  374. * we'll get it for them anyway
  375. */
  376. if ($inbox_subscribed == false || $inbox_in_list == false) {
  377. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  378. TRUE, $response, $message);
  379. /* Another workaround for EIMS */
  380. if (isset($inbox_ary[1]) &&
  381. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  382. $inbox_ary[0], $regs)) {
  383. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  384. '"' . $regs[2];
  385. }
  386. $sorted_list_ary[] = $inbox_ary[0];
  387. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  388. }
  389. $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  390. /** Now, lets sort for special folders **/
  391. $boxesnew = $used = array();
  392. /* Find INBOX */
  393. foreach ( $boxes as $k => $box ) {
  394. if ( strtolower($box['unformatted']) == 'inbox') {
  395. $boxesnew[] = $box;
  396. $used[$k] = TRUE;
  397. } else {
  398. $used[$k] = FALSE;
  399. }
  400. }
  401. /* List special folders and their subfolders, if requested. */
  402. if ($list_special_folders_first == TRUE) {
  403. foreach ( $boxes as $k => $box ) {
  404. if ( !$used[$k] &&
  405. isSpecialMailbox( $box['unformatted'] ) ) {
  406. $boxesnew[] = $box;
  407. $used[$k] = TRUE;
  408. }
  409. }
  410. }
  411. /* Rest of the folders */
  412. foreach ( $boxes as $k => $box ) {
  413. if ( !$used[$k] ) {
  414. $boxesnew[] = $box;
  415. }
  416. }
  417. return( $boxesnew );
  418. }
  419. /*
  420. * Returns a list of all folders, subscribed or not
  421. */
  422. function sqimap_mailbox_list_all ($imap_stream)
  423. {
  424. global $list_special_folders_first, $folder_prefix;
  425. global $delimiter;
  426. if (!function_exists('ary_sort')) {
  427. include_once('../functions/array.php');
  428. }
  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. ?>