6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Manage personal address book.
12 * @package squirrelmail
16 * Path for SquirrelMail required files.
19 define('SM_PATH','../');
21 /** SquirrelMail required files. */
22 require_once(SM_PATH
. 'include/validate.php');
23 require_once(SM_PATH
. 'functions/global.php');
24 require_once(SM_PATH
. 'functions/display_messages.php');
25 require_once(SM_PATH
. 'functions/addressbook.php');
26 require_once(SM_PATH
. 'functions/strings.php');
27 require_once(SM_PATH
. 'functions/html.php');
28 require_once(SM_PATH
. 'functions/forms.php');
30 /** lets get the global vars we may need */
31 sqgetGlobalVar('key', $key, SQ_COOKIE
);
33 sqgetGlobalVar('username', $username, SQ_SESSION
);
34 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
35 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
36 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
38 /* From the address form */
39 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
40 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
41 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
42 sqgetGlobalVar('sel', $sel, SQ_POST
);
43 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
44 sqgetGlobalVar('backend', $backend, SQ_POST
);
45 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
47 /* Get sorting order */
48 $abook_sort_order = get_abook_sort();
52 * @param string $label
53 * @param string $field
56 * @param array $values
59 function addressbook_inp_field($label, $field, $name, $size, $values, $add) {
61 $value = ( isset($values[$field]) ?
$values[$field] : '');
63 $td_str = addInput($name.'['.$field.']', $value, $size)
66 return html_tag( 'tr' ,
67 html_tag( 'td', $label . ':', 'right', $color[4]) .
68 html_tag( 'td', $td_str, 'left', $color[4])
74 * Output form to add and modify address data
76 function address_form($name, $submittext, $values = array()) {
77 global $color, $squirrelmail_language;
79 if ($squirrelmail_language == 'ja_JP')
81 echo html_tag( 'table',
82 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
83 ' <small>' . _("Must be unique") . '</small>') .
84 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
85 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
86 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
87 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
88 list_writable_backends($name) .
91 '<input type="submit" name="' . htmlentities($name) . '[SUBMIT]" value="' .
93 'center', $color[4], 'colspan="2"')
95 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
97 echo html_tag( 'table',
98 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
99 ' <small>' . _("Must be unique") . '</small>') .
100 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
101 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
102 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
103 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
104 list_writable_backends($name) .
107 '<input type="submit" name="' . htmlentities($name) . '[SUBMIT]" value="' .
108 $submittext . '" />',
109 'center', $color[4], 'colspan="2"')
111 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
115 function list_writable_backends($name) {
116 global $color, $abook;
117 if ( $name != 'addaddr' ) { return; }
118 if ( $abook->numbackends
> 1 ) {
119 $ret = '<select name="backend">';
120 $backends = $abook->get_backend_list();
121 while (list($undef,$v) = each($backends)) {
123 $ret .= '<option value="' . $v->bnum
;
124 $ret .= '">' . $v->sname
. "</option>\n";
128 return html_tag( 'tr',
129 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
130 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
132 return html_tag( 'tr',
134 addHidden('backend', '1'),
135 'center', $color[4], 'colspan="2"')) . "\n";
139 /* Open addressbook, with error messages on but without LDAP (the *
140 * second "true"). Don't need LDAP here anyway */
141 $abook = addressbook_init(true, true);
142 if($abook->localbackend
== 0) {
144 _("No personal address book is defined. Contact administrator."),
149 displayPageHeader($color, 'None');
154 $showaddrlist = true;
155 $defselected = array();
156 $form_url = 'addressbook.php';
159 /* Handle user's actions */
160 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
162 /**************************************************
164 **************************************************/
165 if (isset($addaddr)) {
166 if (isset($backend)) {
167 $r = $abook->add($addaddr, $backend);
169 $r = $abook->add($addaddr, $abook->localbackend
);
172 /* Handle error messages */
174 /* Remove backend name from error string */
175 $errstr = $abook->error
;
176 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
178 $formerror = $errstr;
179 $showaddrlist = false;
184 /************************************************
185 * Delete address(es) *
186 ************************************************/
187 if ((!empty($deladdr)) && sizeof($sel) > 0) {
191 /* The selected addresses are identidied by "backend:nickname". *
192 * Sort the list and process one backend at the time */
197 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
198 list($sbackend, $snick) = explode(':', $sel[$i]);
200 /* When we get to a new backend, process addresses in *
202 if ($prevback != $sbackend && $prevback != -1) {
204 $r = $abook->remove($subsel, $prevback);
206 $formerror = $abook->error
;
214 /* Queue for processing */
215 array_push($subsel, $snick);
216 $prevback = $sbackend;
220 $r = $abook->remove($subsel, $prevback);
221 if (!$r) { /* Handle errors */
222 $formerror = $abook->error
;
228 $showaddrlist = true;
229 $defselected = $orig_sel;
234 /***********************************************
235 * Update/modify address *
236 ***********************************************/
237 if (!empty($editaddr)) {
239 /* Stage one: Copy data into form */
240 if (isset($sel) && sizeof($sel) > 0) {
241 if(sizeof($sel) > 1) {
242 $formerror = _("You can only edit one address at the time");
243 $showaddrlist = true;
247 list($ebackend, $enick) = explode(':', $sel[0]);
248 $olddata = $abook->lookup($enick, $ebackend);
250 /* Display the "new address" form */
251 echo '<form action="' . $form_url . '" method="post">' .
256 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
257 'center', $color[0] )
259 'center', '', 'width="100%" ' );
260 address_form("editaddr", _("Update address"), $olddata);
261 echo addHidden('oldnick', $olddata['nickname']).
262 addHidden('backend', $olddata['backend']).
263 addHidden('doedit', '1').
268 /* Stage two: Write new data */
270 $newdata = $editaddr;
271 $r = $abook->modify($oldnick, $newdata, $backend);
273 /* Handle error messages */
276 echo html_tag( 'table',
279 "\n". '<strong><font color="' . $color[2] .
280 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
283 'center', '', 'width="100%"' );
285 /* Display the "new address" form again */
286 echo '<form action="' . $form_url .
287 '" method="post">' . "\n" .
291 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
292 'center', $color[0] )
294 'center', '', 'width="100%"' );
295 address_form("editaddr", _("Update address"), $newdata);
297 addHidden('oldnick', $oldnick).
298 addHidden('backend', $backend).
299 addHidden('doedit', '1').
305 /* Should not get here... */
306 plain_error_message(_("Unknown error"), $color);
310 } /* !empty($editaddr) - Update/modify address */
311 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
312 } /* !empty($addaddr['nickname']) - Add new address */
314 // Some times we end output before forms are printed
316 echo "</body></html>\n";
322 /* =================================================================== *
323 * The following is only executed on a GET request, or on a POST when *
324 * a user is added, or when "delete" or "modify" was successful. *
325 * =================================================================== */
327 /* Display error messages */
328 if (!empty($formerror)) {
329 echo html_tag( 'table',
332 "\n". '<br /><strong><font color="' . $color[2] .
333 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
336 'center', '', 'width="100%"' );
340 /* Display the address management part */
342 /* Get and sort address list */
343 $alist = $abook->list_addr();
344 if(!is_array($alist)) {
345 plain_error_message($abook->error
, $color);
349 usort($alist,'alistcmp');
351 $headerprinted = false;
353 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
356 if (count($alist) > 0) {
357 echo '<form action="' . $form_url . '" method="post">' . "\n";
358 while(list($undef,$row) = each($alist)) {
360 /* New table header for each backend */
361 if($prevbackend != $row['backend']) {
362 if($prevbackend < 0) {
363 echo html_tag( 'table',
366 '<input type="submit" name="editaddr" value="' .
367 _("Edit selected") . "\" />\n" .
368 '<input type="submit" name="deladdr" value="' .
369 _("Delete selected") . "\" />\n",
370 'center', '', 'colspan="5"' )
373 html_tag( 'td', ' <br />', 'center', '', 'colspan="5"' )
376 echo "\n<!-- start of address book table -->\n" .
377 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
378 html_tag( 'tr', "\n" .
379 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . "\n" .
380 html_tag( 'th', _("Nickname") .
381 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
382 'left', '', 'width="1%"' ) . "\n" .
383 html_tag( 'th', _("Name") .
384 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
385 'left', '', 'width="1%"' ) . "\n" .
386 html_tag( 'th', _("E-mail") .
387 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
388 'left', '', 'width="1%"' ) . "\n" .
389 html_tag( 'th', _("Info") .
390 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
391 'left', '', 'width="1%"' ) . "\n",
392 '', $color[9] ) . "\n";
395 // Separate different backends with <hr />
396 if($prevbackend > 0) {
398 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
402 // Print backend name
404 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
408 $headerprinted = true;
409 } /* End of header */
411 $prevbackend = $row['backend'];
413 /* Check if this user is selected */
414 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
416 /* Print one row, with alternating color */
418 $tr_bgcolor = $color[12];
420 $tr_bgcolor = $color[4];
422 if ($squirrelmail_language == 'ja_JP')
424 echo html_tag( 'tr', '', '', $tr_bgcolor);
425 if ($abook->backends
[$row['backend']]->writeable
) {
428 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
430 'center', '', 'valign="top" width="1%"' );
434 'center', '', 'valign="top" width="1%"' );
436 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
437 html_tag( 'td', ' ' . $row['lastname'] . ' ' . $row['firstname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
438 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
440 echo html_tag( 'tr', '', '', $tr_bgcolor);
441 if ($abook->backends
[$row['backend']]->writeable
) {
444 '<input type="checkbox" ' . $selected . ' name="sel[]" value="' .
445 $row['backend'] . ':' . $row['nickname'] . '" /></small>' ,
446 'center', '', 'valign="top" width="1%"' );
450 'center', '', 'valign="top" width="1%"' );
452 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
453 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
454 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
456 $email = $abook->full_address($row);
457 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
458 htmlspecialchars($row['email']) ) .
460 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
465 "\n<!-- end of address book table -->\n";
467 /* End of list. Add edit/delete select buttons */
468 if ($headerprinted) {
469 echo html_tag( 'table',
472 '<input type="submit" name="editaddr" value="' . _("Edit selected") .
474 '<input type="submit" name="deladdr" value="' . _("Delete selected") .
476 'center', '', 'colspan="5"' )
482 } /* end of addresslist */
485 /* Display the "new address" form */
486 echo '<a name="AddAddress"></a>' . "\n" .
487 '<form action="' . $form_url . '" name="f_add" method="post">' . "\n" .
490 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
494 , 'center', '', 'width="100%"' ) ."\n";
495 address_form('addaddr', _("Add address"), $defdata);
498 /* Add hook for anything that wants on the bottom */
499 do_hook('addressbook_bottom');