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
13 * @subpackage addressbook
17 * Path for SquirrelMail required files.
20 define('SM_PATH','../');
22 /** SquirrelMail required files. */
23 require_once(SM_PATH
. 'include/validate.php');
24 require_once(SM_PATH
. 'functions/display_messages.php');
25 require_once(SM_PATH
. 'functions/addressbook.php');
26 require_once(SM_PATH
. 'functions/forms.php');
28 /** lets get the global vars we may need */
29 sqgetGlobalVar('key', $key, SQ_COOKIE
);
31 sqgetGlobalVar('username', $username, SQ_SESSION
);
32 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
33 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
34 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
36 /* From the address form */
37 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
38 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
39 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
40 sqgetGlobalVar('sel', $sel, SQ_POST
);
41 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
42 sqgetGlobalVar('backend', $backend, SQ_POST
);
43 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
45 /* Get sorting order */
46 $abook_sort_order = get_abook_sort();
48 /* Open addressbook with error messages on.
49 remote backends (LDAP) are enabled because they can be used. (list_addr function)
51 $abook = addressbook_init(true, false);
52 if($abook->localbackend
== 0) {
54 _("No personal address book is defined. Contact administrator."),
59 displayPageHeader($color, 'None');
65 $defselected = array();
66 $form_url = 'addressbook.php';
68 /* Handle user's actions */
69 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
71 /**************************************************
73 **************************************************/
74 if (isset($addaddr)) {
75 if (isset($backend)) {
76 $r = $abook->add($addaddr, $backend);
78 $r = $abook->add($addaddr, $abook->localbackend
);
81 /* Handle error messages */
83 /* Remove backend name from error string */
84 $errstr = $abook->error
;
85 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
88 $showaddrlist = false;
93 /************************************************
94 * Delete address(es) *
95 ************************************************/
96 if ((!empty($deladdr)) && sizeof($sel) > 0) {
100 /* The selected addresses are identidied by "backend:nickname". *
101 * Sort the list and process one backend at the time */
106 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
107 list($sbackend, $snick) = explode(':', $sel[$i]);
109 /* When we get to a new backend, process addresses in *
111 if ($prevback != $sbackend && $prevback != -1) {
113 $r = $abook->remove($subsel, $prevback);
115 $formerror = $abook->error
;
123 /* Queue for processing */
124 array_push($subsel, $snick);
125 $prevback = $sbackend;
129 $r = $abook->remove($subsel, $prevback);
130 if (!$r) { /* Handle errors */
131 $formerror = $abook->error
;
137 $showaddrlist = true;
138 $defselected = $orig_sel;
143 /***********************************************
144 * Update/modify address *
145 ***********************************************/
146 if (!empty($editaddr)) {
148 /* Stage one: Copy data into form */
149 if (isset($sel) && sizeof($sel) > 0) {
150 if(sizeof($sel) > 1) {
151 $formerror = _("You can only edit one address at the time");
152 $showaddrlist = true;
156 list($ebackend, $enick) = explode(':', $sel[0]);
157 $olddata = $abook->lookup($enick, $ebackend);
159 /* Display the "new address" form */
160 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
161 echo addHidden('oldnick', $olddata['nickname']).
162 addHidden('backend', $olddata['backend']).
163 addHidden('doedit', '1').
168 /* Stage two: Write new data */
170 $newdata = $editaddr;
171 $r = $abook->modify($oldnick, $newdata, $backend);
173 /* Handle error messages */
176 echo html_tag( 'table',
179 "\n". '<strong><font color="' . $color[2] .
180 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
183 'center', '', 'width="100%"' );
185 /* Display the "new address" form again */
186 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
187 echo addHidden('oldnick', $oldnick).
188 addHidden('backend', $backend).
189 addHidden('doedit', '1').
195 /* Should not get here... */
196 plain_error_message(_("Unknown error"), $color);
200 } /* !empty($editaddr) - Update/modify address */
201 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
202 } /* !empty($addaddr['nickname']) - Add new address */
204 // Some times we end output before forms are printed
206 echo "</body></html>\n";
212 /* =================================================================== *
213 * The following is only executed on a GET request, or on a POST when *
214 * a user is added, or when "delete" or "modify" was successful. *
215 * =================================================================== */
217 /* Display error messages */
218 if (!empty($formerror)) {
219 echo html_tag( 'table',
222 "\n". '<br /><strong><font color="' . $color[2] .
223 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
226 'center', '', 'width="100%"' );
230 /* Display the address management part */
232 /* Get and sort address list */
233 $alist = $abook->list_addr();
234 if(!is_array($alist)) {
235 plain_error_message($abook->error
, $color);
239 usort($alist,'alistcmp');
241 $headerprinted = false;
243 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
246 if (count($alist) > 0) {
247 echo addForm($form_url, 'post');
248 while(list($undef,$row) = each($alist)) {
250 /* New table header for each backend */
251 if($prevbackend != $row['backend']) {
252 if($prevbackend < 0) {
253 echo html_tag( 'table',
256 addSubmit(_("Edit selected"), 'editaddr').
257 addSubmit(_("Delete selected"), 'deladdr'),
258 'center', '', 'colspan="5"' )
261 html_tag( 'td', ' <br />', 'center', '', 'colspan="5"' )
264 echo "\n<!-- start of address book table -->\n" .
265 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
266 html_tag( 'tr', "\n" .
267 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . "\n" .
268 html_tag( 'th', _("Nickname") .
269 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
270 'left', '', 'width="1%"' ) . "\n" .
271 html_tag( 'th', _("Name") .
272 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
273 'left', '', 'width="1%"' ) . "\n" .
274 html_tag( 'th', _("E-mail") .
275 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
276 'left', '', 'width="1%"' ) . "\n" .
277 html_tag( 'th', _("Info") .
278 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
279 'left', '', 'width="1%"' ) . "\n",
280 '', $color[9] ) . "\n";
283 // Separate different backends with <hr />
284 if($prevbackend > 0) {
286 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
290 // Print backend name
292 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
296 $headerprinted = true;
297 } /* End of header */
299 $prevbackend = $row['backend'];
301 /* Check if this user is selected */
302 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
304 /* Print one row, with alternating color */
306 $tr_bgcolor = $color[12];
308 $tr_bgcolor = $color[4];
310 if ($squirrelmail_language == 'ja_JP') {
311 echo html_tag( 'tr', '', '', $tr_bgcolor);
312 if ($abook->backends
[$row['backend']]->writeable
) {
315 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
317 'center', '', 'valign="top" width="1%"' );
321 'center', '', 'valign="top" width="1%"' );
323 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
324 html_tag( 'td', ' ' . $row['lastname'] . ' ' . $row['firstname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
325 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
327 echo html_tag( 'tr', '', '', $tr_bgcolor);
328 if ($abook->backends
[$row['backend']]->writeable
) {
331 addCheckBox('sel[]', $selected, $row['backend'] . ':' . $row['nickname']).
333 'center', '', 'valign="top" width="1%"' );
337 'center', '', 'valign="top" width="1%"' );
339 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
340 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" nowrap' ) .
341 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . ' ';
343 $email = $abook->full_address($row);
344 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
345 htmlspecialchars($row['email'])).
347 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
352 "\n<!-- end of address book table -->\n";
354 /* End of list. Add edit/delete select buttons */
355 if ($headerprinted) {
356 echo html_tag( 'table',
359 addSubmit(_("Edit selected"), 'editaddr') .
360 addSubmit(_("Delete selected"), 'deladdr'),
361 'center', '', 'colspan="5"' )
367 } /* end of addresslist */
370 /* Display the "new address" form */
371 echo '<a name="AddAddress"></a>' . "\n";
372 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
375 /* Add hook for anything that wants on the bottom */
376 echo "<!-- start of addressbook_bottom hook-->\n";
377 do_hook('addressbook_bottom');
378 echo "\n<!-- end of addressbook_bottom hook-->\n";