imap_mailbox.php 19 KB

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