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