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