6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Manage personal address book.
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 require_once('../functions/strings.php');
19 require_once('../functions/html.php');
21 /* Make an input field */
22 function adressbook_inp_field($label, $field, $name, $size, $values, $add) {
24 $td_str = '<INPUT NAME="' . $name . '[' . $field . ']" SIZE="' . $size . '" VALUE="';
25 if (isset($values[$field])) {
26 $td_str .= htmlspecialchars($values[$field]);
28 $td_str .= '">' . $add . '';
29 return html_tag( 'tr' ,
30 html_tag( 'td', $label . ':', 'right', $color[4]) .
31 html_tag( 'td', $td_str, 'left', $color[4])
36 /* Output form to add and modify address data */
37 function address_form($name, $submittext, $values = array()) {
39 echo html_tag( 'table',
40 adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
41 '<SMALL>' . _("Must be unique") . '</SMALL>') .
42 adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
43 adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
44 adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
45 adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
48 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
50 'center', $color[4], 'colspan="2"')
52 , 'center', '', 'border="0" cellpadding="1" cols="2" width="90%"') ."\n";
56 /* Open addressbook, with error messages on but without LDAP (the *
57 * second "true"). Don't need LDAP here anyway */
58 $abook = addressbook_init(true, true);
59 if($abook->localbackend
== 0) {
61 _("No personal address book is defined. Contact administrator."),
66 displayPageHeader($color, 'None');
73 $defselected = array();
76 /* Handle user's actions */
77 if($REQUEST_METHOD == 'POST') {
79 /**************************************************
81 **************************************************/
82 if (!empty($addaddr['nickname'])) {
84 $r = $abook->add($addaddr, $abook->localbackend
);
86 /* Handle error messages */
88 /* Remove backend name from error string */
89 $errstr = $abook->error
;
90 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
93 $showaddrlist = false;
99 /************************************************
100 * Delete address(es) *
101 ************************************************/
102 if ((!empty($deladdr)) && sizeof($sel) > 0) {
106 /* The selected addresses are identidied by "backend:nickname". *
107 * Sort the list and process one backend at the time */
112 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
113 list($sbackend, $snick) = explode(':', $sel[$i]);
115 /* When we get to a new backend, process addresses in *
117 if ($prevback != $sbackend && $prevback != -1) {
119 $r = $abook->remove($subsel, $prevback);
121 $formerror = $abook->error
;
129 /* Queue for processing */
130 array_push($subsel, $snick);
131 $prevback = $sbackend;
135 $r = $abook->remove($subsel, $prevback);
136 if (!$r) { /* Handle errors */
137 $formerror = $abook->error
;
143 $showaddrlist = true;
144 $defselected = $orig_sel;
149 /***********************************************
150 * Update/modify address *
151 ***********************************************/
152 if (!empty($editaddr)) {
154 /* Stage one: Copy data into form */
155 if (isset($sel) && sizeof($sel) > 0) {
156 if(sizeof($sel) > 1) {
157 $formerror = _("You can only edit one address at the time");
158 $showaddrlist = true;
162 list($ebackend, $enick) = explode(':', $sel[0]);
163 $olddata = $abook->lookup($enick, $ebackend);
165 /* Display the "new address" form */
166 echo '<FORM ACTION="' . $PHP_SELF . '" METHOD="POST">' .
171 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
172 'center', $color[0] )
174 'center', '', 'width="100%" cols="1"' ) .
175 address_form("editaddr", _("Update address"), $olddata);
176 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
177 htmlspecialchars($olddata["nickname"]) . "\">\n" .
178 '<INPUT TYPE=hidden NAME=backend VALUE="' .
179 htmlspecialchars($olddata["backend"]) . "\">\n" .
180 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" .
185 /* Stage two: Write new data */
187 $newdata = $editaddr;
188 $r = $abook->modify($oldnick, $newdata, $backend);
190 /* Handle error messages */
193 echo html_tag( 'table',
196 "\n". '<br><strong><font color="' . $color[2] .
197 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
200 'center', '', 'width="100%" cols="1"' );
202 /* Display the "new address" form again */
203 echo '<FORM ACTION="' . $PHP_SELF .
204 '" METHOD="POST">' . "\n" .
208 "\n". '<br><strong>' . _("Update address") . '</strong>' ."\n",
209 'center', $color[0] )
211 'center', '', 'width="100%" cols="1"' ) .
212 address_form("editaddr", _("Update address"), $newdata);
213 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
214 htmlspecialchars($oldnick) . "\">\n" .
215 '<INPUT TYPE=hidden NAME=backend VALUE="' .
216 htmlspecialchars($backend) . "\">\n" .
217 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' .
223 /* Should not get here... */
224 plain_error_message(_("Unknown error"), $color);
228 } /* !empty($editaddr) - Update/modify address */
229 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
230 } /* !empty($addaddr['nickname']) - Add new address */
232 // Some times we end output before forms are printed
234 echo "</BODY></HTML>\n";
240 /* =================================================================== *
241 * The following is only executed on a GET request, or on a POST when *
242 * a user is added, or when "delete" or "modify" was successful. *
243 * =================================================================== */
245 /* Display error messages */
246 if (!empty($formerror)) {
247 echo html_tag( 'table',
250 "\n". '<br><strong><font color="' . $color[2] .
251 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
254 'center', '', 'width="100%" cols="1"' );
258 /* Display the address management part */
260 /* Get and sort address list */
261 $alist = $abook->list_addr();
262 if(!is_array($alist)) {
263 plain_error_message($abook->error
, $color);
267 usort($alist,'alistcmp');
269 $headerprinted = false;
271 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
274 if (count($alist) > 0) {
275 echo '<FORM ACTION="' . $PHP_SELF . '" METHOD="POST">' . "\n";
276 while(list($undef,$row) = each($alist)) {
278 /* New table header for each backend */
279 if($prevbackend != $row['backend']) {
280 if($prevbackend < 0) {
281 echo html_tag( 'table',
284 '<INPUT TYPE=submit NAME=editaddr VALUE="' .
285 _("Edit selected") . "\">\n" .
286 '<INPUT TYPE=submit NAME=deladdr VALUE="' .
287 _("Delete selected") . "\">\n",
288 'center', '', 'colspan="5"' )
291 html_tag( 'td', ' <br>', 'center', '', 'colspan="5"' )
296 echo html_tag( 'table',
298 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] )
300 'center', '', 'width="95%" cols="1"' ) ."\n"
301 . html_tag( 'table', '', 'center', '', 'cols="5" border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
302 html_tag( 'tr', "\n" .
303 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) .
304 html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) .
305 html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) .
306 html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) .
307 html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) ,
308 '', $color[9] ) . "\n";
311 $headerprinted = true;
312 } /* End of header */
314 $prevbackend = $row['backend'];
316 /* Check if this user is selected */
317 if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) {
318 $selected = 'CHECKED';
325 if ($line %
2) { $tr_bgcolor = $color[0]; }
326 echo html_tag( 'tr', '') .
329 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
330 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
331 'center', '', 'valign="top" width="1%"' ) .
332 html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
333 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
334 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
335 $email = $abook->full_address($row);
336 if ($compose_new_win == '1') {
337 echo '<a href="javascript:void(0)" onclick=comp_in_new(false,"compose.php?send_to='.rawurlencode($email).'")>';
340 echo '<A HREF="compose.php?send_to=' . rawurlencode($email).'">';
342 echo htmlspecialchars($row['email']) . '</A> </td>'."\n".
343 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
348 /* End of list. Close table. */
349 if ($headerprinted) {
352 '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
354 '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
356 'center', '', 'colspan="5"' )
359 echo '</table></FORM>';
361 } /* end of addresslist */
364 /* Display the "new address" form */
365 echo '<a name="AddAddress"></a>' . "\n" .
366 '<FORM ACTION="' . $PHP_SELF . '" NAME=f_add METHOD="POST">' . "\n" .
369 html_tag( 'td', "\n". '<strong>' . sprintf(_("Add to %s"), $abook->localbackendname
) . '</strong>' . "\n",
373 , 'center', '', 'width="100%" cols="1"' ) ."\n";
374 address_form('addaddr', _("Add address"), $defdata);
377 /* Add hook for anything that wants on the bottom */
378 do_hook('addressbook_bottom');