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