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