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