Index: branches/2.3/expressoMail1_2/inc/class.imap_functions.inc.php
===================================================================
--- branches/2.3/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 4870)
+++ branches/2.3/expressoMail1_2/inc/class.imap_functions.inc.php	(revision 4927)
@@ -3584,47 +3584,55 @@
 				}
 				else{
-					$search_criteria = imap_search($mbox_stream, $filter, SE_UID);
-                                        if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag'])
-					{
-                                            if((!strpos($filter,"FLAGGED") === false) || (!strpos($filter,"UNFLAGGED") === false))
+                                    if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_important_flag'])
+                                    {
+                                        if((!strpos($filter,"FLAGGED") === false) || (!strpos($filter,"UNFLAGGED") === false))
+                                        {
+                                            $num_msgs = imap_num_msg($mbox_stream);
+                                            $flagged_msgs = array();
+                                            for ($i=$num_msgs; $i>0; $i--)
                                             {
-                                                $num_msgs = imap_num_msg($mbox_stream);
-    						$flagged_msgs = array();
-						for ($i=$num_msgs; $i>0; $i--)
-						{
-        						$iuid = @imap_uid($this->mbox,$i);
-							$header = $this->get_header($iuid);
-							if(trim($header->Flagged))
-							{
-                						$flagged_msgs[$i] = $iuid;
-							}
-						}
-						if((count($flagged_msgs) >0) && (strpos($filter,"UNFLAGGED") === false))
-						{
-                                                    $arry_diff = is_array($search_criteria) ? array_diff($flagged_msgs,$search_criteria):$flagged_msgs;
-                                                    foreach($arry_diff as $msg)
-                                                    {
-							$search_criteria[] = $msg;
-                                                    }
-						}
-						else if((count($flagged_msgs) >0) && (is_array($search_criteria)) && (!strpos($filter,"UNFLAGGED") === false))
-						{
-                                                    $search_criteria = array_diff($search_criteria,$flagged_msgs);
-						}
+                                                $iuid = @imap_uid($this->mbox,$i);
+                                                $header = $this->get_header($iuid);
+                                                if(trim($header->Flagged))
+                                                {
+                                                        $flagged_msgs[$i] = $iuid;
+                                                }
                                             }
-					}
-
-					if( is_array( $search_criteria) )
-					{
-						foreach($search_criteria as $new_search)
-						{
-							$elem = $this->get_msg_detail($new_search,$name_box,$mbox_stream); 
-							$elem['boxname'] = mb_convert_encoding( $name_box, "ISO_8859-1", "UTF7-IMAP" ); 
-							$elem['uid'] = $new_search;
-                                                        /* compare dates in ordering */
-							$elem['udatecomp'] = substr ($elem['udate'], -4) ."-". substr ($elem['udate'], 3, 2) ."-". substr ($elem['udate'], 0, 2);
-							$retorno[] = $elem;
-						}
-					}
+
+                                            $new_filter = trim(preg_replace('/UNFLAGGED|FLAGGED/','',$filter));
+
+                                            $search_without_flagged = imap_search($mbox_stream, $new_filter, SE_UID);
+
+                                            if (strpos($filter,"UNFLAGGED") === false)
+                                            {
+                                                $search_criteria = array_intersect($search_without_flagged, $flagged_msgs);
+                                            }
+                                            else
+                                            {
+                                                $search_criteria = array_diff($search_without_flagged,$flagged_msgs);
+                                            }
+                                        }
+                                        else
+                                        {
+                                            $search_criteria = imap_search($mbox_stream, $filter, SE_UID);
+                                        }
+                                    }
+                                    else
+                                    {
+                                        $search_criteria = imap_search($mbox_stream, $filter, SE_UID);
+                                    }
+
+                                    if( is_array( $search_criteria) )
+                                    {
+                                        foreach($search_criteria as $new_search)
+                                        {
+                                            $elem = $this->get_msg_detail($new_search,$name_box,$mbox_stream);
+                                            $elem['boxname'] = mb_convert_encoding( $name_box, "ISO_8859-1", "UTF7-IMAP" );
+                                            $elem['uid'] = $new_search;
+                                            /* compare dates in ordering */
+                                            $elem['udatecomp'] = substr ($elem['udate'], -4) ."-". substr ($elem['udate'], 3, 2) ."-". substr ($elem['udate'], 0, 2);
+                                            $retorno[] = $elem;
+                                        }
+                                    }
 				}
 			}
