6 * Copyright (c) 1999-2003 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, '') .
83 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
85 'center', $color[4], 'colspan="2"')
87 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
89 echo html_tag( 'table',
90 adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
91 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
92 adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
93 adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
94 adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
95 adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
98 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
100 'center', $color[4], 'colspan="2"')
102 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
106 /* Open addressbook, with error messages on but without LDAP (the *
107 * second "true"). Don't need LDAP here anyway */
108 $abook = addressbook_init(true, true);
109 if($abook->localbackend
== 0) {
111 _("No personal address book is defined. Contact administrator."),
116 displayPageHeader($color, 'None');
121 $showaddrlist = true;
122 $defselected = array();
123 $form_url = 'addressbook.php';
126 /* Handle user's actions */
127 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
129 /**************************************************
131 **************************************************/
132 if (isset($addaddr)) {
133 foreach( $addaddr as $k => $adr ) {
134 $addaddr[$k] = strip_tags( $adr );
136 $r = $abook->add($addaddr, $abook->localbackend
);
138 /* Handle error messages */
140 /* Remove backend name from error string */
141 $errstr = $abook->error
;
142 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
144 $formerror = $errstr;
145 $showaddrlist = false;
150 /************************************************
151 * Delete address(es) *
152 ************************************************/
153 if ((!empty($deladdr)) && sizeof($sel) > 0) {
157 /* The selected addresses are identidied by "backend:nickname". *
158 * Sort the list and process one backend at the time */
163 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
164 list($sbackend, $snick) = explode(':', $sel[$i]);
166 /* When we get to a new backend, process addresses in *
168 if ($prevback != $sbackend && $prevback != -1) {
170 $r = $abook->remove($subsel, $prevback);
172 $formerror = $abook->error
;
180 /* Queue for processing */
181 array_push($subsel, $snick);
182 $prevback = $sbackend;
186 $r = $abook->remove($subsel, $prevback);
187 if (!$r) { /* Handle errors */
188 $formerror = $abook->error
;
194 $showaddrlist = true;
195 $defselected = $orig_sel;
200 /***********************************************
201 * Update/modify address *
202 ***********************************************/
203 if (!empty($editaddr)) {
205 /* Stage one: Copy data into form */
206 if (isset($sel) && sizeof($sel) > 0) {
207 if(sizeof($sel) > 1) {
208 $formerror = _("You can only edit one address at the time");
209 $showaddrlist = true;
213 list($ebackend, $enick) = explode(':', $sel[0]);
214 $olddata = $abook->lookup($enick, $ebackend);
216 /* Display the "new address" form */
217 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
222 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
223 'center', $color[0] )
225 'center', '', 'width="100%" ' );
226 address_form("editaddr", _("Update address"), $olddata);
227 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
228 htmlspecialchars($olddata["nickname"]) . "\">\n" .
229 '<INPUT TYPE=hidden NAME=backend VALUE="' .
230 htmlspecialchars($olddata["backend"]) . "\">\n" .
231 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" .
236 /* Stage two: Write new data */
238 $newdata = $editaddr;
239 $r = $abook->modify($oldnick, $newdata, $backend);
241 /* Handle error messages */
244 echo html_tag( 'table',
247 "\n". '<strong><font color="' . $color[2] .
248 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
251 'center', '', 'width="100%"' );
253 /* Display the "new address" form again */
254 echo '<FORM ACTION="' . $form_url .
255 '" METHOD="POST">' . "\n" .
259 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
260 'center', $color[0] )
262 'center', '', 'width="100%"' );
263 address_form("editaddr", _("Update address"), $newdata);
264 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
265 htmlspecialchars($oldnick) . "\">\n" .
266 '<INPUT TYPE=hidden NAME=backend VALUE="' .
267 htmlspecialchars($backend) . "\">\n" .
268 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' .
274 /* Should not get here... */
275 plain_error_message(_("Unknown error"), $color);
279 } /* !empty($editaddr) - Update/modify address */
280 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
281 } /* !empty($addaddr['nickname']) - Add new address */
283 // Some times we end output before forms are printed
285 echo "</BODY></HTML>\n";
291 /* =================================================================== *
292 * The following is only executed on a GET request, or on a POST when *
293 * a user is added, or when "delete" or "modify" was successful. *
294 * =================================================================== */
296 /* Display error messages */
297 if (!empty($formerror)) {
298 echo html_tag( 'table',
301 "\n". '<br><strong><font color="' . $color[2] .
302 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
305 'center', '', 'width="100%"' );
309 /* Display the address management part */
311 /* Get and sort address list */
312 $alist = $abook->list_addr();
313 if(!is_array($alist)) {
314 plain_error_message($abook->error
, $color);
318 usort($alist,'alistcmp');
320 $headerprinted = false;
322 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
325 if (count($alist) > 0) {
326 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' . "\n";
327 while(list($undef,$row) = each($alist)) {
329 /* New table header for each backend */
330 if($prevbackend != $row['backend']) {
331 if($prevbackend < 0) {
332 echo html_tag( 'table',
335 '<INPUT TYPE=submit NAME=editaddr VALUE="' .
336 _("Edit selected") . "\">\n" .
337 '<INPUT TYPE=submit NAME=deladdr VALUE="' .
338 _("Delete selected") . "\">\n",
339 'center', '', 'colspan="5"' )
342 html_tag( 'td', ' <br>', 'center', '', 'colspan="5"' )
347 echo html_tag( 'table',
349 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] )
351 'center', '', 'width="95%"' ) ."\n"
352 . html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
353 html_tag( 'tr', "\n" .
354 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) .
355 html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) .
356 html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) .
357 html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) .
358 html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) ,
359 '', $color[9] ) . "\n";
362 $headerprinted = true;
363 } /* End of header */
365 $prevbackend = $row['backend'];
367 /* Check if this user is selected */
368 if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) {
369 $selected = 'CHECKED';
376 if ($line %
2) { $tr_bgcolor = $color[0]; }
377 if ($squirrelmail_language == 'ja_JP')
379 echo html_tag( 'tr', '', '', $tr_bgcolor) .
382 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
383 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
384 'center', '', 'valign="top" width="1%"' ) .
385 html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
386 html_tag( 'td', ' ' . $row['lastname'] . ' ' . $row['firstname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
387 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
389 echo html_tag( 'tr', '', '', $tr_bgcolor) .
392 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
393 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
394 'center', '', 'valign="top" width="1%"' ) .
395 html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
396 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
397 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
399 $email = $abook->full_address($row);
400 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
401 htmlspecialchars($row['email']) ) .
403 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
408 /* End of list. Close table. */
409 if ($headerprinted) {
412 '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
414 '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
416 'center', '', 'colspan="5"' )
419 echo '</table></FORM>';
421 } /* end of addresslist */
424 /* Display the "new address" form */
425 echo '<a name="AddAddress"></a>' . "\n" .
426 '<FORM ACTION="' . $form_url . '" NAME=f_add METHOD="POST">' . "\n" .
429 html_tag( 'td', "\n". '<strong>' . sprintf(_("Add to %s"), $abook->localbackendname
) . '</strong>' . "\n",
433 , 'center', '', 'width="100%"' ) ."\n";
434 address_form('addaddr', _("Add address"), $defdata);
437 /* Add hook for anything that wants on the bottom */
438 do_hook('addressbook_bottom');