Removed fourth parameter from the NEVER option. That parameter doesn't exist.
[squirrelmail.git] / src / addressbook.php
1 <?php
2 /**
3 ** addressbook.php
4 **
5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
8 ** Manage personal address book.
9 **
10 ** $Id$
11 **/
12
13 include('../src/validate.php');
14 include('../functions/array.php');
15 include('../functions/display_messages.php');
16 include('../functions/addressbook.php');
17
18 // Sort array by the key "name"
19 function alistcmp($a,$b) {
20 if($a['backend'] > $b['backend'])
21 return 1;
22 else if($a['backend'] < $b['backend'])
23 return -1;
24
25 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
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",
37 $color[4], $name,
38 (isset($values['nickname']))?
39 htmlspecialchars($values['nickname']):"",
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",
45 $color[4], $name,
46 (isset($values["email"]))?
47 htmlspecialchars($values["email"]):"");
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",
52 $color[4], $name,
53 (isset($values["firstname"]))?
54 htmlspecialchars($values["firstname"]):"");
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",
59 $color[4], $name,
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",
66 $color[4], $name,
67 (isset($values["label"]))?
68 htmlspecialchars($values["label"]):"");
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
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
86 displayPageHeader($color, 'None');
87
88
89 $defdata = array();
90 $formerror = '';
91 $abortform = false;
92 $showaddrlist = true;
93 $defselected = array();
94
95
96 // Handle user's actions
97 if($REQUEST_METHOD == 'POST') {
98
99 // ***********************************************
100 // Add new address
101 // ***********************************************
102 if(!empty($addaddr['nickname'])) {
103
104 $r = $abook->add($addaddr, $abook->localbackend);
105
106 // Handle error messages
107 if(!$r) {
108 // Remove backend name from error string
109 $errstr = $abook->error;
110 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
111
112 $formerror = $errstr;
113 $showaddrlist = false;
114 $defdata = $addaddr;
115 }
116
117 }
118
119
120 // ***********************************************
121 // Delete address(es)
122 // ***********************************************
123 else if((!empty($deladdr)) &&
124 sizeof($sel) > 0) {
125 $orig_sel = $sel;
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++) {
135 list($sbackend, $snick) = explode(':', $sel[$i]);
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;
166 $defselected = $orig_sel;
167 }
168 }
169
170
171 // ***********************************************
172 // Update/modify address
173 // ***********************************************
174 else if(!empty($editaddr)) {
175
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;
181 $defselected = $sel;
182 } else {
183 $abortform = true;
184 list($ebackend, $enick) = explode(':', $sel[0]);
185 $olddata = $abook->lookup($enick, $ebackend);
186
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";
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";
200 print '</FORM>';
201 }
202 }
203
204 // Stage two: Write new data
205 else if($doedit = 1) {
206 $newdata = $editaddr;
207 $r = $abook->modify($oldnick, $newdata, $backend);
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",
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";
232 print '</FORM>';
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
247 // Some times we end output before forms are printed
248 if($abortform) {
249 print "</BODY></HTML>\n";
250 exit();
251 }
252 }
253
254
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
269
270 // Display the address management part
271 if($showaddrlist) {
272 // Get and sort address list
273 $alist = $abook->list_addr();
274 if(!is_array($alist)) {
275 plain_error_message($abook->error, $color);
276 exit;
277 }
278
279 usort($alist,'alistcmp');
280 $prevbackend = -1;
281 $headerprinted = false;
282
283 echo "<p align=center><a href=\"#AddAddress\">" .
284 _("Add address") . "</a></p>\n";
285
286 // List addresses
287 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
288 while(list($undef,$row) = each($alist)) {
289
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",
295 _("Edit selected"));
296 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
297 _("Delete selected"));
298 echo "</tr>\n";
299 print '<TR><TD COLSPAN="5" ALIGN=center>';
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
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">&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"));
318 $line = 0;
319 $headerprinted = true;
320 } // End of header
321
322 $prevbackend = $row['backend'];
323
324 // Check if this user is selected
325 if(in_array($row['backend'].':'.$row['nickname'], $defselected))
326 $selected = 'CHECKED';
327 else
328 $selected = '';
329
330 // Print one row
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";
345 $line++;
346 }
347
348 // End of list. Close table.
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 }
357 } // end of addresslist
358
359
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";
367 print "</TABLE>\n";
368 address_form('addaddr', _("Add address"), $defdata);
369 print '</FORM>';
370
371 // Add hook for anything that wants on the bottom
372 do_hook("addressbook_bottom");
373 ?>
374
375 </BODY></HTML>