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 require_once('../src/validate.php');
14 require_once('../functions/array.php');
15 require_once('../functions/display_messages.php');
16 require_once('../functions/addressbook.php');
18 // Sort array by the key "name"
19 function alistcmp($a,$b) {
20 if($a['backend'] > $b['backend'])
22 else if($a['backend'] < $b['backend'])
25 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
28 // Output form to add and modify address data
29 function address_form($name, $submittext, $values = array()) {
31 print "<TABLE BORDER=0 CELLPADDING=1 COLS=2 WIDTH=\"90%\" ALIGN=center>\n";
32 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
34 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
35 "<INPUT NAME=\"%s[nickname]\" SIZE=15 VALUE=\"%s\">".
36 " <SMALL>%s</SMALL></TD></TR>\n",
38 (isset($values['nickname']))?
39 htmlspecialchars($values['nickname']):"",
41 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
43 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
44 "<INPUT NAME=\"%s[email]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
46 (isset($values["email"]))?
47 htmlspecialchars($values["email"]):"");
48 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
50 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
51 "<INPUT NAME=\"%s[firstname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
53 (isset($values["firstname"]))?
54 htmlspecialchars($values["firstname"]):"");
55 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
57 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
58 "<INPUT NAME=\"%s[lastname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
60 (isset($values["lastname"]))?
61 htmlspecialchars($values["lastname"]):"");
62 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
63 _("Additional info"));
64 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
65 "<INPUT NAME=\"%s[label]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
67 (isset($values["label"]))?
68 htmlspecialchars($values["label"]):"");
70 printf("<TR><TD COLSPAN=2 BGCOLOR=\"%s\" ALIGN=center>\n".
71 "<INPUT TYPE=submit NAME=\"%s[SUBMIT]\" VALUE=\"%s\"></TD></TR>\n",
72 $color[4], $name, $submittext);
78 // Open addressbook, with error messages on but without LDAP (the
79 // second "true"). Don't need LDAP here anyway
80 $abook = addressbook_init(true, true);
81 if($abook->localbackend
== 0) {
82 plain_error_message(_("No personal address book is defined. Contact administrator."), $color);
86 displayPageHeader($color, 'None');
93 $defselected = array();
96 // Handle user's actions
97 if($REQUEST_METHOD == 'POST') {
99 // ***********************************************
101 // ***********************************************
102 if(!empty($addaddr['nickname'])) {
104 $r = $abook->add($addaddr, $abook->localbackend
);
106 // Handle error messages
108 // Remove backend name from error string
109 $errstr = $abook->error
;
110 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
112 $formerror = $errstr;
113 $showaddrlist = false;
120 // ***********************************************
121 // Delete address(es)
122 // ***********************************************
123 else if((!empty($deladdr)) &&
128 // The selected addresses are identidied by "backend:nickname".
129 // Sort the list and process one backend at the time
134 for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
135 list($sbackend, $snick) = explode(':', $sel[$i]);
137 // When we get to a new backend, process addresses in
139 if($prevback != $sbackend && $prevback != -1) {
141 $r = $abook->remove($subsel, $prevback);
143 $formerror = $abook->error
;
151 // Queue for processing
152 array_push($subsel, $snick);
153 $prevback = $sbackend;
157 $r = $abook->remove($subsel, $prevback);
158 if(!$r) { // Handle errors
159 $formerror = $abook->error
;
165 $showaddrlist = true;
166 $defselected = $orig_sel;
171 // ***********************************************
172 // Update/modify address
173 // ***********************************************
174 else if(!empty($editaddr)) {
176 // Stage one: Copy data into form
177 if (isset($sel) && sizeof($sel) > 0) {
178 if(sizeof($sel) > 1) {
179 $formerror = _("You can only edit one address at the time");
180 $showaddrlist = true;
184 list($ebackend, $enick) = explode(':', $sel[0]);
185 $olddata = $abook->lookup($enick, $ebackend);
187 // Display the "new address" form
188 print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n";
189 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
190 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
191 print _("Update address");
192 print "<STRONG>\n</TD></TR>\n";
194 address_form("editaddr", _("Update address"), $olddata);
195 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
196 htmlspecialchars($olddata["nickname"]));
197 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
198 htmlspecialchars($olddata["backend"]));
199 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
204 // Stage two: Write new data
205 else if($doedit = 1) {
206 $newdata = $editaddr;
207 $r = $abook->modify($oldnick, $newdata, $backend);
209 // Handle error messages
212 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
213 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
214 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": ".
215 $abook->error
."</FONT>";
216 print "<STRONG>\n</TD></TR>\n";
219 // Display the "new address" form again
220 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
221 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
222 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
223 print _("Update address");
224 print "<STRONG>\n</TD></TR>\n";
226 address_form("editaddr", _("Update address"), $newdata);
227 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
228 htmlspecialchars($oldnick));
229 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
230 htmlspecialchars($backend));
231 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
238 // Should not get here...
240 plain_error_message(_("Unknown error"), $color);
243 } // End of edit address
247 // Some times we end output before forms are printed
249 print "</BODY></HTML>\n";
255 // ===================================================================
256 // The following is only executed on a GET request, or on a POST when
257 // a user is added, or when "delete" or "modify" was successful.
258 // ===================================================================
260 // Display error messages
261 if(!empty($formerror)) {
262 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
263 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
264 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": $formerror</FONT>";
265 print "<STRONG>\n</TD></TR>\n";
270 // Display the address management part
272 // Get and sort address list
273 $alist = $abook->list_addr();
274 if(!is_array($alist)) {
275 plain_error_message($abook->error
, $color);
279 usort($alist,'alistcmp');
281 $headerprinted = false;
283 echo "<p align=center><a href=\"#AddAddress\">" .
284 _("Add address") . "</a></p>\n";
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>\n";
294 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
296 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
297 _("Delete selected"));
299 print '<TR><TD COLSPAN="5" ALIGN=center>';
300 print " <BR></TD></TR></TABLE>\n";
303 print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n";
304 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
305 print $row["source"];
306 print "<STRONG>\n</TD></TR>\n";
309 print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">';
310 printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s"> '.
311 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
312 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
313 "</TR>\n", $color[9], "1%",
319 $headerprinted = true;
322 $prevbackend = $row['backend'];
324 // Check if this user is selected
325 if(in_array($row['backend'].':'.$row['nickname'], $defselected))
326 $selected = 'CHECKED';
332 (($line %
2) ?
" bgcolor=\"$color[0]\"" : ""));
333 print '<TD VALIGN=top ALIGN=center WIDTH="1%"><SMALL>';
334 printf('<INPUT TYPE=checkbox %s NAME="sel[]" VALUE="%s:%s"></SMALL></TD>',
335 $selected, $row["backend"], $row["nickname"]);
336 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>'.
337 '<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>',
338 "1%", $row["nickname"],
340 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> <A HREF="compose.php?send_to=%s">%s</A> </TD>'."\n",
341 "1%", rawurlencode($row["email"]), $row["email"]);
342 printf('<TD VALIGN=top WIDTH="%s"> %s </TD>',
348 // End of list. Close table.
350 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
351 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
353 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
354 _("Delete selected"));
355 print "</TR></TABLE></FORM>";
357 } // end of addresslist
360 // Display the "new address" form
361 echo "<a name=\"AddAddress\"></a>\n";
362 printf("<FORM ACTION=\"%s\" NAME=f_add METHOD=\"POST\">\n", $PHP_SELF);
363 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
364 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
365 printf(_("Add to %s"), $abook->localbackendname
);
366 print "<STRONG>\n</TD></TR>\n";
368 address_form('addaddr', _("Add address"), $defdata);
371 // Add hook for anything that wants on the bottom
372 do_hook("addressbook_bottom");