Uneditable address book entries no longer have checkboxes next to them on addresses...
[squirrelmail.git] / src / addressbook.php
1 <?php
2 /**
3 * addressbook.php
4 *
5 * Copyright (c) 1999-2004 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * Manage personal address book.
9 *
10 * @version $Id$
11 * @package squirrelmail
12 */
13
14 /**
15 * Path for SquirrelMail required files.
16 * @ignore
17 */
18 define('SM_PATH','../');
19
20 /** SquirrelMail required files. */
21 require_once(SM_PATH . 'include/validate.php');
22 require_once(SM_PATH . 'functions/global.php');
23 require_once(SM_PATH . 'functions/display_messages.php');
24 require_once(SM_PATH . 'functions/addressbook.php');
25 require_once(SM_PATH . 'functions/strings.php');
26 require_once(SM_PATH . 'functions/html.php');
27 require_once(SM_PATH . 'functions/forms.php');
28
29 /** lets get the global vars we may need */
30 sqgetGlobalVar('key', $key, SQ_COOKIE);
31
32 sqgetGlobalVar('username', $username, SQ_SESSION);
33 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
34 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
35 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
36
37 /* From the address form */
38 sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
39 sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
40 sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
41 sqgetGlobalVar('sel', $sel, SQ_POST);
42 sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
43 sqgetGlobalVar('backend', $backend, SQ_POST);
44 sqgetGlobalVar('doedit', $doedit, SQ_POST);
45
46 /* Get sorting order */
47 $abook_sort_order = get_abook_sort();
48
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 */
58 function addressbook_inp_field($label, $field, $name, $size, $values, $add) {
59 global $color;
60 $value = ( isset($values[$field]) ? $values[$field] : '');
61
62 $td_str = addInput($name.'['.$field.']', $value, $size)
63 . $add ;
64
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";
70 }
71
72 /**
73 * Output form to add and modify address data
74 */
75 function address_form($name, $submittext, $values = array()) {
76 global $color, $squirrelmail_language;
77
78 if ($squirrelmail_language == 'ja_JP')
79 {
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 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
91 $submittext . '">',
92 'center', $color[4], 'colspan="2"')
93 )
94 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
95 } else {
96 echo html_tag( 'table',
97 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
98 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
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, '') .
103 list_writable_backends($name) .
104 html_tag( 'tr',
105 html_tag( 'td',
106 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
107 $submittext . '">',
108 'center', $color[4], 'colspan="2"')
109 )
110 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
111 }
112 }
113
114 function 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) {
122 $ret .= '<OPTION VALUE=' . $v->bnum;
123 $ret .= '>' . $v->sname . "\n";
124 }
125 }
126 $ret .= "</select>";
127 return html_tag( 'tr',
128 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
129 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
130 } else {
131 return html_tag( 'tr',
132 html_tag( 'td',
133 addHidden('backend', '1'),
134 'center', $color[4], 'colspan="2"')) . "\n";
135 }
136 }
137
138 /* Open addressbook, with error messages on but without LDAP (the *
139 * second "true"). Don't need LDAP here anyway */
140 $abook = addressbook_init(true, true);
141 if($abook->localbackend == 0) {
142 plain_error_message(
143 _("No personal address book is defined. Contact administrator."),
144 $color);
145 exit();
146 }
147
148 displayPageHeader($color, 'None');
149
150 $defdata = array();
151 $formerror = '';
152 $abortform = false;
153 $showaddrlist = true;
154 $defselected = array();
155 $form_url = 'addressbook.php';
156
157
158 /* Handle user's actions */
159 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
160
161 /**************************************************
162 * Add new address *
163 **************************************************/
164 if (isset($addaddr)) {
165 if (isset($backend)) {
166 $r = $abook->add($addaddr, $backend);
167 } else {
168 $r = $abook->add($addaddr, $abook->localbackend);
169 }
170
171 /* Handle error messages */
172 if (!$r) {
173 /* Remove backend name from error string */
174 $errstr = $abook->error;
175 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
176
177 $formerror = $errstr;
178 $showaddrlist = false;
179 $defdata = $addaddr;
180 }
181 } else {
182
183 /************************************************
184 * Delete address(es) *
185 ************************************************/
186 if ((!empty($deladdr)) && sizeof($sel) > 0) {
187 $orig_sel = $sel;
188 sort($sel);
189
190 /* The selected addresses are identidied by "backend:nickname". *
191 * Sort the list and process one backend at the time */
192 $prevback = -1;
193 $subsel = array();
194 $delfailed = false;
195
196 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
197 list($sbackend, $snick) = explode(':', $sel[$i]);
198
199 /* When we get to a new backend, process addresses in *
200 * previous one. */
201 if ($prevback != $sbackend && $prevback != -1) {
202
203 $r = $abook->remove($subsel, $prevback);
204 if (!$r) {
205 $formerror = $abook->error;
206 $i = sizeof($sel);
207 $delfailed = true;
208 break;
209 }
210 $subsel = array();
211 }
212
213 /* Queue for processing */
214 array_push($subsel, $snick);
215 $prevback = $sbackend;
216 }
217
218 if (!$delfailed) {
219 $r = $abook->remove($subsel, $prevback);
220 if (!$r) { /* Handle errors */
221 $formerror = $abook->error;
222 $delfailed = true;
223 }
224 }
225
226 if ($delfailed) {
227 $showaddrlist = true;
228 $defselected = $orig_sel;
229 }
230
231 } else {
232
233 /***********************************************
234 * Update/modify address *
235 ***********************************************/
236 if (!empty($editaddr)) {
237
238 /* Stage one: Copy data into form */
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
249 /* Display the "new address" form */
250 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
251 "\n" .
252 html_tag( 'table',
253 html_tag( 'tr',
254 html_tag( 'td',
255 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
256 'center', $color[0] )
257 ),
258 'center', '', 'width="100%" ' );
259 address_form("editaddr", _("Update address"), $olddata);
260 echo addHidden('oldnick', $olddata['nickname']).
261 addHidden('backend', $olddata['backend']).
262 addHidden('doedit', '1').
263 '</FORM>';
264 }
265 } else {
266
267 /* Stage two: Write new data */
268 if ($doedit = 1) {
269 $newdata = $editaddr;
270 $r = $abook->modify($oldnick, $newdata, $backend);
271
272 /* Handle error messages */
273 if (!$r) {
274 /* Display error */
275 echo html_tag( 'table',
276 html_tag( 'tr',
277 html_tag( 'td',
278 "\n". '<strong><font color="' . $color[2] .
279 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
280 'center' )
281 ),
282 'center', '', 'width="100%"' );
283
284 /* Display the "new address" form again */
285 echo '<FORM ACTION="' . $form_url .
286 '" METHOD="POST">' . "\n" .
287 html_tag( 'table',
288 html_tag( 'tr',
289 html_tag( 'td',
290 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
291 'center', $color[0] )
292 ),
293 'center', '', 'width="100%"' );
294 address_form("editaddr", _("Update address"), $newdata);
295 echo
296 addHidden('oldnick', $oldnick).
297 addHidden('backend', $backend).
298 addHidden('doedit', '1').
299 "\n" . '</FORM>';
300 $abortform = true;
301 }
302 } else {
303
304 /* Should not get here... */
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) {
315 echo "</BODY></HTML>\n";
316 exit();
317 }
318 }
319
320
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 * =================================================================== */
325
326 /* Display error messages */
327 if (!empty($formerror)) {
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 ),
335 'center', '', 'width="100%"' );
336 }
337
338
339 /* Display the address management part */
340 if ($showaddrlist) {
341 /* Get and sort address list */
342 $alist = $abook->list_addr();
343 if(!is_array($alist)) {
344 plain_error_message($abook->error, $color);
345 exit;
346 }
347
348 usort($alist,'alistcmp');
349 $prevbackend = -1;
350 $headerprinted = false;
351
352 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
353
354 /* List addresses */
355 if (count($alist) > 0) {
356 echo '<form action="' . $form_url . '" method="post">' . "\n";
357 while(list($undef,$row) = each($alist)) {
358
359 /* New table header for each backend */
360 if($prevbackend != $row['backend']) {
361 if($prevbackend < 0) {
362 echo html_tag( 'table',
363 html_tag( 'tr',
364 html_tag( 'td',
365 '<input type=submit name=editaddr value="' .
366 _("Edit selected") . "\" />\n" .
367 '<input type=submit name=deladdr value="' .
368 _("Delete selected") . "\" />\n",
369 'center', '', 'colspan="5"' )
370 ) .
371 html_tag( 'tr',
372 html_tag( 'td', '&nbsp;<br>', 'center', '', 'colspan="5"' )
373 ) ,
374 'center' );
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") .
380 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1)
381 , 'left', '', 'width="1%"' ) . "\n" .
382 html_tag( 'th', _("Name") .
383 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3)
384 , 'left', '', 'width="1%"' ) . "\n" .
385 html_tag( 'th', _("E-mail") .
386 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5)
387 , 'left', '', 'width="1%"' ) . "\n" .
388 html_tag( 'th', _("Info") .
389 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7)
390 , 'left', '', 'width="1%"' ) . "\n",
391 '', $color[9] ) . "\n";
392 }
393
394 // Separate different backends with <hr />
395 if($prevbackend > 0) {
396 echo html_tag( 'tr',
397 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
398 );
399 }
400
401 // Print backend name
402 echo html_tag( 'tr',
403 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
404 );
405
406 $line = 0;
407 $headerprinted = true;
408 } /* End of header */
409
410 $prevbackend = $row['backend'];
411
412 /* Check if this user is selected */
413 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
414
415 /* Print one row, with alternating color */
416 if ($line % 2) {
417 $tr_bgcolor = $color[12];
418 } else {
419 $tr_bgcolor = $color[4];
420 }
421 if ($squirrelmail_language == 'ja_JP')
422 {
423 echo html_tag( 'tr', '', '', $tr_bgcolor);
424 if ($abook->backends[$row['backend']]->writeable) {
425 echo html_tag( 'td',
426 '<small>' .
427 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
428 '</small>' ,
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' ) .
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 {
439 echo html_tag( 'tr', '', '', $tr_bgcolor);
440 if ($abook->backends[$row['backend']]->writeable) {
441 echo html_tag( 'td',
442 '<small>' .
443 '<input type=checkbox ' . $selected . ' name="sel[]" value="' .
444 $row['backend'] . ':' . $row['nickname'] . '" /></small>' ,
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' ) .
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;';
454 }
455 $email = $abook->full_address($row);
456 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
457 htmlspecialchars($row['email']) ) .
458 '&nbsp;</td>'."\n".
459 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
460 "</tr>\n";
461 $line++;
462 }
463 echo "</table>" .
464 "\n<!-- end of address book table -->\n";
465
466 /* End of list. Add edit/delete select buttons */
467 if ($headerprinted) {
468 echo html_tag( 'table',
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' );
478 }
479 echo "</form>\n";
480 }
481 } /* end of addresslist */
482
483
484 /* Display the "new address" form */
485 echo '<a name="AddAddress"></a>' . "\n" .
486 '<form action="' . $form_url . '" name="f_add" method="post">' . "\n" .
487 html_tag( 'table',
488 html_tag( 'tr',
489 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
490 'center', $color[0]
491 )
492 )
493 , 'center', '', 'width="100%"' ) ."\n";
494 address_form('addaddr', _("Add address"), $defdata);
495 echo "</form>\n";
496
497 /* Add hook for anything that wants on the bottom */
498 do_hook('addressbook_bottom');
499 ?>
500
501 </body></html>