5 * Copyright (c) 1999-2004 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * Manage personal address book.
11 * @package squirrelmail
15 * Path for SquirrelMail required files.
18 define('SM_PATH','../');
20 /** SquirrelMail required files. */
21 require_once(SM_PATH
. 'include/validate.php');
22 require_once(SM_PATH
. 'functions/global.php');
23 require_once(SM_PATH
. 'functions/display_messages.php');
24 require_once(SM_PATH
. 'functions/addressbook.php');
25 require_once(SM_PATH
. 'functions/strings.php');
26 require_once(SM_PATH
. 'functions/html.php');
27 require_once(SM_PATH
. 'functions/forms.php');
29 /** lets get the global vars we may need */
30 sqgetGlobalVar('key', $key, SQ_COOKIE
);
32 sqgetGlobalVar('username', $username, SQ_SESSION
);
33 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
34 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
35 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
37 /* From the address form */
38 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
39 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
40 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
41 sqgetGlobalVar('sel', $sel, SQ_POST
);
42 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
43 sqgetGlobalVar('backend', $backend, SQ_POST
);
44 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
46 /* Get sorting order */
47 $abook_sort_order = get_abook_sort();
51 * @param string $label
52 * @param string $field
55 * @param array $values
58 function addressbook_inp_field($label, $field, $name, $size, $values, $add) {
60 $value = ( isset($values[$field]) ?
$values[$field] : '');
62 $td_str = addInput($name.'['.$field.']', $value, $size)
65 return html_tag( 'tr' ,
66 html_tag( 'td', $label . ':', 'right', $color[4]) .
67 html_tag( 'td', $td_str, 'left', $color[4])
73 * Output form to add and modify address data
75 function address_form($name, $submittext, $values = array()) {
76 global $color, $squirrelmail_language;
78 if ($squirrelmail_language == 'ja_JP')
80 echo html_tag( 'table',
81 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
82 ' <small>' . _("Must be unique") . '</small>') .
83 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
84 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
85 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
86 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
87 list_writable_backends($name) .
90 '<input type="submit" name="' . htmlentities($name) . '[SUBMIT]" value="' .
92 'center', $color[4], 'colspan="2"')
94 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
96 echo html_tag( 'table',
97 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
98 ' <small>' . _("Must be unique") . '</small>') .
99 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
100 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
101 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
102 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
103 list_writable_backends($name) .
106 '<input type="submit" name="' . htmlentities($name) . '[SUBMIT]" value="' .
107 $submittext . '" />',
108 'center', $color[4], 'colspan="2"')
110 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
114 function list_writable_backends($name) {
115 global $color, $abook;
116 if ( $name != 'addaddr' ) { return; }
117 if ( $abook->numbackends
> 1 ) {
118 $ret = '<select name="backend">';
119 $backends = $abook->get_backend_list();
120 while (list($undef,$v) = each($backends)) {
122 $ret .= '<option value="' . $v->bnum
;
123 $ret .= '">' . $v->sname
. "</option>\n";
127 return html_tag( 'tr',
128 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
129 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
131 return html_tag( 'tr',
133 addHidden('backend', '1'),
134 'center', $color[4], 'colspan="2"')) . "\n";
138 /* Open addressbook, with error messages on but without LDAP (the *
139 * second "true"). Don't need LDAP here anyway */
140 $abook = addressbook_init(true, true);
141 if($abook->localbackend
== 0) {
143 _("No personal address book is defined. Contact administrator."),
148 displayPageHeader($color, 'None');
153 $showaddrlist = true;
154 $defselected = array();
155 $form_url = 'addressbook.php';
158 /* Handle user's actions */
159 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
161 /**************************************************
163 **************************************************/
164 if (isset($addaddr)) {
165 if (isset($backend)) {
166 $r = $abook->add($addaddr, $backend);
168 $r = $abook->add($addaddr, $abook->localbackend
);
171 /* Handle error messages */
173 /* Remove backend name from error string */
174 $errstr = $abook->error
;
175 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
177 $formerror = $errstr;
178 $showaddrlist = false;
183 /************************************************
184 * Delete address(es) *
185 ************************************************/
186 if ((!empty($deladdr)) && sizeof($sel) > 0) {
190 /* The selected addresses are identidied by "backend:nickname". *
191 * Sort the list and process one backend at the time */
196 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
197 list($sbackend, $snick) = explode(':', $sel[$i]);
199 /* When we get to a new backend, process addresses in *
201 if ($prevback != $sbackend && $prevback != -1) {
203 $r = $abook->remove($subsel, $prevback);
205 $formerror = $abook->error
;
213 /* Queue for processing */
214 array_push($subsel, $snick);
215 $prevback = $sbackend;
219 $r = $abook->remove($subsel, $prevback);
220 if (!$r) { /* Handle errors */
221 $formerror = $abook->error
;
227 $showaddrlist = true;
228 $defselected = $orig_sel;
233 /***********************************************
234 * Update/modify address *
235 ***********************************************/
236 if (!empty($editaddr)) {
238 /* Stage one: Copy data into form */
239 if (isset($sel) && sizeof($sel) > 0) {
240 if(sizeof($sel) > 1) {
241 $formerror = _("You can only edit one address at the time");
242 $showaddrlist = true;
246 list($ebackend, $enick) = explode(':', $sel[0]);
247 $olddata = $abook->lookup($enick, $ebackend);
249 /* Display the "new address" form */
250 echo '<form action="' . $form_url . '" method="post">' .
255 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
256 'center', $color[0] )
258 'center', '', 'width="100%" ' );
259 address_form("editaddr", _("Update address"), $olddata);
260 echo addHidden('oldnick', $olddata['nickname']).
261 addHidden('backend', $olddata['backend']).
262 addHidden('doedit', '1').
267 /* Stage two: Write new data */
269 $newdata = $editaddr;
270 $r = $abook->modify($oldnick, $newdata, $backend);
272 /* Handle error messages */
275 echo html_tag( 'table',
278 "\n". '<strong><font color="' . $color[2] .
279 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
282 'center', '', 'width="100%"' );
284 /* Display the "new address" form again */
285 echo '<form action="' . $form_url .
286 '" method="post">' . "\n" .
290 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
291 'center', $color[0] )
293 'center', '', 'width="100%"' );
294 address_form("editaddr", _("Update address"), $newdata);
296 addHidden('oldnick', $oldnick).
297 addHidden('backend', $backend).
298 addHidden('doedit', '1').
304 /* Should not get here... */
305 plain_error_message(_("Unknown error"), $color);
309 } /* !empty($editaddr) - Update/modify address */
310 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
311 } /* !empty($addaddr['nickname']) - Add new address */
313 // Some times we end output before forms are printed
315 echo "</body></html>\n";
321 /* =================================================================== *
322 * The following is only executed on a GET request, or on a POST when *
323 * a user is added, or when "delete" or "modify" was successful. *
324 * =================================================================== */
326 /* Display error messages */
327 if (!empty($formerror)) {
328 echo html_tag( 'table',
331 "\n". '<br /><strong><font color="' . $color[2] .
332 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
335 'center', '', 'width="100%"' );
339 /* Display the address management part */
341 /* Get and sort address list */
342 $alist = $abook->list_addr();
343 if(!is_array($alist)) {
344 plain_error_message($abook->error
, $color);
348 usort($alist,'alistcmp');
350 $headerprinted = false;
352 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
355 if (count($alist) > 0) {
356 echo '<form action="' . $form_url . '" method="post">' . "\n";
357 while(list($undef,$row) = each($alist)) {
359 /* New table header for each backend */
360 if($prevbackend != $row['backend']) {
361 if($prevbackend < 0) {
362 echo html_tag( 'table',
365 '<input type="submit" name="editaddr" value="' .
366 _("Edit selected") . "\" />\n" .
367 '<input type="submit" name="deladdr" value="' .
368 _("Delete selected") . "\" />\n",
369 'center', '', 'colspan="5"' )
372 html_tag( 'td', ' <br />', 'center', '', 'colspan="5"' )
375 echo "\n<!-- start of address book table -->\n" .
376 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
377 html_tag( 'tr', "\n" .
378 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . "\n" .
379 html_tag( 'th', _("Nickname") .
380 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1)
381 , 'left', '', 'width="1%"' ) . "\n" .
382 html_tag( 'th', _("Name") .
383 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3)
384 , 'left', '', 'width="1%"' ) . "\n" .
385 html_tag( 'th', _("E-mail") .
386 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5)
387 , 'left', '', 'width="1%"' ) . "\n" .
388 html_tag( 'th', _("Info") .
389 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7)
390 , 'left', '', 'width="1%"' ) . "\n",
391 '', $color[9] ) . "\n";
394 // Separate different backends with <hr />
395 if($prevbackend > 0) {
397 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
401 // Print backend name
403 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
407 $headerprinted = true;
408 } /* End of header */
410 $prevbackend = $row['backend'];
412 /* Check if this user is selected */
413 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
415 /* Print one row, with alternating color */
417 $tr_bgcolor = $color[12];
419 $tr_bgcolor = $color[4];
421 if ($squirrelmail_language == 'ja_JP')
423 echo html_tag( 'tr', '', '', $tr_bgcolor);
424 if ($abook->backends
[$row['backend']]->writeable
) {
427 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
429 'center', '', 'valign="top" width="1%"' );
433 'center', '', 'valign="top" width="1%"' );
435 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
436 html_tag( 'td', ' ' . $row['lastname'] . ' ' . $row['firstname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
437 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
439 echo html_tag( 'tr', '', '', $tr_bgcolor);
440 if ($abook->backends
[$row['backend']]->writeable
) {
443 '<input type="checkbox" ' . $selected . ' name="sel[]" value="' .
444 $row['backend'] . ':' . $row['nickname'] . '" /></small>' ,
445 'center', '', 'valign="top" width="1%"' );
449 'center', '', 'valign="top" width="1%"' );
451 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
452 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
453 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
455 $email = $abook->full_address($row);
456 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
457 htmlspecialchars($row['email']) ) .
459 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
464 "\n<!-- end of address book table -->\n";
466 /* End of list. Add edit/delete select buttons */
467 if ($headerprinted) {
468 echo html_tag( 'table',
471 '<input type="submit" name="editaddr" value="' . _("Edit selected") .
473 '<input type="submit" name="deladdr" value="' . _("Delete selected") .
475 'center', '', 'colspan="5"' )
481 } /* end of addresslist */
484 /* Display the "new address" form */
485 echo '<a name="AddAddress"></a>' . "\n" .
486 '<form action="' . $form_url . '" name="f_add" method="post">' . "\n" .
489 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
493 , 'center', '', 'width="100%"' ) ."\n";
494 address_form('addaddr', _("Add address"), $defdata);
497 /* Add hook for anything that wants on the bottom */
498 do_hook('addressbook_bottom');