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