Some fixup:
[squirrelmail.git] / src / addressbook.php
CommitLineData
abdfb4d0 1<?php
2 /**
3 ** addressbook.php
4 **
ef870322 5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
abdfb4d0 8 ** Manage personal address book.
9 **
245a6892 10 ** $Id$
abdfb4d0 11 **/
12
ff8a98e7 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');
abdfb4d0 17
18 // Sort array by the key "name"
19 function alistcmp($a,$b) {
c9e9b23c 20 if($a['backend'] > $b['backend'])
2d367c68 21 return 1;
22 else if($a['backend'] < $b['backend'])
23 return -1;
a82f283d 24
c9e9b23c 25 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
abdfb4d0 26 }
27
28 // Output form to add and modify address data
29 function address_form($name, $submittext, $values = array()) {
30 global $color;
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>",
33 _("Nickname"));
34 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
35 "<INPUT NAME=\"%s[nickname]\" SIZE=15 VALUE=\"%s\">".
36 "&nbsp;<SMALL>%s</SMALL></TD></TR>\n",
245a6892 37 $color[4], $name,
c9e9b23c 38 (isset($values['nickname']))?
39 htmlspecialchars($values['nickname']):"",
abdfb4d0 40 _("Must be unique"));
41 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
42 _("E-mail address"));
43 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
44 "<INPUT NAME=\"%s[email]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
245a6892 45 $color[4], $name,
46 (isset($values["email"]))?
47 htmlspecialchars($values["email"]):"");
abdfb4d0 48 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
49 _("First name"));
50 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
51 "<INPUT NAME=\"%s[firstname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
245a6892 52 $color[4], $name,
53 (isset($values["firstname"]))?
54 htmlspecialchars($values["firstname"]):"");
abdfb4d0 55 printf("<TR><TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
56 _("Last name"));
57 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
58 "<INPUT NAME=\"%s[lastname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
245a6892 59 $color[4], $name,
60 (isset($values["lastname"]))?
61 htmlspecialchars($values["lastname"]):"");
abdfb4d0 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",
245a6892 66 $color[4], $name,
67 (isset($values["label"]))?
68 htmlspecialchars($values["label"]):"");
abdfb4d0 69
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);
73
74 print "</TABLE>\n";
75 }
76
77
abdfb4d0 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);
83 exit();
84 }
85
c9e9b23c 86 displayPageHeader($color, 'None');
abdfb4d0 87
88
89 $defdata = array();
c9e9b23c 90 $formerror = '';
abdfb4d0 91 $abortform = false;
92 $showaddrlist = true;
a82f283d 93 $defselected = array();
abdfb4d0 94
95
96 // Handle user's actions
c9e9b23c 97 if($REQUEST_METHOD == 'POST') {
abdfb4d0 98
a82f283d 99 // ***********************************************
100 // Add new address
101 // ***********************************************
c9e9b23c 102 if(!empty($addaddr['nickname'])) {
abdfb4d0 103
b43bdb85 104 $r = $abook->add($addaddr, $abook->localbackend);
abdfb4d0 105
106 // Handle error messages
107 if(!$r) {
108 // Remove backend name from error string
109 $errstr = $abook->error;
c9e9b23c 110 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
abdfb4d0 111
112 $formerror = $errstr;
113 $showaddrlist = false;
b43bdb85 114 $defdata = $addaddr;
abdfb4d0 115 }
116
a82f283d 117 }
abdfb4d0 118
abdfb4d0 119
a82f283d 120 // ***********************************************
121 // Delete address(es)
122 // ***********************************************
b43bdb85 123 else if((!empty($deladdr)) &&
124 sizeof($sel) > 0) {
125 $orig_sel = $sel;
a82f283d 126 sort($sel);
127
128 // The selected addresses are identidied by "backend:nickname".
129 // Sort the list and process one backend at the time
130 $prevback = -1;
131 $subsel = array();
132 $delfailed = false;
133
134 for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
506cb44a 135 list($sbackend, $snick) = explode(':', $sel[$i]);
a82f283d 136
137 // When we get to a new backend, process addresses in
138 // previous one.
139 if($prevback != $sbackend && $prevback != -1) {
140
141 $r = $abook->remove($subsel, $prevback);
142 if(!$r) {
143 $formerror = $abook->error;
144 $i = sizeof($sel);
145 $delfailed = true;
146 break;
147 }
148 $subsel = array();
149 }
150
151 // Queue for processing
152 array_push($subsel, $snick);
153 $prevback = $sbackend;
154 }
155
156 if(!$delfailed) {
157 $r = $abook->remove($subsel, $prevback);
158 if(!$r) { // Handle errors
159 $formerror = $abook->error;
160 $delfailed = true;
161 }
162 }
163
164 if($delfailed) {
165 $showaddrlist = true;
b43bdb85 166 $defselected = $orig_sel;
a82f283d 167 }
abdfb4d0 168 }
169
a82f283d 170
171 // ***********************************************
172 // Update/modify address
173 // ***********************************************
b43bdb85 174 else if(!empty($editaddr)) {
a82f283d 175
176 // Stage one: Copy data into form
664428fa 177 if (isset($sel) && sizeof($sel) > 0) {
b43bdb85 178 if(sizeof($sel) > 1) {
a82f283d 179 $formerror = _("You can only edit one address at the time");
180 $showaddrlist = true;
b43bdb85 181 $defselected = $sel;
a82f283d 182 } else {
183 $abortform = true;
506cb44a 184 list($ebackend, $enick) = explode(':', $sel[0]);
a82f283d 185 $olddata = $abook->lookup($enick, $ebackend);
186
187 // Display the "new address" form
c9e9b23c 188 print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n";
a82f283d 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";
193 print "</TABLE>\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";
c9e9b23c 200 print '</FORM>';
a82f283d 201 }
202 }
203
204 // Stage two: Write new data
b43bdb85 205 else if($doedit = 1) {
206 $newdata = $editaddr;
207 $r = $abook->modify($oldnick, $newdata, $backend);
a82f283d 208
209 // Handle error messages
210 if(!$r) {
211 // Display error
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";
217 print "</TABLE>\n";
218
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";
225 print "</TABLE>\n";
226 address_form("editaddr", _("Update address"), $newdata);
227 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
d9fdc8d3 228 htmlspecialchars($oldnick));
a82f283d 229 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
d9fdc8d3 230 htmlspecialchars($backend));
a82f283d 231 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
c9e9b23c 232 print '</FORM>';
a82f283d 233
234 $abortform = true;
235 }
236 }
237
238 // Should not get here...
239 else {
240 plain_error_message(_("Unknown error"), $color);
241 $abortform = true;
242 }
243 } // End of edit address
244
245
246
abdfb4d0 247 // Some times we end output before forms are printed
248 if($abortform) {
249 print "</BODY></HTML>\n";
250 exit();
251 }
abdfb4d0 252 }
253
a82f283d 254
abdfb4d0 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 // ===================================================================
259
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";
266 print "</TABLE>\n";
267 }
268
a82f283d 269
abdfb4d0 270 // Display the address management part
271 if($showaddrlist) {
abdfb4d0 272 // Get and sort address list
273 $alist = $abook->list_addr();
9428553f 274 if(!is_array($alist)) {
275 plain_error_message($abook->error, $color);
276 exit;
277 }
278
abdfb4d0 279 usort($alist,'alistcmp');
a82f283d 280 $prevbackend = -1;
6ed96f54 281 $headerprinted = false;
a82f283d 282
01589f26 283 echo "<p align=center><a href=\"#AddAddress\">" .
284 _("Add address") . "</a></p>\n";
285
a82f283d 286 // List addresses
9428553f 287 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
fb16d219 288 while(list($undef,$row) = each($alist)) {
a82f283d 289
290 // New table header for each backend
291 if($prevbackend != $row["backend"]) {
292 if($prevbackend >= 0) {
01589f26 293 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
294 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
295 _("Edit selected"));
296 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
297 _("Delete selected"));
298 echo "</tr>\n";
c9e9b23c 299 print '<TR><TD COLSPAN="5" ALIGN=center>';
a82f283d 300 print "&nbsp;<BR></TD></TR></TABLE>\n";
301 }
302
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";
307 print "</TABLE>\n";
308
c9e9b23c 309 print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">';
59bfec55 310 printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s">&nbsp;'.
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%",
314 "1%", _("Nickname"),
315 "1%", _("Name"),
316 "1%", _("E-mail"),
317 "%", _("Info"));
a82f283d 318 $line = 0;
6ed96f54 319 $headerprinted = true;
a82f283d 320 } // End of header
321
c9e9b23c 322 $prevbackend = $row['backend'];
a82f283d 323
324 // Check if this user is selected
c9e9b23c 325 if(in_array($row['backend'].':'.$row['nickname'], $defselected))
326 $selected = 'CHECKED';
a82f283d 327 else
c9e9b23c 328 $selected = '';
a82f283d 329
330 // Print one row
59bfec55 331 printf("<TR%s>",
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">&nbsp;%s&nbsp;</TD>'.
337 '<TD VALIGN=top NOWRAP WIDTH="%s">&nbsp;%s&nbsp;</TD>',
338 "1%", $row["nickname"],
339 "1%", $row["name"]);
340 printf('<TD VALIGN=top NOWRAP WIDTH="%s">&nbsp;<A HREF="compose.php?send_to=%s">%s</A>&nbsp;</TD>'."\n",
341 "1%", rawurlencode($row["email"]), $row["email"]);
342 printf('<TD VALIGN=top WIDTH="%s">&nbsp;%s&nbsp;</TD>',
343 "%", $row["label"]);
344 print "</TR>\n";
abdfb4d0 345 $line++;
a82f283d 346 }
347
348 // End of list. Close table.
6ed96f54 349 if($headerprinted) {
350 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
351 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
352 _("Edit selected"));
353 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
354 _("Delete selected"));
355 print "</TR></TABLE></FORM>";
356 }
a82f283d 357 } // end of addresslist
358
abdfb4d0 359
360 // Display the "new address" form
01589f26 361 echo "<a name=\"AddAddress\"></a>\n";
d7d3c4d4 362 printf("<FORM ACTION=\"%s\" NAME=f_add METHOD=\"POST\">\n", $PHP_SELF);
abdfb4d0 363 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
364 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
a82f283d 365 printf(_("Add to %s"), $abook->localbackendname);
abdfb4d0 366 print "<STRONG>\n</TD></TR>\n";
367 print "</TABLE>\n";
c9e9b23c 368 address_form('addaddr', _("Add address"), $defdata);
369 print '</FORM>';
abdfb4d0 370
efe94e8e 371 // Add hook for anything that wants on the bottom
372 do_hook("addressbook_bottom");
abdfb4d0 373?>
374
375</BODY></HTML>