| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * addressbook.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2001 The Squirrelmail Development Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * Functions and classes for the addressbook system. |
| 10 | * |
| 11 | * $Id$ |
| 12 | */ |
| 13 | |
| 14 | /*****************************************************************/ |
| 15 | /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/ |
| 16 | /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/ |
| 17 | /*** + Base level indent should begin at left margin, as ***/ |
| 18 | /*** the require_once below looks. ***/ |
| 19 | /*** + All identation should consist of four space blocks ***/ |
| 20 | /*** + Tab characters are evil. ***/ |
| 21 | /*** + all comments should use "slash-star ... star-slash" ***/ |
| 22 | /*** style -- no pound characters, no slash-slash style ***/ |
| 23 | /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/ |
| 24 | /*** ALWAYS USE { AND } CHARACTERS!!! ***/ |
| 25 | /*** + Please use ' instead of ", when possible. Note " ***/ |
| 26 | /*** should always be used in _( ) function calls. ***/ |
| 27 | /*** Thank you for your help making the SM code more readable. ***/ |
| 28 | /*****************************************************************/ |
| 29 | |
| 30 | // This is the path to the global site-wide addressbook. |
| 31 | // It looks and feels just like a user's .abook file |
| 32 | // If this is in the data directory, use "$data_dir/global.abook" |
| 33 | // If not, specify the path as though it was accessed from the |
| 34 | // src/ directory ("../global.abook" -> in main directory) |
| 35 | // |
| 36 | // If you don't want a global site-wide addressbook, comment these |
| 37 | // two lines out. (They are disabled by default.) |
| 38 | // |
| 39 | // The global addressbook is unmodifiable by anyone. You must actually |
| 40 | // use a shell script or whatnot to modify the contents. |
| 41 | // |
| 42 | //global $data_dir; |
| 43 | //$address_book_global_filename = "$data_dir/global.abook"; |
| 44 | |
| 45 | // Include backends here. |
| 46 | require_once('../functions/abook_local_file.php'); |
| 47 | require_once('../functions/abook_ldap_server.php'); |
| 48 | |
| 49 | // Use this if you wanna have a global address book |
| 50 | if (isset($address_book_global_filename)) |
| 51 | include_once('../functions/abook_global_file.php'); |
| 52 | |
| 53 | // Only load database backend if database is configured |
| 54 | global $addrbook_dsn; |
| 55 | if(isset($addrbook_dsn)) |
| 56 | include_once('../functions/abook_database.php'); |
| 57 | |
| 58 | |
| 59 | // Create and initialize an addressbook object. |
| 60 | // Returns the created object |
| 61 | function addressbook_init($showerr = true, $onlylocal = false) { |
| 62 | global $data_dir, $username, $ldap_server, $address_book_global_filename; |
| 63 | global $addrbook_dsn; |
| 64 | |
| 65 | // Create a new addressbook object |
| 66 | $abook = new AddressBook; |
| 67 | |
| 68 | // Always add a local backend. We use *either* file-based *or* a |
| 69 | // database addressbook. If $addrbook_dsn is set, the database |
| 70 | // backend is used. If not, addressbooks are stores in files. |
| 71 | if(isset($addrbook_dsn) && !empty($addrbook_dsn)) { |
| 72 | // Database |
| 73 | $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn, |
| 74 | 'owner' => $username, |
| 75 | 'table' => 'address')); |
| 76 | if(!$r && $showerr) { |
| 77 | printf(_("Error initializing addressbook database.")); |
| 78 | exit; |
| 79 | } |
| 80 | } else { |
| 81 | // File |
| 82 | $filename = getHashedFile($username, $data_dir, "$username.abook"); |
| 83 | $r = $abook->add_backend('local_file', Array('filename' => $filename, |
| 84 | 'create' => true)); |
| 85 | if(!$r && $showerr) { |
| 86 | printf(_("Error opening file %s"), $filename); |
| 87 | exit; |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | // This would be for the global addressbook |
| 93 | if (isset($address_book_global_filename)) { |
| 94 | $r = $abook->add_backend('global_file'); |
| 95 | if (!$r && $showerr) { |
| 96 | printf(_("Error initializing global addressbook.")); |
| 97 | exit; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if($onlylocal) |
| 102 | return $abook; |
| 103 | |
| 104 | // Load configured LDAP servers (if PHP has LDAP support) |
| 105 | if(isset($ldap_server) && is_array($ldap_server) && |
| 106 | function_exists('ldap_connect')) { |
| 107 | reset($ldap_server); |
| 108 | while(list($undef,$param) = each($ldap_server)) { |
| 109 | if(is_array($param)) { |
| 110 | $r = $abook->add_backend('ldap_server', $param); |
| 111 | if(!$r && $showerr) { |
| 112 | printf(' ' . _("Error initializing LDAP server %s:") . |
| 113 | "<BR>\n", $param['host']); |
| 114 | print(' ' . $abook->error); |
| 115 | exit; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Return the initialized object |
| 122 | return $abook; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | // Had to move this function outside of the Addressbook Class |
| 127 | // PHP 4.0.4 Seemed to be having problems with inline functions. |
| 128 | function addressbook_cmp($a,$b) { |
| 129 | if($a['backend'] > $b['backend']) |
| 130 | return 1; |
| 131 | else if($a['backend'] < $b['backend']) |
| 132 | return -1; |
| 133 | return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | ** This is the main address book class that connect all the |
| 139 | ** backends and provide services to the functions above. |
| 140 | ** |
| 141 | **/ |
| 142 | |
| 143 | class AddressBook { |
| 144 | var $backends = array(); |
| 145 | var $numbackends = 0; |
| 146 | var $error = ''; |
| 147 | var $localbackend = 0; |
| 148 | var $localbackendname = ''; |
| 149 | |
| 150 | // Constructor function. |
| 151 | function AddressBook() { |
| 152 | $localbackendname = _("Personal address book"); |
| 153 | } |
| 154 | |
| 155 | // Return an array of backends of a given type, |
| 156 | // or all backends if no type is specified. |
| 157 | function get_backend_list($type = '') { |
| 158 | $ret = array(); |
| 159 | for($i = 1 ; $i <= $this->numbackends ; $i++) { |
| 160 | if(empty($type) || $type == $this->backends[$i]->btype) { |
| 161 | $ret[] = &$this->backends[$i]; |
| 162 | } |
| 163 | } |
| 164 | return $ret; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | // ========================== Public ======================== |
| 169 | |
| 170 | // Add a new backend. $backend is the name of a backend |
| 171 | // (without the abook_ prefix), and $param is an optional |
| 172 | // mixed variable that is passed to the backend constructor. |
| 173 | // See each of the backend classes for valid parameters. |
| 174 | function add_backend($backend, $param = '') { |
| 175 | $backend_name = 'abook_' . $backend; |
| 176 | eval('$newback = new ' . $backend_name . '($param);'); |
| 177 | if(!empty($newback->error)) { |
| 178 | $this->error = $newback->error; |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | $this->numbackends++; |
| 183 | |
| 184 | $newback->bnum = $this->numbackends; |
| 185 | $this->backends[$this->numbackends] = $newback; |
| 186 | |
| 187 | // Store ID of first local backend added |
| 188 | if($this->localbackend == 0 && $newback->btype == 'local') { |
| 189 | $this->localbackend = $this->numbackends; |
| 190 | $this->localbackendname = $newback->sname; |
| 191 | } |
| 192 | |
| 193 | return $this->numbackends; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | // Return a list of addresses matching expression in |
| 198 | // all backends of a given type. |
| 199 | function search($expression, $bnum = -1) { |
| 200 | $ret = array(); |
| 201 | $this->error = ''; |
| 202 | |
| 203 | // Search all backends |
| 204 | if($bnum == -1) { |
| 205 | $sel = $this->get_backend_list(''); |
| 206 | $failed = 0; |
| 207 | for($i = 0 ; $i < sizeof($sel) ; $i++) { |
| 208 | $backend = &$sel[$i]; |
| 209 | $backend->error = ''; |
| 210 | $res = $backend->search($expression); |
| 211 | if(is_array($res)) { |
| 212 | $ret = array_merge($ret, $res); |
| 213 | } else { |
| 214 | $this->error .= "<br>\n" . $backend->error; |
| 215 | $failed++; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Only fail if all backends failed |
| 220 | if($failed >= sizeof($sel)) |
| 221 | return false; |
| 222 | |
| 223 | } |
| 224 | |
| 225 | // Search only one backend |
| 226 | else { |
| 227 | $ret = $this->backends[$bnum]->search($expression); |
| 228 | if(!is_array($ret)) { |
| 229 | $this->error .= "<br>\n" . $this->backends[$bnum]->error; |
| 230 | return false; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return $ret; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | // Return a sorted search |
| 239 | function s_search($expression, $bnum = -1) { |
| 240 | |
| 241 | $ret = $this->search($expression, $bnum); |
| 242 | if(!is_array($ret)) |
| 243 | return $ret; |
| 244 | usort($ret, 'addressbook_cmp'); |
| 245 | return $ret; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | // Lookup an address by alias. Only possible in |
| 250 | // local backends. |
| 251 | function lookup($alias, $bnum = -1) { |
| 252 | $ret = array(); |
| 253 | |
| 254 | if($bnum > -1) { |
| 255 | $res = $this->backends[$bnum]->lookup($alias); |
| 256 | if(is_array($res)) { |
| 257 | return $res; |
| 258 | } else { |
| 259 | $this->error = $backend->error; |
| 260 | return false; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | $sel = $this->get_backend_list('local'); |
| 265 | for($i = 0 ; $i < sizeof($sel) ; $i++) { |
| 266 | $backend = &$sel[$i]; |
| 267 | $backend->error = ''; |
| 268 | $res = $backend->lookup($alias); |
| 269 | if(is_array($res)) { |
| 270 | if(!empty($res)) |
| 271 | return $res; |
| 272 | } else { |
| 273 | $this->error = $backend->error; |
| 274 | return false; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return $ret; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | // Return all addresses |
| 283 | function list_addr($bnum = -1) { |
| 284 | $ret = array(); |
| 285 | |
| 286 | if($bnum == -1) |
| 287 | $sel = $this->get_backend_list('local'); |
| 288 | else |
| 289 | $sel = array(0 => &$this->backends[$bnum]); |
| 290 | |
| 291 | for($i = 0 ; $i < sizeof($sel) ; $i++) { |
| 292 | $backend = &$sel[$i]; |
| 293 | $backend->error = ''; |
| 294 | $res = $backend->list_addr(); |
| 295 | if(is_array($res)) { |
| 296 | $ret = array_merge($ret, $res); |
| 297 | } else { |
| 298 | $this->error = $backend->error; |
| 299 | return false; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | return $ret; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | // Create a new address from $userdata, in backend $bnum. |
| 308 | // Return the backend number that the/ address was added |
| 309 | // to, or false if it failed. |
| 310 | function add($userdata, $bnum) { |
| 311 | |
| 312 | // Validate data |
| 313 | if(!is_array($userdata)) { |
| 314 | $this->error = _("Invalid input data"); |
| 315 | return false; |
| 316 | } |
| 317 | if(empty($userdata['firstname']) && |
| 318 | empty($userdata['lastname'])) { |
| 319 | $this->error = _("Name is missing"); |
| 320 | return false; |
| 321 | } |
| 322 | if(empty($userdata['email'])) { |
| 323 | $this->error = _("E-mail address is missing"); |
| 324 | return false; |
| 325 | } |
| 326 | if(empty($userdata['nickname'])) { |
| 327 | $userdata['nickname'] = $userdata['email']; |
| 328 | } |
| 329 | |
| 330 | if(eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) { |
| 331 | $this->error = _("Nickname contains illegal characters"); |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | // Check that specified backend accept new entries |
| 336 | if(!$this->backends[$bnum]->writeable) { |
| 337 | $this->error = _("Addressbook is read-only"); |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | // Add address to backend |
| 342 | $res = $this->backends[$bnum]->add($userdata); |
| 343 | if($res) { |
| 344 | return $bnum; |
| 345 | } else { |
| 346 | $this->error = $this->backends[$bnum]->error; |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | return false; // Not reached |
| 351 | } // end of add() |
| 352 | |
| 353 | |
| 354 | // Remove the user identified by $alias from backend $bnum |
| 355 | // If $alias is an array, all users in the array are removed. |
| 356 | function remove($alias, $bnum) { |
| 357 | |
| 358 | // Check input |
| 359 | if(empty($alias)) |
| 360 | return true; |
| 361 | |
| 362 | // Convert string to single element array |
| 363 | if(!is_array($alias)) |
| 364 | $alias = array(0 => $alias); |
| 365 | |
| 366 | // Check that specified backend is writable |
| 367 | if(!$this->backends[$bnum]->writeable) { |
| 368 | $this->error = _("Addressbook is read-only"); |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | // Remove user from backend |
| 373 | $res = $this->backends[$bnum]->remove($alias); |
| 374 | if($res) { |
| 375 | return $bnum; |
| 376 | } else { |
| 377 | $this->error = $this->backends[$bnum]->error; |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | return false; // Not reached |
| 382 | } // end of remove() |
| 383 | |
| 384 | |
| 385 | // Remove the user identified by $alias from backend $bnum |
| 386 | // If $alias is an array, all users in the array are removed. |
| 387 | function modify($alias, $userdata, $bnum) { |
| 388 | |
| 389 | // Check input |
| 390 | if(empty($alias) || !is_string($alias)) |
| 391 | return true; |
| 392 | |
| 393 | // Validate data |
| 394 | if(!is_array($userdata)) { |
| 395 | $this->error = _("Invalid input data"); |
| 396 | return false; |
| 397 | } |
| 398 | if(empty($userdata['firstname']) && |
| 399 | empty($userdata['lastname'])) { |
| 400 | $this->error = _("Name is missing"); |
| 401 | return false; |
| 402 | } |
| 403 | if(empty($userdata['email'])) { |
| 404 | $this->error = _("E-mail address is missing"); |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) { |
| 409 | $this->error = _("Nickname contains illegal characters"); |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | if(empty($userdata['nickname'])) { |
| 414 | $userdata['nickname'] = $userdata['email']; |
| 415 | } |
| 416 | |
| 417 | // Check that specified backend is writable |
| 418 | if(!$this->backends[$bnum]->writeable) { |
| 419 | $this->error = _("Addressbook is read-only");; |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | // Modify user in backend |
| 424 | $res = $this->backends[$bnum]->modify($alias, $userdata); |
| 425 | if($res) { |
| 426 | return $bnum; |
| 427 | } else { |
| 428 | $this->error = $this->backends[$bnum]->error; |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | return false; // Not reached |
| 433 | } // end of modify() |
| 434 | |
| 435 | |
| 436 | } // End of class Addressbook |
| 437 | |
| 438 | /** |
| 439 | ** Generic backend that all other backends extend |
| 440 | **/ |
| 441 | class addressbook_backend { |
| 442 | |
| 443 | // Variables that all backends must provide. |
| 444 | var $btype = 'dummy'; |
| 445 | var $bname = 'dummy'; |
| 446 | var $sname = 'Dummy backend'; |
| 447 | |
| 448 | // Variables common for all backends, but that |
| 449 | // should not be changed by the backends. |
| 450 | var $bnum = -1; |
| 451 | var $error = ''; |
| 452 | var $writeable = false; |
| 453 | |
| 454 | function set_error($string) { |
| 455 | $this->error = '[' . $this->sname . '] ' . $string; |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | |
| 460 | // ========================== Public ======================== |
| 461 | |
| 462 | function search($expression) { |
| 463 | $this->set_error('search not implemented'); |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | function lookup($alias) { |
| 468 | $this->set_error('lookup not implemented'); |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | function list_addr() { |
| 473 | $this->set_error('list_addr not implemented'); |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | function add($userdata) { |
| 478 | $this->set_error('add not implemented'); |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | function remove($alias) { |
| 483 | $this->set_error('delete not implemented'); |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | function modify($alias, $newuserdata) { |
| 488 | $this->set_error('modify not implemented'); |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | } |
| 493 | |
| 494 | ?> |