imap_mailbox.php 21 KB

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