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