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