updated changelog
[squirrelmail.git] / functions / imap_search.php
CommitLineData
245a6892 1<?php
fc05541b 2/******************************************************************
3 ** IMAP SEARCH ROUTIES
245a6892 4 ** $Id$
fc05541b 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
99fa2b21 17 $imap_search_php = true;
18
fc05541b 19function sqimap_search($imapConnection,$search_where,$search_what,$mailbox,$color) {
df98cf5c 20 global $msgs, $message_highlight_list, $squirrelmail_language, $languages, $index_order;
1809bad8 21 $urlMailbox = urlencode($mailbox);
22
23 # Construct the Search QuERY
24
61423189 25 if (isset($languages[$squirrelmail_language]["CHARSET"]) && $languages[$squirrelmail_language]["CHARSET"]) {
a3432f47 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 }
1809bad8 30 fputs($imapConnection,$ss);
31
32 # Read Data Back From IMAP
367e4f70 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 }
1809bad8 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));
18a148f0 47 else if (isset($errors))
1809bad8 48 $errors = $errors.$readin[$c];
18a148f0 49 else
50 $errors = $readin[$c];
1809bad8 51 $c++;
52 }
53
54 #If nothing is found * SEARCH should be the first error else echo errors
61423189 55 if (isset($errors) && strstr($errors,"* SEARCH")) {
1809bad8 56 echo "<br><CENTER>No Messages Found</CENTER>";
57 return;
61423189 58 } else if (isset($errors)) {
1809bad8 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
6a109962 65
1809bad8 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);
99fa2b21 73
98c2f0af 74 $from[$q] = $hdr->from;
75 $date[$q] = $hdr->date;
76 $subject[$q] = $hdr->subject;
99fa2b21 77 $to[$q] = $hdr->to;
78 $priority[$q] = $hdr->priority;
79 $cc[$q] = $hdr->cc;
98c2f0af 80 $size[$q] = $hdr->size;
81 $type[$q] = $hdr->type0;
1809bad8 82 $id[$q] = $messagelist[$q];
1809bad8 83 $flags[$q] = sqimap_get_flags ($imapConnection, $messagelist[$q]);
84 }
fc05541b 85
1809bad8 86 $j = 0;
87 while ($j < count($messagelist)) {
146e0c45 88 $date[$j] = ereg_replace(' ', ' ', $date[$j]);
1809bad8 89 $tmpdate = explode(" ", trim($date[$j]));
90
91 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
92 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
99fa2b21 93 $messages[$j]["ID"] = $id[$j];
1809bad8 94 $messages[$j]["FROM"] = decodeHeader($from[$j]);
99fa2b21 95 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
1809bad8 96 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
99fa2b21 97 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
98 $messages[$j]["TO"] = decodeHeader($to[$j]);
98c2f0af 99 $messages[$j]["PRIORITY"] = $priority[$j];
99fa2b21 100 $messages[$j]["CC"] = $cc[$j];
98c2f0af 101 $messages[$j]["SIZE"] = $size[$j];
102 $messages[$j]["TYPE0"] = $type[$j];
1809bad8 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;
fc05541b 114 }
1809bad8 115 else if ($flags[$j][$num] == "Flagged") {
116 $messages[$j]["FLAG_FLAGGED"] = true;
117 }
118 $num++;
119 }
120 $j++;
121 }
fc05541b 122
1809bad8 123 /** Find and remove the ones that are deleted */
124 $i = 0;
125 $j = 0;
126 while ($j < count($messagelist)) {
61423189 127 if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) {
fc05541b 128 $j++;
1809bad8 129 continue;
fc05541b 130 }
1809bad8 131 $msgs[$i] = $messages[$j];
fc05541b 132
1809bad8 133 $i++;
134 $j++;
135 }
136 $numMessages = $i;
137
138 // There's gotta be messages in the array for it to sort them.
139
1809bad8 140 if (count($messagelist) > 0) {
141 $j=0;
61423189 142 if (!isset ($msg)) { $msg = ""; }
14eb705c 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;');
df98cf5c 147
1809bad8 148
149 while ($j < count($msgs)) {
150 printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, "", 0, $search_where, $search_what);
1809bad8 151 $j++;
fc05541b 152 }
1809bad8 153 echo "</table>";
154 echo "</tr></td></table>";
fc05541b 155 }
1809bad8 156 }
fc05541b 157
158?>