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