Adding Basque translators' info.
[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 foreach( $addaddr as $k => $adr ) {
166 $addaddr[$k] = strip_tags( $adr );
167 }
168 if (isset($backend)) {
169 $r = $abook->add($addaddr, $backend);
170 } else {
171 $r = $abook->add($addaddr, $abook->localbackend);
172 }
173
174 /* Handle error messages */
175 if (!$r) {
176 /* Remove backend name from error string */
177 $errstr = $abook->error;
178 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
179
180 $formerror = $errstr;
181 $showaddrlist = false;
182 $defdata = $addaddr;
183 }
184 } else {
185
186 /************************************************
187 * Delete address(es) *
188 ************************************************/
189 if ((!empty($deladdr)) && sizeof($sel) > 0) {
190 $orig_sel = $sel;
191 sort($sel);
192
193 /* The selected addresses are identidied by "backend:nickname". *
194 * Sort the list and process one backend at the time */
195 $prevback = -1;
196 $subsel = array();
197 $delfailed = false;
198
199 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
200 list($sbackend, $snick) = explode(':', $sel[$i]);
201
202 /* When we get to a new backend, process addresses in *
203 * previous one. */
204 if ($prevback != $sbackend && $prevback != -1) {
205
206 $r = $abook->remove($subsel, $prevback);
207 if (!$r) {
208 $formerror = $abook->error;
209 $i = sizeof($sel);
210 $delfailed = true;
211 break;
212 }
213 $subsel = array();
214 }
215
216 /* Queue for processing */
217 array_push($subsel, $snick);
218 $prevback = $sbackend;
219 }
220
221 if (!$delfailed) {
222 $r = $abook->remove($subsel, $prevback);
223 if (!$r) { /* Handle errors */
224 $formerror = $abook->error;
225 $delfailed = true;
226 }
227 }
228
229 if ($delfailed) {
230 $showaddrlist = true;
231 $defselected = $orig_sel;
232 }
233
234 } else {
235
236 /***********************************************
237 * Update/modify address *
238 ***********************************************/
239 if (!empty($editaddr)) {
240
241 /* Stage one: Copy data into form */
242 if (isset($sel) && sizeof($sel) > 0) {
243 if(sizeof($sel) > 1) {
244 $formerror = _("You can only edit one address at the time");
245 $showaddrlist = true;
246 $defselected = $sel;
247 } else {
248 $abortform = true;
249 list($ebackend, $enick) = explode(':', $sel[0]);
250 $olddata = $abook->lookup($enick, $ebackend);
251
252 /* Display the "new address" form */
253 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
254 "\n" .
255 html_tag( 'table',
256 html_tag( 'tr',
257 html_tag( 'td',
258 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
259 'center', $color[0] )
260 ),
261 'center', '', 'width="100%" ' );
262 address_form("editaddr", _("Update address"), $olddata);
263 echo addHidden('oldnick', $olddata['nickname']).
264 addHidden('backend', $olddata['backend']).
265 addHidden('doedit', '1').
266 '</FORM>';
267 }
268 } else {
269
270 /* Stage two: Write new data */
271 if ($doedit = 1) {
272 $newdata = $editaddr;
273 $r = $abook->modify($oldnick, $newdata, $backend);
274
275 /* Handle error messages */
276 if (!$r) {
277 /* Display error */
278 echo html_tag( 'table',
279 html_tag( 'tr',
280 html_tag( 'td',
281 "\n". '<strong><font color="' . $color[2] .
282 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
283 'center' )
284 ),
285 'center', '', 'width="100%"' );
286
287 /* Display the "new address" form again */
288 echo '<FORM ACTION="' . $form_url .
289 '" METHOD="POST">' . "\n" .
290 html_tag( 'table',
291 html_tag( 'tr',
292 html_tag( 'td',
293 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
294 'center', $color[0] )
295 ),
296 'center', '', 'width="100%"' );
297 address_form("editaddr", _("Update address"), $newdata);
298 echo
299 addHidden('oldnick', $oldnick).
300 addHidden('backend', $backend).
301 addHidden('doedit', '1').
302 "\n" . '</FORM>';
303 $abortform = true;
304 }
305 } else {
306
307 /* Should not get here... */
308 plain_error_message(_("Unknown error"), $color);
309 $abortform = true;
310 }
311 }
312 } /* !empty($editaddr) - Update/modify address */
313 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
314 } /* !empty($addaddr['nickname']) - Add new address */
315
316 // Some times we end output before forms are printed
317 if($abortform) {
318 echo "</BODY></HTML>\n";
319 exit();
320 }
321 }
322
323
324 /* =================================================================== *
325 * The following is only executed on a GET request, or on a POST when *
326 * a user is added, or when "delete" or "modify" was successful. *
327 * =================================================================== */
328
329 /* Display error messages */
330 if (!empty($formerror)) {
331 echo html_tag( 'table',
332 html_tag( 'tr',
333 html_tag( 'td',
334 "\n". '<br><strong><font color="' . $color[2] .
335 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
336 'center' )
337 ),
338 'center', '', 'width="100%"' );
339 }
340
341
342 /* Display the address management part */
343 if ($showaddrlist) {
344 /* Get and sort address list */
345 $alist = $abook->list_addr();
346 if(!is_array($alist)) {
347 plain_error_message($abook->error, $color);
348 exit;
349 }
350
351 usort($alist,'alistcmp');
352 $prevbackend = -1;
353 $headerprinted = false;
354
355 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
356
357 /* List addresses */
358 if (count($alist) > 0) {
359 echo '<form action="' . $form_url . '" method="post">' . "\n";
360 while(list($undef,$row) = each($alist)) {
361
362 /* New table header for each backend */
363 if($prevbackend != $row['backend']) {
364 if($prevbackend < 0) {
365 echo html_tag( 'table',
366 html_tag( 'tr',
367 html_tag( 'td',
368 '<input type=submit name=editaddr value="' .
369 _("Edit selected") . "\" />\n" .
370 '<input type=submit name=deladdr value="' .
371 _("Delete selected") . "\" />\n",
372 'center', '', 'colspan="5"' )
373 ) .
374 html_tag( 'tr',
375 html_tag( 'td', '&nbsp;<br>', 'center', '', 'colspan="5"' )
376 ) ,
377 'center' );
378 echo "\n<!-- start of address book table -->\n" .
379 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
380 html_tag( 'tr', "\n" .
381 html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) . "\n" .
382 html_tag( 'th', _("Nickname") .
383 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1)
384 , 'left', '', 'width="1%"' ) . "\n" .
385 html_tag( 'th', _("Name") .
386 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3)
387 , 'left', '', 'width="1%"' ) . "\n" .
388 html_tag( 'th', _("E-mail") .
389 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5)
390 , 'left', '', 'width="1%"' ) . "\n" .
391 html_tag( 'th', _("Info") .
392 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7)
393 , 'left', '', 'width="1%"' ) . "\n",
394 '', $color[9] ) . "\n";
395 }
396
397 // Separate different backends with <hr />
398 if($prevbackend > 0) {
399 echo html_tag( 'tr',
400 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
401 );
402 }
403
404 // Print backend name
405 echo html_tag( 'tr',
406 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
407 );
408
409 $line = 0;
410 $headerprinted = true;
411 } /* End of header */
412
413 $prevbackend = $row['backend'];
414
415 /* Check if this user is selected */
416 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
417
418 /* Print one row */
419 $tr_bgcolor = '';
420 if ($line % 2) { $tr_bgcolor = $color[0]; }
421 if ($squirrelmail_language == 'ja_JP')
422 {
423 echo html_tag( 'tr', '', '', $tr_bgcolor) .
424 html_tag( 'td',
425 '<small>' .
426 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
427 '</small>' ,
428 'center', '', 'valign="top" width="1%"' ) .
429 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
430 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
431 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
432 } else {
433 echo html_tag( 'tr', '', '', $tr_bgcolor) .
434 html_tag( 'td',
435 '<small>' .
436 '<input type=checkbox ' . $selected . ' name="sel[]" value="' .
437 $row['backend'] . ':' . $row['nickname'] . '" /></small>' ,
438 'center', '', 'valign="top" width="1%"' ) .
439 html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
440 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
441 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
442 }
443 $email = $abook->full_address($row);
444 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
445 htmlspecialchars($row['email']) ) .
446 '&nbsp;</td>'."\n".
447 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
448 "</tr>\n";
449 $line++;
450 }
451 echo "</table>" .
452 "\n<!-- end of address book table -->\n";
453
454 /* End of list. Add edit/delete select buttons */
455 if ($headerprinted) {
456 echo html_tag( 'table',
457 html_tag( 'tr',
458 html_tag( 'td',
459 '<input type="submit" name="editaddr" value="' . _("Edit selected") .
460 "\" />\n" .
461 '<input type="submit" name="deladdr" value="' . _("Delete selected") .
462 "\" />\n",
463 'center', '', 'colspan="5"' )
464 ),
465 'center' );
466 }
467 echo "</form>\n";
468 }
469 } /* end of addresslist */
470
471
472 /* Display the "new address" form */
473 echo '<a name="AddAddress"></a>' . "\n" .
474 '<form action="' . $form_url . '" name="f_add" method="post">' . "\n" .
475 html_tag( 'table',
476 html_tag( 'tr',
477 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
478 'center', $color[0]
479 )
480 )
481 , 'center', '', 'width="100%"' ) ."\n";
482 address_form('addaddr', _("Add address"), $defdata);
483 echo "</form>\n";
484
485 /* Add hook for anything that wants on the bottom */
486 do_hook('addressbook_bottom');
487 ?>
488
489 </body></html>