added a macosx friendly search function.
[squirrelmail.git] / src / addrbook_search_html.php
1 <?php
2
3 /**
4 * addrbook_search_html.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Handle addressbook searching with pure html.
10 *
11 * This file is included from compose.php
12 *
13 * NOTE: A lot of this code is similar to the code in
14 * addrbook_search.html -- If you change one, change
15 * the other one too!
16 *
17 * $Id$
18 */
19
20 require_once('../src/validate.php');
21 require_once('../functions/date.php');
22 require_once('../functions/smtp.php');
23 require_once('../functions/display_messages.php');
24 require_once('../functions/addressbook.php');
25 require_once('../functions/plugin.php');
26 require_once('../functions/strings.php');
27
28 /* Insert hidden data */
29 function addr_insert_hidden() {
30 global $body, $subject, $send_to, $send_to_cc, $send_to_bcc, $mailbox,
31 $identity, $session;
32
33 echo '<input type=hidden value="';
34 if (substr($body, 0, 1) == "\r") {
35 echo "\n";
36 }
37 echo htmlspecialchars($body) . '" name=body>' . "\n" .
38 '<input type=hidden value="' . $session . '" name=session>' . "\n" .
39 '<input type=hidden value="' . htmlspecialchars($subject) .
40 '" name=subject>' . "\n" .
41 '<input type=hidden value="' . htmlspecialchars($send_to) .
42 '" name=send_to>' . "\n" .
43 '<input type=hidden value="' . htmlspecialchars($send_to_cc) .
44 '" name=send_to_cc>' . "\n" .
45 '<input type=hidden value="' . htmlspecialchars($send_to_bcc) .
46 '" name=send_to_bcc>' . "\n" .
47 '<input type=hidden value="' . htmlspecialchars($identity) .
48 '" name=identity>' . "\n" .
49 '<input type=hidden name=mailbox value="' . htmlspecialchars($mailbox) .
50 "\">\n" . '<input type=hidden value="true" name=from_htmladdr_search>' .
51 "\n";
52 }
53
54
55 /* List search results */
56 function addr_display_result($res, $includesource = true) {
57 global $color, $javascript_on, $PHP_SELF;
58
59 if (sizeof($res) <= 0) return;
60
61 echo '<form method=post action="' . $PHP_SELF . '" name="addrbook">'."\n" .
62 '<input type=hidden name="html_addr_search_done" value="true">' . "\n";
63 addr_insert_hidden();
64 $line = 0;
65
66 if ($javascript_on) {
67 print
68 '<script language="JavaScript" type="text/javascript">' .
69 "\n<!-- \n" .
70 "function CheckAll(ch) {\n" .
71 " for (var i = 0; i < document.addrbook.elements.length; i++) {\n" .
72 " if( document.addrbook.elements[i].type == 'checkbox' &&\n" .
73 " document.addrbook.elements[i].name.substr(0,16) == 'send_to_search['+ch ) {\n" .
74 " document.addrbook.elements[i].checked = !(document.addrbook.elements[i].checked);\n".
75 " }\n" .
76 " }\n" .
77 "}\n" .
78 "//-->\n" .
79 "</script>\n";
80 $chk_all = '<a href="#" onClick="CheckAll(\'T\');">' . _("All") . '</a>&nbsp;<font color="'.$color[9].'">To</font>'.
81 '&nbsp;&nbsp;'.
82 '<a href="#" onClick="CheckAll(\'C\');">' . _("All") . '</a>&nbsp;<font color="'.$color[9].'">Cc</font>'.
83 '&nbsp;&nbsp;'.
84 '<a href="#" onClick="CheckAll(\'B\');">' . _("All") . '</a>';
85 }
86 echo '<TABLE BORDER="0" WIDTH="98%" ALIGN="center">' .
87 '<TR BGCOLOR="' . $color[9] . '"><TH ALIGN="left">&nbsp;' . $chk_all . '</TH>' .
88 '<TH ALIGN="left">&nbsp;' . _("Name") . '</TH>' .
89 '<TH ALIGN="left">&nbsp;' . _("E-mail") . '</TH>' .
90 '<TH ALIGN="left">&nbsp;' . _("Info") . '</TH>';
91
92
93
94 if ($includesource) {
95 echo '<TH ALIGN=left WIDTH="10%">&nbsp;' . _("Source"). '</TH>';
96 }
97
98 echo "</TR>\n";
99
100 foreach ($res as $row) {
101 echo '<tr';
102 if ($line % 2) { echo ' bgcolor="' . $color[0] . '"'; }
103 echo ' nowrap><td nowrap align=center width="5%">' .
104 '<input type=checkbox name="send_to_search[T' . $line . ']" value = "' .
105 htmlspecialchars($row['email']) . '">&nbsp;' . _("To") . '&nbsp;' .
106 '<input type=checkbox name="send_to_search[C' . $line . ']" value = "' .
107 htmlspecialchars($row['email']) . '">&nbsp;' . _("Cc") . '&nbsp;' .
108 '<input type=checkbox name="send_to_search[B' . $line . ']" value = "' .
109 htmlspecialchars($row['email']) . '">&nbsp;' . _("Bcc") . '&nbsp;' .
110 '</td><td nowrap>&nbsp;' . $row['name'] . '&nbsp;</td>' .
111 '<td nowrap>&nbsp;' . $row['email'] . '&nbsp;</td>' .
112 '<td nowrap>&nbsp;' . $row['label'] . '&nbsp;</td>';
113 if ($includesource) {
114 echo '<td nowrap>&nbsp;' . $row['source'] . '&nbsp;</td>';
115 }
116 echo "</tr>\n";
117 $line ++;
118 }
119 echo '<TR><TD ALIGN=center COLSPAN=';
120 if ($includesource) { echo '5'; } else { echo '4'; }
121 echo '><INPUT TYPE=submit NAME="addr_search_done" VALUE="' .
122 _("Use Addresses") . '"></TD></TR>' .
123 '</TABLE>' .
124 '<INPUT TYPE=hidden VALUE=1 NAME="html_addr_search_done">' .
125 '</FORM>';
126 }
127
128 /* --- End functions --- */
129
130 global $mailbox;
131 if ($compose_new_win == '1') {
132 compose_Header($color, $mailbox);
133 }
134 else {
135 displayPageHeader($color, $mailbox);
136 }
137 /* Initialize addressbook */
138 $abook = addressbook_init();
139
140 ?>
141
142 <br>
143 <table width="95%" align=center cellpadding=2 cellspacing=2 border=0>
144 <tr><td bgcolor="<?php echo $color[0] ?>">
145 <center><b><?php echo _("Address Book Search") ?></b></center>
146 </td></tr></table>
147
148 <?php
149
150 /* Search form */
151 echo "<CENTER>\n<TABLE BORDER=0><TR><TD NOWRAP VALIGN=middle>\n" .
152 '<FORM METHOD=post NAME=f ACTION="' . $PHP_SELF .
153 '?html_addr_search=true">' . "\n<CENTER>\n" .
154 ' <nobr><STRONG>' . _("Search for") . "</STRONG>\n";
155 addr_insert_hidden();
156 if (! isset($addrquery))
157 $addrquery = '';
158 echo ' <INPUT TYPE=text NAME=addrquery VALUE="' .
159 htmlspecialchars($addrquery) . "\" SIZE=26>\n";
160
161 /* List all backends to allow the user to choose where to search */
162 if (!isset($backend)) { $backend = ''; }
163 if ($abook->numbackends > 1) {
164 echo '<STRONG>' . _("in") . '</STRONG>&nbsp;<SELECT NAME=backend>' . "\n" .
165 '<OPTION VALUE=-1';
166 if ($backend == -1) { echo ' SELECTED'; }
167 echo '>' . _("All address books") . "\n";
168 $ret = $abook->get_backend_list();
169 while (list($undef,$v) = each($ret)) {
170 echo '<OPTION VALUE=' . $v->bnum;
171 if ($backend == $v->bnum) { echo ' SELECTED'; }
172 echo '>' . $v->sname . "\n";
173 }
174 echo "</SELECT>\n";
175 } else {
176 echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n";
177 }
178 if (isset($session)) {
179 echo "<input type=hidden name=\"session\" value=\"$session\">";
180 }
181
182 echo '<INPUT TYPE=submit VALUE="' . _("Search") . '">' .
183 '&nbsp;|&nbsp;<INPUT TYPE=submit VALUE="' . _("List all") .
184 '" NAME=listall>' . "\n" .
185 '</FORM></center></TD></TR></TABLE>' . "\n";
186 addr_insert_hidden();
187 echo '</CENTER>';
188 do_hook('addrbook_html_search_below');
189 /* End search form */
190
191 /* Show personal addressbook */
192
193 if ( !empty( $listall ) ){
194 $addrquery = '*';
195 }
196
197 if ($addrquery == '' && empty($listall)) {
198
199 if (! isset($backend) || $backend != -1 || $addrquery == '') {
200 if ($addrquery == '') {
201 $backend = $abook->localbackend;
202 }
203
204 /* echo '<H3 ALIGN=center>' . $abook->backends[$backend]->sname) . "</H3>\n"; */
205
206 $res = $abook->list_addr($backend);
207
208 if (is_array($res)) {
209 usort($res,'alistcmp');
210 addr_display_result($res, false);
211 } else {
212 echo '<P ALIGN=center><STRONG>' .
213 sprintf(_("Unable to list addresses from %s"),
214 $abook->backends[$backend]->sname) .
215 "</STRONG></P>\n";
216 }
217
218 } else {
219 $res = $abook->list_addr();
220 usort($res,'alistcmp');
221 addr_display_result($res, true);
222 }
223 exit;
224 }
225 else {
226
227 /* Do the search */
228 if (!empty($addrquery)) {
229
230 if ($backend == -1) {
231 $res = $abook->s_search($addrquery);
232 } else {
233 $res = $abook->s_search($addrquery, $backend);
234 }
235
236 if (!is_array($res)) {
237 echo '<P ALIGN=center><B><BR>' .
238 _("Your search failed with the following error(s)") . ':<br>' .
239 $abook->error . "</B></P>\n</BODY></HTML>\n";
240 } else {
241 if (sizeof($res) == 0) {
242 echo '<P ALIGN=center><BR><B>' .
243 _("No persons matching your search was found") .
244 ".</B></P>\n</BODY></HTML>\n";
245 } else {
246 addr_display_result($res);
247 }
248 }
249 }
250 }
251
252 if ($addrquery == '' || sizeof($res) == 0) {
253 /* printf('<center><FORM METHOD=post NAME=k ACTION="compose.php">'."\n", $PHP_SELF); */
254 echo '<center><FORM METHOD=post NAME=k ACTION="compose.php">' . "\n";
255 addr_insert_hidden();
256 echo '<INPUT TYPE=submit VALUE="' . _("Return") . '" NAME=return>' . "\n" .
257 '</form></center></nobr>';
258 }
259
260 ?>
261 </body></html>