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