imap_mailbox.php 22 KB

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