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