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