adding list sort options. no sort indicator yet.
[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 /**
48 * Make an input field
49 * @param string $label
50 * @param string $field
51 * @param string $name
52 * @param string $size
53 * @param array $values
54 * @param string $add
55 */
56 function addressbook_inp_field($label, $field, $name, $size, $values, $add) {
57 global $color;
58 $value = ( isset($values[$field]) ? $values[$field] : '');
59
60 $td_str = addInput($name.'['.$field.']', $value, $size)
61 . $add ;
62
63 return html_tag( 'tr' ,
64 html_tag( 'td', $label . ':', 'right', $color[4]) .
65 html_tag( 'td', $td_str, 'left', $color[4])
66 )
67 . "\n";
68 }
69
70 /**
71 * Output form to add and modify address data
72 */
73 function address_form($name, $submittext, $values = array()) {
74 global $color, $squirrelmail_language;
75
76 if ($squirrelmail_language == 'ja_JP')
77 {
78 echo html_tag( 'table',
79 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
80 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
81 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
82 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
83 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
84 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
85 list_writable_backends($name) .
86 html_tag( 'tr',
87 html_tag( 'td',
88 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
89 $submittext . '">',
90 'center', $color[4], 'colspan="2"')
91 )
92 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
93 } else {
94 echo html_tag( 'table',
95 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
96 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
97 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
98 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
99 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
100 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
101 list_writable_backends($name) .
102 html_tag( 'tr',
103 html_tag( 'td',
104 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
105 $submittext . '">',
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 . "\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 foreach( $addaddr as $k => $adr ) {
164 $addaddr[$k] = strip_tags( $adr );
165 }
166 if (isset($backend)) {
167 $r = $abook->add($addaddr, $backend);
168 } else {
169 $r = $abook->add($addaddr, $abook->localbackend);
170 }
171
172 /* Handle error messages */
173 if (!$r) {
174 /* Remove backend name from error string */
175 $errstr = $abook->error;
176 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
177
178 $formerror = $errstr;
179 $showaddrlist = false;
180 $defdata = $addaddr;
181 }
182 } else {
183
184 /************************************************
185 * Delete address(es) *
186 ************************************************/
187 if ((!empty($deladdr)) && sizeof($sel) > 0) {
188 $orig_sel = $sel;
189 sort($sel);
190
191 /* The selected addresses are identidied by "backend:nickname". *
192 * Sort the list and process one backend at the time */
193 $prevback = -1;
194 $subsel = array();
195 $delfailed = false;
196
197 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
198 list($sbackend, $snick) = explode(':', $sel[$i]);
199
200 /* When we get to a new backend, process addresses in *
201 * previous one. */
202 if ($prevback != $sbackend && $prevback != -1) {
203
204 $r = $abook->remove($subsel, $prevback);
205 if (!$r) {
206 $formerror = $abook->error;
207 $i = sizeof($sel);
208 $delfailed = true;
209 break;
210 }
211 $subsel = array();
212 }
213
214 /* Queue for processing */
215 array_push($subsel, $snick);
216 $prevback = $sbackend;
217 }
218
219 if (!$delfailed) {
220 $r = $abook->remove($subsel, $prevback);
221 if (!$r) { /* Handle errors */
222 $formerror = $abook->error;
223 $delfailed = true;
224 }
225 }
226
227 if ($delfailed) {
228 $showaddrlist = true;
229 $defselected = $orig_sel;
230 }
231
232 } else {
233
234 /***********************************************
235 * Update/modify address *
236 ***********************************************/
237 if (!empty($editaddr)) {
238
239 /* Stage one: Copy data into form */
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
250 /* Display the "new address" form */
251 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
252 "\n" .
253 html_tag( 'table',
254 html_tag( 'tr',
255 html_tag( 'td',
256 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
257 'center', $color[0] )
258 ),
259 'center', '', 'width="100%" ' );
260 address_form("editaddr", _("Update address"), $olddata);
261 echo addHidden('oldnick', $olddata['nickname']).
262 addHidden('backend', $olddata['backend']).
263 addHidden('doedit', '1').
264 '</FORM>';
265 }
266 } else {
267
268 /* Stage two: Write new data */
269 if ($doedit = 1) {
270 $newdata = $editaddr;
271 $r = $abook->modify($oldnick, $newdata, $backend);
272
273 /* Handle error messages */
274 if (!$r) {
275 /* Display error */
276 echo html_tag( 'table',
277 html_tag( 'tr',
278 html_tag( 'td',
279 "\n". '<strong><font color="' . $color[2] .
280 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
281 'center' )
282 ),
283 'center', '', 'width="100%"' );
284
285 /* Display the "new address" form again */
286 echo '<FORM ACTION="' . $form_url .
287 '" METHOD="POST">' . "\n" .
288 html_tag( 'table',
289 html_tag( 'tr',
290 html_tag( 'td',
291 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
292 'center', $color[0] )
293 ),
294 'center', '', 'width="100%"' );
295 address_form("editaddr", _("Update address"), $newdata);
296 echo
297 addHidden('oldnick', $oldnick).
298 addHidden('backend', $backend).
299 addHidden('doedit', '1').
300 "\n" . '</FORM>';
301 $abortform = true;
302 }
303 } else {
304
305 /* Should not get here... */
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) {
316 echo "</BODY></HTML>\n";
317 exit();
318 }
319 }
320
321
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 * =================================================================== */
326
327 /* Display error messages */
328 if (!empty($formerror)) {
329 echo html_tag( 'table',
330 html_tag( 'tr',
331 html_tag( 'td',
332 "\n". '<br><strong><font color="' . $color[2] .
333 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
334 'center' )
335 ),
336 'center', '', 'width="100%"' );
337 }
338
339
340 /* Display the address management part */
341 if ($showaddrlist) {
342 /* Get and sort address list */
343 $alist = $abook->list_addr();
344 if(!is_array($alist)) {
345 plain_error_message($abook->error, $color);
346 exit;
347 }
348
349 usort($alist,'alistcmp');
350 $prevbackend = -1;
351 $headerprinted = false;
352
353 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
354
355 /* List addresses */
356 if (count($alist) > 0) {
357 echo '<form action="' . $form_url . '" method="post">' . "\n";
358 while(list($undef,$row) = each($alist)) {
359
360 /* New table header for each backend */
361 if($prevbackend != $row['backend']) {
362 if($prevbackend < 0) {
363 echo html_tag( 'table',
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' );
376 echo "\n<!-- start of address book table -->\n" .
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 " <a href=\"$form_url?abook_sort=nickname\">".
382 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" alt=\"sort by nickname\" title=\"" .
383 _("Click here to change the sorting of the address list") .
384 "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
385 html_tag( 'th', _("Name") .
386 " <a href=\"$form_url?abook_sort=name\">" .
387 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
388 "alt=\"sort by name\" title=\"" .
389 _("Click here to change the sorting of the address list") .
390 "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
391 html_tag( 'th', _("E-mail") .
392 " <a href=\"$form_url?abook_sort=email\">" .
393 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
394 "alt=\"sort by email\" title=\"" .
395 _("Click here to change the sorting of the address list") .
396 "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
397 html_tag( 'th', _("Info") .
398 " <a href=\"$form_url?abook_sort=label\">" .
399 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
400 "alt=\"sort by info\" title=\"" .
401 _("Click here to change the sorting of the address list") .
402 "\" /></a>", 'left', '', 'width="1%"' ) . "\n",
403 '', $color[9] ) . "\n";
404 }
405
406 // Separate different backends with <hr />
407 if($prevbackend > 0) {
408 echo html_tag( 'tr',
409 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
410 );
411 }
412 // Print backend name
413 echo html_tag( 'tr',
414 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
415 );
416
417 $line = 0;
418 $headerprinted = true;
419 } /* End of header */
420
421 $prevbackend = $row['backend'];
422
423 /* Check if this user is selected */
424 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
425
426 /* Print one row */
427 $tr_bgcolor = '';
428 if ($line % 2) { $tr_bgcolor = $color[0]; }
429 if ($squirrelmail_language == 'ja_JP')
430 {
431 echo html_tag( 'tr', '', '', $tr_bgcolor) .
432 html_tag( 'td',
433 '<small>' .
434 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
435 '</small>' ,
436 'center', '', 'valign="top" width="1%"' ) .
437 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
438 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
439 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
440 } else {
441 echo html_tag( 'tr', '', '', $tr_bgcolor) .
442 html_tag( 'td',
443 '<small>' .
444 '<input type=checkbox ' . $selected . ' name="sel[]" value="' .
445 $row['backend'] . ':' . $row['nickname'] . '" /></small>' ,
446 'center', '', 'valign="top" width="1%"' ) .
447 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
448 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
449 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
450 }
451 $email = $abook->full_address($row);
452 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
453 htmlspecialchars($row['email']) ) .
454 '&nbsp;</td>'."\n".
455 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
456 "</tr>\n";
457 $line++;
458 }
459 echo "</table>" .
460 "\n<!-- end of address book table -->\n";
461
462 /* End of list. Add edit/delete select buttons */
463 if ($headerprinted) {
464 echo html_tag( 'table',
465 html_tag( 'tr',
466 html_tag( 'td',
467 '<input type="submit" name="editaddr" value="' . _("Edit selected") .
468 "\" />\n" .
469 '<input type="submit" name="deladdr" value="' . _("Delete selected") .
470 "\" />\n",
471 'center', '', 'colspan="5"' )
472 ),
473 'center' );
474 }
475 echo "</form>\n";
476 }
477 } /* end of addresslist */
478
479
480 /* Display the "new address" form */
481 echo '<a name="AddAddress"></a>' . "\n" .
482 '<form action="' . $form_url . '" name="f_add" method="post">' . "\n" .
483 html_tag( 'table',
484 html_tag( 'tr',
485 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
486 'center', $color[0]
487 )
488 )
489 , 'center', '', 'width="100%"' ) ."\n";
490 address_form('addaddr', _("Add address"), $defdata);
491 echo "</form>\n";
492
493 /* Add hook for anything that wants on the bottom */
494 do_hook('addressbook_bottom');
495 ?>
496
497 </body></html>