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