File Formatting
[squirrelmail.git] / src / addressbook.php
CommitLineData
abdfb4d0 1<?php
895905c0 2
35586184 3/**
4 * addressbook.php
5 *
6 * Copyright (c) 1999-2001 The Squirrelmail Development 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/*****************************************************************/
15/*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
16/*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
17/*** + Base level indent should begin at left margin, as ***/
18/*** the require_once below looks. ***/
19/*** + All identation should consist of four space blocks ***/
20/*** + Tab characters are evil. ***/
21/*** + all comments should use "slash-star ... star-slash" ***/
22/*** style -- no pound characters, no slash-slash style ***/
23/*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
24/*** ALWAYS USE { AND } CHARACTERS!!! ***/
25/*** + Please use ' instead of ", when possible. Note " ***/
26/*** should always be used in _( ) function calls. ***/
27/*** Thank you for your help making the SM code more readable. ***/
28/*****************************************************************/
29
30require_once('../src/validate.php');
31require_once('../functions/array.php');
32require_once('../functions/display_messages.php');
33require_once('../functions/addressbook.php');
ffd8224c 34
35 // Sort array by the key "name"
36 function alistcmp($a,$b) {
37 if($a['backend'] > $b['backend'])
38 return 1;
39 else if($a['backend'] < $b['backend'])
40 return -1;
41
42 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
43 }
44
45 // Output form to add and modify address data
46 function address_form($name, $submittext, $values = array()) {
47 global $color;
48 echo "<TABLE BORDER=0 CELLPADDING=1 COLS=2 WIDTH=\"90%\" ALIGN=center>\n" .
49 "<TR><TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>" .
50 _("Nickname") . ' : </TD>'.
51 "<TD BGCOLOR=\"$color[4]\" ALIGN=left>".
52 "<INPUT NAME=\"$name[nickname]\" SIZE=15 VALUE=\"";
53 if (isset($values['nickname']))
54 echo htmlspecialchars($values['nickname']);
55 echo '">'.
56 "&nbsp;<SMALL>" . _("Must be unique") . "</SMALL></TD></TR>\n";
57 printf("<TR><TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
58 _("E-mail address"));
59 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
60 "<INPUT NAME=\"%s[email]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
61 $color[4], $name,
62 (isset($values["email"]))?
63 htmlspecialchars($values["email"]):"");
64 printf("<TR><TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
65 _("First name"));
66 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
67 "<INPUT NAME=\"%s[firstname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
68 $color[4], $name,
69 (isset($values["firstname"]))?
70 htmlspecialchars($values["firstname"]):"");
71 printf("<TR><TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
72 _("Last name"));
73 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
74 "<INPUT NAME=\"%s[lastname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
75 $color[4], $name,
76 (isset($values["lastname"]))?
77 htmlspecialchars($values["lastname"]):"");
78 printf("<TR><TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
79 _("Additional info"));
80 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
81 "<INPUT NAME=\"%s[label]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
82 $color[4], $name,
83 (isset($values["label"]))?
84 htmlspecialchars($values["label"]):"");
85
86 printf("<TR><TD COLSPAN=2 BGCOLOR=\"%s\" ALIGN=center>\n".
87 "<INPUT TYPE=submit NAME=\"%s[SUBMIT]\" VALUE=\"%s\"></TD></TR>\n",
88 $color[4], $name, $submittext);
89
90 print "</TABLE>\n";
91 }
92
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) = explode(':', $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 (isset($sel) && 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) = explode(':', $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 echo "<p align=center><a href=\"#AddAddress\">" .
300 _("Add address") . "</a></p>\n";
301
302 // List addresses
303 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
304 while(list($undef,$row) = each($alist)) {
305
306 // New table header for each backend
307 if($prevbackend != $row["backend"]) {
308 if($prevbackend >= 0) {
309 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
310 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
311 _("Edit selected"));
312 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
313 _("Delete selected"));
314 echo "</tr>\n";
315 print '<TR><TD COLSPAN="5" ALIGN=center>';
316 print "&nbsp;<BR></TD></TR></TABLE>\n";
317 }
318
319 print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n";
320 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
321 print $row["source"];
322 print "<STRONG>\n</TD></TR>\n";
323 print "</TABLE>\n";
324
325 print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">';
326 printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s">&nbsp;'.
327 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
328 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
329 "</TR>\n", $color[9], "1%",
330 "1%", _("Nickname"),
331 "1%", _("Name"),
332 "1%", _("E-mail"),
333 "%", _("Info"));
334 $line = 0;
335 $headerprinted = true;
336 } // End of header
337
338 $prevbackend = $row['backend'];
339
340 // Check if this user is selected
341 if(in_array($row['backend'].':'.$row['nickname'], $defselected))
342 $selected = 'CHECKED';
343 else
344 $selected = '';
345
346 // Print one row
347 printf("<TR%s>",
348 (($line % 2) ? " bgcolor=\"$color[0]\"" : ""));
349 print '<TD VALIGN=top ALIGN=center WIDTH="1%"><SMALL>';
350 printf('<INPUT TYPE=checkbox %s NAME="sel[]" VALUE="%s:%s"></SMALL></TD>',
351 $selected, $row["backend"], $row["nickname"]);
352 printf('<TD VALIGN=top NOWRAP WIDTH="%s">&nbsp;%s&nbsp;</TD>'.
353 '<TD VALIGN=top NOWRAP WIDTH="%s">&nbsp;%s&nbsp;</TD>',
354 "1%", $row["nickname"],
355 "1%", $row["name"]);
356 printf('<TD VALIGN=top NOWRAP WIDTH="%s">&nbsp;<A HREF="compose.php?send_to=%s">%s</A>&nbsp;</TD>'."\n",
357 "1%", rawurlencode($row["email"]), $row["email"]);
358 printf('<TD VALIGN=top WIDTH="%s">&nbsp;%s&nbsp;</TD>',
359 "%", $row["label"]);
360 print "</TR>\n";
361 $line++;
362 }
363
364 // End of list. Close table.
365 if($headerprinted) {
366 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
367 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
368 _("Edit selected"));
369 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
370 _("Delete selected"));
371 print "</TR></TABLE></FORM>";
372 }
373 } // end of addresslist
374
375
376 // Display the "new address" form
377 echo "<a name=\"AddAddress\"></a>\n" .
378 "<FORM ACTION=\"$PHP_SELF\" NAME=f_add METHOD=\"POST\">\n".
379 "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n".
380 "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
381 printf(_("Add to %s"), $abook->localbackendname);
382 echo "<STRONG>\n</TD></TR>\n".
383 "</TABLE>\n";
384 address_form('addaddr', _("Add address"), $defdata);
385 echo '</FORM>';
386
387 // Add hook for anything that wants on the bottom
388 do_hook('addressbook_bottom');
abdfb4d0 389?>
390
35586184 391</BODY></HTML>