adding list sort options. no sort indicator yet.
[squirrelmail.git] / src / addressbook.php
CommitLineData
abdfb4d0 1<?php
895905c0 2
35586184 3/**
4 * addressbook.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Manage personal address book.
10 *
30967a1e 11 * @version $Id$
8f6f9ba5 12 * @package squirrelmail
35586184 13 */
14
30967a1e 15/**
16 * Path for SquirrelMail required files.
17 * @ignore
18 */
86725763 19define('SM_PATH','../');
20
8f6f9ba5 21/** SquirrelMail required files. */
08185f2a 22require_once(SM_PATH . 'include/validate.php');
1e12d1ff 23require_once(SM_PATH . 'functions/global.php');
86725763 24require_once(SM_PATH . 'functions/display_messages.php');
25require_once(SM_PATH . 'functions/addressbook.php');
26require_once(SM_PATH . 'functions/strings.php');
27require_once(SM_PATH . 'functions/html.php');
19347763 28require_once(SM_PATH . 'functions/forms.php');
ffd8224c 29
8f6f9ba5 30/** lets get the global vars we may need */
1e12d1ff 31sqgetGlobalVar('key', $key, SQ_COOKIE);
0b97a708 32
1e12d1ff 33sqgetGlobalVar('username', $username, SQ_SESSION);
34sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
35sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
36sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
0b97a708 37
38/* From the address form */
1e12d1ff 39sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
40sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
41sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
42sqgetGlobalVar('sel', $sel, SQ_POST);
43sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
44sqgetGlobalVar('backend', $backend, SQ_POST);
45sqgetGlobalVar('doedit', $doedit, SQ_POST);
0b97a708 46
8f6f9ba5 47/**
48 * Make an input field
49 * @param string $label
50 * @param string $field
51 * @param string $name
52 * @param string $size
53 * @param array $values
54 * @param string $add
55 */
19347763 56function addressbook_inp_field($label, $field, $name, $size, $values, $add) {
daba719e 57 global $color;
19347763 58 $value = ( isset($values[$field]) ? $values[$field] : '');
59
60 $td_str = addInput($name.'['.$field.']', $value, $size)
61 . $add ;
62
ac987a56 63 return html_tag( 'tr' ,
64 html_tag( 'td', $label . ':', 'right', $color[4]) .
65 html_tag( 'td', $td_str, 'left', $color[4])
66 )
67 . "\n";
daba719e 68}
ffd8224c 69
8f6f9ba5 70/**
71 * Output form to add and modify address data
72 */
daba719e 73function address_form($name, $submittext, $values = array()) {
e842b215 74 global $color, $squirrelmail_language;
c6554ec0 75
e842b215 76 if ($squirrelmail_language == 'ja_JP')
77 {
78 echo html_tag( 'table',
19347763 79 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
e842b215 80 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
19347763 81 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
82 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
83 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
84 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
08188c25 85 list_writable_backends($name) .
e842b215 86 html_tag( 'tr',
87 html_tag( 'td',
19347763 88 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
e842b215 89 $submittext . '">',
90 'center', $color[4], 'colspan="2"')
91 )
92 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
93 } else {
ac987a56 94 echo html_tag( 'table',
19347763 95 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
c6554ec0 96 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
19347763 97 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
98 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
99 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
100 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
08188c25 101 list_writable_backends($name) .
ac987a56 102 html_tag( 'tr',
103 html_tag( 'td',
19347763 104 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
ac987a56 105 $submittext . '">',
106 'center', $color[4], 'colspan="2"')
107 )
ac50138c 108 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
daba719e 109}
e842b215 110}
ffd8224c 111
08188c25 112function list_writable_backends($name) {
113 global $color, $abook;
114 if ( $name != 'addaddr' ) { return; }
115 if ( $abook->numbackends > 1 ) {
116 $ret = "<select name=backend>";
117 $backends = $abook->get_backend_list();
118 while (list($undef,$v) = each($backends)) {
119 if ($v->writeable) {
120 $ret .= '<OPTION VALUE=' . $v->bnum;
121 $ret .= '>' . $v->sname . "\n";
122 }
123 }
124 $ret .= "</select>";
125 return html_tag( 'tr',
126 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
127 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
128 } else {
129 return html_tag( 'tr',
130 html_tag( 'td',
19347763 131 addHidden('backend', '1'),
08188c25 132 'center', $color[4], 'colspan="2"')) . "\n";
133 }
134}
135
f6c945b9 136/* Open addressbook, with error messages on but without LDAP (the *
137 * second "true"). Don't need LDAP here anyway */
daba719e 138$abook = addressbook_init(true, true);
139if($abook->localbackend == 0) {
140 plain_error_message(
141 _("No personal address book is defined. Contact administrator."),
142 $color);
143 exit();
144}
ffd8224c 145
daba719e 146displayPageHeader($color, 'None');
ffd8224c 147
daba719e 148$defdata = array();
149$formerror = '';
150$abortform = false;
151$showaddrlist = true;
152$defselected = array();
07dcee9f 153$form_url = 'addressbook.php';
daba719e 154
155
f6c945b9 156/* Handle user's actions */
1e12d1ff 157if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
daba719e 158
f6c945b9 159 /**************************************************
160 * Add new address *
161 **************************************************/
39b26252 162 if (isset($addaddr)) {
c6554ec0 163 foreach( $addaddr as $k => $adr ) {
164 $addaddr[$k] = strip_tags( $adr );
165 }
08188c25 166 if (isset($backend)) {
167 $r = $abook->add($addaddr, $backend);
168 } else {
169 $r = $abook->add($addaddr, $abook->localbackend);
170 }
ffd8224c 171
f6c945b9 172 /* Handle error messages */
173 if (!$r) {
174 /* Remove backend name from error string */
ffd8224c 175 $errstr = $abook->error;
176 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
177
178 $formerror = $errstr;
179 $showaddrlist = false;
180 $defdata = $addaddr;
181 }
daba719e 182 } else {
ffd8224c 183
f6c945b9 184 /************************************************
185 * Delete address(es) *
186 ************************************************/
187 if ((!empty($deladdr)) && sizeof($sel) > 0) {
daba719e 188 $orig_sel = $sel;
189 sort($sel);
190
f6c945b9 191 /* The selected addresses are identidied by "backend:nickname". *
192 * Sort the list and process one backend at the time */
daba719e 193 $prevback = -1;
194 $subsel = array();
195 $delfailed = false;
196
f6c945b9 197 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
daba719e 198 list($sbackend, $snick) = explode(':', $sel[$i]);
199
f6c945b9 200 /* When we get to a new backend, process addresses in *
201 * previous one. */
202 if ($prevback != $sbackend && $prevback != -1) {
daba719e 203
204 $r = $abook->remove($subsel, $prevback);
f6c945b9 205 if (!$r) {
daba719e 206 $formerror = $abook->error;
207 $i = sizeof($sel);
208 $delfailed = true;
209 break;
210 }
211 $subsel = array();
212 }
213
f6c945b9 214 /* Queue for processing */
daba719e 215 array_push($subsel, $snick);
216 $prevback = $sbackend;
ffd8224c 217 }
ffd8224c 218
f6c945b9 219 if (!$delfailed) {
daba719e 220 $r = $abook->remove($subsel, $prevback);
f6c945b9 221 if (!$r) { /* Handle errors */
daba719e 222 $formerror = $abook->error;
223 $delfailed = true;
224 }
ffd8224c 225 }
ffd8224c 226
f6c945b9 227 if ($delfailed) {
daba719e 228 $showaddrlist = true;
229 $defselected = $orig_sel;
ffd8224c 230 }
ffd8224c 231
daba719e 232 } else {
233
f6c945b9 234 /***********************************************
235 * Update/modify address *
236 ***********************************************/
237 if (!empty($editaddr)) {
daba719e 238
f6c945b9 239 /* Stage one: Copy data into form */
daba719e 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;
244 $defselected = $sel;
245 } else {
246 $abortform = true;
247 list($ebackend, $enick) = explode(':', $sel[0]);
248 $olddata = $abook->lookup($enick, $ebackend);
249
f6c945b9 250 /* Display the "new address" form */
07dcee9f 251 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
f6c945b9 252 "\n" .
ac987a56 253 html_tag( 'table',
254 html_tag( 'tr',
255 html_tag( 'td',
256 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
257 'center', $color[0] )
258 ),
bd9c880b 259 'center', '', 'width="100%" ' );
daba719e 260 address_form("editaddr", _("Update address"), $olddata);
62366261 261 echo addHidden('oldnick', $olddata['nickname']).
262 addHidden('backend', $olddata['backend']).
263 addHidden('doedit', '1').
f6c945b9 264 '</FORM>';
daba719e 265 }
266 } else {
267
f6c945b9 268 /* Stage two: Write new data */
269 if ($doedit = 1) {
daba719e 270 $newdata = $editaddr;
271 $r = $abook->modify($oldnick, $newdata, $backend);
272
f6c945b9 273 /* Handle error messages */
274 if (!$r) {
275 /* Display error */
ac987a56 276 echo html_tag( 'table',
277 html_tag( 'tr',
278 html_tag( 'td',
09743787 279 "\n". '<strong><font color="' . $color[2] .
ac987a56 280 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
281 'center' )
282 ),
ac50138c 283 'center', '', 'width="100%"' );
f6c945b9 284
285 /* Display the "new address" form again */
07dcee9f 286 echo '<FORM ACTION="' . $form_url .
f6c945b9 287 '" METHOD="POST">' . "\n" .
ac987a56 288 html_tag( 'table',
289 html_tag( 'tr',
290 html_tag( 'td',
09743787 291 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
ac987a56 292 'center', $color[0] )
293 ),
09743787 294 'center', '', 'width="100%"' );
daba719e 295 address_form("editaddr", _("Update address"), $newdata);
19347763 296 echo
297 addHidden('oldnick', $oldnick).
298 addHidden('backend', $backend).
299 addHidden('doedit', '1').
f6c945b9 300 "\n" . '</FORM>';
daba719e 301 $abortform = true;
302 }
303 } else {
304
f6c945b9 305 /* Should not get here... */
daba719e 306 plain_error_message(_("Unknown error"), $color);
307 $abortform = true;
308 }
309 }
310 } /* !empty($editaddr) - Update/modify address */
311 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
312 } /* !empty($addaddr['nickname']) - Add new address */
313
314 // Some times we end output before forms are printed
315 if($abortform) {
dcc1cc82 316 echo "</BODY></HTML>\n";
daba719e 317 exit();
ffd8224c 318 }
daba719e 319}
ffd8224c 320
321
f6c945b9 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 * =================================================================== */
ffd8224c 326
f6c945b9 327/* Display error messages */
328if (!empty($formerror)) {
ac987a56 329 echo html_tag( 'table',
330 html_tag( 'tr',
331 html_tag( 'td',
332 "\n". '<br><strong><font color="' . $color[2] .
333 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
334 'center' )
335 ),
ac50138c 336 'center', '', 'width="100%"' );
daba719e 337}
ffd8224c 338
339
f6c945b9 340/* Display the address management part */
341if ($showaddrlist) {
342 /* Get and sort address list */
daba719e 343 $alist = $abook->list_addr();
344 if(!is_array($alist)) {
ffd8224c 345 plain_error_message($abook->error, $color);
346 exit;
daba719e 347 }
ffd8224c 348
daba719e 349 usort($alist,'alistcmp');
350 $prevbackend = -1;
351 $headerprinted = false;
ffd8224c 352
ac987a56 353 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
ffd8224c 354
f6c945b9 355 /* List addresses */
91821fc0 356 if (count($alist) > 0) {
fc93f97c 357 echo '<form action="' . $form_url . '" method="post">' . "\n";
91821fc0 358 while(list($undef,$row) = each($alist)) {
359
360 /* New table header for each backend */
361 if($prevbackend != $row['backend']) {
92cd1e8e 362 if($prevbackend < 0) {
ac987a56 363 echo html_tag( 'table',
364 html_tag( 'tr',
365 html_tag( 'td',
fc93f97c 366 '<input type=submit name=editaddr value="' .
367 _("Edit selected") . "\" />\n" .
368 '<input type=submit name=deladdr value="' .
369 _("Delete selected") . "\" />\n",
ac987a56 370 'center', '', 'colspan="5"' )
371 ) .
372 html_tag( 'tr',
373 html_tag( 'td', '&nbsp;<br>', 'center', '', 'colspan="5"' )
374 ) ,
375 'center' );
d9879e29 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', '&nbsp;', 'left', '', 'width="1%"' ) . "\n" .
380 html_tag( 'th', _("Nickname") .
381 " <a href=\"$form_url?abook_sort=nickname\">".
382 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" alt=\"sort by nickname\" title=\"" .
383 _("Click here to change the sorting of the address list") .
384 "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
385 html_tag( 'th', _("Name") .
386 " <a href=\"$form_url?abook_sort=name\">" .
387 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
388 "alt=\"sort by name\" title=\"" .
389 _("Click here to change the sorting of the address list") .
390 "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
391 html_tag( 'th', _("E-mail") .
392 " <a href=\"$form_url?abook_sort=email\">" .
393 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
394 "alt=\"sort by email\" title=\"" .
395 _("Click here to change the sorting of the address list") .
396 "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
397 html_tag( 'th', _("Info") .
398 " <a href=\"$form_url?abook_sort=label\">" .
399 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
400 "alt=\"sort by info\" title=\"" .
401 _("Click here to change the sorting of the address list") .
402 "\" /></a>", 'left', '', 'width="1%"' ) . "\n",
ac987a56 403 '', $color[9] ) . "\n";
d9879e29 404 }
405
406 // Separate different backends with <hr />
407 if($prevbackend > 0) {
408 echo html_tag( 'tr',
409 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
410 );
411 }
412 // Print backend name
413 echo html_tag( 'tr',
414 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
415 );
416
91821fc0 417 $line = 0;
418 $headerprinted = true;
419 } /* End of header */
fc93f97c 420
91821fc0 421 $prevbackend = $row['backend'];
422
423 /* Check if this user is selected */
19347763 424 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
91821fc0 425
426 /* Print one row */
ac987a56 427 $tr_bgcolor = '';
428 if ($line % 2) { $tr_bgcolor = $color[0]; }
e842b215 429 if ($squirrelmail_language == 'ja_JP')
430 {
ef5bea7d 431 echo html_tag( 'tr', '', '', $tr_bgcolor) .
e842b215 432 html_tag( 'td',
fc93f97c 433 '<small>' .
19347763 434 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
fc93f97c 435 '</small>' ,
e842b215 436 'center', '', 'valign="top" width="1%"' ) .
437 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
438 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
439 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
440 } else {
ef5bea7d 441 echo html_tag( 'tr', '', '', $tr_bgcolor) .
ac987a56 442 html_tag( 'td',
fc93f97c 443 '<small>' .
444 '<input type=checkbox ' . $selected . ' name="sel[]" value="' .
445 $row['backend'] . ':' . $row['nickname'] . '" /></small>' ,
ac987a56 446 'center', '', 'valign="top" width="1%"' ) .
447 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
448 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
449 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
e842b215 450 }
3d0cada3 451 $email = $abook->full_address($row);
d62c4938 452 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
453 htmlspecialchars($row['email']) ) .
454 '&nbsp;</td>'."\n".
4e160237 455 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
ac987a56 456 "</tr>\n";
91821fc0 457 $line++;
daba719e 458 }
d9879e29 459 echo "</table>" .
460 "\n<!-- end of address book table -->\n";
461
fc93f97c 462 /* End of list. Add edit/delete select buttons */
91821fc0 463 if ($headerprinted) {
fc93f97c 464 echo html_tag( 'table',
465 html_tag( 'tr',
ac987a56 466 html_tag( 'td',
fc93f97c 467 '<input type="submit" name="editaddr" value="' . _("Edit selected") .
468 "\" />\n" .
469 '<input type="submit" name="deladdr" value="' . _("Delete selected") .
470 "\" />\n",
ac987a56 471 'center', '', 'colspan="5"' )
fc93f97c 472 ),
473 'center' );
91821fc0 474 }
fc93f97c 475 echo "</form>\n";
daba719e 476 }
f6c945b9 477} /* end of addresslist */
daba719e 478
479
f6c945b9 480/* Display the "new address" form */
481echo '<a name="AddAddress"></a>' . "\n" .
fc93f97c 482 '<form action="' . $form_url . '" name="f_add" method="post">' . "\n" .
c6554ec0 483 html_tag( 'table',
ac987a56 484 html_tag( 'tr',
08188c25 485 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
ac987a56 486 'center', $color[0]
487 )
488 )
ac50138c 489 , 'center', '', 'width="100%"' ) ."\n";
daba719e 490address_form('addaddr', _("Add address"), $defdata);
fc93f97c 491echo "</form>\n";
daba719e 492
f6c945b9 493/* Add hook for anything that wants on the bottom */
daba719e 494do_hook('addressbook_bottom');
dcc1cc82 495?>
496
fc93f97c 497</body></html>