fa5fa5705499e324b0c76f6f06fd3bce5398d0c9
[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 /*****************************************************************/
19 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
20 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
21 /*** + Base level indent should begin at left margin, as ***/
22 /*** the require_once below looks. ***/
23 /*** + All identation should consist of four space blocks ***/
24 /*** + Tab characters are evil. ***/
25 /*** + all comments should use "slash-star ... star-slash" ***/
26 /*** style -- no pound characters, no slash-slash style ***/
27 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
28 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
29 /*** + Please use ' instead of ", when possible. Note " ***/
30 /*** should always be used in _( ) function calls. ***/
31 /*** Thank you for your help making the SM code more readable. ***/
32 /*****************************************************************/
33
34 require_once('../src/validate.php');
35
36 /* Function to include JavaScript code */
37 function insert_javascript() {
38 ?>
39 <SCRIPT LANGUAGE="Javascript"><!--
40
41 function to_and_close($addr) {
42 to_address($addr);
43 parent.close();
44 }
45
46 function to_address($addr) {
47 var prefix = "";
48 var pwintype = typeof parent.opener.document.compose;
49
50 $addr = $addr.replace(/ {1,35}$/, "");
51
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 }
61 }
62
63 function cc_address($addr) {
64 var prefix = "";
65 var pwintype = typeof parent.opener.document.compose;
66
67 $addr = $addr.replace(/ {1,35}$/, "");
68
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 }
78 }
79
80 function bcc_address($addr) {
81 var prefix = "";
82 var pwintype = typeof parent.opener.document.compose;
83
84 $addr = $addr.replace(/ {1,35}$/, "");
85
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 }
95 }
96
97 // --></SCRIPT>
98 <?php
99 } /* End of included JavaScript */
100
101
102 /* List search results */
103 function display_result($res, $includesource = true) {
104 global $color;
105
106 if(sizeof($res) <= 0) return;
107
108 insert_javascript();
109
110 $line = 0;
111 echo '<TABLE BORDER="0" WIDTH="98%" ALIGN=center>' .
112 '<TR BGCOLOR="' . $color[9] . '"><TH ALIGN=left>&nbsp;' .
113 '<TH ALIGN=left>&nbsp;' . _("Name") .
114 '<TH ALIGN=left>&nbsp;' . _("E-mail") .
115 '<TH ALIGN=left>&nbsp;' . _("Info");
116
117 if ($includesource) {
118 echo '<TH ALIGN=left WIDTH="10%">&nbsp;' . _("Source");
119 }
120 echo "</TR>\n";
121
122 while (list($undef, $row) = each($res)) {
123 echo '<tr' . (($line % 2) ? " bgcolor=\"$color[0]\"" : '') .
124 ' nowrap><td valign=top nowrap align=center width="5%">' .
125 '<small><a href="javascript:to_address(' .
126 "'" . $row['email'] . "');\">To</A> | " .
127 '<a href="javascript:cc_address(' .
128 "'" . $row["email'] . "');\">Cc</A> | " .
129 '<a href="javascript:bcc_address(' .
130 "'" . $row["email'] . "');\">Bcc</A></small>" .
131 '<td nowrap valign=top>&nbsp;' .
132 $row['name'] . '&nbsp;<td nowrap valign=top>' .
133 '&nbsp;<a href="javascript:to_and_close(' .
134 "'" . $row["email'] . "');\">" . $row["email'] . '</A>&nbsp;' .
135 '<td valign=top>&nbsp;' . $row['label'] . '&nbsp;';
136 if ($includesource) {
137 echo '<td nowrap valign=top>&nbsp;' . $row['source'];
138 }
139
140 echo "</TR>\n";
141 $line++;
142 }
143 echo '</TABLE>';
144 }
145
146 /* ================= End of functions ================= */
147
148 require_once('../functions/array.php');
149 require_once('../functions/strings.php');
150 require_once('../functions/addressbook.php');
151
152 displayHtmlHeader();
153
154 /* Initialize vars */
155 if (!isset($query)) { $query = ''; }
156 if (!isset($show)) { $show = ''; }
157
158 /* Choose correct colors for top and bottom frame */
159 if ($show == 'form') {
160 echo '<BODY TEXT="' . $color[6] . '" BGCOLOR="' . $color[3] . '" ' .
161 'LINK="' . $color[6] . '" VLINK="' . $color[6] . '" ' .
162 'ALINK="' . $color[6] . '" ' .
163 'OnLoad="document.sform.query.focus();">';
164 } else {
165 echo '<BODY TEXT="' . $color[8] . '" BGCOLOR="' . $color[4] . '" ' .
166 'LINK="' . $color[7] . '" VLINK="' . $color[7] . '" ' .
167 'ALINK="' . $color[7] . "\">\n";
168 }
169
170 /* Empty search */
171 if (empty($query) && empty($show) && empty($listall)) {
172 echo '<P ALIGN=center><BR>' .
173 _("No persons matching your search was found") .
174 "</P>\n</BODY></HTML>\n",
175 exit;
176 }
177
178 /* Initialize addressbook */
179 $abook = addressbook_init();
180
181 /* Create search form */
182 if ($show == 'form') {
183 echo '<FORM NAME=sform TARGET=abookres ACTION="' . $PHP_SELF .
184 '" METHOD="POST">' . "\n" .
185 '<TABLE BORDER="0" WIDTH="100%" HEIGHT="100%">' .
186 '<TR><TD NOWRAP VALIGN=middle>' . "\n" .
187 ' <STRONG>' . _("Search for") . "</STRONG>\n" .
188 ' <INPUT TYPE=text NAME=query VALUE="' . htmlspecialchars($query) .
189 "\" SIZE=26>\n",
190
191 /* List all backends to allow the user to choose where to search */
192 if ($abook->numbackends > 1) {
193 echo '<STRONG>' . _("in") . '</STRONG>&nbsp;<SELECT NAME=backend>'."\n".
194 '<OPTION VALUE=-1 SELECTED>' . _("All address books") . "\n";
195 $ret = $abook->get_backend_list();
196 while (list($undef,$v) = each($ret)) {
197 echo '<OPTION VALUE=' . $v->bnum . '>' . $v->sname . "\n";
198 }
199 echo "</SELECT>\n";
200 } else {
201 echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n";
202 }
203
204 echo '<INPUT TYPE=submit VALUE="' . _("Search") . '">' .
205 '&nbsp;|&nbsp;<INPUT TYPE=submit VALUE="' . _("List all") .
206 '" NAME=listall>' . "\n" .
207 '</TD><TD ALIGN=right>' . "\n" .
208 '<INPUT TYPE=button VALUE=" . _("Close window") .
209 '" onclick="parent.close();">' . "\n" .
210 '</TD></TR></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 display_result($res, false);
224 } else {
225 echo '<P ALIGN=center><STRONG>' .
226 sprintf(_("Unable to list addresses from %s"),
227 $abook->backends[$backend]->sname) .
228 '</STRONG></P>' . "\n";
229 }
230 } else {
231 $res = $abook->list_addr();
232 display_result($res, true);
233 }
234
235 } else {
236
237 /* Do the search */
238 if (!empty($query) && empty($listall)) {
239
240 if($backend == -1) {
241 $res = $abook->s_search($query);
242 } else {
243 $res = $abook->s_search($query, $backend);
244 }
245
246 if (!is_array($res)) {
247 echo '<P ALIGN=center><B><BR>' .
248 _("Your search failed with the following error(s)") .
249 ':<br>' . $abook->error . "</B></P>\n</BODY></HTML>\n";
250 exit;
251 }
252
253 if (sizeof($res) == 0) {
254 echo '<P ALIGN=center><BR><B>' .
255 _("No persons matching your search was found") .
256 ".</B></P>\n</BODY></HTML>\n";
257 exit;
258 }
259
260 display_result($res);
261 }
262 }
263
264 }
265
266 echo "</BODY></HTML>\n";
267
268 ?>