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.
15 if (!isset($strings_php))
16 include('../functions/strings.php');
17 if (!isset($config_php))
18 include('../config/config.php');
19 if (!isset($array_php))
20 include('../functions/array.php');
21 if (!isset($auth_php))
22 include('../functions/auth.php');
23 if (!isset($page_header_php))
24 include('../functions/page_header.php');
25 if (!isset($display_messages_php))
26 include('../functions/display_messages.php');
27 if (!isset($addressbook_php))
28 include('../functions/addressbook.php');
32 // Sort array by the key "name"
33 function alistcmp($a,$b) {
34 if($a['backend'] > $b['backend'])
36 else if($a['backend'] < $b['backend'])
39 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
42 // Output form to add and modify address data
43 function address_form($name, $submittext, $values = array()) {
45 print "<TABLE BORDER=0 CELLPADDING=1 COLS=2 WIDTH=\"90%\" ALIGN=center>\n";
46 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
48 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
49 "<INPUT NAME=\"%s[nickname]\" SIZE=15 VALUE=\"%s\">".
50 " <SMALL>%s</SMALL></TD></TR>\n",
52 (isset($values['nickname']))?
53 htmlspecialchars($values['nickname']):"",
55 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
57 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
58 "<INPUT NAME=\"%s[email]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
60 (isset($values["email"]))?
61 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",
67 (isset($values["firstname"]))?
68 htmlspecialchars($values["firstname"]):"");
69 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
71 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
72 "<INPUT NAME=\"%s[lastname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
74 (isset($values["lastname"]))?
75 htmlspecialchars($values["lastname"]):"");
76 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
77 _("Additional info"));
78 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
79 "<INPUT NAME=\"%s[label]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
81 (isset($values["label"]))?
82 htmlspecialchars($values["label"]):"");
84 printf("<TR><TD COLSPAN=2 BGCOLOR=\"%s\" ALIGN=center>\n".
85 "<INPUT TYPE=submit NAME=\"%s[SUBMIT]\" VALUE=\"%s\"></TD></TR>\n",
86 $color[4], $name, $submittext);
92 include('../src/load_prefs.php');
94 // Open addressbook, with error messages on but without LDAP (the
95 // second "true"). Don't need LDAP here anyway
96 $abook = addressbook_init(true, true);
97 if($abook->localbackend
== 0) {
98 plain_error_message(_("No personal address book is defined. Contact administrator."), $color);
102 displayPageHeader($color, 'None');
108 $showaddrlist = true;
109 $defselected = array();
112 // Handle user's actions
113 if($REQUEST_METHOD == 'POST') {
115 // ***********************************************
117 // ***********************************************
118 if(!empty($addaddr['nickname'])) {
120 $r = $abook->add($addaddr, $abook->localbackend
);
122 // Handle error messages
124 // Remove backend name from error string
125 $errstr = $abook->error
;
126 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
128 $formerror = $errstr;
129 $showaddrlist = false;
136 // ***********************************************
137 // Delete address(es)
138 // ***********************************************
139 else if((!empty($deladdr)) &&
144 // The selected addresses are identidied by "backend:nickname".
145 // Sort the list and process one backend at the time
150 for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
151 list($sbackend, $snick) = split(':', $sel[$i]);
153 // When we get to a new backend, process addresses in
155 if($prevback != $sbackend && $prevback != -1) {
157 $r = $abook->remove($subsel, $prevback);
159 $formerror = $abook->error
;
167 // Queue for processing
168 array_push($subsel, $snick);
169 $prevback = $sbackend;
173 $r = $abook->remove($subsel, $prevback);
174 if(!$r) { // Handle errors
175 $formerror = $abook->error
;
181 $showaddrlist = true;
182 $defselected = $orig_sel;
187 // ***********************************************
188 // Update/modify address
189 // ***********************************************
190 else if(!empty($editaddr)) {
192 // Stage one: Copy data into form
193 if(sizeof($sel) > 0) {
194 if(sizeof($sel) > 1) {
195 $formerror = _("You can only edit one address at the time");
196 $showaddrlist = true;
200 list($ebackend, $enick) = split(":", $sel[0]);
201 $olddata = $abook->lookup($enick, $ebackend);
203 // Display the "new address" form
204 print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n";
205 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
206 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
207 print _("Update address");
208 print "<STRONG>\n</TD></TR>\n";
210 address_form("editaddr", _("Update address"), $olddata);
211 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
212 htmlspecialchars($olddata["nickname"]));
213 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
214 htmlspecialchars($olddata["backend"]));
215 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
220 // Stage two: Write new data
221 else if($doedit = 1) {
222 $newdata = $editaddr;
223 $r = $abook->modify($oldnick, $newdata, $backend);
225 // Handle error messages
228 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
229 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
230 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": ".
231 $abook->error
."</FONT>";
232 print "<STRONG>\n</TD></TR>\n";
235 // Display the "new address" form again
236 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
237 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
238 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
239 print _("Update address");
240 print "<STRONG>\n</TD></TR>\n";
242 address_form("editaddr", _("Update address"), $newdata);
243 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
244 htmlspecialchars($oldnick));
245 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
246 htmlspecialchars($backend));
247 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
254 // Should not get here...
256 plain_error_message(_("Unknown error"), $color);
259 } // End of edit address
263 // Some times we end output before forms are printed
265 print "</BODY></HTML>\n";
271 // ===================================================================
272 // The following is only executed on a GET request, or on a POST when
273 // a user is added, or when "delete" or "modify" was successful.
274 // ===================================================================
276 // Display error messages
277 if(!empty($formerror)) {
278 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
279 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
280 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": $formerror</FONT>";
281 print "<STRONG>\n</TD></TR>\n";
286 // Display the address management part
288 // Get and sort address list
289 $alist = $abook->list_addr();
290 if(!is_array($alist)) {
291 plain_error_message($abook->error
, $color);
295 usort($alist,'alistcmp');
297 $headerprinted = false;
300 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
301 while(list($undef,$row) = each($alist)) {
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 " <BR></TD></TR></TABLE>\n";
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";
316 print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">';
317 printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s"> '.
318 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
319 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
320 "</TR>\n", $color[9], "1%",
326 $headerprinted = true;
329 $prevbackend = $row['backend'];
331 // Check if this user is selected
332 if(in_array($row['backend'].':'.$row['nickname'], $defselected))
333 $selected = 'CHECKED';
339 (($line %
2) ?
" bgcolor=\"$color[0]\"" : ""));
340 print '<TD VALIGN=top ALIGN=center WIDTH="1%"><SMALL>';
341 printf('<INPUT TYPE=checkbox %s NAME="sel[]" VALUE="%s:%s"></SMALL></TD>',
342 $selected, $row["backend"], $row["nickname"]);
343 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>'.
344 '<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>',
345 "1%", $row["nickname"],
347 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> <A HREF="compose.php?send_to=%s">%s</A> </TD>'."\n",
348 "1%", rawurlencode($row["email"]), $row["email"]);
349 printf('<TD VALIGN=top WIDTH="%s"> %s </TD>',
355 // End of list. Close table.
357 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
358 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
360 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
361 _("Delete selected"));
362 print "</TR></TABLE></FORM>";
364 } // end of addresslist
367 // Display the "new address" form
368 printf("<FORM ACTION=\"%s\" NAME=f_add METHOD=\"POST\">\n", $PHP_SELF);
369 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
370 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
371 printf(_("Add to %s"), $abook->localbackendname
);
372 print "<STRONG>\n</TD></TR>\n";
374 address_form('addaddr', _("Add address"), $defdata);
377 // Add hook for anything that wants on the bottom
378 do_hook("addressbook_bottom");