imap_mailbox.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. }
  154. /***********************************************************************
  155. ** Determines if the user is subscribed to the folder or not
  156. **********************************************************************/
  157. function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
  158. $boxesall = sqimap_mailbox_list ($imap_stream);
  159. foreach ($boxesall as $ref) {
  160. if ($ref['unformatted'] == $folder) {
  161. return TRUE;
  162. }
  163. }
  164. return false;
  165. }
  166. /*
  167. Renames a mailbox
  168. */
  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,
  182. TRUE, $response, $message);
  183. sqimap_unsubscribe($imap_stream, $old_name.$postfix);
  184. sqimap_subscribe($imap_stream, $new_name.$postfix);
  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. }
  198. }
  199. }
  200. }
  201. /******************************************************************************
  202. ** Formats a mailbox into 4 parts for the $boxesall array
  203. **
  204. ** The four parts are:
  205. **
  206. ** raw - Raw LIST/LSUB response from the IMAP server
  207. ** formatted - nicely formatted folder name
  208. ** unformatted - unformatted, but with delimiter at end removed
  209. ** unformatted-dm - folder name as it appears in raw response
  210. ** unformatted-disp - unformatted without $folder_prefix
  211. **
  212. ******************************************************************************/
  213. function sqimap_mailbox_parse ($line, $line_lsub)
  214. {
  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 have */
  233. /* a parent. */
  234. $boxesallbyname[$mailbox] = $g;
  235. $parentfolder = readMailboxParent($mailbox, $delimiter);
  236. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  237. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  238. ( isset($boxesallbyname[$parentfolder]) &&
  239. (strlen($parentfolder) > 0) ) ) {
  240. $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
  241. if ($indent > 0) {
  242. $boxesall[$g]['formatted'] = str_repeat("&nbsp;&nbsp;", $indent);
  243. }
  244. else {
  245. $boxesall[$g]['formatted'] = '';
  246. }
  247. $boxesall[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
  248. }
  249. else {
  250. $boxesall[$g]['formatted'] = $mailbox;
  251. }
  252. $boxesall[$g]['unformatted-dm'] = $mailbox;
  253. if (substr($mailbox, -1) == $delimiter) {
  254. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  255. }
  256. $boxesall[$g]['unformatted'] = $mailbox;
  257. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  258. $mailbox = substr($mailbox, strlen($folder_prefix));
  259. }
  260. $boxesall[$g]['unformatted-disp'] = $mailbox;
  261. $boxesall[$g]['id'] = $g;
  262. $boxesall[$g]['flags'] = array();
  263. if (isset($line[$g])) {
  264. ereg("\(([^)]*)\)",$line[$g],$regs);
  265. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  266. if ($flags) {
  267. $boxesall[$g]['flags'] = explode(' ', $flags);
  268. }
  269. }
  270. }
  271. return $boxesall;
  272. }
  273. /**
  274. * Sorting function used to sort mailbox names.
  275. * + Original patch from dave_michmerhuizen@yahoo.com
  276. * + Allows case insensitivity when sorting folders
  277. * + Takes care of the delimiter being sorted to the end, causing
  278. * subfolders to be listed in below folders that are prefixed
  279. * with their parent folders name.
  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;
  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 = $inbox_subscribed = FALSE;
  332. require_once('../src/load_prefs.php');
  333. require_once('../functions/array.php');
  334. /** LSUB array **/
  335. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
  336. TRUE, $response, $message);
  337. /* Section about removing the last element was removed */
  338. /* We don't return "* OK" anymore from sqimap_read_data */
  339. $sorted_lsub_ary = array();
  340. for ($i=0;$i < count($lsub_ary); $i++) {
  341. /* Workaround for EIMS */
  342. /* Doesn't work if the mailbox name is multiple lines */
  343. if (isset($lsub_ary[$i + 1]) &&
  344. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  345. $lsub_ary[$i], $regs)) {
  346. $i ++;
  347. $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
  348. '"' . $regs[2];
  349. }
  350. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  351. $sorted_lsub_ary[] = $temp_mailbox_name;
  352. if (strtoupper($temp_mailbox_name) == 'INBOX') {
  353. $inbox_subscribed = TRUE;
  354. }
  355. }
  356. $new_ary = array();
  357. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  358. if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
  359. $new_ary[] = $sorted_lsub_ary[$i];
  360. }
  361. }
  362. $sorted_lsub_ary = $new_ary;
  363. if (isset($sorted_lsub_ary)) {
  364. usort($sorted_lsub_ary, 'user_strcasecmp');
  365. }
  366. /** LIST array **/
  367. $sorted_list_ary = array();
  368. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  369. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  370. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  371. }
  372. else {
  373. $mbx = $sorted_lsub_ary[$i];
  374. }
  375. $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
  376. TRUE, $response, $message);
  377. /* Another workaround for EIMS */
  378. if (isset($read[1]) &&
  379. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  380. $read[0], $regs)) {
  381. $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
  382. '"' . $regs[2];
  383. }
  384. if (isset($sorted_list_ary[$i])) {
  385. $sorted_list_ary[$i] = '';
  386. }
  387. if (isset($read[0])) {
  388. $sorted_list_ary[$i] = $read[0];
  389. }
  390. else {
  391. $sorted_list_ary[$i] = '';
  392. }
  393. if (isset($sorted_list_ary[$i]) &&
  394. strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
  395. $inbox_in_list = TRUE;
  396. }
  397. }
  398. /**
  399. * Just in case they're not subscribed to their inbox,
  400. * we'll get it for them anyway
  401. */
  402. if ($inbox_subscribed == false || $inbox_in_list == false) {
  403. $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
  404. TRUE, $response, $message);
  405. /* Another workaround for EIMS */
  406. if (isset($inbox_ary[1]) &&
  407. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  408. $inbox_ary[0], $regs)) {
  409. $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
  410. '"' . $regs[2];
  411. }
  412. $sorted_list_ary[] = $inbox_ary[0];
  413. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  414. }
  415. $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  416. /** Now, lets sort for special folders **/
  417. $boxesnew = $used = array();
  418. /* Find INBOX */
  419. foreach ( $boxesall as $k => $box ) {
  420. if ( strtolower($box['unformatted']) == 'inbox') {
  421. $boxesnew[] = $box;
  422. $used[$k] = TRUE;
  423. } else {
  424. $used[$k] = FALSE;
  425. }
  426. }
  427. /* List special folders and their subfolders, if requested. */
  428. if ($list_special_folders_first == TRUE) {
  429. foreach ( $boxesall as $k => $box ) {
  430. if ( !$used[$k] &&
  431. isSpecialMailbox( $box['unformatted'] ) ) {
  432. $boxesnew[] = $box;
  433. $used[$k] = TRUE;
  434. }
  435. }
  436. }
  437. /* Rest of the folders */
  438. foreach ( $boxesall as $k => $box ) {
  439. if ( !$used[$k] ) {
  440. $boxesnew[] = $box;
  441. }
  442. }
  443. }
  444. return( $boxesnew );
  445. }
  446. /*
  447. * Returns a list of all folders, subscribed or not
  448. */
  449. function sqimap_mailbox_list_all($imap_stream) {
  450. GLOBAL $list_special_folders_first, $folder_prefix,
  451. $delimiter;
  452. require_once('../functions/array.php');
  453. $ssid = sqimap_session_id();
  454. $lsid = strlen( $ssid );
  455. fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
  456. $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
  457. $g = 0;
  458. $phase = 'inbox';
  459. for ($i = 0; $i < count($read_ary); $i++) {
  460. /* Another workaround for EIMS */
  461. if (isset($read_ary[$i + 1]) &&
  462. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  463. $read_ary[$i], $regs)) {
  464. $i ++;
  465. $read_ary[$i] = $regs[1] . '"' .
  466. addslashes(trim($read_ary[$i])) .
  467. '"' . $regs[2];
  468. }
  469. if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
  470. /* Store the raw IMAP reply */
  471. $boxes[$g]['raw'] = $read_ary[$i];
  472. /* Count number of delimiters ($delimiter) in folder name */
  473. $mailbox = find_mailbox_name($read_ary[$i]);
  474. $dm_count = substr_count($mailbox, $delimiter);
  475. if (substr($mailbox, -1) == $delimiter) {
  476. /* If name ends in delimiter - decrement count by one */
  477. $dm_count--;
  478. }
  479. /* Format folder name, but only if it's a INBOX.* or have */
  480. /* 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'] =
  503. ereg_replace('^' . $folder_prefix, '', $mailbox);
  504. $boxes[$g]['id'] = $g;
  505. /** Now lets get the flags for this mailbox **/
  506. $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
  507. TRUE, $response, $message);
  508. /* Another workaround for EIMS */
  509. if (isset($read_mlbx[1]) &&
  510. ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  511. $read_mlbx[0], $regs)) {
  512. $read_mlbx[0] = $regs[1] . '"' .
  513. addslashes(trim($read_mlbx[1])) .
  514. '"' . $regs[2];
  515. }
  516. $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1);
  517. $flags = substr($flags, 0, strpos($flags, ')'));
  518. $flags = str_replace('\\', '', $flags);
  519. $flags = trim(strtolower($flags));
  520. if ($flags) {
  521. $boxes[$g]['flags'] = explode(' ', $flags);
  522. } else {
  523. $boxes[$g]['flags'] = array();
  524. }
  525. }
  526. $g++;
  527. }
  528. if(is_array($boxes)) {
  529. $boxes = ary_sort ($boxes, 'unformatted', 1);
  530. }
  531. return $boxes;
  532. }
  533. ?>