fix
[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 /**
47 * Make an input field
48 * @param string $label
49 * @param string $field
50 * @param string $name
51 * @param string $size
52 * @param array $values
53 * @param string $add
54 */
55 function addressbook_inp_field($label, $field, $name, $size, $values, $add) {
56 global $color;
57 $value = ( isset($values[$field]) ? $values[$field] : '');
58
59 $td_str = addInput($name.'['.$field.']', $value, $size)
60 . $add ;
61
62 return html_tag( 'tr' ,
63 html_tag( 'td', $label . ':', 'right', $color[4]) .
64 html_tag( 'td', $td_str, 'left', $color[4])
65 )
66 . "\n";
67 }
68
69 /**
70 * Output form to add and modify address data
71 */
72 function address_form($name, $submittext, $values = array()) {
73 global $color, $squirrelmail_language;
74
75 if ($squirrelmail_language == 'ja_JP')
76 {
77 echo html_tag( 'table',
78 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
79 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
80 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
81 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
82 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
83 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
84 list_writable_backends($name) .
85 html_tag( 'tr',
86 html_tag( 'td',
87 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
88 $submittext . '">',
89 'center', $color[4], 'colspan="2"')
90 )
91 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
92 } else {
93 echo html_tag( 'table',
94 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
95 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
96 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
97 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
98 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
99 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
100 list_writable_backends($name) .
101 html_tag( 'tr',
102 html_tag( 'td',
103 '<INPUT TYPE=submit NAME="' . htmlentities($name) . '[SUBMIT]" VALUE="' .
104 $submittext . '">',
105 'center', $color[4], 'colspan="2"')
106 )
107 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
108 }
109 }
110
111 function list_writable_backends($name) {
112 global $color, $abook;
113 if ( $name != 'addaddr' ) { return; }
114 if ( $abook->numbackends > 1 ) {
115 $ret = "<select name=backend>";
116 $backends = $abook->get_backend_list();
117 while (list($undef,$v) = each($backends)) {
118 if ($v->writeable) {
119 $ret .= '<OPTION VALUE=' . $v->bnum;
120 $ret .= '>' . $v->sname . "\n";
121 }
122 }
123 $ret .= "</select>";
124 return html_tag( 'tr',
125 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
126 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
127 } else {
128 return html_tag( 'tr',
129 html_tag( 'td',
130 addHidden('backend', '1'),
131 'center', $color[4], 'colspan="2"')) . "\n";
132 }
133 }
134
135 /* Open addressbook, with error messages on but without LDAP (the *
136 * second "true"). Don't need LDAP here anyway */
137 $abook = addressbook_init(true, true);
138 if($abook->localbackend == 0) {
139 plain_error_message(
140 _("No personal address book is defined. Contact administrator."),
141 $color);
142 exit();
143 }
144
145 displayPageHeader($color, 'None');
146
147 $defdata = array();
148 $formerror = '';
149 $abortform = false;
150 $showaddrlist = true;
151 $defselected = array();
152 $form_url = 'addressbook.php';
153
154
155 /* Handle user's actions */
156 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
157
158 /**************************************************
159 * Add new address *
160 **************************************************/
161 if (isset($addaddr)) {
162 foreach( $addaddr as $k => $adr ) {
163 $addaddr[$k] = strip_tags( $adr );
164 }
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 " <a href=\"$form_url?abook_sort=nickname\">".
381 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" alt=\"sort by nickname\" title=\"" .
382 _("Click here to change the sorting of the address list") .
383 "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
384 html_tag( 'th', _("Name") .
385 " <a href=\"$form_url?abook_sort=name\">" .
386 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
387 "alt=\"sort by name\" title=\"" .
388 _("Click here to change the sorting of the address list") .
389 "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
390 html_tag( 'th', _("E-mail") .
391 " <a href=\"$form_url?abook_sort=email\">" .
392 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
393 "alt=\"sort by email\" title=\"" .
394 _("Click here to change the sorting of the address list") .
395 "\" /></a>", 'left', '', 'width="1%"' ) . "\n" .
396 html_tag( 'th', _("Info") .
397 " <a href=\"$form_url?abook_sort=label\">" .
398 "<img src=\"../images/sort_none.png\" border=\"0\" width=\"12\" height=\"10\" " .
399 "alt=\"sort by info\" title=\"" .
400 _("Click here to change the sorting of the address list") .
401 "\" /></a>", 'left', '', 'width="1%"' ) . "\n",
402 '', $color[9] ) . "\n";
403 }
404
405 // Separate different backends with <hr />
406 if($prevbackend > 0) {
407 echo html_tag( 'tr',
408 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
409 );
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>