6 * Copyright (c) 1999-2001 The Squirrelmail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Functions and classes for the addressbook system.
14 /*****************************************************************/
15 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
16 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
17 /*** + Base level indent should begin at left margin, as ***/
18 /*** the require_once below looks. ***/
19 /*** + All identation should consist of four space blocks ***/
20 /*** + Tab characters are evil. ***/
21 /*** + all comments should use "slash-star ... star-slash" ***/
22 /*** style -- no pound characters, no slash-slash style ***/
23 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
24 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
25 /*** + Please use ' instead of ", when possible. Note " ***/
26 /*** should always be used in _( ) function calls. ***/
27 /*** Thank you for your help making the SM code more readable. ***/
28 /*****************************************************************/
30 // This is the path to the global site-wide addressbook.
31 // It looks and feels just like a user's .abook file
32 // If this is in the data directory, use "$data_dir/global.abook"
33 // If not, specify the path as though it was accessed from the
34 // src/ directory ("../global.abook" -> in main directory)
36 // If you don't want a global site-wide addressbook, comment these
37 // two lines out. (They are disabled by default.)
39 // The global addressbook is unmodifiable by anyone. You must actually
40 // use a shell script or whatnot to modify the contents.
43 //$address_book_global_filename = "$data_dir/global.abook";
45 // Include backends here.
46 require_once('../functions/abook_local_file.php');
47 require_once('../functions/abook_ldap_server.php');
49 // Use this if you wanna have a global address book
50 if (isset($address_book_global_filename))
51 include_once('../functions/abook_global_file.php');
53 // Only load database backend if database is configured
55 if(isset($addrbook_dsn))
56 include_once('../functions/abook_database.php');
59 // Create and initialize an addressbook object.
60 // Returns the created object
61 function addressbook_init($showerr = true, $onlylocal = false) {
62 global $data_dir, $username, $ldap_server, $address_book_global_filename;
65 // Create a new addressbook object
66 $abook = new AddressBook
;
68 // Always add a local backend. We use *either* file-based *or* a
69 // database addressbook. If $addrbook_dsn is set, the database
70 // backend is used. If not, addressbooks are stores in files.
71 if(isset($addrbook_dsn) && !empty($addrbook_dsn)) {
73 $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
75 'table' => 'address'));
77 printf(_("Error initializing addressbook database."));
82 $filename = getHashedFile($username, $data_dir, "$username.abook");
83 $r = $abook->add_backend('local_file', Array('filename' => $filename,
86 printf(_("Error opening file %s"), $filename);
92 // This would be for the global addressbook
93 if (isset($address_book_global_filename)) {
94 $r = $abook->add_backend('global_file');
95 if (!$r && $showerr) {
96 printf(_("Error initializing global addressbook."));
104 // Load configured LDAP servers (if PHP has LDAP support)
105 if(isset($ldap_server) && is_array($ldap_server) &&
106 function_exists('ldap_connect')) {
108 while(list($undef,$param) = each($ldap_server)) {
109 if(is_array($param)) {
110 $r = $abook->add_backend('ldap_server', $param);
111 if(!$r && $showerr) {
112 printf(' ' . _("Error initializing LDAP server %s:") .
113 "<BR>\n", $param['host']);
114 print(' ' . $abook->error
);
121 // Return the initialized object
126 // Had to move this function outside of the Addressbook Class
127 // PHP 4.0.4 Seemed to be having problems with inline functions.
128 function addressbook_cmp($a,$b) {
129 if($a['backend'] > $b['backend'])
131 else if($a['backend'] < $b['backend'])
133 return (strtolower($a['name']) > strtolower($b['name'])) ?
1 : -1;
138 ** This is the main address book class that connect all the
139 ** backends and provide services to the functions above.
144 var $backends = array();
145 var $numbackends = 0;
147 var $localbackend = 0;
148 var $localbackendname = '';
150 // Constructor function.
151 function AddressBook() {
152 $localbackendname = _("Personal address book");
155 // Return an array of backends of a given type,
156 // or all backends if no type is specified.
157 function get_backend_list($type = '') {
159 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
160 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
161 $ret[] = &$this->backends
[$i];
168 // ========================== Public ========================
170 // Add a new backend. $backend is the name of a backend
171 // (without the abook_ prefix), and $param is an optional
172 // mixed variable that is passed to the backend constructor.
173 // See each of the backend classes for valid parameters.
174 function add_backend($backend, $param = '') {
175 $backend_name = 'abook_' . $backend;
176 eval('$newback = new ' . $backend_name . '($param);');
177 if(!empty($newback->error
)) {
178 $this->error
= $newback->error
;
182 $this->numbackends++
;
184 $newback->bnum
= $this->numbackends
;
185 $this->backends
[$this->numbackends
] = $newback;
187 // Store ID of first local backend added
188 if($this->localbackend
== 0 && $newback->btype
== 'local') {
189 $this->localbackend
= $this->numbackends
;
190 $this->localbackendname
= $newback->sname
;
193 return $this->numbackends
;
197 // Return a list of addresses matching expression in
198 // all backends of a given type.
199 function search($expression, $bnum = -1) {
203 // Search all backends
205 $sel = $this->get_backend_list('');
207 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
208 $backend = &$sel[$i];
209 $backend->error
= '';
210 $res = $backend->search($expression);
212 $ret = array_merge($ret, $res);
214 $this->error
.= "<br>\n" . $backend->error
;
219 // Only fail if all backends failed
220 if($failed >= sizeof($sel))
225 // Search only one backend
227 $ret = $this->backends
[$bnum]->search($expression);
228 if(!is_array($ret)) {
229 $this->error
.= "<br>\n" . $this->backends
[$bnum]->error
;
238 // Return a sorted search
239 function s_search($expression, $bnum = -1) {
241 $ret = $this->search($expression, $bnum);
244 usort($ret, 'addressbook_cmp');
249 // Lookup an address by alias. Only possible in
251 function lookup($alias, $bnum = -1) {
255 $res = $this->backends
[$bnum]->lookup($alias);
259 $this->error
= $backend->error
;
264 $sel = $this->get_backend_list('local');
265 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
266 $backend = &$sel[$i];
267 $backend->error
= '';
268 $res = $backend->lookup($alias);
273 $this->error
= $backend->error
;
282 // Return all addresses
283 function list_addr($bnum = -1) {
287 $sel = $this->get_backend_list('local');
289 $sel = array(0 => &$this->backends
[$bnum]);
291 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
292 $backend = &$sel[$i];
293 $backend->error
= '';
294 $res = $backend->list_addr();
296 $ret = array_merge($ret, $res);
298 $this->error
= $backend->error
;
307 // Create a new address from $userdata, in backend $bnum.
308 // Return the backend number that the/ address was added
309 // to, or false if it failed.
310 function add($userdata, $bnum) {
313 if(!is_array($userdata)) {
314 $this->error
= _("Invalid input data");
317 if(empty($userdata['firstname']) &&
318 empty($userdata['lastname'])) {
319 $this->error
= _("Name is missing");
322 if(empty($userdata['email'])) {
323 $this->error
= _("E-mail address is missing");
326 if(empty($userdata['nickname'])) {
327 $userdata['nickname'] = $userdata['email'];
330 if(eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
331 $this->error
= _("Nickname contains illegal characters");
335 // Check that specified backend accept new entries
336 if(!$this->backends
[$bnum]->writeable
) {
337 $this->error
= _("Addressbook is read-only");
341 // Add address to backend
342 $res = $this->backends
[$bnum]->add($userdata);
346 $this->error
= $this->backends
[$bnum]->error
;
350 return false; // Not reached
354 // Remove the user identified by $alias from backend $bnum
355 // If $alias is an array, all users in the array are removed.
356 function remove($alias, $bnum) {
362 // Convert string to single element array
363 if(!is_array($alias))
364 $alias = array(0 => $alias);
366 // Check that specified backend is writable
367 if(!$this->backends
[$bnum]->writeable
) {
368 $this->error
= _("Addressbook is read-only");
372 // Remove user from backend
373 $res = $this->backends
[$bnum]->remove($alias);
377 $this->error
= $this->backends
[$bnum]->error
;
381 return false; // Not reached
385 // Remove the user identified by $alias from backend $bnum
386 // If $alias is an array, all users in the array are removed.
387 function modify($alias, $userdata, $bnum) {
390 if(empty($alias) ||
!is_string($alias))
394 if(!is_array($userdata)) {
395 $this->error
= _("Invalid input data");
398 if(empty($userdata['firstname']) &&
399 empty($userdata['lastname'])) {
400 $this->error
= _("Name is missing");
403 if(empty($userdata['email'])) {
404 $this->error
= _("E-mail address is missing");
408 if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
409 $this->error
= _("Nickname contains illegal characters");
413 if(empty($userdata['nickname'])) {
414 $userdata['nickname'] = $userdata['email'];
417 // Check that specified backend is writable
418 if(!$this->backends
[$bnum]->writeable
) {
419 $this->error
= _("Addressbook is read-only");;
423 // Modify user in backend
424 $res = $this->backends
[$bnum]->modify($alias, $userdata);
428 $this->error
= $this->backends
[$bnum]->error
;
432 return false; // Not reached
436 } // End of class Addressbook
439 ** Generic backend that all other backends extend
441 class addressbook_backend
{
443 // Variables that all backends must provide.
444 var $btype = 'dummy';
445 var $bname = 'dummy';
446 var $sname = 'Dummy backend';
448 // Variables common for all backends, but that
449 // should not be changed by the backends.
452 var $writeable = false;
454 function set_error($string) {
455 $this->error
= '[' . $this->sname
. '] ' . $string;
460 // ========================== Public ========================
462 function search($expression) {
463 $this->set_error('search not implemented');
467 function lookup($alias) {
468 $this->set_error('lookup not implemented');
472 function list_addr() {
473 $this->set_error('list_addr not implemented');
477 function add($userdata) {
478 $this->set_error('add not implemented');
482 function remove($alias) {
483 $this->set_error('delete not implemented');
487 function modify($alias, $newuserdata) {
488 $this->set_error('modify not implemented');