4789080d9729ece60381f5e63ea00a91066f8d52
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/display_messages.php');
16 include('../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;
284 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
285 while(list($undef,$row) = each($alist)) {
287 // New table header for each backend
288 if($prevbackend != $row["backend"]) {
289 if($prevbackend >= 0) {
290 print '<TR><TD COLSPAN="5" ALIGN=center>';
291 print " <BR></TD></TR></TABLE>\n";
294 print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n";
295 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
296 print $row["source"];
297 print "<STRONG>\n</TD></TR>\n";
300 print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">';
301 printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s"> '.
302 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
303 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
304 "</TR>\n", $color[9], "1%",
310 $headerprinted = true;
313 $prevbackend = $row['backend'];
315 // Check if this user is selected
316 if(in_array($row['backend'].':'.$row['nickname'], $defselected))
317 $selected = 'CHECKED';
323 (($line %
2) ?
" bgcolor=\"$color[0]\"" : ""));
324 print '<TD VALIGN=top ALIGN=center WIDTH="1%"><SMALL>';
325 printf('<INPUT TYPE=checkbox %s NAME="sel[]" VALUE="%s:%s"></SMALL></TD>',
326 $selected, $row["backend"], $row["nickname"]);
327 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>'.
328 '<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>',
329 "1%", $row["nickname"],
331 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> <A HREF="compose.php?send_to=%s">%s</A> </TD>'."\n",
332 "1%", rawurlencode($row["email"]), $row["email"]);
333 printf('<TD VALIGN=top WIDTH="%s"> %s </TD>',
339 // End of list. Close table.
341 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
342 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
344 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
345 _("Delete selected"));
346 print "</TR></TABLE></FORM>";
348 } // end of addresslist
351 // Display the "new address" form
352 printf("<FORM ACTION=\"%s\" NAME=f_add METHOD=\"POST\">\n", $PHP_SELF);
353 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
354 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
355 printf(_("Add to %s"), $abook->localbackendname
);
356 print "<STRONG>\n</TD></TR>\n";
358 address_form('addaddr', _("Add address"), $defdata);
361 // Add hook for anything that wants on the bottom
362 do_hook("addressbook_bottom");