*
* @return string a 4 chars unique string
*/
+
+global $sqimap_session_id;
+$sqimap_session_id = 1;
function sqimap_session_id() {
- global $data_dir, $username;
+ global $data_dir, $username, $sqimap_session_id;
- $IMAPSessionID = substr(session_id(), -4);
- if( $IMAPSessionID == '' ) {
- $IMAPSessionID = 'A001';
- }
+ return( sprintf("A%03d", $sqimap_session_id++) );
+}
+
+/******************************************************************************
+** Both send a command and accept the result from the command. This is
+** to allow proper session number handling.
+******************************************************************************/
+
+function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message) {
+ $sid = sqimap_session_id();
+ fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
+ $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message);
+ return $read;
+}
- return( $IMAPSessionID );
+function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message) {
+ $sid = sqimap_session_id();
+ fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
+ $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message);
+ return $read;
}
exit;
}
- fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) .
- '" "' . quoteIMAP($password) . "\"\r\n");
- $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message);
+ $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
+ $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
/** If the connection was not successful, lets see why **/
if ($response != 'OK') {
}
exit;
} else {
- // If the user does not log in with the correct
- // username and password it is not possible to get the
- // correct locale from the user's preferences.
- // Therefore, apply the same hack as on the login
- // screen.
+ /* If the user does not log in with the correct
+ * username and password it is not possible to get the
+ * correct locale from the user's preferences.
+ * Therefore, apply the same hack as on the login
+ * screen.
+ */
- // $squirrelmail_language is set by a cookie when
- // the user selects language and logs out
+ /* $squirrelmail_language is set by a cookie when
+ * the user selects language and logs out
+ */
set_up_language($squirrelmail_language, true);
* Simply logs out the imap session
*/
function sqimap_logout ($imap_stream) {
- fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n");
+ /* Logout is not valid until the server returns 'BYE' */
+ sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
}
function sqimap_capability($imap_stream, $capability) {
global $sqimap_capabilities;
if (!is_array($sqimap_capabilities)) {
- fputs ($imap_stream, sqimap_session_id() . " CAPABILITY\r\n");
- $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
-
+ $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
+
$c = explode(' ', $read[0]);
for ($i=2; $i < count($c); $i++) {
$cap_list = explode('=', $c[$i]);
OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
OS: We want to lookup all personal NAMESPACES...
*/
- fputs ($imap_stream, sqimap_session_id() . " NAMESPACE\r\n");
- $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
+ $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
$pn = $data2[1];
*/
function sqimap_get_num_messages ($imap_stream, $mailbox) {
- fputs ($imap_stream, sqimap_session_id() . " EXAMINE \"$mailbox\"\r\n");
- $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
+ $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message);
for ($i = 0; $i < count($read_ary); $i++) {
if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
return $regs[1];
/*
-* Takes the From: field, and creates a displayable name.
-* Luke Ehresman <lkehresman@yahoo.com>
-* becomes: Luke Ehresman
-* <lkehresman@yahoo.com>
-* becomes: lkehresman@yahoo.com
-*/
+ * Takes the From: field, and creates a displayable name.
+ * Luke Ehresman <lkehresman@yahoo.com>
+ * "Luke Ehresman" <lkehresman@yahoo.com>
+ * lkehresman@yahoo.com (Luke Ehresman)
+ * becomes: Luke Ehresman
+ * <lkehresman@yahoo.com>
+ * becomes: lkehresman@yahoo.com
+ */
function sqimap_find_displayable_name ($string) {
- $string = ' '.trim($string);
- $orig_string = $string;
- if (strpos($string, '<') && strpos($string, '>')) {
- if (strpos($string, '<') == 1) {
- $string = sqimap_find_email($string);
- } else {
- $string = trim($string);
- $string = substr($string, 0, strpos($string, '<'));
- $string = ereg_replace ('"', '', $string);
- }
-
+ $string = trim($string);
+
+ if ( ereg('^(.+)<.*>', $string, $regs) ) {
+ $orig_string = $string;
+ $string = ereg_replace ('"', '', $regs[1] );
if (trim($string) == '') {
- $string = sqimap_find_email($orig_string);
+ $string = sqimap_find_email($orig_string);
+ }
+ if( $string == '' || $string == ' ' ){
+ $string = ' ';
}
}
- return $string;
-}
-
+ elseif ( ereg('\((.*)\)', $string, $regs) ) {
+ if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){
+ if ( ereg('^(.+) \(', $string, $regs) ) {
+ $string = ereg_replace( ' \(\)$', '', $string );
+ } else {
+ $string = ' ';
+ }
+ } else {
+ $string = $regs[1];
+ }
+ }
+ else {
+ $string = sqimap_find_email($string);
+ }
+ return trim($string);
+}
+
/*
* Returns the number of unseen messages in this folder
*/
function sqimap_unseen_messages ($imap_stream, $mailbox) {
//fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
- fputs ($imap_stream, sqimap_session_id() . " STATUS \"$mailbox\" (UNSEEN)\r\n");
- $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
+ $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
return $regs[1];
}
*/
function sqimap_append ($imap_stream, $sent_folder, $length) {
fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
- $tmp = fgets ($imap_stream, 1024);
+ $tmp = fgets ($imap_stream, 1024);
}
function sqimap_append_done ($imap_stream) {