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