imap_mailbox.php 19 KB

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