a8766a7c1fc615a3e2a2df6f56061a443370a383
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();
65 var $localbackend = 0;
67 // Constructor function.
68 function AddressBook() {
71 // Return an array of backends of a given type,
72 // or all backends if no type is specified.
73 function get_backend_list($type = "") {
75 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
76 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
77 array_push($ret, &$this->backends
[$i]);
84 // ========================== Public ========================
86 // Add a new backend. $backend is the name of a backend
87 // (without the abook_ prefix), and $param is an optional
88 // mixed variable that is passed to the backend constructor.
89 // See each of the backend classes for valid parameters.
90 function add_backend($backend, $param = "") {
91 $backend_name = "abook_".$backend;
92 eval("\$newback = new $backend_name(\$param);");
93 if(!empty($newback->error
)) {
94 $this->error
= $newback->error
;
100 $newback->bnum
= $this->numbackends
;
101 $this->backends
[$this->numbackends
] = $newback;
103 // Store ID of first local backend added
104 if($this->localbackend
== 0 && $newback->btype
== "local")
105 $this->localbackend
= $this->numbackends
;
107 return $this->numbackends
;
111 // Return a list of addresses matching expression in
112 // all backends of a given type.
113 function search($expression, $btype = "") {
117 $sel = $this->get_backend_list($btype);
119 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
120 $backend = &$sel[$i];
121 $backend->error
= "";
122 $res = $backend->search($expression);
124 $ret = array_merge($ret, $res);
126 $this->error
= $this->error
. "<br>\n". $backend->error
;
131 // Only fail if all backends failed
132 if($failed >= sizeof($sel))
139 // Return a sorted search
140 function s_search($expression, $btype = "") {
142 $ret = $this->search($expression, $btype);
146 // Inline function - Not nice, but still..
147 function cmp($a,$b) {
148 if($a["backend"] > $b["backend"])
150 else if($a["backend"] < $b["backend"])
153 return (strtolower($a["name"]) > strtolower($b["name"])) ?
1 : -1;
161 // Lookup an address by alias. Only possible in
163 function lookup($alias) {
166 $sel = $this->get_backend_list("local");
167 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
168 $backend = &$sel[$i];
169 $backend->error
= "";
170 $res = $backend->lookup($alias);
174 $this->error
= $backend->error
;
183 // Return all addresses
184 function list_addr() {
187 $sel = $this->get_backend_list("local");
188 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
189 $backend = &$sel[$i];
190 $backend->error
= "";
191 $res = $backend->list_addr();
193 $ret = array_merge($ret, $res);
195 $this->error
= $backend->error
;
204 // Create a new address from $userdata, in backend $bnum.
205 // Return the backend number that the/ address was added
206 // to, or false if it failed.
207 function add($userdata, $bnum) {
210 if(!is_array($userdata)) {
211 $this->error
= _("Invalid input data");
214 if(empty($userdata["firstname"]) &&
215 empty($userdata["lastname"])) {
216 $this->error
= _("Name is missing");
219 if(empty($userdata["email"])) {
220 $this->error
= _("E-mail address is missing");
223 if(empty($userdata["nickname"])) {
224 $userdata["nickname"] = $userdata["email"];
227 // Check that specified backend accept new entries
228 if(!$this->backends
[$bnum]->writeable
) {
229 $this->error
= _("Addressbook is read-only");
233 // Add address to backend
234 $res = $this->backends
[$bnum]->add($userdata);
238 $this->error
= $this->backends
[$bnum]->error
;
242 return false; // Not reached
249 ** Generic backend that all other backends extend
251 class addressbook_backend
{
253 // Variables that all backends must provide.
254 var $btype = "dummy";
255 var $bname = "dummy";
256 var $sname = "Dummy backend";
258 // Variables common for all backends, but that
259 // should not be changed by the backends.
262 var $writeable = false;
264 function set_error($string) {
265 $this->error
= "[" . $this->sname
. "] " . $string;
270 // ========================== Public ========================
272 function search($expression) {
273 $this->set_error("search not implemented");
277 function lookup($alias) {
278 $this->set_error("lookup not implemented");
282 function list_addr() {
283 $this->set_error("list_addr not implemented");
287 function add($userdata) {
288 $this->set_error("add not implemented");