Rollback
[squirrelmail.git] / src / addrbook_search.php
CommitLineData
5100704d 1<?php
895905c0 2
35586184 3/**
4 * addrbook_search.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 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 */
17
35586184 18require_once('../src/validate.php');
46d38f78 19require_once('../functions/strings.php');
f740c049 20
b0c39665 21/* Function to include JavaScript code */
22function insert_javascript() {
23 ?>
24 <SCRIPT LANGUAGE="Javascript"><!--
87332e20 25
b0c39665 26 function to_and_close($addr) {
27 to_address($addr);
28 parent.close();
29 }
5100704d 30
b0c39665 31 function to_address($addr) {
32 var prefix = "";
33 var pwintype = typeof parent.opener.document.compose;
dfadb553 34
b0c39665 35 $addr = $addr.replace(/ {1,35}$/, "");
9487c2ff 36
b0c39665 37 if (pwintype != "undefined") {
38 if (parent.opener.document.compose.send_to.value) {
39 prefix = ", ";
40 parent.opener.document.compose.send_to.value =
41 parent.opener.document.compose.send_to.value + ", " + $addr;
42 } else {
43 parent.opener.document.compose.send_to.value = $addr;
44 }
45 }
5100704d 46 }
5100704d 47
b0c39665 48 function cc_address($addr) {
49 var prefix = "";
50 var pwintype = typeof parent.opener.document.compose;
5100704d 51
b0c39665 52 $addr = $addr.replace(/ {1,35}$/, "");
dfadb553 53
b0c39665 54 if (pwintype != "undefined") {
55 if (parent.opener.document.compose.send_to_cc.value) {
56 prefix = ", ";
57 parent.opener.document.compose.send_to_cc.value =
58 parent.opener.document.compose.send_to_cc.value + ", " + $addr;
59 } else {
60 parent.opener.document.compose.send_to_cc.value = $addr;
61 }
62 }
5100704d 63 }
5100704d 64
b0c39665 65 function bcc_address($addr) {
66 var prefix = "";
67 var pwintype = typeof parent.opener.document.compose;
9487c2ff 68
b0c39665 69 $addr = $addr.replace(/ {1,35}$/, "");
5100704d 70
b0c39665 71 if (pwintype != "undefined") {
72 if (parent.opener.document.compose.send_to_bcc.value) {
73 prefix = ", ";
74 parent.opener.document.compose.send_to_bcc.value =
75 parent.opener.document.compose.send_to_bcc.value + ", " + $addr;
76 } else {
77 parent.opener.document.compose.send_to_bcc.value = $addr;
78 }
79 }
5100704d 80 }
5100704d 81
82// --></SCRIPT>
9487c2ff 83<?php
b0c39665 84} /* End of included JavaScript */
2f73dc15 85
86
b0c39665 87/* List search results */
88function display_result($res, $includesource = true) {
89 global $color;
9487c2ff 90
b0c39665 91 if(sizeof($res) <= 0) return;
9487c2ff 92
b0c39665 93 insert_javascript();
9487c2ff 94
b0c39665 95 $line = 0;
96 echo '<TABLE BORDER="0" WIDTH="98%" ALIGN=center>' .
97 '<TR BGCOLOR="' . $color[9] . '"><TH ALIGN=left>&nbsp;' .
98 '<TH ALIGN=left>&nbsp;' . _("Name") .
99 '<TH ALIGN=left>&nbsp;' . _("E-mail") .
100 '<TH ALIGN=left>&nbsp;' . _("Info");
101
102 if ($includesource) {
103 echo '<TH ALIGN=left WIDTH="10%">&nbsp;' . _("Source");
104 }
105 echo "</TR>\n";
9487c2ff 106
b0c39665 107 while (list($undef, $row) = each($res)) {
ef97102b 108 echo '<tr';
00a065a6 109 if ($line % 2) { echo ' bgcolor="' . $color[0] . '"'; }
ef97102b 110 echo ' nowrap><td valign=top nowrap align=center width="5%">' .
b0c39665 111 '<small><a href="javascript:to_address(' .
112 "'" . $row['email'] . "');\">To</A> | " .
113 '<a href="javascript:cc_address(' .
ef97102b 114 "'" . $row['email'] . "');\">Cc</A> | " .
b0c39665 115 '<a href="javascript:bcc_address(' .
ef97102b 116 "'" . $row['email'] . "');\">Bcc</A></small>" .
b0c39665 117 '<td nowrap valign=top>&nbsp;' .
b22c4e16 118 $row['name'] . '<td valign=top>' .
119 '<a href="javascript:to_and_close(' .
120 "'" . $row['email'] . "');\">" . $row['email'] . '</A>' .
121 '<td valign=top nowrap>' . $row['label'];
b0c39665 122 if ($includesource) {
123 echo '<td nowrap valign=top>&nbsp;' . $row['source'];
9487c2ff 124 }
b0c39665 125
126 echo "</TR>\n";
127 $line++;
9487c2ff 128 }
b0c39665 129 echo '</TABLE>';
130}
9487c2ff 131
b0c39665 132/* ================= End of functions ================= */
9487c2ff 133
b0c39665 134require_once('../functions/array.php');
135require_once('../functions/strings.php');
136require_once('../functions/addressbook.php');
9487c2ff 137
b0c39665 138displayHtmlHeader();
9487c2ff 139
b0c39665 140/* Initialize vars */
141if (!isset($query)) { $query = ''; }
142if (!isset($show)) { $show = ''; }
5a1ae5ca 143if (!isset($backend)) { $backend = ''; }
9487c2ff 144
b0c39665 145/* Choose correct colors for top and bottom frame */
b22c4e16 146if ($show == 'form' && !isset($listall)) {
b0c39665 147 echo '<BODY TEXT="' . $color[6] . '" BGCOLOR="' . $color[3] . '" ' .
148 'LINK="' . $color[6] . '" VLINK="' . $color[6] . '" ' .
149 'ALINK="' . $color[6] . '" ' .
150 'OnLoad="document.sform.query.focus();">';
151} else {
152 echo '<BODY TEXT="' . $color[8] . '" BGCOLOR="' . $color[4] . '" ' .
153 'LINK="' . $color[7] . '" VLINK="' . $color[7] . '" ' .
154 'ALINK="' . $color[7] . "\">\n";
155}
9487c2ff 156
b0c39665 157/* Empty search */
158if (empty($query) && empty($show) && empty($listall)) {
159 echo '<P ALIGN=center><BR>' .
160 _("No persons matching your search was found") .
161 "</P>\n</BODY></HTML>\n",
162 exit;
163}
9487c2ff 164
b0c39665 165/* Initialize addressbook */
166$abook = addressbook_init();
9487c2ff 167
b0c39665 168/* Create search form */
0397590e 169if ($show == 'form' && empty($listall)) {
5a1ae5ca 170 echo '<FORM NAME=sform TARGET=abookres ACTION="addrbook_search.php'.
b0c39665 171 '" METHOD="POST">' . "\n" .
172 '<TABLE BORDER="0" WIDTH="100%" HEIGHT="100%">' .
b22c4e16 173 '<TR><TD NOWRAP VALIGN=middle align=left width=10%>' . "\n" .
b0c39665 174 ' <STRONG>' . _("Search for") . "</STRONG>\n" .
b22c4e16 175 ' </TD><TD align=left><INPUT TYPE=text NAME=query VALUE="' . htmlspecialchars($query) .
176 "\" SIZE=28>\n";
b0c39665 177
178 /* List all backends to allow the user to choose where to search */
179 if ($abook->numbackends > 1) {
180 echo '<STRONG>' . _("in") . '</STRONG>&nbsp;<SELECT NAME=backend>'."\n".
181 '<OPTION VALUE=-1 SELECTED>' . _("All address books") . "\n";
182 $ret = $abook->get_backend_list();
183 while (list($undef,$v) = each($ret)) {
184 echo '<OPTION VALUE=' . $v->bnum . '>' . $v->sname . "\n";
9487c2ff 185 }
b0c39665 186 echo "</SELECT>\n";
187 } else {
188 echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n";
189 }
9487c2ff 190
b22c4e16 191 echo '</TD></TR><TR><TD></TD><TD align=left>'.
7e235a1a 192 '<INPUT TYPE=submit VALUE="' . _("Search") . '" NAME=show>' .
b0c39665 193 '&nbsp;|&nbsp;<INPUT TYPE=submit VALUE="' . _("List all") .
194 '" NAME=listall>' . "\n" .
b22c4e16 195 '&nbsp;|&nbsp;<INPUT TYPE=button VALUE="' . _("Close") .
b0c39665 196 '" onclick="parent.close();">' . "\n" .
197 '</TD></TR></TABLE></FORM>' . "\n";
198} else {
9487c2ff 199
b0c39665 200 /* Show personal addressbook */
db48e3f5 201 if ($show == 'blank' && empty($listall)) {
9487c2ff 202
203 if($backend != -1 || $show == 'blank') {
b0c39665 204 if ($show == 'blank') {
9487c2ff 205 $backend = $abook->localbackend;
b0c39665 206 }
9487c2ff 207 $res = $abook->list_addr($backend);
208
209 if(is_array($res)) {
a10110a5 210 usort($res,'alistcmp');
9487c2ff 211 display_result($res, false);
212 } else {
b0c39665 213 echo '<P ALIGN=center><STRONG>' .
214 sprintf(_("Unable to list addresses from %s"),
215 $abook->backends[$backend]->sname) .
216 '</STRONG></P>' . "\n";
9487c2ff 217 }
9487c2ff 218 } else {
b0c39665 219 $res = $abook->list_addr();
a10110a5 220 usort($res,'alistcmp');
b0c39665 221 display_result($res, true);
9487c2ff 222 }
223
b0c39665 224 } else {
db48e3f5 225 if( !empty( $listall ) ){
226 $query = '*';
227 }
9487c2ff 228
b0c39665 229 /* Do the search */
db48e3f5 230 if (!empty($query)) {
9487c2ff 231
b0c39665 232 if($backend == -1) {
233 $res = $abook->s_search($query);
234 } else {
235 $res = $abook->s_search($query, $backend);
236 }
9487c2ff 237
b0c39665 238 if (!is_array($res)) {
239 echo '<P ALIGN=center><B><BR>' .
240 _("Your search failed with the following error(s)") .
241 ':<br>' . $abook->error . "</B></P>\n</BODY></HTML>\n";
242 exit;
243 }
9487c2ff 244
b0c39665 245 if (sizeof($res) == 0) {
246 echo '<P ALIGN=center><BR><B>' .
247 _("No persons matching your search was found") .
248 ".</B></P>\n</BODY></HTML>\n";
249 exit;
250 }
9487c2ff 251
b0c39665 252 display_result($res);
253 }
9487c2ff 254 }
255
b0c39665 256}
257
258echo "</BODY></HTML>\n";
9487c2ff 259
35586184 260?>