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