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