6 ** Functions and classes for the addressbook system.
10 $addressbook_php = true;
12 // Include backends here.
13 include("../functions/abook_local_file.php");
14 include("../functions/abook_ldap_server.php");
16 // Create and initialize an addressbook object.
17 // Returns the created object
18 function addressbook_init($showerr = true, $onlylocal = false) {
19 global $data_dir, $username, $ldap_server;
21 // Create a new addressbook object
22 $abook = new AddressBook
;
24 // Always add a local backend
25 $filename = sprintf("%s%s.abook", $data_dir, $username);
26 $r = $abook->add_backend("local_file", Array("filename" => $filename,
29 printf(_("Error opening file %s"), $filename);
36 // Load configured LDAP servers
37 if(is_array($ldap_server)) {
39 while(list($key,$param) = each($ldap_server)) {
40 if(is_array($param)) {
41 $r = $abook->add_backend("ldap_server", $param);
43 printf(" "._("Error initializing LDAP server %s:").
44 "<BR>\n", $param["host"]);
45 printf(" ".$abook->error
);
52 // Return the initialized object
59 ** This is the main address book class that connect all the
60 ** backends and provide services to the functions above.
64 var $backends = array();
67 var $localbackend = 0;
68 var $localbackendname = "";
70 // Constructor function.
71 function AddressBook() {
72 $localbackendname = _("Personal address book");
75 // Return an array of backends of a given type,
76 // or all backends if no type is specified.
77 function get_backend_list($type = "") {
79 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
80 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
81 array_push($ret, &$this->backends
[$i]);
88 // ========================== Public ========================
90 // Add a new backend. $backend is the name of a backend
91 // (without the abook_ prefix), and $param is an optional
92 // mixed variable that is passed to the backend constructor.
93 // See each of the backend classes for valid parameters.
94 function add_backend($backend, $param = "") {
95 $backend_name = "abook_".$backend;
96 eval("\$newback = new $backend_name(\$param);");
97 if(!empty($newback->error
)) {
98 $this->error
= $newback->error
;
102 $this->numbackends++
;
104 $newback->bnum
= $this->numbackends
;
105 $this->backends
[$this->numbackends
] = $newback;
107 // Store ID of first local backend added
108 if($this->localbackend
== 0 && $newback->btype
== "local") {
109 $this->localbackend
= $this->numbackends
;
110 $this->localbackendname
= $newback->sname
;
113 return $this->numbackends
;
117 // Return a list of addresses matching expression in
118 // all backends of a given type.
119 function search($expression, $btype = "") {
123 $sel = $this->get_backend_list($btype);
125 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
126 $backend = &$sel[$i];
127 $backend->error
= "";
128 $res = $backend->search($expression);
130 $ret = array_merge($ret, $res);
132 $this->error
= $this->error
. "<br>\n". $backend->error
;
137 // Only fail if all backends failed
138 if($failed >= sizeof($sel))
145 // Return a sorted search
146 function s_search($expression, $btype = "") {
148 $ret = $this->search($expression, $btype);
152 // Inline function - Not nice, but still..
153 function cmp($a,$b) {
154 if($a["backend"] > $b["backend"])
156 else if($a["backend"] < $b["backend"])
159 return (strtolower($a["name"]) > strtolower($b["name"])) ?
1 : -1;
167 // Lookup an address by alias. Only possible in
169 function lookup($alias, $bnum = -1) {
173 $res = $this->backends
[$bnum]->lookup($alias);
177 $this->error
= $backend->error
;
182 $sel = $this->get_backend_list("local");
183 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
184 $backend = &$sel[$i];
185 $backend->error
= "";
186 $res = $backend->lookup($alias);
191 $this->error
= $backend->error
;
200 // Return all addresses
201 function list_addr() {
204 $sel = $this->get_backend_list("local");
205 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
206 $backend = &$sel[$i];
207 $backend->error
= "";
208 $res = $backend->list_addr();
210 $ret = array_merge($ret, $res);
212 $this->error
= $backend->error
;
221 // Create a new address from $userdata, in backend $bnum.
222 // Return the backend number that the/ address was added
223 // to, or false if it failed.
224 function add($userdata, $bnum) {
227 if(!is_array($userdata)) {
228 $this->error
= _("Invalid input data");
231 if(empty($userdata["firstname"]) &&
232 empty($userdata["lastname"])) {
233 $this->error
= _("Name is missing");
236 if(empty($userdata["email"])) {
237 $this->error
= _("E-mail address is missing");
240 if(empty($userdata["nickname"])) {
241 $userdata["nickname"] = $userdata["email"];
244 if(eregi("[\: \|\#\"\!]", $userdata["nickname"])) {
245 $this->error
= _("Nickname contain illegal characters");
249 // Check that specified backend accept new entries
250 if(!$this->backends
[$bnum]->writeable
) {
251 $this->error
= _("Addressbook is read-only");
255 // Add address to backend
256 $res = $this->backends
[$bnum]->add($userdata);
260 $this->error
= $this->backends
[$bnum]->error
;
264 return false; // Not reached
268 // Remove the user identified by $alias from backend $bnum
269 // If $alias is an array, all users in the array are removed.
270 function remove($alias, $bnum) {
276 // Convert string to single element array
277 if(!is_array($alias))
278 $alias = array(0 => $alias);
280 // Check that specified backend is writable
281 if(!$this->backends
[$bnum]->writeable
) {
282 $this->error
= _("Addressbook is read-only");
286 // Remove user from backend
287 $res = $this->backends
[$bnum]->remove($alias);
291 $this->error
= $this->backends
[$bnum]->error
;
295 return false; // Not reached
299 // Remove the user identified by $alias from backend $bnum
300 // If $alias is an array, all users in the array are removed.
301 function modify($alias, $userdata, $bnum) {
304 if(empty($alias) ||
!is_string($alias))
308 if(!is_array($userdata)) {
309 $this->error
= _("Invalid input data");
312 if(empty($userdata["firstname"]) &&
313 empty($userdata["lastname"])) {
314 $this->error
= _("Name is missing");
317 if(empty($userdata["email"])) {
318 $this->error
= _("E-mail address is missing");
321 if(empty($userdata["nickname"])) {
322 $userdata["nickname"] = $userdata["email"];
325 // Check that specified backend is writable
326 if(!$this->backends
[$bnum]->writeable
) {
327 $this->error
= _("Addressbook $bnum is read-only");
331 // Modify user in backend
332 $res = $this->backends
[$bnum]->modify($alias, $userdata);
336 $this->error
= $this->backends
[$bnum]->error
;
340 return false; // Not reached
343 } // End of class Addressbook
347 ** Generic backend that all other backends extend
349 class addressbook_backend
{
351 // Variables that all backends must provide.
352 var $btype = "dummy";
353 var $bname = "dummy";
354 var $sname = "Dummy backend";
356 // Variables common for all backends, but that
357 // should not be changed by the backends.
360 var $writeable = false;
362 function set_error($string) {
363 $this->error
= "[" . $this->sname
. "] " . $string;
368 // ========================== Public ========================
370 function search($expression) {
371 $this->set_error("search not implemented");
375 function lookup($alias) {
376 $this->set_error("lookup not implemented");
380 function list_addr() {
381 $this->set_error("list_addr not implemented");
385 function add($userdata) {
386 $this->set_error("add not implemented");
390 function remove($alias) {
391 $this->set_error("delete not implemented");
395 function modify($alias, $newuserdata) {
396 $this->set_error("modify not implemented");