updates to make javascript actually behave correctly for real.
[squirrelmail.git] / src / addrbook_search.php
CommitLineData
5100704d 1<?php
895905c0 2
35586184 3/**
4 * addrbook_search.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 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 *
8f6f9ba5 15 * $Id$
16 * @package squirrelmail
35586184 17 */
18
8f6f9ba5 19/**
20 * Path for SquirrelMail required files.
21 */
86725763 22define('SM_PATH','../');
23
8f6f9ba5 24/** SquirrelMail required files. */
08185f2a 25require_once(SM_PATH . 'include/validate.php');
86725763 26require_once(SM_PATH . 'functions/strings.php');
1e12d1ff 27require_once(SM_PATH . 'functions/global.php');
86725763 28require_once(SM_PATH . 'functions/html.php');
f740c049 29
8f6f9ba5 30/** lets get the global vars we may need */
1e12d1ff 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);
0b97a708 40
8f6f9ba5 41/**
42 * Function to include JavaScript code
43 * @return void
44 */
b0c39665 45function insert_javascript() {
46 ?>
47 <SCRIPT LANGUAGE="Javascript"><!--
87332e20 48
b0c39665 49 function to_and_close($addr) {
50 to_address($addr);
51 parent.close();
52 }
5100704d 53
b0c39665 54 function to_address($addr) {
55 var prefix = "";
56 var pwintype = typeof parent.opener.document.compose;
dfadb553 57
b0c39665 58 $addr = $addr.replace(/ {1,35}$/, "");
9487c2ff 59
b0c39665 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 }
5100704d 69 }
5100704d 70
b0c39665 71 function cc_address($addr) {
72 var prefix = "";
73 var pwintype = typeof parent.opener.document.compose;
5100704d 74
b0c39665 75 $addr = $addr.replace(/ {1,35}$/, "");
dfadb553 76
b0c39665 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 }
5100704d 86 }
5100704d 87
b0c39665 88 function bcc_address($addr) {
89 var prefix = "";
90 var pwintype = typeof parent.opener.document.compose;
9487c2ff 91
b0c39665 92 $addr = $addr.replace(/ {1,35}$/, "");
5100704d 93
b0c39665 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 }
5100704d 103 }
5100704d 104
105// --></SCRIPT>
9487c2ff 106<?php
b0c39665 107} /* End of included JavaScript */
2f73dc15 108
109
8f6f9ba5 110/**
111 * List search results
112 * @param array $res Array of search results
113 * @param bool $includesource [Default=true]
114 * @return void
115 */
b0c39665 116function display_result($res, $includesource = true) {
117 global $color;
9487c2ff 118
b0c39665 119 if(sizeof($res) <= 0) return;
9487c2ff 120
b0c39665 121 insert_javascript();
9487c2ff 122
b0c39665 123 $line = 0;
ac987a56 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' );
b0c39665 130
131 if ($includesource) {
ac987a56 132 echo html_tag( 'th', '&nbsp;' . _("Source"), 'left', 'width="10%"' );
b0c39665 133 }
ac987a56 134 echo "</tr>\n";
9487c2ff 135
b0c39665 136 while (list($undef, $row) = each($res)) {
ac987a56 137 $tr_bgcolor = '';
4e160237 138 $email = htmlspecialchars(addcslashes(AddressBook::full_address($row), "'"), ENT_QUOTES);
ac987a56 139 if ($line % 2) { $tr_bgcolor = $color[0]; }
140 echo html_tag( 'tr', '', '', $tr_bgcolor, 'nowrap' ) .
141 html_tag( 'td',
b0c39665 142 '<small><a href="javascript:to_address(' .
3d0cada3 143 "'" . $email . "');\">To</A> | " .
b0c39665 144 '<a href="javascript:cc_address(' .
3d0cada3 145 "'" . $email . "');\">Cc</A> | " .
b0c39665 146 '<a href="javascript:bcc_address(' .
3d0cada3 147 "'" . $email . "');\">Bcc</A></small>",
ac987a56 148 'center', '', 'valign="top" width="5%" nowrap' ) .
4e160237 149 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['name']), 'left', '', 'valign="top" nowrap' ) .
ac987a56 150 html_tag( 'td', '&nbsp;' .
b22c4e16 151 '<a href="javascript:to_and_close(' .
4e160237 152 "'" . $email . "');\">" . htmlspecialchars($row['email']) . '</A>'
ac987a56 153 , 'left', '', 'valign="top"' ) .
4e160237 154 html_tag( 'td', htmlspecialchars($row['label']), 'left', '', 'valign="top" nowrap' );
b0c39665 155 if ($includesource) {
ac987a56 156 echo html_tag( 'td', '&nbsp;' . $row['source'], 'left', '', 'valign="top" nowrap' );
9487c2ff 157 }
b0c39665 158
ac987a56 159 echo "</tr>\n";
b0c39665 160 $line++;
9487c2ff 161 }
ac987a56 162 echo '</table>';
b0c39665 163}
9487c2ff 164
b0c39665 165/* ================= End of functions ================= */
9487c2ff 166
b0c39665 167require_once('../functions/strings.php');
168require_once('../functions/addressbook.php');
9487c2ff 169
b0c39665 170displayHtmlHeader();
9487c2ff 171
b0c39665 172/* Initialize vars */
173if (!isset($query)) { $query = ''; }
174if (!isset($show)) { $show = ''; }
5a1ae5ca 175if (!isset($backend)) { $backend = ''; }
9487c2ff 176
b0c39665 177/* Choose correct colors for top and bottom frame */
b22c4e16 178if ($show == 'form' && !isset($listall)) {
b0c39665 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}
9487c2ff 188
b0c39665 189/* Empty search */
190if (empty($query) && empty($show) && empty($listall)) {
ac987a56 191 echo html_tag( 'p', '<br>' .
192 _("No persons matching your search was found"),
193 'center' ) .
194 "\n</BODY></HTML>\n",
b0c39665 195 exit;
196}
9487c2ff 197
b0c39665 198/* Initialize addressbook */
199$abook = addressbook_init();
9487c2ff 200
b0c39665 201/* Create search form */
0397590e 202if ($show == 'form' && empty($listall)) {
5a1ae5ca 203 echo '<FORM NAME=sform TARGET=abookres ACTION="addrbook_search.php'.
b0c39665 204 '" METHOD="POST">' . "\n" .
ac987a56 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";
b0c39665 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";
9487c2ff 219 }
b0c39665 220 echo "</SELECT>\n";
221 } else {
222 echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n";
223 }
9487c2ff 224
ac987a56 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";
b0c39665 237} else {
9487c2ff 238
b0c39665 239 /* Show personal addressbook */
db48e3f5 240 if ($show == 'blank' && empty($listall)) {
9487c2ff 241
242 if($backend != -1 || $show == 'blank') {
b0c39665 243 if ($show == 'blank') {
9487c2ff 244 $backend = $abook->localbackend;
b0c39665 245 }
9487c2ff 246 $res = $abook->list_addr($backend);
247
248 if(is_array($res)) {
a10110a5 249 usort($res,'alistcmp');
9487c2ff 250 display_result($res, false);
251 } else {
ac987a56 252 echo html_tag( 'p', '<strong>' .
253 sprintf(_("Unable to list addresses from %s"),
254 $abook->backends[$backend]->sname) . '</strong>' ,
255 'center' ) . "\n";
9487c2ff 256 }
9487c2ff 257 } else {
b0c39665 258 $res = $abook->list_addr();
a10110a5 259 usort($res,'alistcmp');
b0c39665 260 display_result($res, true);
9487c2ff 261 }
262
b0c39665 263 } else {
db48e3f5 264 if( !empty( $listall ) ){
265 $query = '*';
266 }
9487c2ff 267
b0c39665 268 /* Do the search */
db48e3f5 269 if (!empty($query)) {
9487c2ff 270
b0c39665 271 if($backend == -1) {
272 $res = $abook->s_search($query);
273 } else {
274 $res = $abook->s_search($query, $backend);
275 }
9487c2ff 276
b0c39665 277 if (!is_array($res)) {
ac987a56 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";
b0c39665 283 exit;
284 }
9487c2ff 285
b0c39665 286 if (sizeof($res) == 0) {
ac987a56 287 echo html_tag( 'p', '<br><b>' .
288 _("No persons matching your search was found") . "</b>\n" ,
289 'center' ) .
290 "\n</BODY></HTML>\n";
b0c39665 291 exit;
292 }
9487c2ff 293
b0c39665 294 display_result($res);
295 }
9487c2ff 296 }
297
b0c39665 298}
299
300echo "</BODY></HTML>\n";
9487c2ff 301
35586184 302?>