If we use forms.php in STABLE we might as well try it in DEVEL
[squirrelmail.git] / src / addressbook.php
1 <?php
2
3 /**
4 * addressbook.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Manage personal address book.
10 *
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /**
16 * Path for SquirrelMail required files.
17 * @ignore
18 */
19 define('SM_PATH','../');
20
21 /** SquirrelMail required files. */
22 require_once(SM_PATH . 'include/validate.php');
23 require_once(SM_PATH . 'functions/global.php');
24 require_once(SM_PATH . 'functions/display_messages.php');
25 require_once(SM_PATH . 'functions/addressbook.php');
26 require_once(SM_PATH . 'functions/strings.php');
27 require_once(SM_PATH . 'functions/html.php');
28 require_once(SM_PATH . 'functions/forms.php');
29
30 /** lets get the global vars we may need */
31 sqgetGlobalVar('key', $key, SQ_COOKIE);
32
33 sqgetGlobalVar('username', $username, SQ_SESSION);
34 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
35 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
36 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
37
38 /* From the address form */
39 sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
40 sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
41 sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
42 sqgetGlobalVar('sel', $sel, SQ_POST);
43 sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
44 sqgetGlobalVar('backend', $backend, SQ_POST);
45 sqgetGlobalVar('doedit', $doedit, SQ_POST);
46
47 /* Get sorting order */
48 $abook_sort_order = get_abook_sort();
49
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 */
59 function addressbook_inp_field($label, $field, $name, $size, $values, $add) {
60 global $color;
61 $value = ( isset($values[$field]) ? $values[$field] : '');
62
63 $td_str = addInput($name.'['.$field.']', $value, $size)
64 . $add ;
65
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";
71 }
72
73 /**
74 * Output form to add and modify address data
75 */
76 function address_form($name, $submittext, $values = array()) {
77 global $color, $squirrelmail_language;
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 }
110 }
111
112 function 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 . "</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";
133 }
134 }
135
136 /* Open addressbook, with error messages on but without LDAP (the *
137 * second "true"). Don't need LDAP here anyway */
138 $abook = addressbook_init(true, true);
139 if($abook->localbackend == 0) {
140 plain_error_message(
141 _("No personal address book is defined. Contact administrator."),
142 $color);
143 exit();
144 }
145
146 displayPageHeader($color, 'None');
147
148 $defdata = array();
149 $formerror = '';
150 $abortform = false;
151 $showaddrlist = true;
152 $defselected = array();
153 $form_url = 'addressbook.php';
154
155
156 /* Handle user's actions */
157 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
158
159 /**************************************************
160 * Add new address *
161 **************************************************/
162 if (isset($addaddr)) {
163 if (isset($backend)) {
164 $r = $abook->add($addaddr, $backend);
165 } else {
166 $r = $abook->add($addaddr, $abook->localbackend);
167 }
168
169 /* Handle error messages */
170 if (!$r) {
171 /* Remove backend name from error string */
172 $errstr = $abook->error;
173 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
174
175 $formerror = $errstr;
176 $showaddrlist = false;
177 $defdata = $addaddr;
178 }
179 } else {
180
181 /************************************************
182 * Delete address(es) *
183 ************************************************/
184 if ((!empty($deladdr)) && sizeof($sel) > 0) {
185 $orig_sel = $sel;
186 sort($sel);
187
188 /* The selected addresses are identidied by "backend:nickname". *
189 * Sort the list and process one backend at the time */
190 $prevback = -1;
191 $subsel = array();
192 $delfailed = false;
193
194 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
195 list($sbackend, $snick) = explode(':', $sel[$i]);
196
197 /* When we get to a new backend, process addresses in *
198 * previous one. */
199 if ($prevback != $sbackend && $prevback != -1) {
200
201 $r = $abook->remove($subsel, $prevback);
202 if (!$r) {
203 $formerror = $abook->error;
204 $i = sizeof($sel);
205 $delfailed = true;
206 break;
207 }
208 $subsel = array();
209 }
210
211 /* Queue for processing */
212 array_push($subsel, $snick);
213 $prevback = $sbackend;
214 }
215
216 if (!$delfailed) {
217 $r = $abook->remove($subsel, $prevback);
218 if (!$r) { /* Handle errors */
219 $formerror = $abook->error;
220 $delfailed = true;
221 }
222 }
223
224 if ($delfailed) {
225 $showaddrlist = true;
226 $defselected = $orig_sel;
227 }
228
229 } else {
230
231 /***********************************************
232 * Update/modify address *
233 ***********************************************/
234 if (!empty($editaddr)) {
235
236 /* Stage one: Copy data into form */
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
247 /* Display the "new address" form */
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%" ' );
256 address_form("editaddr", _("Update address"), $olddata);
257 echo addHidden('oldnick', $olddata['nickname']).
258 addHidden('backend', $olddata['backend']).
259 addHidden('doedit', '1').
260 '</form>';
261 }
262 } else {
263
264 /* Stage two: Write new data */
265 if ($doedit = 1) {
266 $newdata = $editaddr;
267 $r = $abook->modify($oldnick, $newdata, $backend);
268
269 /* Handle error messages */
270 if (!$r) {
271 /* Display error */
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%"' );
280
281 /* Display the "new address" form again */
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%"' );
290 address_form("editaddr", _("Update address"), $newdata);
291 echo
292 addHidden('oldnick', $oldnick).
293 addHidden('backend', $backend).
294 addHidden('doedit', '1').
295 "\n" . '</form>';
296 $abortform = true;
297 }
298 } else {
299
300 /* Should not get here... */
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) {
311 echo "</body></html>\n";
312 exit();
313 }
314 }
315
316
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 * =================================================================== */
321
322 /* Display error messages */
323 if (!empty($formerror)) {
324 echo html_tag( 'table',
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%"' );
332 }
333
334
335 /* Display the address management part */
336 if ($showaddrlist) {
337 /* Get and sort address list */
338 $alist = $abook->list_addr();
339 if(!is_array($alist)) {
340 plain_error_message($abook->error, $color);
341 exit;
342 }
343
344 usort($alist,'alistcmp');
345 $prevbackend = -1;
346 $headerprinted = false;
347
348 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
349
350 /* List addresses */
351 if (count($alist) > 0) {
352 echo addForm($form_url, 'post');
353 while(list($undef,$row) = each($alist)) {
354
355 /* New table header for each backend */
356 if($prevbackend != $row['backend']) {
357 if($prevbackend < 0) {
358 echo html_tag( 'table',
359 html_tag( 'tr',
360 html_tag( 'td',
361 addSubmit(_("Edit selected"), 'editaddr').
362 addSubmit(_("Delete selected"), 'deladdr'),
363 'center', '', 'colspan="5"' )
364 ) .
365 html_tag( 'tr',
366 html_tag( 'td', '&nbsp;<br />', 'center', '', 'colspan="5"' )
367 ),
368 'center' );
369 echo "\n<!-- start of address book table -->\n" .
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";
386 }
387
388 // Separate different backends with <hr />
389 if($prevbackend > 0) {
390 echo html_tag( 'tr',
391 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
392 );
393 }
394
395 // Print backend name
396 echo html_tag( 'tr',
397 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
398 );
399
400 $line = 0;
401 $headerprinted = true;
402 } /* End of header */
403
404 $prevbackend = $row['backend'];
405
406 /* Check if this user is selected */
407 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
408
409 /* Print one row, with alternating color */
410 if ($line % 2) {
411 $tr_bgcolor = $color[12];
412 } else {
413 $tr_bgcolor = $color[4];
414 }
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%"' );
423 } else {
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;';
431 } else {
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%"' );
443 }
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 }
448 $email = $abook->full_address($row);
449 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
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";
454 $line++;
455 }
456 echo "</table>" .
457 "\n<!-- end of address book table -->\n";
458
459 /* End of list. Add edit/delete select buttons */
460 if ($headerprinted) {
461 echo html_tag( 'table',
462 html_tag( 'tr',
463 html_tag( 'td',
464 addSubmit(_("Edit selected"), 'editaddr') .
465 addSubmit(_("Delete selected"), 'deladdr'),
466 'center', '', 'colspan="5"' )
467 ),
468 'center' );
469 }
470 echo "</form>\n";
471 }
472 } /* end of addresslist */
473
474
475 /* Display the "new address" form */
476 echo '<a name="AddAddress"></a>' . "\n" .
477 addForm($form_url, 'post', 'f_add').
478 html_tag( 'table',
479 html_tag( 'tr',
480 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
481 'center', $color[0]
482 )
483 )
484 , 'center', '', 'width="100%"' ) ."\n";
485 address_form('addaddr', _("Add address"), $defdata);
486 echo "</form>\n";
487
488 /* Add hook for anything that wants on the bottom */
489 do_hook('addressbook_bottom');
490 ?>
491 </body></html>