595507cc428b398dc434970942d96903aadd8e32
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 (if PHP has LDAP support)
37 if(is_array($ldap_server) && function_exists("ldap_connect")) {
39 while(list($undef,$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, $bnum = -1) {
123 // Search all backends
125 $sel = $this->get_backend_list("");
127 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
128 $backend = &$sel[$i];
129 $backend->error
= "";
130 $res = $backend->search($expression);
132 $ret = array_merge($ret, $res);
134 $this->error
= $this->error
. "<br>\n". $backend->error
;
139 // Only fail if all backends failed
140 if($failed >= sizeof($sel))
145 // Search only one backend
147 $ret = $this->backends
[$bnum]->search($expression);
148 if(!is_array($ret)) {
149 $this->error
= $this->error
. "<br>\n". $this->backends
[$bnum]->error
;
158 // Return a sorted search
159 function s_search($expression, $bnum = -1) {
161 $ret = $this->search($expression, $bnum);
165 // Inline function - Not nice, but still..
166 function cmp($a,$b) {
167 if($a["backend"] > $b["backend"])
169 else if($a["backend"] < $b["backend"])
172 return (strtolower($a["name"]) > strtolower($b["name"])) ?
1 : -1;
180 // Lookup an address by alias. Only possible in
182 function lookup($alias, $bnum = -1) {
186 $res = $this->backends
[$bnum]->lookup($alias);
190 $this->error
= $backend->error
;
195 $sel = $this->get_backend_list("local");
196 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
197 $backend = &$sel[$i];
198 $backend->error
= "";
199 $res = $backend->lookup($alias);
204 $this->error
= $backend->error
;
213 // Return all addresses
214 function list_addr($bnum = -1) {
218 $sel = $this->get_backend_list("local");
220 $sel = array(0 => &$this->backends
[$bnum]);
222 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
223 $backend = &$sel[$i];
224 $backend->error
= "";
225 $res = $backend->list_addr();
227 $ret = array_merge($ret, $res);
229 $this->error
= $backend->error
;
238 // Create a new address from $userdata, in backend $bnum.
239 // Return the backend number that the/ address was added
240 // to, or false if it failed.
241 function add($userdata, $bnum) {
244 if(!is_array($userdata)) {
245 $this->error
= _("Invalid input data");
248 if(empty($userdata["firstname"]) &&
249 empty($userdata["lastname"])) {
250 $this->error
= _("Name is missing");
253 if(empty($userdata["email"])) {
254 $this->error
= _("E-mail address is missing");
257 if(empty($userdata["nickname"])) {
258 $userdata["nickname"] = $userdata["email"];
261 if(eregi("[\: \|\#\"\!]", $userdata["nickname"])) {
262 $this->error
= _("Nickname contain illegal characters");
266 // Check that specified backend accept new entries
267 if(!$this->backends
[$bnum]->writeable
) {
268 $this->error
= _("Addressbook is read-only");
272 // Add address to backend
273 $res = $this->backends
[$bnum]->add($userdata);
277 $this->error
= $this->backends
[$bnum]->error
;
281 return false; // Not reached
285 // Remove the user identified by $alias from backend $bnum
286 // If $alias is an array, all users in the array are removed.
287 function remove($alias, $bnum) {
293 // Convert string to single element array
294 if(!is_array($alias))
295 $alias = array(0 => $alias);
297 // Check that specified backend is writable
298 if(!$this->backends
[$bnum]->writeable
) {
299 $this->error
= _("Addressbook is read-only");
303 // Remove user from backend
304 $res = $this->backends
[$bnum]->remove($alias);
308 $this->error
= $this->backends
[$bnum]->error
;
312 return false; // Not reached
316 // Remove the user identified by $alias from backend $bnum
317 // If $alias is an array, all users in the array are removed.
318 function modify($alias, $userdata, $bnum) {
321 if(empty($alias) ||
!is_string($alias))
325 if(!is_array($userdata)) {
326 $this->error
= _("Invalid input data");
329 if(empty($userdata["firstname"]) &&
330 empty($userdata["lastname"])) {
331 $this->error
= _("Name is missing");
334 if(empty($userdata["email"])) {
335 $this->error
= _("E-mail address is missing");
338 if(empty($userdata["nickname"])) {
339 $userdata["nickname"] = $userdata["email"];
342 // Check that specified backend is writable
343 if(!$this->backends
[$bnum]->writeable
) {
344 $this->error
= sprintf(_("Addressbook %s is read-only", $bnum));
348 // Modify user in backend
349 $res = $this->backends
[$bnum]->modify($alias, $userdata);
353 $this->error
= $this->backends
[$bnum]->error
;
357 return false; // Not reached
360 } // End of class Addressbook
364 ** Generic backend that all other backends extend
366 class addressbook_backend
{
368 // Variables that all backends must provide.
369 var $btype = "dummy";
370 var $bname = "dummy";
371 var $sname = "Dummy backend";
373 // Variables common for all backends, but that
374 // should not be changed by the backends.
377 var $writeable = false;
379 function set_error($string) {
380 $this->error
= "[" . $this->sname
. "] " . $string;
385 // ========================== Public ========================
387 function search($expression) {
388 $this->set_error("search not implemented");
392 function lookup($alias) {
393 $this->set_error("lookup not implemented");
397 function list_addr() {
398 $this->set_error("list_addr not implemented");
402 function add($userdata) {
403 $this->set_error("add not implemented");
407 function remove($alias) {
408 $this->set_error("delete not implemented");
412 function modify($alias, $newuserdata) {
413 $this->set_error("modify not implemented");