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");
17 // Create and initialize an addressbook object.
18 // Returns the created object
19 function addressbook_init($showerr = true, $onlylocal = false) {
20 global $data_dir, $username, $ldap_server;
22 // Create a new addressbook object
23 $abook = new AddressBook
;
25 // Always add a local backend
26 $filename = sprintf("%s%s.abook", $data_dir, $username);
27 $r = $abook->add_backend("local_file", Array("filename" => $filename,
30 printf(_("Error opening file %s"), $filename);
37 // Load configured LDAP servers (if PHP has LDAP support)
38 if(is_array($ldap_server) && function_exists("ldap_connect")) {
40 while(list($undef,$param) = each($ldap_server)) {
41 if(is_array($param)) {
42 $r = $abook->add_backend("ldap_server", $param);
44 printf(" "._("Error initializing LDAP server %s:").
45 "<BR>\n", $param["host"]);
46 printf(" ".$abook->error
);
53 // Return the initialized object
58 // Had to move this function outside of the Addressbook Class
59 // PHP 4.0.4 Seemed to be having problems with inline functions.
61 if($a["backend"] > $b["backend"])
63 else if($a["backend"] < $b["backend"])
65 return (strtolower($a["name"]) > strtolower($b["name"])) ?
1 : -1;
70 ** This is the main address book class that connect all the
71 ** backends and provide services to the functions above.
76 var $backends = array();
79 var $localbackend = 0;
80 var $localbackendname = "";
82 // Constructor function.
83 function AddressBook() {
84 $localbackendname = _("Personal address book");
87 // Return an array of backends of a given type,
88 // or all backends if no type is specified.
89 function get_backend_list($type = "") {
91 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
92 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
93 array_push($ret, &$this->backends
[$i]);
100 // ========================== Public ========================
102 // Add a new backend. $backend is the name of a backend
103 // (without the abook_ prefix), and $param is an optional
104 // mixed variable that is passed to the backend constructor.
105 // See each of the backend classes for valid parameters.
106 function add_backend($backend, $param = "") {
107 $backend_name = "abook_".$backend;
108 eval("\$newback = new $backend_name(\$param);");
109 if(!empty($newback->error
)) {
110 $this->error
= $newback->error
;
114 $this->numbackends++
;
116 $newback->bnum
= $this->numbackends
;
117 $this->backends
[$this->numbackends
] = $newback;
119 // Store ID of first local backend added
120 if($this->localbackend
== 0 && $newback->btype
== "local") {
121 $this->localbackend
= $this->numbackends
;
122 $this->localbackendname
= $newback->sname
;
125 return $this->numbackends
;
129 // Return a list of addresses matching expression in
130 // all backends of a given type.
131 function search($expression, $bnum = -1) {
135 // Search all backends
137 $sel = $this->get_backend_list("");
139 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
140 $backend = &$sel[$i];
141 $backend->error
= "";
142 $res = $backend->search($expression);
144 $ret = array_merge($ret, $res);
146 $this->error
= $this->error
. "<br>\n". $backend->error
;
151 // Only fail if all backends failed
152 if($failed >= sizeof($sel))
157 // Search only one backend
159 $ret = $this->backends
[$bnum]->search($expression);
160 if(!is_array($ret)) {
161 $this->error
= $this->error
. "<br>\n". $this->backends
[$bnum]->error
;
170 // Return a sorted search
171 function s_search($expression, $bnum = -1) {
173 $ret = $this->search($expression, $bnum);
181 // Lookup an address by alias. Only possible in
183 function lookup($alias, $bnum = -1) {
187 $res = $this->backends
[$bnum]->lookup($alias);
191 $this->error
= $backend->error
;
196 $sel = $this->get_backend_list("local");
197 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
198 $backend = &$sel[$i];
199 $backend->error
= "";
200 $res = $backend->lookup($alias);
205 $this->error
= $backend->error
;
214 // Return all addresses
215 function list_addr($bnum = -1) {
219 $sel = $this->get_backend_list("local");
221 $sel = array(0 => &$this->backends
[$bnum]);
223 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
224 $backend = &$sel[$i];
225 $backend->error
= "";
226 $res = $backend->list_addr();
228 $ret = array_merge($ret, $res);
230 $this->error
= $backend->error
;
239 // Create a new address from $userdata, in backend $bnum.
240 // Return the backend number that the/ address was added
241 // to, or false if it failed.
242 function add($userdata, $bnum) {
245 if(!is_array($userdata)) {
246 $this->error
= _("Invalid input data");
249 if(empty($userdata["firstname"]) &&
250 empty($userdata["lastname"])) {
251 $this->error
= _("Name is missing");
254 if(empty($userdata["email"])) {
255 $this->error
= _("E-mail address is missing");
258 if(empty($userdata["nickname"])) {
259 $userdata["nickname"] = $userdata["email"];
262 if(eregi("[\: \|\#\"\!]", $userdata["nickname"])) {
263 $this->error
= _("Nickname contain illegal characters");
267 // Check that specified backend accept new entries
268 if(!$this->backends
[$bnum]->writeable
) {
269 $this->error
= _("Addressbook is read-only");
273 // Add address to backend
274 $res = $this->backends
[$bnum]->add($userdata);
278 $this->error
= $this->backends
[$bnum]->error
;
282 return false; // Not reached
286 // Remove the user identified by $alias from backend $bnum
287 // If $alias is an array, all users in the array are removed.
288 function remove($alias, $bnum) {
294 // Convert string to single element array
295 if(!is_array($alias))
296 $alias = array(0 => $alias);
298 // Check that specified backend is writable
299 if(!$this->backends
[$bnum]->writeable
) {
300 $this->error
= _("Addressbook is read-only");
304 // Remove user from backend
305 $res = $this->backends
[$bnum]->remove($alias);
309 $this->error
= $this->backends
[$bnum]->error
;
313 return false; // Not reached
317 // Remove the user identified by $alias from backend $bnum
318 // If $alias is an array, all users in the array are removed.
319 function modify($alias, $userdata, $bnum) {
322 if(empty($alias) ||
!is_string($alias))
326 if(!is_array($userdata)) {
327 $this->error
= _("Invalid input data");
330 if(empty($userdata["firstname"]) &&
331 empty($userdata["lastname"])) {
332 $this->error
= _("Name is missing");
335 if(empty($userdata["email"])) {
336 $this->error
= _("E-mail address is missing");
340 if(eregi("[\: \|\#\"\!]", $userdata["nickname"])) {
341 $this->error
= _("Nickname contain illegal characters");
345 if(empty($userdata["nickname"])) {
346 $userdata["nickname"] = $userdata["email"];
349 // Check that specified backend is writable
350 if(!$this->backends
[$bnum]->writeable
) {
351 $this->error
= _("Addressbook is read-only");;
355 // Modify user in backend
356 $res = $this->backends
[$bnum]->modify($alias, $userdata);
360 $this->error
= $this->backends
[$bnum]->error
;
364 return false; // Not reached
368 } // End of class Addressbook
371 ** Generic backend that all other backends extend
373 class addressbook_backend
{
375 // Variables that all backends must provide.
376 var $btype = "dummy";
377 var $bname = "dummy";
378 var $sname = "Dummy backend";
380 // Variables common for all backends, but that
381 // should not be changed by the backends.
384 var $writeable = false;
386 function set_error($string) {
387 $this->error
= "[" . $this->sname
. "] " . $string;
392 // ========================== Public ========================
394 function search($expression) {
395 $this->set_error("search not implemented");
399 function lookup($alias) {
400 $this->set_error("lookup not implemented");
404 function list_addr() {
405 $this->set_error("list_addr not implemented");
409 function add($userdata) {
410 $this->set_error("add not implemented");
414 function remove($alias) {
415 $this->set_error("delete not implemented");
419 function modify($alias, $newuserdata) {
420 $this->set_error("modify not implemented");