+/*
+ * Gets the number of messages in the current mailbox.
+ */
+function sqimap_get_num_messages ($imap_stream, $mailbox) {
+
+ $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];
+ }
+ }
+ return sprintf( "BUG! Couldn't get number of messages in %s!", $mailbox );
+
+}
+
+
+/*
+* Returns a displayable email address
+*/
+function sqimap_find_email ($string) {
+ /** Luke Ehresman <lehresma@css.tayloru.edu>
+ ** <lehresma@css.tayloru.edu>
+ ** lehresma@css.tayloru.edu
+ **
+ ** What about
+ ** lehresma@css.tayloru.edu (Luke Ehresman)
+ **/
+
+ if (ereg("<([^>]+)>", $string, $regs)) {
+ $string = $regs[1];
+ }
+ return trim($string);
+}
+
+
+/*
+ * Takes the From: field, and creates a displayable name.
+ * Luke Ehresman <lkehresman@yahoo.com>
+ * "Luke Ehresman" <lkehresman@yahoo.com>
+ * lkehresman@yahoo.com (Luke Ehresman)
+ * become: Luke Ehresman
+ * <lkehresman@yahoo.com>
+ * becomes: lkehresman@yahoo.com
+ */
+function sqimap_find_displayable_name ($string) {
+ $string = trim($string);
+
+ if ( ereg('^(.+)<.*>', $string, $regs) ) {
+ $string = ereg_replace ('"', '', $regs[1] );
+ }
+ 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");
+ $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
+ ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
+ return $regs[1];
+}
+
+
+/*
+* Saves a message to a given folder -- used for saving sent messages
+*/
+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);
+}
+
+function sqimap_append_done ($imap_stream) {
+ fputs ($imap_stream, "\r\n");
+ $tmp = fgets ($imap_stream, 1024);
+}