+ /******************************************************************************
+ ** Formats a mailbox into 4 parts for the $boxes array
+ **
+ ** The four parts are:
+ **
+ ** raw - Raw LIST/LSUB response from the IMAP server
+ ** formatted - nicely formatted folder name
+ ** unformatted - unformatted, but with delimiter at end removed
+ ** unformatted-dm - folder name as it appears in raw response
+ ** unformatted-disp - unformatted without $folder_prefix
+ **
+ ******************************************************************************/
+ function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
+ global $folder_prefix;
+
+ // Process each folder line
+ for ($g=0; $g < count($line); $g++) {
+
+ // Store the raw IMAP reply
+ if (isset($line[$g]))
+ $boxes[$g]["raw"] = $line[$g];
+ else
+ $boxes[$g]["raw"] = "";
+
+
+ // Count number of delimiters ($dm) in folder name
+ $mailbox = trim($line_lsub[$g]);
+ $dm_count = countCharInString($mailbox, $dm);
+ if (substr($mailbox, -1) == $dm)
+ $dm_count--; // If name ends in delimiter - decrement count by one
+
+ // Format folder name, but only if it's a INBOX.* or have
+ // a parent.
+ $boxesbyname[$mailbox] = $g;
+ $parentfolder = readMailboxParent($mailbox, $dm);
+ if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
+ (ereg("^".$folder_prefix, $mailbox)) ||
+ ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
+ $indent = $dm_count - (countCharInString($folder_prefix, $dm));
+ if ($indent)
+ $boxes[$g]["formatted"] = str_repeat(" ", $indent);
+ else
+ $boxes[$g]["formatted"] = '';
+ $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
+ } else {
+ $boxes[$g]["formatted"] = $mailbox;
+ }
+
+ $boxes[$g]['unformatted-dm'] = $mailbox;
+ if (substr($mailbox, -1) == $dm)
+ $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
+ $boxes[$g]['unformatted'] = $mailbox;
+ $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
+ $boxes[$g]['id'] = $g;
+
+ if (isset($line[$g]))
+ ereg("\(([^)]*)\)",$line[$g],$regs);
+ $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
+ if ($flags) {
+ $boxes[$g]['flags'] = explode(' ', $flags);
+ }
+ }
+
+ return $boxes;
+ }
+
+ /* Apparently you must call a user function with usort instead
+ * of calling a built-in directly. Stupid.
+ * Patch from dave_michmerhuizen@yahoo.com
+ * Allows case insensitivity when sorting folders
+ */
+ function user_strcasecmp($a, $b)
+ {
+ return strcasecmp($a, $b);
+ }
+