8fafa44a656a093b1d65dbbdc593e4313235f1fc
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);
55 if($abook->localbackend
== 0) {
57 _("No personal address book is defined. Contact administrator."),
67 $defselected = array();
68 $form_url = 'addressbook.php';
70 /* Handle user's actions */
71 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
73 /**************************************************
75 **************************************************/
76 if (isset($addaddr)) {
77 if (isset($backend)) {
78 $r = $abook->add($addaddr, $backend);
80 $r = $abook->add($addaddr, $abook->localbackend
);
83 /* Handle error messages */
85 /* Remove backend name from error string */
86 $errstr = $abook->error
;
87 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
90 $showaddrlist = false;
95 /************************************************
96 * Delete address(es) *
97 ************************************************/
98 if ((!empty($deladdr)) && sizeof($sel) > 0) {
102 /* The selected addresses are identidied by "backend:nickname". *
103 * Sort the list and process one backend at the time */
108 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
109 list($sbackend, $snick) = explode(':', $sel[$i]);
111 /* When we get to a new backend, process addresses in *
113 if ($prevback != $sbackend && $prevback != -1) {
115 $r = $abook->remove($subsel, $prevback);
117 $formerror = $abook->error
;
125 /* Queue for processing */
126 array_push($subsel, $snick);
127 $prevback = $sbackend;
131 $r = $abook->remove($subsel, $prevback);
132 if (!$r) { /* Handle errors */
133 $formerror = $abook->error
;
139 $showaddrlist = true;
140 $defselected = $orig_sel;
145 /***********************************************
146 * Update/modify address *
147 ***********************************************/
148 if (!empty($editaddr)) {
150 /* Stage one: Copy data into form */
151 if (isset($sel) && sizeof($sel) > 0) {
152 if(sizeof($sel) > 1) {
153 $formerror = _("You can only edit one address at the time");
154 $showaddrlist = true;
158 list($ebackend, $enick) = explode(':', $sel[0]);
159 $olddata = $abook->lookup($enick, $ebackend);
161 /* Display the "new address" form */
162 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
163 echo addHidden('oldnick', $olddata['nickname']).
164 addHidden('backend', $olddata['backend']).
165 addHidden('doedit', '1').
170 /* Stage two: Write new data */
172 $newdata = $editaddr;
173 $r = $abook->modify($oldnick, $newdata, $backend);
175 /* Handle error messages */
178 echo html_tag( 'table',
181 "\n". '<strong><font color="' . $color[2] .
182 '">' . _("ERROR") . ': ' . $abook->error
. '</font></strong>' ."\n",
185 'center', '', 'width="100%"' );
187 /* Display the "new address" form again */
188 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
189 echo addHidden('oldnick', $oldnick).
190 addHidden('backend', $backend).
191 addHidden('doedit', '1').
197 /* Should not get here... */
198 plain_error_message(_("Unknown error"), $color);
202 } /* !empty($editaddr) - Update/modify address */
203 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
204 } /* !empty($addaddr['nickname']) - Add new address */
206 // Some times we end output before forms are printed
208 echo "</body></html>\n";
214 /* =================================================================== *
215 * The following is only executed on a GET request, or on a POST when *
216 * a user is added, or when "delete" or "modify" was successful. *
217 * =================================================================== */
219 /* Display error messages */
220 if (!empty($formerror)) {
221 echo html_tag( 'table',
224 "\n". '<br /><strong><font color="' . $color[2] .
225 '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
228 'center', '', 'width="100%"' );
232 /* Display the address management part */
234 /* Get and sort address list */
235 $alist = $abook->list_addr();
236 if(!is_array($alist)) {
237 plain_error_message($abook->error
, $color);
241 usort($alist,'alistcmp');
243 $headerprinted = false;
245 echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
248 if (count($alist) > 0) {
249 echo addForm($form_url, 'post');
250 while(list($undef,$row) = each($alist)) {
252 /* New table header for each backend */
253 if($prevbackend != $row['backend']) {
254 if($prevbackend < 0) {
255 echo html_tag( 'table',
258 addSubmit(_("Edit selected"), 'editaddr').
259 addSubmit(_("Delete selected"), 'deladdr'),
260 'center', '', 'colspan="5"' )
263 html_tag( 'td', ' <br />', 'center', '', 'colspan="5"' )
266 echo "\n<!-- start of address book table -->\n" .
267 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
268 html_tag( 'tr', "\n" .
269 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . "\n" .
270 html_tag( 'th', _("Nickname") .
271 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
272 'left', '', 'width="1%"' ) . "\n" .
273 html_tag( 'th', _("Name") .
274 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
275 'left', '', 'width="1%"' ) . "\n" .
276 html_tag( 'th', _("E-mail") .
277 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
278 'left', '', 'width="1%"' ) . "\n" .
279 html_tag( 'th', _("Info") .
280 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
281 'left', '', 'width="1%"' ) . "\n",
282 '', $color[9] ) . "\n";
285 // Separate different backends with <hr />
286 if($prevbackend > 0) {
288 html_tag( 'td', "<hr />", 'center', '' ,'colspan="5"' )
292 // Print backend name
294 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,'colspan="5"' )
298 $headerprinted = true;
299 } /* End of header */
301 $prevbackend = $row['backend'];
303 /* Check if this user is selected */
304 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
306 /* Print one row, with alternating color */
308 $tr_bgcolor = $color[12];
310 $tr_bgcolor = $color[4];
312 if ($squirrelmail_language == 'ja_JP') {
313 echo html_tag( 'tr', '', '', $tr_bgcolor);
314 if ($abook->backends
[$row['backend']]->writeable
) {
317 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
319 'center', '', 'valign="top" width="1%"' );
323 'center', '', 'valign="top" width="1%"' );
325 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
326 html_tag( 'td', ' ' . $row['lastname'] . ' ' . $row['firstname'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
327 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . ' ';
329 echo html_tag( 'tr', '', '', $tr_bgcolor);
330 if ($abook->backends
[$row['backend']]->writeable
) {
333 addCheckBox('sel[]', $selected, $row['backend'] . ':' . $row['nickname']).
335 'center', '', 'valign="top" width="1%"' );
339 'center', '', 'valign="top" width="1%"' );
341 echo html_tag( 'td', ' ' . $row['nickname'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
342 html_tag( 'td', ' ' . $row['name'] . ' ', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) .
343 html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . ' ';
345 $email = $abook->full_address($row);
346 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
347 htmlspecialchars($row['email'])).
349 html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' ) .
354 "\n<!-- end of address book table -->\n";
356 /* End of list. Add edit/delete select buttons */
357 if ($headerprinted) {
358 echo html_tag( 'table',
361 addSubmit(_("Edit selected"), 'editaddr') .
362 addSubmit(_("Delete selected"), 'deladdr'),
363 'center', '', 'colspan="5"' )
369 } /* end of addresslist */
372 /* Display the "new address" form */
373 echo '<a name="AddAddress"></a>' . "\n";
374 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
377 /* Add hook for anything that wants on the bottom */
378 echo "<!-- start of addressbook_bottom hook-->\n";
379 do_hook('addressbook_bottom');
380 echo "\n<!-- end of addressbook_bottom hook-->\n";