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