Added (c) stuff and some formatting.
[squirrelmail.git] / functions / addressbook.php
CommitLineData
5100704d 1<?php
2
2ba13803 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 **/
2d367c68 13
5100704d 14
01589f26 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
5100704d 32 // Include backends here.
0fc2aca0 33 require_once('../functions/abook_local_file.php');
34 require_once('../functions/abook_ldap_server.php');
5100704d 35
01589f26 36 // Use this if you wanna have a global address book
37 if (isset($address_book_global_filename))
0fc2aca0 38 include_once('../functions/abook_global_file.php');
01589f26 39
6ff1c690 40 // Only load database backend if database is configured
66dbe1d4 41 global $addrbook_dsn;
6ff1c690 42 if(isset($addrbook_dsn))
0fc2aca0 43 include_once('../functions/abook_database.php');
8f583e40 44
0515b57a 45
5100704d 46 // Create and initialize an addressbook object.
47 // Returns the created object
0d273153 48 function addressbook_init($showerr = true, $onlylocal = false) {
01589f26 49 global $data_dir, $username, $ldap_server, $address_book_global_filename;
6ff1c690 50 global $addrbook_dsn;
5100704d 51
52 // Create a new addressbook object
53 $abook = new AddressBook;
54
6ff1c690 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 }
01589f26 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 }
5100704d 86 }
0d273153 87
88 if($onlylocal)
89 return $abook;
5100704d 90
95a4d303 91 // Load configured LDAP servers (if PHP has LDAP support)
6aff2ff6 92 if(isset($ldap_server) && is_array($ldap_server) &&
93 function_exists('ldap_connect')) {
1ed2b1e0 94 reset($ldap_server);
fb16d219 95 while(list($undef,$param) = each($ldap_server)) {
1ed2b1e0 96 if(is_array($param)) {
b9bfd165 97 $r = $abook->add_backend('ldap_server', $param);
1ed2b1e0 98 if(!$r && $showerr) {
b9bfd165 99 printf('&nbsp;' . _("Error initializing LDAP server %s:") .
100 "<BR>\n", $param['host']);
101 print('&nbsp;' . $abook->error);
1ed2b1e0 102 exit;
103 }
104 }
105 }
799b9070 106 }
5100704d 107
108 // Return the initialized object
109 return $abook;
110 }
111
0515b57a 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.
485599c7 115 function addressbook_cmp($a,$b) {
b9bfd165 116 if($a['backend'] > $b['backend'])
0515b57a 117 return 1;
b9bfd165 118 else if($a['backend'] < $b['backend'])
0515b57a 119 return -1;
b9bfd165 120 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
0515b57a 121 }
5100704d 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 **/
0515b57a 129
5100704d 130 class AddressBook {
131 var $backends = array();
132 var $numbackends = 0;
b9bfd165 133 var $error = '';
ce916cdf 134 var $localbackend = 0;
b9bfd165 135 var $localbackendname = '';
5100704d 136
137 // Constructor function.
138 function AddressBook() {
1ed2b1e0 139 $localbackendname = _("Personal address book");
5100704d 140 }
141
142 // Return an array of backends of a given type,
143 // or all backends if no type is specified.
b9bfd165 144 function get_backend_list($type = '') {
5100704d 145 $ret = array();
146 for($i = 1 ; $i <= $this->numbackends ; $i++) {
147 if(empty($type) || $type == $this->backends[$i]->btype) {
507832e7 148 $ret[] = &$this->backends[$i];
5100704d 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.
b9bfd165 161 function add_backend($backend, $param = '') {
162 $backend_name = 'abook_' . $backend;
485599c7 163 eval('$newback = new ' . $backend_name . '($param);');
5100704d 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;
ce916cdf 173
174 // Store ID of first local backend added
b9bfd165 175 if($this->localbackend == 0 && $newback->btype == 'local') {
ce916cdf 176 $this->localbackend = $this->numbackends;
1ed2b1e0 177 $this->localbackendname = $newback->sname;
178 }
ce916cdf 179
5100704d 180 return $this->numbackends;
181 }
182
183
184 // Return a list of addresses matching expression in
185 // all backends of a given type.
2f73dc15 186 function search($expression, $bnum = -1) {
5100704d 187 $ret = array();
b9bfd165 188 $this->error = '';
5100704d 189
2f73dc15 190 // Search all backends
191 if($bnum == -1) {
b9bfd165 192 $sel = $this->get_backend_list('');
2f73dc15 193 $failed = 0;
194 for($i = 0 ; $i < sizeof($sel) ; $i++) {
195 $backend = &$sel[$i];
b9bfd165 196 $backend->error = '';
2f73dc15 197 $res = $backend->search($expression);
198 if(is_array($res)) {
199 $ret = array_merge($ret, $res);
200 } else {
b9bfd165 201 $this->error .= "<br>\n" . $backend->error;
2f73dc15 202 $failed++;
203 }
5100704d 204 }
2f73dc15 205
206 // Only fail if all backends failed
207 if($failed >= sizeof($sel))
208 return false;
209
5100704d 210 }
211
2f73dc15 212 // Search only one backend
213 else {
214 $ret = $this->backends[$bnum]->search($expression);
215 if(!is_array($ret)) {
b9bfd165 216 $this->error .= "<br>\n" . $this->backends[$bnum]->error;
2f73dc15 217 return false;
218 }
219 }
0d273153 220
5100704d 221 return $ret;
222 }
223
0515b57a 224
5100704d 225 // Return a sorted search
2f73dc15 226 function s_search($expression, $bnum = -1) {
0515b57a 227
228 $ret = $this->search($expression, $bnum);
229 if(!is_array($ret))
230 return $ret;
485599c7 231 usort($ret, 'addressbook_cmp');
0515b57a 232 return $ret;
5100704d 233 }
234
235
236 // Lookup an address by alias. Only possible in
237 // local backends.
1ed2b1e0 238 function lookup($alias, $bnum = -1) {
5100704d 239 $ret = array();
240
1ed2b1e0 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
b9bfd165 251 $sel = $this->get_backend_list('local');
5100704d 252 for($i = 0 ; $i < sizeof($sel) ; $i++) {
253 $backend = &$sel[$i];
b9bfd165 254 $backend->error = '';
5100704d 255 $res = $backend->lookup($alias);
256 if(is_array($res)) {
1ed2b1e0 257 if(!empty($res))
258 return $res;
5100704d 259 } else {
260 $this->error = $backend->error;
261 return false;
262 }
263 }
264
265 return $ret;
266 }
267
268
269 // Return all addresses
2f73dc15 270 function list_addr($bnum = -1) {
5100704d 271 $ret = array();
272
2f73dc15 273 if($bnum == -1)
b9bfd165 274 $sel = $this->get_backend_list('local');
2f73dc15 275 else
276 $sel = array(0 => &$this->backends[$bnum]);
277
5100704d 278 for($i = 0 ; $i < sizeof($sel) ; $i++) {
279 $backend = &$sel[$i];
b9bfd165 280 $backend->error = '';
5100704d 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 }
b9bfd165 304 if(empty($userdata['firstname']) &&
305 empty($userdata['lastname'])) {
5100704d 306 $this->error = _("Name is missing");
307 return false;
308 }
b9bfd165 309 if(empty($userdata['email'])) {
5100704d 310 $this->error = _("E-mail address is missing");
311 return false;
312 }
b9bfd165 313 if(empty($userdata['nickname'])) {
314 $userdata['nickname'] = $userdata['email'];
5100704d 315 }
316
74bd0763 317 if(eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
d4cbb1ef 318 $this->error = _("Nickname contains illegal characters");
1ed2b1e0 319 return false;
320 }
321
5100704d 322 // Check that specified backend accept new entries
323 if(!$this->backends[$bnum]->writeable) {
1e62e50e 324 $this->error = _("Addressbook is read-only");
5100704d 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
1ed2b1e0 338 } // end of add()
5100704d 339
1ed2b1e0 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 }
b9bfd165 385 if(empty($userdata['firstname']) &&
386 empty($userdata['lastname'])) {
1ed2b1e0 387 $this->error = _("Name is missing");
388 return false;
389 }
b9bfd165 390 if(empty($userdata['email'])) {
1ed2b1e0 391 $this->error = _("E-mail address is missing");
392 return false;
393 }
d9fdc8d3 394
485599c7 395 if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
d65734bd 396 $this->error = _("Nickname contains illegal characters");
d9fdc8d3 397 return false;
398 }
399
b9bfd165 400 if(empty($userdata['nickname'])) {
401 $userdata['nickname'] = $userdata['email'];
1ed2b1e0 402 }
403
404 // Check that specified backend is writable
405 if(!$this->backends[$bnum]->writeable) {
d9fdc8d3 406 $this->error = _("Addressbook is read-only");;
1ed2b1e0 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
5100704d 422
0515b57a 423 } // End of class Addressbook
5100704d 424
425 /**
426 ** Generic backend that all other backends extend
427 **/
428 class addressbook_backend {
429
430 // Variables that all backends must provide.
b9bfd165 431 var $btype = 'dummy';
432 var $bname = 'dummy';
433 var $sname = 'Dummy backend';
5100704d 434
435 // Variables common for all backends, but that
436 // should not be changed by the backends.
437 var $bnum = -1;
b9bfd165 438 var $error = '';
5100704d 439 var $writeable = false;
440
441 function set_error($string) {
b9bfd165 442 $this->error = '[' . $this->sname . '] ' . $string;
5100704d 443 return false;
444 }
445
446
447 // ========================== Public ========================
448
449 function search($expression) {
b9bfd165 450 $this->set_error('search not implemented');
5100704d 451 return false;
452 }
453
454 function lookup($alias) {
b9bfd165 455 $this->set_error('lookup not implemented');
5100704d 456 return false;
457 }
458
459 function list_addr() {
b9bfd165 460 $this->set_error('list_addr not implemented');
5100704d 461 return false;
462 }
463
464 function add($userdata) {
b9bfd165 465 $this->set_error('add not implemented');
5100704d 466 return false;
467 }
468
1ed2b1e0 469 function remove($alias) {
b9bfd165 470 $this->set_error('delete not implemented');
1ed2b1e0 471 return false;
472 }
473
474 function modify($alias, $newuserdata) {
b9bfd165 475 $this->set_error('modify not implemented');
1ed2b1e0 476 return false;
477 }
478
5100704d 479 }
480
d4cbb1ef 481?>