imap_mailbox.php 21 KB

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