| 1 | <?php |
| 2 | /** |
| 3 | * addressbook.php |
| 4 | * |
| 5 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
| 6 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 7 | * |
| 8 | * Functions and classes for the addressbook system. |
| 9 | * |
| 10 | * @version $Id$ |
| 11 | * @package squirrelmail |
| 12 | * @subpackage addressbook |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | This is the path to the global site-wide addressbook. |
| 17 | It looks and feels just like a user's .abook file |
| 18 | If this is in the data directory, use "$data_dir/global.abook" |
| 19 | If not, specify the path as though it was accessed from the |
| 20 | src/ directory ("../global.abook" -> in main directory) |
| 21 | |
| 22 | If you don't want a global site-wide addressbook, comment these |
| 23 | two lines out. (They are disabled by default.) |
| 24 | |
| 25 | The global addressbook is unmodifiable by anyone. You must actually |
| 26 | use a shell script or whatnot to modify the contents. |
| 27 | |
| 28 | global $data_dir, $address_book_global_filename; |
| 29 | $address_book_global_filename = "$data_dir/global.abook"; |
| 30 | |
| 31 | */ |
| 32 | |
| 33 | global $addrbook_dsn, $addrbook_global_dsn; |
| 34 | |
| 35 | /** |
| 36 | Create and initialize an addressbook object. |
| 37 | Returns the created object |
| 38 | */ |
| 39 | function addressbook_init($showerr = true, $onlylocal = false) { |
| 40 | global $data_dir, $username, $ldap_server, $address_book_global_filename; |
| 41 | global $addrbook_dsn, $addrbook_table; |
| 42 | global $abook_global_file, $abook_global_file_writeable; |
| 43 | global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing; |
| 44 | |
| 45 | /* Create a new addressbook object */ |
| 46 | $abook = new AddressBook; |
| 47 | |
| 48 | /* |
| 49 | Always add a local backend. We use *either* file-based *or* a |
| 50 | database addressbook. If $addrbook_dsn is set, the database |
| 51 | backend is used. If not, addressbooks are stores in files. |
| 52 | */ |
| 53 | if (isset($addrbook_dsn) && !empty($addrbook_dsn)) { |
| 54 | /* Database */ |
| 55 | if (!isset($addrbook_table) || empty($addrbook_table)) { |
| 56 | $addrbook_table = 'address'; |
| 57 | } |
| 58 | $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn, |
| 59 | 'owner' => $username, |
| 60 | 'table' => $addrbook_table)); |
| 61 | if (!$r && $showerr) { |
| 62 | echo _("Error initializing addressbook database."); |
| 63 | exit; |
| 64 | } |
| 65 | } else { |
| 66 | /* File */ |
| 67 | $filename = getHashedFile($username, $data_dir, "$username.abook"); |
| 68 | $r = $abook->add_backend('local_file', Array('filename' => $filename, |
| 69 | 'create' => true)); |
| 70 | if(!$r && $showerr) { |
| 71 | printf( _("Error opening file %s"), $filename ); |
| 72 | exit; |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | |
| 77 | /* This would be for the global addressbook */ |
| 78 | if (isset($abook_global_file) && isset($abook_global_file_writeable) |
| 79 | && trim($abook_global_file)!=''){ |
| 80 | // Detect place of address book |
| 81 | if (! preg_match("/[\/\\\]/",$abook_global_file)) { |
| 82 | // no path chars |
| 83 | $abook_global_filename=$data_dir . $abook_global_file; |
| 84 | } elseif (preg_match("/^\/|\w:/",$abook_global_file)) { |
| 85 | // full path is set in options (starts with slash or x:) |
| 86 | $abook_global_filename=$abook_global_file; |
| 87 | } else { |
| 88 | $abook_global_filename=SM_PATH . $abook_global_file; |
| 89 | } |
| 90 | $r = $abook->add_backend('local_file',array('filename'=>$abook_global_filename, |
| 91 | 'name' => _("Global address book"), |
| 92 | 'detect_writeable' => false, |
| 93 | 'writeable'=> $abook_global_file_writeable)); |
| 94 | if (!$r && $showerr) { |
| 95 | echo _("Error initializing global addressbook."); |
| 96 | exit; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* Load global addressbook from SQL if configured */ |
| 101 | if (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) { |
| 102 | /* Database configured */ |
| 103 | if (!isset($addrbook_global_table) || empty($addrbook_global_table)) { |
| 104 | $addrbook_global_table = 'global_abook'; |
| 105 | } |
| 106 | $r = $abook->add_backend('database', |
| 107 | Array('dsn' => $addrbook_global_dsn, |
| 108 | 'owner' => 'global', |
| 109 | 'name' => _("Global address book"), |
| 110 | 'writeable' => $addrbook_global_writeable, |
| 111 | 'listing' => $addrbook_global_listing, |
| 112 | 'table' => $addrbook_global_table)); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * hook allows to include different address book backends. |
| 117 | * plugins should extract $abook and $r from arguments |
| 118 | * and use same add_backend commands as above functions. |
| 119 | */ |
| 120 | $hookReturn = do_hook('abook_init', $abook, $r); |
| 121 | $abook = $hookReturn[1]; |
| 122 | $r = $hookReturn[2]; |
| 123 | |
| 124 | if ($onlylocal) { |
| 125 | return $abook; |
| 126 | } |
| 127 | |
| 128 | /* Load configured LDAP servers (if PHP has LDAP support) */ |
| 129 | if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) { |
| 130 | reset($ldap_server); |
| 131 | while (list($undef,$param) = each($ldap_server)) { |
| 132 | if (is_array($param)) { |
| 133 | $r = $abook->add_backend('ldap_server', $param); |
| 134 | if (!$r && $showerr) { |
| 135 | printf( ' ' . _("Error initializing LDAP server %s:") . |
| 136 | "<br />\n", $param['host']); |
| 137 | echo ' ' . $abook->error; |
| 138 | exit; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* Return the initialized object */ |
| 145 | return $abook; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Display the "new address" form |
| 150 | * |
| 151 | * Form is not closed and you must add closing form tag. |
| 152 | * @since 1.5.1 |
| 153 | * @param string $form_url form action url |
| 154 | * @param string $name form name |
| 155 | * @param string $title form title |
| 156 | * @param string $button form button name |
| 157 | * @param array $defdata values of form fields |
| 158 | */ |
| 159 | function abook_create_form($form_url,$name,$title,$button,$defdata=array()) { |
| 160 | global $color; |
| 161 | echo addForm($form_url, 'post', 'f_add'). |
| 162 | html_tag( 'table', |
| 163 | html_tag( 'tr', |
| 164 | html_tag( 'td', "\n". '<strong>' . $title . '</strong>' . "\n", |
| 165 | 'center', $color[0] |
| 166 | ) |
| 167 | ) |
| 168 | , 'center', '', 'width="100%"' ) ."\n"; |
| 169 | address_form($name, $button, $defdata); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /* |
| 174 | * Had to move this function outside of the Addressbook Class |
| 175 | * PHP 4.0.4 Seemed to be having problems with inline functions. |
| 176 | * Note: this can return now since we don't support 4.0.4 anymore. |
| 177 | */ |
| 178 | function addressbook_cmp($a,$b) { |
| 179 | |
| 180 | if($a['backend'] > $b['backend']) { |
| 181 | return 1; |
| 182 | } else if($a['backend'] < $b['backend']) { |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1; |
| 187 | |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Make an input field |
| 192 | * @param string $label |
| 193 | * @param string $field |
| 194 | * @param string $name |
| 195 | * @param string $size |
| 196 | * @param array $values |
| 197 | * @param string $add |
| 198 | */ |
| 199 | function addressbook_inp_field($label, $field, $name, $size, $values, $add='') { |
| 200 | global $color; |
| 201 | $value = ( isset($values[$field]) ? $values[$field] : ''); |
| 202 | |
| 203 | if (is_array($value)) { |
| 204 | $td_str = addSelect($name.'['.$field.']', $value); |
| 205 | } else { |
| 206 | $td_str = addInput($name.'['.$field.']', $value, $size); |
| 207 | } |
| 208 | $td_str .= $add ; |
| 209 | |
| 210 | return html_tag( 'tr' , |
| 211 | html_tag( 'td', $label . ':', 'right', $color[4]) . |
| 212 | html_tag( 'td', $td_str, 'left', $color[4]) |
| 213 | ) |
| 214 | . "\n"; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Output form to add and modify address data |
| 219 | */ |
| 220 | function address_form($name, $submittext, $values = array()) { |
| 221 | global $color, $squirrelmail_language; |
| 222 | |
| 223 | if ($squirrelmail_language == 'ja_JP') { |
| 224 | echo html_tag( 'table', |
| 225 | addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values, |
| 226 | ' <small>' . _("Must be unique") . '</small>') . |
| 227 | addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') . |
| 228 | addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') . |
| 229 | addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') . |
| 230 | addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') . |
| 231 | list_writable_backends($name) . |
| 232 | html_tag( 'tr', |
| 233 | html_tag( 'td', |
| 234 | addSubmit($submittext, $name.'[SUBMIT]'), |
| 235 | 'center', $color[4], 'colspan="2"') |
| 236 | ) |
| 237 | , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n"; |
| 238 | } else { |
| 239 | echo html_tag( 'table', |
| 240 | addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values, |
| 241 | ' <small>' . _("Must be unique") . '</small>') . |
| 242 | addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') . |
| 243 | addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') . |
| 244 | addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') . |
| 245 | addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') . |
| 246 | list_writable_backends($name) . |
| 247 | html_tag( 'tr', |
| 248 | html_tag( 'td', |
| 249 | addSubmit($submittext, $name.'[SUBMIT]') , |
| 250 | 'center', $color[4], 'colspan="2"') |
| 251 | ) |
| 252 | , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n"; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | function list_writable_backends($name) { |
| 257 | global $color, $abook; |
| 258 | if ( $name != 'addaddr' ) { return; } |
| 259 | if ( $abook->numbackends > 1 ) { |
| 260 | $ret = '<select name="backend">'; |
| 261 | $backends = $abook->get_backend_list(); |
| 262 | while (list($undef,$v) = each($backends)) { |
| 263 | if ($v->writeable) { |
| 264 | $ret .= '<option value="' . $v->bnum; |
| 265 | $ret .= '">' . $v->sname . "</option>\n"; |
| 266 | } |
| 267 | } |
| 268 | $ret .= "</select>"; |
| 269 | return html_tag( 'tr', |
| 270 | html_tag( 'td', _("Add to:"),'right', $color[4] ) . |
| 271 | html_tag( 'td', $ret, 'left', $color[4] )) . "\n"; |
| 272 | } else { |
| 273 | return html_tag( 'tr', |
| 274 | html_tag( 'td', |
| 275 | addHidden('backend', '1'), |
| 276 | 'center', $color[4], 'colspan="2"')) . "\n"; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Sort array by the key "name" |
| 282 | */ |
| 283 | function alistcmp($a,$b) { |
| 284 | $abook_sort_order=get_abook_sort(); |
| 285 | |
| 286 | switch ($abook_sort_order) { |
| 287 | case 0: |
| 288 | case 1: |
| 289 | $abook_sort='nickname'; |
| 290 | break; |
| 291 | case 4: |
| 292 | case 5: |
| 293 | $abook_sort='email'; |
| 294 | break; |
| 295 | case 6: |
| 296 | case 7: |
| 297 | $abook_sort='label'; |
| 298 | break; |
| 299 | case 2: |
| 300 | case 3: |
| 301 | case 8: |
| 302 | default: |
| 303 | $abook_sort='name'; |
| 304 | } |
| 305 | |
| 306 | if ($a['backend'] > $b['backend']) { |
| 307 | return 1; |
| 308 | } else { |
| 309 | if ($a['backend'] < $b['backend']) { |
| 310 | return -1; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if( (($abook_sort_order+2) % 2) == 1) { |
| 315 | return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1; |
| 316 | } else { |
| 317 | return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Address book sorting options |
| 323 | * |
| 324 | * returns address book sorting order |
| 325 | * @return integer book sorting options order |
| 326 | */ |
| 327 | function get_abook_sort() { |
| 328 | global $data_dir, $username; |
| 329 | |
| 330 | /* get sorting order */ |
| 331 | if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) { |
| 332 | $abook_sort_order = (int) $temp; |
| 333 | |
| 334 | if ($abook_sort_order < 0 or $abook_sort_order > 8) |
| 335 | $abook_sort_order=8; |
| 336 | |
| 337 | setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order); |
| 338 | } else { |
| 339 | /* get previous sorting options. default to unsorted */ |
| 340 | $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8); |
| 341 | } |
| 342 | |
| 343 | return $abook_sort_order; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * This function shows the address book sort button. |
| 348 | * |
| 349 | * @param integer $abook_sort_order current sort value |
| 350 | * @param string $alt_tag alt tag value (string visible to text only browsers) |
| 351 | * @param integer $Down sort value when list is sorted ascending |
| 352 | * @param integer $Up sort value when list is sorted descending |
| 353 | * @return string html code with sorting images and urls |
| 354 | */ |
| 355 | function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) { |
| 356 | global $form_url; |
| 357 | |
| 358 | /* Figure out which image we want to use. */ |
| 359 | if ($abook_sort_order != $Up && $abook_sort_order != $Down) { |
| 360 | $img = 'sort_none.png'; |
| 361 | $which = $Up; |
| 362 | } elseif ($abook_sort_order == $Up) { |
| 363 | $img = 'up_pointer.png'; |
| 364 | $which = $Down; |
| 365 | } else { |
| 366 | $img = 'down_pointer.png'; |
| 367 | $which = 8; |
| 368 | } |
| 369 | |
| 370 | /* Now that we have everything figured out, show the actual button. */ |
| 371 | return ' <a href="' . $form_url .'?abook_sort_order=' . $which |
| 372 | . '"><img src="../images/' . $img |
| 373 | . '" border="0" width="12" height="10" alt="' . $alt_tag . '" title="' |
| 374 | . _("Click here to change the sorting of the address list") .'" /></a>'; |
| 375 | } |
| 376 | |
| 377 | |
| 378 | /** |
| 379 | * This is the main address book class that connect all the |
| 380 | * backends and provide services to the functions above. |
| 381 | * @package squirrelmail |
| 382 | * @subpackage addressbook |
| 383 | */ |
| 384 | class AddressBook { |
| 385 | /** |
| 386 | * Enabled address book backends |
| 387 | * @var array |
| 388 | */ |
| 389 | var $backends = array(); |
| 390 | /** |
| 391 | * Number of enabled backends |
| 392 | * @var integer |
| 393 | */ |
| 394 | var $numbackends = 0; |
| 395 | /** |
| 396 | * Error messages |
| 397 | * @var string |
| 398 | */ |
| 399 | var $error = ''; |
| 400 | /** |
| 401 | * id of backend with personal address book |
| 402 | * @var integer |
| 403 | */ |
| 404 | var $localbackend = 0; |
| 405 | /** |
| 406 | * Name of backend with personal address book |
| 407 | * @var string |
| 408 | */ |
| 409 | var $localbackendname = ''; |
| 410 | |
| 411 | /** |
| 412 | * Constructor function. |
| 413 | */ |
| 414 | function AddressBook() { |
| 415 | $this->localbackendname = _("Personal address book"); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Return an array of backends of a given type, |
| 420 | * or all backends if no type is specified. |
| 421 | * @param string $type backend type |
| 422 | * @return array list of backends |
| 423 | */ |
| 424 | function get_backend_list($type = '') { |
| 425 | $ret = array(); |
| 426 | for ($i = 1 ; $i <= $this->numbackends ; $i++) { |
| 427 | if (empty($type) || $type == $this->backends[$i]->btype) { |
| 428 | $ret[] = &$this->backends[$i]; |
| 429 | } |
| 430 | } |
| 431 | return $ret; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | /* ========================== Public ======================== */ |
| 436 | |
| 437 | /** |
| 438 | * Add a new backend. |
| 439 | * |
| 440 | * @param string $backend backend name (without the abook_ prefix) |
| 441 | * @param mixed optional variable that is passed to the backend constructor. |
| 442 | * See each of the backend classes for valid parameters |
| 443 | * @return integer number of backends |
| 444 | */ |
| 445 | function add_backend($backend, $param = '') { |
| 446 | $backend_name = 'abook_' . $backend; |
| 447 | eval('$newback = new ' . $backend_name . '($param);'); |
| 448 | if(!empty($newback->error)) { |
| 449 | $this->error = $newback->error; |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | $this->numbackends++; |
| 454 | |
| 455 | $newback->bnum = $this->numbackends; |
| 456 | $this->backends[$this->numbackends] = $newback; |
| 457 | |
| 458 | /* Store ID of first local backend added */ |
| 459 | if ($this->localbackend == 0 && $newback->btype == 'local') { |
| 460 | $this->localbackend = $this->numbackends; |
| 461 | $this->localbackendname = $newback->sname; |
| 462 | } |
| 463 | |
| 464 | return $this->numbackends; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | /** |
| 469 | * create string with name and email address |
| 470 | * |
| 471 | * This function takes a $row array as returned by the addressbook |
| 472 | * search and returns an e-mail address with the full name or |
| 473 | * nickname optionally prepended. |
| 474 | * @param array $row address book entry |
| 475 | * @return string email address with real name prepended |
| 476 | */ |
| 477 | function full_address($row) { |
| 478 | global $addrsrch_fullname, $data_dir, $username; |
| 479 | $prefix = getPref($data_dir, $username, 'addrsrch_fullname'); |
| 480 | if (($prefix != "" || (isset($addrsrch_fullname) && |
| 481 | $prefix == $addrsrch_fullname)) && $prefix != 'noprefix') { |
| 482 | $name = ($prefix == 'nickname' ? $row['nickname'] : $row['name']); |
| 483 | return $name . ' <' . trim($row['email']) . '>'; |
| 484 | } else { |
| 485 | return trim($row['email']); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Search for entries in address books |
| 491 | * |
| 492 | * Return a list of addresses matching expression in |
| 493 | * all backends of a given type. |
| 494 | * @param string $expression search expression |
| 495 | * @param integer $bnum backend number. default to search in all backends |
| 496 | * @return array search results |
| 497 | */ |
| 498 | function search($expression, $bnum = -1) { |
| 499 | $ret = array(); |
| 500 | $this->error = ''; |
| 501 | |
| 502 | /* Search all backends */ |
| 503 | if ($bnum == -1) { |
| 504 | $sel = $this->get_backend_list(''); |
| 505 | $failed = 0; |
| 506 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
| 507 | $backend = &$sel[$i]; |
| 508 | $backend->error = ''; |
| 509 | $res = $backend->search($expression); |
| 510 | if (is_array($res)) { |
| 511 | $ret = array_merge($ret, $res); |
| 512 | } else { |
| 513 | $this->error .= "<br />\n" . $backend->error; |
| 514 | $failed++; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | /* Only fail if all backends failed */ |
| 519 | if( $failed >= sizeof( $sel ) ) { |
| 520 | $ret = FALSE; |
| 521 | } |
| 522 | |
| 523 | } else { |
| 524 | |
| 525 | /* Search only one backend */ |
| 526 | |
| 527 | $ret = $this->backends[$bnum]->search($expression); |
| 528 | if (!is_array($ret)) { |
| 529 | $this->error .= "<br />\n" . $this->backends[$bnum]->error; |
| 530 | $ret = FALSE; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return( $ret ); |
| 535 | } |
| 536 | |
| 537 | |
| 538 | /** |
| 539 | * Sorted search |
| 540 | * @param string $expression search expression |
| 541 | * @param integer $bnum backend number. default to search in all backends |
| 542 | * @return array search results |
| 543 | */ |
| 544 | function s_search($expression, $bnum = -1) { |
| 545 | |
| 546 | $ret = $this->search($expression, $bnum); |
| 547 | if ( is_array( $ret ) ) { |
| 548 | usort($ret, 'addressbook_cmp'); |
| 549 | } |
| 550 | return $ret; |
| 551 | } |
| 552 | |
| 553 | |
| 554 | /** |
| 555 | * Lookup an address by alias. |
| 556 | * Only possible in local backends. |
| 557 | * @param string $alias |
| 558 | * @param integer backend number |
| 559 | * @return array lookup results. False, if not found. |
| 560 | */ |
| 561 | function lookup($alias, $bnum = -1) { |
| 562 | |
| 563 | $ret = array(); |
| 564 | |
| 565 | if ($bnum > -1) { |
| 566 | $res = $this->backends[$bnum]->lookup($alias); |
| 567 | if (is_array($res)) { |
| 568 | return $res; |
| 569 | } else { |
| 570 | $this->error = $backend->error; |
| 571 | return false; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | $sel = $this->get_backend_list('local'); |
| 576 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
| 577 | $backend = &$sel[$i]; |
| 578 | $backend->error = ''; |
| 579 | $res = $backend->lookup($alias); |
| 580 | if (is_array($res)) { |
| 581 | if(!empty($res)) |
| 582 | return $res; |
| 583 | } else { |
| 584 | $this->error = $backend->error; |
| 585 | return false; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | return $ret; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | /** |
| 594 | * Return all addresses |
| 595 | * @param integer $bnum backend number |
| 596 | * @return array search results |
| 597 | */ |
| 598 | function list_addr($bnum = -1) { |
| 599 | $ret = array(); |
| 600 | |
| 601 | if ($bnum == -1) { |
| 602 | $sel = $this->get_backend_list(''); |
| 603 | } else { |
| 604 | $sel = array(0 => &$this->backends[$bnum]); |
| 605 | } |
| 606 | |
| 607 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
| 608 | $backend = &$sel[$i]; |
| 609 | $backend->error = ''; |
| 610 | $res = $backend->list_addr(); |
| 611 | if (is_array($res)) { |
| 612 | $ret = array_merge($ret, $res); |
| 613 | } else { |
| 614 | $this->error = $backend->error; |
| 615 | return false; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | return $ret; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Create a new address |
| 624 | * @param array $userdata added address record |
| 625 | * @param integer $bnum backend number |
| 626 | * @return integer the backend number that the/ address was added |
| 627 | * to, or false if it failed. |
| 628 | */ |
| 629 | function add($userdata, $bnum) { |
| 630 | |
| 631 | /* Validate data */ |
| 632 | if (!is_array($userdata)) { |
| 633 | $this->error = _("Invalid input data"); |
| 634 | return false; |
| 635 | } |
| 636 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
| 637 | $this->error = _("Name is missing"); |
| 638 | return false; |
| 639 | } |
| 640 | if (empty($userdata['email'])) { |
| 641 | $this->error = _("E-mail address is missing"); |
| 642 | return false; |
| 643 | } |
| 644 | if (empty($userdata['nickname'])) { |
| 645 | $userdata['nickname'] = $userdata['email']; |
| 646 | } |
| 647 | |
| 648 | if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) { |
| 649 | $this->error = _("Nickname contains illegal characters"); |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | /* Check that specified backend accept new entries */ |
| 654 | if (!$this->backends[$bnum]->writeable) { |
| 655 | $this->error = _("Addressbook is read-only"); |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | /* Add address to backend */ |
| 660 | $res = $this->backends[$bnum]->add($userdata); |
| 661 | if ($res) { |
| 662 | return $bnum; |
| 663 | } else { |
| 664 | $this->error = $this->backends[$bnum]->error; |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | return false; // Not reached |
| 669 | } /* end of add() */ |
| 670 | |
| 671 | |
| 672 | /** |
| 673 | * Remove the entries from address book |
| 674 | * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames |
| 675 | * @param integer $bnum backend number |
| 676 | * @return bool true if removed successfully. false if there s an error. $this->error contains error message |
| 677 | */ |
| 678 | function remove($alias, $bnum) { |
| 679 | |
| 680 | /* Check input */ |
| 681 | if (empty($alias)) { |
| 682 | return true; |
| 683 | } |
| 684 | |
| 685 | /* Convert string to single element array */ |
| 686 | if (!is_array($alias)) { |
| 687 | $alias = array(0 => $alias); |
| 688 | } |
| 689 | |
| 690 | /* Check that specified backend is writable */ |
| 691 | if (!$this->backends[$bnum]->writeable) { |
| 692 | $this->error = _("Addressbook is read-only"); |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | /* Remove user from backend */ |
| 697 | $res = $this->backends[$bnum]->remove($alias); |
| 698 | if ($res) { |
| 699 | return $bnum; |
| 700 | } else { |
| 701 | $this->error = $this->backends[$bnum]->error; |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | return FALSE; /* Not reached */ |
| 706 | } /* end of remove() */ |
| 707 | |
| 708 | |
| 709 | /** |
| 710 | * Modify entry in address book |
| 711 | * @param string $alias nickname |
| 712 | * @param array $userdata newdata |
| 713 | * @param integer $bnum backend number |
| 714 | */ |
| 715 | function modify($alias, $userdata, $bnum) { |
| 716 | |
| 717 | /* Check input */ |
| 718 | if (empty($alias) || !is_string($alias)) { |
| 719 | return true; |
| 720 | } |
| 721 | |
| 722 | /* Validate data */ |
| 723 | if(!is_array($userdata)) { |
| 724 | $this->error = _("Invalid input data"); |
| 725 | return false; |
| 726 | } |
| 727 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
| 728 | $this->error = _("Name is missing"); |
| 729 | return false; |
| 730 | } |
| 731 | if (empty($userdata['email'])) { |
| 732 | $this->error = _("E-mail address is missing"); |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) { |
| 737 | $this->error = _("Nickname contains illegal characters"); |
| 738 | return false; |
| 739 | } |
| 740 | |
| 741 | if (empty($userdata['nickname'])) { |
| 742 | $userdata['nickname'] = $userdata['email']; |
| 743 | } |
| 744 | |
| 745 | /* Check that specified backend is writable */ |
| 746 | if (!$this->backends[$bnum]->writeable) { |
| 747 | $this->error = _("Addressbook is read-only");; |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | /* Modify user in backend */ |
| 752 | $res = $this->backends[$bnum]->modify($alias, $userdata); |
| 753 | if ($res) { |
| 754 | return $bnum; |
| 755 | } else { |
| 756 | $this->error = $this->backends[$bnum]->error; |
| 757 | return false; |
| 758 | } |
| 759 | |
| 760 | return FALSE; /* Not reached */ |
| 761 | } /* end of modify() */ |
| 762 | |
| 763 | |
| 764 | } /* End of class Addressbook */ |
| 765 | |
| 766 | /** |
| 767 | * Generic backend that all other backends extend |
| 768 | * @package squirrelmail |
| 769 | * @subpackage addressbook |
| 770 | */ |
| 771 | class addressbook_backend { |
| 772 | |
| 773 | /* Variables that all backends must provide. */ |
| 774 | /** |
| 775 | * Backend type |
| 776 | * |
| 777 | * Can be 'local' or 'remote' |
| 778 | * @var string backend type |
| 779 | */ |
| 780 | var $btype = 'dummy'; |
| 781 | /** |
| 782 | * Internal backend name |
| 783 | * @var string |
| 784 | */ |
| 785 | var $bname = 'dummy'; |
| 786 | /** |
| 787 | * Displayed backend name |
| 788 | * @var string |
| 789 | */ |
| 790 | var $sname = 'Dummy backend'; |
| 791 | |
| 792 | /* |
| 793 | * Variables common for all backends, but that |
| 794 | * should not be changed by the backends. |
| 795 | */ |
| 796 | /** |
| 797 | * Backend number |
| 798 | * @var integer |
| 799 | */ |
| 800 | var $bnum = -1; |
| 801 | /** |
| 802 | * Error messages |
| 803 | * @var string |
| 804 | */ |
| 805 | var $error = ''; |
| 806 | /** |
| 807 | * Writeable flag |
| 808 | * @var bool |
| 809 | */ |
| 810 | var $writeable = false; |
| 811 | |
| 812 | /** |
| 813 | * Set error message |
| 814 | * @param string $string error message |
| 815 | * @return bool |
| 816 | */ |
| 817 | function set_error($string) { |
| 818 | $this->error = '[' . $this->sname . '] ' . $string; |
| 819 | return false; |
| 820 | } |
| 821 | |
| 822 | |
| 823 | /* ========================== Public ======================== */ |
| 824 | |
| 825 | /** |
| 826 | * Search for entries in backend |
| 827 | * @param string $expression |
| 828 | * @return bool |
| 829 | */ |
| 830 | function search($expression) { |
| 831 | $this->set_error('search not implemented'); |
| 832 | return false; |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * Find entry in backend by alias |
| 837 | * @param string $alias name used for id |
| 838 | * @return bool |
| 839 | */ |
| 840 | function lookup($alias) { |
| 841 | $this->set_error('lookup not implemented'); |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * List all entries in backend |
| 847 | * @return bool |
| 848 | */ |
| 849 | function list_addr() { |
| 850 | $this->set_error('list_addr not implemented'); |
| 851 | return false; |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * Add entry to backend |
| 856 | * @param array userdata |
| 857 | * @return bool |
| 858 | */ |
| 859 | function add($userdata) { |
| 860 | $this->set_error('add not implemented'); |
| 861 | return false; |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * Remove entry from backend |
| 866 | * @param string $alias name used for id |
| 867 | * @return bool |
| 868 | */ |
| 869 | function remove($alias) { |
| 870 | $this->set_error('delete not implemented'); |
| 871 | return false; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Modify entry in backend |
| 876 | * @param string $alias name used for id |
| 877 | * @param array $newuserdata new data |
| 878 | * @return bool |
| 879 | */ |
| 880 | function modify($alias, $newuserdata) { |
| 881 | $this->set_error('modify not implemented'); |
| 882 | return false; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | PHP 5 requires that the class be made first, which seems rather |
| 888 | logical, and should have been the way it was generated the first time. |
| 889 | */ |
| 890 | |
| 891 | require_once(SM_PATH . 'functions/abook_local_file.php'); |
| 892 | require_once(SM_PATH . 'functions/abook_ldap_server.php'); |
| 893 | |
| 894 | /* Only load database backend if database is configured */ |
| 895 | if((isset($addrbook_dsn) && !empty($addrbook_dsn)) || |
| 896 | (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) { |
| 897 | include_once(SM_PATH . 'functions/abook_database.php'); |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * hook allows adding different address book classes. |
| 902 | * class must follow address book class coding standards. |
| 903 | * |
| 904 | * see addressbook_backend class and functions/abook_*.php files. |
| 905 | */ |
| 906 | do_hook('abook_add_class'); |
| 907 | |
| 908 | ?> |