This urlencode(urldecode thing breaks stuff and doesn't add real value.
[squirrelmail.git] / src / addrbook_search.php
CommitLineData
5100704d 1<?php
895905c0 2
35586184 3/**
4 * addrbook_search.php
5 *
76911253 6 * Copyright (c) 1999-2003 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/strings.php');
162require_once('../functions/addressbook.php');
9487c2ff 163
b0c39665 164displayHtmlHeader();
9487c2ff 165
b0c39665 166/* Initialize vars */
167if (!isset($query)) { $query = ''; }
168if (!isset($show)) { $show = ''; }
5a1ae5ca 169if (!isset($backend)) { $backend = ''; }
9487c2ff 170
b0c39665 171/* Choose correct colors for top and bottom frame */
b22c4e16 172if ($show == 'form' && !isset($listall)) {
b0c39665 173 echo '<BODY TEXT="' . $color[6] . '" BGCOLOR="' . $color[3] . '" ' .
174 'LINK="' . $color[6] . '" VLINK="' . $color[6] . '" ' .
175 'ALINK="' . $color[6] . '" ' .
176 'OnLoad="document.sform.query.focus();">';
177} else {
178 echo '<BODY TEXT="' . $color[8] . '" BGCOLOR="' . $color[4] . '" ' .
179 'LINK="' . $color[7] . '" VLINK="' . $color[7] . '" ' .
180 'ALINK="' . $color[7] . "\">\n";
181}
9487c2ff 182
b0c39665 183/* Empty search */
184if (empty($query) && empty($show) && empty($listall)) {
ac987a56 185 echo html_tag( 'p', '<br>' .
186 _("No persons matching your search was found"),
187 'center' ) .
188 "\n</BODY></HTML>\n",
b0c39665 189 exit;
190}
9487c2ff 191
b0c39665 192/* Initialize addressbook */
193$abook = addressbook_init();
9487c2ff 194
b0c39665 195/* Create search form */
0397590e 196if ($show == 'form' && empty($listall)) {
5a1ae5ca 197 echo '<FORM NAME=sform TARGET=abookres ACTION="addrbook_search.php'.
b0c39665 198 '" METHOD="POST">' . "\n" .
ac987a56 199 html_tag( 'table', '', '', '', 'border="0" width="100%" height="100%"' ) .
200 html_tag( 'tr' ) .
201 html_tag( 'td', ' <strong>' . _("Search for") . "</strong>\n", 'left', '', 'nowrap valign="middle" width="10%"' ) .
202 html_tag( 'td', '', 'left', '', '' ) .
203 '<INPUT TYPE=text NAME=query VALUE="' . htmlspecialchars($query) .
204 "\" SIZE=28>\n";
b0c39665 205
206 /* List all backends to allow the user to choose where to search */
207 if ($abook->numbackends > 1) {
208 echo '<STRONG>' . _("in") . '</STRONG>&nbsp;<SELECT NAME=backend>'."\n".
209 '<OPTION VALUE=-1 SELECTED>' . _("All address books") . "\n";
210 $ret = $abook->get_backend_list();
211 while (list($undef,$v) = each($ret)) {
212 echo '<OPTION VALUE=' . $v->bnum . '>' . $v->sname . "\n";
9487c2ff 213 }
b0c39665 214 echo "</SELECT>\n";
215 } else {
216 echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n";
217 }
9487c2ff 218
ac987a56 219 echo '</td></tr>' .
220 html_tag( 'tr',
221 html_tag( 'td', '', 'left' ) .
222 html_tag( 'td',
223 '<INPUT TYPE=submit VALUE="' . _("Search") . '" NAME=show>' .
224 '&nbsp;|&nbsp;<INPUT TYPE=submit VALUE="' . _("List all") .
225 '" NAME=listall>' . "\n" .
226 '&nbsp;|&nbsp;<INPUT TYPE=button VALUE="' . _("Close") .
227 '" onclick="parent.close();">' . "\n" ,
228 'left' )
229 ) .
230 '</table></form>' . "\n";
b0c39665 231} else {
9487c2ff 232
b0c39665 233 /* Show personal addressbook */
db48e3f5 234 if ($show == 'blank' && empty($listall)) {
9487c2ff 235
236 if($backend != -1 || $show == 'blank') {
b0c39665 237 if ($show == 'blank') {
9487c2ff 238 $backend = $abook->localbackend;
b0c39665 239 }
9487c2ff 240 $res = $abook->list_addr($backend);
241
242 if(is_array($res)) {
a10110a5 243 usort($res,'alistcmp');
9487c2ff 244 display_result($res, false);
245 } else {
ac987a56 246 echo html_tag( 'p', '<strong>' .
247 sprintf(_("Unable to list addresses from %s"),
248 $abook->backends[$backend]->sname) . '</strong>' ,
249 'center' ) . "\n";
9487c2ff 250 }
9487c2ff 251 } else {
b0c39665 252 $res = $abook->list_addr();
a10110a5 253 usort($res,'alistcmp');
b0c39665 254 display_result($res, true);
9487c2ff 255 }
256
b0c39665 257 } else {
db48e3f5 258 if( !empty( $listall ) ){
259 $query = '*';
260 }
9487c2ff 261
b0c39665 262 /* Do the search */
db48e3f5 263 if (!empty($query)) {
9487c2ff 264
b0c39665 265 if($backend == -1) {
266 $res = $abook->s_search($query);
267 } else {
268 $res = $abook->s_search($query, $backend);
269 }
9487c2ff 270
b0c39665 271 if (!is_array($res)) {
ac987a56 272 echo html_tag( 'p', '<b><br>' .
273 _("Your search failed with the following error(s)") .
274 ':<br>' . $abook->error . "</b>\n" ,
275 'center' ) .
276 "\n</BODY></HTML>\n";
b0c39665 277 exit;
278 }
9487c2ff 279
b0c39665 280 if (sizeof($res) == 0) {
ac987a56 281 echo html_tag( 'p', '<br><b>' .
282 _("No persons matching your search was found") . "</b>\n" ,
283 'center' ) .
284 "\n</BODY></HTML>\n";
b0c39665 285 exit;
286 }
9487c2ff 287
b0c39665 288 display_result($res);
289 }
9487c2ff 290 }
291
b0c39665 292}
293
294echo "</BODY></HTML>\n";
9487c2ff 295
35586184 296?>