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