| 1 | <?php |
| 2 | /** |
| 3 | * functions/addressbook.php - Functions and classes for the addressbook system |
| 4 | * |
| 5 | * Functions require SM_PATH and support of forms.php functions |
| 6 | * |
| 7 | * @copyright © 1999-2009 The SquirrelMail Project Team |
| 8 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 9 | * @version $Id$ |
| 10 | * @package squirrelmail |
| 11 | * @subpackage addressbook |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * Create and initialize an addressbook object. |
| 16 | * @param boolean $showerr display any address book init errors. html page header |
| 17 | * must be created before calling addressbook_init() with $showerr enabled. |
| 18 | * @param boolean $onlylocal enable only local address book backends. Should |
| 19 | * be used when code does not need access to remote backends. Backends |
| 20 | * that provide read only address books with limited listing options can be |
| 21 | * tagged as remote. |
| 22 | * @return object address book object. |
| 23 | */ |
| 24 | function addressbook_init($showerr = true, $onlylocal = false) { |
| 25 | global $data_dir, $username, $ldap_server, $address_book_global_filename; |
| 26 | global $addrbook_dsn, $addrbook_table; |
| 27 | global $abook_global_file, $abook_global_file_writeable, $abook_global_file_listing; |
| 28 | global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing; |
| 29 | global $abook_file_line_length; |
| 30 | |
| 31 | /* Create a new addressbook object */ |
| 32 | $abook = new AddressBook; |
| 33 | |
| 34 | /* Create empty error message */ |
| 35 | $abook_init_error=''; |
| 36 | |
| 37 | /* |
| 38 | Always add a local backend. We use *either* file-based *or* a |
| 39 | database addressbook. If $addrbook_dsn is set, the database |
| 40 | backend is used. If not, addressbooks are stores in files. |
| 41 | */ |
| 42 | if (isset($addrbook_dsn) && !empty($addrbook_dsn)) { |
| 43 | /* Database */ |
| 44 | if (!isset($addrbook_table) || empty($addrbook_table)) { |
| 45 | $addrbook_table = 'address'; |
| 46 | } |
| 47 | $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn, |
| 48 | 'owner' => $username, |
| 49 | 'table' => $addrbook_table)); |
| 50 | if (!$r && $showerr) { |
| 51 | $abook_init_error.=_("Error initializing address book database.") . "\n" . $abook->error; |
| 52 | } |
| 53 | } else { |
| 54 | /* File */ |
| 55 | $filename = getHashedFile($username, $data_dir, "$username.abook"); |
| 56 | $r = $abook->add_backend('local_file', Array('filename' => $filename, |
| 57 | 'umask' => 0077, |
| 58 | 'line_length' => $abook_file_line_length, |
| 59 | 'create' => true)); |
| 60 | if(!$r && $showerr) { |
| 61 | // no need to use $abook->error, because message explains error. |
| 62 | $abook_init_error.=sprintf( _("Error opening file %s"), $filename ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* Global file based addressbook */ |
| 67 | if (isset($abook_global_file) && |
| 68 | isset($abook_global_file_writeable) && |
| 69 | isset($abook_global_file_listing) && |
| 70 | trim($abook_global_file)!=''){ |
| 71 | |
| 72 | // Detect place of address book |
| 73 | if (! preg_match("/[\/\\\]/",$abook_global_file)) { |
| 74 | /* no path chars, address book stored in data directory |
| 75 | * make sure that there is a slash between data directory |
| 76 | * and address book file name |
| 77 | */ |
| 78 | $abook_global_filename=$data_dir |
| 79 | . ((substr($data_dir, -1) != '/') ? '/' : '') |
| 80 | . $abook_global_file; |
| 81 | } elseif (preg_match("/^\/|\w:/",$abook_global_file)) { |
| 82 | // full path is set in options (starts with slash or x:) |
| 83 | $abook_global_filename=$abook_global_file; |
| 84 | } else { |
| 85 | $abook_global_filename=SM_PATH . $abook_global_file; |
| 86 | } |
| 87 | |
| 88 | $r = $abook->add_backend('local_file',array('filename'=>$abook_global_filename, |
| 89 | 'name' => _("Global Address Book"), |
| 90 | 'detect_writeable' => false, |
| 91 | 'line_length' => $abook_file_line_length, |
| 92 | 'writeable'=> $abook_global_file_writeable, |
| 93 | 'listing' => $abook_global_file_listing)); |
| 94 | |
| 95 | /* global abook init error is not fatal. add error message and continue */ |
| 96 | if (!$r && $showerr) { |
| 97 | if ($abook_init_error!='') $abook_init_error.="\n"; |
| 98 | $abook_init_error.=_("Error initializing global address book.") . "\n" . $abook->error; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* Load global addressbook from SQL if configured */ |
| 103 | if (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) { |
| 104 | /* Database configured */ |
| 105 | if (!isset($addrbook_global_table) || empty($addrbook_global_table)) { |
| 106 | $addrbook_global_table = 'global_abook'; |
| 107 | } |
| 108 | $r = $abook->add_backend('database', |
| 109 | Array('dsn' => $addrbook_global_dsn, |
| 110 | 'owner' => 'global', |
| 111 | 'name' => _("Global Address Book"), |
| 112 | 'writeable' => $addrbook_global_writeable, |
| 113 | 'listing' => $addrbook_global_listing, |
| 114 | 'table' => $addrbook_global_table)); |
| 115 | /* global abook init error is not fatal. add error message and continue */ |
| 116 | if (!$r && $showerr) { |
| 117 | if ($abook_init_error!='') $abook_init_error.="\n"; |
| 118 | $abook_init_error.=_("Error initializing global address book.") . "\n" . $abook->error; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * hook allows to include different address book backends. |
| 124 | * plugins should extract $abook and $r from arguments |
| 125 | * and use same add_backend commands as above functions. |
| 126 | * Since 1.5.2 hook sends third ($onlylocal) argument to address book |
| 127 | * plugins in order to allow detection of local address book init. |
| 128 | * @since 1.5.1 and 1.4.5 |
| 129 | * Since 1.5.2, the plugin arguments are passed inside an array |
| 130 | * and by reference, so plugins hooking in here need to accept arguments |
| 131 | * in an array and change those values as needed instead of returning |
| 132 | * the changed values. |
| 133 | */ |
| 134 | $temp = array(&$abook, &$r, &$onlylocal); |
| 135 | do_hook('abook_init', $temp); |
| 136 | if (!$r && $showerr) { |
| 137 | if ($abook_init_error!='') $abook_init_error.="\n"; |
| 138 | $abook_init_error.=_("Error initializing other address books.") . "\n" . $abook->error; |
| 139 | } |
| 140 | |
| 141 | /* Load configured LDAP servers (if PHP has LDAP support) */ |
| 142 | if (isset($ldap_server) && is_array($ldap_server)) { |
| 143 | reset($ldap_server); |
| 144 | while (list($undef,$param) = each($ldap_server)) { |
| 145 | if (!is_array($param)) |
| 146 | continue; |
| 147 | |
| 148 | /* if onlylocal is true, we only add writeable ldap servers */ |
| 149 | if ($onlylocal && (!isset($param['writeable']) || $param['writeable'] != true)) |
| 150 | continue; |
| 151 | |
| 152 | $r = $abook->add_backend('ldap_server', $param); |
| 153 | if (!$r && $showerr) { |
| 154 | if ($abook_init_error!='') $abook_init_error.="\n"; |
| 155 | $abook_init_error.=sprintf(_("Error initializing LDAP server %s:"), $param['host'])."\n"; |
| 156 | $abook_init_error.= $abook->error; |
| 157 | } |
| 158 | } |
| 159 | } // end of ldap server init |
| 160 | |
| 161 | /** |
| 162 | * display address book init errors. |
| 163 | */ |
| 164 | if ($abook_init_error!='' && $showerr) { |
| 165 | error_box(nl2br(htmlspecialchars($abook_init_error))); |
| 166 | } |
| 167 | |
| 168 | /* Return the initialized object */ |
| 169 | return $abook; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Constructs the "new address" form |
| 174 | * |
| 175 | * NOTE! The form is not closed - the caller |
| 176 | * must add the closing form tag itself. |
| 177 | * |
| 178 | * @since 1.5.1 |
| 179 | * |
| 180 | * @param string $form_url Form action url |
| 181 | * @param string $name Form name |
| 182 | * @param string $title Form title |
| 183 | * @param string $button Form button name |
| 184 | * @param int $backend The current backend being displayed |
| 185 | * @param array $defdata Values of form fields |
| 186 | * |
| 187 | * @return string The desired address form display code |
| 188 | * |
| 189 | */ |
| 190 | function abook_create_form($form_url, $name, $title, $button, |
| 191 | $backend, $defdata=array()) { |
| 192 | |
| 193 | global $oTemplate; |
| 194 | |
| 195 | $output = addForm($form_url, 'post', 'f_add', '', '', array(), TRUE); |
| 196 | |
| 197 | if ($button == _("Update address")) { |
| 198 | $edit = true; |
| 199 | $backends = NULL; |
| 200 | } else { |
| 201 | $edit = false; |
| 202 | $backends = getWritableBackends(); |
| 203 | } |
| 204 | |
| 205 | $fields = array ( |
| 206 | 'nickname' => 'NickName', |
| 207 | 'firstname' => 'FirstName', |
| 208 | 'lastname' => 'LastName', |
| 209 | 'email' => 'Email', |
| 210 | 'label' => 'Info', |
| 211 | ); |
| 212 | $values = array(); |
| 213 | foreach ($fields as $sqm=>$template) { |
| 214 | $values[$template] = isset($defdata[$sqm]) ? $defdata[$sqm] : ''; |
| 215 | } |
| 216 | |
| 217 | $oTemplate->assign('writable_backends', $backends); |
| 218 | $oTemplate->assign('values', $values); |
| 219 | $oTemplate->assign('edit', $edit); |
| 220 | $oTemplate->assign('current_backend', $backend); |
| 221 | |
| 222 | $output .= $oTemplate->fetch('addrbook_addedit.tpl'); |
| 223 | |
| 224 | return $output; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /** |
| 229 | * Had to move this function outside of the Addressbook Class |
| 230 | * PHP 4.0.4 Seemed to be having problems with inline functions. |
| 231 | * Note: this can return now since we don't support 4.0.4 anymore. |
| 232 | */ |
| 233 | function addressbook_cmp($a,$b) { |
| 234 | |
| 235 | if($a['backend'] > $b['backend']) { |
| 236 | return 1; |
| 237 | } else if($a['backend'] < $b['backend']) { |
| 238 | return -1; |
| 239 | } |
| 240 | |
| 241 | return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1; |
| 242 | |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Retrieve a list of writable backends |
| 247 | * @since 1.5.2 |
| 248 | */ |
| 249 | function getWritableBackends () { |
| 250 | global $abook; |
| 251 | |
| 252 | $write = array(); |
| 253 | $backends = $abook->get_backend_list(); |
| 254 | while (list($undef,$v) = each($backends)) { |
| 255 | if ($v->writeable) { |
| 256 | $write[$v->bnum]=$v->sname; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return $write; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Sort array by the key "name" |
| 265 | */ |
| 266 | function alistcmp($a,$b) { |
| 267 | $abook_sort_order=get_abook_sort(); |
| 268 | |
| 269 | switch ($abook_sort_order) { |
| 270 | case 0: |
| 271 | case 1: |
| 272 | $abook_sort='nickname'; |
| 273 | break; |
| 274 | case 4: |
| 275 | case 5: |
| 276 | $abook_sort='email'; |
| 277 | break; |
| 278 | case 6: |
| 279 | case 7: |
| 280 | $abook_sort='label'; |
| 281 | break; |
| 282 | case 2: |
| 283 | case 3: |
| 284 | case 8: |
| 285 | default: |
| 286 | $abook_sort='name'; |
| 287 | } |
| 288 | |
| 289 | if ($a['backend'] > $b['backend']) { |
| 290 | return 1; |
| 291 | } else { |
| 292 | if ($a['backend'] < $b['backend']) { |
| 293 | return -1; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if( (($abook_sort_order+2) % 2) == 1) { |
| 298 | return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1; |
| 299 | } else { |
| 300 | return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Address book sorting options |
| 306 | * |
| 307 | * returns address book sorting order |
| 308 | * @return integer book sorting options order |
| 309 | */ |
| 310 | function get_abook_sort() { |
| 311 | global $data_dir, $username; |
| 312 | |
| 313 | /* get sorting order */ |
| 314 | if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) { |
| 315 | $abook_sort_order = (int) $temp; |
| 316 | |
| 317 | if ($abook_sort_order < 0 or $abook_sort_order > 8) |
| 318 | $abook_sort_order=8; |
| 319 | |
| 320 | setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order); |
| 321 | } else { |
| 322 | /* get previous sorting options. default to unsorted */ |
| 323 | $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8); |
| 324 | } |
| 325 | |
| 326 | return $abook_sort_order; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * This function shows the address book sort button. |
| 331 | * |
| 332 | * @param integer $abook_sort_order Current sort value |
| 333 | * @param string $alt_tag The alt tag value (string |
| 334 | * visible to text only browsers) |
| 335 | * @param integer $Down Sort value when list is sorted |
| 336 | * ascending |
| 337 | * @param integer $Up Sort value when list is sorted |
| 338 | * descending |
| 339 | * @param array $uri_extra Any additional parameters to add |
| 340 | * to the button's link, as an |
| 341 | * associative array of key/value pairs |
| 342 | * (OPTIONAL; default none) |
| 343 | * |
| 344 | * @return string html code with sorting images and urls |
| 345 | * |
| 346 | */ |
| 347 | function show_abook_sort_button($abook_sort_order, $alt_tag, |
| 348 | $Down, $Up, $uri_extra=array() ) { |
| 349 | |
| 350 | global $form_url, $icon_theme_path; |
| 351 | |
| 352 | /* Figure out which image we want to use. */ |
| 353 | if ($abook_sort_order != $Up && $abook_sort_order != $Down) { |
| 354 | $img = 'sort_none.png'; |
| 355 | $text_icon = '◻'; // U+25FB WHITE MEDIUM SQUARE |
| 356 | $which = $Up; |
| 357 | } elseif ($abook_sort_order == $Up) { |
| 358 | $img = 'up_pointer.png'; |
| 359 | $text_icon = '⇧'; // U+21E7 UPWARDS WHITE ARROW |
| 360 | $which = $Down; |
| 361 | } else { |
| 362 | $img = 'down_pointer.png'; |
| 363 | $text_icon = '⇩'; // U+21E9 DOWNWARDS WHITE ARROW |
| 364 | $which = 8; |
| 365 | } |
| 366 | |
| 367 | $uri_extra['abook_sort_order'] = $which; |
| 368 | $uri = set_uri_vars($form_url, $uri_extra, FALSE); |
| 369 | |
| 370 | /* Now that we have everything figured out, show the actual button. */ |
| 371 | return create_hyperlink($uri, |
| 372 | getIcon($icon_theme_path, $img, $text_icon, $alt_tag), |
| 373 | '', '', '', '', '', |
| 374 | array('style' => 'text-decoration:none', |
| 375 | 'title' => $alt_tag), |
| 376 | FALSE); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | /** |
| 381 | * This is the main address book class that connect all the |
| 382 | * backends and provide services to the functions above. |
| 383 | * @package squirrelmail |
| 384 | * @subpackage addressbook |
| 385 | */ |
| 386 | class AddressBook { |
| 387 | /** |
| 388 | * Enabled address book backends |
| 389 | * @var array |
| 390 | */ |
| 391 | var $backends = array(); |
| 392 | /** |
| 393 | * Number of enabled backends |
| 394 | * @var integer |
| 395 | */ |
| 396 | var $numbackends = 0; |
| 397 | /** |
| 398 | * Error messages |
| 399 | * @var string |
| 400 | */ |
| 401 | var $error = ''; |
| 402 | /** |
| 403 | * id of backend with personal address book |
| 404 | * @var integer |
| 405 | */ |
| 406 | var $localbackend = 0; |
| 407 | /** |
| 408 | * Name of backend with personal address book |
| 409 | * @var string |
| 410 | */ |
| 411 | var $localbackendname = ''; |
| 412 | /** |
| 413 | * Controls use of 'extra' field |
| 414 | * |
| 415 | * Extra field can be used to add link to form, which allows |
| 416 | * to modify all fields supported by backend. This is the only field |
| 417 | * that is not sanitized with htmlspecialchars. Backends MUST make |
| 418 | * sure that field data is sanitized and displayed correctly inside |
| 419 | * table cell. Use of html formating in other address book fields is |
| 420 | * not allowed. Backends that don't return 'extra' row in address book |
| 421 | * data should not modify this object property. |
| 422 | * @var boolean |
| 423 | * @since 1.5.1 |
| 424 | */ |
| 425 | var $add_extra_field = false; |
| 426 | |
| 427 | /** |
| 428 | * Constructor function. |
| 429 | */ |
| 430 | function AddressBook() { |
| 431 | $this->localbackendname = _("Personal Address Book"); |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Return an array of backends of a given type, |
| 436 | * or all backends if no type is specified. |
| 437 | * @param string $type backend type |
| 438 | * @return array list of backends |
| 439 | */ |
| 440 | function get_backend_list($type = '') { |
| 441 | $ret = array(); |
| 442 | for ($i = 1 ; $i <= $this->numbackends ; $i++) { |
| 443 | if (empty($type) || $type == $this->backends[$i]->btype) { |
| 444 | $ret[] = &$this->backends[$i]; |
| 445 | } |
| 446 | } |
| 447 | return $ret; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | /* ========================== Public ======================== */ |
| 452 | |
| 453 | /** |
| 454 | * Add a new backend. |
| 455 | * |
| 456 | * @param string $backend backend name (without the abook_ prefix) |
| 457 | * @param mixed optional variable that is passed to the backend constructor. |
| 458 | * See each of the backend classes for valid parameters |
| 459 | * @return integer number of backends |
| 460 | */ |
| 461 | function add_backend($backend, $param = '') { |
| 462 | static $backend_classes; |
| 463 | if (!isset($backend_classes)) { |
| 464 | $backend_classes = array(); |
| 465 | } |
| 466 | if (!isset($backend_classes[$backend])) { |
| 467 | /** |
| 468 | * Support backend provided by plugins. Plugin function must |
| 469 | * return an associative array with as key the backend name ($backend) |
| 470 | * and as value the file including the path containing the backend class. |
| 471 | * i.e.: $aBackend = array('backend_template' => SM_PATH . 'plugins/abook_backend_template/functions.php') |
| 472 | * |
| 473 | * NB: Because the backend files are included from within this function they DO NOT have access to |
| 474 | * vars in the global scope. This function is the global scope for the included backend !!! |
| 475 | */ |
| 476 | global $null; |
| 477 | $aBackend = do_hook('abook_add_class', $null); |
| 478 | if (isset($aBackend) && is_array($aBackend) && isset($aBackend[$backend])) { |
| 479 | require_once($aBackend[$backend]); |
| 480 | } else { |
| 481 | require_once(SM_PATH . 'functions/abook_'.$backend.'.php'); |
| 482 | } |
| 483 | $backend_classes[$backend] = true; |
| 484 | } |
| 485 | $backend_name = 'abook_' . $backend; |
| 486 | $newback = new $backend_name($param); |
| 487 | //eval('$newback = new ' . $backend_name . '($param);'); |
| 488 | if(!empty($newback->error)) { |
| 489 | $this->error = $newback->error; |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | $this->numbackends++; |
| 494 | |
| 495 | $newback->bnum = $this->numbackends; |
| 496 | $this->backends[$this->numbackends] = $newback; |
| 497 | |
| 498 | /* Store ID of first local backend added */ |
| 499 | if ($this->localbackend == 0 && $newback->btype == 'local') { |
| 500 | $this->localbackend = $this->numbackends; |
| 501 | $this->localbackendname = $newback->sname; |
| 502 | } |
| 503 | |
| 504 | return $this->numbackends; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | /** |
| 509 | * create string with name and email address |
| 510 | * |
| 511 | * This function takes a $row array as returned by the addressbook |
| 512 | * search and returns an e-mail address with the full name or |
| 513 | * nickname optionally prepended. |
| 514 | * @param array $row address book entry |
| 515 | * @return string email address with real name prepended |
| 516 | */ |
| 517 | function full_address($row) { |
| 518 | global $data_dir, $username; |
| 519 | $addrsrch_fullname = getPref($data_dir, $username, 'addrsrch_fullname'); |
| 520 | |
| 521 | // allow multiple addresses in one row (poor person's grouping - bah) |
| 522 | // (separate with commas) |
| 523 | // |
| 524 | $return = ''; |
| 525 | $addresses = explode(',', $row['email']); |
| 526 | foreach ($addresses as $address) { |
| 527 | |
| 528 | if (!empty($return)) $return .= ', '; |
| 529 | |
| 530 | if ($addrsrch_fullname == 'fullname') |
| 531 | $return .= '"' . $row['name'] . '" <' . trim($address) . '>'; |
| 532 | else if ($addrsrch_fullname == 'nickname') |
| 533 | $return .= '"' . $row['nickname'] . '" <' . trim($address) . '>'; |
| 534 | else // "noprefix" |
| 535 | $return .= trim($address); |
| 536 | |
| 537 | } |
| 538 | |
| 539 | return $return; |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Search for entries in address books |
| 544 | * |
| 545 | * Return a list of addresses matching expression in |
| 546 | * all backends of a given type. |
| 547 | * @param string $expression search expression |
| 548 | * @param integer $bnum backend number. default to search in all backends |
| 549 | * @return array search results |
| 550 | */ |
| 551 | function search($expression, $bnum = -1) { |
| 552 | $ret = array(); |
| 553 | $this->error = ''; |
| 554 | |
| 555 | /* Search all backends */ |
| 556 | if ($bnum == -1) { |
| 557 | $sel = $this->get_backend_list(''); |
| 558 | $failed = 0; |
| 559 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
| 560 | $backend = &$sel[$i]; |
| 561 | $backend->error = ''; |
| 562 | $res = $backend->search($expression); |
| 563 | if (is_array($res)) { |
| 564 | $ret = array_merge($ret, $res); |
| 565 | } else { |
| 566 | $this->error .= "\n" . $backend->error; |
| 567 | $failed++; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /* Only fail if all backends failed */ |
| 572 | if( $failed >= sizeof( $sel ) ) { |
| 573 | $ret = FALSE; |
| 574 | } |
| 575 | |
| 576 | } elseif (! isset($this->backends[$bnum])) { |
| 577 | /* make sure that backend exists */ |
| 578 | $this->error = _("Unknown address book backend"); |
| 579 | $ret = false; |
| 580 | } else { |
| 581 | |
| 582 | /* Search only one backend */ |
| 583 | |
| 584 | $ret = $this->backends[$bnum]->search($expression); |
| 585 | if (!is_array($ret)) { |
| 586 | $this->error .= "\n" . $this->backends[$bnum]->error; |
| 587 | $ret = FALSE; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | return( $ret ); |
| 592 | } |
| 593 | |
| 594 | |
| 595 | /** |
| 596 | * Sorted search |
| 597 | * @param string $expression search expression |
| 598 | * @param integer $bnum backend number. default to search in all backends |
| 599 | * @return array search results |
| 600 | */ |
| 601 | function s_search($expression, $bnum = -1) { |
| 602 | |
| 603 | $ret = $this->search($expression, $bnum); |
| 604 | if ( is_array( $ret ) ) { |
| 605 | usort($ret, 'addressbook_cmp'); |
| 606 | } |
| 607 | return $ret; |
| 608 | } |
| 609 | |
| 610 | |
| 611 | /** |
| 612 | * Lookup an address by the indicated field. |
| 613 | * |
| 614 | * Only possible in local backends. |
| 615 | * |
| 616 | * @param string $value The value to look up |
| 617 | * @param integer $bnum The number of the backend to |
| 618 | * look within (OPTIONAL; defaults |
| 619 | * to look in all local backends) |
| 620 | * @param integer $field The field to look in, should be one |
| 621 | * of the SM_ABOOK_FIELD_* constants |
| 622 | * defined in include/constants.php |
| 623 | * (OPTIONAL; defaults to nickname field) |
| 624 | * NOTE: uniqueness is only guaranteed |
| 625 | * when the nickname field is used here; |
| 626 | * otherwise, the first matching address |
| 627 | * is returned. |
| 628 | * |
| 629 | * @return mixed Array with lookup results when the value |
| 630 | * was found, an empty array if the value was |
| 631 | * not found, or false if an error occured. |
| 632 | * |
| 633 | */ |
| 634 | function lookup($value, $bnum = -1, $field = SM_ABOOK_FIELD_NICKNAME) { |
| 635 | |
| 636 | $ret = array(); |
| 637 | |
| 638 | if ($bnum > -1) { |
| 639 | if (!isset($this->backends[$bnum])) { |
| 640 | $this->error = _("Unknown address book backend"); |
| 641 | return false; |
| 642 | } |
| 643 | $res = $this->backends[$bnum]->lookup($value, $field); |
| 644 | if (is_array($res)) { |
| 645 | return $res; |
| 646 | } else { |
| 647 | $this->error = $this->backends[$bnum]->error; |
| 648 | return false; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | $sel = $this->get_backend_list('local'); |
| 653 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
| 654 | $backend = &$sel[$i]; |
| 655 | $backend->error = ''; |
| 656 | $res = $backend->lookup($value, $field); |
| 657 | |
| 658 | // return an address if one is found |
| 659 | // (empty array means lookup concluded |
| 660 | // but no result found - in this case, |
| 661 | // proceed to next backend) |
| 662 | // |
| 663 | if (is_array($res)) { |
| 664 | if (!empty($res)) return $res; |
| 665 | } else { |
| 666 | $this->error = $backend->error; |
| 667 | return false; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | return $ret; |
| 672 | } |
| 673 | |
| 674 | |
| 675 | /** |
| 676 | * Return all addresses |
| 677 | * @param integer $bnum backend number |
| 678 | * @return mixed array with search results or boolean false on error. |
| 679 | */ |
| 680 | function list_addr($bnum = -1) { |
| 681 | $ret = array(); |
| 682 | |
| 683 | if ($bnum == -1) { |
| 684 | $sel = $this->get_backend_list(''); |
| 685 | } elseif (! isset($this->backends[$bnum])) { |
| 686 | /* make sure that backend exists */ |
| 687 | $this->error = _("Unknown address book backend"); |
| 688 | $ret = false; |
| 689 | } else { |
| 690 | $sel = array(0 => &$this->backends[$bnum]); |
| 691 | } |
| 692 | |
| 693 | for ($i = 0 ; $i < sizeof($sel) ; $i++) { |
| 694 | $backend = &$sel[$i]; |
| 695 | $backend->error = ''; |
| 696 | $res = $backend->list_addr(); |
| 697 | if (is_array($res)) { |
| 698 | $ret = array_merge($ret, $res); |
| 699 | } else { |
| 700 | $this->error = $backend->error; |
| 701 | return false; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | return $ret; |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Create a new address |
| 710 | * @param array $userdata added address record |
| 711 | * @param integer $bnum backend number |
| 712 | * @return integer the backend number that the/ address was added |
| 713 | * to, or false if it failed. |
| 714 | */ |
| 715 | function add($userdata, $bnum) { |
| 716 | |
| 717 | /* Validate data */ |
| 718 | if (!is_array($userdata)) { |
| 719 | $this->error = _("Invalid input data"); |
| 720 | return false; |
| 721 | } |
| 722 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
| 723 | $this->error = _("Name is missing"); |
| 724 | return false; |
| 725 | } |
| 726 | if (empty($userdata['email'])) { |
| 727 | $this->error = _("E-mail address is missing"); |
| 728 | return false; |
| 729 | } |
| 730 | if (empty($userdata['nickname'])) { |
| 731 | $userdata['nickname'] = $userdata['email']; |
| 732 | } |
| 733 | |
| 734 | /* Blocks use of space, :, |, #, " and ! in nickname */ |
| 735 | if (preg_match('/[ :|#"!]/', $userdata['nickname'])) { |
| 736 | $this->error = _("Nickname contains illegal characters"); |
| 737 | return false; |
| 738 | } |
| 739 | |
| 740 | /* make sure that backend exists */ |
| 741 | if (! isset($this->backends[$bnum])) { |
| 742 | $this->error = _("Unknown address book backend"); |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | /* Check that specified backend accept new entries */ |
| 747 | if (!$this->backends[$bnum]->writeable) { |
| 748 | $this->error = _("Address book is read-only"); |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | /* Add address to backend */ |
| 753 | $res = $this->backends[$bnum]->add($userdata); |
| 754 | if ($res) { |
| 755 | return $bnum; |
| 756 | } else { |
| 757 | $this->error = $this->backends[$bnum]->error; |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | return false; // Not reached |
| 762 | } /* end of add() */ |
| 763 | |
| 764 | |
| 765 | /** |
| 766 | * Remove the entries from address book |
| 767 | * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames |
| 768 | * @param integer $bnum backend number |
| 769 | * @return bool true if removed successfully. false if there s an error. $this->error contains error message |
| 770 | */ |
| 771 | function remove($alias, $bnum) { |
| 772 | |
| 773 | /* Check input */ |
| 774 | if (empty($alias)) { |
| 775 | return true; |
| 776 | } |
| 777 | |
| 778 | /* Convert string to single element array */ |
| 779 | if (!is_array($alias)) { |
| 780 | $alias = array(0 => $alias); |
| 781 | } |
| 782 | |
| 783 | /* make sure that backend exists */ |
| 784 | if (! isset($this->backends[$bnum])) { |
| 785 | $this->error = _("Unknown address book backend"); |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | /* Check that specified backend is writable */ |
| 790 | if (!$this->backends[$bnum]->writeable) { |
| 791 | $this->error = _("Address book is read-only"); |
| 792 | return false; |
| 793 | } |
| 794 | |
| 795 | /* Remove user from backend */ |
| 796 | $res = $this->backends[$bnum]->remove($alias); |
| 797 | if ($res) { |
| 798 | return $bnum; |
| 799 | } else { |
| 800 | $this->error = $this->backends[$bnum]->error; |
| 801 | return false; |
| 802 | } |
| 803 | |
| 804 | return FALSE; /* Not reached */ |
| 805 | } /* end of remove() */ |
| 806 | |
| 807 | |
| 808 | /** |
| 809 | * Modify entry in address book |
| 810 | * @param string $alias nickname |
| 811 | * @param array $userdata newdata |
| 812 | * @param integer $bnum backend number |
| 813 | */ |
| 814 | function modify($alias, $userdata, $bnum) { |
| 815 | |
| 816 | /* Check input */ |
| 817 | if (empty($alias) || !is_string($alias)) { |
| 818 | return true; |
| 819 | } |
| 820 | |
| 821 | /* Validate data */ |
| 822 | if(!is_array($userdata)) { |
| 823 | $this->error = _("Invalid input data"); |
| 824 | return false; |
| 825 | } |
| 826 | if (empty($userdata['firstname']) && empty($userdata['lastname'])) { |
| 827 | $this->error = _("Name is missing"); |
| 828 | return false; |
| 829 | } |
| 830 | if (empty($userdata['email'])) { |
| 831 | $this->error = _("E-mail address is missing"); |
| 832 | return false; |
| 833 | } |
| 834 | |
| 835 | if (preg_match('/[: |#"!]/', $userdata['nickname'])) { |
| 836 | $this->error = _("Nickname contains illegal characters"); |
| 837 | return false; |
| 838 | } |
| 839 | |
| 840 | if (empty($userdata['nickname'])) { |
| 841 | $userdata['nickname'] = $userdata['email']; |
| 842 | } |
| 843 | |
| 844 | /* make sure that backend exists */ |
| 845 | if (! isset($this->backends[$bnum])) { |
| 846 | $this->error = _("Unknown address book backend"); |
| 847 | return false; |
| 848 | } |
| 849 | |
| 850 | /* Check that specified backend is writable */ |
| 851 | if (!$this->backends[$bnum]->writeable) { |
| 852 | $this->error = _("Address book is read-only");; |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | /* Modify user in backend */ |
| 857 | $res = $this->backends[$bnum]->modify($alias, $userdata); |
| 858 | if ($res) { |
| 859 | return $bnum; |
| 860 | } else { |
| 861 | $this->error = $this->backends[$bnum]->error; |
| 862 | return false; |
| 863 | } |
| 864 | |
| 865 | return FALSE; /* Not reached */ |
| 866 | } /* end of modify() */ |
| 867 | |
| 868 | |
| 869 | } /* End of class Addressbook */ |
| 870 | |
| 871 | /** |
| 872 | * Generic backend that all other backends extend |
| 873 | * @package squirrelmail |
| 874 | * @subpackage addressbook |
| 875 | */ |
| 876 | class addressbook_backend { |
| 877 | |
| 878 | /* Variables that all backends must provide. */ |
| 879 | /** |
| 880 | * Backend type |
| 881 | * |
| 882 | * Can be 'local' or 'remote' |
| 883 | * @var string backend type |
| 884 | */ |
| 885 | var $btype = 'dummy'; |
| 886 | /** |
| 887 | * Internal backend name |
| 888 | * @var string |
| 889 | */ |
| 890 | var $bname = 'dummy'; |
| 891 | /** |
| 892 | * Displayed backend name |
| 893 | * @var string |
| 894 | */ |
| 895 | var $sname = 'Dummy backend'; |
| 896 | |
| 897 | /* |
| 898 | * Variables common for all backends, but that |
| 899 | * should not be changed by the backends. |
| 900 | */ |
| 901 | /** |
| 902 | * Backend number |
| 903 | * @var integer |
| 904 | */ |
| 905 | var $bnum = -1; |
| 906 | /** |
| 907 | * Error messages |
| 908 | * @var string |
| 909 | */ |
| 910 | var $error = ''; |
| 911 | /** |
| 912 | * Writeable flag |
| 913 | * @var bool |
| 914 | */ |
| 915 | var $writeable = false; |
| 916 | |
| 917 | /** |
| 918 | * Set error message |
| 919 | * @param string $string error message |
| 920 | * @return bool |
| 921 | */ |
| 922 | function set_error($string) { |
| 923 | $this->error = '[' . $this->sname . '] ' . $string; |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | |
| 928 | /* ========================== Public ======================== */ |
| 929 | |
| 930 | /** |
| 931 | * Search for entries in backend |
| 932 | * |
| 933 | * Working backend should support use of wildcards. * symbol |
| 934 | * should match one or more symbols. ? symbol should match any |
| 935 | * single symbol. |
| 936 | * @param string $expression |
| 937 | * @return bool |
| 938 | */ |
| 939 | function search($expression) { |
| 940 | $this->set_error('search is not implemented'); |
| 941 | return false; |
| 942 | } |
| 943 | |
| 944 | /** |
| 945 | * Find entry in backend by the indicated field |
| 946 | * |
| 947 | * @param string $value The value to look up |
| 948 | * @param integer $field The field to look in, should be one |
| 949 | * of the SM_ABOOK_FIELD_* constants |
| 950 | * defined in include/constants.php |
| 951 | * NOTE: uniqueness is only guaranteed |
| 952 | * when the nickname field is used here; |
| 953 | * otherwise, the first matching address |
| 954 | * is returned. |
| 955 | * |
| 956 | * @return mixed Array with lookup results when the value |
| 957 | * was found, an empty array if the value was |
| 958 | * not found, or false if an error occured. |
| 959 | * |
| 960 | */ |
| 961 | function lookup($value, $field) { |
| 962 | $this->set_error('lookup is not implemented'); |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * List all entries in backend |
| 968 | * |
| 969 | * Working backend should provide this function or at least |
| 970 | * dummy function that returns empty array. |
| 971 | * @return bool |
| 972 | */ |
| 973 | function list_addr() { |
| 974 | $this->set_error('list_addr is not implemented'); |
| 975 | return false; |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * Add entry to backend |
| 980 | * @param array userdata |
| 981 | * @return bool |
| 982 | */ |
| 983 | function add($userdata) { |
| 984 | $this->set_error('add is not implemented'); |
| 985 | return false; |
| 986 | } |
| 987 | |
| 988 | /** |
| 989 | * Remove entry from backend |
| 990 | * @param string $alias name used for id |
| 991 | * @return bool |
| 992 | */ |
| 993 | function remove($alias) { |
| 994 | $this->set_error('delete is not implemented'); |
| 995 | return false; |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * Modify entry in backend |
| 1000 | * @param string $alias name used for id |
| 1001 | * @param array $newuserdata new data |
| 1002 | * @return bool |
| 1003 | */ |
| 1004 | function modify($alias, $newuserdata) { |
| 1005 | $this->set_error('modify is not implemented'); |
| 1006 | return false; |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * Creates full name from given name and surname |
| 1011 | * |
| 1012 | * Handles name order differences. Function always runs in SquirrelMail gettext domain. |
| 1013 | * Plugins don't have to switch domains before calling this function. |
| 1014 | * @param string $firstname given name |
| 1015 | * @param string $lastname surname |
| 1016 | * @return string full name |
| 1017 | * @since 1.5.2 |
| 1018 | */ |
| 1019 | function fullname($firstname,$lastname) { |
| 1020 | // i18n: allows to control fullname layout in address book listing |
| 1021 | // first %s is for first name, second %s is for last name. |
| 1022 | // Translate it to '%2$s %1$s', if surname must be displayed first in your language. |
| 1023 | // Please note that variables can be set to empty string and extra formating |
| 1024 | // (for example '%2$s, %1$s' as in 'Smith, John') might break. Use it only for |
| 1025 | // setting name and surname order. scripts will remove all prepended and appended |
| 1026 | // whitespace. |
| 1027 | return trim(sprintf(dgettext('squirrelmail',"%s %s"),$firstname,$lastname)); |
| 1028 | } |
| 1029 | } |