(c) stuff
[squirrelmail.git] / src / addressbook.php
1 <?php
2
3 /**
4 ** addressbook.php
5 **
6 ** Copyright (c) 1999-2001 The SquirrelMail development team
7 ** Licensed under the GNU GPL. For full terms see the file COPYING.
8 **
9 ** Manage personal address book.
10 **
11 ** $Id$
12 **/
13
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');
18
19 // Sort array by the key "name"
20 function alistcmp($a,$b) {
21 if($a['backend'] > $b['backend'])
22 return 1;
23 else if($a['backend'] < $b['backend'])
24 return -1;
25
26 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
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",
38 $color[4], $name,
39 (isset($values['nickname']))?
40 htmlspecialchars($values['nickname']):"",
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",
46 $color[4], $name,
47 (isset($values["email"]))?
48 htmlspecialchars($values["email"]):"");
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",
53 $color[4], $name,
54 (isset($values["firstname"]))?
55 htmlspecialchars($values["firstname"]):"");
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",
60 $color[4], $name,
61 (isset($values["lastname"]))?
62 htmlspecialchars($values["lastname"]):"");
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",
67 $color[4], $name,
68 (isset($values["label"]))?
69 htmlspecialchars($values["label"]):"");
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
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
87 displayPageHeader($color, 'None');
88
89
90 $defdata = array();
91 $formerror = '';
92 $abortform = false;
93 $showaddrlist = true;
94 $defselected = array();
95
96
97 // Handle user's actions
98 if($REQUEST_METHOD == 'POST') {
99
100 // ***********************************************
101 // Add new address
102 // ***********************************************
103 if(!empty($addaddr['nickname'])) {
104
105 $r = $abook->add($addaddr, $abook->localbackend);
106
107 // Handle error messages
108 if(!$r) {
109 // Remove backend name from error string
110 $errstr = $abook->error;
111 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
112
113 $formerror = $errstr;
114 $showaddrlist = false;
115 $defdata = $addaddr;
116 }
117
118 }
119
120
121 // ***********************************************
122 // Delete address(es)
123 // ***********************************************
124 else if((!empty($deladdr)) &&
125 sizeof($sel) > 0) {
126 $orig_sel = $sel;
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++) {
136 list($sbackend, $snick) = explode(':', $sel[$i]);
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;
167 $defselected = $orig_sel;
168 }
169 }
170
171
172 // ***********************************************
173 // Update/modify address
174 // ***********************************************
175 else if(!empty($editaddr)) {
176
177 // Stage one: Copy data into form
178 if (isset($sel) && sizeof($sel) > 0) {
179 if(sizeof($sel) > 1) {
180 $formerror = _("You can only edit one address at the time");
181 $showaddrlist = true;
182 $defselected = $sel;
183 } else {
184 $abortform = true;
185 list($ebackend, $enick) = explode(':', $sel[0]);
186 $olddata = $abook->lookup($enick, $ebackend);
187
188 // Display the "new address" form
189 print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n";
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";
201 print '</FORM>';
202 }
203 }
204
205 // Stage two: Write new data
206 else if($doedit = 1) {
207 $newdata = $editaddr;
208 $r = $abook->modify($oldnick, $newdata, $backend);
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",
229 htmlspecialchars($oldnick));
230 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
231 htmlspecialchars($backend));
232 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
233 print '</FORM>';
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
248 // Some times we end output before forms are printed
249 if($abortform) {
250 print "</BODY></HTML>\n";
251 exit();
252 }
253 }
254
255
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
270
271 // Display the address management part
272 if($showaddrlist) {
273 // Get and sort address list
274 $alist = $abook->list_addr();
275 if(!is_array($alist)) {
276 plain_error_message($abook->error, $color);
277 exit;
278 }
279
280 usort($alist,'alistcmp');
281 $prevbackend = -1;
282 $headerprinted = false;
283
284 echo "<p align=center><a href=\"#AddAddress\">" .
285 _("Add address") . "</a></p>\n";
286
287 // List addresses
288 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
289 while(list($undef,$row) = each($alist)) {
290
291 // New table header for each backend
292 if($prevbackend != $row["backend"]) {
293 if($prevbackend >= 0) {
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";
300 print '<TR><TD COLSPAN="5" ALIGN=center>';
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
310 print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">';
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"));
319 $line = 0;
320 $headerprinted = true;
321 } // End of header
322
323 $prevbackend = $row['backend'];
324
325 // Check if this user is selected
326 if(in_array($row['backend'].':'.$row['nickname'], $defselected))
327 $selected = 'CHECKED';
328 else
329 $selected = '';
330
331 // Print one row
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";
346 $line++;
347 }
348
349 // End of list. Close table.
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 }
358 } // end of addresslist
359
360
361 // Display the "new address" form
362 echo "<a name=\"AddAddress\"></a>\n";
363 printf("<FORM ACTION=\"%s\" NAME=f_add METHOD=\"POST\">\n", $PHP_SELF);
364 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
365 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
366 printf(_("Add to %s"), $abook->localbackendname);
367 print "<STRONG>\n</TD></TR>\n";
368 print "</TABLE>\n";
369 address_form('addaddr', _("Add address"), $defdata);
370 print '</FORM>';
371
372 // Add hook for anything that wants on the bottom
373 do_hook("addressbook_bottom");
374 ?>
375
376 </BODY></HTML>