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