6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Manage personal address book.
12 * @package squirrelmail
15 /** Path for SquirrelMail required files. */
16 define('SM_PATH','../');
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');
26 /** lets get the global vars we may need */
27 sqgetGlobalVar('key', $key, SQ_COOKIE
);
29 sqgetGlobalVar('username', $username, SQ_SESSION
);
30 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
31 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
32 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
34 /* From the address form */
35 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
36 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
37 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
38 sqgetGlobalVar('sel', $sel, SQ_POST
);
39 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
40 sqgetGlobalVar('backend', $backend, SQ_POST
);
41 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
45 * @param string $label
46 * @param string $field
49 * @param array $values
52 function adressbook_inp_field($label, $field, $name, $size, $values, $add) {
54 $td_str = '<INPUT NAME="' . $name . '[' . $field . ']" SIZE="' . $size . '" VALUE="';
55 if (isset($values[$field])) {
56 $td_str .= htmlspecialchars( strip_tags( $values[$field] ) );
58 $td_str .= '">' . $add . '';
59 return html_tag( 'tr' ,
60 html_tag( 'td', $label . ':', 'right', $color[4]) .
61 html_tag( 'td', $td_str, 'left', $color[4])
67 * Output form to add and modify address data
69 function address_form($name, $submittext, $values = array()) {
70 global $color, $squirrelmail_language;
72 if ($squirrelmail_language == 'ja_JP')
74 echo html_tag( 'table',
75 adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
76 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
77 adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
78 adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
79 adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
80 adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
81 list_writable_backends($name) .
84 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
86 'center', $color[4], 'colspan="2"')
88 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
90 echo html_tag( 'table',
91 adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
92 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
93 adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
94 adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
95 adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
96 adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
97 list_writable_backends($name) .
100 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
102 'center', $color[4], 'colspan="2"')
104 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
108 function list_writable_backends($name) {
109 global $color, $abook;
110 if ( $name != 'addaddr' ) { return; }
111 if ( $abook->numbackends
> 1 ) {
112 $ret = "<select name=backend>";
113 $backends = $abook->get_backend_list();
114 while (list($undef,$v) = each($backends)) {
116 $ret .= '<OPTION VALUE=' . $v->bnum
;
117 $ret .= '>' . $v->sname
. "\n";
121 return html_tag( 'tr',
122 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
123 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
125 return html_tag( 'tr',
127 '<input type=hidden name=backend value=1>',
128 'center', $color[4], 'colspan="2"')) . "\n";
132 /* Open addressbook, with error messages on but without LDAP (the *
133 * second "true"). Don't need LDAP here anyway */
134 $abook = addressbook_init(true, true);
135 if($abook->localbackend
== 0) {
137 _("No personal address book is defined. Contact administrator."),
142 displayPageHeader($color, 'None');
147 $showaddrlist = true;
148 $defselected = array();
149 $form_url = 'addressbook.php';
152 /* Handle user's actions */
153 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
155 /**************************************************
157 **************************************************/
158 if (isset($addaddr)) {
159 foreach( $addaddr as $k => $adr ) {
160 $addaddr[$k] = strip_tags( $adr );
162 if (isset($backend)) {
163 $r = $abook->add($addaddr, $backend);
165 $r = $abook->add($addaddr, $abook->localbackend
);
168 /* Handle error messages */
170 /* Remove backend name from error string */
171 $errstr = $abook->error
;
172 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
174 $formerror = $errstr;
175 $showaddrlist = false;
180 /************************************************
181 * Delete address(es) *
182 ************************************************/
183 if ((!empty($deladdr)) && sizeof($sel) > 0) {
187 /* The selected addresses are identidied by "backend:nickname". *
188 * Sort the list and process one backend at the time */
193 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
194 list($sbackend, $snick) = explode(':', $sel[$i]);
196 /* When we get to a new backend, process addresses in *
198 if ($prevback != $sbackend && $prevback != -1) {
200 $r = $abook->remove($subsel, $prevback);
202 $formerror = $abook->error
;
210 /* Queue for processing */
211 array_push($subsel, $snick);
212 $prevback = $sbackend;
216 $r = $abook->remove($subsel, $prevback);
217 if (!$r) { /* Handle errors */
218 $formerror = $abook->error
;
224 $showaddrlist = true;
225 $defselected = $orig_sel;
230 /***********************************************
231 * Update/modify address *
232 ***********************************************/
233 if (!empty($editaddr)) {
235 /* Stage one: Copy data into form */
236 if (isset($sel) && sizeof($sel) > 0) {
237 if(sizeof($sel) > 1) {
238 $formerror = _("You can only edit one address at the time");
239 $showaddrlist = true;
243 list($ebackend, $enick) = explode(':', $sel[0]);
244 $olddata = $abook->lookup($enick, $ebackend);
246 /* Display the "new address" form */
247 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
252 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
253 'center', $color[0] )
255 'center', '', 'width="100%" ' );
256 address_form("editaddr", _("Update address"), $olddata);
257 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
258 htmlspecialchars($olddata["nickname"]) . "\">\n" .
259 '<INPUT TYPE=hidden NAME=backend VALUE="' .
260 htmlspecialchars($olddata["backend"]) . "\">\n" .
261 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" .
266 /* Stage two: Write new data */
268 $newdata = $editaddr;
269 $r = $abook->modify($oldnick, $newdata, $backend);
271 /* Handle error messages */
274 echo html_tag( 'table',
277 "\n". '<strong><font color="' . $color[2] .
278 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
281 'center', '', 'width="100%"' );
283 /* Display the "new address" form again */
284 echo '<FORM ACTION="' . $form_url .
285 '" METHOD="POST">' . "\n" .
289 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
290 'center', $color[0] )
292 'center', '', 'width="100%"' );
293 address_form("editaddr", _("Update address"), $newdata);
294 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
295 htmlspecialchars($oldnick) . "\">\n" .
296 '<INPUT TYPE=hidden NAME=backend VALUE="' .
297 htmlspecialchars($backend) . "\">\n" .
298 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' .
304 /* Should not get here... */
305 plain_error_message(_("Unknown error"), $color);
309 } /* !empty($editaddr) - Update/modify address */
310 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
311 } /* !empty($addaddr['nickname']) - Add new address */
313 // Some times we end output before forms are printed
315 echo "</BODY></HTML>\n";
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 * =================================================================== */
326 /* Display error messages */
327 if (!empty($formerror)) {
328 echo html_tag( 'table',
331 "\n". '<br><strong><font color="' . $color[2] .
332 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
335 'center', '', 'width="100%"' );
339 /* Display the address management part */
341 /* Get and sort address list */
342 $alist = $abook->list_addr();
343 if(!is_array($alist)) {
344 plain_error_message($abook->error
, $color);
348 usort($alist,'alistcmp');
350 $headerprinted = false;
352 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
355 if (count($alist) > 0) {
356 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' . "\n";
357 while(list($undef,$row) = each($alist)) {
359 /* New table header for each backend */
360 if($prevbackend != $row['backend']) {
361 if($prevbackend < 0) {
362 echo html_tag( 'table',
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"' )
372 html_tag( 'td', ' <br>', 'center', '', 'colspan="5"' )
377 echo html_tag( 'table',
379 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] )
381 'center', '', 'width="95%"' ) ."\n"
382 . html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
383 html_tag( 'tr', "\n" .
384 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) .
385 html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) .
386 html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) .
387 html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) .
388 html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) ,
389 '', $color[9] ) . "\n";
392 $headerprinted = true;
393 } /* End of header */
395 $prevbackend = $row['backend'];
397 /* Check if this user is selected */
398 if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) {
399 $selected = 'CHECKED';
406 if ($line %
2) { $tr_bgcolor = $color[0]; }
407 if ($squirrelmail_language == 'ja_JP')
409 echo html_tag( 'tr', '', '', $tr_bgcolor) .
412 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
413 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
414 'center', '', 'valign="top" width="1%"' ) .
415 html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
416 html_tag( 'td', ' ' . $row['lastname'] . ' ' . $row['firstname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
417 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
419 echo html_tag( 'tr', '', '', $tr_bgcolor) .
422 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
423 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
424 'center', '', 'valign="top" width="1%"' ) .
425 html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
426 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
427 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
429 $email = $abook->full_address($row);
430 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
431 htmlspecialchars($row['email']) ) .
433 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
438 /* End of list. Close table. */
439 if ($headerprinted) {
442 '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
444 '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
446 'center', '', 'colspan="5"' )
449 echo '</table></FORM>';
451 } /* end of addresslist */
454 /* Display the "new address" form */
455 echo '<a name="AddAddress"></a>' . "\n" .
456 '<FORM ACTION="' . $form_url . '" NAME=f_add METHOD="POST">' . "\n" .
459 html_tag( 'td', "\n". '<strong>' . _("Add to address book") . '</strong>' . "\n",
463 , 'center', '', 'width="100%"' ) ."\n";
464 address_form('addaddr', _("Add address"), $defdata);
467 /* Add hook for anything that wants on the bottom */
468 do_hook('addressbook_bottom');