5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
8 ** Manage personal address book.
13 include('../src/validate.php');
14 include('../functions/array.php');
15 include('../functions/page_header.php');
16 include('../functions/display_messages.php');
17 include('../functions/addressbook.php');
19 // Sort array by the key "name"
20 function alistcmp($a,$b) {
21 if($a['backend'] > $b['backend'])
23 else if($a['backend'] < $b['backend'])
26 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
29 // Output form to add and modify address data
30 function address_form($name, $submittext, $values = array()) {
32 print "<TABLE BORDER=0 CELLPADDING=1 COLS=2 WIDTH=\"90%\" ALIGN=center>\n";
33 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
35 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
36 "<INPUT NAME=\"%s[nickname]\" SIZE=15 VALUE=\"%s\">".
37 " <SMALL>%s</SMALL></TD></TR>\n",
39 (isset($values['nickname']))?
40 htmlspecialchars($values['nickname']):"",
42 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
44 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
45 "<INPUT NAME=\"%s[email]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
47 (isset($values["email"]))?
48 htmlspecialchars($values["email"]):"");
49 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
51 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
52 "<INPUT NAME=\"%s[firstname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
54 (isset($values["firstname"]))?
55 htmlspecialchars($values["firstname"]):"");
56 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
58 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
59 "<INPUT NAME=\"%s[lastname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
61 (isset($values["lastname"]))?
62 htmlspecialchars($values["lastname"]):"");
63 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
64 _("Additional info"));
65 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
66 "<INPUT NAME=\"%s[label]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
68 (isset($values["label"]))?
69 htmlspecialchars($values["label"]):"");
71 printf("<TR><TD COLSPAN=2 BGCOLOR=\"%s\" ALIGN=center>\n".
72 "<INPUT TYPE=submit NAME=\"%s[SUBMIT]\" VALUE=\"%s\"></TD></TR>\n",
73 $color[4], $name, $submittext);
79 include('../src/load_prefs.php');
81 // Open addressbook, with error messages on but without LDAP (the
82 // second "true"). Don't need LDAP here anyway
83 $abook = addressbook_init(true, true);
84 if($abook->localbackend
== 0) {
85 plain_error_message(_("No personal address book is defined. Contact administrator."), $color);
89 displayPageHeader($color, 'None');
96 $defselected = array();
99 // Handle user's actions
100 if($REQUEST_METHOD == 'POST') {
102 // ***********************************************
104 // ***********************************************
105 if(!empty($addaddr['nickname'])) {
107 $r = $abook->add($addaddr, $abook->localbackend
);
109 // Handle error messages
111 // Remove backend name from error string
112 $errstr = $abook->error
;
113 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
115 $formerror = $errstr;
116 $showaddrlist = false;
123 // ***********************************************
124 // Delete address(es)
125 // ***********************************************
126 else if((!empty($deladdr)) &&
131 // The selected addresses are identidied by "backend:nickname".
132 // Sort the list and process one backend at the time
137 for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
138 list($sbackend, $snick) = explode(':', $sel[$i]);
140 // When we get to a new backend, process addresses in
142 if($prevback != $sbackend && $prevback != -1) {
144 $r = $abook->remove($subsel, $prevback);
146 $formerror = $abook->error
;
154 // Queue for processing
155 array_push($subsel, $snick);
156 $prevback = $sbackend;
160 $r = $abook->remove($subsel, $prevback);
161 if(!$r) { // Handle errors
162 $formerror = $abook->error
;
168 $showaddrlist = true;
169 $defselected = $orig_sel;
174 // ***********************************************
175 // Update/modify address
176 // ***********************************************
177 else if(!empty($editaddr)) {
179 // Stage one: Copy data into form
180 if(sizeof($sel) > 0) {
181 if(sizeof($sel) > 1) {
182 $formerror = _("You can only edit one address at the time");
183 $showaddrlist = true;
187 list($ebackend, $enick) = explode(':', $sel[0]);
188 $olddata = $abook->lookup($enick, $ebackend);
190 // Display the "new address" form
191 print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n";
192 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
193 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
194 print _("Update address");
195 print "<STRONG>\n</TD></TR>\n";
197 address_form("editaddr", _("Update address"), $olddata);
198 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
199 htmlspecialchars($olddata["nickname"]));
200 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
201 htmlspecialchars($olddata["backend"]));
202 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
207 // Stage two: Write new data
208 else if($doedit = 1) {
209 $newdata = $editaddr;
210 $r = $abook->modify($oldnick, $newdata, $backend);
212 // Handle error messages
215 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
216 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
217 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": ".
218 $abook->error
."</FONT>";
219 print "<STRONG>\n</TD></TR>\n";
222 // Display the "new address" form again
223 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
224 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
225 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
226 print _("Update address");
227 print "<STRONG>\n</TD></TR>\n";
229 address_form("editaddr", _("Update address"), $newdata);
230 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
231 htmlspecialchars($oldnick));
232 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
233 htmlspecialchars($backend));
234 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
241 // Should not get here...
243 plain_error_message(_("Unknown error"), $color);
246 } // End of edit address
250 // Some times we end output before forms are printed
252 print "</BODY></HTML>\n";
258 // ===================================================================
259 // The following is only executed on a GET request, or on a POST when
260 // a user is added, or when "delete" or "modify" was successful.
261 // ===================================================================
263 // Display error messages
264 if(!empty($formerror)) {
265 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
266 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
267 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": $formerror</FONT>";
268 print "<STRONG>\n</TD></TR>\n";
273 // Display the address management part
275 // Get and sort address list
276 $alist = $abook->list_addr();
277 if(!is_array($alist)) {
278 plain_error_message($abook->error
, $color);
282 usort($alist,'alistcmp');
284 $headerprinted = false;
287 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
288 while(list($undef,$row) = each($alist)) {
290 // New table header for each backend
291 if($prevbackend != $row["backend"]) {
292 if($prevbackend >= 0) {
293 print '<TR><TD COLSPAN="5" ALIGN=center>';
294 print " <BR></TD></TR></TABLE>\n";
297 print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n";
298 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
299 print $row["source"];
300 print "<STRONG>\n</TD></TR>\n";
303 print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">';
304 printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s"> '.
305 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
306 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
307 "</TR>\n", $color[9], "1%",
313 $headerprinted = true;
316 $prevbackend = $row['backend'];
318 // Check if this user is selected
319 if(in_array($row['backend'].':'.$row['nickname'], $defselected))
320 $selected = 'CHECKED';
326 (($line %
2) ?
" bgcolor=\"$color[0]\"" : ""));
327 print '<TD VALIGN=top ALIGN=center WIDTH="1%"><SMALL>';
328 printf('<INPUT TYPE=checkbox %s NAME="sel[]" VALUE="%s:%s"></SMALL></TD>',
329 $selected, $row["backend"], $row["nickname"]);
330 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>'.
331 '<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>',
332 "1%", $row["nickname"],
334 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> <A HREF="compose.php?send_to=%s">%s</A> </TD>'."\n",
335 "1%", rawurlencode($row["email"]), $row["email"]);
336 printf('<TD VALIGN=top WIDTH="%s"> %s </TD>',
342 // End of list. Close table.
344 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
345 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
347 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
348 _("Delete selected"));
349 print "</TR></TABLE></FORM>";
351 } // end of addresslist
354 // Display the "new address" form
355 printf("<FORM ACTION=\"%s\" NAME=f_add METHOD=\"POST\">\n", $PHP_SELF);
356 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
357 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
358 printf(_("Add to %s"), $abook->localbackendname
);
359 print "<STRONG>\n</TD></TR>\n";
361 address_form('addaddr', _("Add address"), $defdata);
364 // Add hook for anything that wants on the bottom
365 do_hook("addressbook_bottom");