imap_mailbox.php 21 KB

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