* Changed some regexps into string matches and the like
[squirrelmail.git] / functions / imap_search.php
1 <?php
2 /******************************************************************
3 ** IMAP SEARCH ROUTIES
4 ** $Id$
5 *****************************************************************/
6 if (!isset($imap_php))
7 include("../functions/imap.php");
8 if (!isset($date_php))
9 include("../functions/date.php");
10 if (!isset($array_php))
11 include("../functions/array.php");
12 if (!isset($mailbox_display_php))
13 include("../functions/mailbox_display.php");
14 if (!isset($mime_php))
15 include("../functions/mime.php");
16
17 $imap_search_php = true;
18
19 function sqimap_search($imapConnection,$search_where,$search_what,$mailbox,$color) {
20 global $msgs, $message_highlight_list, $squirrelmail_language, $languages, $index_order;
21 $urlMailbox = urlencode($mailbox);
22
23 # Construct the Search QuERY
24
25 if (isset($languages[$squirrelmail_language]["CHARSET"]) && $languages[$squirrelmail_language]["CHARSET"]) {
26 $ss = "a001 SEARCH CHARSET ".$languages[$squirrelmail_language]["CHARSET"]." ALL $search_where \"$search_what\"\r\n";
27 } else {
28 $ss = "a001 SEARCH ALL $search_where \"$search_what\"\r\n";
29 }
30 fputs($imapConnection,$ss);
31
32 # Read Data Back From IMAP
33 $readin = sqimap_read_data ($imapConnection, "a001", false, $result, $message);
34 if (isset($languages[$squirrelmail_language]["CHARSET"]) && strtolower($result) == "no") {
35 $ss = "a001 SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\"\r\n";
36 fputs ($imapConnection, $ss);
37 $readin = sqimap_read_data ($imapConnection, "a001", true, $result, $message);
38 }
39 unset($messagelist); $msgs=""; $c = 0;
40
41 #Keep going till we find the SEARCH responce
42 while ($c < count($readin)) {
43
44 #Check to see if a SEARCH Responce was recived
45 if (substr($readin[$c],0,9) == "* SEARCH ")
46 $messagelist = explode(" ",substr($readin[$c],9));
47 else if (isset($errors))
48 $errors = $errors.$readin[$c];
49 else
50 $errors = $readin[$c];
51 $c++;
52 }
53
54 #If nothing is found * SEARCH should be the first error else echo errors
55 if (isset($errors) && strstr($errors,"* SEARCH")) {
56 echo "<br><CENTER>No Messages Found</CENTER>";
57 return;
58 } else if (isset($errors)) {
59 echo "<!-- ".$errors." -->";
60 }
61
62 # HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
63 # exsitising code with a search true/false varible.
64
65
66 global $sent_folder;
67 for ($q = 0; $q < count($messagelist); $q++) {
68 $messagelist[$q] = trim($messagelist[$q]);
69 if ($mailbox == $sent_folder)
70 $hdr = sqimap_get_small_header ($imapConnection, $messagelist[$q], true);
71 else
72 $hdr = sqimap_get_small_header ($imapConnection, $messagelist[$q], false);
73
74 $from[$q] = $hdr->from;
75 $date[$q] = $hdr->date;
76 $subject[$q] = $hdr->subject;
77 $to[$q] = $hdr->to;
78 $priority[$q] = $hdr->priority;
79 $cc[$q] = $hdr->cc;
80 $size[$q] = $hdr->size;
81 $type[$q] = $hdr->type0;
82 $id[$q] = $messagelist[$q];
83 $flags[$q] = sqimap_get_flags ($imapConnection, $messagelist[$q]);
84 }
85
86 $j = 0;
87 while ($j < count($messagelist)) {
88 $date[$j] = ereg_replace(' ', ' ', $date[$j]);
89 $tmpdate = explode(" ", trim($date[$j]));
90
91 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
92 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
93 $messages[$j]["ID"] = $id[$j];
94 $messages[$j]["FROM"] = decodeHeader($from[$j]);
95 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
96 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
97 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
98 $messages[$j]["TO"] = decodeHeader($to[$j]);
99 $messages[$j]["PRIORITY"] = $priority[$j];
100 $messages[$j]["CC"] = $cc[$j];
101 $messages[$j]["SIZE"] = $size[$j];
102 $messages[$j]["TYPE0"] = $type[$j];
103
104 $num = 0;
105 while ($num < count($flags[$j])) {
106 if ($flags[$j][$num] == "Deleted") {
107 $messages[$j]["FLAG_DELETED"] = true;
108 }
109 else if ($flags[$j][$num] == "Answered") {
110 $messages[$j]["FLAG_ANSWERED"] = true;
111 }
112 else if ($flags[$j][$num] == "Seen") {
113 $messages[$j]["FLAG_SEEN"] = true;
114 }
115 else if ($flags[$j][$num] == "Flagged") {
116 $messages[$j]["FLAG_FLAGGED"] = true;
117 }
118 $num++;
119 }
120 $j++;
121 }
122
123 /** Find and remove the ones that are deleted */
124 $i = 0;
125 $j = 0;
126 while ($j < count($messagelist)) {
127 if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) {
128 $j++;
129 continue;
130 }
131 $msgs[$i] = $messages[$j];
132
133 $i++;
134 $j++;
135 }
136 $numMessages = $i;
137
138 // There's gotta be messages in the array for it to sort them.
139
140 if (count($messagelist) > 0) {
141 $j=0;
142 if (!isset ($msg)) { $msg = ""; }
143 mail_message_listing_beginning($imapConnection,
144 "move_messages.php?msg=$msg&mailbox=$urlMailbox&where=".urlencode($search_where)."&what=".urlencode($search_what),
145 '', -1, '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>',
146 '&nbsp;');
147
148
149 while ($j < count($msgs)) {
150 printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, "", 0, $search_where, $search_what);
151 $j++;
152 }
153 echo "</table>";
154 echo "</tr></td></table>";
155 }
156 }
157
158 ?>