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