if encoding function can't find charset, it does us-ascii encoding.
[squirrelmail.git] / src / addressbook.php
CommitLineData
abdfb4d0 1<?php
134e4174 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);
2741cc97 45sqgetGlobalVar('doedit', $doedit, SQ_POST);
0b97a708 46
08e71631 47/* Get sorting order */
48$abook_sort_order = get_abook_sort();
49
8f6f9ba5 50/**
51 * Make an input field
52 * @param string $label
53 * @param string $field
54 * @param string $name
55 * @param string $size
56 * @param array $values
57 * @param string $add
58 */
19347763 59function addressbook_inp_field($label, $field, $name, $size, $values, $add) {
daba719e 60 global $color;
19347763 61 $value = ( isset($values[$field]) ? $values[$field] : '');
62
63 $td_str = addInput($name.'['.$field.']', $value, $size)
64 . $add ;
2741cc97 65
ac987a56 66 return html_tag( 'tr' ,
67 html_tag( 'td', $label . ':', 'right', $color[4]) .
68 html_tag( 'td', $td_str, 'left', $color[4])
69 )
70 . "\n";
daba719e 71}
ffd8224c 72
8f6f9ba5 73/**
74 * Output form to add and modify address data
75 */
daba719e 76function address_form($name, $submittext, $values = array()) {
e842b215 77 global $color, $squirrelmail_language;
c6554ec0 78
e842b215 79 if ($squirrelmail_language == 'ja_JP')
80 {
81 echo html_tag( 'table',
19347763 82 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
39bfea8f 83 ' <small>' . _("Must be unique") . '</small>') .
19347763 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, '') .
2741cc97 88 list_writable_backends($name) .
e842b215 89 html_tag( 'tr',
90 html_tag( 'td',
39bfea8f 91 '<input type="submit" name="' . htmlentities($name) . '[SUBMIT]" value="' .
92 $submittext . '" />',
e842b215 93 'center', $color[4], 'colspan="2"')
94 )
95 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
96 } else {
ac987a56 97 echo html_tag( 'table',
19347763 98 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
39bfea8f 99 ' <small>' . _("Must be unique") . '</small>') .
19347763 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, '') .
2741cc97 104 list_writable_backends($name) .
ac987a56 105 html_tag( 'tr',
106 html_tag( 'td',
39bfea8f 107 '<input type="submit" name="' . htmlentities($name) . '[SUBMIT]" value="' .
108 $submittext . '" />',
ac987a56 109 'center', $color[4], 'colspan="2"')
110 )
ac50138c 111 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
daba719e 112}
e842b215 113}
ffd8224c 114
08188c25 115function list_writable_backends($name) {
116 global $color, $abook;
117 if ( $name != 'addaddr' ) { return; }
118 if ( $abook->numbackends > 1 ) {
39bfea8f 119 $ret = '<select name="backend">';
08188c25 120 $backends = $abook->get_backend_list();
121 while (list($undef,$v) = each($backends)) {
122 if ($v->writeable) {
39bfea8f 123 $ret .= '<option value="' . $v->bnum;
124 $ret .= '">' . $v->sname . "</option>\n";
08188c25 125 }
126 }
127 $ret .= "</select>";
128 return html_tag( 'tr',
2741cc97 129 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
130 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
08188c25 131 } else {
132 return html_tag( 'tr',
2741cc97 133 html_tag( 'td',
134 addHidden('backend', '1'),
08188c25 135 'center', $color[4], 'colspan="2"')) . "\n";
136 }
137}
138
f6c945b9 139/* Open addressbook, with error messages on but without LDAP (the *
140 * second "true"). Don't need LDAP here anyway */
daba719e 141$abook = addressbook_init(true, true);
142if($abook->localbackend == 0) {
143 plain_error_message(
144 _("No personal address book is defined. Contact administrator."),
145 $color);
146 exit();
147}
ffd8224c 148
daba719e 149displayPageHeader($color, 'None');
ffd8224c 150
daba719e 151$defdata = array();
152$formerror = '';
153$abortform = false;
154$showaddrlist = true;
155$defselected = array();
07dcee9f 156$form_url = 'addressbook.php';
daba719e 157
158
f6c945b9 159/* Handle user's actions */
1e12d1ff 160if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
daba719e 161
f6c945b9 162 /**************************************************
163 * Add new address *
164 **************************************************/
39b26252 165 if (isset($addaddr)) {
a123157f 166 if (isset($backend)) {
167 $r = $abook->add($addaddr, $backend);
168 } else {
169 $r = $abook->add($addaddr, $abook->localbackend);
c6554ec0 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 */
39bfea8f 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);
2741cc97 261 echo addHidden('oldnick', $olddata['nickname']).
62366261 262 addHidden('backend', $olddata['backend']).
2741cc97 263 addHidden('doedit', '1').
39bfea8f 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 */
39bfea8f 286 echo '<form action="' . $form_url .
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
2741cc97 297 addHidden('oldnick', $oldnick).
298 addHidden('backend', $backend).
299 addHidden('doedit', '1').
39bfea8f 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) {
39bfea8f 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',
39bfea8f 332 "\n". '<br /><strong><font color="' . $color[2] .
ac987a56 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',
134e4174 364 html_tag( 'tr',
365 html_tag( 'td',
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"' )
371 ) .
372 html_tag( 'tr',
373 html_tag( 'td', '&nbsp;<br />', 'center', '', 'colspan="5"' )
374 ),
375 'center' );
2741cc97 376 echo "\n<!-- start of address book table -->\n" .
134e4174 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 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";
d9879e29 393 }
394
2741cc97 395 // Separate different backends with <hr />
d9879e29 396 if($prevbackend > 0) {
397 echo html_tag( 'tr',
398 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
399 );
2741cc97 400 }
401
402 // Print backend name
d9879e29 403 echo html_tag( 'tr',
404 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
405 );
406
91821fc0 407 $line = 0;
408 $headerprinted = true;
409 } /* End of header */
fc93f97c 410
91821fc0 411 $prevbackend = $row['backend'];
412
413 /* Check if this user is selected */
19347763 414 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
91821fc0 415
9f6c97c3 416 /* Print one row, with alternating color */
417 if ($line % 2) {
418 $tr_bgcolor = $color[12];
419 } else {
420 $tr_bgcolor = $color[4];
421 }
e842b215 422 if ($squirrelmail_language == 'ja_JP')
423 {
19d01c6b 424 echo html_tag( 'tr', '', '', $tr_bgcolor);
425 if ($abook->backends[$row['backend']]->writeable) {
426 echo html_tag( 'td',
fc93f97c 427 '<small>' .
2741cc97 428 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
fc93f97c 429 '</small>' ,
19d01c6b 430 'center', '', 'valign="top" width="1%"' );
431 } else {
432 echo html_tag( 'td',
433 '&nbsp;' ,
434 'center', '', 'valign="top" width="1%"' );
435 }
436 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
e842b215 437 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
438 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
439 } else {
19d01c6b 440 echo html_tag( 'tr', '', '', $tr_bgcolor);
441 if ($abook->backends[$row['backend']]->writeable) {
442 echo html_tag( 'td',
fc93f97c 443 '<small>' .
39bfea8f 444 '<input type="checkbox" ' . $selected . ' name="sel[]" value="' .
fc93f97c 445 $row['backend'] . ':' . $row['nickname'] . '" /></small>' ,
19d01c6b 446 'center', '', 'valign="top" width="1%"' );
447 } else {
448 echo html_tag( 'td',
449 '&nbsp;' ,
450 'center', '', 'valign="top" width="1%"' );
451 }
452 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
ac987a56 453 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
454 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
e842b215 455 }
3d0cada3 456 $email = $abook->full_address($row);
d62c4938 457 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
458 htmlspecialchars($row['email']) ) .
459 '&nbsp;</td>'."\n".
4e160237 460 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
ac987a56 461 "</tr>\n";
91821fc0 462 $line++;
daba719e 463 }
2741cc97 464 echo "</table>" .
465 "\n<!-- end of address book table -->\n";
d9879e29 466
fc93f97c 467 /* End of list. Add edit/delete select buttons */
91821fc0 468 if ($headerprinted) {
fc93f97c 469 echo html_tag( 'table',
134e4174 470 html_tag( 'tr',
471 html_tag( 'td',
472 '<input type="submit" name="editaddr" value="' . _("Edit selected") .
473 "\" />\n" .
474 '<input type="submit" name="deladdr" value="' . _("Delete selected") .
475 "\" />\n",
476 'center', '', 'colspan="5"' )
477 ),
478 'center' );
91821fc0 479 }
fc93f97c 480 echo "</form>\n";
daba719e 481 }
f6c945b9 482} /* end of addresslist */
daba719e 483
484
f6c945b9 485/* Display the "new address" form */
486echo '<a name="AddAddress"></a>' . "\n" .
fc93f97c 487 '<form action="' . $form_url . '" name="f_add" method="post">' . "\n" .
c6554ec0 488 html_tag( 'table',
ac987a56 489 html_tag( 'tr',
08188c25 490 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
ac987a56 491 'center', $color[0]
492 )
493 )
ac50138c 494 , 'center', '', 'width="100%"' ) ."\n";
daba719e 495address_form('addaddr', _("Add address"), $defdata);
fc93f97c 496echo "</form>\n";
daba719e 497
f6c945b9 498/* Add hook for anything that wants on the bottom */
daba719e 499do_hook('addressbook_bottom');
dcc1cc82 500?>
9f6c97c3 501</body></html>