5100704d |
1 | <?php |
895905c0 |
2 | |
35586184 |
3 | /** |
4 | * addrbook_search.php |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 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 | |
35586184 |
18 | require_once('../src/validate.php'); |
f740c049 |
19 | |
b0c39665 |
20 | /* Function to include JavaScript code */ |
21 | function insert_javascript() { |
22 | ?> |
23 | <SCRIPT LANGUAGE="Javascript"><!-- |
87332e20 |
24 | |
b0c39665 |
25 | function to_and_close($addr) { |
26 | to_address($addr); |
27 | parent.close(); |
28 | } |
5100704d |
29 | |
b0c39665 |
30 | function to_address($addr) { |
31 | var prefix = ""; |
32 | var pwintype = typeof parent.opener.document.compose; |
dfadb553 |
33 | |
b0c39665 |
34 | $addr = $addr.replace(/ {1,35}$/, ""); |
9487c2ff |
35 | |
b0c39665 |
36 | if (pwintype != "undefined") { |
37 | if (parent.opener.document.compose.send_to.value) { |
38 | prefix = ", "; |
39 | parent.opener.document.compose.send_to.value = |
40 | parent.opener.document.compose.send_to.value + ", " + $addr; |
41 | } else { |
42 | parent.opener.document.compose.send_to.value = $addr; |
43 | } |
44 | } |
5100704d |
45 | } |
5100704d |
46 | |
b0c39665 |
47 | function cc_address($addr) { |
48 | var prefix = ""; |
49 | var pwintype = typeof parent.opener.document.compose; |
5100704d |
50 | |
b0c39665 |
51 | $addr = $addr.replace(/ {1,35}$/, ""); |
dfadb553 |
52 | |
b0c39665 |
53 | if (pwintype != "undefined") { |
54 | if (parent.opener.document.compose.send_to_cc.value) { |
55 | prefix = ", "; |
56 | parent.opener.document.compose.send_to_cc.value = |
57 | parent.opener.document.compose.send_to_cc.value + ", " + $addr; |
58 | } else { |
59 | parent.opener.document.compose.send_to_cc.value = $addr; |
60 | } |
61 | } |
5100704d |
62 | } |
5100704d |
63 | |
b0c39665 |
64 | function bcc_address($addr) { |
65 | var prefix = ""; |
66 | var pwintype = typeof parent.opener.document.compose; |
9487c2ff |
67 | |
b0c39665 |
68 | $addr = $addr.replace(/ {1,35}$/, ""); |
5100704d |
69 | |
b0c39665 |
70 | if (pwintype != "undefined") { |
71 | if (parent.opener.document.compose.send_to_bcc.value) { |
72 | prefix = ", "; |
73 | parent.opener.document.compose.send_to_bcc.value = |
74 | parent.opener.document.compose.send_to_bcc.value + ", " + $addr; |
75 | } else { |
76 | parent.opener.document.compose.send_to_bcc.value = $addr; |
77 | } |
78 | } |
5100704d |
79 | } |
5100704d |
80 | |
81 | // --></SCRIPT> |
9487c2ff |
82 | <?php |
b0c39665 |
83 | } /* End of included JavaScript */ |
2f73dc15 |
84 | |
85 | |
b0c39665 |
86 | /* List search results */ |
87 | function display_result($res, $includesource = true) { |
88 | global $color; |
9487c2ff |
89 | |
b0c39665 |
90 | if(sizeof($res) <= 0) return; |
9487c2ff |
91 | |
b0c39665 |
92 | insert_javascript(); |
9487c2ff |
93 | |
b0c39665 |
94 | $line = 0; |
95 | echo '<TABLE BORDER="0" WIDTH="98%" ALIGN=center>' . |
96 | '<TR BGCOLOR="' . $color[9] . '"><TH ALIGN=left> ' . |
97 | '<TH ALIGN=left> ' . _("Name") . |
98 | '<TH ALIGN=left> ' . _("E-mail") . |
99 | '<TH ALIGN=left> ' . _("Info"); |
100 | |
101 | if ($includesource) { |
102 | echo '<TH ALIGN=left WIDTH="10%"> ' . _("Source"); |
103 | } |
104 | echo "</TR>\n"; |
9487c2ff |
105 | |
b0c39665 |
106 | while (list($undef, $row) = each($res)) { |
ef97102b |
107 | echo '<tr'; |
00a065a6 |
108 | if ($line % 2) { echo ' bgcolor="' . $color[0] . '"'; } |
ef97102b |
109 | echo ' nowrap><td valign=top nowrap align=center width="5%">' . |
b0c39665 |
110 | '<small><a href="javascript:to_address(' . |
111 | "'" . $row['email'] . "');\">To</A> | " . |
112 | '<a href="javascript:cc_address(' . |
ef97102b |
113 | "'" . $row['email'] . "');\">Cc</A> | " . |
b0c39665 |
114 | '<a href="javascript:bcc_address(' . |
ef97102b |
115 | "'" . $row['email'] . "');\">Bcc</A></small>" . |
b0c39665 |
116 | '<td nowrap valign=top> ' . |
117 | $row['name'] . ' <td nowrap valign=top>' . |
118 | ' <a href="javascript:to_and_close(' . |
ef97102b |
119 | "'" . $row['email'] . "');\">" . $row['email'] . '</A> ' . |
b0c39665 |
120 | '<td valign=top> ' . $row['label'] . ' '; |
121 | if ($includesource) { |
122 | echo '<td nowrap valign=top> ' . $row['source']; |
9487c2ff |
123 | } |
b0c39665 |
124 | |
125 | echo "</TR>\n"; |
126 | $line++; |
9487c2ff |
127 | } |
b0c39665 |
128 | echo '</TABLE>'; |
129 | } |
9487c2ff |
130 | |
b0c39665 |
131 | /* ================= End of functions ================= */ |
9487c2ff |
132 | |
b0c39665 |
133 | require_once('../functions/array.php'); |
134 | require_once('../functions/strings.php'); |
135 | require_once('../functions/addressbook.php'); |
9487c2ff |
136 | |
b0c39665 |
137 | displayHtmlHeader(); |
9487c2ff |
138 | |
b0c39665 |
139 | /* Initialize vars */ |
140 | if (!isset($query)) { $query = ''; } |
141 | if (!isset($show)) { $show = ''; } |
9487c2ff |
142 | |
b0c39665 |
143 | /* Choose correct colors for top and bottom frame */ |
144 | if ($show == 'form') { |
145 | echo '<BODY TEXT="' . $color[6] . '" BGCOLOR="' . $color[3] . '" ' . |
146 | 'LINK="' . $color[6] . '" VLINK="' . $color[6] . '" ' . |
147 | 'ALINK="' . $color[6] . '" ' . |
148 | 'OnLoad="document.sform.query.focus();">'; |
149 | } else { |
150 | echo '<BODY TEXT="' . $color[8] . '" BGCOLOR="' . $color[4] . '" ' . |
151 | 'LINK="' . $color[7] . '" VLINK="' . $color[7] . '" ' . |
152 | 'ALINK="' . $color[7] . "\">\n"; |
153 | } |
9487c2ff |
154 | |
b0c39665 |
155 | /* Empty search */ |
156 | if (empty($query) && empty($show) && empty($listall)) { |
157 | echo '<P ALIGN=center><BR>' . |
158 | _("No persons matching your search was found") . |
159 | "</P>\n</BODY></HTML>\n", |
160 | exit; |
161 | } |
9487c2ff |
162 | |
b0c39665 |
163 | /* Initialize addressbook */ |
164 | $abook = addressbook_init(); |
9487c2ff |
165 | |
b0c39665 |
166 | /* Create search form */ |
167 | if ($show == 'form') { |
168 | echo '<FORM NAME=sform TARGET=abookres ACTION="' . $PHP_SELF . |
169 | '" METHOD="POST">' . "\n" . |
170 | '<TABLE BORDER="0" WIDTH="100%" HEIGHT="100%">' . |
171 | '<TR><TD NOWRAP VALIGN=middle>' . "\n" . |
172 | ' <STRONG>' . _("Search for") . "</STRONG>\n" . |
173 | ' <INPUT TYPE=text NAME=query VALUE="' . htmlspecialchars($query) . |
ef97102b |
174 | "\" SIZE=26>\n"; |
b0c39665 |
175 | |
176 | /* List all backends to allow the user to choose where to search */ |
177 | if ($abook->numbackends > 1) { |
178 | echo '<STRONG>' . _("in") . '</STRONG> <SELECT NAME=backend>'."\n". |
179 | '<OPTION VALUE=-1 SELECTED>' . _("All address books") . "\n"; |
180 | $ret = $abook->get_backend_list(); |
181 | while (list($undef,$v) = each($ret)) { |
182 | echo '<OPTION VALUE=' . $v->bnum . '>' . $v->sname . "\n"; |
9487c2ff |
183 | } |
b0c39665 |
184 | echo "</SELECT>\n"; |
185 | } else { |
186 | echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n"; |
187 | } |
9487c2ff |
188 | |
b0c39665 |
189 | echo '<INPUT TYPE=submit VALUE="' . _("Search") . '">' . |
190 | ' | <INPUT TYPE=submit VALUE="' . _("List all") . |
191 | '" NAME=listall>' . "\n" . |
192 | '</TD><TD ALIGN=right>' . "\n" . |
ef97102b |
193 | '<INPUT TYPE=button VALUE="' . _("Close window") . |
b0c39665 |
194 | '" onclick="parent.close();">' . "\n" . |
195 | '</TD></TR></TABLE></FORM>' . "\n"; |
196 | } else { |
9487c2ff |
197 | |
b0c39665 |
198 | /* Show personal addressbook */ |
199 | if ($show == 'blank' || !empty($listall)) { |
9487c2ff |
200 | |
201 | if($backend != -1 || $show == 'blank') { |
b0c39665 |
202 | if ($show == 'blank') { |
9487c2ff |
203 | $backend = $abook->localbackend; |
b0c39665 |
204 | } |
9487c2ff |
205 | $res = $abook->list_addr($backend); |
206 | |
207 | if(is_array($res)) { |
a10110a5 |
208 | usort($res,'alistcmp'); |
9487c2ff |
209 | display_result($res, false); |
210 | } else { |
b0c39665 |
211 | echo '<P ALIGN=center><STRONG>' . |
212 | sprintf(_("Unable to list addresses from %s"), |
213 | $abook->backends[$backend]->sname) . |
214 | '</STRONG></P>' . "\n"; |
9487c2ff |
215 | } |
9487c2ff |
216 | } else { |
b0c39665 |
217 | $res = $abook->list_addr(); |
a10110a5 |
218 | usort($res,'alistcmp'); |
b0c39665 |
219 | display_result($res, true); |
9487c2ff |
220 | } |
221 | |
b0c39665 |
222 | } else { |
9487c2ff |
223 | |
b0c39665 |
224 | /* Do the search */ |
225 | if (!empty($query) && empty($listall)) { |
9487c2ff |
226 | |
b0c39665 |
227 | if($backend == -1) { |
228 | $res = $abook->s_search($query); |
229 | } else { |
230 | $res = $abook->s_search($query, $backend); |
231 | } |
9487c2ff |
232 | |
b0c39665 |
233 | if (!is_array($res)) { |
234 | echo '<P ALIGN=center><B><BR>' . |
235 | _("Your search failed with the following error(s)") . |
236 | ':<br>' . $abook->error . "</B></P>\n</BODY></HTML>\n"; |
237 | exit; |
238 | } |
9487c2ff |
239 | |
b0c39665 |
240 | if (sizeof($res) == 0) { |
241 | echo '<P ALIGN=center><BR><B>' . |
242 | _("No persons matching your search was found") . |
243 | ".</B></P>\n</BODY></HTML>\n"; |
244 | exit; |
245 | } |
9487c2ff |
246 | |
b0c39665 |
247 | display_result($res); |
248 | } |
9487c2ff |
249 | } |
250 | |
b0c39665 |
251 | } |
252 | |
253 | echo "</BODY></HTML>\n"; |
9487c2ff |
254 | |
35586184 |
255 | ?> |