738be3ffa7d0d11ecd209a5fd39d65569fc7c9e7
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
38 while(list($key,$param) = each($ldap_server)) {
39 if(is_array($param)) {
40 $r = $abook->add_backend("ldap_server", $param);
42 printf(" "._("Error initializing LDAP server %s:")."<BR>\n",
44 printf(" ".$abook->error
);
50 // Return the initialized object
57 ** This is the main address book class that connect all the
58 ** backends and provide services to the functions above.
62 var $backends = array();
66 // Constructor function.
67 function AddressBook() {
70 // Return an array of backends of a given type,
71 // or all backends if no type is specified.
72 function get_backend_list($type = "") {
74 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
75 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
76 array_push($ret, &$this->backends
[$i]);
83 // ========================== Public ========================
85 // Add a new backend. $backend is the name of a backend
86 // (without the abook_ prefix), and $param is an optional
87 // mixed variable that is passed to the backend constructor.
88 // See each of the backend classes for valid parameters.
89 function add_backend($backend, $param = "") {
90 $backend_name = "abook_".$backend;
91 eval("\$newback = new $backend_name(\$param);");
92 if(!empty($newback->error
)) {
93 $this->error
= $newback->error
;
99 $newback->bnum
= $this->numbackends
;
100 $this->backends
[$this->numbackends
] = $newback;
101 return $this->numbackends
;
105 // Return a list of addresses matching expression in
106 // all backends of a given type.
107 function search($expression, $btype = "") {
111 $sel = $this->get_backend_list($btype);
113 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
114 $backend = &$sel[$i];
115 $backend->error
= "";
116 $res = $backend->search($expression);
118 $ret = array_merge($ret, $res);
120 $this->error
= $this->error
. "<br>\n". $backend->error
;
125 // Only fail if all backends failed
126 if($failed >= sizeof($sel))
133 // Return a sorted search
134 function s_search($expression, $btype = "") {
136 $ret = $this->search($expression, $btype);
140 // Inline function - Not nice, but still..
141 function cmp($a,$b) {
142 if($a["backend"] > $b["backend"])
144 else if($a["backend"] < $b["backend"])
147 return (strtolower($a["name"]) > strtolower($b["name"])) ?
1 : -1;
155 // Lookup an address by alias. Only possible in
157 function lookup($alias) {
160 $sel = $this->get_backend_list("local");
161 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
162 $backend = &$sel[$i];
163 $backend->error
= "";
164 $res = $backend->lookup($alias);
168 $this->error
= $backend->error
;
177 // Return all addresses
178 function list_addr() {
181 $sel = $this->get_backend_list("local");
182 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
183 $backend = &$sel[$i];
184 $backend->error
= "";
185 $res = $backend->list_addr();
187 $ret = array_merge($ret, $res);
189 $this->error
= $backend->error
;
198 // Create a new address from $userdata, in backend $bnum.
199 // Return the backend number that the/ address was added
200 // to, or false if it failed.
201 function add($userdata, $bnum) {
204 if(!is_array($userdata)) {
205 $this->error
= _("Invalid input data");
208 if(empty($userdata["fullname"]) &&
209 empty($userdata["lastname"])) {
210 $this->error
= _("Name is missing");
213 if(empty($userdata["email"])) {
214 $this->error
= _("E-mail address is missing");
217 if(empty($userdata["nickname"])) {
218 $userdata["nickname"] = $userdata["email"];
221 // Check that specified backend accept new entries
222 if(!$this->backends
[$bnum]->writeable
) {
223 $this->error
= _("Addressbook is read-only");
227 // Add address to backend
228 $res = $this->backends
[$bnum]->add($userdata);
232 $this->error
= $this->backends
[$bnum]->error
;
236 return false; // Not reached
243 ** Generic backend that all other backends extend
245 class addressbook_backend
{
247 // Variables that all backends must provide.
248 var $btype = "dummy";
249 var $bname = "dummy";
250 var $sname = "Dummy backend";
252 // Variables common for all backends, but that
253 // should not be changed by the backends.
256 var $writeable = false;
258 function set_error($string) {
259 $this->error
= "[" . $this->sname
. "] " . $string;
264 // ========================== Public ========================
266 function search($expression) {
267 $this->set_error("search not implemented");
271 function lookup($alias) {
272 $this->set_error("lookup not implemented");
276 function list_addr() {
277 $this->set_error("list_addr not implemented");
281 function add($userdata) {
282 $this->set_error("add not implemented");