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