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