"me" to 'you'
[squirrelmail.git] / src / addressbook.php
CommitLineData
abdfb4d0 1<?php
2 /**
3 ** addressbook.php
4 **
ef870322 5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
abdfb4d0 8 ** Manage personal address book.
9 **
245a6892 10 ** $Id$
abdfb4d0 11 **/
12
2a32fc83 13 session_start();
14
245a6892 15 if (!isset($strings_php))
c9e9b23c 16 include('../functions/strings.php');
abdfb4d0 17 if (!isset($config_php))
c9e9b23c 18 include('../config/config.php');
abdfb4d0 19 if (!isset($array_php))
c9e9b23c 20 include('../functions/array.php');
959f43e3 21 if (!isset($auth_php))
c9e9b23c 22 include('../functions/auth.php');
abdfb4d0 23 if (!isset($page_header_php))
c9e9b23c 24 include('../functions/page_header.php');
abdfb4d0 25 if (!isset($display_messages_php))
c9e9b23c 26 include('../functions/display_messages.php');
abdfb4d0 27 if (!isset($addressbook_php))
c9e9b23c 28 include('../functions/addressbook.php');
abdfb4d0 29
959f43e3 30 is_logged_in();
abdfb4d0 31
32 // Sort array by the key "name"
33 function alistcmp($a,$b) {
c9e9b23c 34 if($a['backend'] > $b['backend'])
a82f283d 35 return 1;
c9e9b23c 36 else if($a['backend'] < $b['backend'])
a82f283d 37 return -1;
38
c9e9b23c 39 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
abdfb4d0 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",
245a6892 51 $color[4], $name,
c9e9b23c 52 (isset($values['nickname']))?
53 htmlspecialchars($values['nickname']):"",
abdfb4d0 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",
245a6892 59 $color[4], $name,
60 (isset($values["email"]))?
61 htmlspecialchars($values["email"]):"");
abdfb4d0 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",
245a6892 66 $color[4], $name,
67 (isset($values["firstname"]))?
68 htmlspecialchars($values["firstname"]):"");
abdfb4d0 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",
245a6892 73 $color[4], $name,
74 (isset($values["lastname"]))?
75 htmlspecialchars($values["lastname"]):"");
abdfb4d0 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",
245a6892 80 $color[4], $name,
81 (isset($values["label"]))?
82 htmlspecialchars($values["label"]):"");
abdfb4d0 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
c9e9b23c 92 include('../src/load_prefs.php');
abdfb4d0 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
c9e9b23c 102 displayPageHeader($color, 'None');
abdfb4d0 103
104
105 $defdata = array();
c9e9b23c 106 $formerror = '';
abdfb4d0 107 $abortform = false;
108 $showaddrlist = true;
a82f283d 109 $defselected = array();
abdfb4d0 110
111
112 // Handle user's actions
c9e9b23c 113 if($REQUEST_METHOD == 'POST') {
abdfb4d0 114
a82f283d 115 // ***********************************************
116 // Add new address
117 // ***********************************************
c9e9b23c 118 if(!empty($addaddr['nickname'])) {
abdfb4d0 119
b43bdb85 120 $r = $abook->add($addaddr, $abook->localbackend);
abdfb4d0 121
122 // Handle error messages
123 if(!$r) {
124 // Remove backend name from error string
125 $errstr = $abook->error;
c9e9b23c 126 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
abdfb4d0 127
128 $formerror = $errstr;
129 $showaddrlist = false;
b43bdb85 130 $defdata = $addaddr;
abdfb4d0 131 }
132
a82f283d 133 }
abdfb4d0 134
abdfb4d0 135
a82f283d 136 // ***********************************************
137 // Delete address(es)
138 // ***********************************************
b43bdb85 139 else if((!empty($deladdr)) &&
140 sizeof($sel) > 0) {
141 $orig_sel = $sel;
a82f283d 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++) {
c9e9b23c 151 list($sbackend, $snick) = split(':', $sel[$i]);
a82f283d 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;
b43bdb85 182 $defselected = $orig_sel;
a82f283d 183 }
abdfb4d0 184 }
185
a82f283d 186
187 // ***********************************************
188 // Update/modify address
189 // ***********************************************
b43bdb85 190 else if(!empty($editaddr)) {
a82f283d 191
192 // Stage one: Copy data into form
b43bdb85 193 if(sizeof($sel) > 0) {
194 if(sizeof($sel) > 1) {
a82f283d 195 $formerror = _("You can only edit one address at the time");
196 $showaddrlist = true;
b43bdb85 197 $defselected = $sel;
a82f283d 198 } else {
199 $abortform = true;
b43bdb85 200 list($ebackend, $enick) = split(":", $sel[0]);
a82f283d 201 $olddata = $abook->lookup($enick, $ebackend);
202
203 // Display the "new address" form
c9e9b23c 204 print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n";
a82f283d 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";
c9e9b23c 216 print '</FORM>';
a82f283d 217 }
218 }
219
220 // Stage two: Write new data
b43bdb85 221 else if($doedit = 1) {
222 $newdata = $editaddr;
223 $r = $abook->modify($oldnick, $newdata, $backend);
a82f283d 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",
d9fdc8d3 244 htmlspecialchars($oldnick));
a82f283d 245 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
d9fdc8d3 246 htmlspecialchars($backend));
a82f283d 247 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
c9e9b23c 248 print '</FORM>';
a82f283d 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
abdfb4d0 263 // Some times we end output before forms are printed
264 if($abortform) {
265 print "</BODY></HTML>\n";
266 exit();
267 }
abdfb4d0 268 }
269
a82f283d 270
abdfb4d0 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
a82f283d 285
abdfb4d0 286 // Display the address management part
287 if($showaddrlist) {
abdfb4d0 288 // Get and sort address list
289 $alist = $abook->list_addr();
9428553f 290 if(!is_array($alist)) {
291 plain_error_message($abook->error, $color);
292 exit;
293 }
294
abdfb4d0 295 usort($alist,'alistcmp');
a82f283d 296 $prevbackend = -1;
6ed96f54 297 $headerprinted = false;
a82f283d 298
299 // List addresses
9428553f 300 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
fb16d219 301 while(list($undef,$row) = each($alist)) {
a82f283d 302
303 // New table header for each backend
304 if($prevbackend != $row["backend"]) {
305 if($prevbackend >= 0) {
c9e9b23c 306 print '<TR><TD COLSPAN="5" ALIGN=center>';
a82f283d 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
c9e9b23c 316 print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">';
59bfec55 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"));
a82f283d 325 $line = 0;
6ed96f54 326 $headerprinted = true;
a82f283d 327 } // End of header
328
c9e9b23c 329 $prevbackend = $row['backend'];
a82f283d 330
331 // Check if this user is selected
c9e9b23c 332 if(in_array($row['backend'].':'.$row['nickname'], $defselected))
333 $selected = 'CHECKED';
a82f283d 334 else
c9e9b23c 335 $selected = '';
a82f283d 336
337 // Print one row
59bfec55 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";
abdfb4d0 352 $line++;
a82f283d 353 }
354
355 // End of list. Close table.
6ed96f54 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 }
a82f283d 364 } // end of addresslist
365
abdfb4d0 366
367 // Display the "new address" form
d7d3c4d4 368 printf("<FORM ACTION=\"%s\" NAME=f_add METHOD=\"POST\">\n", $PHP_SELF);
abdfb4d0 369 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
370 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
a82f283d 371 printf(_("Add to %s"), $abook->localbackendname);
abdfb4d0 372 print "<STRONG>\n</TD></TR>\n";
373 print "</TABLE>\n";
c9e9b23c 374 address_form('addaddr', _("Add address"), $defdata);
375 print '</FORM>';
abdfb4d0 376
efe94e8e 377 // Add hook for anything that wants on the bottom
378 do_hook("addressbook_bottom");
abdfb4d0 379?>
380
381</BODY></HTML>