c57fdb241ce81c6f24a6d9a5b6b4de4eccb4f5e8
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))
38 $abook->add_backend("ldap_server", $param);
40 // Return the initialized object
47 ** This is the main address book class that connect all the
48 ** backends and provide services to the functions above.
52 var $backends = array();
56 // Constructor function.
57 function AddressBook() {
60 // Return an array of backends of a given type,
61 // or all backends if no type is specified.
62 function get_backend_list($type = "") {
64 for($i = 1 ; $i <= $this->numbackends
; $i++
) {
65 if(empty($type) ||
$type == $this->backends
[$i]->btype
) {
66 array_push($ret, &$this->backends
[$i]);
73 // ========================== Public ========================
75 // Add a new backend. $backend is the name of a backend
76 // (without the abook_ prefix), and $param is an optional
77 // mixed variable that is passed to the backend constructor.
78 // See each of the backend classes for valid parameters.
79 function add_backend($backend, $param = "") {
80 $backend_name = "abook_".$backend;
81 eval("\$newback = new $backend_name(\$param);");
82 if(!empty($newback->error
)) {
83 $this->error
= $newback->error
;
89 $newback->bnum
= $this->numbackends
;
90 $this->backends
[$this->numbackends
] = $newback;
91 return $this->numbackends
;
95 // Return a list of addresses matching expression in
96 // all backends of a given type.
97 function search($expression, $btype = "") {
100 $sel = $this->get_backend_list($btype);
101 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
102 $backend = &$sel[$i];
103 $backend->error
= "";
104 $res = $backend->search($expression);
106 $ret = array_merge($ret, $res);
108 $this->error
= $backend->error
;
117 // Return a sorted search
118 function s_search($expression, $btype = "") {
119 $ret = $this->search($expression, $btype);
121 // Inline function - Not nice, but still..
122 function cmp($a,$b) {
123 if($a["backend"] > $b["backend"])
125 else if($a["backend"] < $b["backend"])
128 return (strtolower($a["name"]) > strtolower($b["name"])) ?
1 : -1;
136 // Lookup an address by alias. Only possible in
138 function lookup($alias) {
141 $sel = $this->get_backend_list("local");
142 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
143 $backend = &$sel[$i];
144 $backend->error
= "";
145 $res = $backend->lookup($alias);
149 $this->error
= $backend->error
;
158 // Return all addresses
159 function list_addr() {
162 $sel = $this->get_backend_list("local");
163 for($i = 0 ; $i < sizeof($sel) ; $i++
) {
164 $backend = &$sel[$i];
165 $backend->error
= "";
166 $res = $backend->list_addr();
168 $ret = array_merge($ret, $res);
170 $this->error
= $backend->error
;
179 // Create a new address from $userdata, in backend $bnum.
180 // Return the backend number that the/ address was added
181 // to, or false if it failed.
182 function add($userdata, $bnum) {
185 if(!is_array($userdata)) {
186 $this->error
= _("Invalid input data");
189 if(empty($userdata["fullname"]) &&
190 empty($userdata["lastname"])) {
191 $this->error
= _("Name is missing");
194 if(empty($userdata["email"])) {
195 $this->error
= _("E-mail address is missing");
198 if(empty($userdata["nickname"])) {
199 $userdata["nickname"] = $userdata["email"];
202 // Check that specified backend accept new entries
203 if(!$this->backends
[$bnum]->writeable
) {
204 $this->error
= _("Addressbook is read-only");
208 // Add address to backend
209 $res = $this->backends
[$bnum]->add($userdata);
213 $this->error
= $this->backends
[$bnum]->error
;
217 return false; // Not reached
224 ** Generic backend that all other backends extend
226 class addressbook_backend
{
228 // Variables that all backends must provide.
229 var $btype = "dummy";
230 var $bname = "dummy";
231 var $sname = "Dummy backend";
233 // Variables common for all backends, but that
234 // should not be changed by the backends.
237 var $writeable = false;
239 function set_error($string) {
240 $this->error
= "[" . $this->sname
. "] " . $string;
245 // ========================== Public ========================
247 function search($expression) {
248 $this->set_error("search not implemented");
252 function lookup($alias) {
253 $this->set_error("lookup not implemented");
257 function list_addr() {
258 $this->set_error("list_addr not implemented");
262 function add($userdata) {
263 $this->set_error("add not implemented");