imap_mailbox.php 21 KB

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