adding address book block
[squirrelmail.git] / functions / addressbook.php
CommitLineData
5100704d 1<?php
35586184 2/**
3 * addressbook.php
4 *
82d304a0 5 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
81fa4801 8 * Functions and classes for the addressbook system.
35586184 9 *
a9d318b0 10 * @version $Id$
d6c32258 11 * @package squirrelmail
a9d318b0 12 * @subpackage addressbook
35586184 13 */
14
d6c32258 15/**
81fa4801 16 This is the path to the global site-wide addressbook.
17 It looks and feels just like a user's .abook file
18 If this is in the data directory, use "$data_dir/global.abook"
19 If not, specify the path as though it was accessed from the
20 src/ directory ("../global.abook" -> in main directory)
21
22 If you don't want a global site-wide addressbook, comment these
23 two lines out. (They are disabled by default.)
24
25 The global addressbook is unmodifiable by anyone. You must actually
26 use a shell script or whatnot to modify the contents.
27
91e51743 28 global $data_dir, $address_book_global_filename;
81fa4801 29 $address_book_global_filename = "$data_dir/global.abook";
30
81fa4801 31*/
32
30e9932c 33global $addrbook_dsn, $addrbook_global_dsn;
81fa4801 34
d6c32258 35/**
81fa4801 36 Create and initialize an addressbook object.
37 Returns the created object
38*/
39function addressbook_init($showerr = true, $onlylocal = false) {
40 global $data_dir, $username, $ldap_server, $address_book_global_filename;
41 global $addrbook_dsn, $addrbook_table;
4272758c 42 global $abook_global_file, $abook_global_file_writeable;
30e9932c 43 global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing;
81fa4801 44
45 /* Create a new addressbook object */
46 $abook = new AddressBook;
47
48 /*
49 Always add a local backend. We use *either* file-based *or* a
50 database addressbook. If $addrbook_dsn is set, the database
51 backend is used. If not, addressbooks are stores in files.
52 */
53 if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
54 /* Database */
55 if (!isset($addrbook_table) || empty($addrbook_table)) {
56 $addrbook_table = 'address';
57 }
58 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
59 'owner' => $username,
60 'table' => $addrbook_table));
61 if (!$r && $showerr) {
62 echo _("Error initializing addressbook database.");
63 exit;
64 }
65 } else {
66 /* File */
67 $filename = getHashedFile($username, $data_dir, "$username.abook");
68 $r = $abook->add_backend('local_file', Array('filename' => $filename,
69 'create' => true));
70 if(!$r && $showerr) {
71 printf( _("Error opening file %s"), $filename );
72 exit;
73 }
74
75 }
76
77 /* This would be for the global addressbook */
4272758c 78 if (isset($abook_global_file) && isset($abook_global_file_writeable)
79 && trim($abook_global_file)!=''){
80 // Detect place of address book
81 if (! preg_match("/[\/\\\]/",$abook_global_file)) {
82 // no path chars
83 $abook_global_filename=$data_dir . $abook_global_file;
84 } elseif (preg_match("/^\/|\w:/",$abook_global_file)) {
85 // full path is set in options (starts with slash or x:)
86 $abook_global_filename=$abook_global_file;
87 } else {
88 $abook_global_filename=SM_PATH . $abook_global_file;
89 }
90 $r = $abook->add_backend('local_file',array('filename'=>$abook_global_filename,
91 'name' => _("Global address book"),
92 'detect_writeable' => false,
93 'writeable'=> $abook_global_file_writeable));
81fa4801 94 if (!$r && $showerr) {
95 echo _("Error initializing global addressbook.");
96 exit;
97 }
98 }
99
30e9932c 100 /* Load global addressbook from SQL if configured */
101 if (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) {
102 /* Database configured */
103 if (!isset($addrbook_global_table) || empty($addrbook_global_table)) {
c1ac62d4 104 $addrbook_global_table = 'global_abook';
30e9932c 105 }
106 $r = $abook->add_backend('database',
c1ac62d4 107 Array('dsn' => $addrbook_global_dsn,
108 'owner' => 'global',
109 'name' => _("Global address book"),
110 'writeable' => $addrbook_global_writeable,
111 'listing' => $addrbook_global_listing,
112 'table' => $addrbook_global_table));
30e9932c 113 }
114
df788686 115 /*
116 * hook allows to include different address book backends.
117 * plugins should extract $abook and $r from arguments
118 * and use same add_backend commands as above functions.
119 */
120 $hookReturn = do_hook('abook_init', $abook, $r);
121 $abook = $hookReturn[1];
122 $r = $hookReturn[2];
62f7daa5 123
81fa4801 124 if ($onlylocal) {
125 return $abook;
126 }
127
128 /* Load configured LDAP servers (if PHP has LDAP support) */
129 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
130 reset($ldap_server);
131 while (list($undef,$param) = each($ldap_server)) {
132 if (is_array($param)) {
133 $r = $abook->add_backend('ldap_server', $param);
134 if (!$r && $showerr) {
135 printf( '&nbsp;' . _("Error initializing LDAP server %s:") .
6fd95361 136 "<br />\n", $param['host']);
81fa4801 137 echo '&nbsp;' . $abook->error;
138 exit;
139 }
140 }
141 }
142 }
4935919f 143
81fa4801 144 /* Return the initialized object */
145 return $abook;
4935919f 146}
147
c1ac62d4 148/**
149 * Display the "new address" form
150 *
151 * Form is not closed and you must add closing form tag.
152 * @since 1.5.1
153 * @param string $form_url form action url
154 * @param string $name form name
155 * @param string $title form title
156 * @param string $button form button name
157 * @param array $defdata values of form fields
158 */
159function abook_create_form($form_url,$name,$title,$button,$defdata=array()) {
160 global $color;
161 echo addForm($form_url, 'post', 'f_add').
162 html_tag( 'table',
163 html_tag( 'tr',
164 html_tag( 'td', "\n". '<strong>' . $title . '</strong>' . "\n",
165 'center', $color[0]
166 )
167 )
168 , 'center', '', 'width="100%"' ) ."\n";
169 address_form($name, $button, $defdata);
170}
171
4935919f 172
81fa4801 173/*
174 * Had to move this function outside of the Addressbook Class
175 * PHP 4.0.4 Seemed to be having problems with inline functions.
abd74f7d 176 * Note: this can return now since we don't support 4.0.4 anymore.
62f7daa5 177 */
81fa4801 178function addressbook_cmp($a,$b) {
4935919f 179
81fa4801 180 if($a['backend'] > $b['backend']) {
181 return 1;
182 } else if($a['backend'] < $b['backend']) {
183 return -1;
184 }
62f7daa5 185
81fa4801 186 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
4935919f 187
81fa4801 188}
4935919f 189
c1ac62d4 190/**
191 * Make an input field
192 * @param string $label
193 * @param string $field
194 * @param string $name
195 * @param string $size
196 * @param array $values
197 * @param string $add
198 */
199function addressbook_inp_field($label, $field, $name, $size, $values, $add='') {
200 global $color;
201 $value = ( isset($values[$field]) ? $values[$field] : '');
202
203 if (is_array($value)) {
204 $td_str = addSelect($name.'['.$field.']', $value);
205 } else {
206 $td_str = addInput($name.'['.$field.']', $value, $size);
207 }
208 $td_str .= $add ;
209
210 return html_tag( 'tr' ,
211 html_tag( 'td', $label . ':', 'right', $color[4]) .
212 html_tag( 'td', $td_str, 'left', $color[4])
213 )
214 . "\n";
215}
216
217/**
218 * Output form to add and modify address data
219 */
220function address_form($name, $submittext, $values = array()) {
221 global $color, $squirrelmail_language;
222
223 if ($squirrelmail_language == 'ja_JP') {
224 echo html_tag( 'table',
225 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
226 ' <small>' . _("Must be unique") . '</small>') .
227 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
228 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
229 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
230 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
231 list_writable_backends($name) .
232 html_tag( 'tr',
233 html_tag( 'td',
234 addSubmit($submittext, $name.'[SUBMIT]'),
235 'center', $color[4], 'colspan="2"')
236 )
237 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
238 } else {
239 echo html_tag( 'table',
240 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
241 ' <small>' . _("Must be unique") . '</small>') .
242 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
243 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
244 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
245 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
246 list_writable_backends($name) .
247 html_tag( 'tr',
248 html_tag( 'td',
249 addSubmit($submittext, $name.'[SUBMIT]') ,
250 'center', $color[4], 'colspan="2"')
251 )
252 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
253 }
254}
255
256function list_writable_backends($name) {
257 global $color, $abook;
258 if ( $name != 'addaddr' ) { return; }
259 if ( $abook->numbackends > 1 ) {
260 $ret = '<select name="backend">';
261 $backends = $abook->get_backend_list();
262 while (list($undef,$v) = each($backends)) {
263 if ($v->writeable) {
264 $ret .= '<option value="' . $v->bnum;
265 $ret .= '">' . $v->sname . "</option>\n";
266 }
267 }
268 $ret .= "</select>";
269 return html_tag( 'tr',
270 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
271 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
272 } else {
273 return html_tag( 'tr',
274 html_tag( 'td',
275 addHidden('backend', '1'),
276 'center', $color[4], 'colspan="2"')) . "\n";
277 }
278}
279
280/**
281 * Sort array by the key "name"
282 */
283function alistcmp($a,$b) {
284 $abook_sort_order=get_abook_sort();
285
286 switch ($abook_sort_order) {
287 case 0:
288 case 1:
289 $abook_sort='nickname';
290 break;
291 case 4:
292 case 5:
293 $abook_sort='email';
294 break;
295 case 6:
296 case 7:
297 $abook_sort='label';
298 break;
299 case 2:
300 case 3:
301 case 8:
302 default:
303 $abook_sort='name';
304 }
305
306 if ($a['backend'] > $b['backend']) {
307 return 1;
308 } else {
309 if ($a['backend'] < $b['backend']) {
310 return -1;
311 }
312 }
313
314 if( (($abook_sort_order+2) % 2) == 1) {
315 return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1;
316 } else {
317 return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1;
318 }
319}
320
321/**
322 * Address book sorting options
323 *
324 * returns address book sorting order
325 * @return integer book sorting options order
326 */
327function get_abook_sort() {
328 global $data_dir, $username;
329
330 /* get sorting order */
331 if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) {
332 $abook_sort_order = (int) $temp;
333
334 if ($abook_sort_order < 0 or $abook_sort_order > 8)
335 $abook_sort_order=8;
336
337 setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order);
338 } else {
339 /* get previous sorting options. default to unsorted */
340 $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
341 }
342
343 return $abook_sort_order;
344}
345
346/**
347 * This function shows the address book sort button.
348 *
349 * @param integer $abook_sort_order current sort value
350 * @param string $alt_tag alt tag value (string visible to text only browsers)
351 * @param integer $Down sort value when list is sorted ascending
352 * @param integer $Up sort value when list is sorted descending
353 * @return string html code with sorting images and urls
354 */
355function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) {
356 global $form_url;
357
358 /* Figure out which image we want to use. */
359 if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
360 $img = 'sort_none.png';
361 $which = $Up;
362 } elseif ($abook_sort_order == $Up) {
363 $img = 'up_pointer.png';
364 $which = $Down;
365 } else {
366 $img = 'down_pointer.png';
367 $which = 8;
368 }
369
370 /* Now that we have everything figured out, show the actual button. */
371 return ' <a href="' . $form_url .'?abook_sort_order=' . $which
372 . '"><img src="../images/' . $img
373 . '" border="0" width="12" height="10" alt="' . $alt_tag . '" title="'
374 . _("Click here to change the sorting of the address list") .'" /></a>';
375}
376
4935919f 377
8f6f9ba5 378/**
81fa4801 379 * This is the main address book class that connect all the
380 * backends and provide services to the functions above.
8f6f9ba5 381 * @package squirrelmail
c1ac62d4 382 * @subpackage addressbook
81fa4801 383 */
81fa4801 384class AddressBook {
4272758c 385 /**
386 * Enabled address book backends
387 * @var array
388 */
81fa4801 389 var $backends = array();
4272758c 390 /**
391 * Number of enabled backends
392 * @var integer
393 */
81fa4801 394 var $numbackends = 0;
4272758c 395 /**
396 * Error messages
397 * @var string
398 */
81fa4801 399 var $error = '';
4272758c 400 /**
401 * id of backend with personal address book
402 * @var integer
403 */
81fa4801 404 var $localbackend = 0;
4272758c 405 /**
406 * Name of backend with personal address book
407 * @var string
408 */
81fa4801 409 var $localbackendname = '';
62f7daa5 410
4272758c 411 /**
412 * Constructor function.
413 */
81fa4801 414 function AddressBook() {
c6b8b46c 415 $this->localbackendname = _("Personal address book");
81fa4801 416 }
4935919f 417
4272758c 418 /**
81fa4801 419 * Return an array of backends of a given type,
420 * or all backends if no type is specified.
4272758c 421 * @param string $type backend type
422 * @return array list of backends
81fa4801 423 */
424 function get_backend_list($type = '') {
425 $ret = array();
426 for ($i = 1 ; $i <= $this->numbackends ; $i++) {
427 if (empty($type) || $type == $this->backends[$i]->btype) {
428 $ret[] = &$this->backends[$i];
429 }
4935919f 430 }
81fa4801 431 return $ret;
432 }
4935919f 433
434
4272758c 435 /* ========================== Public ======================== */
81fa4801 436
4272758c 437 /**
438 * Add a new backend.
439 *
440 * @param string $backend backend name (without the abook_ prefix)
441 * @param mixed optional variable that is passed to the backend constructor.
442 * See each of the backend classes for valid parameters
443 * @return integer number of backends
81fa4801 444 */
445 function add_backend($backend, $param = '') {
446 $backend_name = 'abook_' . $backend;
447 eval('$newback = new ' . $backend_name . '($param);');
448 if(!empty($newback->error)) {
449 $this->error = $newback->error;
450 return false;
451 }
452
453 $this->numbackends++;
454
455 $newback->bnum = $this->numbackends;
456 $this->backends[$this->numbackends] = $newback;
62f7daa5 457
81fa4801 458 /* Store ID of first local backend added */
459 if ($this->localbackend == 0 && $newback->btype == 'local') {
460 $this->localbackend = $this->numbackends;
461 $this->localbackendname = $newback->sname;
462 }
463
464 return $this->numbackends;
465 }
4935919f 466
4935919f 467
4272758c 468 /**
469 * create string with name and email address
470 *
62f7daa5 471 * This function takes a $row array as returned by the addressbook
2e542990 472 * search and returns an e-mail address with the full name or
473 * nickname optionally prepended.
4272758c 474 * @param array $row address book entry
475 * @return string email address with real name prepended
2e542990 476 */
2e542990 477 function full_address($row) {
1ba8cd6b 478 global $addrsrch_fullname, $data_dir, $username;
20ad4fdd 479 $prefix = getPref($data_dir, $username, 'addrsrch_fullname');
480 if (($prefix != "" || (isset($addrsrch_fullname) &&
481 $prefix == $addrsrch_fullname)) && $prefix != 'noprefix') {
482 $name = ($prefix == 'nickname' ? $row['nickname'] : $row['name']);
2e542990 483 return $name . ' <' . trim($row['email']) . '>';
484 } else {
485 return trim($row['email']);
486 }
487 }
488
4272758c 489 /**
490 * Search for entries in address books
491 *
492 * Return a list of addresses matching expression in
493 * all backends of a given type.
494 * @param string $expression search expression
495 * @param integer $bnum backend number. default to search in all backends
496 * @return array search results
497 */
81fa4801 498 function search($expression, $bnum = -1) {
499 $ret = array();
500 $this->error = '';
501
502 /* Search all backends */
503 if ($bnum == -1) {
504 $sel = $this->get_backend_list('');
505 $failed = 0;
506 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
507 $backend = &$sel[$i];
508 $backend->error = '';
509 $res = $backend->search($expression);
510 if (is_array($res)) {
511 $ret = array_merge($ret, $res);
512 } else {
6fd95361 513 $this->error .= "<br />\n" . $backend->error;
81fa4801 514 $failed++;
75e19c7f 515 }
516 }
4935919f 517
81fa4801 518 /* Only fail if all backends failed */
519 if( $failed >= sizeof( $sel ) ) {
520 $ret = FALSE;
4935919f 521 }
4935919f 522
81fa4801 523 } else {
4935919f 524
81fa4801 525 /* Search only one backend */
4935919f 526
81fa4801 527 $ret = $this->backends[$bnum]->search($expression);
528 if (!is_array($ret)) {
6fd95361 529 $this->error .= "<br />\n" . $this->backends[$bnum]->error;
81fa4801 530 $ret = FALSE;
531 }
532 }
533
534 return( $ret );
4935919f 535 }
536
537
4272758c 538 /**
539 * Sorted search
540 * @param string $expression search expression
541 * @param integer $bnum backend number. default to search in all backends
542 * @return array search results
543 */
81fa4801 544 function s_search($expression, $bnum = -1) {
62f7daa5 545
81fa4801 546 $ret = $this->search($expression, $bnum);
547 if ( is_array( $ret ) ) {
548 usort($ret, 'addressbook_cmp');
62f7daa5 549 }
81fa4801 550 return $ret;
551 }
4935919f 552
553
4272758c 554 /**
555 * Lookup an address by alias.
556 * Only possible in local backends.
557 * @param string $alias
558 * @param integer backend number
559 * @return array lookup results. False, if not found.
81fa4801 560 */
561 function lookup($alias, $bnum = -1) {
62f7daa5 562
81fa4801 563 $ret = array();
62f7daa5 564
81fa4801 565 if ($bnum > -1) {
566 $res = $this->backends[$bnum]->lookup($alias);
567 if (is_array($res)) {
568 return $res;
569 } else {
570 $this->error = $backend->error;
571 return false;
572 }
573 }
62f7daa5 574
81fa4801 575 $sel = $this->get_backend_list('local');
576 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
577 $backend = &$sel[$i];
578 $backend->error = '';
579 $res = $backend->lookup($alias);
580 if (is_array($res)) {
581 if(!empty($res))
582 return $res;
583 } else {
584 $this->error = $backend->error;
585 return false;
586 }
587 }
62f7daa5 588
81fa4801 589 return $ret;
4935919f 590 }
591
4935919f 592
4272758c 593 /**
594 * Return all addresses
595 * @param integer $bnum backend number
596 * @return array search results
597 */
81fa4801 598 function list_addr($bnum = -1) {
599 $ret = array();
62f7daa5 600
81fa4801 601 if ($bnum == -1) {
4272758c 602 $sel = $this->get_backend_list('');
81fa4801 603 } else {
604 $sel = array(0 => &$this->backends[$bnum]);
605 }
62f7daa5 606
81fa4801 607 for ($i = 0 ; $i < sizeof($sel) ; $i++) {
608 $backend = &$sel[$i];
609 $backend->error = '';
610 $res = $backend->list_addr();
611 if (is_array($res)) {
612 $ret = array_merge($ret, $res);
613 } else {
614 $this->error = $backend->error;
615 return false;
616 }
617 }
62f7daa5 618
81fa4801 619 return $ret;
620 }
4935919f 621
4272758c 622 /**
623 * Create a new address
624 * @param array $userdata added address record
625 * @param integer $bnum backend number
626 * @return integer the backend number that the/ address was added
81fa4801 627 * to, or false if it failed.
628 */
629 function add($userdata, $bnum) {
62f7daa5 630
81fa4801 631 /* Validate data */
632 if (!is_array($userdata)) {
633 $this->error = _("Invalid input data");
634 return false;
635 }
636 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
637 $this->error = _("Name is missing");
638 return false;
639 }
640 if (empty($userdata['email'])) {
641 $this->error = _("E-mail address is missing");
642 return false;
643 }
644 if (empty($userdata['nickname'])) {
645 $userdata['nickname'] = $userdata['email'];
646 }
62f7daa5 647
81fa4801 648 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
649 $this->error = _("Nickname contains illegal characters");
650 return false;
651 }
62f7daa5 652
81fa4801 653 /* Check that specified backend accept new entries */
654 if (!$this->backends[$bnum]->writeable) {
655 $this->error = _("Addressbook is read-only");
656 return false;
657 }
62f7daa5 658
81fa4801 659 /* Add address to backend */
660 $res = $this->backends[$bnum]->add($userdata);
661 if ($res) {
662 return $bnum;
663 } else {
664 $this->error = $this->backends[$bnum]->error;
665 return false;
666 }
62f7daa5 667
81fa4801 668 return false; // Not reached
669 } /* end of add() */
670
671
4272758c 672 /**
673 * Remove the entries from address book
674 * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames
675 * @param integer $bnum backend number
676 * @return bool true if removed successfully. false if there s an error. $this->error contains error message
81fa4801 677 */
678 function remove($alias, $bnum) {
62f7daa5 679
81fa4801 680 /* Check input */
681 if (empty($alias)) {
682 return true;
683 }
62f7daa5 684
81fa4801 685 /* Convert string to single element array */
686 if (!is_array($alias)) {
687 $alias = array(0 => $alias);
688 }
62f7daa5 689
690 /* Check that specified backend is writable */
81fa4801 691 if (!$this->backends[$bnum]->writeable) {
692 $this->error = _("Addressbook is read-only");
693 return false;
694 }
62f7daa5 695
81fa4801 696 /* Remove user from backend */
697 $res = $this->backends[$bnum]->remove($alias);
698 if ($res) {
699 return $bnum;
700 } else {
701 $this->error = $this->backends[$bnum]->error;
702 return false;
703 }
62f7daa5 704
81fa4801 705 return FALSE; /* Not reached */
706 } /* end of remove() */
707
708
4272758c 709 /**
710 * Modify entry in address book
711 * @param string $alias nickname
712 * @param array $userdata newdata
713 * @param integer $bnum backend number
81fa4801 714 */
715 function modify($alias, $userdata, $bnum) {
62f7daa5 716
81fa4801 717 /* Check input */
718 if (empty($alias) || !is_string($alias)) {
719 return true;
720 }
62f7daa5 721
81fa4801 722 /* Validate data */
723 if(!is_array($userdata)) {
724 $this->error = _("Invalid input data");
725 return false;
726 }
727 if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
728 $this->error = _("Name is missing");
729 return false;
730 }
731 if (empty($userdata['email'])) {
732 $this->error = _("E-mail address is missing");
733 return false;
734 }
62f7daa5 735
81fa4801 736 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
737 $this->error = _("Nickname contains illegal characters");
738 return false;
739 }
62f7daa5 740
81fa4801 741 if (empty($userdata['nickname'])) {
742 $userdata['nickname'] = $userdata['email'];
743 }
62f7daa5 744
745 /* Check that specified backend is writable */
81fa4801 746 if (!$this->backends[$bnum]->writeable) {
747 $this->error = _("Addressbook is read-only");;
748 return false;
749 }
62f7daa5 750
81fa4801 751 /* Modify user in backend */
752 $res = $this->backends[$bnum]->modify($alias, $userdata);
753 if ($res) {
754 return $bnum;
755 } else {
756 $this->error = $this->backends[$bnum]->error;
757 return false;
758 }
62f7daa5 759
81fa4801 760 return FALSE; /* Not reached */
761 } /* end of modify() */
62f7daa5 762
763
81fa4801 764} /* End of class Addressbook */
765
8f6f9ba5 766/**
81fa4801 767 * Generic backend that all other backends extend
8f6f9ba5 768 * @package squirrelmail
c1ac62d4 769 * @subpackage addressbook
81fa4801 770 */
771class addressbook_backend {
772
773 /* Variables that all backends must provide. */
4272758c 774 /**
775 * Backend type
776 *
777 * Can be 'local' or 'remote'
778 * @var string backend type
779 */
81fa4801 780 var $btype = 'dummy';
4272758c 781 /**
782 * Internal backend name
783 * @var string
784 */
81fa4801 785 var $bname = 'dummy';
4272758c 786 /**
787 * Displayed backend name
788 * @var string
789 */
81fa4801 790 var $sname = 'Dummy backend';
62f7daa5 791
81fa4801 792 /*
793 * Variables common for all backends, but that
794 * should not be changed by the backends.
795 */
4272758c 796 /**
797 * Backend number
798 * @var integer
799 */
81fa4801 800 var $bnum = -1;
4272758c 801 /**
802 * Error messages
803 * @var string
804 */
81fa4801 805 var $error = '';
4272758c 806 /**
807 * Writeable flag
808 * @var bool
809 */
81fa4801 810 var $writeable = false;
62f7daa5 811
4272758c 812 /**
813 * Set error message
814 * @param string $string error message
815 * @return bool
816 */
81fa4801 817 function set_error($string) {
818 $this->error = '[' . $this->sname . '] ' . $string;
819 return false;
820 }
62f7daa5 821
822
81fa4801 823 /* ========================== Public ======================== */
62f7daa5 824
4272758c 825 /**
826 * Search for entries in backend
827 * @param string $expression
828 * @return bool
829 */
81fa4801 830 function search($expression) {
831 $this->set_error('search not implemented');
832 return false;
833 }
62f7daa5 834
4272758c 835 /**
836 * Find entry in backend by alias
837 * @param string $alias name used for id
838 * @return bool
839 */
81fa4801 840 function lookup($alias) {
841 $this->set_error('lookup not implemented');
842 return false;
a10110a5 843 }
62f7daa5 844
4272758c 845 /**
846 * List all entries in backend
847 * @return bool
848 */
81fa4801 849 function list_addr() {
850 $this->set_error('list_addr not implemented');
851 return false;
852 }
62f7daa5 853
4272758c 854 /**
855 * Add entry to backend
856 * @param array userdata
857 * @return bool
858 */
81fa4801 859 function add($userdata) {
860 $this->set_error('add not implemented');
861 return false;
862 }
62f7daa5 863
4272758c 864 /**
865 * Remove entry from backend
866 * @param string $alias name used for id
867 * @return bool
868 */
81fa4801 869 function remove($alias) {
870 $this->set_error('delete not implemented');
871 return false;
872 }
62f7daa5 873
4272758c 874 /**
875 * Modify entry in backend
876 * @param string $alias name used for id
877 * @param array $newuserdata new data
878 * @return bool
879 */
81fa4801 880 function modify($alias, $newuserdata) {
881 $this->set_error('modify not implemented');
882 return false;
883 }
81fa4801 884}
885
0419106e 886/*
887 PHP 5 requires that the class be made first, which seems rather
888 logical, and should have been the way it was generated the first time.
889*/
890
891require_once(SM_PATH . 'functions/abook_local_file.php');
892require_once(SM_PATH . 'functions/abook_ldap_server.php');
893
0419106e 894/* Only load database backend if database is configured */
62f7daa5 895if((isset($addrbook_dsn) && !empty($addrbook_dsn)) ||
30e9932c 896 (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) {
0419106e 897 include_once(SM_PATH . 'functions/abook_database.php');
898}
899
df788686 900/*
901 * hook allows adding different address book classes.
902 * class must follow address book class coding standards.
903 *
904 * see addressbook_backend class and functions/abook_*.php files.
905 */
906do_hook('abook_add_class');
0419106e 907
796f91d9 908?>