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