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