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