Added All messages display
[squirrelmail.git] / functions / imap_search.php
1 <?php
2
3 /**
4 * imap_search.php
5 *
6 * Copyright (c) 1999-2001 The Squirrelmail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * IMAP search routines
10 *
11 * $Id$
12 */
13
14 /*****************************************************************/
15 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
16 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
17 /*** + Base level indent should begin at left margin, as ***/
18 /*** the require_once below. ***/
19 /*** + All identation should consist of four space blocks ***/
20 /*** + Tab characters are evil. ***/
21 /*** + all comments should use "slash-star ... star-slash" ***/
22 /*** style -- no pound characters, no slash-slash style ***/
23 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
24 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
25 /*** + Please use ' instead of ", when possible. Note " ***/
26 /*** should always be used in _( ) function calls. ***/
27 /*** Thank you for your help making the SM code more readable. ***/
28 /*****************************************************************/
29
30 require_once('../functions/imap.php');
31 require_once('../functions/date.php');
32 require_once('../functions/array.php');
33 require_once('../functions/mailbox_display.php');
34 require_once('../functions/mime.php');
35
36
37 function sqimap_search($imapConnection,$search_where,$search_what,$mailbox,$color) {
38
39 global $msgs, $message_highlight_list, $squirrelmail_language, $languages, $index_order;
40
41 $urlMailbox = urlencode($mailbox);
42 $isid = sqimap_session_id();
43
44 /* Construct the Search QuERY */
45 $ss = $isid;
46 if (isset($languages[$squirrelmail_language]['CHARSET']) &&
47 $languages[$squirrelmail_language]['CHARSET']) {
48 $ss .= " SEARCH CHARSET ".$languages[$squirrelmail_language]['CHARSET']." ALL $search_where \"$search_what\"\r\n";
49 } else {
50 $ss .= " SEARCH ALL $search_where \"$search_what\"\r\n";
51 }
52 fputs($imapConnection,$ss);
53
54 # Read Data Back From IMAP
55 $readin = sqimap_read_data ($imapConnection, $isid, false, $result, $message);
56 if (isset($languages[$squirrelmail_language]['CHARSET']) && strtolower($result) == 'no') {
57 $ss = $isid . " SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\"\r\n";
58 fputs ($imapConnection, $ss);
59 $readin = sqimap_read_data ($imapConnection, $isid, true, $result, $message);
60 }
61
62 unset($messagelist); $msgs=""; $c = 0;
63
64 /* Keep going till we find the SEARCH responce */
65 while ($c < count( $readin )) {
66
67 /* Check to see if a SEARCH Responce was recived */
68 if (substr($readin[$c],0,9) == "* SEARCH ")
69 $messagelist = explode(" ",substr($readin[$c],9));
70 else if (isset($errors))
71 $errors = $errors.$readin[$c];
72 else
73 $errors = $readin[$c];
74 $c++;
75 }
76
77 #If nothing is found * SEARCH should be the first error else echo errors
78 if (isset($errors) && strstr($errors,"* SEARCH")) {
79 echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
80 return;
81 } else if (isset($errors)) {
82 echo "<!-- ".$errors." -->";
83 }
84
85 # HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
86 # exsitising code with a search true/false varible.
87
88
89 global $sent_folder;
90 for ($q = 0; $q < count($messagelist); $q++) {
91 $id[$q] = trim($messagelist[$q]);
92 }
93 $issent = ($mailbox == $sent_folder);
94 $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
95 $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
96 foreach ($hdr_list as $hdr) {
97 $from[] = $hdr->from;
98 $date[] = $hdr->date;
99 $subject[] = $hdr->subject;
100 $to[] = $hdr->to;
101 $priority[] = $hdr->priority;
102 $cc[] = $hdr->cc;
103 $size[] = $hdr->size;
104 $type[] = $hdr->type0;
105 }
106
107 $j = 0;
108 while ($j < count($messagelist)) {
109 $date[$j] = ereg_replace(' ', ' ', $date[$j]);
110 $tmpdate = explode(" ", trim($date[$j]));
111
112 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
113 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
114 $messages[$j]["ID"] = $id[$j];
115 $messages[$j]["FROM"] = decodeHeader($from[$j]);
116 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
117 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
118 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
119 $messages[$j]["TO"] = decodeHeader($to[$j]);
120 $messages[$j]["PRIORITY"] = $priority[$j];
121 $messages[$j]["CC"] = $cc[$j];
122 $messages[$j]["SIZE"] = $size[$j];
123 $messages[$j]["TYPE0"] = $type[$j];
124
125 $num = 0;
126 while ($num < count($flags[$j])) {
127 if ($flags[$j][$num] == 'Deleted') {
128 $messages[$j]['FLAG_DELETED'] = true;
129 } else if ($flags[$j][$num] == 'Answered') {
130 $messages[$j]['FLAG_ANSWERED'] = true;
131 } else if ($flags[$j][$num] == 'Seen') {
132 $messages[$j]['FLAG_SEEN'] = true;
133 } else if ($flags[$j][$num] == 'Flagged') {
134 $messages[$j]['FLAG_FLAGGED'] = true;
135 }
136 $num++;
137 }
138 $j++;
139 }
140
141 /** Find and remove the ones that are deleted */
142 $i = 0;
143 $j = 0;
144 while ($j < count($messagelist)) {
145 if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) {
146 $j++;
147 continue;
148 }
149 $msgs[$i] = $messages[$j];
150
151 $i++;
152 $j++;
153 }
154 $numMessages = $i;
155
156 // There's gotta be messages in the array for it to sort them.
157
158 if (count($messagelist) > 0) {
159 $j=0;
160 if (!isset ($msg)) { $msg = ""; }
161 mail_message_listing_beginning($imapConnection,
162 "move_messages.php?msg=$msg&mailbox=$urlMailbox&where=".urlencode($search_where)."&what=".urlencode($search_what),
163 $mailbox, -1, '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>',
164 '&nbsp;');
165
166
167 while ($j < count($msgs)) {
168 printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, "", 0, $search_where, $search_what);
169 $j++;
170 }
171 echo '</table></tr></td></table>';
172 }
173 }
174
175 ?>