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']) {
40 if($a['backend'] < $b['backend']) {
44 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
47 /* Make an input field */
48 function adressbook_inp_field($label, $field, $name, $size, $values, $add) {
50 echo '<TR><TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' .
52 '<TD BGCOLOR="' . $color[4] . '" ALIGN=left>' .
53 '<INPUT NAME="' . $name . '[' . $field . ']" SIZE="' . $size . '" VALUE="';
54 if (isset($values[$field])) {
55 echo htmlspecialchars($values[$field]);
57 echo '">' . $add . '</TD></TR>' . "\n";
60 /* Output form to add and modify address data */
61 function address_form($name, $submittext, $values = array()) {
64 echo '<TABLE BORDER=0 CELLPADDING=1 COLS=2 WIDTH="90%" ALIGN=center>' ."\n";
66 adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
67 '<SMALL>' . _("Must be unique") . '</SMALL>');
68 adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '');
69 adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '');
70 adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '');
71 adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '');
73 echo '<TR><TD COLSPAN=2 BGCOLOR="' . $color[4] . '" ALIGN=center>' . "\n" .
74 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
75 $submittext . '"></TD></TR>' .
80 // Open addressbook, with error messages on but without LDAP (the
81 // second "true"). Don't need LDAP here anyway
82 $abook = addressbook_init(true, true);
83 if($abook->localbackend
== 0) {
85 _("No personal address book is defined. Contact administrator."),
90 displayPageHeader($color, 'None');
97 $defselected = array();
100 // Handle user's actions
101 if($REQUEST_METHOD == 'POST') {
103 // ***********************************************
105 // ***********************************************
106 if(!empty($addaddr['nickname'])) {
108 $r = $abook->add($addaddr, $abook->localbackend
);
110 // Handle error messages
112 // Remove backend name from error string
113 $errstr = $abook->error
;
114 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
116 $formerror = $errstr;
117 $showaddrlist = false;
123 // ***********************************************
124 // Delete address(es)
125 // ***********************************************
126 if((!empty($deladdr)) && sizeof($sel) > 0) {
130 // The selected addresses are identidied by "backend:nickname".
131 // Sort the list and process one backend at the time
136 for($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
137 list($sbackend, $snick) = explode(':', $sel[$i]);
139 // When we get to a new backend, process addresses in
141 if($prevback != $sbackend && $prevback != -1) {
143 $r = $abook->remove($subsel, $prevback);
145 $formerror = $abook->error
;
153 // Queue for processing
154 array_push($subsel, $snick);
155 $prevback = $sbackend;
159 $r = $abook->remove($subsel, $prevback);
160 if(!$r) { // Handle errors
161 $formerror = $abook->error
;
167 $showaddrlist = true;
168 $defselected = $orig_sel;
173 // ***********************************************
174 // Update/modify address
175 // ***********************************************
176 if(!empty($editaddr)) {
178 // Stage one: Copy data into form
179 if (isset($sel) && sizeof($sel) > 0) {
180 if(sizeof($sel) > 1) {
181 $formerror = _("You can only edit one address at the time");
182 $showaddrlist = true;
186 list($ebackend, $enick) = explode(':', $sel[0]);
187 $olddata = $abook->lookup($enick, $ebackend);
189 // Display the "new address" form
190 print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n";
191 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
192 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
193 print _("Update address");
194 print "<STRONG>\n</TD></TR>\n";
196 address_form("editaddr", _("Update address"), $olddata);
197 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
198 htmlspecialchars($olddata["nickname"]));
199 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
200 htmlspecialchars($olddata["backend"]));
201 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
206 // Stage two: Write new data
208 $newdata = $editaddr;
209 $r = $abook->modify($oldnick, $newdata, $backend);
211 // Handle error messages
214 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
215 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
216 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": ".
217 $abook->error
."</FONT>";
218 print "<STRONG>\n</TD></TR>\n";
221 // Display the "new address" form again
222 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
223 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
224 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
225 print _("Update address");
226 print "<STRONG>\n</TD></TR>\n";
228 address_form("editaddr", _("Update address"), $newdata);
229 printf("<INPUT TYPE=hidden NAME=oldnick VALUE=\"%s\">\n",
230 htmlspecialchars($oldnick));
231 printf("<INPUT TYPE=hidden NAME=backend VALUE=\"%s\">\n",
232 htmlspecialchars($backend));
233 print "<INPUT TYPE=hidden NAME=doedit VALUE=1>\n";
240 // Should not get here...
241 plain_error_message(_("Unknown error"), $color);
245 } /* !empty($editaddr) - Update/modify address */
246 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
247 } /* !empty($addaddr['nickname']) - Add new address */
249 // Some times we end output before forms are printed
251 print "</BODY></HTML>\n";
257 // ===================================================================
258 // The following is only executed on a GET request, or on a POST when
259 // a user is added, or when "delete" or "modify" was successful.
260 // ===================================================================
262 // Display error messages
263 if(!empty($formerror)) {
264 print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
265 print "<TR><TD ALIGN=CENTER>\n<br><STRONG>";
266 print "<FONT COLOR=\"$color[2]\">"._("ERROR").": $formerror</FONT>";
267 print "<STRONG>\n</TD></TR>\n";
272 // Display the address management part
274 // Get and sort address list
275 $alist = $abook->list_addr();
276 if(!is_array($alist)) {
277 plain_error_message($abook->error
, $color);
281 usort($alist,'alistcmp');
283 $headerprinted = false;
285 echo "<p align=center><a href=\"#AddAddress\">" .
286 _("Add address") . "</a></p>\n";
289 printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
290 while(list($undef,$row) = each($alist)) {
292 // New table header for each backend
293 if($prevbackend != $row["backend"]) {
294 if($prevbackend >= 0) {
295 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
296 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
298 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
299 _("Delete selected"));
301 print '<TR><TD COLSPAN="5" ALIGN=center>';
302 print " <BR></TD></TR></TABLE>\n";
305 print "<TABLE WIDTH=\"95%\" COLS=1 ALIGN=CENTER>\n";
306 print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
307 print $row["source"];
308 print "<STRONG>\n</TD></TR>\n";
311 print '<TABLE COLS="5" BORDER="0" CELLPADDING="1" CELLSPACING="0" WIDTH="90%" ALIGN="center">';
312 printf('<TR BGCOLOR="%s"><TH ALIGN=left WIDTH="%s"> '.
313 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
314 '<TH ALIGN=left WIDTH="%s">%s<TH ALIGN=left WIDTH="%s">%s'.
315 "</TR>\n", $color[9], "1%",
321 $headerprinted = true;
324 $prevbackend = $row['backend'];
326 // Check if this user is selected
327 if(in_array($row['backend'].':'.$row['nickname'], $defselected)) {
328 $selected = 'CHECKED';
335 (($line %
2) ?
" bgcolor=\"$color[0]\"" : ""));
336 print '<TD VALIGN=top ALIGN=center WIDTH="1%"><SMALL>';
337 printf('<INPUT TYPE=checkbox %s NAME="sel[]" VALUE="%s:%s"></SMALL></TD>',
338 $selected, $row["backend"], $row["nickname"]);
339 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>'.
340 '<TD VALIGN=top NOWRAP WIDTH="%s"> %s </TD>',
341 "1%", $row["nickname"],
343 printf('<TD VALIGN=top NOWRAP WIDTH="%s"> <A HREF="compose.php?send_to=%s">%s</A> </TD>'."\n",
344 "1%", rawurlencode($row["email"]), $row["email"]);
345 printf('<TD VALIGN=top WIDTH="%s"> %s </TD>',
351 // End of list. Close table.
353 print "<TR><TD COLSPAN=5 ALIGN=center>\n";
354 printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
356 printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
357 _("Delete selected"));
358 print "</TR></TABLE></FORM>";
360 } // end of addresslist
363 // Display the "new address" form
364 echo "<a name=\"AddAddress\"></a>\n" .
365 "<FORM ACTION=\"$PHP_SELF\" NAME=f_add METHOD=\"POST\">\n".
366 "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n".
367 "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";
368 printf(_("Add to %s"), $abook->localbackendname
);
369 echo "<STRONG>\n</TD></TR>\n".
371 address_form('addaddr', _("Add address"), $defdata);
374 // Add hook for anything that wants on the bottom
375 do_hook('addressbook_bottom');