* To reduce on errors, I moved the includes for config.php and strings.php
[squirrelmail.git] / src / addrbook_search.php
1 <?php
2 /**
3 ** addrbook_search.php
4 **
5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
8 ** Handle addressbook searching in the popup window.
9 **
10 ** NOTE: A lot of this code is similar to the code in
11 ** addrbook_search_html.html -- If you change one,
12 ** change the other one too!
13 **
14 ** $Id$
15 **/
16
17 include("../src/validate.php");
18
19 // Function to include JavaScript code
20 function insert_javascript() {
21 ?>
22 <SCRIPT LANGUAGE="Javascript"><!--
23
24 function to_and_close($addr) {
25 to_address($addr);
26 parent.close();
27 }
28
29 function to_address($addr) {
30 var prefix = "";
31 var pwintype = typeof parent.opener.document.compose;
32
33 $addr = $addr.replace(/ {1,35}$/, "");
34
35 if(pwintype != "undefined" ) {
36 if ( parent.opener.document.compose.send_to.value ) {
37 prefix = ", ";
38 parent.opener.document.compose.send_to.value =
39 parent.opener.document.compose.send_to.value + ", " + $addr;
40
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
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 print '<TABLE BORDER="0" WIDTH="98%" ALIGN=center>';
97 printf("<TR BGCOLOR=\"$color[9]\"><TH ALIGN=left>&nbsp;".
98 "<TH ALIGN=left>&nbsp;%s<TH ALIGN=left>&nbsp;%s".
99 "<TH ALIGN=left>&nbsp;%s",
100 _("Name"), _("E-mail"), _("Info"));
101
102 if($includesource)
103 printf("<TH ALIGN=left WIDTH=\"10%%\">&nbsp;%s", _("Source"));
104
105 print "</TR>\n";
106
107 while(list($undef, $row) = each($res)) {
108 printf("<tr%s nowrap><td valign=top nowrap align=center width=\"5%%\">".
109 "<small><a href=\"javascript:to_address('%s');\">To</A> | ".
110 "<a href=\"javascript:cc_address('%s');\">Cc</A> | ".
111 "<a href=\"javascript:bcc_address('%s');\">Bcc</A></small>".
112 "<td nowrap valign=top>&nbsp;%s&nbsp;<td nowrap valign=top>".
113 "&nbsp;<a href=\"javascript:to_and_close('%s');\">%s</A>&nbsp;".
114 "<td valign=top>&nbsp;%s&nbsp;",
115 ($line % 2) ? " bgcolor=\"$color[0]\"" : "",
116 $row["email"], $row["email"], $row["email"],
117 $row["name"], $row["email"], $row["email"],
118 $row["label"]);
119
120 if($includesource)
121 printf("<td nowrap valign=top>&nbsp;%s", $row["source"]);
122
123 print "</TR>\n";
124 $line++;
125 }
126 print '</TABLE>';
127 }
128
129 /* ================= End of functions ================= */
130
131 include('../functions/array.php');
132 include('../functions/strings.php');
133 include('../functions/page_header.php');
134 include('../functions/addressbook.php');
135 include('../src/load_prefs.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 BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" ";
146 echo "LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\" ";
147 echo 'OnLoad="document.sform.query.focus();">';
148 } else {
149 echo "<BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" ";
150 echo "LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
151 }
152
153 // Empty search
154 if(empty($query) && empty($show) && empty($listall)) {
155 printf("<P ALIGN=center><BR>%s</P>\n</BODY></HTML>\n",
156 _("No persons matching your search was found"));
157 exit;
158 }
159
160 // Initialize addressbook
161 $abook = addressbook_init();
162
163 // Create search form
164 if($show == 'form') {
165 printf("<FORM NAME=sform TARGET=abookres ACTION=\"%s\" METHOD=\"POST\">\n",
166 $PHP_SELF);
167 print('<TABLE BORDER="0" WIDTH="100%" HEIGHT="100%">');
168 print("<TR><TD NOWRAP VALIGN=middle>\n");
169 printf(" <STRONG>%s</STRONG>\n", _("Search for"));
170 printf(" <INPUT TYPE=text NAME=query VALUE=\"%s\" SIZE=26>\n",
171 htmlspecialchars($query));
172
173 // List all backends to allow the user to choose where to search
174 if($abook->numbackends > 1) {
175 printf("<STRONG>%s</STRONG>&nbsp;<SELECT NAME=backend>\n",
176 _("in"));
177 printf("<OPTION VALUE=-1 SELECTED>%s\n",
178 _("All address books"));
179 $ret = $abook->get_backend_list();
180 while(list($undef,$v) = each($ret))
181 printf("<OPTION VALUE=%d>%s\n", $v->bnum, $v->sname);
182 print "</SELECT>\n";
183 } else {
184 print "<INPUT TYPE=hidden NAME=backend VALUE=-1>\n";
185 }
186
187 printf("<INPUT TYPE=submit VALUE=\"%s\">",
188 _("Search"));
189 printf("&nbsp;|&nbsp;<INPUT TYPE=submit VALUE=\"%s\" NAME=listall>\n",
190 _("List all"));
191 print "</TD><TD ALIGN=right>\n";
192 printf("<INPUT TYPE=button VALUE=\"%s\" onclick=\"parent.close();\">\n",
193 _("Close window"));
194 print "</TD></TR></TABLE></FORM>\n";
195 } else
196
197 // Show personal addressbook
198 if($show == 'blank' || !empty($listall)) {
199
200 if($backend != -1 || $show == 'blank') {
201 if($show == 'blank')
202 $backend = $abook->localbackend;
203
204 //printf("<H3 ALIGN=center>%s</H3>\n", $abook->backends[$backend]->sname);
205
206 $res = $abook->list_addr($backend);
207
208 if(is_array($res)) {
209 display_result($res, false);
210 } else {
211 printf("<P ALIGN=center><STRONG>"._("Unable to list addresses from %s").
212 "</STRONG></P>\n", $abook->backends[$backend]->sname);
213 }
214
215 } else {
216 $res = $abook->list_addr();
217 display_result($res, true);
218 }
219
220 } else
221
222 // Do the search
223 if(!empty($query) && empty($listall)) {
224
225 if($backend == -1) {
226 $res = $abook->s_search($query);
227 } else {
228 $res = $abook->s_search($query, $backend);
229 }
230
231 if(!is_array($res)) {
232 printf("<P ALIGN=center><B><BR>%s:<br>%s</B></P>\n</BODY></HTML>\n",
233 _("Your search failed with the following error(s)"),
234 $abook->error);
235 exit;
236 }
237
238 if(sizeof($res) == 0) {
239 printf("<P ALIGN=center><BR><B>%s.</B></P>\n</BODY></HTML>\n",
240 _("No persons matching your search was found"));
241 exit;
242 }
243
244 display_result($res);
245 }
246 ?>
247
248 </BODY></HTML>