fixed more warnings
[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", true, $result, $message);
34 unset($messagelist); $msgs=""; $c = 0;
35
36 #Keep going till we find the SEARCH responce
37 while ($c < count($readin)) {
38
39 #Check to see if a SEARCH Responce was recived
40 if (substr($readin[$c],0,9) == "* SEARCH ")
41 $messagelist = explode(" ",substr($readin[$c],9));
42 else
43 $errors = $errors.$readin[$c];
44 $c++;
45 }
46
47 #If nothing is found * SEARCH should be the first error else echo errors
48 if (isset($errors) && strstr($errors,"* SEARCH")) {
49 echo "<br><CENTER>No Messages Found</CENTER>";
50 return;
51 } else if (isset($errors)) {
52 echo "<!-- ".$errors." -->";
53 }
54
55 # HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
56 # exsitising code with a search true/false varible.
57
58
59 global $sent_folder;
60 for ($q = 0; $q < count($messagelist); $q++) {
61 $messagelist[$q] = trim($messagelist[$q]);
62 if ($mailbox == $sent_folder)
63 $hdr = sqimap_get_small_header ($imapConnection, $messagelist[$q], true);
64 else
65 $hdr = sqimap_get_small_header ($imapConnection, $messagelist[$q], false);
66
67 $from[$q] = $hdr->from;
68 $date[$q] = $hdr->date;
69 $subject[$q] = $hdr->subject;
70 $to[$q] = $hdr->to;
71 $priority[$q] = $hdr->priority;
72 $cc[$q] = $hdr->cc;
73 $size[$q] = $hdr->size;
74 $type[$q] = $hdr->type0;
75 $id[$q] = $messagelist[$q];
76 $flags[$q] = sqimap_get_flags ($imapConnection, $messagelist[$q]);
77 }
78
79 $j = 0;
80 while ($j < count($messagelist)) {
81 $date[$j] = ereg_replace(" ", " ", $date[$j]);
82 $tmpdate = explode(" ", trim($date[$j]));
83
84 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
85 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
86 $messages[$j]["ID"] = $id[$j];
87 $messages[$j]["FROM"] = decodeHeader($from[$j]);
88 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
89 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
90 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
91 $messages[$j]["TO"] = decodeHeader($to[$j]);
92 $messages[$j]["PRIORITY"] = $priority[$j];
93 $messages[$j]["CC"] = $cc[$j];
94 $messages[$j]["SIZE"] = $size[$j];
95 $messages[$j]["TYPE0"] = $type[$j];
96
97 $num = 0;
98 while ($num < count($flags[$j])) {
99 if ($flags[$j][$num] == "Deleted") {
100 $messages[$j]["FLAG_DELETED"] = true;
101 }
102 else if ($flags[$j][$num] == "Answered") {
103 $messages[$j]["FLAG_ANSWERED"] = true;
104 }
105 else if ($flags[$j][$num] == "Seen") {
106 $messages[$j]["FLAG_SEEN"] = true;
107 }
108 else if ($flags[$j][$num] == "Flagged") {
109 $messages[$j]["FLAG_FLAGGED"] = true;
110 }
111 $num++;
112 }
113 $j++;
114 }
115
116 /** Find and remove the ones that are deleted */
117 $i = 0;
118 $j = 0;
119 while ($j < count($messagelist)) {
120 if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) {
121 $j++;
122 continue;
123 }
124 $msgs[$i] = $messages[$j];
125
126 $i++;
127 $j++;
128 }
129 $numMessages = $i;
130
131 // There's gotta be messages in the array for it to sort them.
132
133 if (count($messagelist) > 0) {
134 $j=0;
135 if (!isset ($msg)) { $msg = ""; }
136 mail_message_listing_beginning($imapConnection,
137 "move_messages.php?msg=$msg&mailbox=$urlMailbox&where=".urlencode($search_where)."&what=".urlencode($search_what),
138 '', -1, '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>',
139 '&nbsp;');
140
141
142 while ($j < count($msgs)) {
143 printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, "", 0, $search_where, $search_what);
144 $j++;
145 }
146 echo "</table>";
147 echo "</tr></td></table>";
148 }
149 }
150
151 ?>