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