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