6 * Manage personal address book.
8 * @copyright 1999-2012 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('smtoken', $submitted_token, SQ_POST
, '');
35 sqgetGlobalVar('addaddr', $addaddr, SQ_POST
);
36 sqgetGlobalVar('editaddr', $editaddr, SQ_POST
);
37 sqgetGlobalVar('deladdr', $deladdr, SQ_POST
);
38 sqgetGlobalVar('compose_to', $compose_to, 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
);
43 $page_size = $abook_show_num;
44 if (!sqGetGlobalVar('page_number', $page_number, SQ_FORM
))
45 if (!sqGetGlobalVar('current_page_number', $page_number, SQ_FORM
))
47 if (!sqGetGlobalVar('show_all', $show_all, SQ_FORM
))
50 /* Get sorting order */
51 $abook_sort_order = get_abook_sort();
53 // Create page header before addressbook_init in order to
54 // display error messages correctly, unless we might be
55 // redirecting the browser to the compose page.
57 if ((empty($compose_to)) ||
sizeof($sel) < 1)
58 displayPageHeader($color);
60 /* Open addressbook with error messages on.
61 remote backends (LDAP) are enabled because they can be used. (list_addr function)
63 $abook = addressbook_init(true, false);
65 // FIXME: do we really have to stop use of address book when localbackend is not present?
66 if($abook->localbackend
== 0) {
67 plain_error_message(_("No personal address book is defined. Contact administrator."));
71 $current_backend = $abook->localbackend
;
72 if (sqgetGlobalVar('new_bnum', $new_backend, SQ_FORM
)
73 && array_key_exists($new_backend, $abook->backends
)) {
74 $current_backend = (int) $new_backend;
77 $abook_selection = ' ';
78 $list_backends = array();
79 if (count($abook->backends
) > 1) {
80 foreach($abook->get_backend_list() as $oBackend) {
81 if ($oBackend->listing
) {
82 $list_backends[$oBackend->bnum
]=$oBackend->sname
;
85 if (count($list_backends)>1) {
86 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
87 .addSubmit(_("Change"),'change_abook');
95 $defselected = array();
96 $form_url = 'addressbook.php';
98 /* Handle user's actions */
99 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER
) && $req_method == 'POST') {
101 // first, validate security token
102 sm_validate_security_token($submitted_token, 3600, TRUE);
104 /**************************************************
106 **************************************************/
107 if (isset($addaddr)) {
108 if (isset($backend)) {
109 $r = $abook->add($addaddr, $backend);
111 $r = $abook->add($addaddr, $abook->localbackend
);
114 /* Handle error messages */
116 /* Remove backend name from error string */
117 $errstr = $abook->error
;
118 $errstr = preg_replace('/^\[.*\] */', '', $errstr);
120 $formerror = $errstr;
121 $showaddrlist = false;
126 /************************************************
127 * Delete address(es) *
128 ************************************************/
129 if ((!empty($deladdr)) && sizeof($sel) > 0) {
133 /* The selected addresses are identified by "backend_nickname". *
134 * Sort the list and process one backend at the time */
139 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++
) {
140 list($sbackend, $snick) = explode('_', $sel[$i], 2);
142 /* When we get to a new backend, process addresses in *
144 if ($prevback != $sbackend && $prevback != -1) {
146 $r = $abook->remove($subsel, $prevback);
148 $formerror = $abook->error
;
156 /* Queue for processing */
157 array_push($subsel, $snick);
158 $prevback = $sbackend;
162 $r = $abook->remove($subsel, $prevback);
163 if (!$r) { /* Handle errors */
164 $formerror = $abook->error
;
170 $showaddrlist = true;
171 $defselected = $orig_sel;
174 /************************************************
175 * Compose to selected address(es) *
176 ************************************************/
177 } else if ((!empty($compose_to)) && sizeof($sel) > 0) {
181 // The selected addresses are identified by "backend_nickname"
182 $lookup_failed = false;
185 for ($i = 0 ; (($i < sizeof($sel)) && !$lookup_failed) ; $i++
) {
186 list($sbackend, $snick) = explode('_', $sel[$i], 2);
188 $data = $abook->lookup($snick, $sbackend);
191 $formerror = $abook->error
;
192 $lookup_failed = true;
195 $addr = $abook->full_address($data);
197 $send_to .= $addr . ', ';
202 if ($lookup_failed ||
empty($send_to)) {
203 $showaddrlist = true;
206 // we skipped the page header above for this functionality, so add it here
207 displayPageHeader($color);
211 // send off to compose screen
213 $send_to = trim($send_to, ', ');
214 header('Location: ' . $base_uri . 'src/compose.php?send_to=' . rawurlencode($send_to));
220 /***********************************************
221 * Update/modify address *
222 ***********************************************/
223 if (!empty($editaddr)) {
224 /* Stage one: Copy data into form */
225 if (isset($sel) && sizeof($sel) > 0) {
226 if(sizeof($sel) > 1) {
227 $formerror = _("You can only edit one address at the time");
228 $showaddrlist = true;
232 list($ebackend, $enick) = explode('_', current($sel), 2);
233 $olddata = $abook->lookup($enick, $ebackend);
234 // Test if $olddata really contains anything and return an error message if it doesn't
236 error_box(nl2br(sm_encode_html_special_chars($abook->error
)));
238 /* Display the "new address" form */
239 echo abook_create_form($form_url, 'editaddr',
244 echo addHidden('oldnick', $olddata['nickname']).
245 addHidden('backend', $olddata['backend']).
246 addHidden('doedit', '1').
250 } elseif ($doedit == 1) {
251 /* Stage two: Write new data */
252 $newdata = $editaddr;
253 $r = $abook->modify($oldnick, $newdata, $backend);
255 /* Handle error messages */
258 plain_error_message( nl2br(sm_encode_html_special_chars($abook->error
)));
260 /* Display the "new address" form again */
261 echo abook_create_form($form_url, 'editaddr',
266 echo addHidden('oldnick', $oldnick).
267 addHidden('backend', $backend).
268 addHidden('doedit', '1').
274 * $editaddr is set, but $sel (address selection in address listing)
275 * and $doedit (address edit form) are not set.
276 * Assume that user clicked on "Edit address" without selecting any address.
278 $formerror = _("Please select address that you want to edit");
279 $showaddrlist = true;
280 } /* end of edit stage detection */
281 } /* !empty($editaddr) - Update/modify address */
282 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es)
283 or (!empty($compose_to)) && sizeof($sel) > 0 - Compose to address(es) */
284 } /* !empty($addaddr['nickname']) - Add new address */
286 // Some times we end output before forms are printed
288 //FIXME: use footer.tpl; remove HTML from core
289 echo "</body></html>\n";
295 /* =================================================================== *
296 * The following is only executed on a GET request, or on a POST when *
297 * a user is added, or when "delete" or "modify" was successful. *
298 * =================================================================== */
300 /* Display error messages */
301 if (!empty($formerror)) {
302 plain_error_message(nl2br(sm_encode_html_special_chars($formerror)));
306 /* Display the address management part */
307 $addresses = array();
308 while (list($k, $backend) = each ($abook->backends
)) {
310 $a['BackendID'] = $backend->bnum
;
311 $a['BackendSource'] = $backend->sname
;
312 $a['BackendWritable'] = $backend->writeable
;
313 $a['Addresses'] = array();
315 // don't do address lookup if we are not viewing that backend
317 if ($backend->bnum
== $current_backend) {
318 $alist = $abook->list_addr($backend->bnum
);
320 /* check return (array with data or boolean false) */
321 if (is_array($alist)) {
322 usort($alist,'alistcmp');
324 $a['Addresses'] = formatAddressList($alist);
326 $addresses[$backend->bnum
] = $a;
328 // list_addr() returns boolean
329 plain_error_message(nl2br(sm_encode_html_special_chars($abook->error
)));
332 $addresses[$backend->bnum
] = $a;
337 $current_page_args = array(
338 'abook_sort_order' => $abook_sort_order,
339 'new_bnum' => $current_backend,
340 'page_number' => $page_number,
344 // note that plugins can add to $current_page_args as well as
345 // filter the address list
347 $temp = array(&$addresses, &$current_backend, &$page_number, &$current_page_args);
348 do_hook('abook_list_filter', $temp);
351 // NOTE to address book backend authors and plugin authors: if a backend does
352 // pagination (which might be more efficient), it needs to place a key
353 // in every address listing it returns called "paginated", whose value
354 // should evaluate to boolean TRUE. However, if a plugin will also be
355 // used on the hook above to filter the addresses (perhaps by group), then
356 // the backend should be made compatible with the filtering plugin and
357 // should do the actual filtering too. Otherwise, the backend will paginate
358 // before filtering has taken place, the output of which is clearly wrong.
359 // It is proposed that filtering be based on a GET/POST variable called
360 // "abook_groups_X" where X is the current backend number. The value of
361 // this varaible would be an array of possible filter names, which the
362 // plugin and the backend would both know about. The plugin would only
363 // filter based on that value if the backend didn't already do it. The
364 // backend can insert a "grouped" key into all address listings, whose
365 // value evaluates to boolean TRUE, telling the plugin not to do any
366 // filtering itself. For an example of this implementation, see the
367 // Address Book Grouping and Pagination plugin.
370 // if no pagination was done by a plugin or the abook
371 // backend (which is indicated by the presence of a
372 // "paginated" key within all of the address entries
373 // in the list of addresses for the backend currently
374 // being viewed), then we provide default pagination
376 $total_addresses = 0;
378 && is_array($addresses[$current_backend]['Addresses'])
379 && empty($addresses[$current_backend]['Addresses'][0]['paginated'])) {
381 // at this point, we assume the current list is
384 $total_addresses = sizeof($addresses[$current_backend]['Addresses']);
386 // iterate through all the entries, building list of addresses
387 // to keep based on current page
389 $new_address_list = array();
390 $total_pages = ceil($total_addresses / $page_size);
391 if ($page_number > $total_pages) $page_number = $total_pages;
393 $page_item_count = 0;
394 foreach ($addresses[$current_backend]['Addresses'] as $addr) {
396 if ($page_item_count > $page_size) {
398 $page_item_count = 1;
400 if ($page_count == $page_number)
401 $new_address_list[] = $addr;
403 $addresses[$current_backend]['Addresses'] = $new_address_list;
410 $oTemplate->assign('show_all', $show_all);
411 $oTemplate->assign('page_number', $page_number);
412 $oTemplate->assign('page_size', $page_size);
413 $oTemplate->assign('total_addresses', $total_addresses);
414 $oTemplate->assign('abook_compact_paginator', $abook_compact_paginator);
415 $oTemplate->assign('abook_page_selector', $abook_page_selector);
416 $oTemplate->assign('current_page_args', $current_page_args);
417 $oTemplate->assign('abook_page_selector_max', $abook_page_selector_max);
418 $oTemplate->assign('addresses', $addresses);
419 $oTemplate->assign('current_backend', $current_backend);
420 $oTemplate->assign('backends', $list_backends);
421 $oTemplate->assign('abook_has_extra_field', $abook->add_extra_field
);
422 $oTemplate->assign('compose_new_win', $compose_new_win);
423 $oTemplate->assign('compose_height', $compose_height);
424 $oTemplate->assign('compose_width', $compose_width);
425 $oTemplate->assign('form_action', $form_url);
427 $oTemplate->display('addressbook_list.tpl');
431 /* Display the "new address" form */
432 //FIXME: Remove HTML from here! (echo abook_create_form() is OK, since it is all template based output
433 echo '<a name="AddAddress"></a>' . "\n";
434 echo abook_create_form($form_url, 'addaddr',
435 _("Add to address book"),
441 /* Hook for extra address book blocks */
442 do_hook('addressbook_bottom', $null);
444 $oTemplate->display('footer.tpl');