6 * Copyright (c) 1999-2004 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
16 * Path for SquirrelMail required files.
19 define('SM_PATH','../');
21 /** SquirrelMail required files. */
22 require_once(SM_PATH
. 'include/validate.php');
23 require_once(SM_PATH
. 'functions/display_messages.php');
24 require_once(SM_PATH
. 'functions/addressbook.php');
25 require_once(SM_PATH
. 'functions/forms.php');
27 /** lets get the global vars we may need */
28 sqgetGlobalVar('key', $key, SQ_COOKIE
);
30 sqgetGlobalVar('username', $username, SQ_SESSION
);
31 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
32 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
33 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
35 /* From the address form */
36 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
37 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
38 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
39 sqgetGlobalVar('sel', $sel, SQ_POST
);
40 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
41 sqgetGlobalVar('backend', $backend, SQ_POST
);
42 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
44 /* Get sorting order */
45 $abook_sort_order = get_abook_sort();
47 /* Open addressbook, with error messages on but without LDAP (the *
48 * second "true"). Don't need LDAP here anyway */
49 $abook = addressbook_init(true, true);
50 if($abook->localbackend
== 0) {
52 _("No personal address book is defined. Contact administrator."),
57 displayPageHeader($color, 'None');
63 $defselected = array();
64 $form_url = 'addressbook.php';
67 /* Handle user's actions */
68 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
70 /**************************************************
72 **************************************************/
73 if (isset($addaddr)) {
74 if (isset($backend)) {
75 $r = $abook->add($addaddr, $backend);
77 $r = $abook->add($addaddr, $abook->localbackend
);
80 /* Handle error messages */
82 /* Remove backend name from error string */
83 $errstr = $abook->error
;
84 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
87 $showaddrlist = false;
92 /************************************************
93 * Delete address(es) *
94 ************************************************/
95 if ((!empty($deladdr)) && sizeof($sel) > 0) {
99 /* The selected addresses are identidied by "backend:nickname". *
100 * Sort the list and process one backend at the time */
105 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
106 list($sbackend, $snick) = explode(':', $sel[$i]);
108 /* When we get to a new backend, process addresses in *
110 if ($prevback != $sbackend && $prevback != -1) {
112 $r = $abook->remove($subsel, $prevback);
114 $formerror = $abook->error
;
122 /* Queue for processing */
123 array_push($subsel, $snick);
124 $prevback = $sbackend;
128 $r = $abook->remove($subsel, $prevback);
129 if (!$r) { /* Handle errors */
130 $formerror = $abook->error
;
136 $showaddrlist = true;
137 $defselected = $orig_sel;
142 /***********************************************
143 * Update/modify address *
144 ***********************************************/
145 if (!empty($editaddr)) {
147 /* Stage one: Copy data into form */
148 if (isset($sel) && sizeof($sel) > 0) {
149 if(sizeof($sel) > 1) {
150 $formerror = _("You can only edit one address at the time");
151 $showaddrlist = true;
155 list($ebackend, $enick) = explode(':', $sel[0]);
156 $olddata = $abook->lookup($enick, $ebackend);
158 /* Display the "new address" form */
159 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
160 echo addHidden('oldnick', $olddata['nickname']).
161 addHidden('backend', $olddata['backend']).
162 addHidden('doedit', '1').
167 /* Stage two: Write new data */
169 $newdata = $editaddr;
170 $r = $abook->modify($oldnick, $newdata, $backend);
172 /* Handle error messages */
175 echo html_tag( 'table',
178 "\n". '<strong><font color="' . $color[2] .
179 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
182 'center', '', 'width="100%"' );
184 /* Display the "new address" form again */
185 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
186 echo addHidden('oldnick', $oldnick).
187 addHidden('backend', $backend).
188 addHidden('doedit', '1').
194 /* Should not get here... */
195 plain_error_message(_("Unknown error"), $color);
199 } /* !empty($editaddr) - Update/modify address */
200 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
201 } /* !empty($addaddr['nickname']) - Add new address */
203 // Some times we end output before forms are printed
205 echo "</body></html>\n";
211 /* =================================================================== *
212 * The following is only executed on a GET request, or on a POST when *
213 * a user is added, or when "delete" or "modify" was successful. *
214 * =================================================================== */
216 /* Display error messages */
217 if (!empty($formerror)) {
218 echo html_tag( 'table',
221 "\n". '<br /><strong><font color="' . $color[2] .
222 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
225 'center', '', 'width="100%"' );
229 /* Display the address management part */
231 /* Get and sort address list */
232 $alist = $abook->list_addr();
233 if(!is_array($alist)) {
234 plain_error_message($abook->error
, $color);
238 usort($alist,'alistcmp');
240 $headerprinted = false;
242 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
245 if (count($alist) > 0) {
246 echo addForm($form_url, 'post');
247 while(list($undef,$row) = each($alist)) {
249 /* New table header for each backend */
250 if($prevbackend != $row['backend']) {
251 if($prevbackend < 0) {
252 echo html_tag( 'table',
255 addSubmit(_("Edit selected"), 'editaddr').
256 addSubmit(_("Delete selected"), 'deladdr'),
257 'center', '', 'colspan="5"' )
260 html_tag( 'td', ' <br />', 'center', '', 'colspan="5"' )
263 echo "\n<!-- start of address book table -->\n" .
264 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
265 html_tag( 'tr', "\n" .
266 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . "\n" .
267 html_tag( 'th', _("Nickname") .
268 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
269 'left', '', 'width="1%"' ) . "\n" .
270 html_tag( 'th', _("Name") .
271 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
272 'left', '', 'width="1%"' ) . "\n" .
273 html_tag( 'th', _("E-mail") .
274 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
275 'left', '', 'width="1%"' ) . "\n" .
276 html_tag( 'th', _("Info") .
277 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
278 'left', '', 'width="1%"' ) . "\n",
279 '', $color[9] ) . "\n";
282 // Separate different backends with <hr />
283 if($prevbackend > 0) {
285 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
289 // Print backend name
291 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
295 $headerprinted = true;
296 } /* End of header */
298 $prevbackend = $row['backend'];
300 /* Check if this user is selected */
301 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
303 /* Print one row, with alternating color */
305 $tr_bgcolor = $color[12];
307 $tr_bgcolor = $color[4];
309 if ($squirrelmail_language == 'ja_JP') {
310 echo html_tag( 'tr', '', '', $tr_bgcolor);
311 if ($abook->backends
[$row['backend']]->writeable
) {
314 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
316 'center', '', 'valign="top" width="1%"' );
320 'center', '', 'valign="top" width="1%"' );
322 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
323 html_tag( 'td', ' ' . $row['lastname'] . ' ' . $row['firstname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
324 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
326 echo html_tag( 'tr', '', '', $tr_bgcolor);
327 if ($abook->backends
[$row['backend']]->writeable
) {
330 addCheckBox('sel[]', $selected, $row['backend'] . ':' . $row['nickname']).
332 'center', '', 'valign="top" width="1%"' );
336 'center', '', 'valign="top" width="1%"' );
338 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
339 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
340 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
342 $email = $abook->full_address($row);
343 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
344 htmlspecialchars($row['email'])).
346 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
351 "\n<!-- end of address book table -->\n";
353 /* End of list. Add edit/delete select buttons */
354 if ($headerprinted) {
355 echo html_tag( 'table',
358 addSubmit(_("Edit selected"), 'editaddr') .
359 addSubmit(_("Delete selected"), 'deladdr'),
360 'center', '', 'colspan="5"' )
366 } /* end of addresslist */
369 /* Display the "new address" form */
370 echo '<a name="AddAddress"></a>' . "\n";
371 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
374 /* Add hook for anything that wants on the bottom */
375 do_hook('addressbook_bottom');