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.
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/global.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 /* lets get the global vars we may need */
26 sqgetGlobalVar('key', $key, SQ_COOKIE
);
28 sqgetGlobalVar('username', $username, SQ_SESSION
);
29 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
30 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
31 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
33 /* From the address form */
34 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
35 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
36 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
37 sqgetGlobalVar('sel', $sel, SQ_POST
);
38 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
39 sqgetGlobalVar('backend', $backend, SQ_POST
);
40 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
42 /* Make an input field */
43 function adressbook_inp_field($label, $field, $name, $size, $values, $add) {
45 $td_str = '<INPUT NAME="' . $name . '[' . $field . ']" SIZE="' . $size . '" VALUE="';
46 if (isset($values[$field])) {
47 $td_str .= htmlspecialchars( strip_tags( $values[$field] ) );
49 $td_str .= '">' . $add . '';
50 return html_tag( 'tr' ,
51 html_tag( 'td', $label . ':', 'right', $color[4]) .
52 html_tag( 'td', $td_str, 'left', $color[4])
57 /* Output form to add and modify address data */
58 function address_form($name, $submittext, $values = array()) {
61 echo html_tag( 'table',
62 adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
63 ' <SMALL>' . _("Must be unique") . '</SMALL>') .
64 adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
65 adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
66 adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
67 adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
70 '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
72 'center', $color[4], 'colspan="2"')
74 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
77 /* Open addressbook, with error messages on but without LDAP (the *
78 * second "true"). Don't need LDAP here anyway */
79 $abook = addressbook_init(true, true);
80 if($abook->localbackend
== 0) {
82 _("No personal address book is defined. Contact administrator."),
87 displayPageHeader($color, 'None');
93 $defselected = array();
94 $form_url = 'addressbook.php';
97 /* Handle user's actions */
98 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
100 /**************************************************
102 **************************************************/
103 if (!empty($addaddr['nickname'])) {
104 foreach( $addaddr as $k => $adr ) {
105 $addaddr[$k] = strip_tags( $adr );
107 $r = $abook->add($addaddr, $abook->localbackend
);
109 /* Handle error messages */
111 /* Remove backend name from error string */
112 $errstr = $abook->error
;
113 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
115 $formerror = $errstr;
116 $showaddrlist = false;
121 /************************************************
122 * Delete address(es) *
123 ************************************************/
124 if ((!empty($deladdr)) && sizeof($sel) > 0) {
128 /* The selected addresses are identidied by "backend:nickname". *
129 * Sort the list and process one backend at the time */
134 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
135 list($sbackend, $snick) = explode(':', $sel[$i]);
137 /* When we get to a new backend, process addresses in *
139 if ($prevback != $sbackend && $prevback != -1) {
141 $r = $abook->remove($subsel, $prevback);
143 $formerror = $abook->error
;
151 /* Queue for processing */
152 array_push($subsel, $snick);
153 $prevback = $sbackend;
157 $r = $abook->remove($subsel, $prevback);
158 if (!$r) { /* Handle errors */
159 $formerror = $abook->error
;
165 $showaddrlist = true;
166 $defselected = $orig_sel;
171 /***********************************************
172 * Update/modify address *
173 ***********************************************/
174 if (!empty($editaddr)) {
176 /* Stage one: Copy data into form */
177 if (isset($sel) && sizeof($sel) > 0) {
178 if(sizeof($sel) > 1) {
179 $formerror = _("You can only edit one address at the time");
180 $showaddrlist = true;
184 list($ebackend, $enick) = explode(':', $sel[0]);
185 $olddata = $abook->lookup($enick, $ebackend);
187 /* Display the "new address" form */
188 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
193 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
194 'center', $color[0] )
196 'center', '', 'width="100%" ' );
197 address_form("editaddr", _("Update address"), $olddata);
198 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
199 htmlspecialchars($olddata["nickname"]) . "\">\n" .
200 '<INPUT TYPE=hidden NAME=backend VALUE="' .
201 htmlspecialchars($olddata["backend"]) . "\">\n" .
202 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" .
207 /* Stage two: Write new data */
209 $newdata = $editaddr;
210 $r = $abook->modify($oldnick, $newdata, $backend);
212 /* Handle error messages */
215 echo html_tag( 'table',
218 "\n". '<br><strong><font color="' . $color[2] .
219 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
222 'center', '', 'width="100%"' );
224 /* Display the "new address" form again */
225 echo '<FORM ACTION="' . $form_url .
226 '" METHOD="POST">' . "\n" .
230 "\n". '<br><strong>' . _("Update address") . '</strong>' ."\n",
231 'center', $color[0] )
233 'center', '', 'width="100%"' ) .
234 address_form("editaddr", _("Update address"), $newdata);
235 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
236 htmlspecialchars($oldnick) . "\">\n" .
237 '<INPUT TYPE=hidden NAME=backend VALUE="' .
238 htmlspecialchars($backend) . "\">\n" .
239 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' .
245 /* Should not get here... */
246 plain_error_message(_("Unknown error"), $color);
250 } /* !empty($editaddr) - Update/modify address */
251 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
252 } /* !empty($addaddr['nickname']) - Add new address */
254 // Some times we end output before forms are printed
256 echo "</BODY></HTML>\n";
262 /* =================================================================== *
263 * The following is only executed on a GET request, or on a POST when *
264 * a user is added, or when "delete" or "modify" was successful. *
265 * =================================================================== */
267 /* Display error messages */
268 if (!empty($formerror)) {
269 echo html_tag( 'table',
272 "\n". '<br><strong><font color="' . $color[2] .
273 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
276 'center', '', 'width="100%"' );
280 /* Display the address management part */
282 /* Get and sort address list */
283 $alist = $abook->list_addr();
284 if(!is_array($alist)) {
285 plain_error_message($abook->error
, $color);
289 usort($alist,'alistcmp');
291 $headerprinted = false;
293 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
296 if (count($alist) > 0) {
297 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' . "\n";
298 while(list($undef,$row) = each($alist)) {
300 /* New table header for each backend */
301 if($prevbackend != $row['backend']) {
302 if($prevbackend < 0) {
303 echo html_tag( 'table',
306 '<INPUT TYPE=submit NAME=editaddr VALUE="' .
307 _("Edit selected") . "\">\n" .
308 '<INPUT TYPE=submit NAME=deladdr VALUE="' .
309 _("Delete selected") . "\">\n",
310 'center', '', 'colspan="5"' )
313 html_tag( 'td', ' <br>', 'center', '', 'colspan="5"' )
318 echo html_tag( 'table',
320 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] )
322 'center', '', 'width="95%"' ) ."\n"
323 . html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
324 html_tag( 'tr', "\n" .
325 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) .
326 html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) .
327 html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) .
328 html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) .
329 html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) ,
330 '', $color[9] ) . "\n";
333 $headerprinted = true;
334 } /* End of header */
336 $prevbackend = $row['backend'];
338 /* Check if this user is selected */
339 if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) {
340 $selected = 'CHECKED';
347 if ($line %
2) { $tr_bgcolor = $color[0]; }
348 echo html_tag( 'tr', '') .
351 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
352 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
353 'center', '', 'valign="top" width="1%"' ) .
354 html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
355 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
356 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
357 $email = $abook->full_address($row);
358 if ($compose_new_win == '1') {
359 echo '<a href="javascript:void(0)" onclick=comp_in_new(false,"compose.php?send_to='.rawurlencode($email).'")>';
362 echo '<A HREF="compose.php?send_to=' . rawurlencode($email).'">';
364 echo htmlspecialchars($row['email']) . '</A> </td>'."\n".
365 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
370 /* End of list. Close table. */
371 if ($headerprinted) {
374 '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
376 '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
378 'center', '', 'colspan="5"' )
381 echo '</table></FORM>';
383 } /* end of addresslist */
386 /* Display the "new address" form */
387 echo '<a name="AddAddress"></a>' . "\n" .
388 '<FORM ACTION="' . $form_url . '" NAME=f_add METHOD="POST">' . "\n" .
391 html_tag( 'td', "\n". '<strong>' . sprintf(_("Add to %s"), $abook->localbackendname
) . '</strong>' . "\n",
395 , 'center', '', 'width="100%"' ) ."\n";
396 address_form('addaddr', _("Add address"), $defdata);
399 /* Add hook for anything that wants on the bottom */
400 do_hook('addressbook_bottom');