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