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)) {
151 /* Stage one: Copy data into form */
152 if (isset($sel) && sizeof($sel) > 0) {
153 if(sizeof($sel) > 1) {
154 $formerror = _("You can only edit one address at the time");
155 $showaddrlist = true;
159 list($ebackend, $enick) = explode(':', $sel[0]);
160 $olddata = $abook->lookup($enick, $ebackend);
162 /* Display the "new address" form */
163 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
164 echo addHidden('oldnick', $olddata['nickname']).
165 addHidden('backend', $olddata['backend']).
166 addHidden('doedit', '1').
169 } elseif ($doedit == 1) {
170 /* Stage two: Write new data */
171 $newdata = $editaddr;
172 $r = $abook->modify($oldnick, $newdata, $backend);
174 /* Handle error messages */
177 echo html_tag( 'table',
180 "\n". '<strong><font color="' . $color[2] .
181 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
184 'center', '', 'width="100%"' );
186 /* Display the "new address" form again */
187 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
188 echo addHidden('oldnick', $oldnick).
189 addHidden('backend', $backend).
190 addHidden('doedit', '1').
196 * $editaddr is set, but $sel (address selection in address listing)
197 * and $doedit (address edit form) are not set.
198 * Assume that user clicked on "Edit address" without selecting any address.
200 $formerror = _("Please select address that you want to edit");
201 $showaddrlist = true;
202 } /* end of edit stage detection */
203 } /* !empty($editaddr) - Update/modify address */
204 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
205 } /* !empty($addaddr['nickname']) - Add new address */
207 // Some times we end output before forms are printed
209 echo "</body></html>\n";
215 /* =================================================================== *
216 * The following is only executed on a GET request, or on a POST when *
217 * a user is added, or when "delete" or "modify" was successful. *
218 * =================================================================== */
220 /* Display error messages */
221 if (!empty($formerror)) {
222 echo html_tag( 'table',
225 "\n". '<br /><strong><font color="' . $color[2] .
226 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
229 'center', '', 'width="100%"' );
233 /* Display the address management part */
235 /* Get and sort address list */
236 $alist = $abook->list_addr();
237 if(!is_array($alist)) {
238 plain_error_message($abook->error
, $color);
242 usort($alist,'alistcmp');
244 $headerprinted = false;
246 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
249 if (count($alist) > 0) {
250 echo addForm($form_url, 'post');
251 while(list($undef,$row) = each($alist)) {
253 /* New table header for each backend */
254 if($prevbackend != $row['backend']) {
255 if($prevbackend < 0) {
256 echo html_tag( 'table',
259 addSubmit(_("Edit selected"), 'editaddr').
260 addSubmit(_("Delete selected"), 'deladdr'),
261 'center', '', 'colspan="5"' )
264 html_tag( 'td', ' <br />', 'center', '', 'colspan="5"' )
267 echo "\n<!-- start of address book table -->\n" .
268 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
269 html_tag( 'tr', "\n" .
270 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . "\n" .
271 html_tag( 'th', _("Nickname") .
272 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
273 'left', '', 'width="1%"' ) . "\n" .
274 html_tag( 'th', _("Name") .
275 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
276 'left', '', 'width="1%"' ) . "\n" .
277 html_tag( 'th', _("E-mail") .
278 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
279 'left', '', 'width="1%"' ) . "\n" .
280 html_tag( 'th', _("Info") .
281 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
282 'left', '', 'width="1%"' ) . "\n",
283 '', $color[9] ) . "\n";
286 // Separate different backends with <hr />
287 if($prevbackend > 0) {
289 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
293 // Print backend name
295 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
299 $headerprinted = true;
300 } /* End of header */
302 $prevbackend = $row['backend'];
304 /* Check if this user is selected */
305 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
307 /* Print one row, with alternating color */
309 $tr_bgcolor = $color[12];
311 $tr_bgcolor = $color[4];
313 if ($squirrelmail_language == 'ja_JP') {
314 echo html_tag( 'tr', '', '', $tr_bgcolor);
315 if ($abook->backends
[$row['backend']]->writeable
) {
318 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
320 'center', '', 'valign="top" width="1%"' );
324 'center', '', 'valign="top" width="1%"' );
326 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
327 html_tag( 'td', ' ' . $row['lastname'] . ' ' . $row['firstname'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
328 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . ' ';
330 echo html_tag( 'tr', '', '', $tr_bgcolor);
331 if ($abook->backends
[$row['backend']]->writeable
) {
334 addCheckBox('sel[]', $selected, $row['backend'] . ':' . $row['nickname']).
336 'center', '', 'valign="top" width="1%"' );
340 'center', '', 'valign="top" width="1%"' );
342 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
343 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
344 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . ' ';
346 $email = $abook->full_address($row);
347 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
348 htmlspecialchars($row['email'])).
350 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
355 "\n<!-- end of address book table -->\n";
357 /* End of list. Add edit/delete select buttons */
358 if ($headerprinted) {
359 echo html_tag( 'table',
362 addSubmit(_("Edit selected"), 'editaddr') .
363 addSubmit(_("Delete selected"), 'deladdr'),
364 'center', '', 'colspan="5"' )
370 } /* end of addresslist */
373 /* Display the "new address" form */
374 echo '<a name="AddAddress"></a>' . "\n";
375 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
378 /* Add hook for anything that wants on the bottom */
379 echo "<!-- start of addressbook_bottom hook-->\n";
380 do_hook('addressbook_bottom');
381 echo "\n<!-- end of addressbook_bottom hook-->\n";