check if object is returned.
[squirrelmail.git] / functions / addressbook.php
CommitLineData
5100704d 1<?php
35586184 2/**
6ad2bbe2 3 * functions/addressbook.php - Functions and classes for the addressbook system
35586184 4 *
6c84ba1e 5 * Copyright (c) 1999-2005 The SquirrelMail Project Team
35586184 6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
6ad2bbe2 8 * Functions require SM_PATH and support of forms.php functions
35586184 9 *
a9d318b0 10 * @version $Id$
d6c32258 11 * @package squirrelmail
a9d318b0 12 * @subpackage addressbook
35586184 13 */
14
04875ae0 15/**
16 * If SM_PATH isn't defined, define it. Required to include files.
17 * @ignore
18 */
19if (!defined('SM_PATH')) {
20 define('SM_PATH','../');
21}
22
23/* make sure that display_messages.php is loaded */
24include_once(SM_PATH . 'functions/display_messages.php');
25
30e9932c 26global $addrbook_dsn, $addrbook_global_dsn;
81fa4801 27
d6c32258 28/**
0d4096aa 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 */
81fa4801 35function addressbook_init($showerr = true, $onlylocal = false) {
04875ae0 36 global $data_dir, $username, $color, $ldap_server, $address_book_global_filename;
81fa4801 37 global $addrbook_dsn, $addrbook_table;
4272758c 38 global $abook_global_file, $abook_global_file_writeable;
30e9932c 39 global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing;
81fa4801 40
41 /* Create a new addressbook object */
42 $abook = new AddressBook;
43
04875ae0 44 /* Create empty error message */
45 $abook_init_error='';
46
81fa4801 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) {
0d4096aa 61 $abook_init_error.=_("Error initializing addressbook database.") . "<br />\n" . $abook->error;
81fa4801 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) {
0d4096aa 69 // no need to use $abook->error, because message explains error.
70 $abook_init_error.=sprintf( _("Error opening file %s"), $filename );
81fa4801 71 }
72
73 }
74
75 /* This would be for the global addressbook */
4272758c 76 if (isset($abook_global_file) && isset($abook_global_file_writeable)
91e0dccc 77 && trim($abook_global_file)!=''){
4272758c 78 // Detect place of address book
79 if (! preg_match("/[\/\\\]/",$abook_global_file)) {
e4a468a7 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;
4272758c 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));
04875ae0 97
98 /* global abook init error is not fatal. add error message and continue */
81fa4801 99 if (!$r && $showerr) {
0d4096aa 100 if ($abook_init_error!='') $abook_init_error.="<br />\n";
101 $abook_init_error.=_("Error initializing global addressbook.") . "<br />\n" . $abook->error;
81fa4801 102 }
103 }
104
30e9932c 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)) {
c1ac62d4 109 $addrbook_global_table = 'global_abook';
30e9932c 110 }
111 $r = $abook->add_backend('database',
c1ac62d4 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));
0d4096aa 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 }
30e9932c 123 }
124
df788686 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.
e4a468a7 129 * @since 1.5.1
df788686 130 */
131 $hookReturn = do_hook('abook_init', $abook, $r);
132 $abook = $hookReturn[1];
133 $r = $hookReturn[2];
62f7daa5 134
0d4096aa 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 }
81fa4801 148 }
149 }
0d4096aa 150 } // end of ldap server init
151 } // end of remote abook backend init
4935919f 152
04875ae0 153 /**
154 * display address book init errors.
155 */
156 if ($abook_init_error!='' && $showerr) {
157 error_box($abook_init_error,$color);
158 }
81fa4801 159 /* Return the initialized object */
160 return $abook;
4935919f 161}
162
c1ac62d4 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 */
174function 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 )
81642286 183 , 'center', '', 'width="90%"' ) ."\n";
c1ac62d4 184 address_form($name, $button, $defdata);
185}
186
4935919f 187
81fa4801 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.
abd74f7d 191 * Note: this can return now since we don't support 4.0.4 anymore.
62f7daa5 192 */
81fa4801 193function addressbook_cmp($a,$b) {
4935919f 194
81fa4801 195 if($a['backend'] > $b['backend']) {
196 return 1;
197 } else if($a['backend'] < $b['backend']) {
198 return -1;
199 }
62f7daa5 200
81fa4801 201 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
4935919f 202
81fa4801 203}
4935919f 204
c1ac62d4 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 */
214function 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 */
235function 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
6ad2bbe2 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 */
c1ac62d4 277function list_writable_backends($name) {
278 global $color, $abook;
279 if ( $name != 'addaddr' ) { return; }
6ad2bbe2 280 $writeable_abook = 1;
c1ac62d4 281 if ( $abook->numbackends > 1 ) {
c1ac62d4 282 $backends = $abook->get_backend_list();
6ad2bbe2 283 $writeable_abooks=array();
c1ac62d4 284 while (list($undef,$v) = each($backends)) {
285 if ($v->writeable) {
6ad2bbe2 286 // add each backend to array
287 $writeable_abooks[$v->bnum]=$v->sname;
288 // save backend number
289 $writeable_abook=$v->bnum;
c1ac62d4 290 }
291 }
6ad2bbe2 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 }
c1ac62d4 299 }
6ad2bbe2 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";
c1ac62d4 305}
306
307/**
308 * Sort array by the key "name"
309 */
310function 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 */
354function 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 */
382function 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
4935919f 404
8f6f9ba5 405/**
81fa4801 406 * This is the main address book class that connect all the
407 * backends and provide services to the functions above.
8f6f9ba5 408 * @package squirrelmail
c1ac62d4 409 * @subpackage addressbook
81fa4801 410 */
81fa4801 411class AddressBook {
4272758c 412 /**
413 * Enabled address book backends
414 * @var array
415 */
81fa4801 416 var $backends = array();
4272758c 417 /**
418 * Number of enabled backends
419 * @var integer
420 */
81fa4801 421 var $numbackends = 0;
4272758c 422 /**
423 * Error messages
424 * @var string
425 */
81fa4801 426 var $error = '';
4272758c 427 /**
428 * id of backend with personal address book
429 * @var integer
430 */
81fa4801 431 var $localbackend = 0;
4272758c 432 /**
433 * Name of backend with personal address book
434 * @var string
435 */
81fa4801 436 var $localbackendname = '';
62f7daa5 437
4272758c 438 /**
439 * Constructor function.
440 */
81fa4801 441 function AddressBook() {
c6b8b46c 442 $this->localbackendname = _("Personal address book");
81fa4801 443 }
4935919f 444
4272758c 445 /**
81fa4801 446 * Return an array of backends of a given type,
447 * or all backends if no type is specified.
4272758c 448 * @param string $type backend type
449 * @return array list of backends
81fa4801 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 }
4935919f 457 }
81fa4801 458 return $ret;
459 }
4935919f 460
461
4272758c 462 /* ========================== Public ======================== */
81fa4801 463
4272758c 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
81fa4801 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;
62f7daa5 484
81fa4801 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 }
4935919f 493
4935919f 494
4272758c 495 /**
496 * create string with name and email address
497 *
62f7daa5 498 * This function takes a $row array as returned by the addressbook
2e542990 499 * search and returns an e-mail address with the full name or
500 * nickname optionally prepended.
4272758c 501 * @param array $row address book entry
502 * @return string email address with real name prepended
2e542990 503 */
2e542990 504 function full_address($row) {
1ba8cd6b 505 global $addrsrch_fullname, $data_dir, $username;
20ad4fdd 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']);
2e542990 510 return $name . ' <' . trim($row['email']) . '>';
511 } else {
512 return trim($row['email']);
513 }
514 }
515
4272758c 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 */
81fa4801 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 {
6fd95361 540 $this->error .= "<br />\n" . $backend->error;
81fa4801 541 $failed++;
75e19c7f 542 }
543 }
4935919f 544
81fa4801 545 /* Only fail if all backends failed */
546 if( $failed >= sizeof( $sel ) ) {
547 $ret = FALSE;
4935919f 548 }
4935919f 549
81fa4801 550 } else {
4935919f 551
81fa4801 552 /* Search only one backend */
4935919f 553
81fa4801 554 $ret = $this->backends[$bnum]->search($expression);
555 if (!is_array($ret)) {
6fd95361 556 $this->error .= "<br />\n" . $this->backends[$bnum]->error;
81fa4801 557 $ret = FALSE;
558 }
559 }
560
561 return( $ret );
4935919f 562 }
563
564
4272758c 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 */
81fa4801 571 function s_search($expression, $bnum = -1) {
62f7daa5 572
81fa4801 573 $ret = $this->search($expression, $bnum);
574 if ( is_array( $ret ) ) {
575 usort($ret, 'addressbook_cmp');
62f7daa5 576 }
81fa4801 577 return $ret;
578 }
4935919f 579
580
4272758c 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.
81fa4801 587 */
588 function lookup($alias, $bnum = -1) {
62f7daa5 589
81fa4801 590 $ret = array();
62f7daa5 591
81fa4801 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 }
62f7daa5 601
81fa4801 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 }
62f7daa5 615
81fa4801 616 return $ret;
4935919f 617 }
618
4935919f 619
4272758c 620 /**
621 * Return all addresses
622 * @param integer $bnum backend number
623 * @return array search results
624 */
81fa4801 625 function list_addr($bnum = -1) {
626 $ret = array();
62f7daa5 627
81fa4801 628 if ($bnum == -1) {
4272758c 629 $sel = $this->get_backend_list('');
81fa4801 630 } else {
631 $sel = array(0 => &$this->backends[$bnum]);
632 }
62f7daa5 633
81fa4801 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 }
62f7daa5 645
81fa4801 646 return $ret;
647 }
4935919f 648
4272758c 649 /**
91e0dccc 650 * Create a new address
4272758c 651 * @param array $userdata added address record
652 * @param integer $bnum backend number
653 * @return integer the backend number that the/ address was added
81fa4801 654 * to, or false if it failed.
655 */
656 function add($userdata, $bnum) {
62f7daa5 657
81fa4801 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 }
62f7daa5 674
81fa4801 675 if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
676 $this->error = _("Nickname contains illegal characters");
677 return false;
678 }
62f7daa5 679
81fa4801 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 }
62f7daa5 685
81fa4801 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 }
62f7daa5 694
81fa4801 695 return false; // Not reached
696 } /* end of add() */
697
698
4272758c 699 /**
700 * Remove the entries from address book
91e0dccc 701 * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames
4272758c 702 * @param integer $bnum backend number
703 * @return bool true if removed successfully. false if there s an error. $this->error contains error message
81fa4801 704 */
705 function remove($alias, $bnum) {
62f7daa5 706
81fa4801 707 /* Check input */
708 if (empty($alias)) {
709 return true;
710 }
62f7daa5 711
81fa4801 712 /* Convert string to single element array */
713 if (!is_array($alias)) {
714 $alias = array(0 => $alias);
715 }
62f7daa5 716
717 /* Check that specified backend is writable */
81fa4801 718 if (!$this->backends[$bnum]->writeable) {
719 $this->error = _("Addressbook is read-only");
720 return false;
721 }
62f7daa5 722
81fa4801 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 }
62f7daa5 731
81fa4801 732 return FALSE; /* Not reached */
733 } /* end of remove() */
734
735
4272758c 736 /**
737 * Modify entry in address book
738 * @param string $alias nickname
739 * @param array $userdata newdata
740 * @param integer $bnum backend number
81fa4801 741 */
742 function modify($alias, $userdata, $bnum) {
62f7daa5 743
81fa4801 744 /* Check input */
745 if (empty($alias) || !is_string($alias)) {
746 return true;
747 }
62f7daa5 748
81fa4801 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 }
62f7daa5 762
81fa4801 763 if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
764 $this->error = _("Nickname contains illegal characters");
765 return false;
766 }
62f7daa5 767
81fa4801 768 if (empty($userdata['nickname'])) {
769 $userdata['nickname'] = $userdata['email'];
770 }
62f7daa5 771
772 /* Check that specified backend is writable */
81fa4801 773 if (!$this->backends[$bnum]->writeable) {
774 $this->error = _("Addressbook is read-only");;
775 return false;
776 }
62f7daa5 777
81fa4801 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 }
62f7daa5 786
81fa4801 787 return FALSE; /* Not reached */
788 } /* end of modify() */
62f7daa5 789
790
81fa4801 791} /* End of class Addressbook */
792
8f6f9ba5 793/**
81fa4801 794 * Generic backend that all other backends extend
8f6f9ba5 795 * @package squirrelmail
c1ac62d4 796 * @subpackage addressbook
81fa4801 797 */
798class addressbook_backend {
799
800 /* Variables that all backends must provide. */
4272758c 801 /**
802 * Backend type
803 *
804 * Can be 'local' or 'remote'
805 * @var string backend type
806 */
81fa4801 807 var $btype = 'dummy';
4272758c 808 /**
809 * Internal backend name
810 * @var string
811 */
81fa4801 812 var $bname = 'dummy';
4272758c 813 /**
814 * Displayed backend name
815 * @var string
816 */
81fa4801 817 var $sname = 'Dummy backend';
62f7daa5 818
81fa4801 819 /*
820 * Variables common for all backends, but that
821 * should not be changed by the backends.
822 */
4272758c 823 /**
824 * Backend number
825 * @var integer
826 */
81fa4801 827 var $bnum = -1;
4272758c 828 /**
829 * Error messages
830 * @var string
831 */
81fa4801 832 var $error = '';
4272758c 833 /**
834 * Writeable flag
835 * @var bool
836 */
81fa4801 837 var $writeable = false;
62f7daa5 838
4272758c 839 /**
840 * Set error message
841 * @param string $string error message
842 * @return bool
843 */
81fa4801 844 function set_error($string) {
845 $this->error = '[' . $this->sname . '] ' . $string;
846 return false;
847 }
62f7daa5 848
849
81fa4801 850 /* ========================== Public ======================== */
62f7daa5 851
4272758c 852 /**
853 * Search for entries in backend
854 * @param string $expression
855 * @return bool
856 */
81fa4801 857 function search($expression) {
858 $this->set_error('search not implemented');
859 return false;
860 }
62f7daa5 861
4272758c 862 /**
863 * Find entry in backend by alias
864 * @param string $alias name used for id
865 * @return bool
866 */
81fa4801 867 function lookup($alias) {
868 $this->set_error('lookup not implemented');
869 return false;
a10110a5 870 }
62f7daa5 871
4272758c 872 /**
873 * List all entries in backend
874 * @return bool
875 */
81fa4801 876 function list_addr() {
877 $this->set_error('list_addr not implemented');
878 return false;
879 }
62f7daa5 880
4272758c 881 /**
882 * Add entry to backend
883 * @param array userdata
884 * @return bool
885 */
81fa4801 886 function add($userdata) {
887 $this->set_error('add not implemented');
888 return false;
889 }
62f7daa5 890
4272758c 891 /**
892 * Remove entry from backend
893 * @param string $alias name used for id
894 * @return bool
895 */
81fa4801 896 function remove($alias) {
897 $this->set_error('delete not implemented');
898 return false;
899 }
62f7daa5 900
4272758c 901 /**
902 * Modify entry in backend
903 * @param string $alias name used for id
904 * @param array $newuserdata new data
905 * @return bool
906 */
81fa4801 907 function modify($alias, $newuserdata) {
908 $this->set_error('modify not implemented');
909 return false;
910 }
81fa4801 911}
912
0419106e 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
918require_once(SM_PATH . 'functions/abook_local_file.php');
919require_once(SM_PATH . 'functions/abook_ldap_server.php');
920
0419106e 921/* Only load database backend if database is configured */
62f7daa5 922if((isset($addrbook_dsn) && !empty($addrbook_dsn)) ||
30e9932c 923 (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) {
0419106e 924 include_once(SM_PATH . 'functions/abook_database.php');
925}
926
df788686 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.
e4a468a7 932 * @since 1.5.1
df788686 933 */
934do_hook('abook_add_class');
0419106e 935
796f91d9 936?>