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