Always display $org_name in the title
[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 /* Get sorting order */
48 $abook_sort_order = get_abook_sort();
49
50 /**
51 * Make an input field
52 * @param string $label
53 * @param string $field
54 * @param string $name
55 * @param string $size
56 * @param array $values
57 * @param string $add
58 */
59 function addressbook_inp_field($label, $field, $name, $size, $values, $add) {
60 global $color;
61 $value = ( isset($values[$field]) ? $values[$field] : '');
62
63 $td_str = addInput($name.'['.$field.']', $value, $size)
64 . $add ;
65
66 return html_tag( 'tr' ,
67 html_tag( 'td', $label . ':', 'right', $color[4]) .
68 html_tag( 'td', $td_str, 'left', $color[4])
69 )
70 . "\n";
71 }
72
73 /**
74 * Output form to add and modify address data
75 */
76 function address_form($name, $submittext, $values = array()) {
77 global $color, $squirrelmail_language;
78
79 if ($squirrelmail_language == 'ja_JP')
80 {
81 echo html_tag( 'table',
82 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
83 ' <small>' . _("Must be unique") . '</small>') .
84 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
85 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
86 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
87 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
88 list_writable_backends($name) .
89 html_tag( 'tr',
90 html_tag( 'td',
91 '<input type="submit" name="' . htmlentities($name) . '[SUBMIT]" value="' .
92 $submittext . '" />',
93 'center', $color[4], 'colspan="2"')
94 )
95 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
96 } else {
97 echo html_tag( 'table',
98 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
99 ' <small>' . _("Must be unique") . '</small>') .
100 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
101 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
102 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
103 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
104 list_writable_backends($name) .
105 html_tag( 'tr',
106 html_tag( 'td',
107 '<input type="submit" name="' . htmlentities($name) . '[SUBMIT]" value="' .
108 $submittext . '" />',
109 'center', $color[4], 'colspan="2"')
110 )
111 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
112 }
113 }
114
115 function list_writable_backends($name) {
116 global $color, $abook;
117 if ( $name != 'addaddr' ) { return; }
118 if ( $abook->numbackends > 1 ) {
119 $ret = '<select name="backend">';
120 $backends = $abook->get_backend_list();
121 while (list($undef,$v) = each($backends)) {
122 if ($v->writeable) {
123 $ret .= '<option value="' . $v->bnum;
124 $ret .= '">' . $v->sname . "</option>\n";
125 }
126 }
127 $ret .= "</select>";
128 return html_tag( 'tr',
129 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
130 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
131 } else {
132 return html_tag( 'tr',
133 html_tag( 'td',
134 addHidden('backend', '1'),
135 'center', $color[4], 'colspan="2"')) . "\n";
136 }
137 }
138
139 /* Open addressbook, with error messages on but without LDAP (the *
140 * second "true"). Don't need LDAP here anyway */
141 $abook = addressbook_init(true, true);
142 if($abook->localbackend == 0) {
143 plain_error_message(
144 _("No personal address book is defined. Contact administrator."),
145 $color);
146 exit();
147 }
148
149 displayPageHeader($color, 'None');
150
151 $defdata = array();
152 $formerror = '';
153 $abortform = false;
154 $showaddrlist = true;
155 $defselected = array();
156 $form_url = 'addressbook.php';
157
158
159 /* Handle user's actions */
160 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
161
162 /**************************************************
163 * Add new address *
164 **************************************************/
165 if (isset($addaddr)) {
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 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
382 'left', '', 'width="1%"' ) . "\n" .
383 html_tag( 'th', _("Name") .
384 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
385 'left', '', 'width="1%"' ) . "\n" .
386 html_tag( 'th', _("E-mail") .
387 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
388 'left', '', 'width="1%"' ) . "\n" .
389 html_tag( 'th', _("Info") .
390 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
391 'left', '', 'width="1%"' ) . "\n",
392 '', $color[9] ) . "\n";
393 }
394
395 // Separate different backends with <hr />
396 if($prevbackend > 0) {
397 echo html_tag( 'tr',
398 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
399 );
400 }
401
402 // Print backend name
403 echo html_tag( 'tr',
404 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
405 );
406
407 $line = 0;
408 $headerprinted = true;
409 } /* End of header */
410
411 $prevbackend = $row['backend'];
412
413 /* Check if this user is selected */
414 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
415
416 /* Print one row, with alternating color */
417 if ($line % 2) {
418 $tr_bgcolor = $color[12];
419 } else {
420 $tr_bgcolor = $color[4];
421 }
422 if ($squirrelmail_language == 'ja_JP')
423 {
424 echo html_tag( 'tr', '', '', $tr_bgcolor);
425 if ($abook->backends[$row['backend']]->writeable) {
426 echo html_tag( 'td',
427 '<small>' .
428 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
429 '</small>' ,
430 'center', '', 'valign="top" width="1%"' );
431 } else {
432 echo html_tag( 'td',
433 '&nbsp;' ,
434 'center', '', 'valign="top" width="1%"' );
435 }
436 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
437 html_tag( 'td', '&nbsp;' . $row['lastname'] . ' ' . $row['firstname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
438 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
439 } else {
440 echo html_tag( 'tr', '', '', $tr_bgcolor);
441 if ($abook->backends[$row['backend']]->writeable) {
442 echo 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 } else {
448 echo html_tag( 'td',
449 '&nbsp;' ,
450 'center', '', 'valign="top" width="1%"' );
451 }
452 echo html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
453 html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
454 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
455 }
456 $email = $abook->full_address($row);
457 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
458 htmlspecialchars($row['email']) ) .
459 '&nbsp;</td>'."\n".
460 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
461 "</tr>\n";
462 $line++;
463 }
464 echo "</table>" .
465 "\n<!-- end of address book table -->\n";
466
467 /* End of list. Add edit/delete select buttons */
468 if ($headerprinted) {
469 echo html_tag( 'table',
470 html_tag( 'tr',
471 html_tag( 'td',
472 '<input type="submit" name="editaddr" value="' . _("Edit selected") .
473 "\" />\n" .
474 '<input type="submit" name="deladdr" value="' . _("Delete selected") .
475 "\" />\n",
476 'center', '', 'colspan="5"' )
477 ),
478 'center' );
479 }
480 echo "</form>\n";
481 }
482 } /* end of addresslist */
483
484
485 /* Display the "new address" form */
486 echo '<a name="AddAddress"></a>' . "\n" .
487 '<form action="' . $form_url . '" name="f_add" method="post">' . "\n" .
488 html_tag( 'table',
489 html_tag( 'tr',
490 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
491 'center', $color[0]
492 )
493 )
494 , 'center', '', 'width="100%"' ) ."\n";
495 address_form('addaddr', _("Add address"), $defdata);
496 echo "</form>\n";
497
498 /* Add hook for anything that wants on the bottom */
499 do_hook('addressbook_bottom');
500 ?>
501 </body></html>