Added addressbook+LDAP functions.
[squirrelmail.git] / functions / addressbook.php
CommitLineData
5100704d 1<?php
2
3 /**
4 ** addressbook.php
5 **
6 ** Functions and classes for the addressbook system.
7 **
8 **/
9
10 $addressbook_php = true;
11
12 // Include backends here.
13 include("../functions/abook_local_file.php");
14 include("../functions/abook_ldap_server.php");
15
16 // Create and initialize an addressbook object.
17 // Returns the created object
18 function addressbook_init() {
19 global $data_dir, $username, $ldap_server;
20
21 // Create a new addressbook object
22 $abook = new AddressBook;
23
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,
27 "create" => true));
28 if(!$r) {
29 print _("Error opening ") ."$filename";
30 exit;
31 }
32
33
34 // Load configured LDAP servers
35 reset($ldap_server);
36 while(list($key,$param) = each($ldap_server))
37 if(is_array($param))
38 $abook->add_backend("ldap_server", $param);
39
40 // Return the initialized object
41 return $abook;
42 }
43
44
45
46 /**
47 ** This is the main address book class that connect all the
48 ** backends and provide services to the functions above.
49 **
50 **/
51 class AddressBook {
52 var $backends = array();
53 var $numbackends = 0;
54 var $error = "";
55
56 // Constructor function.
57 function AddressBook() {
58 }
59
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 = "") {
63 $ret = array();
64 for($i = 1 ; $i <= $this->numbackends ; $i++) {
65 if(empty($type) || $type == $this->backends[$i]->btype) {
66 array_push($ret, &$this->backends[$i]);
67 }
68 }
69 return $ret;
70 }
71
72
73 // ========================== Public ========================
74
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;
84 return false;
85 }
86
87 $this->numbackends++;
88
89 $newback->bnum = $this->numbackends;
90 $this->backends[$this->numbackends] = $newback;
91 return $this->numbackends;
92 }
93
94
95 // Return a list of addresses matching expression in
96 // all backends of a given type.
97 function search($expression, $btype = "") {
98 $ret = array();
99
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);
105 if(is_array($res)) {
106 $ret = array_merge($ret, $res);
107 } else {
108 $this->error = $backend->error;
109 return false;
110 }
111 }
112
113 return $ret;
114 }
115
116
117 // Return a sorted search
118 function s_search($expression, $btype = "") {
119 $ret = $this->search($expression, $btype);
120
121 // Inline function - Not nice, but still..
122 function cmp($a,$b) {
123 if($a["backend"] > $b["backend"])
124 return 1;
125 else if($a["backend"] < $b["backend"])
126 return -1;
127
128 return (strtolower($a["name"]) > strtolower($b["name"])) ? 1 : -1;
129 }
130
131 usort($ret, 'cmp');
132 return $ret;
133 }
134
135
136 // Lookup an address by alias. Only possible in
137 // local backends.
138 function lookup($alias) {
139 $ret = array();
140
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);
146 if(is_array($res)) {
147 return $res;
148 } else {
149 $this->error = $backend->error;
150 return false;
151 }
152 }
153
154 return $ret;
155 }
156
157
158 // Return all addresses
159 function list_addr() {
160 $ret = array();
161
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();
167 if(is_array($res)) {
168 $ret = array_merge($ret, $res);
169 } else {
170 $this->error = $backend->error;
171 return false;
172 }
173 }
174
175 return $ret;
176 }
177
178
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) {
183
184 // Validate data
185 if(!is_array($userdata)) {
186 $this->error = _("Invalid input data");
187 return false;
188 }
189 if(empty($userdata["fullname"]) &&
190 empty($userdata["lastname"])) {
191 $this->error = _("Name is missing");
192 return false;
193 }
194 if(empty($userdata["email"])) {
195 $this->error = _("E-mail address is missing");
196 return false;
197 }
198 if(empty($userdata["nickname"])) {
199 $userdata["nickname"] = $userdata["email"];
200 }
201
202 // Check that specified backend accept new entries
203 if(!$this->backends[$bnum]->writeable) {
204 $this->error = _("Addressbook is not writable");
205 return false;
206 }
207
208 // Add address to backend
209 $res = $this->backends[$bnum]->add($userdata);
210 if($res) {
211 return $bnum;
212 } else {
213 $this->error = $this->backends[$bnum]->error;
214 return false;
215 }
216
217 return false; // Not reached
218 }
219
220 }
221
222
223 /**
224 ** Generic backend that all other backends extend
225 **/
226 class addressbook_backend {
227
228 // Variables that all backends must provide.
229 var $btype = "dummy";
230 var $bname = "dummy";
231 var $sname = "Dummy backend";
232
233 // Variables common for all backends, but that
234 // should not be changed by the backends.
235 var $bnum = -1;
236 var $error = "";
237 var $writeable = false;
238
239 function set_error($string) {
240 $this->error = "[" . $this->sname . "] " . $string;
241 return false;
242 }
243
244
245 // ========================== Public ========================
246
247 function search($expression) {
248 $this->set_error("search not implemented");
249 return false;
250 }
251
252 function lookup($alias) {
253 $this->set_error("lookup not implemented");
254 return false;
255 }
256
257 function list_addr() {
258 $this->set_error("list_addr not implemented");
259 return false;
260 }
261
262 function add($userdata) {
263 $this->set_error("add not implemented");
264 return false;
265 }
266
267 }
268
269?>