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