/** * Formats a mailbox into parts for the $boxesall array * * The parts are: * <ul> * <li>raw - Raw LIST/LSUB response from the IMAP server * <li>formatted - nicely formatted folder name * <li>unformatted - unformatted, but with delimiter at end removed * <li>unformatted-dm - folder name as it appears in raw response * <li>unformatted-disp - unformatted without $folder_prefix * <li>id - TODO: document me * <li>flags - TODO: document me * </ul> * Before 1.2.0 used third argument for delimiter. * * Before 1.5.1 used second argument for lsub line. Argument was removed in order to use * find_mailbox_name() on the raw input. Since 1.5.1 includes RFC3501 names in flags * array (for example, "\NoSelect" in addition to "noselect") * @param array $line * @return array * @since 1.0 or older * @todo document id and flags keys in boxes array and function arguments. */ function sqimap_mailbox_parse($line) { global $folder_prefix, $delimiter; /* Process each folder line */ for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) { /* Store the raw IMAP reply */ if (isset($line[$g])) { $boxesall[$g]['raw'] = $line[$g]; } else { $boxesall[$g]['raw'] = ''; } /* Count number of delimiters ($delimiter) in folder name */ $mailbox = find_mailbox_name($line[$g]); $dm_count = substr_count($mailbox, $delimiter); if (substr($mailbox, -1) == $delimiter) { /* If name ends in delimiter, decrement count by one */ $dm_count--; } /* Format folder name, but only if it's a INBOX.* or has a parent. */ $boxesallbyname[$mailbox] = $g; $parentfolder = readMailboxParent($mailbox, $delimiter); if (strtolower(substr($mailbox, 0, 5)) == "inbox" || substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix || isset($boxesallbyname[$parentfolder]) && strlen($parentfolder) > 0) { $indent = $dm_count - substr_count($folder_prefix, $delimiter); if ($indent > 0) { $boxesall[$g]['formatted'] = str_repeat(' ', $indent); } else { $boxesall[$g]['formatted'] = ''; } $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter)); } else { $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox); } $boxesall[$g]['unformatted-dm'] = $mailbox; if (substr($mailbox, -1) == $delimiter) { $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); } $boxesall[$g]['unformatted'] = $mailbox; if (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) { $mailbox = substr($mailbox, strlen($folder_prefix)); } $boxesall[$g]['unformatted-disp'] = $mailbox; $boxesall[$g]['id'] = $g; $boxesall[$g]['flags'] = array(); if (isset($line[$g]) && preg_match('/\\(([^)]*)\\)/', $line[$g], $regs)) { /** * Since 1.5.1 flags are stored with RFC3501 naming * and also the old way for backwards compatibility * so for example "\NoSelect" and "noselect" */ $flags = trim($regs[1]); if ($flags) { $flagsarr = explode(' ', $flags); $flagsarrnew = $flagsarr; // add old type foreach ($flagsarr as $flag) { $flagsarrnew[] = strtolower(str_replace('\\', '', $flag)); } $boxesall[$g]['flags'] = $flagsarrnew; } } } return $boxesall; }
/** * Returns a list of all folders, subscribed or not */ function sqimap_mailbox_list_all($imap_stream) { global $list_special_folders_first, $folder_prefix, $delimiter; $ssid = sqimap_session_id(); $lsid = strlen($ssid); fputs($imap_stream, $ssid . " LIST \"{$folder_prefix}\" *\r\n"); $read_ary = sqimap_read_data($imap_stream, $ssid, true, $response, $message); $g = 0; $phase = 'inbox'; $fld_pre_length = strlen($folder_prefix); for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) { /* Another workaround for EIMS */ if (isset($read_ary[$i + 1]) && ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)\$", $read_ary[$i], $regs)) { $i++; $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2]; } if (substr($read_ary[$i], 0, $lsid) != $ssid) { /* Store the raw IMAP reply */ $boxes[$g]['raw'] = $read_ary[$i]; /* Count number of delimiters ($delimiter) in folder name */ $mailbox = find_mailbox_name($read_ary[$i]); $dm_count = substr_count($mailbox, $delimiter); if (substr($mailbox, -1) == $delimiter) { /* If name ends in delimiter - decrement count by one */ $dm_count--; } /* Format folder name, but only if it's a INBOX.* or has a parent. */ $boxesallbyname[$mailbox] = $g; $parentfolder = readMailboxParent($mailbox, $delimiter); if (eregi('^inbox' . quotemeta($delimiter), $mailbox) || ereg('^' . $folder_prefix, $mailbox) || isset($boxesallbyname[$parentfolder]) && strlen($parentfolder) > 0) { if ($dm_count) { $boxes[$g]['formatted'] = str_repeat(' ', $dm_count); } else { $boxes[$g]['formatted'] = ''; } $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter)); } else { $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox); } $boxes[$g]['unformatted-dm'] = $mailbox; if (substr($mailbox, -1) == $delimiter) { $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); } $boxes[$g]['unformatted'] = $mailbox; $boxes[$g]['unformatted-disp'] = substr($mailbox, $fld_pre_length); $boxes[$g]['id'] = $g; /* Now lets get the flags for this mailbox */ $read_mlbx = $read_ary[$i]; // $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"", // true, $response, $message); /* Another workaround for EIMS */ // if (isset($read_mlbx[1]) && // ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) { // $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2]; // } // echo $read_mlbx[0] .' raw 2 <br>'; $flags = substr($read_mlbx, strpos($read_mlbx, '(') + 1); $flags = substr($flags, 0, strpos($flags, ')')); $flags = str_replace('\\', '', $flags); $flags = trim(strtolower($flags)); if ($flags) { $boxes[$g]['flags'] = explode(' ', $flags); } else { $boxes[$g]['flags'] = array(); } } $g++; } if (is_array($boxes)) { sort($boxes); } return $boxes; }
/** * Returns a list of all folders, subscribed or not */ function sqimap_mailbox_list_all($imap_stream) { global $list_special_folders_first, $folder_prefix, $delimiter; $read_ary = sqimap_run_command($imap_stream, "LIST \"{$folder_prefix}\" *", true, $response, $message, false); $read_ary = compact_mailboxes_response($read_ary); $g = 0; $phase = 'inbox'; $fld_pre_length = strlen($folder_prefix); for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) { /* Store the raw IMAP reply */ $boxes[$g]['raw'] = $read_ary[$i]; /* Count number of delimiters ($delimiter) in folder name */ $mailbox = find_mailbox_name($read_ary[$i]); $dm_count = substr_count($mailbox, $delimiter); if (substr($mailbox, -1) == $delimiter) { /* If name ends in delimiter - decrement count by one */ $dm_count--; } /* Format folder name, but only if it's a INBOX.* or has a parent. */ $boxesallbyname[$mailbox] = $g; $parentfolder = readMailboxParent($mailbox, $delimiter); if (eregi('^inbox' . quotemeta($delimiter), $mailbox) || ereg('^' . $folder_prefix, $mailbox) || isset($boxesallbyname[$parentfolder]) && strlen($parentfolder) > 0) { if ($dm_count) { $boxes[$g]['formatted'] = str_repeat(' ', $dm_count); } else { $boxes[$g]['formatted'] = ''; } $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter)); } else { $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox); } $boxes[$g]['unformatted-dm'] = $mailbox; if (substr($mailbox, -1) == $delimiter) { $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); } $boxes[$g]['unformatted'] = $mailbox; $boxes[$g]['unformatted-disp'] = substr($mailbox, $fld_pre_length); $boxes[$g]['id'] = $g; /* Now lets get the flags for this mailbox */ $read_mlbx = $read_ary[$i]; $flags = substr($read_mlbx, strpos($read_mlbx, '(') + 1); $flags = substr($flags, 0, strpos($flags, ')')); $flags = str_replace('\\', '', $flags); $flags = trim(strtolower($flags)); if ($flags) { $boxes[$g]['flags'] = explode(' ', $flags); } else { $boxes[$g]['flags'] = array(); } $g++; } if (is_array($boxes)) { sort($boxes); } return $boxes; }