6745b429e17ed9f2a88230f0a5d798c0bb5523d4
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 /* lets get the global vars we may need */
26 $key = $_COOKIE['key'];
28 $username = $_SESSION['username'];
29 $onetimepad = $_SESSION['onetimepad'];
30 $base_uri = $_SESSION['base_uri'];
31 $delimiter = $_SESSION['delimiter'];
33 /* From the address form */
34 if ( isset($_POST['addaddr']) ) {
35 $addaddr = &$_POST['addaddr'];
37 if ( isset($_POST['editaddr']) ) {
38 $editaddr = &$_POST['editaddr'];
40 if ( isset($_POST['deladdr']) ) {
41 $deladdr = &$_POST['deladdr'];
43 $sel = &$_POST['sel'];
45 if (isset($_POST['oldnick'])) {
46 $oldnick = $_POST['oldnick'];
48 if (isset($_POST['backend'])) {
49 $backend = $_POST['backend'];
51 if (isset($_POST['doedit'])) {
52 $doedit = $_POST['doedit'];
55 /* Make an input field */
56 function adressbook_inp_field($label, $field, $name, $size, $values, $add) {
58 $td_str = '<INPUT NAME="' . $name . '[' . $field . ']" SIZE="' . $size . '" VALUE="';
59 if (isset($values[$field])) {
60 $td_str .= htmlspecialchars( strip_tags( $values[$field] ) );
62 $td_str .= '">' . $add . '';
63 return html_tag( 'tr' ,
64 html_tag( 'td', $label . ':', 'right', $color[4]) .
65 html_tag( 'td', $td_str, 'left', $color[4])
70 /* Output form to add and modify address data */
71 function address_form($name, $submittext, $values = array()) {
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(_("First name"), 'firstname', $name, 45, $values, '') .
79 adressbook_inp_field(_("Last name"), 'lastname', $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";
90 /* Open addressbook, with error messages on but without LDAP (the *
91 * second "true"). Don't need LDAP here anyway */
92 $abook = addressbook_init(true, true);
93 if($abook->localbackend
== 0) {
95 _("No personal address book is defined. Contact administrator."),
100 displayPageHeader($color, 'None');
105 $showaddrlist = true;
106 $defselected = array();
107 $form_url = 'addressbook.php';
110 /* Handle user's actions */
111 if($_SERVER['REQUEST_METHOD'] == 'POST') {
113 /**************************************************
115 **************************************************/
116 if (!empty($addaddr['nickname'])) {
117 foreach( $addaddr as $k => $adr ) {
118 $addaddr[$k] = strip_tags( $adr );
120 $r = $abook->add($addaddr, $abook->localbackend
);
122 /* Handle error messages */
124 /* Remove backend name from error string */
125 $errstr = $abook->error
;
126 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
128 $formerror = $errstr;
129 $showaddrlist = false;
134 /************************************************
135 * Delete address(es) *
136 ************************************************/
137 if ((!empty($deladdr)) && sizeof($sel) > 0) {
141 /* The selected addresses are identidied by "backend:nickname". *
142 * Sort the list and process one backend at the time */
147 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
148 list($sbackend, $snick) = explode(':', $sel[$i]);
150 /* When we get to a new backend, process addresses in *
152 if ($prevback != $sbackend && $prevback != -1) {
154 $r = $abook->remove($subsel, $prevback);
156 $formerror = $abook->error
;
164 /* Queue for processing */
165 array_push($subsel, $snick);
166 $prevback = $sbackend;
170 $r = $abook->remove($subsel, $prevback);
171 if (!$r) { /* Handle errors */
172 $formerror = $abook->error
;
178 $showaddrlist = true;
179 $defselected = $orig_sel;
184 /***********************************************
185 * Update/modify address *
186 ***********************************************/
187 if (!empty($editaddr)) {
189 /* Stage one: Copy data into form */
190 if (isset($sel) && sizeof($sel) > 0) {
191 if(sizeof($sel) > 1) {
192 $formerror = _("You can only edit one address at the time");
193 $showaddrlist = true;
197 list($ebackend, $enick) = explode(':', $sel[0]);
198 $olddata = $abook->lookup($enick, $ebackend);
200 /* Display the "new address" form */
201 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' .
206 "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
207 'center', $color[0] )
209 'center', '', 'width="100%" ' );
210 address_form("editaddr", _("Update address"), $olddata);
211 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
212 htmlspecialchars($olddata["nickname"]) . "\">\n" .
213 '<INPUT TYPE=hidden NAME=backend VALUE="' .
214 htmlspecialchars($olddata["backend"]) . "\">\n" .
215 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" .
220 /* Stage two: Write new data */
222 $newdata = $editaddr;
223 $r = $abook->modify($oldnick, $newdata, $backend);
225 /* Handle error messages */
228 echo html_tag( 'table',
231 "\n". '<br><strong><font color="' . $color[2] .
232 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
235 'center', '', 'width="100%"' );
237 /* Display the "new address" form again */
238 echo '<FORM ACTION="' . $form_url .
239 '" METHOD="POST">' . "\n" .
243 "\n". '<br><strong>' . _("Update address") . '</strong>' ."\n",
244 'center', $color[0] )
246 'center', '', 'width="100%"' ) .
247 address_form("editaddr", _("Update address"), $newdata);
248 echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
249 htmlspecialchars($oldnick) . "\">\n" .
250 '<INPUT TYPE=hidden NAME=backend VALUE="' .
251 htmlspecialchars($backend) . "\">\n" .
252 '<INPUT TYPE=hidden NAME=doedit VALUE=1>' .
258 /* Should not get here... */
259 plain_error_message(_("Unknown error"), $color);
263 } /* !empty($editaddr) - Update/modify address */
264 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
265 } /* !empty($addaddr['nickname']) - Add new address */
267 // Some times we end output before forms are printed
269 echo "</BODY></HTML>\n";
275 /* =================================================================== *
276 * The following is only executed on a GET request, or on a POST when *
277 * a user is added, or when "delete" or "modify" was successful. *
278 * =================================================================== */
280 /* Display error messages */
281 if (!empty($formerror)) {
282 echo html_tag( 'table',
285 "\n". '<br><strong><font color="' . $color[2] .
286 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
289 'center', '', 'width="100%"' );
293 /* Display the address management part */
295 /* Get and sort address list */
296 $alist = $abook->list_addr();
297 if(!is_array($alist)) {
298 plain_error_message($abook->error
, $color);
302 usort($alist,'alistcmp');
304 $headerprinted = false;
306 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
309 if (count($alist) > 0) {
310 echo '<FORM ACTION="' . $form_url . '" METHOD="POST">' . "\n";
311 while(list($undef,$row) = each($alist)) {
313 /* New table header for each backend */
314 if($prevbackend != $row['backend']) {
315 if($prevbackend < 0) {
316 echo html_tag( 'table',
319 '<INPUT TYPE=submit NAME=editaddr VALUE="' .
320 _("Edit selected") . "\">\n" .
321 '<INPUT TYPE=submit NAME=deladdr VALUE="' .
322 _("Delete selected") . "\">\n",
323 'center', '', 'colspan="5"' )
326 html_tag( 'td', ' <br>', 'center', '', 'colspan="5"' )
331 echo html_tag( 'table',
333 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] )
335 'center', '', 'width="95%"' ) ."\n"
336 . html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
337 html_tag( 'tr', "\n" .
338 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) .
339 html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) .
340 html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) .
341 html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) .
342 html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) ,
343 '', $color[9] ) . "\n";
346 $headerprinted = true;
347 } /* End of header */
349 $prevbackend = $row['backend'];
351 /* Check if this user is selected */
352 if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) {
353 $selected = 'CHECKED';
360 if ($line %
2) { $tr_bgcolor = $color[0]; }
361 echo html_tag( 'tr', '') .
364 '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
365 $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
366 'center', '', 'valign="top" width="1%"' ) .
367 html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
368 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
369 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
370 $email = $abook->full_address($row);
371 if ($compose_new_win == '1') {
372 echo '<a href="javascript:void(0)" onclick=comp_in_new(false,"compose.php?send_to='.rawurlencode($email).'")>';
375 echo '<A HREF="compose.php?send_to=' . rawurlencode($email).'">';
377 echo htmlspecialchars($row['email']) . '</A> </td>'."\n".
378 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
383 /* End of list. Close table. */
384 if ($headerprinted) {
387 '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
389 '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
391 'center', '', 'colspan="5"' )
394 echo '</table></FORM>';
396 } /* end of addresslist */
399 /* Display the "new address" form */
400 echo '<a name="AddAddress"></a>' . "\n" .
401 '<FORM ACTION="' . $form_url . '" NAME=f_add METHOD="POST">' . "\n" .
404 html_tag( 'td', "\n". '<strong>' . sprintf(_("Add to %s"), $abook->localbackendname
) . '</strong>' . "\n",
408 , 'center', '', 'width="100%"' ) ."\n";
409 address_form('addaddr', _("Add address"), $defdata);
412 /* Add hook for anything that wants on the bottom */
413 do_hook('addressbook_bottom');