5 ** Manage personal address book.
11 if(!isset($logged_in)) {
12 echo _("You must login first.");
15 if(!isset($username) ||
!isset($key)) {
16 echo _("You need a valid user and password to access this page!");
20 if (!isset($config_php))
21 include("../config/config.php");
22 if (!isset($array_php))
23 include("../functions/array.php");
24 if (!isset($strings_php))
25 include("../functions/strings.php");
26 if (!isset($imap_php))
27 include("../functions/imap.php");
28 if (!isset($page_header_php))
29 include("../functions/page_header.php");
30 if (!isset($display_messages_php))
31 include("../functions/display_messages.php");
32 if (!isset($addressbook_php))
33 include("../functions/addressbook.php");
36 // Sort array by the key "name"
37 function alistcmp($a,$b) {
38 if($a["backend"] > $b["backend"])
40 else if($a["backend"] < $b["backend"])
43 return (strtolower($a["name"]) > strtolower($b["name"])) ?
1 : -1;
46 // Output form to add and modify address data
47 function address_form($name, $submittext, $values = array()) {
49 print "<TABLE BORDER=0 CELLPADDING=1 COLS=2 WIDTH=\"90%\" ALIGN=center>\n";
50 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
52 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
53 "<INPUT NAME=\"%s[nickname]\" SIZE=15 VALUE=\"%s\">".
54 " <SMALL>%s</SMALL></TD></TR>\n",
55 $color[4], $name, htmlspecialchars($values["nickname"]),
57 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
59 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
60 "<INPUT NAME=\"%s[email]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
61 $color[4], $name, htmlspecialchars($values["email"]));
62 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
64 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
65 "<INPUT NAME=\"%s[firstname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
66 $color[4], $name, htmlspecialchars($values["firstname"]));
67 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
69 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
70 "<INPUT NAME=\"%s[lastname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
71 $color[4], $name, htmlspecialchars($values["lastname"]));
72 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
73 _("Additional info"));
74 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
75 "<INPUT NAME=\"%s[label]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
76 $color[4], $name, htmlspecialchars($values["label"]));
78 printf("<TR><TD COLSPAN=2 BGCOLOR=\"%s\" ALIGN=center>\n".
79 "<INPUT TYPE=submit NAME=\"%s[SUBMIT]\" VALUE=\"%s\"></TD></TR>\n",
80 $color[4], $name, $submittext);
87 $imapConnection = sqimap_login ($username, $key,
88 $imapServerAddress, $imapPort, 10);
89 include("../src/load_prefs.php");
90 sqimap_logout ($imapConnection);
93 // Open addressbook, with error messages on but without LDAP (the
94 // second "true"). Don't need LDAP here anyway
95 $abook = addressbook_init(true, true);
96 if($abook->localbackend
== 0) {
97 plain_error_message(_("No personal address book is defined. Contact administrator."), $color);
101 displayPageHeader($color, "None");
107 $showaddrlist = true;
108 $defselected = array();
111 // Handle user's actions
112 if($REQUEST_METHOD == "POST") {
114 // ***********************************************
116 // ***********************************************
117 if(!empty($addaddr["nickname"])) {
119 $r = $abook->add($addaddr, $abook->localbackend
);
121 // Handle error messages
123 // Remove backend name from error string
124 $errstr = $abook->error
;
125 $errstr = ereg_replace("^\[.*\] *", "", $errstr);
127 $formerror = $errstr;
128 $showaddrlist = false;
135 // ***********************************************
136 // Delete address(es)
137 // ***********************************************
138 else if((!empty($deladdr)) &&
143 // The selected addresses are identidied by "backend:nickname".
144 // Sort the list and process one backend at the time
149 for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
150 list($sbackend, $snick) = split(":", $sel[$i]);
152 // When we get to a new backend, process addresses in
154 if($prevback != $sbackend && $prevback != -1) {
156 $r = $abook->remove($subsel, $prevback);
158 $formerror = $abook->error
;
166 // Queue for processing
167 array_push($subsel, $snick);
168 $prevback = $sbackend;
172 $r = $abook->remove($subsel, $prevback);
173 if(!$r) { // Handle errors
174 $formerror = $abook->error
;
180 $showaddrlist = true;
181 $defselected = $orig_sel;
186 // ***********************************************
187 // Update/modify address
188 // ***********************************************
189 else if(!empty($editaddr)) {
191 // Stage one: Copy data into form
192 if(sizeof($sel) > 0) {
193 if(sizeof($sel) > 1) {
194 $formerror = _("You can only edit one address at the time");
195 $showaddrlist = true;
199 list($ebackend, $enick) = split(":", $sel[0]);
200 $olddata = $abook->lookup($enick, $ebackend);
202 // Display the "new address" form
203 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
204 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
205 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
206 print _("Update address");
207 print "<STRONG>\n</TD></TR>\n";
209 address_form("editaddr", _("Update address"), $olddata);
210 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
211 htmlspecialchars($olddata["nickname"]));
212 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
213 htmlspecialchars($olddata["backend"]));
214 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
219 // Stage two: Write new data
220 else if($doedit = 1) {
221 $newdata = $editaddr;
222 $r = $abook->modify($oldnick, $newdata, $backend);
224 // Handle error messages
227 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
228 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
229 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": ".
230 $abook->error
."</FONT>";
231 print "<STRONG>\n</TD></TR>\n";
234 // Display the "new address" form again
235 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
236 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
237 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
238 print _("Update address");
239 print "<STRONG>\n</TD></TR>\n";
241 address_form("editaddr", _("Update address"), $newdata);
242 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
243 htmlspecialchars($newdata["nickname"]));
244 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
245 htmlspecialchars($newdata["backend"]));
246 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
253 // Should not get here...
255 plain_error_message(_("Unknown error"), $color);
258 } // End of edit address
262 // Some times we end output before forms are printed
264 print "</BODY></HTML>\n";
270 // ===================================================================
271 // The following is only executed on a GET request, or on a POST when
272 // a user is added, or when "delete" or "modify" was successful.
273 // ===================================================================
275 // Display error messages
276 if(!empty($formerror)) {
277 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
278 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
279 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": $formerror</FONT>";
280 print "<STRONG>\n</TD></TR>\n";
285 // Display the address management part
287 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
289 // Get and sort address list
290 $alist = $abook->list_addr();
291 usort($alist,'alistcmp');
293 $headerprinted = false;
296 while(list($key,$row) = each($alist)) {
298 // New table header for each backend
299 if($prevbackend != $row["backend"]) {
300 if($prevbackend >= 0) {
301 print "<TR><TD COLSPAN=5 ALIGN=center>";
302 print " <BR></TD></TR></TABLE>\n";
305 print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n";
306 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
307 print $row["source"];
308 print "<STRONG>\n</TD></TR>\n";
311 print '<TABLE COLS=5 BORDER=0 CELLPADDING=1 CELLSPACING=0 WIDTH="90%" ALIGN=center>';
312 printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s"> '.
313 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
314 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
315 "</TR>\n", $color[9], "1%",
321 $headerprinted = true;
324 $prevbackend = $row["backend"];
326 // Check if this user is selected
327 if(in_array($row["backend"].":".$row["nickname"], $defselected))
328 $selected = "CHECKED";
334 (($line %
2) ?
" bgcolor=\"$color[0]\"" : ""));
335 print '<TD VALIGN=top ALIGN=center WIDTH="1%"><SMALL>';
336 printf('<INPUT TYPE=checkbox %s NAME="sel[]" VALUE="%s:%s"></SMALL></TD>',
337 $selected, $row["backend"], $row["nickname"]);
338 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>'.
339 '<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>',
340 "1%", $row["nickname"],
342 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> <A HREF="compose.php?send_to=%s">%s</A> </TD>'."\n",
343 "1%", rawurlencode($row["email"]), $row["email"]);
344 printf('<TD VALIGN=top WIDTH="%s"> %s </TD>',
350 // End of list. Close table.
352 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
353 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
355 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
356 _("Delete selected"));
357 print "</TR></TABLE></FORM>";
359 } // end of addresslist
362 // Display the "new address" form
363 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
364 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
365 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
366 printf(_("Add to %s"), $abook->localbackendname
);
367 print "<STRONG>\n</TD></TR>\n";
369 address_form("addaddr", _("Add address"), $defdata);