SORT and THREAD handling in search
[squirrelmail.git] / src / addrbook_search.php
... / ...
CommitLineData
1<?php
2
3/**
4 * addrbook_search.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Handle addressbook searching in the popup window.
10 *
11 * NOTE: A lot of this code is similar to the code in
12 * addrbook_search_html.html -- If you change one,
13 * change the other one too!
14 *
15 * $Id$
16 * @package squirrelmail
17 */
18
19/**
20 * Path for SquirrelMail required files.
21 */
22define('SM_PATH','../');
23
24/** SquirrelMail required files. */
25require_once(SM_PATH . 'include/validate.php');
26require_once(SM_PATH . 'functions/strings.php');
27require_once(SM_PATH . 'functions/global.php');
28require_once(SM_PATH . 'functions/html.php');
29
30/** lets get the global vars we may need */
31sqgetGlobalVar('key', $key, SQ_COOKIE);
32sqgetGlobalVar('username', $username, SQ_SESSION);
33sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
34sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
35
36sqgetGlobalVar('show' , $show);
37sqgetGlobalVar('query', $query, SQ_POST);
38sqgetGlobalVar('listall', $listall, SQ_POST);
39sqgetGlobalVar('backend', $backend, SQ_POST);
40
41/**
42 * Function to include JavaScript code
43 * @return void
44 */
45function insert_javascript() {
46 ?>
47 <SCRIPT LANGUAGE="Javascript"><!--
48
49 function to_and_close($addr) {
50 to_address($addr);
51 parent.close();
52 }
53
54 function to_address($addr) {
55 var prefix = "";
56 var pwintype = typeof parent.opener.document.compose;
57
58 $addr = $addr.replace(/ {1,35}$/, "");
59
60 if (pwintype != "undefined") {
61 if (parent.opener.document.compose.send_to.value) {
62 prefix = ", ";
63 parent.opener.document.compose.send_to.value =
64 parent.opener.document.compose.send_to.value + ", " + $addr;
65 } else {
66 parent.opener.document.compose.send_to.value = $addr;
67 }
68 }
69 }
70
71 function cc_address($addr) {
72 var prefix = "";
73 var pwintype = typeof parent.opener.document.compose;
74
75 $addr = $addr.replace(/ {1,35}$/, "");
76
77 if (pwintype != "undefined") {
78 if (parent.opener.document.compose.send_to_cc.value) {
79 prefix = ", ";
80 parent.opener.document.compose.send_to_cc.value =
81 parent.opener.document.compose.send_to_cc.value + ", " + $addr;
82 } else {
83 parent.opener.document.compose.send_to_cc.value = $addr;
84 }
85 }
86 }
87
88 function bcc_address($addr) {
89 var prefix = "";
90 var pwintype = typeof parent.opener.document.compose;
91
92 $addr = $addr.replace(/ {1,35}$/, "");
93
94 if (pwintype != "undefined") {
95 if (parent.opener.document.compose.send_to_bcc.value) {
96 prefix = ", ";
97 parent.opener.document.compose.send_to_bcc.value =
98 parent.opener.document.compose.send_to_bcc.value + ", " + $addr;
99 } else {
100 parent.opener.document.compose.send_to_bcc.value = $addr;
101 }
102 }
103 }
104
105// --></SCRIPT>
106<?php
107} /* End of included JavaScript */
108
109
110/**
111 * List search results
112 * @param array $res Array of search results
113 * @param bool $includesource [Default=true]
114 * @return void
115 */
116function display_result($res, $includesource = true) {
117 global $color;
118
119 if(sizeof($res) <= 0) return;
120
121 insert_javascript();
122
123 $line = 0;
124 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
125 html_tag( 'tr', '', '', $color[9] ) .
126 html_tag( 'th', '&nbsp;', 'left' ) .
127 html_tag( 'th', '&nbsp;' . _("Name"), 'left' ) .
128 html_tag( 'th', '&nbsp;' . _("E-mail"), 'left' ) .
129 html_tag( 'th', '&nbsp;' . _("Info"), 'left' );
130
131 if ($includesource) {
132 echo html_tag( 'th', '&nbsp;' . _("Source"), 'left', 'width="10%"' );
133 }
134 echo "</tr>\n";
135
136 while (list($undef, $row) = each($res)) {
137 $tr_bgcolor = '';
138 $email = htmlspecialchars(addcslashes(AddressBook::full_address($row), "'"), ENT_QUOTES);
139 if ($line % 2) { $tr_bgcolor = $color[0]; }
140 echo html_tag( 'tr', '', '', $tr_bgcolor, 'nowrap' ) .
141 html_tag( 'td',
142 '<small><a href="javascript:to_address(' .
143 "'" . $email . "');\">To</A> | " .
144 '<a href="javascript:cc_address(' .
145 "'" . $email . "');\">Cc</A> | " .
146 '<a href="javascript:bcc_address(' .
147 "'" . $email . "');\">Bcc</A></small>",
148 'center', '', 'valign="top" width="5%" nowrap' ) .
149 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['name']), 'left', '', 'valign="top" nowrap' ) .
150 html_tag( 'td', '&nbsp;' .
151 '<a href="javascript:to_and_close(' .
152 "'" . $email . "');\">" . htmlspecialchars($row['email']) . '</A>'
153 , 'left', '', 'valign="top"' ) .
154 html_tag( 'td', htmlspecialchars($row['label']), 'left', '', 'valign="top" nowrap' );
155 if ($includesource) {
156 echo html_tag( 'td', '&nbsp;' . $row['source'], 'left', '', 'valign="top" nowrap' );
157 }
158
159 echo "</tr>\n";
160 $line++;
161 }
162 echo '</table>';
163}
164
165/* ================= End of functions ================= */
166
167require_once('../functions/strings.php');
168require_once('../functions/addressbook.php');
169
170displayHtmlHeader();
171
172/* Initialize vars */
173if (!isset($query)) { $query = ''; }
174if (!isset($show)) { $show = ''; }
175if (!isset($backend)) { $backend = ''; }
176
177/* Choose correct colors for top and bottom frame */
178if ($show == 'form' && !isset($listall)) {
179 echo '<BODY TEXT="' . $color[6] . '" BGCOLOR="' . $color[3] . '" ' .
180 'LINK="' . $color[6] . '" VLINK="' . $color[6] . '" ' .
181 'ALINK="' . $color[6] . '" ' .
182 'OnLoad="document.sform.query.focus();">';
183} else {
184 echo '<BODY TEXT="' . $color[8] . '" BGCOLOR="' . $color[4] . '" ' .
185 'LINK="' . $color[7] . '" VLINK="' . $color[7] . '" ' .
186 'ALINK="' . $color[7] . "\">\n";
187}
188
189/* Empty search */
190if (empty($query) && empty($show) && empty($listall)) {
191 echo html_tag( 'p', '<br>' .
192 _("No persons matching your search was found"),
193 'center' ) .
194 "\n</BODY></HTML>\n",
195 exit;
196}
197
198/* Initialize addressbook */
199$abook = addressbook_init();
200
201/* Create search form */
202if ($show == 'form' && empty($listall)) {
203 echo '<FORM NAME=sform TARGET=abookres ACTION="addrbook_search.php'.
204 '" METHOD="POST">' . "\n" .
205 html_tag( 'table', '', '', '', 'border="0" width="100%" height="100%"' ) .
206 html_tag( 'tr' ) .
207 html_tag( 'td', ' <strong>' . _("Search for") . "</strong>\n", 'left', '', 'nowrap valign="middle" width="10%"' ) .
208 html_tag( 'td', '', 'left', '', '' ) .
209 '<INPUT TYPE=text NAME=query VALUE="' . htmlspecialchars($query) .
210 "\" SIZE=28>\n";
211
212 /* List all backends to allow the user to choose where to search */
213 if ($abook->numbackends > 1) {
214 echo '<STRONG>' . _("in") . '</STRONG>&nbsp;<SELECT NAME=backend>'."\n".
215 '<OPTION VALUE=-1 SELECTED>' . _("All address books") . "\n";
216 $ret = $abook->get_backend_list();
217 while (list($undef,$v) = each($ret)) {
218 echo '<OPTION VALUE=' . $v->bnum . '>' . $v->sname . "\n";
219 }
220 echo "</SELECT>\n";
221 } else {
222 echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n";
223 }
224
225 echo '</td></tr>' .
226 html_tag( 'tr',
227 html_tag( 'td', '', 'left' ) .
228 html_tag( 'td',
229 '<INPUT TYPE=submit VALUE="' . _("Search") . '" NAME=show>' .
230 '&nbsp;|&nbsp;<INPUT TYPE=submit VALUE="' . _("List all") .
231 '" NAME=listall>' . "\n" .
232 '&nbsp;|&nbsp;<INPUT TYPE=button VALUE="' . _("Close") .
233 '" onclick="parent.close();">' . "\n" ,
234 'left' )
235 ) .
236 '</table></form>' . "\n";
237} else {
238
239 /* Show personal addressbook */
240 if ($show == 'blank' && empty($listall)) {
241
242 if($backend != -1 || $show == 'blank') {
243 if ($show == 'blank') {
244 $backend = $abook->localbackend;
245 }
246 $res = $abook->list_addr($backend);
247
248 if(is_array($res)) {
249 usort($res,'alistcmp');
250 display_result($res, false);
251 } else {
252 echo html_tag( 'p', '<strong>' .
253 sprintf(_("Unable to list addresses from %s"),
254 $abook->backends[$backend]->sname) . '</strong>' ,
255 'center' ) . "\n";
256 }
257 } else {
258 $res = $abook->list_addr();
259 usort($res,'alistcmp');
260 display_result($res, true);
261 }
262
263 } else {
264 if( !empty( $listall ) ){
265 $query = '*';
266 }
267
268 /* Do the search */
269 if (!empty($query)) {
270
271 if($backend == -1) {
272 $res = $abook->s_search($query);
273 } else {
274 $res = $abook->s_search($query, $backend);
275 }
276
277 if (!is_array($res)) {
278 echo html_tag( 'p', '<b><br>' .
279 _("Your search failed with the following error(s)") .
280 ':<br>' . $abook->error . "</b>\n" ,
281 'center' ) .
282 "\n</BODY></HTML>\n";
283 exit;
284 }
285
286 if (sizeof($res) == 0) {
287 echo html_tag( 'p', '<br><b>' .
288 _("No persons matching your search was found") . "</b>\n" ,
289 'center' ) .
290 "\n</BODY></HTML>\n";
291 exit;
292 }
293
294 display_result($res);
295 }
296 }
297
298}
299
300echo "</BODY></HTML>\n";
301
302?>