bugfix. table was incorrectly closed when system had more than one backend listing.
[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 * $Id$
12 * @package squirrelmail
13 */
14
15 /** Path for SquirrelMail required files. */
16 define('SM_PATH','../');
17
18 /** SquirrelMail required files. */
19 require_once(SM_PATH . 'include/validate.php');
20 require_once(SM_PATH . 'functions/global.php');
21 require_once(SM_PATH . 'functions/display_messages.php');
22 require_once(SM_PATH . 'functions/addressbook.php');
23 require_once(SM_PATH . 'functions/strings.php');
24 require_once(SM_PATH . 'functions/html.php');
25 require_once(SM_PATH . 'functions/forms.php');
26
27 /** lets get the global vars we may need */
28 sqgetGlobalVar('key', $key, SQ_COOKIE);
29
30 sqgetGlobalVar('username', $username, SQ_SESSION);
31 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
32 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
33 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
34
35 /* From the address form */
36 sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
37 sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
38 sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
39 sqgetGlobalVar('sel', $sel, SQ_POST);
40 sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
41 sqgetGlobalVar('backend', $backend, SQ_POST);
42 sqgetGlobalVar('doedit', $doedit, SQ_POST);
43
44 /**
45 * Make an input field
46 * @param string $label
47 * @param string $field
48 * @param string $name
49 * @param string $size
50 * @param array $values
51 * @param string $add
52 */
53 function addressbook_inp_field($label, $field, $name, $size, $values, $add) {
54 global $color;
55 $value = ( isset($values[$field]) ? $values[$field] : '');
56
57 $td_str = addInput($name.'['.$field.']', $value, $size)
58 . $add ;
59
60 return html_tag( 'tr' ,
61 html_tag( 'td', $label . ':', 'right', $color[4]) .
62 html_tag( 'td', $td_str, 'left', $color[4])
63 )
64 . "\n";
65 }
66
67 /**
68 * Output form to add and modify address data
69 */
70 function address_form($name, $submittext, $values = array()) {
71 global $color, $squirrelmail_language;
72
73 if ($squirrelmail_language == 'ja_JP')
74 {
75 echo html_tag( 'table',
76 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
77 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
78 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
79 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
80 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
81 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
82 list_writable_backends($name) .
83 html_tag( 'tr',
84 html_tag( 'td',
85 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
86 $submittext . '">',
87 'center', $color[4], 'colspan="2"')
88 )
89 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
90 } else {
91 echo html_tag( 'table',
92 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
93 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
94 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
95 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
96 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
97 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
98 list_writable_backends($name) .
99 html_tag( 'tr',
100 html_tag( 'td',
101 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
102 $submittext . '">',
103 'center', $color[4], 'colspan="2"')
104 )
105 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
106 }
107 }
108
109 function list_writable_backends($name) {
110 global $color, $abook;
111 if ( $name != 'addaddr' ) { return; }
112 if ( $abook->numbackends > 1 ) {
113 $ret = "<select name=backend>";
114 $backends = $abook->get_backend_list();
115 while (list($undef,$v) = each($backends)) {
116 if ($v->writeable) {
117 $ret .= '<OPTION VALUE=' . $v->bnum;
118 $ret .= '>' . $v->sname . "\n";
119 }
120 }
121 $ret .= "</select>";
122 return html_tag( 'tr',
123 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
124 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
125 } else {
126 return html_tag( 'tr',
127 html_tag( 'td',
128 addHidden('backend', '1'),
129 'center', $color[4], 'colspan="2"')) . "\n";
130 }
131 }
132
133 /* Open addressbook, with error messages on but without LDAP (the *
134 * second "true"). Don't need LDAP here anyway */
135 $abook = addressbook_init(true, true);
136 if($abook->localbackend == 0) {
137 plain_error_message(
138 _("No personal address book is defined. Contact administrator."),
139 $color);
140 exit();
141 }
142
143 displayPageHeader($color, 'None');
144
145 $defdata = array();
146 $formerror = '';
147 $abortform = false;
148 $showaddrlist = true;
149 $defselected = array();
150 $form_url = 'addressbook.php';
151
152
153 /* Handle user's actions */
154 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
155
156 /**************************************************
157 * Add new address *
158 **************************************************/
159 if (isset($addaddr)) {
160 foreach( $addaddr as $k => $adr ) {
161 $addaddr[$k] = strip_tags( $adr );
162 }
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 '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
249 "\n" .
250 html_tag( 'table',
251 html_tag( 'tr',
252 html_tag( 'td',
253 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
254 'center', $color[0] )
255 ),
256 'center', '', 'width="100%" ' );
257 address_form("editaddr", _("Update address"), $olddata);
258 echo addHidden('oldnick', $olddata['nickname']).
259 addHidden('backend', $olddata['backend']).
260 addHidden('doedit', '1').
261 '</FORM>';
262 }
263 } else {
264
265 /* Stage two: Write new data */
266 if ($doedit = 1) {
267 $newdata = $editaddr;
268 $r = $abook->modify($oldnick, $newdata, $backend);
269
270 /* Handle error messages */
271 if (!$r) {
272 /* Display error */
273 echo html_tag( 'table',
274 html_tag( 'tr',
275 html_tag( 'td',
276 "\n". '<strong><font color="' . $color[2] .
277 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
278 'center' )
279 ),
280 'center', '', 'width="100%"' );
281
282 /* Display the "new address" form again */
283 echo '<FORM ACTION="' . $form_url .
284 '" METHOD="POST">' . "\n" .
285 html_tag( 'table',
286 html_tag( 'tr',
287 html_tag( 'td',
288 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
289 'center', $color[0] )
290 ),
291 'center', '', 'width="100%"' );
292 address_form("editaddr", _("Update address"), $newdata);
293 echo
294 addHidden('oldnick', $oldnick).
295 addHidden('backend', $backend).
296 addHidden('doedit', '1').
297 "\n" . '</FORM>';
298 $abortform = true;
299 }
300 } else {
301
302 /* Should not get here... */
303 plain_error_message(_("Unknown error"), $color);
304 $abortform = true;
305 }
306 }
307 } /* !empty($editaddr) - Update/modify address */
308 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
309 } /* !empty($addaddr['nickname']) - Add new address */
310
311 // Some times we end output before forms are printed
312 if($abortform) {
313 echo "</BODY></HTML>\n";
314 exit();
315 }
316 }
317
318
319 /* =================================================================== *
320 * The following is only executed on a GET request, or on a POST when *
321 * a user is added, or when "delete" or "modify" was successful. *
322 * =================================================================== */
323
324 /* Display error messages */
325 if (!empty($formerror)) {
326 echo html_tag( 'table',
327 html_tag( 'tr',
328 html_tag( 'td',
329 "\n". '<br><strong><font color="' . $color[2] .
330 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
331 'center' )
332 ),
333 'center', '', 'width="100%"' );
334 }
335
336
337 /* Display the address management part */
338 if ($showaddrlist) {
339 /* Get and sort address list */
340 $alist = $abook->list_addr();
341 if(!is_array($alist)) {
342 plain_error_message($abook->error, $color);
343 exit;
344 }
345
346 usort($alist,'alistcmp');
347 $prevbackend = -1;
348 $headerprinted = false;
349
350 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
351
352 /* List addresses */
353 if (count($alist) > 0) {
354 echo '<form action="' . $form_url . '" method="post">' . "\n";
355 while(list($undef,$row) = each($alist)) {
356
357 /* New table header for each backend */
358 if($prevbackend != $row['backend']) {
359 if($prevbackend < 0) {
360 echo html_tag( 'table',
361 html_tag( 'tr',
362 html_tag( 'td',
363 '<input type=submit name=editaddr value="' .
364 _("Edit selected") . "\" />\n" .
365 '<input type=submit name=deladdr value="' .
366 _("Delete selected") . "\" />\n",
367 'center', '', 'colspan="5"' )
368 ) .
369 html_tag( 'tr',
370 html_tag( 'td', '&nbsp;<br>', 'center', '', 'colspan="5"' )
371 ) ,
372 'center' );
373 }
374
375 echo html_tag( 'table',
376 html_tag( 'tr',
377 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] )
378 ) ,
379 'center', '', 'width="95%"' ) ."\n" .
380 "<!-- start of address book table -->\n" .
381 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
382 html_tag( 'tr', "\n" .
383 html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) .
384 html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) .
385 html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) .
386 html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) .
387 html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) ,
388 '', $color[9] ) . "\n";
389
390 $line = 0;
391 $headerprinted = true;
392 } /* End of header */
393
394 $prevbackend = $row['backend'];
395
396 /* Check if this user is selected */
397 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
398
399 /* Print one row */
400 $tr_bgcolor = '';
401 if ($line % 2) { $tr_bgcolor = $color[0]; }
402 if ($squirrelmail_language == 'ja_JP')
403 {
404 echo html_tag( 'tr', '', '', $tr_bgcolor) .
405 html_tag( 'td',
406 '<small>' .
407 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
408 '</small>' ,
409 'center', '', 'valign="top" width="1%"' ) .
410 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
411 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
412 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
413 } else {
414 echo html_tag( 'tr', '', '', $tr_bgcolor) .
415 html_tag( 'td',
416 '<small>' .
417 '<input type=checkbox ' . $selected . ' name="sel[]" value="' .
418 $row['backend'] . ':' . $row['nickname'] . '" /></small>' ,
419 'center', '', 'valign="top" width="1%"' ) .
420 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
421 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
422 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
423 }
424 $email = $abook->full_address($row);
425 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
426 htmlspecialchars($row['email']) ) .
427 '&nbsp;</td>'."\n".
428 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
429 "</tr>\n";
430 $line++;
431 echo "</table>\n".
432 "<!-- end of address book table -->\n";
433 }
434
435 /* End of list. Add edit/delete select buttons */
436 if ($headerprinted) {
437 echo html_tag( 'table',
438 html_tag( 'tr',
439 html_tag( 'td',
440 '<input type="submit" name="editaddr" value="' . _("Edit selected") .
441 "\" />\n" .
442 '<input type="submit" name="deladdr" value="' . _("Delete selected") .
443 "\" />\n",
444 'center', '', 'colspan="5"' )
445 ),
446 'center' );
447 }
448 echo "</form>\n";
449 }
450 } /* end of addresslist */
451
452
453 /* Display the "new address" form */
454 echo '<a name="AddAddress"></a>' . "\n" .
455 '<form action="' . $form_url . '" name="f_add" method="post">' . "\n" .
456 html_tag( 'table',
457 html_tag( 'tr',
458 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
459 'center', $color[0]
460 )
461 )
462 , 'center', '', 'width="100%"' ) ."\n";
463 address_form('addaddr', _("Add address"), $defdata);
464 echo "</form>\n";
465
466 /* Add hook for anything that wants on the bottom */
467 do_hook('addressbook_bottom');
468 ?>
469
470 </body></html>