Address management (delete & modify) complete.
[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 // ***********************************************
abdfb4d0 122 $add_data = $HTTP_POST_VARS["addaddr"];
123 if(!empty($add_data["nickname"])) {
124
125 $r = $abook->add($add_data, $abook->localbackend);
126
127 // Handle error messages
128 if(!$r) {
129 // Remove backend name from error string
130 $errstr = $abook->error;
131 $errstr = ereg_replace("^\[.*\] *", "", $errstr);
132
133 $formerror = $errstr;
134 $showaddrlist = false;
135 $defdata = $add_data;
136 }
137
a82f283d 138 }
abdfb4d0 139
abdfb4d0 140
a82f283d 141 // ***********************************************
142 // Delete address(es)
143 // ***********************************************
144 else if((!empty($HTTP_POST_VARS["deladdr"])) &&
abdfb4d0 145 sizeof($HTTP_POST_VARS["sel"]) > 0) {
a82f283d 146
147 $sel = $HTTP_POST_VARS["sel"];
148 sort($sel);
149
150 // The selected addresses are identidied by "backend:nickname".
151 // Sort the list and process one backend at the time
152 $prevback = -1;
153 $subsel = array();
154 $delfailed = false;
155
156 for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
157 list($sbackend, $snick) = split(":", $sel[$i]);
158
159 // When we get to a new backend, process addresses in
160 // previous one.
161 if($prevback != $sbackend && $prevback != -1) {
162
163 $r = $abook->remove($subsel, $prevback);
164 if(!$r) {
165 $formerror = $abook->error;
166 $i = sizeof($sel);
167 $delfailed = true;
168 break;
169 }
170 $subsel = array();
171 }
172
173 // Queue for processing
174 array_push($subsel, $snick);
175 $prevback = $sbackend;
176 }
177
178 if(!$delfailed) {
179 $r = $abook->remove($subsel, $prevback);
180 if(!$r) { // Handle errors
181 $formerror = $abook->error;
182 $delfailed = true;
183 }
184 }
185
186 if($delfailed) {
187 $showaddrlist = true;
188 $defselected = $HTTP_POST_VARS["sel"];
189 }
abdfb4d0 190 }
191
a82f283d 192
193 // ***********************************************
194 // Update/modify address
195 // ***********************************************
196 else if(!empty($HTTP_POST_VARS["editaddr"])) {
197
198 // Stage one: Copy data into form
199 if(sizeof($HTTP_POST_VARS["sel"]) > 0) {
200 if(sizeof($HTTP_POST_VARS["sel"]) > 1) {
201 $formerror = _("You can only edit one address at the time");
202 $showaddrlist = true;
203 $defselected = $HTTP_POST_VARS["sel"];
204 } else {
205 $abortform = true;
206 list($ebackend, $enick) = split(":", $HTTP_POST_VARS["sel"][0]);
207 $olddata = $abook->lookup($enick, $ebackend);
208
209 // Display the "new address" form
210 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
211 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
212 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
213 print _("Update address");
214 print "<STRONG>\n</TD></TR>\n";
215 print "</TABLE>\n";
216 address_form("editaddr", _("Update address"), $olddata);
217 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
218 htmlspecialchars($olddata["nickname"]));
219 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
220 htmlspecialchars($olddata["backend"]));
221 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
222 print "</FORM>";
223 }
224 }
225
226 // Stage two: Write new data
227 else if($HTTP_POST_VARS["doedit"] = 1) {
228 $newdata = $HTTP_POST_VARS["editaddr"];
229 $r = $abook->modify($HTTP_POST_VARS["oldnick"],
230 $newdata,
231 $HTTP_POST_VARS["backend"]);
232
233 // Handle error messages
234 if(!$r) {
235 // Display error
236 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
237 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
238 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": ".
239 $abook->error."</FONT>";
240 print "<STRONG>\n</TD></TR>\n";
241 print "</TABLE>\n";
242
243 // Display the "new address" form again
244 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
245 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
246 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
247 print _("Update address");
248 print "<STRONG>\n</TD></TR>\n";
249 print "</TABLE>\n";
250 address_form("editaddr", _("Update address"), $newdata);
251 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
252 htmlspecialchars($newdata["nickname"]));
253 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
254 htmlspecialchars($newdata["backend"]));
255 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
256 print "</FORM>";
257
258 $abortform = true;
259 }
260 }
261
262 // Should not get here...
263 else {
264 plain_error_message(_("Unknown error"), $color);
265 $abortform = true;
266 }
267 } // End of edit address
268
269
270
abdfb4d0 271 // Some times we end output before forms are printed
272 if($abortform) {
273 print "</BODY></HTML>\n";
274 exit();
275 }
abdfb4d0 276 }
277
a82f283d 278
abdfb4d0 279 // ===================================================================
280 // The following is only executed on a GET request, or on a POST when
281 // a user is added, or when "delete" or "modify" was successful.
282 // ===================================================================
283
284 // Display error messages
285 if(!empty($formerror)) {
286 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
287 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
288 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": $formerror</FONT>";
289 print "<STRONG>\n</TD></TR>\n";
290 print "</TABLE>\n";
291 }
292
a82f283d 293
abdfb4d0 294 // Display the address management part
295 if($showaddrlist) {
296 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
297
abdfb4d0 298 // Get and sort address list
299 $alist = $abook->list_addr();
300 usort($alist,'alistcmp');
a82f283d 301 $prevbackend = -1;
302
303 // List addresses
abdfb4d0 304 while(list($key,$row) = each($alist)) {
a82f283d 305
306 // New table header for each backend
307 if($prevbackend != $row["backend"]) {
308 if($prevbackend >= 0) {
309 print "<TR><TD COLSPAN=5 ALIGN=center>";
310 print "&nbsp;<BR></TD></TR></TABLE>\n";
311 }
312
313 print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n";
314 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
315 print $row["source"];
316 print "<STRONG>\n</TD></TR>\n";
317 print "</TABLE>\n";
318
319 print "<TABLE COLS=5 BORDER=0 CELLPADDING=1 CELLSPACING=0 ".
320 "WIDTH=\"90%\" ALIGN=center>";
321 printf("<tr bgcolor=\"$color[9]\"><TH align=left width=\"3%%\">".
322 "&nbsp;<TH align=left width=\"10%%\">%s<TH align=left>%s".
323 "<TH align=left>%s<TH align=left>%s</TR>\n",
324 _("Nickname"), _("Name"), _("E-mail"), _("Info"));
325 $line = 0;
326 } // End of header
327
328 $prevbackend = $row["backend"];
329
330 // Check if this user is selected
331 if(in_array($row["backend"].":".$row["nickname"], $defselected))
332 $selected = "CHECKED";
333 else
334 $selected = "";
335
336 // Print one row
337 printf("<TR%s NOWRAP>\n <TD align=center><SMALL>".
338 "<INPUT TYPE=checkbox %s NAME=\"sel[]\" VALUE=\"%d:%s\">".
339 "</SMALL><TD NOWRAP>&nbsp;%s&nbsp;<TD NOWRAP>&nbsp;%s&nbsp;".
340 "<TD NOWRAP>&nbsp;<A HREF=\"compose.php?send_to=%s\">%s</A>".
341 "&nbsp;<TD NOWRAP>&nbsp;%s</TR>\n",
342 ($line % 2) ? " bgcolor=\"$color[0]\"" : "",
343 $selected, $row["backend"], $row["nickname"],
344 $row["nickname"], $row["name"],
345 rawurlencode($row["email"]), $row["email"], $row["label"]);
abdfb4d0 346 $line++;
a82f283d 347 }
348
349 // End of list. Close table.
abdfb4d0 350 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
351 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
352 _("Edit selected"));
353 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
354 _("Delete selected"));
355 print "</TR></TABLE></FORM>";
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>