Removed html formating from address book backend classes. Added
[squirrelmail.git] / src / addrbook_search.php
CommitLineData
5100704d 1<?php
895905c0 2
35586184 3/**
4 * addrbook_search.php
5 *
35586184 6 * Handle addressbook searching in the popup window.
7 *
8 * NOTE: A lot of this code is similar to the code in
9 * addrbook_search_html.html -- If you change one,
10 * change the other one too!
11 *
47ccfad4 12 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
91e0dccc 14 * @version $Id$
8f6f9ba5 15 * @package squirrelmail
de00443c 16 * @subpackage addressbook
35586184 17 */
18
8f6f9ba5 19/**
202bcbcc 20 * Include the SquirrelMail initialization file.
8f6f9ba5 21 */
202bcbcc 22require('../include/init.php');
86725763 23
6027422c 24include_once(SM_PATH . 'functions/forms.php');
25include_once(SM_PATH . 'functions/addressbook.php');
0b97a708 26
8f6f9ba5 27/**
28 * Function to include JavaScript code
29 * @return void
30 */
b0c39665 31function insert_javascript() {
32 ?>
2c92ea9d 33 <script type="text/javascript"><!--
87332e20 34
b0c39665 35 function to_and_close($addr) {
36 to_address($addr);
37 parent.close();
38 }
5100704d 39
b0c39665 40 function to_address($addr) {
41 var prefix = "";
42 var pwintype = typeof parent.opener.document.compose;
dfadb553 43
b0c39665 44 $addr = $addr.replace(/ {1,35}$/, "");
9487c2ff 45
b0c39665 46 if (pwintype != "undefined") {
47 if (parent.opener.document.compose.send_to.value) {
48 prefix = ", ";
49 parent.opener.document.compose.send_to.value =
50 parent.opener.document.compose.send_to.value + ", " + $addr;
51 } else {
52 parent.opener.document.compose.send_to.value = $addr;
53 }
54 }
5100704d 55 }
5100704d 56
b0c39665 57 function cc_address($addr) {
58 var prefix = "";
59 var pwintype = typeof parent.opener.document.compose;
5100704d 60
b0c39665 61 $addr = $addr.replace(/ {1,35}$/, "");
dfadb553 62
b0c39665 63 if (pwintype != "undefined") {
64 if (parent.opener.document.compose.send_to_cc.value) {
65 prefix = ", ";
66 parent.opener.document.compose.send_to_cc.value =
67 parent.opener.document.compose.send_to_cc.value + ", " + $addr;
68 } else {
69 parent.opener.document.compose.send_to_cc.value = $addr;
70 }
71 }
5100704d 72 }
5100704d 73
b0c39665 74 function bcc_address($addr) {
75 var prefix = "";
76 var pwintype = typeof parent.opener.document.compose;
9487c2ff 77
b0c39665 78 $addr = $addr.replace(/ {1,35}$/, "");
5100704d 79
b0c39665 80 if (pwintype != "undefined") {
81 if (parent.opener.document.compose.send_to_bcc.value) {
82 prefix = ", ";
83 parent.opener.document.compose.send_to_bcc.value =
84 parent.opener.document.compose.send_to_bcc.value + ", " + $addr;
85 } else {
86 parent.opener.document.compose.send_to_bcc.value = $addr;
87 }
88 }
5100704d 89 }
5100704d 90
6fd95361 91// --></script>
9487c2ff 92<?php
b0c39665 93} /* End of included JavaScript */
2f73dc15 94
95
8f6f9ba5 96/**
97 * List search results
98 * @param array $res Array of search results
99 * @param bool $includesource [Default=true]
100 * @return void
101 */
b0c39665 102function display_result($res, $includesource = true) {
103 global $color;
91e0dccc 104
b0c39665 105 if(sizeof($res) <= 0) return;
91e0dccc 106
b0c39665 107 insert_javascript();
91e0dccc 108
b0c39665 109 $line = 0;
ac987a56 110 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
111 html_tag( 'tr', '', '', $color[9] ) .
112 html_tag( 'th', '&nbsp;', 'left' ) .
113 html_tag( 'th', '&nbsp;' . _("Name"), 'left' ) .
114 html_tag( 'th', '&nbsp;' . _("E-mail"), 'left' ) .
115 html_tag( 'th', '&nbsp;' . _("Info"), 'left' );
b0c39665 116
117 if ($includesource) {
bcc04ed0 118 echo html_tag( 'th', '&nbsp;' . _("Source"), 'left', '', 'width="10%"' );
91e0dccc 119 }
ac987a56 120 echo "</tr>\n";
91e0dccc 121
b0c39665 122 while (list($undef, $row) = each($res)) {
4e160237 123 $email = htmlspecialchars(addcslashes(AddressBook::full_address($row), "'"), ENT_QUOTES);
91e0dccc 124 if ($line % 2) {
93adc018 125 $tr_bgcolor = $color[12];
126 } else {
127 $tr_bgcolor = $color[4];
128 }
c435f076 129 echo html_tag( 'tr', '', '', $tr_bgcolor, 'style="white-space: nowrap;"' ) .
ac987a56 130 html_tag( 'td',
91e0dccc 131 '<small><a href="javascript:to_address(' .
c7b99f3b 132 "'" . $email . "');\">"._("To")."</a> | " .
91e0dccc 133 '<a href="javascript:cc_address(' .
c7b99f3b 134 "'" . $email . "');\">"._("Cc")."</a> | " .
91e0dccc 135 '<a href="javascript:bcc_address(' .
c7b99f3b 136 "'" . $email . "');\">"._("Bcc")."</a></small>",
c435f076 137 'center', '', 'valign="top" width="5%" style="white-space: nowrap;"' ) .
138 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['name']), 'left', '', 'valign="top" style="white-space: nowrap;"' ) .
ac987a56 139 html_tag( 'td', '&nbsp;' .
b22c4e16 140 '<a href="javascript:to_and_close(' .
39bfea8f 141 "'" . $email . "');\">" . htmlspecialchars($row['email']) . '</a>'
ac987a56 142 , 'left', '', 'valign="top"' ) .
c435f076 143 html_tag( 'td', htmlspecialchars($row['label']), 'left', '', 'valign="top" style="white-space: nowrap;"' );
b0c39665 144 if ($includesource) {
c435f076 145 echo html_tag( 'td', '&nbsp;' . $row['source'], 'left', '', 'valign="top" style="white-space: 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 ================= */
91e0dccc 155
6027422c 156/** lets get the global vars we may need */
157sqgetGlobalVar('key', $key, SQ_COOKIE);
158sqgetGlobalVar('username', $username, SQ_SESSION);
159sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
160sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
161
162if (! sqgetGlobalVar('show' , $show)) {
163 $show = '';
164}
165if (! sqgetGlobalVar('query', $query, SQ_POST)) {
166 $query = '';
167}
168if (! sqgetGlobalVar('listall', $listall, SQ_POST)) {
169 unset($listall);
170}
171if (! sqgetGlobalVar('backend', $backend, SQ_POST)) {
172 $backend = '';
173}
91e0dccc 174
b0c39665 175displayHtmlHeader();
91e0dccc 176
40262494 177/** set correct value of $default_charset */
178global $default_charset;
179set_my_charset();
180
b0c39665 181/* Choose correct colors for top and bottom frame */
b22c4e16 182if ($show == 'form' && !isset($listall)) {
39bfea8f 183 echo '<body text="' . $color[6] . '" bgcolor="' . $color[3] . '" ' .
184 'link="' . $color[6] . '" vlink="' . $color[6] . '" ' .
185 'alink="' . $color[6] . '" ' .
b0c39665 186 'OnLoad="document.sform.query.focus();">';
187} else {
39bfea8f 188 echo '<body text="' . $color[8] . '" bgcolor="' . $color[4] . '" ' .
189 'link="' . $color[7] . '" vlink="' . $color[7] . '" ' .
190 'alink="' . $color[7] . "\">\n";
b0c39665 191}
9487c2ff 192
b0c39665 193/* Empty search */
6027422c 194if (empty($query) && empty($show) && !isset($listall)) {
39bfea8f 195 echo html_tag( 'p', '<br />' .
62366261 196 _("No persons matching your search were found"),
ac987a56 197 'center' ) .
716df0e3 198 "\n</body></html>\n";
b0c39665 199 exit;
200}
9487c2ff 201
6dda6453 202/* Initialize addressbook, show init errors only in bottom frame */
203$showerr=($show=='form' ? false : true);
204$abook = addressbook_init($showerr);
9487c2ff 205
6027422c 206/* Create search form (top frame) */
207if ($show == 'form' && ! isset($listall)) {
91e0dccc 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' ) .
c435f076 212 html_tag( 'td', ' <strong>' . _("Search for") . "</strong>\n", 'left', '', 'style="white-space: nowrap;" valign="middle" width="10%"' ) .
ac987a56 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) {
51698ad9 218 echo '<strong>' . _("in") . '</strong>&nbsp;'."\n";
219 $selopts = array();
220 $selopts['-1'] = _("All address books");
134e4174 221
b0c39665 222 $ret = $abook->get_backend_list();
223 while (list($undef,$v) = each($ret)) {
62366261 224 $selopts[$v->bnum] = $v->sname;
9487c2ff 225 }
134e4174 226 echo addSelect('backend', $selopts, '-1', TRUE);
b0c39665 227 } else {
62366261 228 echo addHidden('backend', '-1');
b0c39665 229 }
91e0dccc 230
ac987a56 231 echo '</td></tr>' .
232 html_tag( 'tr',
233 html_tag( 'td', '', 'left' ) .
234 html_tag( 'td',
39bfea8f 235 '<input type="submit" value="' . _("Search") . '" name="show" />' .
236 '&nbsp;|&nbsp;<input type="submit" value="' . _("List all") .
237 '" name="listall" />' . "\n" .
238 '&nbsp;|&nbsp;<input type="button" value="' . _("Close") .
239 '" onclick="parent.close();" />' . "\n" ,
ac987a56 240 'left' )
241 ) .
242 '</table></form>' . "\n";
b0c39665 243} else {
6027422c 244 /**
245 * List addresses (bottom frame)
246 * If listall is set, list all entries in selected backend.
247 * If $show is 'blank' (initial call of address book popup) - list
248 * personal address book.
249 */
250 if ($show == 'blank' || isset($listall)) {
9487c2ff 251
252 if($backend != -1 || $show == 'blank') {
b0c39665 253 if ($show == 'blank') {
9487c2ff 254 $backend = $abook->localbackend;
b0c39665 255 }
9487c2ff 256 $res = $abook->list_addr($backend);
257
258 if(is_array($res)) {
a10110a5 259 usort($res,'alistcmp');
9487c2ff 260 display_result($res, false);
261 } else {
ac987a56 262 echo html_tag( 'p', '<strong>' .
6027422c 263 sprintf(_("Unable to list addresses from %s"),
264 $abook->backends[$backend]->sname) . '</strong>' ,
265 'center' ) . "\n";
9487c2ff 266 }
9487c2ff 267 } else {
b0c39665 268 $res = $abook->list_addr();
a10110a5 269 usort($res,'alistcmp');
b0c39665 270 display_result($res, true);
9487c2ff 271 }
272
6027422c 273 } elseif (!empty($query)) {
274 /* Do the search (listall is not set. query is set.)*/
91e0dccc 275
6027422c 276 if($backend == -1) {
277 $res = $abook->s_search($query);
278 } else {
279 $res = $abook->s_search($query, $backend);
280 }
91e0dccc 281
6027422c 282 if (!is_array($res)) {
283 echo html_tag( 'p', '<b><br />' .
284 _("Your search failed with the following error(s)") .
35235328 285 ':<br />' . nl2br(htmlspecialchars($abook->error)) . "</b>\n" ,
766ad164 286 'center' );
287 } elseif (sizeof($res) == 0) {
6027422c 288 echo html_tag( 'p', '<br /><b>' .
289 _("No persons matching your search were found") . "</b>\n" ,
766ad164 290 'center' );
291 } else {
292 display_result($res);
b0c39665 293 }
6027422c 294 } else {
295 /**
296 * listall is not set, query is not set or empty.
297 * User hit search button without entering search expression.
298 */
299 echo html_tag( 'p', '<br /><b>' . _("Nothing to search") . "</b>\n",'center' );
300 }
b0c39665 301}
5c4ff7bf 302$oTemplate->display('footer.tpl');
303?>