* To reduce on errors, I moved the includes for config.php and strings.php
[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/page_header.php');
16 include('../functions/display_messages.php');
17 include('../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 include('../src/load_prefs.php');
80
81 // Open addressbook, with error messages on but without LDAP (the
82 // second "true"). Don't need LDAP here anyway
83 $abook = addressbook_init(true, true);
84 if($abook->localbackend == 0) {
85 plain_error_message(_("No personal address book is defined. Contact administrator."), $color);
86 exit();
87 }
88
89 displayPageHeader($color, 'None');
90
91
92 $defdata = array();
93 $formerror = '';
94 $abortform = false;
95 $showaddrlist = true;
96 $defselected = array();
97
98
99 // Handle user's actions
100 if($REQUEST_METHOD == 'POST') {
101
102 // ***********************************************
103 // Add new address
104 // ***********************************************
105 if(!empty($addaddr['nickname'])) {
106
107 $r = $abook->add($addaddr, $abook->localbackend);
108
109 // Handle error messages
110 if(!$r) {
111 // Remove backend name from error string
112 $errstr = $abook->error;
113 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
114
115 $formerror = $errstr;
116 $showaddrlist = false;
117 $defdata = $addaddr;
118 }
119
120 }
121
122
123 // ***********************************************
124 // Delete address(es)
125 // ***********************************************
126 else if((!empty($deladdr)) &&
127 sizeof($sel) > 0) {
128 $orig_sel = $sel;
129 sort($sel);
130
131 // The selected addresses are identidied by "backend:nickname".
132 // Sort the list and process one backend at the time
133 $prevback = -1;
134 $subsel = array();
135 $delfailed = false;
136
137 for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
138 list($sbackend, $snick) = explode(':', $sel[$i]);
139
140 // When we get to a new backend, process addresses in
141 // previous one.
142 if($prevback != $sbackend && $prevback != -1) {
143
144 $r = $abook->remove($subsel, $prevback);
145 if(!$r) {
146 $formerror = $abook->error;
147 $i = sizeof($sel);
148 $delfailed = true;
149 break;
150 }
151 $subsel = array();
152 }
153
154 // Queue for processing
155 array_push($subsel, $snick);
156 $prevback = $sbackend;
157 }
158
159 if(!$delfailed) {
160 $r = $abook->remove($subsel, $prevback);
161 if(!$r) { // Handle errors
162 $formerror = $abook->error;
163 $delfailed = true;
164 }
165 }
166
167 if($delfailed) {
168 $showaddrlist = true;
169 $defselected = $orig_sel;
170 }
171 }
172
173
174 // ***********************************************
175 // Update/modify address
176 // ***********************************************
177 else if(!empty($editaddr)) {
178
179 // Stage one: Copy data into form
180 if(sizeof($sel) > 0) {
181 if(sizeof($sel) > 1) {
182 $formerror = _("You can only edit one address at the time");
183 $showaddrlist = true;
184 $defselected = $sel;
185 } else {
186 $abortform = true;
187 list($ebackend, $enick) = explode(':', $sel[0]);
188 $olddata = $abook->lookup($enick, $ebackend);
189
190 // Display the "new address" form
191 print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n";
192 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
193 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
194 print _("Update address");
195 print "<STRONG>\n</TD></TR>\n";
196 print "</TABLE>\n";
197 address_form("editaddr", _("Update address"), $olddata);
198 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
199 htmlspecialchars($olddata["nickname"]));
200 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
201 htmlspecialchars($olddata["backend"]));
202 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
203 print '</FORM>';
204 }
205 }
206
207 // Stage two: Write new data
208 else if($doedit = 1) {
209 $newdata = $editaddr;
210 $r = $abook->modify($oldnick, $newdata, $backend);
211
212 // Handle error messages
213 if(!$r) {
214 // Display error
215 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
216 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
217 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": ".
218 $abook->error."</FONT>";
219 print "<STRONG>\n</TD></TR>\n";
220 print "</TABLE>\n";
221
222 // Display the "new address" form again
223 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
224 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
225 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
226 print _("Update address");
227 print "<STRONG>\n</TD></TR>\n";
228 print "</TABLE>\n";
229 address_form("editaddr", _("Update address"), $newdata);
230 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
231 htmlspecialchars($oldnick));
232 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
233 htmlspecialchars($backend));
234 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
235 print '</FORM>';
236
237 $abortform = true;
238 }
239 }
240
241 // Should not get here...
242 else {
243 plain_error_message(_("Unknown error"), $color);
244 $abortform = true;
245 }
246 } // End of edit address
247
248
249
250 // Some times we end output before forms are printed
251 if($abortform) {
252 print "</BODY></HTML>\n";
253 exit();
254 }
255 }
256
257
258 // ===================================================================
259 // The following is only executed on a GET request, or on a POST when
260 // a user is added, or when "delete" or "modify" was successful.
261 // ===================================================================
262
263 // Display error messages
264 if(!empty($formerror)) {
265 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
266 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
267 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": $formerror</FONT>";
268 print "<STRONG>\n</TD></TR>\n";
269 print "</TABLE>\n";
270 }
271
272
273 // Display the address management part
274 if($showaddrlist) {
275 // Get and sort address list
276 $alist = $abook->list_addr();
277 if(!is_array($alist)) {
278 plain_error_message($abook->error, $color);
279 exit;
280 }
281
282 usort($alist,'alistcmp');
283 $prevbackend = -1;
284 $headerprinted = false;
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>';
294 print "&nbsp;<BR></TD></TR></TABLE>\n";
295 }
296
297 print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n";
298 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
299 print $row["source"];
300 print "<STRONG>\n</TD></TR>\n";
301 print "</TABLE>\n";
302
303 print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">';
304 printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s">&nbsp;'.
305 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
306 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
307 "</TR>\n", $color[9], "1%",
308 "1%", _("Nickname"),
309 "1%", _("Name"),
310 "1%", _("E-mail"),
311 "%", _("Info"));
312 $line = 0;
313 $headerprinted = true;
314 } // End of header
315
316 $prevbackend = $row['backend'];
317
318 // Check if this user is selected
319 if(in_array($row['backend'].':'.$row['nickname'], $defselected))
320 $selected = 'CHECKED';
321 else
322 $selected = '';
323
324 // Print one row
325 printf("<TR%s>",
326 (($line % 2) ? " bgcolor=\"$color[0]\"" : ""));
327 print '<TD VALIGN=top ALIGN=center WIDTH="1%"><SMALL>';
328 printf('<INPUT TYPE=checkbox %s NAME="sel[]" VALUE="%s:%s"></SMALL></TD>',
329 $selected, $row["backend"], $row["nickname"]);
330 printf('<TD VALIGN=top NOWRAP WIDTH="%s">&nbsp;%s&nbsp;</TD>'.
331 '<TD VALIGN=top NOWRAP WIDTH="%s">&nbsp;%s&nbsp;</TD>',
332 "1%", $row["nickname"],
333 "1%", $row["name"]);
334 printf('<TD VALIGN=top NOWRAP WIDTH="%s">&nbsp;<A HREF="compose.php?send_to=%s">%s</A>&nbsp;</TD>'."\n",
335 "1%", rawurlencode($row["email"]), $row["email"]);
336 printf('<TD VALIGN=top WIDTH="%s">&nbsp;%s&nbsp;</TD>',
337 "%", $row["label"]);
338 print "</TR>\n";
339 $line++;
340 }
341
342 // End of list. Close table.
343 if($headerprinted) {
344 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
345 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
346 _("Edit selected"));
347 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
348 _("Delete selected"));
349 print "</TR></TABLE></FORM>";
350 }
351 } // end of addresslist
352
353
354 // Display the "new address" form
355 printf("<FORM ACTION=\"%s\" NAME=f_add METHOD=\"POST\">\n", $PHP_SELF);
356 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
357 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
358 printf(_("Add to %s"), $abook->localbackendname);
359 print "<STRONG>\n</TD></TR>\n";
360 print "</TABLE>\n";
361 address_form('addaddr', _("Add address"), $defdata);
362 print '</FORM>';
363
364 // Add hook for anything that wants on the bottom
365 do_hook("addressbook_bottom");
366 ?>
367
368 </BODY></HTML>