ab148752457edb1dc1877e9c2c16444be75b8e83
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Manage personal address book.
14 /* Path for SquirrelMail required files. */
15 define('SM_PATH','../');
17 /* SquirrelMail required files. */
18 require_once(SM_PATH
. 'include/validate.php');
19 require_once(SM_PATH
. 'functions/array.php');
20 require_once(SM_PATH
. 'functions/display_messages.php');
21 require_once(SM_PATH
. 'functions/addressbook.php');
22 require_once(SM_PATH
. 'functions/strings.php');
23 require_once(SM_PATH
. 'functions/html.php');
25 /* Make an input field */
26 function adressbook_inp_field($label, $field, $name, $size, $values, $add) {
28 $td_str = '<INPUT NAME="' . $name . '[' . $field . ']" SIZE="' . $size . '" VALUE="';
29 if (isset($values[$field])) {
30 $td_str .= htmlspecialchars($values[$field]);
32 $td_str .= '">' . $add . '';
33 return html_tag( 'tr' ,
34 html_tag( 'td', $label . ':', 'right', $color[4]) .
35 html_tag( 'td', $td_str, 'left', $color[4])
40 /* Output form to add and modify address data */
41 function address_form($name, $submittext, $values = array()) {
43 echo html_tag( 'table',
44 adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
45 '<SMALL>' . _("Must be unique") . '</SMALL>') .
46 adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
47 adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
48 adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
49 adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
52 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
54 'center', $color[4], 'colspan="2"')
56 , 'center', '', 'border="0" cellpadding="1" cols="2" width="90%"') ."\n";
60 /* Open addressbook, with error messages on but without LDAP (the *
61 * second "true"). Don't need LDAP here anyway */
62 $abook = addressbook_init(true, true);
63 if($abook->localbackend
== 0) {
65 _("No personal address book is defined. Contact administrator."),
70 displayPageHeader($color, 'None');
77 $defselected = array();
80 /* Handle user's actions */
81 if($REQUEST_METHOD == 'POST') {
83 /**************************************************
85 **************************************************/
86 if (!empty($addaddr['nickname'])) {
88 $r = $abook->add($addaddr, $abook->localbackend
);
90 /* Handle error messages */
92 /* Remove backend name from error string */
93 $errstr = $abook->error
;
94 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
97 $showaddrlist = false;
103 /************************************************
104 * Delete address(es) *
105 ************************************************/
106 if ((!empty($deladdr)) && sizeof($sel) > 0) {
110 /* The selected addresses are identidied by "backend:nickname". *
111 * Sort the list and process one backend at the time */
116 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
117 list($sbackend, $snick) = explode(':', $sel[$i]);
119 /* When we get to a new backend, process addresses in *
121 if ($prevback != $sbackend && $prevback != -1) {
123 $r = $abook->remove($subsel, $prevback);
125 $formerror = $abook->error
;
133 /* Queue for processing */
134 array_push($subsel, $snick);
135 $prevback = $sbackend;
139 $r = $abook->remove($subsel, $prevback);
140 if (!$r) { /* Handle errors */
141 $formerror = $abook->error
;
147 $showaddrlist = true;
148 $defselected = $orig_sel;
153 /***********************************************
154 * Update/modify address *
155 ***********************************************/
156 if (!empty($editaddr)) {
158 /* Stage one: Copy data into form */
159 if (isset($sel) && sizeof($sel) > 0) {
160 if(sizeof($sel) > 1) {
161 $formerror = _("You can only edit one address at the time");
162 $showaddrlist = true;
166 list($ebackend, $enick) = explode(':', $sel[0]);
167 $olddata = $abook->lookup($enick, $ebackend);
169 /* Display the "new address" form */
170 echo '<FORM ACTION="' . $PHP_SELF . '" METHOD="POST">' .
175 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
176 'center', $color[0] )
178 'center', '', 'width="100%" cols="1"' ) .
179 address_form("editaddr", _("Update address"), $olddata);
180 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
181 htmlspecialchars($olddata["nickname"]) . "\">\n" .
182 '<INPUT TYPE=hidden NAME=backend VALUE="' .
183 htmlspecialchars($olddata["backend"]) . "\">\n" .
184 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" .
189 /* Stage two: Write new data */
191 $newdata = $editaddr;
192 $r = $abook->modify($oldnick, $newdata, $backend);
194 /* Handle error messages */
197 echo html_tag( 'table',
200 "\n". '<br><strong><font color="' . $color[2] .
201 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
204 'center', '', 'width="100%" cols="1"' );
206 /* Display the "new address" form again */
207 echo '<FORM ACTION="' . $PHP_SELF .
208 '" METHOD="POST">' . "\n" .
212 "\n". '<br><strong>' . _("Update address") . '</strong>' ."\n",
213 'center', $color[0] )
215 'center', '', 'width="100%" cols="1"' ) .
216 address_form("editaddr", _("Update address"), $newdata);
217 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
218 htmlspecialchars($oldnick) . "\">\n" .
219 '<INPUT TYPE=hidden NAME=backend VALUE="' .
220 htmlspecialchars($backend) . "\">\n" .
221 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' .
227 /* Should not get here... */
228 plain_error_message(_("Unknown error"), $color);
232 } /* !empty($editaddr) - Update/modify address */
233 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
234 } /* !empty($addaddr['nickname']) - Add new address */
236 // Some times we end output before forms are printed
238 echo "</BODY></HTML>\n";
244 /* =================================================================== *
245 * The following is only executed on a GET request, or on a POST when *
246 * a user is added, or when "delete" or "modify" was successful. *
247 * =================================================================== */
249 /* Display error messages */
250 if (!empty($formerror)) {
251 echo html_tag( 'table',
254 "\n". '<br><strong><font color="' . $color[2] .
255 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
258 'center', '', 'width="100%" cols="1"' );
262 /* Display the address management part */
264 /* Get and sort address list */
265 $alist = $abook->list_addr();
266 if(!is_array($alist)) {
267 plain_error_message($abook->error
, $color);
271 usort($alist,'alistcmp');
273 $headerprinted = false;
275 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
278 if (count($alist) > 0) {
279 echo '<FORM ACTION="' . $PHP_SELF . '" METHOD="POST">' . "\n";
280 while(list($undef,$row) = each($alist)) {
282 /* New table header for each backend */
283 if($prevbackend != $row['backend']) {
284 if($prevbackend < 0) {
285 echo html_tag( 'table',
288 '<INPUT TYPE=submit NAME=editaddr VALUE="' .
289 _("Edit selected") . "\">\n" .
290 '<INPUT TYPE=submit NAME=deladdr VALUE="' .
291 _("Delete selected") . "\">\n",
292 'center', '', 'colspan="5"' )
295 html_tag( 'td', ' <br>', 'center', '', 'colspan="5"' )
300 echo html_tag( 'table',
302 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] )
304 'center', '', 'width="95%" cols="1"' ) ."\n"
305 . html_tag( 'table', '', 'center', '', 'cols="5" border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
306 html_tag( 'tr', "\n" .
307 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) .
308 html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) .
309 html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) .
310 html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) .
311 html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) ,
312 '', $color[9] ) . "\n";
315 $headerprinted = true;
316 } /* End of header */
318 $prevbackend = $row['backend'];
320 /* Check if this user is selected */
321 if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) {
322 $selected = 'CHECKED';
329 if ($line %
2) { $tr_bgcolor = $color[0]; }
330 echo html_tag( 'tr', '') .
333 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
334 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
335 'center', '', 'valign="top" width="1%"' ) .
336 html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
337 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
338 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
339 $email = $abook->full_address($row);
340 if ($compose_new_win == '1') {
341 echo '<a href="javascript:void(0)" onclick=comp_in_new(false,"compose.php?send_to='.rawurlencode($email).'")>';
344 echo '<A HREF="compose.php?send_to=' . rawurlencode($email).'">';
346 echo htmlspecialchars($row['email']) . '</A> </td>'."\n".
347 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
352 /* End of list. Close table. */
353 if ($headerprinted) {
356 '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
358 '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
360 'center', '', 'colspan="5"' )
363 echo '</table></FORM>';
365 } /* end of addresslist */
368 /* Display the "new address" form */
369 echo '<a name="AddAddress"></a>' . "\n" .
370 '<FORM ACTION="' . $PHP_SELF . '" NAME=f_add METHOD="POST">' . "\n" .
373 html_tag( 'td', "\n". '<strong>' . sprintf(_("Add to %s"), $abook->localbackendname
) . '</strong>' . "\n",
377 , 'center', '', 'width="100%" cols="1"' ) ."\n";
378 address_form('addaddr', _("Add address"), $defdata);
381 /* Add hook for anything that wants on the bottom */
382 do_hook('addressbook_bottom');