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