6 * Manage personal address book.
8 * @copyright © 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
12 * @subpackage addressbook
15 /** This is the addressbook page */
16 define('PAGE_NAME', 'addressbook');
19 * Include the SquirrelMail initialization file.
21 include('../include/init.php');
23 /** SquirrelMail required files. */
24 /* address book functions */
25 require_once(SM_PATH
. 'functions/addressbook.php');
26 include_once(SM_PATH
. 'templates/util_addressbook.php');
29 require_once(SM_PATH
. 'functions/forms.php');
31 /** lets get the global vars we may need */
33 /* From the address form */
34 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
35 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
36 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
37 sqgetGlobalVar('compose_to', $compose_to, SQ_POST
);
38 sqgetGlobalVar('sel', $sel, SQ_POST
);
39 sqgetGlobalVar('oldnick', $oldnick, SQ_POST
);
40 sqgetGlobalVar('backend', $backend, SQ_POST
);
41 sqgetGlobalVar('doedit', $doedit, SQ_POST
);
42 $page_size = $abook_show_num;
43 if (!sqGetGlobalVar('page_number', $page_number, SQ_FORM
))
44 if (!sqGetGlobalVar('current_page_number', $page_number, SQ_FORM
))
46 if (!sqGetGlobalVar('show_all', $show_all, SQ_FORM
))
49 /* Get sorting order */
50 $abook_sort_order = get_abook_sort();
52 // Create page header before addressbook_init in order to
53 // display error messages correctly, unless we might be
54 // redirecting the browser to the compose page.
56 if ((empty($compose_to)) ||
sizeof($sel) < 1)
57 displayPageHeader($color);
59 /* Open addressbook with error messages on.
60 remote backends (LDAP) are enabled because they can be used. (list_addr function)
62 $abook = addressbook_init(true, false);
64 // FIXME: do we really have to stop use of address book when localbackend is not present?
65 if($abook->localbackend
== 0) {
66 plain_error_message(_("No personal address book is defined. Contact administrator."));
70 $current_backend = $abook->localbackend
;
71 if (sqgetGlobalVar('new_bnum', $new_backend, SQ_FORM
)
72 && array_key_exists($new_backend, $abook->backends
)) {
73 $current_backend = (int) $new_backend;
76 $abook_selection = ' ';
77 $list_backends = array();
78 if (count($abook->backends
) > 1) {
79 foreach($abook->get_backend_list() as $oBackend) {
80 if ($oBackend->listing
) {
81 $list_backends[$oBackend->bnum
]=$oBackend->sname
;
84 if (count($list_backends)>1) {
85 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
86 .addSubmit(_("Change"),'change_abook');
94 $defselected = array();
95 $form_url = 'addressbook.php';
97 /* Handle user's actions */
98 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
100 /**************************************************
102 **************************************************/
103 if (isset($addaddr)) {
104 if (isset($backend)) {
105 $r = $abook->add($addaddr, $backend);
107 $r = $abook->add($addaddr, $abook->localbackend
);
110 /* Handle error messages */
112 /* Remove backend name from error string */
113 $errstr = $abook->error
;
114 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
116 $formerror = $errstr;
117 $showaddrlist = false;
122 /************************************************
123 * Delete address(es) *
124 ************************************************/
125 if ((!empty($deladdr)) && sizeof($sel) > 0) {
129 /* The selected addresses are identified by "backend_nickname". *
130 * Sort the list and process one backend at the time */
135 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
136 list($sbackend, $snick) = explode('_', $sel[$i], 2);
138 /* When we get to a new backend, process addresses in *
140 if ($prevback != $sbackend && $prevback != -1) {
142 $r = $abook->remove($subsel, $prevback);
144 $formerror = $abook->error
;
152 /* Queue for processing */
153 array_push($subsel, $snick);
154 $prevback = $sbackend;
158 $r = $abook->remove($subsel, $prevback);
159 if (!$r) { /* Handle errors */
160 $formerror = $abook->error
;
166 $showaddrlist = true;
167 $defselected = $orig_sel;
170 /************************************************
171 * Compose to selected address(es) *
172 ************************************************/
173 } else if ((!empty($compose_to)) && sizeof($sel) > 0) {
177 // The selected addresses are identified by "backend_nickname"
178 $lookup_failed = false;
181 for ($i = 0 ; (($i < sizeof($sel)) && !$lookup_failed) ; $i++
) {
182 list($sbackend, $snick) = explode('_', $sel[$i], 2);
184 $data = $abook->lookup($snick, $sbackend);
187 $formerror = $abook->error
;
188 $lookup_failed = true;
191 $addr = $abook->full_address($data);
193 $send_to .= $addr . ', ';
198 if ($lookup_failed ||
empty($send_to)) {
199 $showaddrlist = true;
202 // we skipped the page header above for this functionality, so add it here
203 displayPageHeader($color);
207 // send off to compose screen
209 $send_to = trim($send_to, ', ');
210 header('Location: ' . $base_uri . 'src/compose.php?send_to=' . rawurlencode($send_to));
216 /***********************************************
217 * Update/modify address *
218 ***********************************************/
219 if (!empty($editaddr)) {
220 /* Stage one: Copy data into form */
221 if (isset($sel) && sizeof($sel) > 0) {
222 if(sizeof($sel) > 1) {
223 $formerror = _("You can only edit one address at the time");
224 $showaddrlist = true;
228 list($ebackend, $enick) = explode('_', current($sel), 2);
229 $olddata = $abook->lookup($enick, $ebackend);
230 // Test if $olddata really contains anything and return an error message if it doesn't
232 error_box(nl2br(htmlspecialchars($abook->error
)));
234 /* Display the "new address" form */
235 echo abook_create_form($form_url, 'editaddr',
240 echo addHidden('oldnick', $olddata['nickname']).
241 addHidden('backend', $olddata['backend']).
242 addHidden('doedit', '1').
246 } elseif ($doedit == 1) {
247 /* Stage two: Write new data */
248 $newdata = $editaddr;
249 $r = $abook->modify($oldnick, $newdata, $backend);
251 /* Handle error messages */
254 plain_error_message( nl2br(htmlspecialchars($abook->error
)));
256 /* Display the "new address" form again */
257 echo abook_create_form($form_url, 'editaddr',
262 echo addHidden('oldnick', $oldnick).
263 addHidden('backend', $backend).
264 addHidden('doedit', '1').
270 * $editaddr is set, but $sel (address selection in address listing)
271 * and $doedit (address edit form) are not set.
272 * Assume that user clicked on "Edit address" without selecting any address.
274 $formerror = _("Please select address that you want to edit");
275 $showaddrlist = true;
276 } /* end of edit stage detection */
277 } /* !empty($editaddr) - Update/modify address */
278 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es)
279 or (!empty($compose_to)) && sizeof($sel) > 0 - Compose to address(es) */
280 } /* !empty($addaddr['nickname']) - Add new address */
282 // Some times we end output before forms are printed
284 //FIXME: use footer.tpl; remove HTML from core
285 echo "</body></html>\n";
291 /* =================================================================== *
292 * The following is only executed on a GET request, or on a POST when *
293 * a user is added, or when "delete" or "modify" was successful. *
294 * =================================================================== */
296 /* Display error messages */
297 if (!empty($formerror)) {
298 plain_error_message(nl2br(htmlspecialchars($formerror)));
302 /* Display the address management part */
303 $addresses = array();
304 while (list($k, $backend) = each ($abook->backends
)) {
306 $a['BackendID'] = $backend->bnum
;
307 $a['BackendSource'] = $backend->sname
;
308 $a['BackendWritable'] = $backend->writeable
;
309 $a['Addresses'] = array();
311 // don't do address lookup if we are not viewing that backend
313 if ($backend->bnum
== $current_backend) {
314 $alist = $abook->list_addr($backend->bnum
);
316 /* check return (array with data or boolean false) */
317 if (is_array($alist)) {
318 usort($alist,'alistcmp');
320 $a['Addresses'] = formatAddressList($alist);
322 $addresses[$backend->bnum
] = $a;
324 // list_addr() returns boolean
325 plain_error_message(nl2br(htmlspecialchars($abook->error
)));
328 $addresses[$backend->bnum
] = $a;
333 $current_page_args = array(
334 'abook_sort_order' => $abook_sort_order,
335 'new_bnum' => $current_backend,
336 'page_number' => $page_number,
340 // note that plugins can add to $current_page_args as well as
341 // filter the address list
343 $temp = array(&$addresses, &$current_backend, &$page_number, &$current_page_args);
344 do_hook('abook_list_filter', $temp);
347 // NOTE to address book backend authors and plugin authors: if a backend does
348 // pagination (which might be more efficient), it needs to place a key
349 // in every address listing it returns called "paginated", whose value
350 // should evaluate to boolean TRUE. However, if a plugin will also be
351 // used on the hook above to filter the addresses (perhaps by group), then
352 // the backend should be made compatible with the filtering plugin and
353 // should do the actual filtering too. Otherwise, the backend will paginate
354 // before filtering has taken place, the output of which is clearly wrong.
355 // It is proposed that filtering be based on a GET/POST variable called
356 // "abook_groups_X" where X is the current backend number. The value of
357 // this varaible would be an array of possible filter names, which the
358 // plugin and the backend would both know about. The plugin would only
359 // filter based on that value if the backend didn't already do it. The
360 // backend can insert a "grouped" key into all address listings, whose
361 // value evaluates to boolean TRUE, telling the plugin not to do any
362 // filtering itself. For an example of this implementation, see the
363 // Address Book Grouping and Pagination plugin.
366 // if no pagination was done by a plugin or the abook
367 // backend (which is indicated by the presence of a
368 // "paginated" key within all of the address entries
369 // in the list of addresses for the backend currently
370 // being viewed), then we provide default pagination
372 $total_addresses = 0;
374 && is_array($addresses[$current_backend]['Addresses'])
375 && empty($addresses[$current_backend]['Addresses'][0]['paginated'])) {
377 // at this point, we assume the current list is
380 $total_addresses = sizeof($addresses[$current_backend]['Addresses']);
382 // iterate through all the entries, building list of addresses
383 // to keep based on current page
385 $new_address_list = array();
386 $total_pages = ceil($total_addresses / $page_size);
387 if ($page_number > $total_pages) $page_number = $total_pages;
389 $page_item_count = 0;
390 foreach ($addresses[$current_backend]['Addresses'] as $addr) {
392 if ($page_item_count > $page_size) {
394 $page_item_count = 1;
396 if ($page_count == $page_number)
397 $new_address_list[] = $addr;
399 $addresses[$current_backend]['Addresses'] = $new_address_list;
406 $oTemplate->assign('show_all', $show_all);
407 $oTemplate->assign('page_number', $page_number);
408 $oTemplate->assign('page_size', $page_size);
409 $oTemplate->assign('total_addresses', $total_addresses);
410 $oTemplate->assign('abook_compact_paginator', $abook_compact_paginator);
411 $oTemplate->assign('abook_page_selector', $abook_page_selector);
412 $oTemplate->assign('current_page_args', $current_page_args);
413 $oTemplate->assign('abook_page_selector_max', $abook_page_selector_max);
414 $oTemplate->assign('addresses', $addresses);
415 $oTemplate->assign('current_backend', $current_backend);
416 $oTemplate->assign('backends', $list_backends);
417 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field
);
418 $oTemplate->assign('compose_new_win', $compose_new_win);
419 $oTemplate->assign('compose_height', $compose_height);
420 $oTemplate->assign('compose_width', $compose_width);
421 $oTemplate->assign('form_action', $form_url);
423 $oTemplate->display('addressbook_list.tpl');
427 /* Display the "new address" form */
428 //FIXME: Remove HTML from here! (echo abook_create_form() is OK, since it is all template based output
429 echo '<a name="AddAddress"></a>' . "\n";
430 echo abook_create_form($form_url, 'addaddr',
431 _("Add to address book"),
437 /* Hook for extra address book blocks */
438 do_hook('addressbook_bottom', $null);
440 $oTemplate->display('footer.tpl');