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