Sync configtest with core
[squirrelmail.git] / src / addressbook.php
1 <?php
2
3 /**
4 * addressbook.php
5 *
6 * Manage personal address book.
7 *
8 * @copyright 1999-2015 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage addressbook
13 */
14
15 /** This is the addressbook page */
16 define('PAGE_NAME', 'addressbook');
17
18 /**
19 * Include the SquirrelMail initialization file.
20 */
21 include('../include/init.php');
22
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');
27
28 /* form functions */
29 require_once(SM_PATH . 'functions/forms.php');
30
31 /** lets get the global vars we may need */
32
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))
46 $page_number = 1;
47 if (!sqGetGlobalVar('show_all', $show_all, SQ_FORM))
48 $show_all = 0;
49
50 /* Get sorting order */
51 $abook_sort_order = get_abook_sort();
52
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.
56 //
57 if ((empty($compose_to)) || sizeof($sel) < 1)
58 displayPageHeader($color);
59
60 /* Open addressbook with error messages on.
61 remote backends (LDAP) are enabled because they can be used. (list_addr function)
62 */
63 $abook = addressbook_init(true, false);
64
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."));
68 exit();
69 }
70
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;
75 }
76
77 $abook_selection = '&nbsp;';
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;
83 }
84 }
85 if (count($list_backends)>1) {
86 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
87 .addSubmit(_("Change"),'change_abook');
88 }
89 }
90
91 $defdata = array();
92 $formerror = '';
93 $abortform = false;
94 $showaddrlist = true;
95 $defselected = array();
96 $form_url = 'addressbook.php';
97
98 /* Handle user's actions */
99 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
100
101 // first, validate security token
102 sm_validate_security_token($submitted_token, -1, TRUE);
103
104 /**************************************************
105 * Add new address *
106 **************************************************/
107 if (isset($addaddr)) {
108 if (isset($backend)) {
109 $r = $abook->add($addaddr, $backend);
110 } else {
111 $r = $abook->add($addaddr, $abook->localbackend);
112 }
113
114 /* Handle error messages */
115 if (!$r) {
116 /* Remove backend name from error string */
117 $errstr = $abook->error;
118 $errstr = preg_replace('/^\[.*\] */', '', $errstr);
119
120 $formerror = $errstr;
121 $showaddrlist = false;
122 $defdata = $addaddr;
123 }
124 } else {
125
126 /************************************************
127 * Delete address(es) *
128 ************************************************/
129 if ((!empty($deladdr)) && sizeof($sel) > 0) {
130 $orig_sel = $sel;
131 sort($sel);
132
133 /* The selected addresses are identified by "backend_nickname". *
134 * Sort the list and process one backend at the time */
135 $prevback = -1;
136 $subsel = array();
137 $delfailed = false;
138
139 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
140 list($sbackend, $snick) = explode('_', $sel[$i], 2);
141
142 /* When we get to a new backend, process addresses in *
143 * previous one. */
144 if ($prevback != $sbackend && $prevback != -1) {
145
146 $r = $abook->remove($subsel, $prevback);
147 if (!$r) {
148 $formerror = $abook->error;
149 $i = sizeof($sel);
150 $delfailed = true;
151 break;
152 }
153 $subsel = array();
154 }
155
156 /* Queue for processing */
157 array_push($subsel, $snick);
158 $prevback = $sbackend;
159 }
160
161 if (!$delfailed) {
162 $r = $abook->remove($subsel, $prevback);
163 if (!$r) { /* Handle errors */
164 $formerror = $abook->error;
165 $delfailed = true;
166 }
167 }
168
169 if ($delfailed) {
170 $showaddrlist = true;
171 $defselected = $orig_sel;
172 }
173
174 /************************************************
175 * Compose to selected address(es) *
176 ************************************************/
177 } else if ((!empty($compose_to)) && sizeof($sel) > 0) {
178 $orig_sel = $sel;
179 sort($sel);
180
181 // The selected addresses are identified by "backend_nickname"
182 $lookup_failed = false;
183 $send_to = '';
184
185 for ($i = 0 ; (($i < sizeof($sel)) && !$lookup_failed) ; $i++) {
186 list($sbackend, $snick) = explode('_', $sel[$i], 2);
187
188 $data = $abook->lookup($snick, $sbackend);
189
190 if (!$data) {
191 $formerror = $abook->error;
192 $lookup_failed = true;
193 break;
194 } else {
195 $addr = $abook->full_address($data);
196 if (!empty($addr))
197 $send_to .= $addr . ', ';
198 }
199 }
200
201
202 if ($lookup_failed || empty($send_to)) {
203 $showaddrlist = true;
204 $defselected = $sel;
205
206 // we skipped the page header above for this functionality, so add it here
207 displayPageHeader($color);
208 }
209
210
211 // send off to compose screen
212 else {
213 $send_to = trim($send_to, ', ');
214 header('Location: ' . $base_uri . 'src/compose.php?send_to=' . rawurlencode($send_to));
215 exit;
216 }
217
218 } else {
219
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;
229 $defselected = $sel;
230 } else {
231 $abortform = 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
235 if (!$olddata) {
236 error_box(nl2br(sm_encode_html_special_chars($abook->error)));
237 } else {
238 /* Display the "new address" form */
239 echo abook_create_form($form_url, 'editaddr',
240 _("Update address"),
241 _("Update address"),
242 $current_backend,
243 $olddata);
244 echo addHidden('oldnick', $olddata['nickname']).
245 addHidden('backend', $olddata['backend']).
246 addHidden('doedit', '1').
247 '</form>';
248 }
249 }
250 } elseif ($doedit == 1) {
251 /* Stage two: Write new data */
252 $newdata = $editaddr;
253 $r = $abook->modify($oldnick, $newdata, $backend);
254
255 /* Handle error messages */
256 if (!$r) {
257 /* Display error */
258 plain_error_message( nl2br(sm_encode_html_special_chars($abook->error)));
259
260 /* Display the "new address" form again */
261 echo abook_create_form($form_url, 'editaddr',
262 _("Update address"),
263 _("Update address"),
264 $current_backend,
265 $newdata);
266 echo addHidden('oldnick', $oldnick).
267 addHidden('backend', $backend).
268 addHidden('doedit', '1').
269 "\n" . '</form>';
270 $abortform = true;
271 }
272 } else {
273 /**
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.
277 */
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 */
285
286 // Some times we end output before forms are printed
287 if($abortform) {
288 //FIXME: use footer.tpl; remove HTML from core
289 echo "</body></html>\n";
290 exit();
291 }
292 }
293
294
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 * =================================================================== */
299
300 /* Display error messages */
301 if (!empty($formerror)) {
302 plain_error_message(nl2br(sm_encode_html_special_chars($formerror)));
303 }
304
305
306 /* Display the address management part */
307 $addresses = array();
308 while (list($k, $backend) = each ($abook->backends)) {
309 $a = array();
310 $a['BackendID'] = $backend->bnum;
311 $a['BackendSource'] = $backend->sname;
312 $a['BackendWritable'] = $backend->writeable;
313 $a['Addresses'] = array();
314
315 // don't do address lookup if we are not viewing that backend
316 //
317 if ($backend->bnum == $current_backend) {
318 $alist = $abook->list_addr($backend->bnum);
319
320 /* check return (array with data or boolean false) */
321 if (is_array($alist)) {
322 usort($alist,'alistcmp');
323
324 $a['Addresses'] = formatAddressList($alist);
325
326 $addresses[$backend->bnum] = $a;
327 } else {
328 // list_addr() returns boolean
329 plain_error_message(nl2br(sm_encode_html_special_chars($abook->error)));
330 }
331 } else {
332 $addresses[$backend->bnum] = $a;
333 }
334 }
335
336
337 $current_page_args = array(
338 'abook_sort_order' => $abook_sort_order,
339 'new_bnum' => $current_backend,
340 'page_number' => $page_number,
341 );
342
343
344 // note that plugins can add to $current_page_args as well as
345 // filter the address list
346 //
347 $temp = array(&$addresses, &$current_backend, &$page_number, &$current_page_args);
348 do_hook('abook_list_filter', $temp);
349
350
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.
368
369
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
375 //
376 $total_addresses = 0;
377 if (!$show_all
378 && is_array($addresses[$current_backend]['Addresses'])
379 && empty($addresses[$current_backend]['Addresses'][0]['paginated'])) {
380
381 // at this point, we assume the current list is
382 // the *full* list
383 //
384 $total_addresses = sizeof($addresses[$current_backend]['Addresses']);
385
386 // iterate through all the entries, building list of addresses
387 // to keep based on current page
388 //
389 $new_address_list = array();
390 $total_pages = ceil($total_addresses / $page_size);
391 if ($page_number > $total_pages) $page_number = $total_pages;
392 $page_count = 1;
393 $page_item_count = 0;
394 foreach ($addresses[$current_backend]['Addresses'] as $addr) {
395 $page_item_count++;
396 if ($page_item_count > $page_size) {
397 $page_count++;
398 $page_item_count = 1;
399 }
400 if ($page_count == $page_number)
401 $new_address_list[] = $addr;
402 }
403 $addresses[$current_backend]['Addresses'] = $new_address_list;
404
405 }
406
407
408 if ($showaddrlist) {
409
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);
426
427 $oTemplate->display('addressbook_list.tpl');
428
429 }
430
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"),
436 _("Add address"),
437 $current_backend,
438 $defdata);
439 echo "</form>\n";
440
441 /* Hook for extra address book blocks */
442 do_hook('addressbook_bottom', $null);
443
444 $oTemplate->display('footer.tpl');