open file rw only if it is writable
[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
30e9932c 15global $addrbook_dsn, $addrbook_global_dsn;
81fa4801 16
d6c32258 17/**
81fa4801 18 Create and initialize an addressbook object.
19 Returns the created object
20*/
21function addressbook_init($showerr = true, $onlylocal = false) {
22 global $data_dir, $username, $ldap_server, $address_book_global_filename;
23 global $addrbook_dsn, $addrbook_table;
4272758c 24 global $abook_global_file, $abook_global_file_writeable;
30e9932c 25 global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing;
81fa4801 26
27 /* Create a new addressbook object */
28 $abook = new AddressBook;
29
30 /*
31 Always add a local backend. We use *either* file-based *or* a
32 database addressbook. If $addrbook_dsn is set, the database
33 backend is used. If not, addressbooks are stores in files.
34 */
35 if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
36 /* Database */
37 if (!isset($addrbook_table) || empty($addrbook_table)) {
38 $addrbook_table = 'address';
39 }
40 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
41 'owner' => $username,
42 'table' => $addrbook_table));
43 if (!$r && $showerr) {
44 echo _("Error initializing addressbook database.");
45 exit;
46 }
47 } else {
48 /* File */
49 $filename = getHashedFile($username, $data_dir, "$username.abook");
50 $r = $abook->add_backend('local_file', Array('filename' => $filename,
51 'create' => true));
52 if(!$r && $showerr) {
53 printf( _("Error opening file %s"), $filename );
54 exit;
55 }
56
57 }
58
59 /* This would be for the global addressbook */
4272758c 60 if (isset($abook_global_file) && isset($abook_global_file_writeable)
91e0dccc 61 && trim($abook_global_file)!=''){
4272758c 62 // Detect place of address book
63 if (! preg_match("/[\/\\\]/",$abook_global_file)) {
e4a468a7 64 /* no path chars, address book stored in data directory
65 * make sure that there is a slash between data directory
66 * and address book file name
67 */
68 $abook_global_filename=$data_dir
69 . ((substr($data_dir, -1) != '/') ? '/' : '')
70 . $abook_global_file;
4272758c 71 } elseif (preg_match("/^\/|\w:/",$abook_global_file)) {
72 // full path is set in options (starts with slash or x:)
73 $abook_global_filename=$abook_global_file;
74 } else {
75 $abook_global_filename=SM_PATH . $abook_global_file;
76 }
77 $r = $abook->add_backend('local_file',array('filename'=>$abook_global_filename,
78 'name' => _("Global address book"),
79 'detect_writeable' => false,
80 'writeable'=> $abook_global_file_writeable));
81fa4801 81 if (!$r && $showerr) {
82 echo _("Error initializing global addressbook.");
83 exit;
84 }
85 }
86
30e9932c 87 /* Load global addressbook from SQL if configured */
88 if (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) {
89 /* Database configured */
90 if (!isset($addrbook_global_table) || empty($addrbook_global_table)) {
c1ac62d4 91 $addrbook_global_table = 'global_abook';
30e9932c 92 }
93 $r = $abook->add_backend('database',
c1ac62d4 94 Array('dsn' => $addrbook_global_dsn,
95 'owner' => 'global',
96 'name' => _("Global address book"),
97 'writeable' => $addrbook_global_writeable,
98 'listing' => $addrbook_global_listing,
99 'table' => $addrbook_global_table));
30e9932c 100 }
101
df788686 102 /*
103 * hook allows to include different address book backends.
104 * plugins should extract $abook and $r from arguments
105 * and use same add_backend commands as above functions.
e4a468a7 106 * @since 1.5.1
df788686 107 */
108 $hookReturn = do_hook('abook_init', $abook, $r);
109 $abook = $hookReturn[1];
110 $r = $hookReturn[2];
62f7daa5 111
81fa4801 112 if ($onlylocal) {
113 return $abook;
114 }
115
116 /* Load configured LDAP servers (if PHP has LDAP support) */
117 if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
118 reset($ldap_server);
119 while (list($undef,$param) = each($ldap_server)) {
120 if (is_array($param)) {
121 $r = $abook->add_backend('ldap_server', $param);
122 if (!$r && $showerr) {
123 printf( '&nbsp;' . _("Error initializing LDAP server %s:") .
6fd95361 124 "<br />\n", $param['host']);
81fa4801 125 echo '&nbsp;' . $abook->error;
126 exit;
127 }
128 }
129 }
130 }
4935919f 131
81fa4801 132 /* Return the initialized object */
133 return $abook;
4935919f 134}
135
c1ac62d4 136/**
137 * Display the "new address" form
138 *
139 * Form is not closed and you must add closing form tag.
140 * @since 1.5.1
141 * @param string $form_url form action url
142 * @param string $name form name
143 * @param string $title form title
144 * @param string $button form button name
145 * @param array $defdata values of form fields
146 */
147function abook_create_form($form_url,$name,$title,$button,$defdata=array()) {
148 global $color;
149 echo addForm($form_url, 'post', 'f_add').
150 html_tag( 'table',
151 html_tag( 'tr',
152 html_tag( 'td', "\n". '<strong>' . $title . '</strong>' . "\n",
153 'center', $color[0]
154 )
155 )
156 , 'center', '', 'width="100%"' ) ."\n";
157 address_form($name, $button, $defdata);
158}
159
4935919f 160
81fa4801 161/*
162 * Had to move this function outside of the Addressbook Class
163 * PHP 4.0.4 Seemed to be having problems with inline functions.
abd74f7d 164 * Note: this can return now since we don't support 4.0.4 anymore.
62f7daa5 165 */
81fa4801 166function addressbook_cmp($a,$b) {
4935919f 167
81fa4801 168 if($a['backend'] > $b['backend']) {
169 return 1;
170 } else if($a['backend'] < $b['backend']) {
171 return -1;
172 }
62f7daa5 173
81fa4801 174 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
4935919f 175
81fa4801 176}
4935919f 177
c1ac62d4 178/**
179 * Make an input field
180 * @param string $label
181 * @param string $field
182 * @param string $name
183 * @param string $size
184 * @param array $values
185 * @param string $add
186 */
187function addressbook_inp_field($label, $field, $name, $size, $values, $add='') {
188 global $color;
189 $value = ( isset($values[$field]) ? $values[$field] : '');
190
191 if (is_array($value)) {
192 $td_str = addSelect($name.'['.$field.']', $value);
193 } else {
194 $td_str = addInput($name.'['.$field.']', $value, $size);
195 }
196 $td_str .= $add ;
197
198 return html_tag( 'tr' ,
199 html_tag( 'td', $label . ':', 'right', $color[4]) .
200 html_tag( 'td', $td_str, 'left', $color[4])
201 )
202 . "\n";
203}
204
205/**
206 * Output form to add and modify address data
207 */
208function address_form($name, $submittext, $values = array()) {
209 global $color, $squirrelmail_language;
210
211 if ($squirrelmail_language == 'ja_JP') {
212 echo html_tag( 'table',
213 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
214 ' <small>' . _("Must be unique") . '</small>') .
215 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
216 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
217 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
218 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
219 list_writable_backends($name) .
220 html_tag( 'tr',
221 html_tag( 'td',
222 addSubmit($submittext, $name.'[SUBMIT]'),
223 'center', $color[4], 'colspan="2"')
224 )
225 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
226 } else {
227 echo html_tag( 'table',
228 addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
229 ' <small>' . _("Must be unique") . '</small>') .
230 addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
231 addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
232 addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
233 addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
234 list_writable_backends($name) .
235 html_tag( 'tr',
236 html_tag( 'td',
237 addSubmit($submittext, $name.'[SUBMIT]') ,
238 'center', $color[4], 'colspan="2"')
239 )
240 , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
241 }
242}
243
6ad2bbe2 244/**
245 * Provides list of writeable backends.
246 * Works only when address is added ($name='addaddr')
247 * @param string $name name of form
248 * @return string html formated backend field (select or hidden)
249 */
c1ac62d4 250function list_writable_backends($name) {
251 global $color, $abook;
252 if ( $name != 'addaddr' ) { return; }
6ad2bbe2 253 $writeable_abook = 1;
c1ac62d4 254 if ( $abook->numbackends > 1 ) {
c1ac62d4 255 $backends = $abook->get_backend_list();
6ad2bbe2 256 $writeable_abooks=array();
c1ac62d4 257 while (list($undef,$v) = each($backends)) {
258 if ($v->writeable) {
6ad2bbe2 259 // add each backend to array
260 $writeable_abooks[$v->bnum]=$v->sname;
261 // save backend number
262 $writeable_abook=$v->bnum;
c1ac62d4 263 }
264 }
6ad2bbe2 265 if (count($writeable_abooks)>1) {
266 // we have more than one writeable backend
267 $ret=addSelect('backend',$writeable_abooks,null,true);
268 return html_tag( 'tr',
269 html_tag( 'td', _("Add to:"),'right', $color[4] ) .
270 html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
271 }
c1ac62d4 272 }
6ad2bbe2 273 // Only one backend exists or is writeable.
274 return html_tag( 'tr',
275 html_tag( 'td',
276 addHidden('backend', $writeable_abook),
277 'center', $color[4], 'colspan="2"')) . "\n";
c1ac62d4 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 /**
91e0dccc 623 * Create a new address
4272758c 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
91e0dccc 674 * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames
4272758c 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.
e4a468a7 905 * @since 1.5.1
df788686 906 */
907do_hook('abook_add_class');
0419106e 908
796f91d9 909?>