Fixed bug in HTML when addressbook is empty.
[squirrelmail.git] / src / addressbook.php
CommitLineData
abdfb4d0 1<?php
2 /**
3 ** addressbook.php
4 **
5 ** Manage personal address book.
6 **
7 **/
8
9 if(!isset($logged_in)) {
10 echo _("You must login first.");
11 exit;
12 }
13 if(!isset($username) || !isset($key)) {
14 echo _("You need a valid user and password to access this page!");
15 exit;
16 }
17
18 if (!isset($config_php))
19 include("../config/config.php");
20 if (!isset($array_php))
21 include("../functions/array.php");
22 if (!isset($strings_php))
23 include("../functions/strings.php");
24 if (!isset($imap_php))
25 include("../functions/imap.php");
26 if (!isset($page_header_php))
27 include("../functions/page_header.php");
28 if (!isset($display_messages_php))
29 include("../functions/display_messages.php");
30 if (!isset($addressbook_php))
31 include("../functions/addressbook.php");
32
33
34 // Sort array by the key "name"
35 function alistcmp($a,$b) {
a82f283d 36 if($a["backend"] > $b["backend"])
37 return 1;
38 else if($a["backend"] < $b["backend"])
39 return -1;
40
abdfb4d0 41 return (strtolower($a["name"]) > strtolower($b["name"])) ? 1 : -1;
42 }
43
44 // Output form to add and modify address data
45 function address_form($name, $submittext, $values = array()) {
46 global $color;
47 print "<TABLE BORDER=0 CELLPADDING=1 COLS=2 WIDTH=\"90%\" ALIGN=center>\n";
48 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
49 _("Nickname"));
50 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
51 "<INPUT NAME=\"%s[nickname]\" SIZE=15 VALUE=\"%s\">".
52 "&nbsp;<SMALL>%s</SMALL></TD></TR>\n",
53 $color[4], $name, htmlspecialchars($values["nickname"]),
54 _("Must be unique"));
55 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
56 _("E-mail address"));
57 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
58 "<INPUT NAME=\"%s[email]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
59 $color[4], $name, htmlspecialchars($values["email"]));
60 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
61 _("First name"));
62 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
63 "<INPUT NAME=\"%s[firstname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
64 $color[4], $name, htmlspecialchars($values["firstname"]));
65 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
66 _("Last name"));
67 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
68 "<INPUT NAME=\"%s[lastname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
69 $color[4], $name, htmlspecialchars($values["lastname"]));
70 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
71 _("Additional info"));
72 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
73 "<INPUT NAME=\"%s[label]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
74 $color[4], $name, htmlspecialchars($values["label"]));
75
76 printf("<TR><TD COLSPAN=2 BGCOLOR=\"%s\" ALIGN=center>\n".
77 "<INPUT TYPE=submit NAME=\"%s[SUBMIT]\" VALUE=\"%s\"></TD></TR>\n",
78 $color[4], $name, $submittext);
79
80 print "</TABLE>\n";
81 }
82
83
84 // IMAP Login
85 $imapConnection = sqimap_login ($username, $key,
86 $imapServerAddress, $imapPort, 10);
87 include("../src/load_prefs.php");
88 sqimap_logout ($imapConnection);
89
90
91 // Open addressbook, with error messages on but without LDAP (the
92 // second "true"). Don't need LDAP here anyway
93 $abook = addressbook_init(true, true);
94 if($abook->localbackend == 0) {
95 plain_error_message(_("No personal address book is defined. Contact administrator."), $color);
96 exit();
97 }
98
99 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
100 print "<HTML><HEAD><TITLE>\n";
101 printf("%s: %s\n", $org_title, _("Address Book"));
102 print "</TITLE></HEAD>\n\n";
103
104 printf('<BODY TEXT="%s" BGCOLOR="%s" LINK="%s" VLINK="%s" ALINK="%s">',
105 $color[8], $color[4], $color[7], $color[7], $color[7]);
106 displayPageHeader($color, "None");
107
108
109 $defdata = array();
110 $formerror = "";
111 $abortform = false;
112 $showaddrlist = true;
a82f283d 113 $defselected = array();
abdfb4d0 114
115
116 // Handle user's actions
117 if($REQUEST_METHOD == "POST") {
118
a82f283d 119 // ***********************************************
120 // Add new address
121 // ***********************************************
b43bdb85 122 if(!empty($addaddr["nickname"])) {
abdfb4d0 123
b43bdb85 124 $r = $abook->add($addaddr, $abook->localbackend);
abdfb4d0 125
126 // Handle error messages
127 if(!$r) {
128 // Remove backend name from error string
129 $errstr = $abook->error;
130 $errstr = ereg_replace("^\[.*\] *", "", $errstr);
131
132 $formerror = $errstr;
133 $showaddrlist = false;
b43bdb85 134 $defdata = $addaddr;
abdfb4d0 135 }
136
a82f283d 137 }
abdfb4d0 138
abdfb4d0 139
a82f283d 140 // ***********************************************
141 // Delete address(es)
142 // ***********************************************
b43bdb85 143 else if((!empty($deladdr)) &&
144 sizeof($sel) > 0) {
145 $orig_sel = $sel;
a82f283d 146 sort($sel);
147
148 // The selected addresses are identidied by "backend:nickname".
149 // Sort the list and process one backend at the time
150 $prevback = -1;
151 $subsel = array();
152 $delfailed = false;
153
154 for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
155 list($sbackend, $snick) = split(":", $sel[$i]);
156
157 // When we get to a new backend, process addresses in
158 // previous one.
159 if($prevback != $sbackend && $prevback != -1) {
160
161 $r = $abook->remove($subsel, $prevback);
162 if(!$r) {
163 $formerror = $abook->error;
164 $i = sizeof($sel);
165 $delfailed = true;
166 break;
167 }
168 $subsel = array();
169 }
170
171 // Queue for processing
172 array_push($subsel, $snick);
173 $prevback = $sbackend;
174 }
175
176 if(!$delfailed) {
177 $r = $abook->remove($subsel, $prevback);
178 if(!$r) { // Handle errors
179 $formerror = $abook->error;
180 $delfailed = true;
181 }
182 }
183
184 if($delfailed) {
185 $showaddrlist = true;
b43bdb85 186 $defselected = $orig_sel;
a82f283d 187 }
abdfb4d0 188 }
189
a82f283d 190
191 // ***********************************************
192 // Update/modify address
193 // ***********************************************
b43bdb85 194 else if(!empty($editaddr)) {
a82f283d 195
196 // Stage one: Copy data into form
b43bdb85 197 if(sizeof($sel) > 0) {
198 if(sizeof($sel) > 1) {
a82f283d 199 $formerror = _("You can only edit one address at the time");
200 $showaddrlist = true;
b43bdb85 201 $defselected = $sel;
a82f283d 202 } else {
203 $abortform = true;
b43bdb85 204 list($ebackend, $enick) = split(":", $sel[0]);
a82f283d 205 $olddata = $abook->lookup($enick, $ebackend);
206
207 // Display the "new address" form
208 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
209 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
210 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
211 print _("Update address");
212 print "<STRONG>\n</TD></TR>\n";
213 print "</TABLE>\n";
214 address_form("editaddr", _("Update address"), $olddata);
215 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
216 htmlspecialchars($olddata["nickname"]));
217 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
218 htmlspecialchars($olddata["backend"]));
219 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
220 print "</FORM>";
221 }
222 }
223
224 // Stage two: Write new data
b43bdb85 225 else if($doedit = 1) {
226 $newdata = $editaddr;
227 $r = $abook->modify($oldnick, $newdata, $backend);
a82f283d 228
229 // Handle error messages
230 if(!$r) {
231 // Display error
232 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
233 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
234 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": ".
235 $abook->error."</FONT>";
236 print "<STRONG>\n</TD></TR>\n";
237 print "</TABLE>\n";
238
239 // Display the "new address" form again
240 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
241 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
242 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
243 print _("Update address");
244 print "<STRONG>\n</TD></TR>\n";
245 print "</TABLE>\n";
246 address_form("editaddr", _("Update address"), $newdata);
247 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
248 htmlspecialchars($newdata["nickname"]));
249 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
250 htmlspecialchars($newdata["backend"]));
251 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
252 print "</FORM>";
253
254 $abortform = true;
255 }
256 }
257
258 // Should not get here...
259 else {
260 plain_error_message(_("Unknown error"), $color);
261 $abortform = true;
262 }
263 } // End of edit address
264
265
266
abdfb4d0 267 // Some times we end output before forms are printed
268 if($abortform) {
269 print "</BODY></HTML>\n";
270 exit();
271 }
abdfb4d0 272 }
273
a82f283d 274
abdfb4d0 275 // ===================================================================
276 // The following is only executed on a GET request, or on a POST when
277 // a user is added, or when "delete" or "modify" was successful.
278 // ===================================================================
279
280 // Display error messages
281 if(!empty($formerror)) {
282 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
283 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
284 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": $formerror</FONT>";
285 print "<STRONG>\n</TD></TR>\n";
286 print "</TABLE>\n";
287 }
288
a82f283d 289
abdfb4d0 290 // Display the address management part
291 if($showaddrlist) {
292 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
293
abdfb4d0 294 // Get and sort address list
295 $alist = $abook->list_addr();
296 usort($alist,'alistcmp');
a82f283d 297 $prevbackend = -1;
6ed96f54 298 $headerprinted = false;
a82f283d 299
300 // List addresses
abdfb4d0 301 while(list($key,$row) = each($alist)) {
a82f283d 302
303 // New table header for each backend
304 if($prevbackend != $row["backend"]) {
305 if($prevbackend >= 0) {
306 print "<TR><TD COLSPAN=5 ALIGN=center>";
307 print "&nbsp;<BR></TD></TR></TABLE>\n";
308 }
309
310 print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n";
311 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
312 print $row["source"];
313 print "<STRONG>\n</TD></TR>\n";
314 print "</TABLE>\n";
315
316 print "<TABLE COLS=5 BORDER=0 CELLPADDING=1 CELLSPACING=0 ".
317 "WIDTH=\"90%\" ALIGN=center>";
318 printf("<tr bgcolor=\"$color[9]\"><TH align=left width=\"3%%\">".
319 "&nbsp;<TH align=left width=\"10%%\">%s<TH align=left>%s".
320 "<TH align=left>%s<TH align=left>%s</TR>\n",
321 _("Nickname"), _("Name"), _("E-mail"), _("Info"));
322 $line = 0;
6ed96f54 323 $headerprinted = true;
a82f283d 324 } // End of header
325
326 $prevbackend = $row["backend"];
327
328 // Check if this user is selected
329 if(in_array($row["backend"].":".$row["nickname"], $defselected))
330 $selected = "CHECKED";
331 else
332 $selected = "";
333
334 // Print one row
335 printf("<TR%s NOWRAP>\n <TD align=center><SMALL>".
336 "<INPUT TYPE=checkbox %s NAME=\"sel[]\" VALUE=\"%d:%s\">".
337 "</SMALL><TD NOWRAP>&nbsp;%s&nbsp;<TD NOWRAP>&nbsp;%s&nbsp;".
338 "<TD NOWRAP>&nbsp;<A HREF=\"compose.php?send_to=%s\">%s</A>".
339 "&nbsp;<TD NOWRAP>&nbsp;%s</TR>\n",
340 ($line % 2) ? " bgcolor=\"$color[0]\"" : "",
341 $selected, $row["backend"], $row["nickname"],
342 $row["nickname"], $row["name"],
343 rawurlencode($row["email"]), $row["email"], $row["label"]);
abdfb4d0 344 $line++;
a82f283d 345 }
346
347 // End of list. Close table.
6ed96f54 348 if($headerprinted) {
349 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
350 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
351 _("Edit selected"));
352 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
353 _("Delete selected"));
354 print "</TR></TABLE></FORM>";
355 }
a82f283d 356 } // end of addresslist
357
abdfb4d0 358
359 // Display the "new address" form
360 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
361 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
362 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
a82f283d 363 printf(_("Add to %s"), $abook->localbackendname);
abdfb4d0 364 print "<STRONG>\n</TD></TR>\n";
365 print "</TABLE>\n";
366 address_form("addaddr", _("Add address"), $defdata);
367 print "</FORM>";
368
369?>
370
371</BODY></HTML>