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() {
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);
34 // Load configured LDAP servers
36 while(list($key,$param) = each($ldap_server)) {
37 if(is_array($param)) {
38 $r = $abook->add_backend("ldap_server", $param);
40 printf(" "._("Error initializing LDAP server %s:")."<BR>\n",
42 printf(" ".$abook->error
);
48 // Return the initialized object
55 ** This is the main address book class that connect all the
56 ** backends and provide services to the functions above.
60 var $backends = array();
64 // Constructor function.
65 function AddressBook() {
68 // Return an array of backends of a given type,
69 // or all backends if no type is specified.
70 function get_backend_list($type = "") {
72 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
73 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
74 array_push($ret, &$this->backends
[$i]);
81 // ========================== Public ========================
83 // Add a new backend. $backend is the name of a backend
84 // (without the abook_ prefix), and $param is an optional
85 // mixed variable that is passed to the backend constructor.
86 // See each of the backend classes for valid parameters.
87 function add_backend($backend, $param = "") {
88 $backend_name = "abook_".$backend;
89 eval("\$newback = new $backend_name(\$param);");
90 if(!empty($newback->error
)) {
91 $this->error
= $newback->error
;
97 $newback->bnum
= $this->numbackends
;
98 $this->backends
[$this->numbackends
] = $newback;
99 return $this->numbackends
;
103 // Return a list of addresses matching expression in
104 // all backends of a given type.
105 function search($expression, $btype = "") {
108 $sel = $this->get_backend_list($btype);
109 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
110 $backend = &$sel[$i];
111 $backend->error
= "";
112 $res = $backend->search($expression);
114 $ret = array_merge($ret, $res);
116 $this->error
= $backend->error
;
125 // Return a sorted search
126 function s_search($expression, $btype = "") {
127 $ret = $this->search($expression, $btype);
129 // Inline function - Not nice, but still..
130 function cmp($a,$b) {
131 if($a["backend"] > $b["backend"])
133 else if($a["backend"] < $b["backend"])
136 return (strtolower($a["name"]) > strtolower($b["name"])) ?
1 : -1;
144 // Lookup an address by alias. Only possible in
146 function lookup($alias) {
149 $sel = $this->get_backend_list("local");
150 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
151 $backend = &$sel[$i];
152 $backend->error
= "";
153 $res = $backend->lookup($alias);
157 $this->error
= $backend->error
;
166 // Return all addresses
167 function list_addr() {
170 $sel = $this->get_backend_list("local");
171 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
172 $backend = &$sel[$i];
173 $backend->error
= "";
174 $res = $backend->list_addr();
176 $ret = array_merge($ret, $res);
178 $this->error
= $backend->error
;
187 // Create a new address from $userdata, in backend $bnum.
188 // Return the backend number that the/ address was added
189 // to, or false if it failed.
190 function add($userdata, $bnum) {
193 if(!is_array($userdata)) {
194 $this->error
= _("Invalid input data");
197 if(empty($userdata["fullname"]) &&
198 empty($userdata["lastname"])) {
199 $this->error
= _("Name is missing");
202 if(empty($userdata["email"])) {
203 $this->error
= _("E-mail address is missing");
206 if(empty($userdata["nickname"])) {
207 $userdata["nickname"] = $userdata["email"];
210 // Check that specified backend accept new entries
211 if(!$this->backends
[$bnum]->writeable
) {
212 $this->error
= _("Addressbook is read-only");
216 // Add address to backend
217 $res = $this->backends
[$bnum]->add($userdata);
221 $this->error
= $this->backends
[$bnum]->error
;
225 return false; // Not reached
232 ** Generic backend that all other backends extend
234 class addressbook_backend
{
236 // Variables that all backends must provide.
237 var $btype = "dummy";
238 var $bname = "dummy";
239 var $sname = "Dummy backend";
241 // Variables common for all backends, but that
242 // should not be changed by the backends.
245 var $writeable = false;
247 function set_error($string) {
248 $this->error
= "[" . $this->sname
. "] " . $string;
253 // ========================== Public ========================
255 function search($expression) {
256 $this->set_error("search not implemented");
260 function lookup($alias) {
261 $this->set_error("lookup not implemented");
265 function list_addr() {
266 $this->set_error("list_addr not implemented");
270 function add($userdata) {
271 $this->set_error("add not implemented");