6 * Copyright (c) 1999-2005 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 /* Create page header before addressbook_init in order to display error messages correctly. */
49 displayPageHeader($color, 'None');
51 /* Open addressbook with error messages on.
52 remote backends (LDAP) are enabled because they can be used. (list_addr function)
54 $abook = addressbook_init(true, false);
56 // FIXME: do we have to stop use of address book, when localbackend is not present.
57 if($abook->localbackend
== 0) {
59 _("No personal address book is defined. Contact administrator."),
69 $defselected = array();
70 $form_url = 'addressbook.php';
72 /* Handle user's actions */
73 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
75 /**************************************************
77 **************************************************/
78 if (isset($addaddr)) {
79 if (isset($backend)) {
80 $r = $abook->add($addaddr, $backend);
82 $r = $abook->add($addaddr, $abook->localbackend
);
85 /* Handle error messages */
87 /* Remove backend name from error string */
88 $errstr = $abook->error
;
89 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
92 $showaddrlist = false;
97 /************************************************
98 * Delete address(es) *
99 ************************************************/
100 if ((!empty($deladdr)) && sizeof($sel) > 0) {
104 /* The selected addresses are identidied by "backend:nickname". *
105 * Sort the list and process one backend at the time */
110 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
111 list($sbackend, $snick) = explode(':', $sel[$i]);
113 /* When we get to a new backend, process addresses in *
115 if ($prevback != $sbackend && $prevback != -1) {
117 $r = $abook->remove($subsel, $prevback);
119 $formerror = $abook->error
;
127 /* Queue for processing */
128 array_push($subsel, $snick);
129 $prevback = $sbackend;
133 $r = $abook->remove($subsel, $prevback);
134 if (!$r) { /* Handle errors */
135 $formerror = $abook->error
;
141 $showaddrlist = true;
142 $defselected = $orig_sel;
147 /***********************************************
148 * Update/modify address *
149 ***********************************************/
150 if (!empty($editaddr)) {
152 /* Stage one: Copy data into form */
153 if (isset($sel) && sizeof($sel) > 0) {
154 if(sizeof($sel) > 1) {
155 $formerror = _("You can only edit one address at the time");
156 $showaddrlist = true;
160 list($ebackend, $enick) = explode(':', $sel[0]);
161 $olddata = $abook->lookup($enick, $ebackend);
163 /* Display the "new address" form */
164 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
165 echo addHidden('oldnick', $olddata['nickname']).
166 addHidden('backend', $olddata['backend']).
167 addHidden('doedit', '1').
172 /* Stage two: Write new data */
174 $newdata = $editaddr;
175 $r = $abook->modify($oldnick, $newdata, $backend);
177 /* Handle error messages */
180 echo html_tag( 'table',
183 "\n". '<strong><font color="' . $color[2] .
184 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
187 'center', '', 'width="100%"' );
189 /* Display the "new address" form again */
190 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
191 echo addHidden('oldnick', $oldnick).
192 addHidden('backend', $backend).
193 addHidden('doedit', '1').
199 /* Should not get here... */
200 plain_error_message(_("Unknown error"), $color);
204 } /* !empty($editaddr) - Update/modify address */
205 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
206 } /* !empty($addaddr['nickname']) - Add new address */
208 // Some times we end output before forms are printed
210 echo "</body></html>\n";
216 /* =================================================================== *
217 * The following is only executed on a GET request, or on a POST when *
218 * a user is added, or when "delete" or "modify" was successful. *
219 * =================================================================== */
221 /* Display error messages */
222 if (!empty($formerror)) {
223 echo html_tag( 'table',
226 "\n". '<br /><strong><font color="' . $color[2] .
227 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
230 'center', '', 'width="100%"' );
234 /* Display the address management part */
236 /* Get and sort address list */
237 $alist = $abook->list_addr();
238 if(!is_array($alist)) {
239 plain_error_message($abook->error
, $color);
243 usort($alist,'alistcmp');
245 $headerprinted = false;
247 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
250 if (count($alist) > 0) {
251 echo addForm($form_url, 'post');
252 while(list($undef,$row) = each($alist)) {
254 /* New table header for each backend */
255 if($prevbackend != $row['backend']) {
256 if($prevbackend < 0) {
257 echo html_tag( 'table',
260 addSubmit(_("Edit selected"), 'editaddr').
261 addSubmit(_("Delete selected"), 'deladdr'),
262 'center', '', 'colspan="5"' )
265 html_tag( 'td', ' <br />', 'center', '', 'colspan="5"' )
268 echo "\n<!-- start of address book table -->\n" .
269 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
270 html_tag( 'tr', "\n" .
271 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . "\n" .
272 html_tag( 'th', _("Nickname") .
273 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
274 'left', '', 'width="1%"' ) . "\n" .
275 html_tag( 'th', _("Name") .
276 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
277 'left', '', 'width="1%"' ) . "\n" .
278 html_tag( 'th', _("E-mail") .
279 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
280 'left', '', 'width="1%"' ) . "\n" .
281 html_tag( 'th', _("Info") .
282 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
283 'left', '', 'width="1%"' ) . "\n",
284 '', $color[9] ) . "\n";
287 // Separate different backends with <hr />
288 if($prevbackend > 0) {
290 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
294 // Print backend name
296 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
300 $headerprinted = true;
301 } /* End of header */
303 $prevbackend = $row['backend'];
305 /* Check if this user is selected */
306 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
308 /* Print one row, with alternating color */
310 $tr_bgcolor = $color[12];
312 $tr_bgcolor = $color[4];
314 if ($squirrelmail_language == 'ja_JP') {
315 echo html_tag( 'tr', '', '', $tr_bgcolor);
316 if ($abook->backends
[$row['backend']]->writeable
) {
319 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
321 'center', '', 'valign="top" width="1%"' );
325 'center', '', 'valign="top" width="1%"' );
327 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
328 html_tag( 'td', ' ' . $row['lastname'] . ' ' . $row['firstname'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
329 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . ' ';
331 echo html_tag( 'tr', '', '', $tr_bgcolor);
332 if ($abook->backends
[$row['backend']]->writeable
) {
335 addCheckBox('sel[]', $selected, $row['backend'] . ':' . $row['nickname']).
337 'center', '', 'valign="top" width="1%"' );
341 'center', '', 'valign="top" width="1%"' );
343 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
344 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
345 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . ' ';
347 $email = $abook->full_address($row);
348 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
349 htmlspecialchars($row['email'])).
351 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
356 "\n<!-- end of address book table -->\n";
358 /* End of list. Add edit/delete select buttons */
359 if ($headerprinted) {
360 echo html_tag( 'table',
363 addSubmit(_("Edit selected"), 'editaddr') .
364 addSubmit(_("Delete selected"), 'deladdr'),
365 'center', '', 'colspan="5"' )
371 } /* end of addresslist */
374 /* Display the "new address" form */
375 echo '<a name="AddAddress"></a>' . "\n";
376 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
379 /* Add hook for anything that wants on the bottom */
380 echo "<!-- start of addressbook_bottom hook-->\n";
381 do_hook('addressbook_bottom');
382 echo "\n<!-- end of addressbook_bottom hook-->\n";