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