6 * Copyright (c) 1999-2001 The Squirrelmail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Manage personal address book.
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 /*****************************************************************/
30 require_once('../src/validate.php');
31 require_once('../functions/array.php');
32 require_once('../functions/display_messages.php');
33 require_once('../functions/addressbook.php');
35 // Sort array by the key "name"
36 function alistcmp($a,$b) {
37 if($a['backend'] > $b['backend'])
39 else if($a['backend'] < $b['backend'])
42 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
45 // Output form to add and modify address data
46 function address_form($name, $submittext, $values = array()) {
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']);
56 " <SMALL>" . _("Must be unique") . "</SMALL></TD></TR>\n";
57 printf("<TR><TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
59 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
60 "<INPUT NAME=\"%s[email]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
62 (isset($values["email"]))?
63 htmlspecialchars($values["email"]):"");
64 printf("<TR><TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
66 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
67 "<INPUT NAME=\"%s[firstname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
69 (isset($values["firstname"]))?
70 htmlspecialchars($values["firstname"]):"");
71 printf("<TR><TD BGCOLOR=\"$color[4]\" ALIGN=RIGHT>%s:</TD>",
73 printf("<TD BGCOLOR=\"%s\" ALIGN=left>".
74 "<INPUT NAME=\"%s[lastname]\" SIZE=45 VALUE=\"%s\"></TD></TR>\n",
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",
83 (isset($values["label"]))?
84 htmlspecialchars($values["label"]):"");
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);
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);
102 displayPageHeader($color, 'None');
108 $showaddrlist = true;
109 $defselected = array();
112 // Handle user's actions
113 if($REQUEST_METHOD == 'POST') {
115 // ***********************************************
117 // ***********************************************
118 if(!empty($addaddr['nickname'])) {
120 $r = $abook->add($addaddr, $abook->localbackend
);
122 // Handle error messages
124 // Remove backend name from error string
125 $errstr = $abook->error
;
126 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
128 $formerror = $errstr;
129 $showaddrlist = false;
136 // ***********************************************
137 // Delete address(es)
138 // ***********************************************
139 else if((!empty($deladdr)) &&
144 // The selected addresses are identidied by "backend:nickname".
145 // Sort the list and process one backend at the time
150 for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
151 list($sbackend, $snick) = explode(':', $sel[$i]);
153 // When we get to a new backend, process addresses in
155 if($prevback != $sbackend && $prevback != -1) {
157 $r = $abook->remove($subsel, $prevback);
159 $formerror = $abook->error
;
167 // Queue for processing
168 array_push($subsel, $snick);
169 $prevback = $sbackend;
173 $r = $abook->remove($subsel, $prevback);
174 if(!$r) { // Handle errors
175 $formerror = $abook->error
;
181 $showaddrlist = true;
182 $defselected = $orig_sel;
187 // ***********************************************
188 // Update/modify address
189 // ***********************************************
190 else if(!empty($editaddr)) {
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;
200 list($ebackend, $enick) = explode(':', $sel[0]);
201 $olddata = $abook->lookup($enick, $ebackend);
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";
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";
220 // Stage two: Write new data
221 else if($doedit = 1) {
222 $newdata = $editaddr;
223 $r = $abook->modify($oldnick, $newdata, $backend);
225 // Handle error messages
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";
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";
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";
254 // Should not get here...
256 plain_error_message(_("Unknown error"), $color);
259 } // End of edit address
263 // Some times we end output before forms are printed
265 print "</BODY></HTML>\n";
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 // ===================================================================
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";
286 // Display the address management part
288 // Get and sort address list
289 $alist = $abook->list_addr();
290 if(!is_array($alist)) {
291 plain_error_message($abook->error
, $color);
295 usort($alist,'alistcmp');
297 $headerprinted = false;
299 echo "<p align=center><a href=\"#AddAddress\">" .
300 _("Add address") . "</a></p>\n";
303 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
304 while(list($undef,$row) = each($alist)) {
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",
312 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
313 _("Delete selected"));
315 print '<TR><TD COLSPAN="5" ALIGN=center>';
316 print " <BR></TD></TR></TABLE>\n";
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";
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"> '.
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%",
335 $headerprinted = true;
338 $prevbackend = $row['backend'];
340 // Check if this user is selected
341 if(in_array($row['backend'].':'.$row['nickname'], $defselected))
342 $selected = 'CHECKED';
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"> %s </TD>'.
353 '<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>',
354 "1%", $row["nickname"],
356 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> <A HREF="compose.php?send_to=%s">%s</A> </TD>'."\n",
357 "1%", rawurlencode($row["email"]), $row["email"]);
358 printf('<TD VALIGN=top WIDTH="%s"> %s </TD>',
364 // End of list. Close table.
366 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
367 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
369 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
370 _("Delete selected"));
371 print "</TR></TABLE></FORM>";
373 } // end of addresslist
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".
384 address_form('addaddr', _("Add address"), $defdata);
387 // Add hook for anything that wants on the bottom
388 do_hook('addressbook_bottom');