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( strip_tags( $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()) {
44 echo html_tag( 'table',
45 adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
46 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
47 adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
48 adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
49 adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
50 adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
53 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
55 'center', $color[4], 'colspan="2"')
57 , 'center', '', 'border="0" cellpadding="1" 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');
76 $defselected = array();
77 $form_url = 'addressbook.php';
80 /* Handle user's actions */
81 if($REQUEST_METHOD == 'POST') {
83 /**************************************************
85 **************************************************/
86 if (!empty($addaddr['nickname'])) {
87 foreach( $addaddr as $k => $adr ) {
88 $addaddr[$k] = strip_tags( $adr );
90 $r = $abook->add($addaddr, $abook->localbackend
);
92 /* Handle error messages */
94 /* Remove backend name from error string */
95 $errstr = $abook->error
;
96 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
99 $showaddrlist = false;
104 /************************************************
105 * Delete address(es) *
106 ************************************************/
107 if ((!empty($deladdr)) && sizeof($sel) > 0) {
111 /* The selected addresses are identidied by "backend:nickname". *
112 * Sort the list and process one backend at the time */
117 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
118 list($sbackend, $snick) = explode(':', $sel[$i]);
120 /* When we get to a new backend, process addresses in *
122 if ($prevback != $sbackend && $prevback != -1) {
124 $r = $abook->remove($subsel, $prevback);
126 $formerror = $abook->error
;
134 /* Queue for processing */
135 array_push($subsel, $snick);
136 $prevback = $sbackend;
140 $r = $abook->remove($subsel, $prevback);
141 if (!$r) { /* Handle errors */
142 $formerror = $abook->error
;
148 $showaddrlist = true;
149 $defselected = $orig_sel;
154 /***********************************************
155 * Update/modify address *
156 ***********************************************/
157 if (!empty($editaddr)) {
159 /* Stage one: Copy data into form */
160 if (isset($sel) && sizeof($sel) > 0) {
161 if(sizeof($sel) > 1) {
162 $formerror = _("You can only edit one address at the time");
163 $showaddrlist = true;
167 list($ebackend, $enick) = explode(':', $sel[0]);
168 $olddata = $abook->lookup($enick, $ebackend);
170 /* Display the "new address" form */
171 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
176 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
177 'center', $color[0] )
179 'center', '', 'width="100%" ' );
180 address_form("editaddr", _("Update address"), $olddata);
181 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
182 htmlspecialchars($olddata["nickname"]) . "\">\n" .
183 '<INPUT TYPE=hidden NAME=backend VALUE="' .
184 htmlspecialchars($olddata["backend"]) . "\">\n" .
185 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" .
190 /* Stage two: Write new data */
192 $newdata = $editaddr;
193 $r = $abook->modify($oldnick, $newdata, $backend);
195 /* Handle error messages */
198 echo html_tag( 'table',
201 "\n". '<br><strong><font color="' . $color[2] .
202 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
205 'center', '', 'width="100%"' );
207 /* Display the "new address" form again */
208 echo '<FORM ACTION="' . $form_url .
209 '" METHOD="POST">' . "\n" .
213 "\n". '<br><strong>' . _("Update address") . '</strong>' ."\n",
214 'center', $color[0] )
216 'center', '', 'width="100%"' ) .
217 address_form("editaddr", _("Update address"), $newdata);
218 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
219 htmlspecialchars($oldnick) . "\">\n" .
220 '<INPUT TYPE=hidden NAME=backend VALUE="' .
221 htmlspecialchars($backend) . "\">\n" .
222 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' .
228 /* Should not get here... */
229 plain_error_message(_("Unknown error"), $color);
233 } /* !empty($editaddr) - Update/modify address */
234 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
235 } /* !empty($addaddr['nickname']) - Add new address */
237 // Some times we end output before forms are printed
239 echo "</BODY></HTML>\n";
245 /* =================================================================== *
246 * The following is only executed on a GET request, or on a POST when *
247 * a user is added, or when "delete" or "modify" was successful. *
248 * =================================================================== */
250 /* Display error messages */
251 if (!empty($formerror)) {
252 echo html_tag( 'table',
255 "\n". '<br><strong><font color="' . $color[2] .
256 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
259 'center', '', 'width="100%"' );
263 /* Display the address management part */
265 /* Get and sort address list */
266 $alist = $abook->list_addr();
267 if(!is_array($alist)) {
268 plain_error_message($abook->error
, $color);
272 usort($alist,'alistcmp');
274 $headerprinted = false;
276 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
279 if (count($alist) > 0) {
280 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' . "\n";
281 while(list($undef,$row) = each($alist)) {
283 /* New table header for each backend */
284 if($prevbackend != $row['backend']) {
285 if($prevbackend < 0) {
286 echo html_tag( 'table',
289 '<INPUT TYPE=submit NAME=editaddr VALUE="' .
290 _("Edit selected") . "\">\n" .
291 '<INPUT TYPE=submit NAME=deladdr VALUE="' .
292 _("Delete selected") . "\">\n",
293 'center', '', 'colspan="5"' )
296 html_tag( 'td', ' <br>', 'center', '', 'colspan="5"' )
301 echo html_tag( 'table',
303 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] )
305 'center', '', 'width="95%"' ) ."\n"
306 . html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
307 html_tag( 'tr', "\n" .
308 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) .
309 html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) .
310 html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) .
311 html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) .
312 html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) ,
313 '', $color[9] ) . "\n";
316 $headerprinted = true;
317 } /* End of header */
319 $prevbackend = $row['backend'];
321 /* Check if this user is selected */
322 if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) {
323 $selected = 'CHECKED';
330 if ($line %
2) { $tr_bgcolor = $color[0]; }
331 echo html_tag( 'tr', '') .
334 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
335 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
336 'center', '', 'valign="top" width="1%"' ) .
337 html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
338 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
339 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
340 $email = $abook->full_address($row);
341 if ($compose_new_win == '1') {
342 echo '<a href="javascript:void(0)" onclick=comp_in_new(false,"compose.php?send_to='.rawurlencode($email).'")>';
345 echo '<A HREF="compose.php?send_to=' . rawurlencode($email).'">';
347 echo htmlspecialchars($row['email']) . '</A> </td>'."\n".
348 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
353 /* End of list. Close table. */
354 if ($headerprinted) {
357 '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
359 '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
361 'center', '', 'colspan="5"' )
364 echo '</table></FORM>';
366 } /* end of addresslist */
369 /* Display the "new address" form */
370 echo '<a name="AddAddress"></a>' . "\n" .
371 '<FORM ACTION="' . $form_url . '" NAME=f_add METHOD="POST">' . "\n" .
374 html_tag( 'td', "\n". '<strong>' . sprintf(_("Add to %s"), $abook->localbackendname
) . '</strong>' . "\n",
378 , 'center', '', 'width="100%"' ) ."\n";
379 address_form('addaddr', _("Add address"), $defdata);
382 /* Add hook for anything that wants on the bottom */
383 do_hook('addressbook_bottom');