Uneditable address book entries no longer have checkboxes next to them on addresses...
[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)) {
a123157f 165 if (isset($backend)) {
166 $r = $abook->add($addaddr, $backend);
167 } else {
168 $r = $abook->add($addaddr, $abook->localbackend);
c6554ec0 169 }
ffd8224c 170
f6c945b9 171 /* Handle error messages */
172 if (!$r) {
173 /* Remove backend name from error string */
ffd8224c 174 $errstr = $abook->error;
175 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
176
177 $formerror = $errstr;
178 $showaddrlist = false;
179 $defdata = $addaddr;
180 }
daba719e 181 } else {
ffd8224c 182
f6c945b9 183 /************************************************
184 * Delete address(es) *
185 ************************************************/
186 if ((!empty($deladdr)) && sizeof($sel) > 0) {
daba719e 187 $orig_sel = $sel;
188 sort($sel);
189
f6c945b9 190 /* The selected addresses are identidied by "backend:nickname". *
191 * Sort the list and process one backend at the time */
daba719e 192 $prevback = -1;
193 $subsel = array();
194 $delfailed = false;
195
f6c945b9 196 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
daba719e 197 list($sbackend, $snick) = explode(':', $sel[$i]);
198
f6c945b9 199 /* When we get to a new backend, process addresses in *
200 * previous one. */
201 if ($prevback != $sbackend && $prevback != -1) {
daba719e 202
203 $r = $abook->remove($subsel, $prevback);
f6c945b9 204 if (!$r) {
daba719e 205 $formerror = $abook->error;
206 $i = sizeof($sel);
207 $delfailed = true;
208 break;
209 }
210 $subsel = array();
211 }
212
f6c945b9 213 /* Queue for processing */
daba719e 214 array_push($subsel, $snick);
215 $prevback = $sbackend;
ffd8224c 216 }
ffd8224c 217
f6c945b9 218 if (!$delfailed) {
daba719e 219 $r = $abook->remove($subsel, $prevback);
f6c945b9 220 if (!$r) { /* Handle errors */
daba719e 221 $formerror = $abook->error;
222 $delfailed = true;
223 }
ffd8224c 224 }
ffd8224c 225
f6c945b9 226 if ($delfailed) {
daba719e 227 $showaddrlist = true;
228 $defselected = $orig_sel;
ffd8224c 229 }
ffd8224c 230
daba719e 231 } else {
232
f6c945b9 233 /***********************************************
234 * Update/modify address *
235 ***********************************************/
236 if (!empty($editaddr)) {
daba719e 237
f6c945b9 238 /* Stage one: Copy data into form */
daba719e 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;
243 $defselected = $sel;
244 } else {
245 $abortform = true;
246 list($ebackend, $enick) = explode(':', $sel[0]);
247 $olddata = $abook->lookup($enick, $ebackend);
248
f6c945b9 249 /* Display the "new address" form */
07dcee9f 250 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
f6c945b9 251 "\n" .
ac987a56 252 html_tag( 'table',
253 html_tag( 'tr',
254 html_tag( 'td',
255 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
256 'center', $color[0] )
257 ),
bd9c880b 258 'center', '', 'width="100%" ' );
daba719e 259 address_form("editaddr", _("Update address"), $olddata);
2741cc97 260 echo addHidden('oldnick', $olddata['nickname']).
62366261 261 addHidden('backend', $olddata['backend']).
2741cc97 262 addHidden('doedit', '1').
f6c945b9 263 '</FORM>';
daba719e 264 }
265 } else {
266
f6c945b9 267 /* Stage two: Write new data */
268 if ($doedit = 1) {
daba719e 269 $newdata = $editaddr;
270 $r = $abook->modify($oldnick, $newdata, $backend);
271
f6c945b9 272 /* Handle error messages */
273 if (!$r) {
274 /* Display error */
ac987a56 275 echo html_tag( 'table',
276 html_tag( 'tr',
277 html_tag( 'td',
09743787 278 "\n". '<strong><font color="' . $color[2] .
ac987a56 279 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
280 'center' )
281 ),
ac50138c 282 'center', '', 'width="100%"' );
f6c945b9 283
284 /* Display the "new address" form again */
07dcee9f 285 echo '<FORM ACTION="' . $form_url .
f6c945b9 286 '" METHOD="POST">' . "\n" .
ac987a56 287 html_tag( 'table',
288 html_tag( 'tr',
289 html_tag( 'td',
09743787 290 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
ac987a56 291 'center', $color[0] )
292 ),
09743787 293 'center', '', 'width="100%"' );
daba719e 294 address_form("editaddr", _("Update address"), $newdata);
19347763 295 echo
2741cc97 296 addHidden('oldnick', $oldnick).
297 addHidden('backend', $backend).
298 addHidden('doedit', '1').
f6c945b9 299 "\n" . '</FORM>';
daba719e 300 $abortform = true;
301 }
302 } else {
303
f6c945b9 304 /* Should not get here... */
daba719e 305 plain_error_message(_("Unknown error"), $color);
306 $abortform = true;
307 }
308 }
309 } /* !empty($editaddr) - Update/modify address */
310 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
311 } /* !empty($addaddr['nickname']) - Add new address */
312
313 // Some times we end output before forms are printed
314 if($abortform) {
dcc1cc82 315 echo "</BODY></HTML>\n";
daba719e 316 exit();
ffd8224c 317 }
daba719e 318}
ffd8224c 319
320
f6c945b9 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 * =================================================================== */
ffd8224c 325
f6c945b9 326/* Display error messages */
327if (!empty($formerror)) {
ac987a56 328 echo html_tag( 'table',
329 html_tag( 'tr',
330 html_tag( 'td',
331 "\n". '<br><strong><font color="' . $color[2] .
332 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
333 'center' )
334 ),
ac50138c 335 'center', '', 'width="100%"' );
daba719e 336}
ffd8224c 337
338
f6c945b9 339/* Display the address management part */
340if ($showaddrlist) {
341 /* Get and sort address list */
daba719e 342 $alist = $abook->list_addr();
343 if(!is_array($alist)) {
ffd8224c 344 plain_error_message($abook->error, $color);
345 exit;
daba719e 346 }
ffd8224c 347
daba719e 348 usort($alist,'alistcmp');
349 $prevbackend = -1;
350 $headerprinted = false;
ffd8224c 351
ac987a56 352 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
ffd8224c 353
f6c945b9 354 /* List addresses */
91821fc0 355 if (count($alist) > 0) {
fc93f97c 356 echo '<form action="' . $form_url . '" method="post">' . "\n";
91821fc0 357 while(list($undef,$row) = each($alist)) {
358
359 /* New table header for each backend */
360 if($prevbackend != $row['backend']) {
92cd1e8e 361 if($prevbackend < 0) {
ac987a56 362 echo html_tag( 'table',
363 html_tag( 'tr',
364 html_tag( 'td',
fc93f97c 365 '<input type=submit name=editaddr value="' .
366 _("Edit selected") . "\" />\n" .
367 '<input type=submit name=deladdr value="' .
368 _("Delete selected") . "\" />\n",
ac987a56 369 'center', '', 'colspan="5"' )
370 ) .
371 html_tag( 'tr',
372 html_tag( 'td', '&nbsp;<br>', 'center', '', 'colspan="5"' )
373 ) ,
374 'center' );
2741cc97 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', '&nbsp;', 'left', '', 'width="1%"' ) . "\n" .
379 html_tag( 'th', _("Nickname") .
08e71631 380 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1)
381 , 'left', '', 'width="1%"' ) . "\n" .
2741cc97 382 html_tag( 'th', _("Name") .
08e71631 383 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3)
384 , 'left', '', 'width="1%"' ) . "\n" .
2741cc97 385 html_tag( 'th', _("E-mail") .
08e71631 386 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5)
387 , 'left', '', 'width="1%"' ) . "\n" .
2741cc97 388 html_tag( 'th', _("Info") .
08e71631 389 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7)
390 , 'left', '', 'width="1%"' ) . "\n",
2741cc97 391 '', $color[9] ) . "\n";
d9879e29 392 }
393
2741cc97 394 // Separate different backends with <hr />
d9879e29 395 if($prevbackend > 0) {
396 echo html_tag( 'tr',
397 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
398 );
2741cc97 399 }
400
401 // Print backend name
d9879e29 402 echo html_tag( 'tr',
403 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
404 );
405
91821fc0 406 $line = 0;
407 $headerprinted = true;
408 } /* End of header */
fc93f97c 409
91821fc0 410 $prevbackend = $row['backend'];
411
412 /* Check if this user is selected */
19347763 413 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
91821fc0 414
9f6c97c3 415 /* Print one row, with alternating color */
416 if ($line % 2) {
417 $tr_bgcolor = $color[12];
418 } else {
419 $tr_bgcolor = $color[4];
420 }
e842b215 421 if ($squirrelmail_language == 'ja_JP')
422 {
19d01c6b 423 echo html_tag( 'tr', '', '', $tr_bgcolor);
424 if ($abook->backends[$row['backend']]->writeable) {
425 echo html_tag( 'td',
fc93f97c 426 '<small>' .
2741cc97 427 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
fc93f97c 428 '</small>' ,
19d01c6b 429 'center', '', 'valign="top" width="1%"' );
430 } else {
431 echo html_tag( 'td',
432 '&nbsp;' ,
433 'center', '', 'valign="top" width="1%"' );
434 }
435 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
e842b215 436 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
437 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
438 } else {
19d01c6b 439 echo html_tag( 'tr', '', '', $tr_bgcolor);
440 if ($abook->backends[$row['backend']]->writeable) {
441 echo html_tag( 'td',
fc93f97c 442 '<small>' .
443 '<input type=checkbox ' . $selected . ' name="sel[]" value="' .
444 $row['backend'] . ':' . $row['nickname'] . '" /></small>' ,
19d01c6b 445 'center', '', 'valign="top" width="1%"' );
446 } else {
447 echo html_tag( 'td',
448 '&nbsp;' ,
449 'center', '', 'valign="top" width="1%"' );
450 }
451 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
ac987a56 452 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
453 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
e842b215 454 }
3d0cada3 455 $email = $abook->full_address($row);
d62c4938 456 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
457 htmlspecialchars($row['email']) ) .
458 '&nbsp;</td>'."\n".
4e160237 459 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
ac987a56 460 "</tr>\n";
91821fc0 461 $line++;
daba719e 462 }
2741cc97 463 echo "</table>" .
464 "\n<!-- end of address book table -->\n";
d9879e29 465
fc93f97c 466 /* End of list. Add edit/delete select buttons */
91821fc0 467 if ($headerprinted) {
fc93f97c 468 echo html_tag( 'table',
2741cc97 469 html_tag( 'tr',
470 html_tag( 'td',
471 '<input type="submit" name="editaddr" value="' . _("Edit selected") .
472 "\" />\n" .
473 '<input type="submit" name="deladdr" value="' . _("Delete selected") .
474 "\" />\n",
475 'center', '', 'colspan="5"' )
476 ),
477 'center' );
91821fc0 478 }
fc93f97c 479 echo "</form>\n";
daba719e 480 }
f6c945b9 481} /* end of addresslist */
daba719e 482
483
f6c945b9 484/* Display the "new address" form */
485echo '<a name="AddAddress"></a>' . "\n" .
fc93f97c 486 '<form action="' . $form_url . '" name="f_add" method="post">' . "\n" .
c6554ec0 487 html_tag( 'table',
ac987a56 488 html_tag( 'tr',
08188c25 489 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
ac987a56 490 'center', $color[0]
491 )
492 )
ac50138c 493 , 'center', '', 'width="100%"' ) ."\n";
daba719e 494address_form('addaddr', _("Add address"), $defdata);
fc93f97c 495echo "</form>\n";
daba719e 496
f6c945b9 497/* Add hook for anything that wants on the bottom */
daba719e 498do_hook('addressbook_bottom');
dcc1cc82 499?>
500
9f6c97c3 501</body></html>