imap_mailbox.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. global $data_dir, $username;
  154. $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
  155. true, $response, $message);
  156. sqimap_unsubscribe ($imap_stream, $mailbox);
  157. do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', ''));
  158. removePref($data_dir, $username, "thread_$mailbox");
  159. }
  160. /* Determines if the user is subscribed to the folder or not */
  161. function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
  162. $boxesall = sqimap_mailbox_list ($imap_stream);
  163. foreach ($boxesall as $ref) {
  164. if ($ref['unformatted'] == $folder) {
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. /* Renames a mailbox */
  171. function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
  172. if ( $old_name != $new_name ) {
  173. global $delimiter, $imap_server_type, $data_dir, $username;
  174. if ( substr( $old_name, -1 ) == $delimiter ) {
  175. $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
  176. $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
  177. $postfix = $delimiter;
  178. } else {
  179. $postfix = '';
  180. }
  181. $boxesall = sqimap_mailbox_list($imap_stream);
  182. $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
  183. $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
  184. sqimap_unsubscribe($imap_stream, $old_name.$postfix);
  185. $oldpref = getPref($data_dir, $username, "thread_".$old_name.$postfix);
  186. removePref($data_dir, $username, "thread_".$old_name.$postfix);
  187. sqimap_subscribe($imap_stream, $new_name.$postfix);
  188. setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref);
  189. do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name));
  190. $l = strlen( $old_name ) + 1;
  191. $p = 'unformatted';
  192. foreach ( $boxesall as $box ) {
  193. if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
  194. $new_sub = $new_name . $delimiter . substr($box[$p], $l);
  195. if ($imap_server_type == 'cyrus') {
  196. $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
  197. $data = sqimap_run_command($imap_stream, $cmd, true,
  198. $response, $message);
  199. }
  200. sqimap_unsubscribe($imap_stream, $box[$p]);
  201. $oldpref = getPref($data_dir, $username, "thread_".$box[$p]);
  202. removePref($data_dir, $username, "thread_".$box[$p]);
  203. sqimap_subscribe($imap_stream, $new_sub);
  204. setPref($data_dir, $username, "thread_".$new_sub, $oldpref);
  205. do_hook_function("rename_or_delete_folder",
  206. $args = array($box[$p], 'rename', $new_sub));
  207. }
  208. }
  209. }
  210. }
  211. /*
  212. * Formats a mailbox into 4 parts for the $boxesall array
  213. *
  214. * The four parts are:
  215. *
  216. * raw - Raw LIST/LSUB response from the IMAP server
  217. * formatted - nicely formatted folder name
  218. * unformatted - unformatted, but with delimiter at end removed
  219. * unformatted-dm - folder name as it appears in raw response
  220. * unformatted-disp - unformatted without $folder_prefix
  221. */
  222. function sqimap_mailbox_parse ($line, $line_lsub) {
  223. global $folder_prefix, $delimiter;
  224. /* Process each folder line */
  225. for ($g=0; $g < count($line); $g++) {
  226. /* Store the raw IMAP reply */
  227. if (isset($line[$g])) {
  228. $boxesall[$g]["raw"] = $line[$g];
  229. }
  230. else {
  231. $boxesall[$g]["raw"] = '';
  232. }
  233. /* Count number of delimiters ($delimiter) in folder name */
  234. $mailbox = trim($line_lsub[$g]);
  235. $dm_count = substr_count($mailbox, $delimiter);
  236. if (substr($mailbox, -1) == $delimiter) {
  237. /* If name ends in delimiter, decrement count by one */
  238. $dm_count--;
  239. }
  240. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  241. $boxesallbyname[$mailbox] = $g;
  242. $parentfolder = readMailboxParent($mailbox, $delimiter);
  243. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  244. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  245. ( isset($boxesallbyname[$parentfolder]) &&
  246. (strlen($parentfolder) > 0) ) ) {
  247. $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
  248. if ($indent > 0) {
  249. $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
  250. }
  251. else {
  252. $boxesall[$g]['formatted'] = '';
  253. }
  254. $boxesall[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
  255. }
  256. else {
  257. $boxesall[$g]['formatted'] = $mailbox;
  258. }
  259. $boxesall[$g]['unformatted-dm'] = $mailbox;
  260. if (substr($mailbox, -1) == $delimiter) {
  261. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  262. }
  263. $boxesall[$g]['unformatted'] = $mailbox;
  264. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  265. $mailbox = substr($mailbox, strlen($folder_prefix));
  266. }
  267. $boxesall[$g]['unformatted-disp'] = $mailbox;
  268. $boxesall[$g]['id'] = $g;
  269. $boxesall[$g]['flags'] = array();
  270. if (isset($line[$g])) {
  271. ereg("\(([^)]*)\)",$line[$g],$regs);
  272. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  273. if ($flags) {
  274. $boxesall[$g]['flags'] = explode(' ', $flags);
  275. }
  276. }
  277. }
  278. return $boxesall;
  279. }
  280. /*
  281. * Sorting function used to sort mailbox names.
  282. * + Original patch from dave_michmerhuizen@yahoo.com
  283. * + Allows case insensitivity when sorting folders
  284. * + Takes care of the delimiter being sorted to the end, causing
  285. * subfolders to be listed in below folders that are prefixed
  286. * with their parent folders name.
  287. *
  288. * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
  289. * Without special sort function: foobar between foo and foo.bar
  290. * With special sort function: foobar AFTER foo and foo.bar :)
  291. */
  292. function user_strcasecmp($a, $b) {
  293. global $delimiter;
  294. /* Calculate the length of some strings. */
  295. $a_length = strlen($a);
  296. $b_length = strlen($b);
  297. $min_length = min($a_length, $b_length);
  298. $delimiter_length = strlen($delimiter);
  299. /* Set the initial result value. */
  300. $result = 0;
  301. /* Check the strings... */
  302. for ($c = 0; $c < $min_length; ++$c) {
  303. $a_del = substr($a, $c, $delimiter_length);
  304. $b_del = substr($b, $c, $delimiter_length);
  305. if (($a_del == $delimiter) && ($b_del == $delimiter)) {
  306. $result = 0;
  307. } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
  308. $result = -1;
  309. } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
  310. $result = 1;
  311. } else {
  312. $result = strcasecmp($a{$c}, $b{$c});
  313. }
  314. if ($result != 0) {
  315. break;
  316. }
  317. }
  318. /* If one string is a prefix of the other... */
  319. if ($result == 0) {
  320. if ($a_length < $b_length) {
  321. $result = -1;
  322. } else if ($a_length > $b_length) {
  323. $result = 1;
  324. }
  325. }
  326. return $result;
  327. }
  328. /*
  329. * Returns sorted mailbox lists in several different ways.
  330. * See comment on sqimap_mailbox_parse() for info about the returned array.
  331. */
  332. function sqimap_mailbox_list($imap_stream) {
  333. global $boxesnew, $default_folder_prefix;
  334. if ( !isset( $boxesnew ) ) {
  335. global $data_dir, $username, $list_special_folders_first,
  336. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  337. $move_to_trash, $move_to_sent, $save_as_draft,
  338. $delimiter;
  339. $inbox_in_list = false;
  340. $inbox_subscribed = false;
  341. require_once('../src/load_prefs.php');
  342. require_once('../functions/array.php');
  343. /* LSUB array */
  344. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*%\"",
  345. true, $response, $message);
  346. /*
  347. * Section about removing the last element was removed
  348. * We don't return "* OK" anymore from sqimap_read_data
  349. */
  350. $sorted_lsub_ary = array();
  351. for ($i=0;$i < count($lsub_ary); $i++) {
  352. /*
  353. * Workaround for EIMS
  354. * Doesn't work if the mailbox name is multiple lines
  355. */
  356. if (isset($lsub_ary[$i + 1]) &&
  357. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  358. $lsub_ary[$i], $regs)) {
  359. $i ++;
  360. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
  361. }
  362. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  363. $sorted_lsub_ary[] = $temp_mailbox_name;
  364. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  365. $inbox_subscribed = true;
  366. }
  367. }
  368. $new_ary = array();
  369. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  370. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  371. $new_ary[] = $sorted_lsub_ary[$i];
  372. }
  373. }
  374. $sorted_lsub_ary = $new_ary;
  375. if (isset($sorted_lsub_ary)) {
  376. usort($sorted_lsub_ary, 'user_strcasecmp');
  377. }
  378. /* LIST array */
  379. $sorted_list_ary = array();
  380. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  381. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  382. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  383. }
  384. else {
  385. $mbx = $sorted_lsub_ary[$i];
  386. }
  387. $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
  388. true, $response, $message);
  389. /* Another workaround for EIMS */
  390. if (isset($read[1]) &&
  391. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  392. $read[0], $regs)) {
  393. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
  394. }
  395. if (isset($sorted_list_ary[$i])) {
  396. $sorted_list_ary[$i] = '';
  397. }
  398. if (isset($read[0])) {
  399. $sorted_list_ary[$i] = $read[0];
  400. }
  401. else {
  402. $sorted_list_ary[$i] = '';
  403. }
  404. if (isset($sorted_list_ary[$i]) &&
  405. strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
  406. $inbox_in_list = true;
  407. }
  408. }
  409. /*
  410. * Just in case they're not subscribed to their inbox,
  411. * we'll get it for them anyway
  412. */
  413. if (!$inbox_subscribed || !$inbox_in_list) {
  414. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  415. true, $response, $message);
  416. /* Another workaround for EIMS */
  417. if (isset($inbox_ary[1]) &&
  418. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  419. $inbox_ary[0], $regs)) {
  420. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  421. '"' . $regs[2];
  422. }
  423. $sorted_list_ary[] = $inbox_ary[0];
  424. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  425. }
  426. $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  427. /* Now, lets sort for special folders */
  428. $boxesnew = $used = array();
  429. /* Find INBOX */
  430. foreach ( $boxesall as $k => $box ) {
  431. if ( strtolower($box['unformatted']) == 'inbox') {
  432. $boxesnew[] = $box;
  433. $used[$k] = true;
  434. } else {
  435. $used[$k] = false;
  436. }
  437. }
  438. /* List special folders and their subfolders, if requested. */
  439. if ($list_special_folders_first) {
  440. foreach ( $boxesall as $k => $box ) {
  441. if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
  442. $boxesnew[] = $box;
  443. $used[$k] = true;
  444. }
  445. $spec_sub = str_replace('&nbsp;', '', $box['formatted']);
  446. /* In case of problems with preg
  447. here is a ereg version
  448. if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
  449. if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
  450. $boxesnew[] = $box;
  451. $used[$k] = true;
  452. }
  453. }
  454. }
  455. /* Rest of the folders */
  456. foreach ( $boxesall as $k => $box ) {
  457. if ( !$used[$k] ) {
  458. $boxesnew[] = $box;
  459. }
  460. }
  461. }
  462. return $boxesnew;
  463. }
  464. /*
  465. * Returns a list of all folders, subscribed or not
  466. */
  467. function sqimap_mailbox_list_all($imap_stream) {
  468. global $list_special_folders_first, $folder_prefix, $delimiter;
  469. require_once('../functions/array.php');
  470. $ssid = sqimap_session_id();
  471. $lsid = strlen( $ssid );
  472. fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
  473. $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
  474. $g = 0;
  475. $phase = 'inbox';
  476. for ($i = 0; $i < count($read_ary); $i++) {
  477. /* Another workaround for EIMS */
  478. if (isset($read_ary[$i + 1]) &&
  479. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  480. $read_ary[$i], $regs)) {
  481. $i ++;
  482. $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
  483. }
  484. if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
  485. /* Store the raw IMAP reply */
  486. $boxes[$g]['raw'] = $read_ary[$i];
  487. /* Count number of delimiters ($delimiter) in folder name */
  488. $mailbox = find_mailbox_name($read_ary[$i]);
  489. $dm_count = substr_count($mailbox, $delimiter);
  490. if (substr($mailbox, -1) == $delimiter) {
  491. /* If name ends in delimiter - decrement count by one */
  492. $dm_count--;
  493. }
  494. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  495. $boxesallbyname[$mailbox] = $g;
  496. $parentfolder = readMailboxParent($mailbox, $delimiter);
  497. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  498. (ereg('^'.$folder_prefix, $mailbox)) ||
  499. ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  500. if ($dm_count) {
  501. $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
  502. }
  503. else {
  504. $boxes[$g]['formatted'] = '';
  505. }
  506. $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
  507. }
  508. else {
  509. $boxes[$g]['formatted'] = $mailbox;
  510. }
  511. $boxes[$g]['unformatted-dm'] = $mailbox;
  512. if (substr($mailbox, -1) == $delimiter) {
  513. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  514. }
  515. $boxes[$g]['unformatted'] = $mailbox;
  516. $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
  517. $boxes[$g]['id'] = $g;
  518. /* Now lets get the flags for this mailbox */
  519. $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
  520. true, $response, $message);
  521. /* Another workaround for EIMS */
  522. if (isset($read_mlbx[1]) &&
  523. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
  524. $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
  525. }
  526. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1);
  527. $flags = substr($flags, 0, strpos($flags, ')'));
  528. $flags = str_replace('\\', '', $flags);
  529. $flags = trim(strtolower($flags));
  530. if ($flags) {
  531. $boxes[$g]['flags'] = explode(' ', $flags);
  532. } else {
  533. $boxes[$g]['flags'] = array();
  534. }
  535. }
  536. $g++;
  537. }
  538. if(is_array($boxes)) {
  539. $boxes = ary_sort ($boxes, 'unformatted', 1);
  540. }
  541. return $boxes;
  542. }
  543. ?>