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