6 * Manage personal address book.
8 * @copyright © 1999-2005 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
12 * @subpackage addressbook
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 /* Create page header before addressbook_init in order to display error messages correctly. */
48 displayPageHeader($color, 'None');
50 /* Open addressbook with error messages on.
51 remote backends (LDAP) are enabled because they can be used. (list_addr function)
53 $abook = addressbook_init(true, false);
55 // FIXME: do we have to stop use of address book, when localbackend is not present.
56 if($abook->localbackend
== 0) {
58 _("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)) {
149 /* Stage one: Copy data into form */
150 if (isset($sel) && sizeof($sel) > 0) {
151 if(sizeof($sel) > 1) {
152 $formerror = _("You can only edit one address at the time");
153 $showaddrlist = true;
157 list($ebackend, $enick) = explode(':', $sel[0]);
158 $olddata = $abook->lookup($enick, $ebackend);
159 // FIXME: Test if $olddata really contains anything and return an error message if it doesn't
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').
168 } elseif ($doedit == 1) {
169 /* 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 * $editaddr is set, but $sel (address selection in address listing)
196 * and $doedit (address edit form) are not set.
197 * Assume that user clicked on "Edit address" without selecting any address.
199 $formerror = _("Please select address that you want to edit");
200 $showaddrlist = true;
201 } /* end of edit stage detection */
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 if ($abook->add_extra_field
) {
255 while(list($undef,$row) = each($alist)) {
257 /* New table header for each backend */
258 if($prevbackend != $row['backend']) {
259 if($prevbackend < 0) {
260 echo html_tag( 'table',
263 addSubmit(_("Edit selected"), 'editaddr').
264 addSubmit(_("Delete selected"), 'deladdr'),
265 'center', '', "colspan=\"$abook_fields\"" )
268 html_tag( 'td', ' <br />', 'center', '', "colspan=\"$abook_fields\"" )
271 echo "\n<!-- start of address book table -->\n" .
272 html_tag( 'table', '', 'center', '', 'border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
273 html_tag( 'tr', "\n" .
274 html_tag( 'th', ' ', 'left', '', 'width="1%"' ) . "\n" .
275 html_tag( 'th', _("Nickname") .
276 show_abook_sort_button($abook_sort_order, _("sort by nickname"), 0, 1),
277 'left', '', 'width="1%"' ) . "\n" .
278 html_tag( 'th', _("Name") .
279 show_abook_sort_button($abook_sort_order, _("sort by name"), 2, 3),
280 'left', '', 'width="1%"' ) . "\n" .
281 html_tag( 'th', _("E-mail") .
282 show_abook_sort_button($abook_sort_order, _("sort by email"), 4, 5),
283 'left', '', 'width="1%"' ) . "\n" .
284 html_tag( 'th', _("Info") .
285 show_abook_sort_button($abook_sort_order, _("sort by info"), 6, 7),
286 'left', '', 'width="1%"' ) .
287 ($abook->add_extra_field ?
html_tag( 'th', ' ','left', '', 'width="1%"'): '') .
289 '', $color[9] ) . "\n";
292 // Separate different backends with <hr />
293 if($prevbackend > 0) {
295 html_tag( 'td', "<hr />", 'center', '' ,"colspan=\"$abook_fields\"" )
299 // Print backend name
301 html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] ,"colspan=\"$abook_fields\"" )
305 $headerprinted = true;
306 } /* End of header */
308 $prevbackend = $row['backend'];
310 /* Check if this user is selected */
311 $selected = in_array($row['backend'] . ':' . $row['nickname'], $defselected);
313 /* Print one row, with alternating color */
315 $tr_bgcolor = $color[12];
317 $tr_bgcolor = $color[4];
319 echo html_tag( 'tr', '', '', $tr_bgcolor);
320 if ($abook->backends
[$row['backend']]->writeable
) {
323 addCheckBox('sel[]', $selected, $row['backend'].':'.$row['nickname']).
325 'center', '', 'valign="top" width="1%"' );
329 'center', '', 'valign="top" width="1%"' );
332 ' ' . htmlspecialchars($row['nickname']) . ' ',
333 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
335 // different full name display formating for Japanese translation
336 if ($squirrelmail_language == 'ja_JP') {
338 * translation uses euc-jp character set internally.
339 * htmlspecialchars() should not break any characters.
342 ' ' . htmlspecialchars($row['lastname']) . ' ' . htmlspecialchars($row['firstname']) . ' ',
343 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
346 ' ' . htmlspecialchars($row['name']) . ' ',
347 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' );
350 // email address column
351 echo html_tag( 'td', '', 'left', '', 'valign="top" width="1%" style="white-space: nowrap;"' ) . ' ';
352 $email = $abook->full_address($row);
353 echo makeComposeLink('src/compose.php?send_to='.rawurlencode($email),
354 htmlspecialchars($row['email'])).
358 echo html_tag( 'td', ' ' . htmlspecialchars($row['label']) . ' ', 'left', '', 'valign="top" width="1%"' );
360 // add extra column if third party backend needs it
361 if ($abook->add_extra_field
) {
363 ' ' . (isset($row['extra']) ?
$row['extra'] : '') . ' ',
364 'left', '', 'valign="top" width="1%"' );
370 "\n<!-- end of address book table -->\n";
372 /* End of list. Add edit/delete select buttons */
373 if ($headerprinted) {
374 echo html_tag( 'table',
377 addSubmit(_("Edit selected"), 'editaddr') .
378 addSubmit(_("Delete selected"), 'deladdr'),
379 'center', '', "colspan=\"$abook_fields\"" )
385 } /* end of addresslist */
388 /* Display the "new address" form */
389 echo '<a name="AddAddress"></a>' . "\n";
390 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
393 /* Add hook for anything that wants on the bottom */
394 echo "<!-- start of addressbook_bottom hook-->\n";
395 do_hook('addressbook_bottom');
396 echo "\n<!-- end of addressbook_bottom hook-->\n";